最外层评估策略如何评估函数的部分应用和柯里化函数的应用

How does the outermost evaluation strategy evaluate partial application of a function and application of a curried function

Hutton 在 Haskell 中的编程说

When evaluating an expression, in what order should the reductions be performed? One common strategy, known as innermost evaluation, is to always choose a redex that is innermost, in the sense that it contains no other redex. If there is more than one innermost redex, by convention we choose the one that begins at the leftmost position in the expression.

Another common strategy for evaluating an expression, dual to innermost evaluation, is to always choose a redex that is outermost, in the sense that it is contained in no other redex. If there is more than one such redex then as previously we choose that which begins at the leftmost position. Not surprisingly, this evaluation strategy is known as outermost evaluation.

在函数的部分应用中,例如 mult(3)(4),其中 mult 定义为

mult    ::  (Int,Int)   ->  Int
mult    (x,y)   =   x   *   y

最内层的求值会先将mult(3)求值为\y->3*y,然后再求(\y->3*y)4。 最外层的评估将如何评估mult(3)(4)

在柯里化函数的应用中,例如mult'(3)(4),其中

mult'   ::  Int ->  Int ->  Int
mult'   x   =   \y  ->  x   *   y

最内层的求值会先将mult'(3)求值为\y->3*y,然后再求(\y->3*y)4。 最外层的评估将如何评估mult'(3)(4)

唯一合理的解释方式:

mult :: (Int, Int) -> Int
mult (x,y) = x * y

在你更大的问题的上下文中是一个一元函数,它接受一个元组类型(Int, Int)的参数。因此,mult 不能部分应用。特别是,mult(3) 没有任何意义,因为 3 不是 (Int, Int).

类型的元组

因此,无论使用最外层归约还是最内层归约,Hutton 所指的表达式 mult (3,4) 的归约都是相同的。这里只有一个redex/application,将mult应用到(3,4),最外层和最内层的归约都会得到归约:

mult (3,4)
=>  3 * 4
=>  12

对于函数:

mult' :: Int -> Int -> Int
mult' x y = x * y

或等同于:

mult' = \x -> (\y -> x * y)

表达式 mult' 3 4 或等效的 (mult' 3) 4 经历了最内层的归约:

(mult' 3) 4
= ((\x -> (\y -> x * y)) 3) 4
=> (\y -> 3 * y) 4
=> 3 * 4
=> 12

奇怪的是,最外层的还原以完全相同的方式进行:

(mult' 3) 4
= ((\x -> (\y -> x * y)) 3) 4     -- (1)
=> (\y -> 3 * y) 4
=> 3 * 4
=> 12

那是因为第 (1) 行中 ((\x -> \y -> x * y) 3)4 的应用,虽然它是最外层的 应用 ,但它不是 redex。它不能减少,因为应用的东西 ((\x -> \y -> x * y) 3) 不是 lambda 表达式。 (这是 lambda 表达式对参数的应用。)

因此,与第一次出现相反,第(1)行中只有一个redex,最内层和最外层归约策略选择相同的redex。