
Hi Neil,
On Dec 26, 2007 7:16 PM, Neil Mitchell
Given the code:
fred = 2 + 2
bob = fred + fred
In a Haskell implementation fred would be evaluated once to 4, then used twice. The 2+2 would only happen once (ignore defaulting and overloaded numerics for now).
Is this sharing mandated by the standard? (I don't think so) Is there some paper that describes why this is desirable, and gives any detail?
I think if the report does not mandate it, it's for the same reason that Haskell-the-language is "non-strict," not "lazy"-- it thinks that it's the compiler's business to decide things like this. (After all, there are situations where it might be better to evaluate Fred twice, and the compiler might recognize some of them.) But the Haskell design certainly anticipates that this is what compilers will normally do: the point of the dreaded monomorphism restriction is to make sure that 'fred' doesn't accidentally become (Num a => a), which would make it infeasible to evaluate it only once (because you would have to store the value for every instance of Num). It's a lot like you can expect Haskell implementations to be lazy, not call-by-name. (I don't know when this behavior originated.) - Benja