About 'pseq' in Haskell

社会主义新天地 提交于 2021-02-08 12:34:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!