caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Rogoff <bpr@best.com>
To: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
Cc: <caml-list@inria.fr>
Subject: Re: [Caml-list] Re: Phantom types (very long)
Date: Fri, 14 Sep 2001 12:10:08 -0700 (PDT)	[thread overview]
Message-ID: <20010914115849.O2695-100000@shell5.ba.best.com> (raw)
In-Reply-To: <20010914174922Y.garrigue@kurims.kyoto-u.ac.jp>

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


      reply	other threads:[~2001-09-14 19:10 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-09-07 18:35 [Caml-list] opaque polymorphism Charles Martin
2001-09-10  7:02 ` Francois Pottier
2001-09-10 23:19   ` Phantom types (very long) (Was Re: [Caml-list] opaque polymorphism) Brian Rogoff
2001-09-11  9:44     ` Andreas Rossberg
2001-09-11 18:38     ` [Caml-list] Re: Phantom types (very long) j h woodyatt
2001-09-11 19:16       ` Brian Rogoff
2001-09-12  9:33       ` Daan Leijen
2001-09-14  8:49       ` Jacques Garrigue
2001-09-14 19:10         ` Brian Rogoff [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20010914115849.O2695-100000@shell5.ba.best.com \
    --to=bpr@best.com \
    --cc=caml-list@inria.fr \
    --cc=garrigue@kurims.kyoto-u.ac.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).