简体   繁体   English

在Haskell中seq与rnf

[英]seq vs. rnf in Haskell

According to the definitions given by "Parallel and Concurrent Programming in Haskell" on page 29, the class method "return normal form" is defined as: 根据第29页上的“Haskell中的并行和并发编程”给出的定义,类方法“返回普通形式”定义为:

rnf a = a `seq` ()

Just to see if wrapping seq in another function, as the authors prescribe, really had the result of forcing 'a' to be evaluated to normal form, I tried implementing the function myself and got a result in the negative: 只是为了看看是否在另一个函数中包装seq,正如作者所规定的那样,确实有强制'a'被评估为正常形式的结果,我尝试自己实现该函数并得到否定的结果:

Prelude Control.DeepSeq > myrnf a = a `seq` ()
Prelude Control.DeepSeq > xs = map (+1) [1..10] :: [Int]
Prelude Control.DeepSeq > :sprint xs
xs = _
Prelude Control.DeepSeq > myrunf xs
()
Prelude Control.DeepSeq > :sprint xs
xs = _ : _
Prelude Control.DeepSeq > rnf xs
()
Prelude Control.DeepSeq > :sprint xs
xs = [2,3,4,5,6,7,8,9,10,11]

So have the authors made a glaring mistake, or am I missing something here? 那么作者是否犯了一个明显的错误,或者我错过了什么?

Edit: I realised my original question contained elementary mistakes. 编辑:我意识到我的原始问题包含基本错误。 Here is the proper form of the question. 这是问题的正确形式。

Perhaps, by declaring the implementation in class, one is saying that that is the default implementation, unless stated otherwise in a specific instance declaration? 也许,通过在类中声明实现,可以说这是默认实现,除非在特定实例声明中另有说明?

Yes, that's exactly what it means. 是的,这正是它的意思。 What's unusual (IMO) is that normally this default implementation is valid (ie does what you want) for all instances but may be overridden for efficiency or to break a cycle of default definitions (eg in Eq the default implementations are x == y = not (x /= y) and x /= y = not (x == y) , so you can override whichever is more convenient). 有什么不寻常的(IMO)通常这个默认实现对所有实例都有效(即做你想要的),但可能会被覆盖以提高效率或打破默认定义的循环(例如,在Eq ,默认实现是x == y = not (x /= y)x /= y = not (x == y) ,所以你可以覆盖哪个更方便)。

But in case of rnf , the documentation for 1.3.0.0 says 但是在rnf情况下, rnf 的文档

The default implementation of rnf ... may be convenient when defining instances for data types with no unevaluated fields (eg enumerations). 在为没有未评估字段(例如枚举)的数据类型定义实例时, rnf ...的默认实现可能很方便。

ie it doesn't really work for nearly all types. 即它几乎不适用于所有类型。

This problem has been fixed since 1.4.0.0 . 自1.4.0.0以来,此问题已得到修复。

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

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