# Lecture: Lambda the Magnificent

### Background Material

• Lecture on functional programming and scheme.

• Chapter 10 (sec. 6) of the text (lambda calculus)
• The scheme reference manual available with MIT-Scheme

### Theme

The semantics of a pure functional programming language can be mathematically described by a substitution process that mimics our understanding of function application - i.e., substitute all occurrences of the formal parameters of a function in the function body by the arguments in the function call.

In order to formally study function definition and application and computability, logicians (Alonzo Church, Stephen Cole Kleene, and others) in the 1930s developed a formal notation called the lambda calculus and rules defining an equivalence relation on lambda expressions that encodes function definition and application. Church and others were able to prove properties of the lambda calculus that provide deep understanding of functions and provide important insight into programming languages. In particular, they showed the universality of the lambda calculus, the uniqueness of normal forms, and the undecidablity of the equivalence of lambda expressions.

### Topics

1. substitution model of function application
1. applicative order [call be value]
2. normal order [call by need]
2. lambda expressions
1. < exp > &rarr variable
2. application: < exp > &rarr (< exp > < exp >)
3. abstraction: < exp > &rarr (lambda variable . < exp >)
3. free and bound variables
4. alpha conversion: (lambda x.E) is equivalent to (lambda y,E[x/y]) provided y does not appear free in E and y is not bound by a lambda when substituted for x in E.
5. beta reduction: ((lambda x.E) F) is equivalent to E[F/x], where F is substituted for all free occurences of the variable x in E, provided all free variables in F remain free when substituted for x in E.
6. Normal forms and the Church-Rosser theorem.
7. Universality of the lambda calculus
1. Church numerals and arithmetic using lambda calculus
2. boolean logic, predicates, and conditional statements using lambda calculus
3. Data structures (lists, cons, car, cdr) using lambda calculus
4. Recursion using lambda calculus
• (define fact (lambda (n) (if (= n 0) 1 (* n (fact (- n 1))))))
• (define g (lambda (f n) (if (= n 0) 1 (* n (f (- n 1))))))
• Y combinator: (define Y (lambda (g) ((lambda (x) (g (x x))) (lambda (x) (g (x x))))))
• fixed point: (g (Y g)) = (Y g)
• (g (Y g) n)

### Sample Functions

• church.scm - scheme implementation of church numerals including successor, addition, and multiplication functions.
• boolean.scm - lambda calculus encoding of boolean logic, predicates, and conditional expressions.
• list.scm - lambda calculus encoding of lists and list primitives cons, car, and cdr.

### Exercises

• Study code implementing Church numerals and the implementation of conditionals and lists in the lambda calculus.
• Trace through substitution of Y combinator (verify fixed point property) and implementation of recursion in factorial.
• Sketch how to implement beta-reduction and alpha-conversion.
Created: May 22, 2008 (modified April 19, 2012) by jjohnson AT cs DOT drexel DOT DOT edu