The following code (full code available here[1], example taken from comments here[2]),
test1 c = let a = const' (nthPrime 100000)
test2 c = let a = \_ -> (nthPrime 100000)
in a ghci session (with :set +s):
(0.89 secs, 106657468 bytes)
(1.80 secs, 213077068 bytes)
test1, although denotationally equivalent to test2 runs about twice as fast.
My questions are:
- What is the optimization that test1 is taking advantage of called?
- Is said optimization required to conform to the Haskell98 standard? If so, where is it stated?
- Could someone explain or point to a precise explanation of this optimization? If I'm doing an operational reduction by hand on paper, how would I take account for this optimization?
Thanks,
David
[1] http://hpaste.org/40033/operational_semantics
[2] http://conal.net/blog/posts/lazier-functional-programming-part-2/