Sharing

From HaskellWiki
Jump to navigation Jump to search

Sharing means that temporary data is physically stored, if it is used multiple times.

In

let x = sin 2
in  x*x

x is used twice as factor in the product x*x. Due to Referential transparency it does not play a role, whether sin 2 is computed twice or whether it is computed once and the result is stored and reused. However, when you write let the Haskell compiler will certainly decide to store the result. This can be the wrong way, if a computation is cheap but its result is huge. Consider

[0..1000000] ++ [0..1000000]

where it is much cheaper to compute the list of numbers than to store it with full length.

Because the sharing property cannot be observed in Haskell, it is hard to transfer the sharing property to foreign programs when you use Haskell as an Embedded domain specific language. You must design a monad or use unsafePerformIO hacks, which should be avoided.