这里有两个等价的偏函数:
\True -> \() -> ()
\True () -> ()
申请
False
应该提出 PatternMatchFail
异常(exception)。当我使用
Control.Exception.evaluate
强制从前者生成的 thunk 到 WHNF,它强制 PatternMatchFail
异常(exception):ghci> _ <- evaluate $ (\True -> \() -> ()) False
*** Exception: <interactive>:10:18-35: Non-exhaustive patterns in lambda
当我使用后一种形式时,它不会:
ghci> _ <- evaluate $ (\True () -> ()) False
为什么不?
(在 GHC-8.0.1 上)
最佳答案
来自 Haskell 2010 年的报告:
The following identity holds:
\ p1 … pn -> e = \ x1 … xn -> case (x1, …, xn) of (p1, …, pn) -> e
https://www.haskell.org/onlinereport/haskell2010/haskellch3.html#x8-260003.3
所以
\True () -> e
脱糖至 \x y -> case (x, y) of (True, ()) -> e
, 在部分应用到 False
之后是 \y -> case (False, y) of ...
即,一个函数抽象,所以这不是底部。相比之下,
\True -> f
(其中 f = \() -> e
)脱糖为 \x -> case x of True -> ...
, 也有异常(exception)。
关于haskell - Control.Exception.evaluate 对等效函数生成的 thunk 的处理方式不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56399982/