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

Chung-chieh Shan ccshan at post.harvard.edu
Thu Mar 19 00:37:42 EDT 2009

On 2009-03-18T21:24:58-0600, Luke Palmer wrote:
> On Wed, Mar 18, 2009 at 6:21 AM, Chung-chieh Shan
> <ccshan at post.harvard.edu>wrote:
> >    computation = [
> >        smallIOfunc x0
> >      , smallIOfunc x1
> >      , smallIOfunc x2
> >      , smallIOfunc x3
> >       ]
> >      where smallIOfunc a = print a >> return a
> >             x0 = timeConsumingPureOperation0
> >            x1 = timeConsumingPureOperation1
> >            x2 = timeConsumingPureOperation2
> >            x3 = timeConsumingPureOperation3
> Um, just to clarify, this code is exactly equivalent to the original,
> including sharing behavior.  The only time a let (or where) clause changes
> sharing is if the variable is used more than once in the body.

Ah, good point!  Of course, "timeConsumingPureOperation0" above is a
metavariable for a Haskell expression, not just a Haskell variable.  But
I guess I also took "smallIOfunc" to be a metavariable for a Haskell
context (i.e., a Haskell expression with a hole), not just a Haskell
function name.

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.

Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig
100 Days to close Guantanamo and end torture http://100dayscampaign.org/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20090319/b52f0bc8/attachment.bin

More information about the Haskell-Cafe mailing list