I originally wrote a reverse function like this:
let reverse =
let rec aux acc = function
| [] -> acc
| h :: t -> aux (h :: acc) t
in
aux []
However, it does not get a polymorphic type, due to the value restriction (utop gives the actual type as val reverse : '_weak1 list -> '_weak1 list = <fun>).
I can, however, make it work by taking an explicit list argument:
let reverse list =
let rec aux acc = function
| [] -> acc
| h :: t -> aux (h :: acc) t
in
aux [] list
But I am still curious if it is possible to force reverse to be polymorphic using a type signature. I tried making it explicitly polymorphic:
let reverse : 'a. 'a list -> 'a list =
let rec aux acc = function
| [] -> acc
| h :: t -> aux (h :: acc) t
in
aux []
But then I get the following error
Error: This definition has type 'a list -> 'a list which is less general than
'a0. 'a0 list -> 'a0 list
It seems the weak types have disappeared - is the 'a0 type variable a truly polymorphic type, or is it another name for '_weak1? And is it possible to allow this function with the value restriction by changing its signature?