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 LAA32249; Mon, 23 Aug 2004 11:53:37 +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 LAA32367 for ; Mon, 23 Aug 2004 11:53:36 +0200 (MET DST) Received: from ptb-relay02.plus.net (ptb-relay02.plus.net [212.159.14.213]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i7N9rZmL025803 for ; Mon, 23 Aug 2004 11:53:35 +0200 Received: from [80.229.56.224] (helo=chetara) by ptb-relay02.plus.net with esmtp (Exim) id 1BzBWA-0007ZA-O8 for caml-list@inria.fr; Mon, 23 Aug 2004 09:53:34 +0000 From: Jon Harrop To: caml-list@inria.fr Subject: Re: [Caml-list] Phantom types Date: Mon, 23 Aug 2004 10:49:59 +0100 User-Agent: KMail/1.6.2 References: <410B5EBD.6060800@cgorski.org> <200407311444.56864.jon@jdh30.plus.com> <20040731163143.GB15775@fichte.ai.univie.ac.at> In-Reply-To: <20040731163143.GB15775@fichte.ai.univie.ac.at> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200408231049.59342.jon@jdh30.plus.com> X-Miltered: at nez-perce with ID 4129BE9F.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 2004:99 markus':01 val:01 val:01 struct:01 failwith:01 failwith:01 abstr:01 abstr:01 monomorphic:01 phantom:01 phantom:01 sig:01 sig:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Saturday 31 July 2004 17:31, Markus Mottl wrote: > module type PHANTOM_INT = sig > type 'p t > ... Right, I've had a bit more of a chance to play with phantom types now, and I'm quite confused. :-) As far as I can tell, there were a few errors in Markus' original (I may well be wrong, of course), so here is my altered version: module PhantomInt : sig type 'p t val add : 'p t -> 'p t -> 'p t val add_even_even : [ `Even ] t -> [ `Even ] t -> [ `Even ] t val add_even_odd : [ `Even ] t -> [ `Odd ] t -> [ `Odd ] t val add_odd_even : [ `Odd ] t -> [ `Even ] t -> [ `Odd ] t val add_odd_odd : [ `Odd ] t -> [ `Odd ] t -> [ `Even ] t val neg : 'a t -> 'a t val make_even : int -> [ `Even ] t val make_odd : int -> [ `Odd ] t end = struct type 'p t = int let add = (+) let add_even_even = add let add_even_odd = add let add_odd_even = add let add_odd_odd = add let neg n = -n let make_even n = if n mod 2 = 0 then n else failwith "not even" let make_odd n = if n mod 2 <> 0 then n else failwith "not odd" end;; So I've changed the types to be [ `Even ] instead of [> `Even ] and the "make" functions to be "int -> ...". This appear to work as desired: # let i = PhantomInt.make_even 2;; val i : [ `Even ] PhantomInt.t = # let j = PhantomInt.make_odd 3;; val j : [ `Odd ] PhantomInt.t = # PhantomInt.add_even_odd i j;; - : [ `Odd ] PhantomInt.t = # PhantomInt.add_even_even i j;; This expression has type [ `Odd ] PhantomInt.t but is here used with type [ `Even ] PhantomInt.t These two variant types have no intersection Now, there are some subtle peculiarities of these which I don't understand. Firstly, the type checking of the phantom types only seems to work if the type is made abstract in the module signature. I can't think why this should make a difference though. For example, changing "type 'p t" to "type 'p t = int" in "PhantomInt : sig" then allows: # PhantomInt.add_even_even i j;; - : [ `Even ] PhantomInt.t = 5 which is clearly undesirable. Secondly, specifying the types as Markus did (e.g. [> `Even]), which I think should have been correct, leads to some kind of monomorphic type: # PhantomInt.add_even_odd i j;; - : _[> `Odd ] PhantomInt.t = Note the "_" preceding the "[> `Odd]". I'm not sure what the implications of this are. Can someone explain these to me, please? Cheers, Jon. PS: I'm using 3.08.0, in case that makes a difference. ------------------- 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