Programming Languages (CS 328) Lecture on 2000-01-10 Computer Science and Engineering Gaurav Bhalotia Indian Institute of Technology Bombay

1 Brief Summary Of Previous lecture

1.1 Application and abstraction

The lambda calculus has two basic operations. The first one is application. The expression (F A) denotes the data F considered as alogorithm applied to A considered as input. The theory is type-free; it is allowed to consider expression like (F F), that is, F applied to itself.

The other basic operation is abstraction: if M = M[x] is an expression containing x, then (x M) denotes the map x → M[x].

1.2 Curried Functions

(λI_{1}(λI_{2}T)) ≡ (λ(I_{1}I_{2})T) abstraction
(F T_{c1}T_{c2}) ≡ ((F T_{c1})T_{c2}) application

1.3 Bound and Free Variables

An occurence of a variable is bound if there is an enclosing lambda abstraction which binds it, and is free otherwise. For example, x and y occur bound, but z occurs free in

                                 (λ x (+ (λ y (+ y z)) 7))

The terms ’bound’ and ’free’ refer to specific occurrences of the variable in an expression. This is because a variable may have both a bound occurrence and a free occurence in an expression.

1.4 Congruence

T_{1} ⇔ T_{2} ⟹ (λIT_{1}) ⇔ (λIT_{2})
T_{1} ⇔ T_{2}, T_{2} ⇔ T_{3} ⟹ T_{1} ⇔ T_{3}

2 Reduction Rules

So far we have seen only the syntax of the lambda calculus, but to dignify it with the title of a ‘calculus’ we must say how to ‘calculate’ with it. We will do this by giving three conversion rules which describe how to convert one lambda expression into another.

2.1 β rule

Gives the semantics of evaluating an abstraction

((λIT_{body})T_{op}) ⟺ [T_{op}/I]T_{body}

Rather than regarding β-reduction as an operation, we can regard it as expressing the equivalenceof two expressions which ’look different’ but ’ought to mean the same’.

2.2 η rule

This rule expresses the equivalence of expressions that behave exactly in the same way when applied to an argument.

(λ I (T I)) ↔ T
given F V [[T]] ∉ I

The last condition is to ensure that there are no false conversions.

2.3 α Rule

α-conversion allows us to change the name of the formal paramter of any lambda abstraction, so long as we do consistently. The newly introduced name must not, of course, occur free in the body of the original lambda abstraction.

(λI_{bound}T_{body}) ↔ (λI_{fresh}[I_{fresh}/I_{bound}]T_{body})
such that I_{fresh} ∉ F V [[T_{body}]]

α-conversion is used solely to eliminate name clashes. Notation :λ − expr_{1} ⟹_{β} λ − expr_{2} ⟹_{η} …

2.4 Summary

1. Name changing.α-conversion allows us to change the name of the formal parameter of a lambda abstraction, so long as we do it consistently.

2. Function application. β-reduction allows us to apply a lambda abstraction to an argument, by making a new instance of the body of the abstraction, substituting the argument for free occurrences of the formal parameter. Special care needs to be taken when the argument contains free variables.

3. eliminating redundant lambda abstractionsη-reduction can sometimes eliminate a lambda abstraction.

3 Representation Games

These allow us to define lambda expressions for various functions. (Some expressions are in curried form.)

3.1 Boolean

                                TRUE ≡ (λ (a b) a)
                               FALSE ≡ (λ (a b) b)
                                  IF ≡ (λ (t c a) (t c a))

3.2 Pairs

Packaged up structures with two things.

                              PAIR ≡ (λ (l r) (λ s (s l r)))
                              LEFT ≡ (λ p (p (λ (l r) l)))
                             RIGHT ≡ (λ p (p (λ (l r) r)))

3.3 Integers

Non-negative numbers can be represented in the λ-grammar as “Church numerals:”

                             ¯0 ≡ (λ f (λ a a))
                             ¯1 ≡ (λ f (λ a (f a)))
                             ¯2 ≡ (λ f (λ a (f (f a))))

n̄ ≡ (λ f (λ a (f \underbrace{… (f a) …}_{n applications})))

Operations can also be defined on Church numerals (DEC is tricky):

       ZERO? ≡ (λ n ((n (λ x FALSE)) TRUE))
        INC_{1} ≡ (λ n (λ (f a) n f (f a)))
        INC_{2} ≡ (λ n (λ (f a) (f (n f a))))
       PLUS_{1} ≡ (λ (m n) (λ (f a) ((m f) (n f) a)))
       PLUS_{2} ≡ (λ (m n) (n INC m))
         DEC ≡ (λ n (RIGHT (n (λ p (PAIR (INC (LEFT p)) (LEFT p))) (PAIR ¯0 ¯0) )) )