Docsity

Programming Languages - Lecture Notes | CSCI 4430, Study notes of Programming Languages

Toggle sidebar
bg1
CSCI.4430/6969 Programming Languages
Lecture Notes
September 1st, 2005
2.2 Order of Evaluation
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.x2(λx.x + 1 2)),
the argument (λx.x + 1 2) should be simplified first. The result is
⇒(λx.x22 + 1) ⇒(λx.x23) ⇒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=x2and 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 consider the expanded expression
(λx.y (λ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=yand M= (λx.(x x)λx.(x x)). Since there are no x’s in Eto
replace, the result is simply y. 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();
}
1
pf3
pf4

Partial preview of the text

Download Programming Languages - Lecture Notes | CSCI 4430 and more Study notes Programming Languages in PDF only on Docsity!

CSCI.4430/6969 Programming Languages

Lecture Notes

September 1st, 2005

2.2 Order of Evaluation

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 consider the expanded expression

(λx.y (λ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 = y and M = (λx.(x x) λx.(x x)). Since there are no x’s in E to replace, the result is simply y. 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) = x + x. To find f (4/2) using normal order, we hold off on evaluating the argument until after placing the argument in the function, so it yields

f (4/2) = 4/2 + 4/2 = 2 + 2 = 4,

and the division needs to be done twice. If we use applicative order, we get

f (4/2) = f (2) = 2 + 2 = 4,

which only requires one division. Since applicative order avoids repetitive computations, it is the preferred method of evaluation in most programming languages, where short execution time is critical.

2.3 Combinators

Any lambda calculus expression with no free variables is called a combinator. Because the meaning of a lambda expression is dependent only on the bindings of its free variables, combinators always have the same meaning independently of the context in which they are used. There are certain combinators that are very useful in lambda calculus: The identity combinator is defined as I = λx.x.

It simply returns whatever is given to it. For example

(I 5) ⇒ (λx.x 5) ⇒ 5.

The identity combinator in Oz can be written:

declare I = fun {$ X} X end

Contrast it to, for example, a Circumference function:

declare Circumference = fun {$ Radius} 2PIRadius end

The semantics of the Circumference function depends on the definitions of PI and *. It is, therefore, not a combinator. The application combinator is App = λf.λx.(f x),

and allows you to evaluate a function with an argument. For example

((App λx.x^2 ) 3) ⇒ ((λf.λx.(f x) λx.x^2 ) 3) ⇒ (λx.(λx.x^2 x) 3) ⇒ (λx.x^2 3) ⇒ 9.

This may seem very much like the definition of the PlusC function. But is the PlusC function a combinator? No, because the function + is a free variable. Remember that the definition of a combinator is that it has no free variables. In Oz, the + operator is considered a procedure that is defined in the Number module and can be accessed as Number.‘+‘. This operator has a specific behavior, making this code specific, not universal. So it is crucial in the definition of the currying combinator that the + function be changed to a generic function F, which can be set to any function you like. The Curry function has no free variables, and therefore is a combinator.

Exercises

  1. Write a function composition combinator in the λ-calculus.
  2. Define a curried version of Compose in Oz, ComposeC, without using the Curry combinator. (Hint: It should look very similar to the λ-calculus expression from the previous exercise.)