caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Christopher Zimmermann <christopher@gmerlin.de>
Cc: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] matching GADT option types
Date: Thu, 17 Jan 2019 11:18:34 +0100	[thread overview]
Message-ID: <CAPFanBFMLSFOBHwkWduJDmwDEzAyT-t1inCy5_EA1xG_snr-Cw@mail.gmail.com> (raw)
In-Reply-To: <20190117110227.3368e34b@mortimer.gmerlin.de>

[-- Attachment #1: Type: text/plain, Size: 2125 bytes --]

Your function g is incorrect; in the None case, it claims that the
constructor A has the type (a t) for an unknown (arbitrary) type parameter
(a). A only has the type (a t) for a specific type (a = [`A]), not for all
types.
If you want g to be able to return a value of type (a t) for *some* type
(a) that g chooses, instead of for *any* type (a) chosen by the
context/caller, then you should use an existential type wrapping:

  type any_t = Any : a t -> any_t.

  [...]
  | Some A -> Any A
  | Some B -> Any B
  | None -> Any A

On Thu, Jan 17, 2019 at 11:02 AM Christopher Zimmermann <
christopher@gmerlin.de> wrote:

> On Thu, 17 Jan 2019 10:46:11 +0100
> Christopher Zimmermann <christopher@gmerlin.de> wrote:
>
> Hi,
>
> why does the f type correctly while g fails to type?
>
> Christopher
>
> type 'a t =
>   | A : unit t
>
> let f =
>   fun (type a) ~(p :a t option) () -> match p with
>     | Some A -> ()
>     | None -> ()
>
> let g =
>   fun (type a) ~(p :a t option) () -> match p with
>     | Some A (* TYPING ERROR HERE *)
>     | None -> ()
>
> Error: This pattern matches values of type unit t
>        but a pattern was expected which matches values of type a t
>        Type unit is not compatible with type a
>
>
>
> to add to the confusion, why does f type while g fails to type ?
>
> type 'a t =
>   | A : [`A] t
>   | B : [`A|`B] t
>
> let f =
>   fun (type a) ~(p :a t) () -> match p with
>     | A -> (A: a t)
>     | B -> (B: a t)
>
> let g =
>   fun (type a) ~(p :a t option) () -> match p with
>     | Some B -> (B: a t)
>     | Some A -> (A: a t)
>     | None -> (A (* TYPE ERROR HERE *) : a t)
>
> Error: This expression has type [ `A ] t
>        but an expression was expected of type a t
>        Type [ `A ] is not compatible with type a
>
> --
> http://gmerlin.de
> OpenPGP: http://gmerlin.de/christopher.pub
> CB07 DA40 B0B6 571D 35E2  0DEF 87E2 92A7 13E5 DEE1
>

-- 
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list https://inbox.ocaml.org/caml-list
Forum: https://discuss.ocaml.org/
Bug reports: http://caml.inria.fr/bin/caml-bugs

[-- Attachment #2: Type: text/html, Size: 2780 bytes --]

  reply	other threads:[~2019-01-17 10:08 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-17  9:46 Christopher Zimmermann
2019-01-17  9:58 ` Jeremy Yallop
2019-01-17 10:02 ` Christopher Zimmermann
2019-01-17 10:18   ` Gabriel Scherer [this message]
2019-01-17 10:12 ` Gabriel Scherer
2019-01-17 10:08   ` Christopher Zimmermann

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=CAPFanBFMLSFOBHwkWduJDmwDEzAyT-t1inCy5_EA1xG_snr-Cw@mail.gmail.com \
    --to=gabriel.scherer@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=christopher@gmerlin.de \
    /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).