From: Christophe TROESTLER <Christophe.Troestler+ocaml@umh.ac.be>
To: dthierbach@gmx.de
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Induction over types
Date: Fri, 01 Feb 2008 22:31:23 +0100 (CET) [thread overview]
Message-ID: <20080201.223123.562109690294574114.Christophe.Troestler+ocaml@umh.ac.be> (raw)
In-Reply-To: <20080131214336.GA11358@feanor>
On Thu, 31 Jan 2008 22:43:36 +0100, Dirk Thierbach wrote:
>
> type 'a deep_trie = Inner of 'a | Nest of ('a list) deep_trie
>
> But to deal with this type, we need what is called polymorphic
> recursion -- inside a recursive definition, the recursive function
> must still have the quantifiers, so it can be used for a different
> type. This can only work if the type is known in advance, and the
> only way to encode this into OCaml at the moment is with a record-type.
Actually, a nice way to do the same and which is much more readable
(at heart, it is the very same solution) is to use recursive modules
(their semantic are not completely fixed but I guess the following
should work even in the future):
module rec Deep : sig
type 'a trie = Inner of 'a | Nest of ('a list) trie
val map : ('a -> 'b) -> 'a trie -> 'b trie
val rev_map : ('a -> 'b) -> 'a trie -> 'b trie
end = struct
type 'a trie = Inner of 'a | Nest of ('a list) trie
let map f = function
| Inner x -> Inner(f x)
| Nest l -> Nest(Deep.map (List.map f) l)
let rev_map f = function
| Inner x -> Inner(f x)
| Nest l -> Nest(Deep.map (List.rev_map f) l)
end;;
open Deep;;
> let y = Nest (Nest (Inner [[1;2]; [3;4]]))
# Deep.rev_map (fun x -> x) y;;
- : int Deep.trie = Nest (Nest (Inner [[4; 3]; [2; 1]]))
My 0.02€,
ChriS
prev parent reply other threads:[~2008-02-01 21:31 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-01-31 17:38 Dawid Toton
2008-01-31 18:18 ` [Caml-list] " Christopher L Conway
2008-01-31 21:43 ` Dirk Thierbach
2008-02-01 21:31 ` Christophe TROESTLER [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20080201.223123.562109690294574114.Christophe.Troestler+ocaml@umh.ac.be \
--to=christophe.troestler+ocaml@umh.ac.be \
--cc=caml-list@yquem.inria.fr \
--cc=dthierbach@gmx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox