Login

Type inference

# let inc : 'a -> 'a = fun x -> x + 1;;
val inc : int -> int = <fun>
# let f x =
   let y (w:'a) = 0 + w in
   (x:'a);;
val f : int -> int = <fun>

vs.

# let inc : 'a. 'a -> 'a = fun x -> x + 1;;
Error: This definition has type int -> int which is less general than
         'a. 'a -> 'a
# let inc (type a): a -> a = fun x -> x + 1;;
Error: The value x has type a but an expression was expected of type int
# let inc: type a. a -> a = fun x -> x + 1;;
Error: The value x has type a but an expression was expected of type int

https://discuss.ocaml.org/t/understanding-type-quantification-and-locally-abstract-types/17701