[Haskell-cafe] Re: Memoizing partially evaluated computations.

Luke Palmer lrpalmer at gmail.com
Thu Mar 19 00:57:32 EDT 2009


On Wed, Mar 18, 2009 at 10:37 PM, Chung-chieh Shan
<ccshan at post.harvard.edu>wrote:

> You make an important point that sharing is changed only if the variable
> (such as x0) is used more than once in the body.  Let me note that the
> definition of "computation" doesn't have to mention "x0" multiple times
> syntactically for x0 to be used more than once.  It's enough for "x0" to
> appear once under a lambda.  Here's a concrete example:
>
>    main :: IO ()
>    main = once >> once
>
>    once :: IO ()
>    once = do
>        putStrLn "foo"
>        putStrLn (unsafePerformIO (putStrLn "hello") `seq` "world")
>
> If I put "() <-" in front of the second-to-last line, then "hello"
> appears twice, not once, in the output.


You're right.  Of course, now you're in the compiler's territory.  If you
compile the () <- version with optimizations, the unsafePerformIO is lifted
out and "hello" is again only printed once.  So yes, to ensure sharing under
a lambda, it's safest to use an explicit let/where clause.

Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090318/69be7424/attachment-0001.htm


More information about the Haskell-Cafe mailing list