[Haskell-cafe] Shared thunk optimization. Looking to solidify my understanding

David Sankel camior at gmail.com
Wed Sep 22 11:10:06 EDT 2010


The following code (full code available here[1], example taken from comments
here[2]),

const' = \a _ -> a

test1 c = let a = const' (nthPrime 100000)
          in (a c, a c)

test2 c = let a = \_ -> (nthPrime 100000)
          in (a c, a c)


 in a ghci session (with :set +s):

*Primes> test1 1
(9592,9592)
(0.89 secs, 106657468 bytes)
*Primes> test2 1
(9592,9592)
(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/

-- 
David Sankel
Sankel Software
www.sankelsoftware.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100922/7508692c/attachment.html


More information about the Haskell-Cafe mailing list