简体   繁体   English

Haskell的“评估”是否会降低到正常水平或WHNF?

[英]Does Haskell's 'evaluate' reduce to normal or WHNF?

I understand ( I think ) that Haskell's seq , will (generally) reduce its first argument to WHNF , and see this behavior as expected in GHCi: 我理解( 我认为 )Haskell的seq将(通常) 其第一个参数减少WHNF ,并在GHCi中看到这种行为:

λ> let x = (trace "foo" Foo (trace "bar" Bar 100)) in seq x 0
foo
0

However, though the documentation for evaluate says that it also reduces its argument to WHNF, it looks like it actually fully reduces its argument to normal form: 但是,虽然evaluate文档说它也减少了它对WHNF的参数,但看起来它实际上完全将它的参数减少到正常形式:

λ> let x = (trace "foo" Foo (trace "bar" Bar 100)) in evaluate x
foo
Foo bar
(Bar 100)

I can confirm this (apparent) discrepancy with 我可以确认这个(明显的)差异

λ> let y = (trace "foo" Foo (trace "bar" Bar 100))
λ> seq y 0
foo
0
λ> :sprint y
y = <Foo> _

and

λ> let z = (trace "foo" Foo (trace "bar" Bar 100))
λ> evaluate z
foo
Foo bar
(Bar 100)
λ> :sprint z
z = <Foo> (<Bar> 100)

If the documentation for evaluate is correct, shouldn't the behavior of seq and evaluate be the same? 如果evaluate文档是正确的,那么seqevaluate的行为evaluate应该相同? What am I missing here (as a Haskell beginner)? 我在这里缺少什么(作为Haskell初学者)?

What you are missing is that GHCi also prints the result of IO actions (if they can be shown and are not () ), which does cause it to evaluate to normal form. 您缺少的是GHCi还会打印 IO操作的结果(如果它们可以显示而不是() ),这导致它评估为正常形式。 Try instead: 尝试改为:

λ> let x = (trace "foo" Foo (trace "bar" Bar 100)) in evaluate x >> return ()
foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM