Is eta-expansion foolproof in OCaml?
Solution 1:
The relaxed value restriction classifies the eta-expanded form fun y -> f x y
as a value that can thus be generalized by let bindings, contrarily to the non-value f y
. See https://ocaml.org/manual/polymorphism.html#s%3Aweak-polymorphism .
Moreover, in a eager language like OCaml, eta-expansion does change the semantics of functions. Consider
let print x =
Format.printf "x=%s@." x;
fun y -> Format.printf "y=%s@."
and
let print1 = print "x"
let print2 y = print "x" y
Thus, eta-expansion is a semantic-changing transformation rather than a "foolproof" transformation.