问题
Consider the following two statements:
(a `par` b) `pseq` (a + b)
and
a `par` (b `pseq` (a + b))
Can someone explain how their behavior differ from each other?
For the first one, if the main thread has done with computing b
but the spark computing a
hasn't finished, will the main thread proceed to compute a + b
?
回答1:
par a b
is semantically equivalent to b
, but it gives the hint that it might be useful to start evaluating a
early. On the otherhand pseq
forces the evaluation of its first argument, but is simply the (lazy) identity function in its second argument.
So,
(a `par` b) `pseq` (a + b)
is semantically equivalent to
b `pseq` (a + b)
which is equivalent to
a `par` (b `pseq` (a + b))
in that the both say "evaluate b
then become the thunk a + b
". Given the non precision in the consequences of par
no other difference can be gleamed from the language definition. Rather, on your particular compiler/runtime they might do slightly different things.
来源:https://stackoverflow.com/questions/12852715/about-pseq-in-haskell