[Haskell-cafe] Memoizing partially evaluated computations.

Sebastiaan Visser sfvisser at cs.uu.nl
Wed Mar 18 06:46:17 EDT 2009

Suppose I have a list of IO computations that depends on a few very  
time consuming pure operations. The pure operations are not dependent  
on the real world:

 >  computation :: [IO Int]
 >  computation = [
 >      smallIOfunc timeConsumingPureOperation0
 >    , smallIOfunc timeConsumingPureOperation1
 >    , smallIOfunc timeConsumingPureOperation2
 >    , smallIOfunc timeConsumingPureOperation3
 >    ]
 >    where smallIOfunc a = print a >> return a

In my main function I would like to repeatedly print the values

 >  main = forever $
 >    sequence_ (map (>>=print) computation)

When I do this, all the time consuming operations will be reevaluated  
every run of the main loop. Is there a any (simple or smart) way to  
prevent the garbage collector from cleaning up the fully evaluated  
thunks inside my computation? As if it were something like this:

 >  computation :: [IO Int]
 >  computation = [smallIOfunc 42, smallIOfunc 34385, smallIOfunc 3,  
smallIOfunc 55]

Of course I could plugin some kind of Int memoizer inside my  
computation, but I do not really have the control to change things  
`deep' inside the code. I want to have some form of snapshot of a list  
of partially evaluated IO computations...

Any suggestions?


More information about the Haskell-Cafe mailing list