memoization hints?

Simon Marlow
Tue, 22 Jan 2002 16:09:58 -0000

> Take a standard sort of example:
> fib n =3D let
>                 f =3D 0:1:zipWith (+) f (tail f)
>            in
>                 f !! n
> -- code that first calculates fib 1000, then fib 3, fib 4, then does
> some other stuff
> I believe that the entire list will be maintained (when compiled with
> ghc -O) after calculating fib 1000, despite the fact that it=20
> will never
> be used again.

GHC will retain the list only if it determines that it might still be
required, in other words if fib might be called again.  If fib will
never be called again, then the chances are that the list will be
garbage collected. (I'm being intentionally imprecise, because of course
the compiler can't in general determine accurately whether fib will or
will not be called again).=20

If you don't want the list to be retained at all, you can use the
-fno-full-laziness flag to GHC.  Unfortunately there's no way to do this
at the level of a single binding.