caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Martin DeMello <martindemello@gmail.com>
To: Jeremy Yallop <yallop@gmail.com>
Cc: OCaml List <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] "this ground coercion is not principal"
Date: Wed, 25 Feb 2015 13:35:48 -0800	[thread overview]
Message-ID: <CAFrFfuHAF06-m5NWA6aEMRsTjjPGcvWOs8ri=eKtjewAutEmBQ@mail.gmail.com> (raw)
In-Reply-To: <CAAxsn=EAaNQ3DszD4bsHt4=A0vmshm28W3omtfk685+_S0i25w@mail.gmail.com>

Thanks, that explained everything very nicely!

I'm still curious about where the term "ground coercion" came from,
but I understand what the warning is saying now.

martin

On Wed, Feb 25, 2015 at 2:38 AM, Jeremy Yallop <yallop@gmail.com> wrote:
> On 25 February 2015 at 04:47, Martin DeMello <martindemello@gmail.com> wrote:
>> What does this warning mean?
>
> [tl;dr: the message means "The type of the expression is not known.
> Add type annotations for the variables in the expression."]
>
> Background: a private type abbreviation is defined by a type alias
> definition with the word 'private'.  For example, the following
> definition
>
>    type t = private int
>
> makes t a kind of half alias for int: you can convert from type t to
> int, but you can't convert from int to t.  Coercions are performed
> with the ':>' operator, so you can write things like this
>
>    let f (x : t) = (x :>  int)
>
> to convert from the private type t to the abbreviated type int.
>
> Now, in order to check whether the following coercion is valid
>
>    (x :> int)
>
> the compiler needs to know the type of x.  There might be several
> candidates: for example, with the private type abbreviation above in
> scope, the coercion is valid if x has type t, but do-nothing coercions
> are also allowed, so int is another reasonable possibility.  How can
> the compiler choose between these alternatives to find the type of x?
> In the definition of f above choosing is easy: x is a function
> argument with an annotation, so the compiler just uses that
> annotation.  Here's a slightly trickier case:
>
>    let g (y : t) = ()
>
>    let h x = (g x, (x :> int))
>
> What's the type of x here?  The compiler's inference algorithm checks
> the elements of a pair from left to right, so here's what happens:
>
>   (1) Initially, when type checking for h starts, the type of x is unknown
>   (2) The subexpression g x is checked, assigning x the type t, i.e.
> the type of g's argument
>   (3) The coercion (x :> int) is checked, and determined to be correct
> since t can be coerced to int.
>
> However, if the inference algorithm instead checked the elements of a
> pair from right to left we'd have the following sequence of steps:
>
>   (1) Initially, when type checking for h starts, the type of x is unknown
>   (2) The coercion (x :> int) is checked, and the compiler guesses the
> type of x.  In the absence of other information it guesses int.
>   (3) The subexpression g x is checked and rejected, because x has
> type int, not t.
>
> Indeed, if we exchange the elements of the pair to simulate this
> second behaviour
>
>    let h2 x = ((x :> int), g x)
>
> then the coercion is rejected:
>
>     let h x = ((x :> int), g x);;
>                              ^
>     Error: This expression has type int but an expression was expected of type t
>
> Since it's better for programs not to depend on the particular order
> used by the inference algorithm, the compiler emits a warning.  You
> can address the warning by annotating the binding for x:
>
>    let h (x : t) = (g x, (x :> int))
>
> Jeremy.

  reply	other threads:[~2015-02-25 21:35 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-25  4:47 Martin DeMello
2015-02-25  9:29 ` Jeremie Dimino
2015-02-25 10:38 ` Jeremy Yallop
2015-02-25 21:35   ` Martin DeMello [this message]
2015-02-25 23:04     ` Jeremy Yallop
  -- strict thread matches above, loose matches on Subject: below --
2013-01-10 20:15 [Caml-list] This ground coercion is not principal? bob zhang
2013-01-10 21:06 ` Jeremie Dimino
2013-01-10 21:12   ` bob zhang

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='CAFrFfuHAF06-m5NWA6aEMRsTjjPGcvWOs8ri=eKtjewAutEmBQ@mail.gmail.com' \
    --to=martindemello@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=yallop@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).