* [Caml-list] matching GADT option types
@ 2019-01-17 9:46 Christopher Zimmermann
2019-01-17 9:58 ` Jeremy Yallop
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Christopher Zimmermann @ 2019-01-17 9:46 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 620 bytes --]
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
--
http://gmerlin.de
OpenPGP: http://gmerlin.de/christopher.pub
CB07 DA40 B0B6 571D 35E2 0DEF 87E2 92A7 13E5 DEE1
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] matching GADT option types
2019-01-17 9:46 [Caml-list] matching GADT option types Christopher Zimmermann
@ 2019-01-17 9:58 ` Jeremy Yallop
2019-01-17 10:02 ` Christopher Zimmermann
2019-01-17 10:12 ` Gabriel Scherer
2 siblings, 0 replies; 6+ messages in thread
From: Jeremy Yallop @ 2019-01-17 9:58 UTC (permalink / raw)
To: Christopher Zimmermann; +Cc: Caml List
On Thu, 17 Jan 2019 at 09:46, Christopher Zimmermann
<christopher@gmerlin.de> wrote:
> 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
GADT matching under or-patterns isn't currently supported in the
current release. However, the following PR added some support, and
your code is now accepted with the trunk compiler:
Allow GADT constructors to introduce equations and existential
types under or-patterns
https://github.com/ocaml/ocaml/pull/2110
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] matching GADT option types
2019-01-17 9:46 [Caml-list] matching GADT option types Christopher Zimmermann
2019-01-17 9:58 ` Jeremy Yallop
@ 2019-01-17 10:02 ` Christopher Zimmermann
2019-01-17 10:18 ` Gabriel Scherer
2019-01-17 10:12 ` Gabriel Scherer
2 siblings, 1 reply; 6+ messages in thread
From: Christopher Zimmermann @ 2019-01-17 10:02 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 1240 bytes --]
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
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] matching GADT option types
2019-01-17 10:12 ` Gabriel Scherer
@ 2019-01-17 10:08 ` Christopher Zimmermann
0 siblings, 0 replies; 6+ messages in thread
From: Christopher Zimmermann @ 2019-01-17 10:08 UTC (permalink / raw)
To: Gabriel Scherer; +Cc: caml users
[-- Attachment #1: Type: text/plain, Size: 2435 bytes --]
Thank you for the fast answer!
That means my second example will still be rejected in 4.08?
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
On Thu, 17 Jan 2019 11:12:02 +0100
Gabriel Scherer <gabriel.scherer@gmail.com> wrote:
> Matching on a constructor of a GADT may introduce a typing equation.
> In your example, matching on `A` introduces the equation `a = unit`.
> For this reason, the status of or-patterns (p1 | p2) containing GADT
> constructors is delicate: to type-check them we have to decide which
> equations from both sides are preserved in the result, computing a
> sort of intersection.
>
> Released versions of OCaml avoid this difficulty by not supporting
> GADTs in or-patterns at all; you have to expand the pattern into two
> branches, as you did in your function `f` above.
>
> In the current trunk, a change from Thomas Réfis and Leo White has
> been merged that allows GADTs in or-patterns, but discards the
> equations. Your specific example (the function `g`) is now accepted,
> and will be typeable in 4.08. Other examples, where you need to use
> an equation (provided by both sides of the or-patterns) in the body
> of the clause, will still be rejected.
>
>
> On Thu, Jan 17, 2019 at 10:46 AM 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
--
http://gmerlin.de
OpenPGP: http://gmerlin.de/christopher.pub
CB07 DA40 B0B6 571D 35E2 0DEF 87E2 92A7 13E5 DEE1
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] matching GADT option types
2019-01-17 9:46 [Caml-list] matching GADT option types Christopher Zimmermann
2019-01-17 9:58 ` Jeremy Yallop
2019-01-17 10:02 ` Christopher Zimmermann
@ 2019-01-17 10:12 ` Gabriel Scherer
2019-01-17 10:08 ` Christopher Zimmermann
2 siblings, 1 reply; 6+ messages in thread
From: Gabriel Scherer @ 2019-01-17 10:12 UTC (permalink / raw)
To: Christopher Zimmermann; +Cc: caml users
[-- Attachment #1: Type: text/plain, Size: 1711 bytes --]
Matching on a constructor of a GADT may introduce a typing equation. In
your example, matching on `A` introduces the equation `a = unit`.
For this reason, the status of or-patterns (p1 | p2) containing GADT
constructors is delicate: to type-check them we have to decide which
equations from both sides are preserved in the result, computing a sort of
intersection.
Released versions of OCaml avoid this difficulty by not supporting GADTs in
or-patterns at all; you have to expand the pattern into two branches, as
you did in your function `f` above.
In the current trunk, a change from Thomas Réfis and Leo White has been
merged that allows GADTs in or-patterns, but discards the equations. Your
specific example (the function `g`) is now accepted, and will be typeable
in 4.08. Other examples, where you need to use an equation (provided by
both sides of the or-patterns) in the body of the clause, will still be
rejected.
On Thu, Jan 17, 2019 at 10:46 AM 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
>
>
> --
> http://gmerlin.de
> OpenPGP: http://gmerlin.de/christopher.pub
> CB07 DA40 B0B6 571D 35E2 0DEF 87E2 92A7 13E5 DEE1
>
[-- Attachment #2: Type: text/html, Size: 2304 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] matching GADT option types
2019-01-17 10:02 ` Christopher Zimmermann
@ 2019-01-17 10:18 ` Gabriel Scherer
0 siblings, 0 replies; 6+ messages in thread
From: Gabriel Scherer @ 2019-01-17 10:18 UTC (permalink / raw)
To: Christopher Zimmermann; +Cc: caml users
[-- Attachment #1: Type: text/plain, Size: 1899 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
>
[-- Attachment #2: Type: text/html, Size: 2780 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2019-01-17 10:08 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-17 9:46 [Caml-list] matching GADT option types Christopher Zimmermann
2019-01-17 9:58 ` Jeremy Yallop
2019-01-17 10:02 ` Christopher Zimmermann
2019-01-17 10:18 ` Gabriel Scherer
2019-01-17 10:12 ` Gabriel Scherer
2019-01-17 10:08 ` Christopher Zimmermann
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox