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 OAA17929; Thu, 29 Apr 2004 14:40:38 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id OAA17899 for ; Thu, 29 Apr 2004 14:40:36 +0200 (MET DST) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from mwinf0404.wanadoo.fr (smtp4.wanadoo.fr [193.252.22.27]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i3TCeZYM001508 for ; Thu, 29 Apr 2004 14:40:36 +0200 Received: from vanicat.homelinux.org (ca-bordeaux-30-69.w80-8.abo.wanadoo.fr [80.8.200.69]) by mwinf0404.wanadoo.fr (SMTP Server) with ESMTP id 96582380025A for ; Thu, 29 Apr 2004 14:40:35 +0200 (CEST) Received: from moi by vanicat.homelinux.org with local (Exim 4.32) id 1BJAqD-0001OZ-WC for caml-list@inria.fr; Thu, 29 Apr 2004 14:40:38 +0200 To: caml-list@inria.fr Subject: Re: [Caml-list] Documentation error - #myvariant References: From: Remi Vanicat Mail-Copy-To: never Date: Thu, 29 Apr 2004 14:40:37 +0200 In-Reply-To: (Keith Wansbrough's message of "Thu, 29 Apr 2004 12:18:54 +0100") Message-ID: <8765bjvtx6.dlv@vanicat.homelinux.org> User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: 8bit X-Miltered: at concorde by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 or-patterns:01 rewrote:01 testlabl:01 runtime:01 mixin:01 bool:01 bool:01 ocaml:01 ocaml:01 variants:01 int:01 int:01 rec:01 rec:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Keith Wansbrough writes: > Hi.. the OCaml documentation, section 4.2 on polymorphic variants, > Advanced use, explains that > > To make this even more confortable, you may use type definitions as > abbreviations for or-patterns. That is, if you have defined type > myvariant = [`Tag1 int | `Tag2 bool], then the pattern #myvariant is > equivalent to writing (`Tag1(_ : int) | `Tag2(_ : bool)). > > But this is not correct! Consider > > type de = [`D | `E of de];; > > type def = [`D | `E of def | `F of def];; > > let rec deproc2 rfun = > function > | `D -> print_string "D"; `D > | `E(x) -> print_string "E"; `E(rfun x) > > let rec deproc3 x = deproc2 deproc3 x > > let rec defproc2 rfun = > function > (* | (`D | `E(_)) as x -> deproc2 rfun x *) > | #de as x -> deproc2 rfun x > | `F(x) -> print_string "f"; `F(rfun x) If I read the documentation, this is rewrote as let rec defproc2 rfun = function | (`D | `E(_:de)) as x -> deproc2 rfun x | `F(x) -> print_string "f"; `F(rfun x) that mean that what is in the `E must be a de, not a def. There is your error. In fact, your code will do what you want if you don't define your type as recursive but as polymorphic : type 'a de = [`D | `E of 'a];; type 'a def = [`D | `E of 'a | `F of 'a];; then, your code will work as expected. Well, the last definition became : let rec defproc3 : ('a def as 'a) -> 'a = fun x -> defproc2 defproc3 x There is a very interesting example about this in the ocaml source : ocaml/testlabl/mixin.ml [...] > as expected. It looks to me like #de means (`D | `E(_:de)), rather > than (`D | `E(_)) as I expected; except that I'm not even sure what > `E(_:de) means in this case - does it do type-directed matching at > runtime? no, it is a constraint on the type of what is the `E done at compile time. And it is the source of your error because a de is not a def. > > Could the documentation please be made more accurate at this point? > The documentation is accurate. -- Rémi Vanicat ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners