From: reig@dcs.gla.ac.uk
To: caml-list@inria.fr
Cc: Michel.Mauny@inria.fr
Subject: Re: [Caml-list] variant with tuple arg in pattern match?
Date: Tue, 10 Apr 2001 11:44:02 +0100 (BST) [thread overview]
Message-ID: <200104101044.LAA10626@crab.dcs.gla.ac.uk> (raw)
In-Reply-To: <20010410102323.A13306@quincy.inria.fr> from "Michel Mauny" at Apr 10, 2001 10:23:23 AM
Michel Mauny wrote:
> Well, as far as I understand, there is an interaction with
> pattern-matching. Consider the following example:
>
> type 'a t =
> Int : int -> int t
> | Bool : bool -> bool t
> | Node : 'a t -> 'a t -> 'a t
>
> This definition implies that data constructors Int and Bool cannot
> appear in the same tree (no way to be at the same time an int t, and a
> bool t, unless being an 'a t, which cannot occur at all).
> [...]
This is how you do it in haskell:
data T a = I Int
| B Bool
| N (T a) (T a)
| Uncurried (Int,Int)
hugs (a haskell interpreter) reports this:
Main> :i T
-- type constructor
data T a
-- constructors:
I :: Int -> T a
B :: Bool -> T a
N :: T a -> T a -> T a
Uncurried :: (Int,Int) -> T a
Of course, you can have trees with bools and ints:
Main> N (I 1) (B True)
N (I 1) (B True) :: T a
Notice that I has type Int -> T a, not Int -> T Int
The equivalent in ocaml, using Pierre's new syntax, would be:
type 'a t =
Int : int -> 'a t
| Bool : bool -> 'a t
| Node : 'a t -> 'a t -> 'a t
| Uncurried : int * int -> 'a t
Moreover, the last part (-> 'a t) is the same for all constructors and
can be omitted like you do in haskell (so that Xavier is happier :)
type 'a t =
Int : int
| Bool : bool
| Node : 'a t -> 'a t
| Uncurried : int * int
[I assume there would be no type checking problems for ocaml, but I
don't know for sure]
One other thing changes too: data constructors can be partially
applied in haskell. Ex:
Main> :t N (I 1)
N (I 1) :: T a -> T a
Main> map I [1,2,3]
[I 1,I 2,I 3] :: [T a]
[Don't need to eta expand (\x -> I x)]
Personally, I quite like this feature of haskell compared to ML.
Fermin Reig
-------------------
To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr
next prev parent reply other threads:[~2001-04-10 10:44 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-04-04 11:04 Chris Hecker
2001-04-04 18:47 ` Alain Frisch
2001-04-04 19:18 ` Patrick M Doane
2001-04-04 19:36 ` Chris Hecker
2001-04-04 19:49 ` Daniel de Rauglaudre
2001-04-05 8:19 ` Christian RINDERKNECHT
2001-04-04 19:49 ` Patrick M Doane
2001-04-06 13:52 ` Xavier Leroy
2001-04-07 1:42 ` Patrick M Doane
2001-04-07 6:44 ` Daniel de Rauglaudre
2001-04-07 7:42 ` Fergus Henderson
2001-04-08 19:45 ` Pierre Weis
2001-04-08 20:37 ` Charles Martin
2001-04-08 23:57 ` Brian Rogoff
2001-04-09 0:22 ` Alain Frisch
2001-04-09 16:07 ` Pierre Weis
2001-04-10 8:23 ` Michel Mauny
2001-04-10 9:14 ` Xavier Leroy
2001-04-10 10:09 ` Michel Mauny
2001-04-10 10:44 ` reig [this message]
2001-04-10 11:32 ` Michel Mauny
2001-04-10 11:47 ` reig
2001-04-10 12:10 ` reig
2001-04-10 12:35 ` Michel Mauny
2001-04-10 12:49 ` Marcin 'Qrczak' Kowalczyk
2001-04-09 6:23 ` Mattias Waldau
2001-04-09 7:34 ` Daniel de Rauglaudre
2001-04-09 15:57 ` Pierre Weis
2001-04-10 9:07 ` Sven LUTHER
2001-04-09 8:20 ` Christian RINDERKNECHT
2001-04-10 2:54 ` Patrick M Doane
2001-04-10 19:04 ` John Max Skaller
2001-04-08 0:22 jgm
2001-04-10 12:17 Dave Berry
2001-04-10 13:12 ` Marcin 'Qrczak' Kowalczyk
2001-04-10 21:26 ` Bruce Hoult
2001-04-10 22:34 ` John Prevost
2001-04-10 13:51 ` Frank Atanassow
2001-04-10 17:25 Dave Berry
2001-04-10 23:16 ` Marcin 'Qrczak' Kowalczyk
2001-04-10 17:33 Dave Berry
2001-04-10 22:34 ` John Prevost
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=200104101044.LAA10626@crab.dcs.gla.ac.uk \
--to=reig@dcs.gla.ac.uk \
--cc=Michel.Mauny@inria.fr \
--cc=caml-list@inria.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