Вопрос

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?

Это было полезно?

Решение

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.

Другие советы

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

...can either be compiled to a piece of graph which will be shared by all uses or to some shared code which will overwrite itself with some graph the first time it is evaluated.

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top