Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] const equivalent for mutable types?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-08-25 (21:03)
From: brogoff <brogoff@s...>
Subject: Re: [Caml-list] Phantom types
On Mon, 23 Aug 2004, Jon Harrop wrote:

> 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:


Yes, those look like errors, and your fixes look good.

> 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:

that's what you want in this example.

> 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.

This looks like a bug in the OCaml type checker. It also occurs if you replace
the polymorphic variants with type even = Even and odd = Odd. It remains if
you replace the phantom type int by int64 or even int array, but goes away when
you replace the phantom type by  type 'p t = { data = int } or
type 'p t = Int int. So the built in types appear to be being treated

> 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 = <abstr>

A [> or [< means there's an implicit type variable.

-- Brian

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: