caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <yallop@gmail.com>
To: Reed Wilson <cedilla@gmail.com>
Cc: Leo White <leo@lpw25.net>, Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Type generalization confusion
Date: Tue, 9 May 2017 21:54:27 +0100	[thread overview]
Message-ID: <CAAxsn=EgzfTjHWF_xM7KnggEoA0kfT9MV+ttw9NwQ8R4LJTwHg@mail.gmail.com> (raw)
In-Reply-To: <CALLFq5S5pFRtE2Rat5KAwgbmrXpfix5CxxhSFP_3u=CKF1rVtQ@mail.gmail.com>

On 9 May 2017 at 20:56, Reed Wilson <cedilla@gmail.com> wrote:
>   type (_,_) field =
>   | Int8 : ('a,'b) field -> ((int -> 'a),'b) field
>   | String : (int * ('a,'b) field) -> ((string -> 'a),'b) field
>   | End : ('b,'b) field
[...]
> No number of "+" or "-" in my type parameters fix the issue as they did with
> the example in my previous e-mail. Everything returns:
>   Error: In this GADT definition, the variance of some parameter cannot be
> checked

Indeed, soundness requires the parameters of 'field' to be invariant,
so there's no possibility of adding variance annotations here.

For example, the type of the End constructor makes the following
definition well typed:

  let x : (< m : int >, < m : int >) field = End

If one of the parameters (the first, say) were covariant, then that
parameter could be coerced to a supertype of < m:int >, like this

  let y = (x :> ( < >, < m : int >) field)

and then matching against y would introduce an invalid equality
between < > and < m: int >:

  match y with
    End ->
      (* Here the type checker knows by the type of End that the
         two type parameters are equal, i.e. that < > is equal to < m : int > *)
    let x = (object end) in x#m + 1

A similar argument can be made against making either parameter
contravariant.  The parameters of 'field' are therefore invariant, and
so the relaxed value restriction doesn't apply.

> The current issue I'm actually facing is trying to create a lookup table for
> these templates, which only differ by the length of some of the fields. For
> example:
>
>   let lookup_array = [| String (0, End); String (1, End) |]

The value restriction prevents generalization here, since arrays are
mutable.  If bindings of array expressions were generalized then the
following program, which writes to the array at one type (int option)
and reads from the array at a different type (string option), would be
allowed.

   let array = [| None |] in                   (* val array : 'a option array *)
   let () = array.(0) <- Some 3 in
   let Some x = array.(0) in                (* val x : 'a *)
   "abc" ^ x

So it's essential for soundness that the types of array values are not
generalized.

  reply	other threads:[~2017-05-09 20:54 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-08 18:32 Reed Wilson
2017-05-09  3:49 ` Gabriel Scherer
2017-05-09  5:43   ` Reed Wilson
2017-05-09  8:54     ` Leo White
2017-05-09 19:56       ` Reed Wilson
2017-05-09 20:54         ` Jeremy Yallop [this message]
2017-05-10 18:14           ` Reed Wilson
2017-05-10 13:29         ` Mikhail Mandrykin

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='CAAxsn=EgzfTjHWF_xM7KnggEoA0kfT9MV+ttw9NwQ8R4LJTwHg@mail.gmail.com' \
    --to=yallop@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=cedilla@gmail.com \
    --cc=leo@lpw25.net \
    /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).