Generalisation error

This is more for personal curiosity, but maybe someone can point me in the right direction. If I have the following piece of code

type 'a t = 'a list 
let f : _ t -> int t = List.map (fun _ -> 1) 
let f' (l : _ t) : int t = List.map (fun _ -> 1) l 

then the definition of function f causes an error:

2 | let f : _ t -> int t = List.map (fun _ -> 1)
Error: The type of this expression, '_weak1 t -> int t,
contains type variables that cannot be generalized

While I don’t fully understand why the type system cannot generalise the type variable here, that is actually not my main question. For all odds and purposes I thought that the two definitions f and f' are equivalent, however, the second one does not give an error.

Thanks for any input :slight_smile:

This is called the value restriction. More information is in the manual (See this section for instance).
The first definition (f) is not a value in the sense of lambda-calculus semantics, while the second (f') is.

1 Like

Thank you! That’s exactly what I was looking for :slight_smile: