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:

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)?

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’.

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.

I’m used to think the compiler always gets the most general type for an expression…

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.