关于Haskell中的"pseq" [英] About 'pseq' in Haskell
问题描述
请考虑以下两个语句:
(a `par` b) `pseq` (a + b)
和
a `par` (b `pseq` (a + b))
有人可以解释他们的行为有何不同吗?
Can someone explain how their behavior differ from each other?
对于第一个,如果主线程完成了计算 b
的操作,但火花计算 a
尚未完成,则主线程将继续进行 b
的计算吗?> a + b ?
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
?
推荐答案
par ab
在语义上等效于 b
,但它暗示了它可能对尽早开始评估 a
.另一方面, pseq
强制对其第一个参数进行求值,而只是第二个参数中的(懒散)恒等函数.
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.
所以
(a `par` b) `pseq` (a + b)
在语义上等同于
b `pseq` (a + b)
等效于
a `par` (b `pseq` (a + b))
,因为两者都说先评估 b
然后成为笨拙的 a + b
".鉴于 par
结果的不精确性,语言定义不会有其他区别.相反,在您的特定编译器/运行时上,它们可能会做些细微的事情.
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.
这篇关于关于Haskell中的"pseq"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!