Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About 'pseq' in Haskell

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?

like image 306
qddpx Avatar asked Oct 12 '12 05:10

qddpx


1 Answers

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.

like image 192
Philip JF Avatar answered Nov 16 '22 00:11

Philip JF