Haskell 中评估的单调性

Monotonicity of evaluation in Haskell

Let < denote the semantic approximation order in Haskell.

Then the monotonicity of evaluation guarantees that if e1 < e2 then [[e1]] < [[e2]], where e1, e2 are expressions and [[e1]] denotes the evaluation of e1.

在我关注的注释中,这是一个非常有用的 属性 推理 Haskell 程序,但没有给出示例。

谁能给我一个这样推理的例子?

由于undefined <= 4,根据单调性我们可以得出undefined + 12 <= 4 + 12。的确,前者是undefined,而后者是16。我们也有 [undefined] <= [4],或者更多 f undefined <= f 4

直觉是这样的:假设 f :: Int -> T。当调用 f x 时,f 需要整数参数 x 或不需要。如果是这样,f undefined 将是未定义的(崩溃/异常/未终止),因此 f 4 肯定会比 >=。如果它不要求 x,那么 f undefined = f 4 = f x 对任何 x,所以 <= 再次成立,平凡。

复杂类型越多,事情就越复杂。如果 g :: (Int,Int) -> T 我们有

g undefined
<= g (undefined, undefined)
<= g (undefined, 2)
<= g (3, 2)

单调性背后非常粗略的想法是:如果我们向函数传递一个更明确的参数,我们会得到更明确(或同样明确)的结果。或者更直白地说:更少的崩溃输入,更少的崩溃输出。