Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml's `type a. a t` syntax

I've just come across the following code snippet in OCaml's documentation about GADTs:

let rec eval : type a. a term -> a = function
  | Int n -> n
  | Add -> (fun x y -> x + y)
  | App (f, x) -> (eval f) (eval x)

which, once evaluated in utop, has the following signature:

val eval : 'a term -> 'a = <fun>

I also noticed that, when replacing type a. a term -> a by 'a term -> 'a or just removing the signature, the function doesn't compile anymore.

...
| Add -> (fun x y -> x + y)
...
Error: This pattern matches values of type (int -> int -> int) term
  but a pattern was expected which matches values of type int term
  Type int -> int -> int is not compatible with type int 

So what is this notation? What makes it different of 'a t ?

Is it specific to GADTs?

like image 421
Antoine Avatar asked Sep 15 '14 17:09

Antoine


1 Answers

The manual explains the syntax a few sections up: http://caml.inria.fr/pub/docs/manual-ocaml-400/manual021.html#toc80

In short, type a. ... means that the locally abstract type a must be polymorphic.

like image 185
hcarty Avatar answered Nov 09 '22 17:11

hcarty