【发布时间】:2018-07-13 21:18:41
【问题描述】:
在评论another question 时,我发现引用透明度明显中断。在 ghci 中:
> f g h = g `seq` h `seq` \x -> g (h x)
> seq (f undefined id) ()
()
> seq (undefined `seq` id `seq` \x -> undefined (id x)) ()
*** Exception: Prelude.undefined
CallStack (from HasCallStack):
error, called at libraries/base/GHC/Err.hs:79:14 in base:GHC.Err
undefined, called at <interactive>:3:14 in interactive:Ghci9
为什么第一个不是底部? (GHC 8.2.2 以防万一——我目前没有在这台机器上安装任何其他版本。)
【问题讨论】:
-
GHC 8.2.1 产生相同的行为
-
在我看来
f中的 lambda 正在浮动。奇怪,但也许在某种程度上是有效的。或者,在某些版本的 ghc 中浮动 lambda 时,可能不尊重seq。 -
我可以在 8.0 和 8.4 上重现这种令人惊讶的行为。它看起来肯定是错误的。编译后的程序会抛出异常。
-
这似乎不会发生在已编译的程序中,只会发生在 ghci 中。
-
简体:
f g = g `seq` \x -> x.