Revert a CAF?

wren ng thornton wren at
Tue Dec 6 18:48:32 CET 2011

So, I have an optimization/internals question. Does the GHC API have any 
hooks for being able to revert a CAF to the original expression, thus 
discarding the previously computed result?

The reason I'm wanting this is that I have a particular CAF which is an 
infinite list. Unfolding that list takes a fair deal of work, so we want 
to share it whenever possible; however it doesn't take an overwhelming 
amount of work, so if we know we've evaluated more of the list than 
necessary (for a long while), it'd be nice to be able to revert the 
evaluation in order to save on memory overhead (e.g., by calling relax 
:: IO()).

I could hack something together based on unsafePerformIO and top-level 
IORefs, and it's clear that this is in fact a safe thing to do, but I'm 
worried about the semantic issues inherent in unsafePerformIOed 
top-level IORefs (e.g., the fact that their scope isn't particularly 
well defined: is it per library instance? per runtime?...). 
Unfortunately, for what I'm doing, it isn't really feasible to just 
leave the IO type in there nor to pass around the infinite list so we 
can use scoping rules to decide when to free it.

(Feel free to offer alternative suggestions to handling this situation too.)

Live well,

More information about the Glasgow-haskell-users mailing list