Lambda calculus (2) reduction! Reduction! Reduction!

(one)
The formal definition of the λ term is not listed here, just remember that the semantic construction of the λ expression is:
  1. x
A single variable name is a lambda term expression;
  1. (λx.M)
The λ represents a function. where M is the body of this function, and M itself is a  λ term.
In addition to x, there may be other variable names in M, and the λ symbol is used to indicate that the parameter of the function body M is x.
For ease of understanding, M can be regarded as a function body, and x can be regarded as a formal parameter, that is, a variable name.
 
For example: λx.x+3 means a function f(x) = x+3, which returns the result of adding x and 3
It should be noted that my writing here is not standardized. In the λ expression, the binary operator is used as a prefix. x+3 should be written as (+ x 3). So the normal way to write this expression is λx.(+ x 3). As to why, we will talk about it later.
  1. (M N)
where M and N are both λ term expressions.
If M is itself a function, we say to apply the function M to N. If M is not a function and takes no arguments, N will be ignored during evaluation.
For example: (λx.(+ x 3) 4), where M is the expression λx.x+3 and N is 4. This expression says to apply the function to 4, i.e. f(4) 
 
So a typical λ expression has the form:
  (λx.M) N, written λx.MN without parentheses for convenience
 
Among them, M is the function body, x can be regarded as a formal parameter, that is, the variable name, and N can be regarded as an actual parameter, that is, the variable value.
 
The function itself can also be used as a parameter, for example (λx.x) (λx.(+x 3)), the left side of the expression is a function, this function will return its own parameters, apply this function to the function f we said earlier , the argument is f, then the function will return f.
 
As such, the first form of the lambda expression represents a variable;
The second expression represents a function;
The third expression represents applying the function to a value, a function call.
 
(2) Reduction
The value of the first expression is itself;
The value of the second expression represents the function itself;
Therefore, when discussing evaluation, we are only concerned with the third, that is, how a function is evaluated when it is applied to another lambda expression.
 
As mentioned above, the application of the lambda expression is equivalent to a function call. We know that when the function is called, all the formal parameters are replaced with the values ​​of the actual parameters, and the calculation result is returned. Therefore, we say that in the expression λx.MN, the x in the function body M is bound to the expression N, and the remaining unbound variables in M ​​are free variables.
 
When evaluating λx.MN, all x's in M ​​will be replaced by N. This process is called reduction. After reduction, a simplified λ expression is obtained, which can be further reduced until it can no longer be reduced. The expression at this time is the result of evaluating the original expression.
 
Consider a function f(x)=x+3, written λ expression is (λx.(+x 3)).
As before, when we want to compute f(4), applying the function to 4, the expression is λx.(+ x 3) 4.
According to the law of reduction, the x in the function body (+ x 3) is replaced by 4, the function expression becomes (+ 4 3), and the result is 7.
 
Consider another function g(y)=y*2, the λ expression λy.(* y 2).
If we want to apply the function g to the computed result of f(4), i.e. g(f(4)), we can apply the λ expression of g to the above expression, i.e. λy.(* y 2) (λx. (+ x 3) 4)
 
Then we do the reduction, there are two reduction orders here.
 
One is to calculate the value of the expression on the right side as before, which is reduced to λy.(* y 2) 7.
Then replace y in the function body (* y 2) with 7 to get (* 7 2).
The final result is 14.
 
Another reduction order is to compute the outermost application first, that is, replace y with the expression on the right (λx.(+ x 3) 4), resulting in the reduced expression (* (λx.(+ x 3 ) 4) 2).
Re-reduce the inner application, replacing x to get (* (+ 4 3) 2).
The final result is (* 7 2)=14.
 
In this example, different reduction orders yield the same result, however, this is not a common phenomenon. This hides a common concept in computer programs: scope.
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325063580&siteId=291194637