



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!




The normal order sequencing combinator is
Seq = λx.λy.(λz.y x)
where z is chosen so that it does not appear free in y. This combinator guarantees that x is evaluated before y, which is important in programs with side- effects. Assuming we had a “display” function sending output to the console, an example is
((Seq (display “hello”)) (display “world”))
The combinator would not work in applicative order (call by value) evaluation because evaluating the display functions before getting them passed to the Seq function would defeat the purpose of the combinator: to sequence execution. In particular, if the arguments are evaluated right to left, execution would not be as expected. The applicative-order sequencing combinator can be written as follows:
ASeq = λx.λy.(y x)
where y is a lambda abstraction that wraps the original last expression to evaluate. The same example above would be written as follows:
((ASeq (display “hello”)) λx.(display “world”))
with x fresh, that is, not appearing free in the second expression. This strategy of wrapping a lambda-calculus expression to make it a value and delay its evaluation is very useful. It enables to simulate call by name parameter passing in languages using call by value. The process of wrapping is also called freezing an expression, and the resulting frozen expression is called a thunk. Evaluating a thunk to get back the original expression is also called thawing.
Consider the expression (λx.(λx.x^2 x) y).
Using β-reduction, we can take E = (λx.x^2 x) and M = y. In the reduction we only replace the one x that is free in E to get β → (λx.x^2 y).
We use the symbol β → to show that we are performing β-reduction on the expression (As another example we may write λx.x^2 α → λy.y^2 since α-renaming is taking place).
Another type of operation possible on lambda calculus expressions is called η-conversion (“eta”-reduction when applied from left to right). We perform η-reduction using the rule
λx.(E x) η → E.
η-reduction can only be applied if E is a lambda expression taking a single argument, and x does not appear free in E. Starting with the expression, λx.(λx.x^2 x), we can perform η-reduction to obtain
λx.(λx.x^2 x) η → λx.x^2.
Starting with the same expression as before, (λx.(λx.x^2 x) y), we can perform η-reduction to obtain
(λx.(λx.x^2 x) y) η → (λx.x^2 y),
which gives the same result as β-reduction. Another example of η-reduction follows:
λx.(y x) η → y.
η-reduction can be considered a program optimization. For example, consider the following Oz defini- tions:
declare Increment = fun {$ X} X+1 end
declare Inc = fun {$ X} {Increment X} end
Using η-reduction, we could reduce {Inc 6} to {Increment 6} avoiding one extra-function call. This compiler optimization is also called inlining. η-conversion can also affect termination of expressions in applicative order expression evaluation. For example, the Y reduction combinator has a terminating applicative order form that can be derived from the normal order combinator form by using η-conversion (see Section 2.6.)
The recursion combinator allows recursion in lambda expressions. For example, suppose we want to implement a recursive version of the factorial operation,
n! =
1 if n = 0 n(n − 1)! if n > 0
We could start by attempting to write a the recursive function f in the lambda calculus (assuming it has been extended with conditionals, and numbers):
f : λgλn(if (= n 0) 1 (∗ n (g (− n 1)))).
This function does not work yet because it does not receive a single argument. Before we can input an integer to the function, we must input a function to satisfy g so that the returning function is the desired factorial. Let’s call this function X. Looking within the function, we see that the function X must take an integer and return an integer, that is, its type is Z → Z. The function f will return the proper recursive function with the type Z → Z, but only when supplied with the correct function X. Knowing the input and output types of f , we can write the type of f as
f : (Z → Z) → (Z → Z).
For example, the Curry combinator, whose type is:
Curry : (Z × Z → Z) → (Z → (Z → Z)).
is a higher-order (third order) function. It takes a function of type Z × Z → Z and returns a function of type Z → (Z → Z). That is, Curry takes a first-order function and returns a second-order function. The ability to view functions as data is called higher-order programming.
Higher-order programming is a very powerful technique, as shown in the following Oz example. Consider an exponential function, Exp, as follows:
declare Exp = fun {$ B N} if N==0 then 1 else B * {Exp B N-1} end end.
And recall the Curry combinator in Oz:
declare Curry = fun {$ F} fun {$ X} fun {$ Y} {F X Y} end end end.
We can create a function to compute the powers of 2, TwoE, by just using: declare TwoE= {{Curry Exp} 2}.
If we want to create a Square function, using Exp, we can create a reverse curry combinator, RCurry, as:
declare RCurry = fun {$ F} fun {$ X} fun {$ Y} {F Y X} end end end,
where the arguments to the function are simply reversed. We can then define Square as:
declare Square = {{RCurry Exp} 2}. Lisp is a programming language that has demonstrated the use of higher-order programming to an extreme: a full Lisp interpreter written in Lisp, treats Lisp programs simply as data. This allows the interpreter program to be an input to itself.
The λ-calculus is a Turing-complete language, that is, any computable function can be expressed in the pure lambda calculus. In many of the examples, however, we have said: “assume that the calculus has been extended with numbers and conditionals”. Let us see one possible representation of numbers in the pure lambda calculus:
| 0 | = λx.x | 1 | = λx.λx.x ... |n + 1| = λx.|n| That is, zero is represented as the identity combinator. Each succesive number (n + 1) is represented as a lambda abstraction (or procedure) that takes any value and returns the representation of its predecessor (n). You can think of zero as a first-order function, one as a second-order function, and so on. In Oz, this would be written:
declare Zero = I
declare Succ = fun {$ N} fun {$ X} N end end
Using this representation, the number 2, for example, would be the lambda-calculus expression: λx.λx.λx.x, or equivalently in Oz:
{Succ {Succ Zero}}
Now, let us see one possible representation of booleans in the pure lambda calculus:
|true| = λx.λy.x |f alse| = λx.λy.y |if | = λb.λt.λe.((b t) e) That is, true is represented as a function that takes two arguments and returns the first, while false is represented as a function that takes two arguments and returns the second. if is a function that takes:
and returns either t if b represents true, or e if b represents false.
Give an example of using the {RCurry Compose} function.