gpt4 book ai didi

haskell - Haskell 中的 seq 与 rnf

转载 作者:行者123 更新时间:2023-12-01 13:31:44 24 4
gpt4 key购买 nike

根据第29页《Haskell中的并行与并发编程》给出的定义,类方法“返回范式”定义为:

rnf a = a `seq` ()

只是为了看看是否像作者所描述的那样将 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]

那么是作者犯了一个明显的错误,还是我遗漏了什么?

编辑:我意识到我原来的问题包含基本错误。这是问题的正确形式。

最佳答案

Perhaps, by declaring the implementation in class, one is saying that that is the default implementation, unless stated otherwise in a specific instance declaration?

是的,就是这个意思。不寻常的(IMO)是通常这个默认实现对所有实例都是有效的(即做你想做的)但可能会被覆盖以提高效率或打破默认定义的循环(例如在 Eq 中默认实现是 x == y = not (x/= y)x/= y = not (x == y),因此您可以覆盖更方便的那个).

但在 rnf 的情况下,the documentation for 1.3.0.0

The default implementation of rnf ... may be convenient when defining instances for data types with no unevaluated fields (e.g. enumerations).

即它并不适用于几乎所有类型。

此问题已修复since 1.4.0.0 .

关于haskell - Haskell 中的 seq 与 rnf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45717639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com