Is it possible for this match to typecheck when constructors seem to be incompatible?

type 'a t =
| I: int -> int t
| S: string -> string t
let () =
let i = I 42 in
match i with
| I n -> Printf.printf "int: %d\n" n
| S t -> Printf.printf "string: %s\n" t (* error *)

The typechecker tries (and fails) to unify type parameters:

Error: This pattern matches values of type string t
but a pattern was expected which matches values of type int t
Type string is not compatible with type int

Iâd like to do this, too, but I guess itâs related:

As a general rule, only abstract types can be refined by matching on GADT-constructors. When defining function operating on GADTs, it is therefore often necessary to use locally abstract types:

let get (type a): a t -> a = function
| I n -> n
| S s -> s

( If the function is recursive and , the locally abstract notation may need to be combined with the explicit polymorphic notation yielding let get: type a. a t -> a=âŚ .)

However, on your first example, the problem is that your second case is simply impossible: since i has for type int t, the only possible case when matching on i is Int _. Consequently, your example can be rewritten as

let () =
let i = I 42 in
match i with
| I n -> Printf.printf "int: %d\n" n

And this pattern matching is exhaustive. If you donât feel reassured or if for some extremely imperious reasons you are not using the exhaustiveness check for pattern matching, it is possible to complete the pattern with a refutation case:

let () =
let i = I 42 in
match i with
| I n -> Printf.printf "int: %d\n" n
| _ -> .

Here the dot . on the right hand side of the case indicates that the pattern on the left hand side should not be reachable and the type-checker will check that it is impossible to produce a value that matches this pattern.

Thank you! Iâd only heard about forall but it still looks like magic to me, Iâm used to think the compiler always gets the most general type for an expressionâŚ

My first example didnât make a lot of sense since it was monomorphic. Of course, if true then I 42 else S "fourty-two" wonât typecheck (int â string), but is it possible to ever get an 'a t from it (Iâm just curious)?

Itâs indirectly possible by using what is usually called an existential wrapper, where the type variable is hidden inside a GADT (here, any):

type _ t =
| B : bool -> bool t
| S : string -> string t
type any = Any : 'a t -> any

With that you can express something which is close to your if example:

if true then Any (I 3) else Any (S "foo")

Note that when you pattern match on an any you get an 'a t where the type 'a is unknown and must not escape. The type checker will reject programs that donât meet the non-escape condition with an error message (that is frequently not as helpful as you would like). Sometimes it isnât obvious how to fix the problem, which is part of why programming with GADTs can get âinterestingâ.

Thanks! I guess 'a canât escape since it isnât available at runtimeâŚ

As octachron said (I wouldâve asked, otherwise), it works with a recursive function, too, so I guess my issue is completey solved!

type 'a t =
| I: int -> int t
| S: string -> string t
| O: 'a t option -> 'a option t
let rec get: type a. a t -> a = function
| I n -> n
| S t -> t
| O None -> None
| O (Some x) -> Some (get x)

âŚ but I thought forall and type did the same thing. And the former doesnât work. Why?

forall and type are indeed different. type binds a locally abstract type variable (the kind that can have a different type for each GADT constructor that you match against), and forall requires a type variable to be polymorphic.

For this definition of get, you need both (which is what the type a. ... syntax gives you). The reason you need forall is because this function exhibits polymorphic recursion, where a recursive function is used at a different type within its definition. Because of limitations of type inference, annotations are required for this to type check.

An existential variable can escape quite easily: an simple example of a program which is rejected for this reason is let test eq (Any x) (Any y) = eq x y.

This property âusually holdsâ in OCaml. Formally we say that the language has âprincipal typesâ, there exists most general types that the inference engine can look for. This works well for the core fragments of OCaml (or other ML languages), but this property crumbles in presence of more advanced type-system features (GADTs, or polymorphic record fields for example); typically we still have principal types but only with respect to a specificationÂš that requires extra annotations in some places.

Âš: scientific aside: âhaving principal typesâ is a property of a type system; you can make it very strong by making this type system very implicit, or fairly weak by forcing programs using this type system to have explicit and ad-hoc/arbitrary specifications. This idea that it is relative to a type system that may or may not be as âdeclarativeâ as you expect is key to compare and evaluate some of the more recent/advanced literature on type inference â to read the fine prints. âHas principal typesâ by itself does not necessarily mean that the type system is nice, if the reference system is very algorithmic in nature.