



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




In lambda calculus and Scheme, the lines are blurred between functions (or code) and data. For example, we may redefine the multiplication operation:
(define mult *)
We may then use it just as you would the times operation:
(* 2 3) 6 (mult 2 3) 6
Let’s define the increment function in Scheme:
(define increment (lambda (x) (+ x 1)))
Note that in Scheme, parentheses are critical. The command
(increment (increment 1)) 3
is valid, but adding an extra set of parentheses creates an error:
((increment (increment 1))) Error! 3 is not a function.
This is because the Scheme interpreter always expects the first item after an open parenthesis to be a function. In this case, however, the statement evaluates to (3), then Scheme tries to find the function 3 and fails. Since Scheme can treat functions as data to other functions, we can create a function that composes two other functions:
(define compose (lambda (f g) (lambda (x) (f (g x)))))
Applying this to the increment function, we can create a function that in- crements by two:
(define inc2 (compose increment increment)) (inc2 3) 5
Unlike in lambda calculus, functions in Scheme are allowed to take more than one variable. The process of currying can then be avoided. However, it is pos- sible to use currying in Scheme if you want to. Take for example the definition of a simple addition function that takes two variables:
(define plus (lambda (x y) (+ x y)))
This function is used simply by placing the function name and the two arguments into a set of parentheses
(plus 4 2) 6
If we instead want to use currying (that is, limit the number of arguments to one per function), we need to create a function that takes the first argument and returns a function which generates the proper answer when provided with the second argument. In Scheme this is coded by
(define plusc (lambda (x) (lambda (y) (+ x y))))
This code more closely resembles the lambda calculus statement
λx.λy.x + y
that uses currying. To use the Scheme function, we must supply the arguments one at a time. If we just provide the first argument, we obtain a function:
(define plus2 (plusc 2))
This function will produce the correct output when supplied with the other argument; that is the function will perform addition by two:
since the final x is unbound, or free. To simplify the expression
(λx.(λx.x^2 x + 1) 2)
You could let E = (λx.x^2 x + 1) and M = 2. The only free x in E is the final one so the correct reduction is
(λx.x^2 2 + 1).
The x in x^2 is bound, so it is not replaced. To complicate things further, it is possible when performing β -reduction to inadvertently change a free variable into a bound variable, which changes the meaning of the expression. In the statement
(λx.λy.(x y) (y w)),
the second y is bound to λy and the final y is free. Taking E = λy.(x y) and M = (y w), we could mistakenly arrive at the simplified expression
λy.((y w) y).
But now both the second and third y’s are bound, because they are both a part of of the λy function definition. This is wrong because one of the y’s should remain free as it was in the original expression. To get around this, we can change the λy expression to a λz expression
(λx.λz.(x z) (y w)),
which again does not change the meaning of the expression. This process is called α -renaming. Now when we perform the β -reduction, the original two y variables are not confused. The result is
λz.((y w) z).
Here, the free y remains free.
There are different ways to evaluate lambda expressions. The first method is to always fully evaluate the arguments of a function before evaluating the function itself. This order is called applicative order. In the expression
(λx.x^2 (λx.x + 1 2)),
the argument (λx.x + 1 2) should be simplified first. The result is
⇒ (λx.x^2 2 + 1) ⇒ (λx.x^2 3) ⇒ 32 ⇒ 9.
Another method is to evaluate the left-most redex first. A redex is an expression of the form (λx.E M ), on which β -reduction can be performed. This order is
called normal order. The same expression would be reduced from the outside in, with E = x^2 and M = (λx.x + 1 2). In this case the result is
⇒ (λx.x + 1 2)^2 ⇒ (2 + 1)^2 ⇒ 9.
As you can see, both orders produced the same result. But is this always the case? It turns out that the answer is “no” for certain expressions whose simplification does not terminate. Consider the expression
(λx.(x x) λx.(x x)).
It is easy to see that reducing this expression gives the same expression back, creating an infinite loop. If we expand the expression to
(λx. 3 (λx.(x x) λx.(x x))),
we find that the two evaluation orders are not equivalent. Using applicative order, the (λx.(x x) λx.(x x)) expression must be evaluated first, but this never terminates. If we use normal order, however, we evaluate the entire expression first, with E = 3 and M = (λx.(x x) λx.(x x)). Since there are no x’s in E to replace, the result is simply 3. It turns out that it is only in these particular non-terminating cases that the two orders may give different results. The Church-Rosser theorem (also called the confluence property or the diamond property) states that if a lambda calculus expression can be evaluated in two different ways and both ways terminate, both ways will yield the same result. Also, if there is a way for an expression to terminate, using normal order will cause the termination. In other words, normal order is the best if you want to avoid infinite loops. Take as another example the C program
int loop() { return loop(); }
int f(int x, int y) { return x; }
int main() { return f(3, loop()); }
In this case, using applicative order will cause the program to hang, because the second argument loop() will be evaluated. Using normal order will terminate because the unneeded y variable will never be evaluated. Though normal order is better in this respect, applicative order is the one used by most programming languages. Why? Consider the function f (x) =