caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Mathieu Barbin <mathieu.barbin@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] GADT: question about inference
Date: Fri, 26 Jul 2013 22:27:20 +0200	[thread overview]
Message-ID: <CAPFanBHHQ70TaK6cmURx9OjbcXGOkc0K7giotAWk8gtsX09iaQ@mail.gmail.com> (raw)
In-Reply-To: <CANXn40m1+bzDuS13_=S3PFmj4h_iz5aZb6fV8PLuuB-Jv_syPg@mail.gmail.com>

This is explained in the manual (emphasis mine)
  http://caml.inria.fr/pub/docs/manual-ocaml/manual021.html#toc85

> The constraints associated to each constructor can be recovered through pattern-matching.
> Namely, **if the type of the scrutinee of a pattern-matching contains a locally abstract type**,
> this type can be refined according to the constructor used.

The only types that can be refined by the type equalities introduced
by GADT are locally abstract types, the variable-like constructor "a"
introduced by the "(type a)" and "foo : type a . bar" syntaxes. Your
first two examples have no locally abstract type, only type members of
modules, so there was no GADT refinement happening.

On Fri, Jul 26, 2013 at 7:15 PM, Mathieu Barbin
<mathieu.barbin@gmail.com> wrote:
> Hello,
>
> Playing around with a reduced version of a dynamic types, I ran into some
> type errors, and I feel that I'd love to understand more about the way the
> inference work:
>
> type _ ty  = Int : int ty | String : string ty
>
> module H : sig
>   type t
>   val ty : t ty
> end = struct
>   type t = int
>   let ty = Int
> end
>
> (* from now, trying various implementation for [f] *)
>
> let f : H.t -> int =
>     match H.ty with
>     | Int -> (fun (a : H.t) -> (a : int))
>     | _ -> assert false
>   ;;
> Error: This expression has type H.t but an expression was expected of type
>          int
>
> let f : H.t -> int = fun (a : H.t) ->
>     match H.ty with
>     | Int -> (a : int)
>     | _ -> assert false
>   ;;
> Error: This expression has type H.t but an expression was expected of type
>          int
>
> let f : H.t -> int =
>   let aux : type a. a ty -> a -> int = function
>     | Int -> (fun a -> a)
>     | _ -> assert false
>   in
>   aux H.ty
>   ;;
>
> (* val f : H.t -> int = <fun> *)
>
> I can't quite come up with a clear mental model of why in-lining the pattern
> matching in the first versions of [f]t would not type check. I tried adding
> some more type coercions without great success. I'd be very grateful to get
> some feed back about this restriction.
>
> Thanks,
> Mathieu
>

  reply	other threads:[~2013-07-26 20:28 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-26 17:15 Mathieu Barbin
2013-07-26 20:27 ` Gabriel Scherer [this message]
2013-07-26 21:25   ` Alain Frisch
2013-07-28  0:29     ` Mathieu Barbin

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=CAPFanBHHQ70TaK6cmURx9OjbcXGOkc0K7giotAWk8gtsX09iaQ@mail.gmail.com \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=mathieu.barbin@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).