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] opaque polymorphism
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-09-14 (19:10)
From: Brian Rogoff <bpr@b...>
Subject: Re: [Caml-list] Re: Phantom types (very long)
On Fri, 14 Sep 2001, Jacques Garrigue wrote:
> > This "phantom types" design pattern is one I have never seen before.  It
> > doesn't seem to be used in the standard library anywhere I can see.  It
> > looks like it might be useful in presenting a safer network programming
> > interface than the low-level wrappers around BSD sockets (which I've
> > never liked).
> Thay are used in both Bigarray and Labltk (the 'a widget type). In
> fact they were already used in Labltk before the word phantom started
> to be used for them.

Right, and I think that each new use causes some of the newer ML (and
Haskell, Mercury too?) programmers to do a few double takes. I know the
first time I saw this usage on this mailing list I was a bit boggled.
In retrospect each new usage seems relatively obvious, but it wouldn't
hurt to have this (and the parameterization trick, and the trick that
Tyng-Ruey Chuang uses to write polynomial data types, and a whole bunch of
others ...) explained simply for the practitioner.

> > Are there any other mind-blowingly elegant design patterns lurking in
> > the corners of the Caml type inference engine that I should know about?
> I don't know if this is mind blowing, but thanks to variance
> annotations on abstract types, you can also have subtyping on
> "phantom" types. This is used in lablgtk for instance:
> type (-'a) gtkobj
> type widget = [`widget]
> type container = [`widget|`container]
> type box = [`widget|`container|`box]
> now you can cast safely between classes:
>     (mybox : box gtkobj :> container gtkobj)
> Note that the parameter must be contravariant if you use polymorphic
> variant types as indexes, or covariant if you rather use object types.
> This also works ok with polymorphism, encoding inheritance (even
> multiple) in a direct way.
> val add : [> `container] gtkobj -> [> `widget] gtkobj -> unit

What a great mailing list! Thanks for that one. I hope to see it in the
lablgtk docs soon.

-- Brian

Bug reports:  FAQ:
To unsubscribe, mail  Archives: