From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Dario Teixeira <darioteixeira@yahoo.com>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] GADTs and parsers
Date: Sun, 15 Jul 2012 13:37:10 -0700 [thread overview]
Message-ID: <CAPFanBG0oyyps6pPPsGakHu7a4ZNazrHRQ0d-qfxCCwb2GN6DQ@mail.gmail.com> (raw)
In-Reply-To: <1342378720.56667.YahooMailNeo@web111507.mail.gq1.yahoo.com>
> 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
Precisely, your example does *not* use subtyping (it's easy to see: in
OCaml, subtyping is explicit, you have to use (e :> t) casts. If there
are none, you're not using subtyping). [> `Link ] is actually a
shortcut for something like [ `Link | 'r ], where 'r is a polymorphic
row variable that stands for any number of additional cases. So going
from [> `Link ] to [> `Link | `Nolink ] is not subtyping, only
instantiation of 'r into (`Nolink | 'r2) for a fresh 'r2.
So it may be the case that your previous example also could work with
the parameter invariant. I haven't looked at it deeply enough to tell.
On Sun, Jul 15, 2012 at 11:58 AM, Dario Teixeira
<darioteixeira@yahoo.com> wrote:
> 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
next prev parent reply other threads:[~2012-07-15 20:37 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
2012-07-15 20:37 ` Gabriel Scherer [this message]
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=CAPFanBG0oyyps6pPPsGakHu7a4ZNazrHRQ0d-qfxCCwb2GN6DQ@mail.gmail.com \
--to=gabriel.scherer@gmail.com \
--cc=caml-list@inria.fr \
--cc=darioteixeira@yahoo.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