On Wed, Mar 18, 2009 at 10:37 PM, Chung-chieh Shan <ccshan@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