[Haskell-cafe] Help understanding sharing

Albert Y. C. Lai trebla at vex.net
Mon Apr 14 23:24:36 EDT 2008


Patrick Surry wrote:
> I've seen other discussions that suggest that lists are always shared 
> while in scope (so the fibs trick works).  But is that just a feature of 
> the standard compilers, or is it somewhere mandated in the Hakell spec 
> (I don't see anything obvious in the Haskell Report tho haven't read it 
> cover to cover)?

It is just a feature of most compilers. The Haskell Report does not 
specify sharing.

For most compilers, a sufficient condition for sharing is aliasing, e.g.,

let y = f x in (y,y,y,y,y)

you can be sure that most compilers share one copy of "f x" for those 
five mentions of "y".

As another example,

let x = 0:x in x

you can be sure that most compilers create a tight cyclic graph for that.

In contrast, most compilers may create redundantly new expressions for 
the following:

(f x, f x, f x, f x, f x)

-- given the definition: recurse f = f (recurse f)
recurse (\x -> 0:x)



More information about the Haskell-Cafe mailing list