Can pseq be defined in terms of seq?
As far as I know, seq ab
evaluates (forces) a
and b
before returning b
. It does not guarantee that a
is evaluated first.
pseq ab
evaluates a
first, then evaluates/returns b
.
Now consider the following:
xseq a b = (seq a id) b
Function application needs to evaluate the left operand first (to get a lambda form), and it can't blindly evaluate the right operand before entering the function because that would violate Haskell's non-strict semantics.
Therefore (seq a id) b
must evaluate seq a id
first, which forces a
and id
(in some unspecified order (but evaluating id
does nothing)), then returns id b
(which is b
); therefore xseq ab
evaluates a
before b
.
Is xseq
a valid implementation of pseq
? If not, what's wrong with the above argument (and is it possible to define pseq
in terms of seq
at all)?
上一篇: 弱头正常形式和正常形式
下一篇: 可以用seq来定义pseq吗?