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 RAA21662; Mon, 1 Mar 2004 17:35:35 +0100 (MET) 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 RAA21651 for ; Mon, 1 Mar 2004 17:35:34 +0100 (MET) Received: from pob.cs.unibo.it (pob.cs.unibo.it [130.136.10.114]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i21GZfIq004925 for ; Mon, 1 Mar 2004 17:35:42 +0100 Received: from le1.cs.unibo.it (backle.cs.unibo.it [130.136.10.110]) by pob.cs.unibo.it (Postfix) with ESMTP id 440252A1EA for ; Mon, 1 Mar 2004 17:35:33 +0100 (CET) Received: from localhost.cs.unibo.it (liu.cs.unibo.it [130.136.7.125]) by le1.cs.unibo.it (8.9.3p2/8.9.3/Debian 8.9.3-21) with ESMTP id RAA19167 for ; Mon, 1 Mar 2004 17:35:33 +0100 X-Authentication-Warning: le1.cs.unibo.it: Host liu.cs.unibo.it [130.136.7.125] claimed to be localhost.cs.unibo.it Received: by localhost.cs.unibo.it (Postfix, from userid 2662) id 0E1892ACD5; Mon, 1 Mar 2004 17:35:32 +0100 (CET) Date: Mon, 1 Mar 2004 17:35:32 +0100 From: Claudio Sacerdoti Coen To: caml-list@inria.fr Subject: Re: [Caml-list] Getting rid of impossible polymorphic variant tags from infered types Message-ID: <20040301163532.GA7661@cs.unibo.it> References: <4043520E.2040802@bik-gmbh.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4043520E.2040802@bik-gmbh.de> User-Agent: Mutt/1.3.28i X-Operating-System: Debian GNU/Linux X-Organization: Department of computer science, University of Bologna, Eurpean Union X-Miltered: at nez-perce by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; sacerdoti:01 coen:01 sacerdot:01 caml-list:01 foo:01 foo:01 val:01 extensible:01 val:01 sacerdoti:01 coen:01 sacerdot:01 variants:01 phantom:01 polymorphic:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 7 > type foo = [ `Foo | `Bar | `Fnord ] > type foo_flat = [ `Foo | `Bar ] > > let f' l = > let f_r (v: foo) (init: foo_flat list) = > match v with > | `Fnord -> init > | _ -> Obj.magic v :: init > in > List.fold_right f_r l [] There is no need for Obj.magic: type foo_flat = [ `Foo | `Bar ] type foo = [ foo_flat | `Fnord ] let f_r (v : foo) (init : foo_flat list) = match v with `Fnord -> init | #foo_flat as v -> v :: init ;; type foo = [ `Bar | `Fnord | `Foo ] type foo_flat = [ `Bar | `Foo ] val f_r : foo -> foo_flat list -> foo_flat list Since the list `Foo | `Bar of variants is written only once, the code is also reasonably extensible. Of course it is still not what you want. If you really need to exclude the `Fnord value at compile time, an alternative solution is to use phantom types: type 'a foo = [ `Foo | `Bar | `Fnord ] let f_r (v : 'a foo) (init : ([> `NoFnord] foo) list) = match v with `Fnord -> init | _ -> v :: init ;; type 'a foo = [ `Bar | `Fnord | `Foo ] val f_r : 'a foo -> ([> `NoFnord ] as 'b) foo list -> 'b foo list The `NoFnord variant explicitly tells that the Fnord value is not allowed. Notice how the first parameter does not have to satisfy this restriction, while the second one has to. As usual, you need to use modules to enforce that a value of type `Foo has type [`NoFnord] foo whereas a value of type `NoFnord has NOT type [`NoFnord]. Regards, C.S.C. -- ---------------------------------------------------------------- Real name: Claudio Sacerdoti Coen PhD Student in Computer Science at University of Bologna E-mail: sacerdot@cs.unibo.it http://www.cs.unibo.it/~sacerdot ---------------------------------------------------------------- ------------------- 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