From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id MAA21759; Tue, 10 Apr 2001 12:44:10 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id MAA21755 for ; Tue, 10 Apr 2001 12:44:09 +0200 (MET DST) Received: from iona.dcs.gla.ac.uk (iona.dcs.gla.ac.uk [130.209.240.35]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f3AAi4n05075; Tue, 10 Apr 2001 12:44:04 +0200 (MET DST) Received: from crab.dcs.gla.ac.uk ([130.209.241.150]) by iona.dcs.gla.ac.uk with esmtp (Exim 3.13 #1) id 14mvcx-0003HK-00; Tue, 10 Apr 2001 11:44:03 +0100 Received: by crab.dcs.gla.ac.uk (8.8.8+Sun/Dumb) id LAA10626; Tue, 10 Apr 2001 11:44:02 +0100 (BST) From: reig@dcs.gla.ac.uk Message-Id: <200104101044.LAA10626@crab.dcs.gla.ac.uk> Subject: Re: [Caml-list] variant with tuple arg in pattern match? To: caml-list@inria.fr Date: Tue, 10 Apr 2001 11:44:02 +0100 (BST) Cc: Michel.Mauny@inria.fr In-Reply-To: <20010410102323.A13306@quincy.inria.fr> from "Michel Mauny" at Apr 10, 2001 10:23:23 AM X-Mailer: ELM [version 2.5 PL3] MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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