caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Leo White <lpw25@cam.ac.uk>
Cc: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>,
	OCaML List Mailing <caml-list@inria.fr>
Subject: Re: [Caml-list] Request for feedback: A problem with injectivity and GADTs
Date: Tue, 30 Apr 2013 13:30:49 +0200	[thread overview]
Message-ID: <CAPFanBEntKG-thQk2w2XHSYr7X7wCqyEzibQBEdqUqr_XxQGVg@mail.gmail.com> (raw)
In-Reply-To: <8738u8ba3j.fsf@kingston.cl.cam.ac.uk>

Indeed, the (=) in my message above where in the context of the work
where it is define as ((<=) and (>=)), so in a sense "equality in the
sense of subtyping rather than unification". Sorry for the confusion.
I'll now mark this relation as <=>.

>> The practical reason is to make easier to define indices.
>> If we keep the bi-variance in an invariant context, then the following
>> type definition is refused:
>>
>>       type 'a t = T;;
>>       type _ g = G : 'a -> 'a t g;;
>>
>> In 4.00, this definition is refused because 'a in 'a t g is bi-variant, but 'a appears
>> in a covariant position.
>
> I don't see why this could not be allowed without the restriction you
> propose. I thought that this was rejected in 4.00 because 4.00 used
> bi-variance as an (unsafe) approximation of non-injective. Since we now
> track injectivity separately from variance g be accepted (with 'a covariant).

In our work, this GADT definition would be accepted, and:
(1) matching on the constructor G does not give any information on the
value of the existential type 'a
(2) the parameter of g (not 'a, the one marked _ above) may marked
covariant or invariant, because the constructor t is upward-closed but
not downward-closed (private types)

In particular, the following does not type-check:
  let extract : int t g -> int = function
    | G n -> n

The G constructor at type ('b t) has type (exists 'a ['a t <=> 'b].
'a); so in the G branch the type-checker introduces the hypothesis ('a
t <=> int t) for an existential variable 'a. This does not allows to
deduce ('a <=> int) and therefore the body does not type-check.

However, in the OCaml type system, GADT type equalities are understood
in the unification sense (because it is the one that mixes well with
the inference engine), so the constructor G at type ('b g) would have
type (exists 'a. ['a t = 'b]), with (=) in the unification sense. As
Jacques said, this means that we must either reject the definition of
('b g) as is currently done, or stop considering ('a t) injective.

PS: Note that you could understand type parameter constraints in the
same way, which makes them safe without requiring injectivity, but
Didier remarked that this will only delay failure as code writers
generally assume injectivity. It's before to fail at definition site
than at usage site if you know you should fail anyway.

On Tue, Apr 30, 2013 at 12:12 PM, Leo White <lpw25@cam.ac.uk> wrote:
> [Sorry if this is a repeat post]
>
>> But it seems to me that this contradicts the definition of injectivity.
>> Namely, if we follow your definition, and have 'a bivar = 'b bivar, then
>> clearly bivar is not injective.
>
> I think Gabriel meant that 'a bivar = 'b bivar were equal in the
> subtyiping relationship, rather than in the unification
> relationship. (Perhaps we should use a different symbol for subtyping
> equality).
>
>> So there are two solutions: either we do not allow a bi-variant type
>> to be injective (breaking our simple statement that concrete types
>> are injective in all their parameters), or we consider bi-variance +
>> injectivity is some intermediary state, where we can use both directions
>> of subtyping, but not strong (unification) equality.
>
> I don't see how this implies the need for the strengthening you
> describe. As I see it:
>
>   type 'a t = T
>
> creates a type that is bi-variant in its parameter, so all occurences of
> 'a in:
>
>   type 'a u = 'a t -> 'a t
>
> are in bi-variant positions, so u should also be bi-variant.
>
>> The practical reason is to make easier to define indices.
>> If we keep the bi-variance in an invariant context, then the following
>> type definition is refused:
>>
>>       type 'a t = T;;
>>       type _ g = G : 'a -> 'a t g;;
>>
>> In 4.00, this definition is refused because 'a in 'a t g is bi-variant, but 'a appears
>> in a covariant position.
>
> I don't see why this could not be allowed without the restriction you
> propose. I thought that this was rejected in 4.00 because 4.00 used
> bi-variance as an (unsafe) approximation of non-injective. Since we now
> track injectivity separately from variance g be accepted (with 'a covariant).

  reply	other threads:[~2013-04-30 11:31 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-28  0:02 Jacques Garrigue
2013-04-28  2:45 ` Markus Mottl
2013-04-28 10:28   ` Jacques Garrigue
2013-04-28  5:54 ` Jacques Le Normand
2013-04-29  3:45 ` Ivan Gotovchits
2013-04-29  4:03   ` Ivan Gotovchits
2013-04-29  5:17 ` Jacques Le Normand
2013-04-29  7:58   ` Alain Frisch
2013-04-29 10:52     ` Jacques Garrigue
2013-04-29 11:23       ` Alain Frisch
2013-04-29 16:37         ` Nathan Mishra Linger
2013-04-29 23:53           ` Jacques Garrigue
2013-04-30  5:45       ` Jacques Garrigue
2013-05-04  6:46         ` Jacques Garrigue
2013-05-04  7:09           ` Gabriel Scherer
2013-05-04 12:28             ` Jacques Garrigue
2013-04-30  6:59       ` Alain Frisch
2013-04-30  7:56         ` Jacques Garrigue
2013-04-30  8:02           ` Alain Frisch
2013-04-30  8:18             ` Jacques Garrigue
2013-04-30  9:11               ` Gabriel Scherer
2013-04-30  9:55                 ` Jacques Garrigue
2013-04-30 10:12                   ` Leo White
2013-04-30 11:30                     ` Gabriel Scherer [this message]
2013-04-30 13:06                       ` Leo White
2013-04-29  7:59   ` Gabriel Scherer
2013-07-01 14:47 ` Alain Frisch
2013-07-01 23:20   ` Jacques Garrigue
2013-07-03 16:08     ` Alain Frisch
2013-07-03 16:13       ` Gabriel Scherer
2013-07-04  6:07         ` [Caml-list] Request for feedback: A problem with injectivity oleg
2013-07-04  7:35           ` Alain Frisch
2013-07-05 10:30             ` oleg
2013-07-05 12:02               ` Alain Frisch
2013-07-04  1:00       ` [Caml-list] Request for feedback: A problem with injectivity and GADTs Jacques Garrigue
2013-07-04  8:14         ` Alain Frisch
2013-07-04  8:52           ` Jacques Garrigue

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=CAPFanBEntKG-thQk2w2XHSYr7X7wCqyEzibQBEdqUqr_XxQGVg@mail.gmail.com \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=garrigue@math.nagoya-u.ac.jp \
    --cc=lpw25@cam.ac.uk \
    /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).