[Haskell-cafe] Re: CAF's in Haskell

Jon Fairbairn jon.fairbairn at cl.cam.ac.uk
Thu Dec 27 06:25:17 EST 2007

"Neil Mitchell" <ndmitchell at gmail.com> writes:
> I should have been more precise with my question. Given the code:
> fred = 2 + 2
> bob = fred + fred
> In a Haskell implementation fred would be evaluated once to 4, then
> used twice. The 2+2 would only happen once (ignore defaulting and
> overloaded numerics for now).

Not necessarily.  The Haskell 98 standard very carefully
avoids mandating any particular evaluation strategy beyond
that it should be non-strict. So bob is always going to be
8, but just how it gets there is up to the implementation.

If you'd had

fred = [1..]


bob = do something with fred
         a lot of other stuff
         something else with fred

it's much less obvious that keeping the first-calculated
value for fred around the whole time is the right thing to

> Do all Haskell compilers support the sharing.

I don't know all Haskell compilers, but I'm pretty sure
there have been implementations that don't, or don't always
because of the above problem.

Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk

More information about the Haskell-Cafe mailing list