* [Caml-list] GADT: question about inference
@ 2013-07-26 17:15 Mathieu Barbin
2013-07-26 20:27 ` Gabriel Scherer
0 siblings, 1 reply; 4+ messages in thread
From: Mathieu Barbin @ 2013-07-26 17:15 UTC (permalink / raw)
To: caml-list
[-- Attachment #1: Type: text/plain, Size: 1225 bytes --]
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
[-- Attachment #2: Type: text/html, Size: 1562 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] GADT: question about inference
2013-07-26 17:15 [Caml-list] GADT: question about inference Mathieu Barbin
@ 2013-07-26 20:27 ` Gabriel Scherer
2013-07-26 21:25 ` Alain Frisch
0 siblings, 1 reply; 4+ messages in thread
From: Gabriel Scherer @ 2013-07-26 20:27 UTC (permalink / raw)
To: Mathieu Barbin; +Cc: caml-list
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
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] GADT: question about inference
2013-07-26 20:27 ` Gabriel Scherer
@ 2013-07-26 21:25 ` Alain Frisch
2013-07-28 0:29 ` Mathieu Barbin
0 siblings, 1 reply; 4+ messages in thread
From: Alain Frisch @ 2013-07-26 21:25 UTC (permalink / raw)
To: Gabriel Scherer; +Cc: Mathieu Barbin, caml-list
On 7/26/2013 10:27 PM, Gabriel Scherer wrote:
> 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.
A feature request related to allowing more kinds of types to be refined
by GADT patterns (abstract types in the current environment, introduced
by unpacking a module -- but the same would be useful for abstract types
in a functor's argument):
http://caml.inria.fr/mantis/view.php?id=5713
-- Alain
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Caml-list] GADT: question about inference
2013-07-26 21:25 ` Alain Frisch
@ 2013-07-28 0:29 ` Mathieu Barbin
0 siblings, 0 replies; 4+ messages in thread
From: Mathieu Barbin @ 2013-07-28 0:29 UTC (permalink / raw)
To: Alain Frisch; +Cc: Gabriel Scherer, caml-list
[-- Attachment #1: Type: text/plain, Size: 1546 bytes --]
Got it. I missed that somehow until now. Indeed using eta expansion to
enforce introducing variable-like constructors works well, and looks like a
very reasonable work around for the cases I have in mind.
Thank you very much for the relevant links and the quick answers.
2013/7/26 Alain Frisch <alain@frisch.fr>
> On 7/26/2013 10:27 PM, Gabriel Scherer wrote:
>
>> This is explained in the manual (emphasis mine)
>> http://caml.inria.fr/pub/docs/**manual-ocaml/manual021.html#**toc85<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.
>>
>
> A feature request related to allowing more kinds of types to be refined by
> GADT patterns (abstract types in the current environment, introduced by
> unpacking a module -- but the same would be useful for abstract types in a
> functor's argument):
>
> http://caml.inria.fr/mantis/**view.php?id=5713<http://caml.inria.fr/mantis/view.php?id=5713>
>
> -- Alain
>
[-- Attachment #2: Type: text/html, Size: 2301 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-07-28 0:29 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-26 17:15 [Caml-list] GADT: question about inference Mathieu Barbin
2013-07-26 20:27 ` Gabriel Scherer
2013-07-26 21:25 ` Alain Frisch
2013-07-28 0:29 ` Mathieu Barbin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox