Another question about sharing

Malcolm Wallace
Mon, 10 Dec 2001 14:51:28 +0000

> > 	data Path = L Path | R Path | T
> > 	paths = T : branch paths
> > 	branch (p:ps) = L p : R p : branch ps

> 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

Well, how about the following little circular program?

        paths :: () -> [Path]
    	paths () = let r = T : branch r in r

As far as I can understand what you are looking for, I think this meets
the bill.  Every use of the expression `paths ()' will re-evaluate
the infinite structure to the extent its context requires it, and the
expanded value will be thrown away as soon as the value of this
instance of `paths ()' is no longer required.