Could someone please explain to me what is going on in these three bindings? What is the significance of the parens? What is the meaning of g and why does it have to be a g as an argument in the anonymous function? And overall what is actually happening in all of this... Thank you!
val a = fn g => (fn x => fn y => x) (g 0) (g 7);
val b = fn g => (fn x => fn y => x) (g 0) (g "happy");
val c = fn g => (fn x => fn y => x) (g 0) (g (g 7));
The function
(fn x => fn y => x)is the constant function. It takes two arguments (xandy) and always returns the first argument (i.e.x).This function is applied to two arguments in all three functions
a,bandc:athe constant function is applied to(g 0)and(g 7).bthe constant function is applied to(g 0)and(g "happy").cthe constant function is applied to(g 0)and(g (g 7)).In all three cases, the result is
(g 0)because the second argument is discarded. Therefore, all the three functions can be simplified to:Actually, the second function (i.e.
b) raises a type error becausegis first applied to anintand later applied to astring.Although the functions
aandchave the same result yet they do not have the same type:ais(int -> 'a) -> 'abecause we don't know the return type of the functiong. Hence, it is a polymorphic function and it's more general thanc.cis(int -> int) -> intbecausegis applied to its own result (i.e.gis applied to(g 7)). Hence, its return type must be the same as its argument type (i.e.int).Therefore,
ais more general thancand every instance ofccan be safely replaced withabut not vice versa.