caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: nalexander@amavi.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Naming polymorphic variant types -- additional questions
Date: Thu, 22 Aug 2002 23:26:30 +0900	[thread overview]
Message-ID: <20020822232630X.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <1066.207.194.7.55.1029979596.squirrel@mail.gx.ca>

From: "Nick Alexander" <nalexander@amavi.com>
> When I was trying this, I accidently tried:
> type outer = [< 'A | 'B]
> and was duly rewarded with
> Unbound type parameter [..]

OK, [< t] and [> t] types contain an hidden type variable.
If you really want to define a polymorphic type, you must write

   type 'a outer = 'a constraint 'a = [< `A | `B]

Looks a bit confusing? This just means that ('a outer) can unify with
anything included in [< `A | `B]

> I am also a neophyte polymorphic variants user.  It seems very intuitive
> to me that [`A] is a subtype of [`A | `B].  Jacques informs me that
> unification rejects the subtyping in favour of equality.  So, what is the
> situation where [`A] is _not_ a subtype of [`A | `B]?  Ie, what common
> construct, design pattern, or idiom reflects this?

[`A] _is_ a subtype of [`A | `B]. The point is just that in ocaml
subtyping is not implicit, like it is in most OO languages.
     fun (x : [`A]) -> (x :> [`A|`B]) ,
where :> denotes an explicit coercion, is typable, showing the
subtyping relation.

In ocaml, the implicit subsumption relation is instanciation rather
than subtyping. While subtyping makes function arguments
contravariant, with instanciation everything is "covariant", which has
some advantages, but clearly doesn't mix well with subtyping.

Polymorphic variants are based on instanciation: a polymorphic variant
type can be seen as a constraint on the values that may go through
this type. Unification refines it.
Another small point is that in ML quantification only occurs at
toplevel. This means that [< `A | `B] itself is not a closed type, and
that you cannot close it easily.  As a result, you cannot compare it
to [`A], other than say they are unifiable. On the other hand, if you
have a function whose type is All('a). ([< `A | `B] as 'a) -> t  (this
is the meaning of "val f : [< `A | `B] -> t") and another function
whose type is [`A] -> t (here there is nothing to quantify), then you
can see that the first one will accept more inputs, and as a result is
more general then the second one.

Not very clear, but this is the rough idea.

Jacques Garrigue
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


      reply	other threads:[~2002-08-22 14:26 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-21  4:22 [Caml-list] Naming polymorphic variant types Nick Alexander
2002-08-21  5:27 ` Jacques Garrigue
2002-08-21 16:02   ` Nick Alexander
2002-08-21 23:50     ` Jacques Garrigue
2002-08-22  1:26   ` [Caml-list] Naming polymorphic variant types -- additional questions Nick Alexander
2002-08-22 14:26     ` Jacques Garrigue [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=20020822232630X.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=nalexander@amavi.com \
    /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).