Another question about sharing

Adrian Hey ahey@iee.org
Mon, 10 Dec 2001 14:37:37 +0000


On Monday 10 December 2001 11:18 am, Simon Marlow wrote:
> > If I have..
> > 	data Path = L Path | R Path | T
> > 	paths = T : branch paths
> > 	branch (p:ps) = L p : R p : branch ps
> >
> > This will be a CAF which can never be garbage collected, but
> > may grow indefinitely large as it gets reduced. Correct?
>
> GHC will collect the CAF when it can determine that it is no longer
> required.  In GHCi, the CAF will only be collected if you turn on ':set
> +r', otherwise evaluated CAFs are retained between evaluations.

Thanks,

I'm curious, how does GHC determine that the CAF is no longer required
if it is referenced by code (somehow)? If code was also some kind of
heap allocated data structure I guess this would be possible, but I
thought this wasn't so with GHC.

But in any case, I'm not sure this really helps me. I don't really
mind if the unreduced form of the CAF is garbage collected or not
(it's only going to be a few words of memory). The effect I'm trying
to get is to ensure that the (partially) reduced form of the CAF only
lives as long as any (non code) heap object which references it.
Does that make sense? (probably not:-)

This code was originally written in Clean, and the Clean designers
addressed this problem by allowing the programmer to distinguish
between constants and functions with no arguments. (The latter
being a fragment of code which reconstructs the constant each time
it's called, rather than having just one occurrence as a CAF.)
It's typically used to inhibit sharing.

I don't believe there is any such distinction in Haskell.
I thought maybe there was a trick you could use in Haskell to
achieve the same effect, but I don't think there is (unless
I'm missing something).

Regards
-- 
Adrian Hey