Naming and subexpressions

Keith Wansbrough Keith.Wansbrough@cl.cam.ac.uk
Thu, 08 May 2003 10:53:34 +0100


Ketil Z. Malde writes:

> I have a program, structured approximately like so
> 
>    main = do
>         let stage1 = e1...
>         let stage2 = e2...stage1...
>         stage3 <- e3...stage2
>         case something of
>                 foo -> output stage1
>                 bar -> output stage2
>                 baz -> output stage3
[..]
> It appearsą, however, that naming the result of stage1 prevents it from
> being garbage collected when subsequent stages are produced.  Since
> stage1 is a huge list, this leads to a space leak.

What is "something"?  Is it a literal constructor (Foo, Bar, or Baz),
or something computed elsewhere, or something taken from the command
line?  If it's a literal constructor, GHC can optimise the case away;
in the other cases, it can't know until runtime which will be used.
If "something" depends on stage3, then stage1 and stage2 *must* be
held onto until the end.

--KW 8-)



--KW 8-)