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 VAA26087; Fri, 14 Sep 2001 21:10:15 +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 VAA08003 for ; Fri, 14 Sep 2001 21:10:14 +0200 (MET DST) Received: from shell5.ba.best.com (shell5.ba.best.com [206.184.139.136]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f8EJADf10739 for ; Fri, 14 Sep 2001 21:10:13 +0200 (MET DST) Received: from localhost (bpr@localhost) by shell5.ba.best.com (8.9.3/8.9.2/best.sh) with ESMTP id MAA11657; Fri, 14 Sep 2001 12:10:09 -0700 (PDT) Date: Fri, 14 Sep 2001 12:10:08 -0700 (PDT) From: Brian Rogoff To: Jacques Garrigue cc: Subject: Re: [Caml-list] Re: Phantom types (very long) In-Reply-To: <20010914174922Y.garrigue@kurims.kyoto-u.ac.jp> Message-ID: <20010914115849.O2695-100000@shell5.ba.best.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr