caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Dario Teixeira <darioteixeira@yahoo.com>
To: Gabriel Scherer <gabriel.scherer@gmail.com>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] GADTs and parsers
Date: Sun, 15 Jul 2012 11:58:40 -0700 (PDT)	[thread overview]
Message-ID: <1342378720.56667.YahooMailNeo@web111507.mail.gq1.yahoo.com> (raw)
In-Reply-To: <CAPFanBEJ6jf3qzDGdy6kt3Hy7yaYjAb6D+6U1woXMQXMipArUg@mail.gmail.com>

Hi,

And thanks for the prompt reply!

> I suspect the problem may be one of variance: you are using

> polymorphic variants, but implicitly assuming that ([ `Nolink ]
> inline_t) is a subtype of ([ `Nolink | `Link ] inline_t), which means
> that "inline_t" is covariant in its first parameter. However, variance
> of GADTs is currently very strict, and your inline_t definition is in
> fact considered invariant (to check that, replace "type _ inline_t" by
> "type (+'a) inline_t" and watch the error), so this conversion does
> not pass. On the contrary, with a phantom type definition, the
> parameter is inferred to be irrelevant (any variance is accepted), so
> you can do whatever you want inside the abstraction boundary.

Ah, indeed.  And on a related note: is there a way to get the compiler
to spill out the variance it infers?  I'm assuming that 'ocamlc -i' does
not include inferred variance because it is generally "obvious" and
not something you usually have to worry about unless you want to
make a type abstract.  However, in a case like this it would help.
(Or does the compiler *always* assume invariance with GADTs?)


> (For more information on GADTs and variance, see the previous thread
> on this on the mailing-list; the summary is that it's complicated, but
> I still hope we could get something finer in the future. It would
> probably not work with polymorphic variant instances, anyway.)

In this case the use of polymorphic variants for the type parameter comes
in handy because there is a subtyping relation and the [> ] declaration
allows for useful mixing:

# let x1 = Text "ola";;
val x1 : [> `Nonlink ] inline_t = Text "ola"

# let x2 = Link "lnk";;
val x2 : [> `Link ] inline_t = Link "lnk"

# let xs = [x1; x2];;
val xs : [> `Link | `Nonlink ] inline_t list = [Text "ola"; Link "lnk"]

Cheers,
Dario

  reply	other threads:[~2012-07-15 18:58 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-15 16:54 Dario Teixeira
2012-07-15 17:43 ` Gabriel Scherer
2012-07-15 18:58   ` Dario Teixeira [this message]
2012-07-15 20:37     ` Gabriel Scherer
2012-07-15 19:55 ` Leo P White
2012-07-16 14:45   ` Dario Teixeira
2012-07-15 22:41 ` Jacques Garrigue
2012-07-16  8:50   ` Leo P White
2012-07-16 10:06     ` Leo P White
2012-07-16 15:12   ` Dario Teixeira

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=1342378720.56667.YahooMailNeo@web111507.mail.gq1.yahoo.com \
    --to=darioteixeira@yahoo.com \
    --cc=caml-list@inria.fr \
    --cc=gabriel.scherer@gmail.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).