From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: serge.leblanc@orange.fr
Cc: nicolas.pouillard@gmail.com, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] revised syntax for abstract types ?
Date: Fri, 11 Dec 2009 09:14:56 +0900 (JST) [thread overview]
Message-ID: <20091211.091456.04442319.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <1260468628.22959.510.camel@serge2.localnet>
From: Serge Leblanc <serge.leblanc@orange.fr>
> In the following types definitions,
>
> type trie 'a = [ Trie of arcs 'a ]
> and arcs 'a = list ('a * trie 'a);
>
> type zipper 'a = [ Top | Zip of (arcs 'a * 'a * arcs 'a * zipper 'a) ]
> and edit_state 'a = (zipper 'a * trie 'a);
>
> why is it not possible to describe them thus ?
>
> type letter = 'a;
> type trie = [ Trie of arcs ]
> and arcs = list (letter * trie);
>
> type zipper = [ Top | Zip of (arcs * letter * arcs * zipper) ]
> and edit_state = (zipper * trie);
Note first that revised syntax is just syntax, it does not change the
semantics. So, translating your question on a simpler example in
standard syntax, how does
type 'a list = Nil | Cons of 'a * 'a list
relate to
type elt
type list = Nil | Cons of elt * list
The answer is that they describe the same data, but in an incompatible
way. The first approach uses ML polymorphism, so that you can build a
list of any given type, letting the type checker choose the element
type.
The second is a signature, and should be used in combination with
functors, the type being chosen explicitly. For instance, you can
write a map function in the following way:
module type List = sig
type elt
type list = Nil | Cons of elt * list
end
module F(T:List) = struct
open T
let rec map f = function
Nil -> Nil
| Cons (h,t) -> Cons (f h, map f t)
end
module IntList = struct
type elt = int
type list = Nil | Cons of elt * list
end
module IntM = F(IntList);;
IntM.map succ (IntList.Cons (1, IntList.Nil));;
Again, these two definitions of list, while representing the same data,
are incompatible.
Hope this helps.
Jacques Garrigue
prev parent reply other threads:[~2009-12-11 0:18 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-12-10 11:56 Serge Leblanc
2009-12-10 13:49 ` [Caml-list] " Nicolas Pouillard
2009-12-10 14:29 ` Stefano Zacchiroli
2009-12-10 18:10 ` Serge Leblanc
2009-12-11 0:14 ` Jacques Garrigue [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=20091211.091456.04442319.garrigue@math.nagoya-u.ac.jp \
--to=garrigue@math.nagoya-u.ac.jp \
--cc=caml-list@yquem.inria.fr \
--cc=nicolas.pouillard@gmail.com \
--cc=serge.leblanc@orange.fr \
/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