caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Erick Tryzelaar" <idadesub@users.sourceforge.net>
To: "OCaml List" <caml-list@yquem.inria.fr>
Subject: help with open vs closed polymorphic variants
Date: Sat, 15 Mar 2008 21:15:09 -0700	[thread overview]
Message-ID: <1ef034530803152115n27d005d4kf7a136309bc90f9d@mail.gmail.com> (raw)

I'm not sure if this is the right term for describing polymorphic
variants, but it seems to me that there are two ways to work with a
complex hierarchy of polymorphic types to get something that's similar
to inheritance. First is to use what I call closed variants, where you
build the tree bottom up like this:

type foo2 = [`Foo2];;
type foo = [foo2 | `Foo];;
type bar = [`Bar];;
type baz = [`Baz];;
type value = [foo|bar|baz];;

The open variants are the other way around:

type value = [`value];;
type foo = [value|`Foo];;
type bar = [value|`Bar];;
type baz = [value|`Baz];;
type foo2 = [foo|`Foo2];;

These both let you call a function on a subset of variants. For closed
variants, you can do:

type 'a t = T of 'a;;
let f (x:[< foo] t) = ();;
f (T (`Foo :> foo));;
f (T (`Foo2 :> foo2));;

but this is a type error: "f (T (`Bar :> bar))". Likewise with open variants:

type 'a t = T of 'a;;
let f (x:[> `Foo] t) = ();;
f (T (`Foo :> foo));;
f (T (`Foo2 :> foo2));;

With "f (T (`Bar :> bar))" being an error as well. However, say we
wanted to allow for a function that works on two subsets of variant
tree. I can do this with closed variants:

let f (x:[< foo|bar] t) = ();;
f (T (`Foo :> foo));;
f (T (`Foo2 :> foo2));;
f (T (`Bar :> bar));;

But now "f (T (`Baz :> baz))" is a type error. However, I can't figure
out if there's an equivalent with open variants. The naive solution
doesn't work:

# let f (x:[< `Foo|`Bar] t) = ();;
# f (make_foo ());;
This expression has type foo t but is here used with type ([< `Foo ] as 'a) t
Type foo = [ `Foo | `value ] is not compatible with type 'a
The second variant type does not allow tag(s) `value

It doesn't even type the first call. Is this impossible?


                 reply	other threads:[~2008-03-16  4:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1ef034530803152115n27d005d4kf7a136309bc90f9d@mail.gmail.com \
    --to=idadesub@users.sourceforge.net \
    --cc=caml-list@yquem.inria.fr \
    /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).