Take the 2-minute tour ×

While looking into parallel programming, and subsequently evaluation strategies, the question whether thunks are mutable came up. To give an example, let's say I have the following code:

foo = 1 + 2         -- Thunk
bar = foo `seq` foo -- Evaluates foo

Calling seq when evaluating bar evaluates foo, giving bar the normal form value 3. Does this evaluation affect foo as well? That is, is the value of foo still 1+2 or is it 3 after evaluating bar?

share|improve this question
seq x x doesn't act like you expect: it is exactly the same as just x. (seq is a little strange: there is even an entire article about it on the Haskell wiki.) –  huon-dbaupp Jun 19 '12 at 4:34
That Wiki entry gives quite a good answer, wish I could upvote it. –  David Jun 19 '12 at 4:55
If foo is the result of a pure computation, then you'd want it to be evaluated at most once, for the sake of efficiency. It turns out that most compilers do what you would want in this case, given that the Haskell report does not specify the exact behavior. –  Dan Burton Jun 19 '12 at 6:49

2 Answers 2

up vote 8 down vote accepted

The Haskell report only specifies that the evaluation order is "non-strict", so either behavior would conform to the standard.

However, using lazy ("call by need") evaluation, which involves sharing values in a way that makes thunks "mutable" as you describe, offers asymptotic improvements over "call by name" (that is, no sharing).

So in GHC (and probably most other reasonable implementations), foo will become 3 after you force it for the first time. However, this is not mandated by the standard, which you should keep in mind.

share|improve this answer

foo and bar are Constant Applicative Forms, which (quoting from the link):


The second part of this sentence corresponds to lazy evaluation, where foo will be 3 after evaluating bar. The first part is more general (doesn't say what happens to the shared piece of graph), so it leaves a possibility for re-evaluation of foo.

share|improve this answer

Your Answer


