最外部的评估如何在应用咖喱函数时起作用? [英] How does outermost evaluation work on an application of a curried function?
问题描述
mult
被定义为咖喱函数:
mult :: Int -> Int -> Int
mult x = \y -> x * y
在 mult(1 + 2)(2 + 3)
,
- 什么是redex。它们是
mult(1 + 2)
,1 + 2
和2 + 3
? - 什么是最外面的redex,它是
2 + 3
吗?
- what are the redex's. and are they
mult(1+2)
,1+2
and2+3
? - What is the outermost redex, and is it
2+3
?
最里面的评估按以下表达式工作:
Innermost evaluation works on the expression as following, according to Programming in Haskell by Hutton:
mult (1+2) (2+3)
= { applying the first + }
mult 3 (2+3)
= { applying mult }
(\y -> 3 * y) (2+3)
= { applying + }
(\y -> 3 * y) 5
= { applying the lambda }
3 * 5
= { applying * }
15
mult(1 + 2)(2 + 3)
的最外部评估工作?
最外面的评估是否按以下方式工作?
How does outermost evaluation work on mult (1+2) (2+3)
?
Does outermost evaluation works as the following?
mult (1+2) (2+3)
= mult (1+2) 5
= (\y -> (1+2) * y) 5
= (1+2) * 5 // Is (1+2) evaluated before (1+2) * 5, because builtin function "*" is strict, i.e. application of builtin function always happen after evaluation of its args?
= 3*5
= 15
谢谢。
推荐答案
写下分析树:
o
/ \
o o
/ \ /|\
mult o 2 + 3
/|\
1 + 2
(为简单起见,我正在处理二进制中缀 +
运算符作为单个应用程序,它也可能是((+)1)2
)
(For simplicitly I'm treating the binary infix +
operators as a single application, it could have been ((+) 1) 2
as well)
现在最外层的函数应用是 mult(1 + 2)
到参数 2 + 3
的应用,但它不可约,因为函数不是单个值,而是应用程序本身。我们必须首先进行评估:
Now the outermost function application is that of mult (1+2)
to the argument 2+3
, but it's not reducible because the function is not a single value but an application itself. We have to evaluate that first:
(mult (1+2)) (2+3)
((\x->\y->x*y) (1+2)) (2+3) -- the value that `mult` refers to
(\y->(1+2)*y) (2+3) -- evaluate the application of `\x->`
$ b $的应用b
现在我们可以评估根函数应用程序:
Now we can evaluate the root function application:
(1+2) * (2+3) -- application of `\y->`
现在最外面的表达式是 *
,但是您知道这些整数运算符很严格,因此需要首先评估其参数(从左到右,IIRC):
Now the outermost expression is the *
, but as you know these integer operators are strict so they need to evaluate their arguments first (left-to-right, IIRC):
3 * (2+3)
3 * 5
15
这篇关于最外部的评估如何在应用咖喱函数时起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!