Transcript Lambda

Lambda Calculus
What is the simplest functional language that is still
Turing complete?
Where do functional languages really come from?
Historical Context
Syntax of the Lambda Calculus
Examples
Alpha reduction, Beta reduction
Simulating multiple arguments: “Currying”
Combinators
True, False, and the IF-THEN-ELSE construct
Church numerals
Church’s Y combinator; recursion; Factorial
Church’s Thesis
CSE 341 -- S. Tanimoto
Lambda Calculus
1
Historical Context
Like Alan Turing, another mathematician, Alonzo Church, was
very interested, during the 1930s, in the question “What is a
computable function?”
He developed a formal system known as the pure lambda
calculus, in order to describe programs in a simple and precise
way.
Today the Lambda Calculus serves as a mathematical foundation
for the study of functional programming languages, and
especially for the study of “denotational semantics.”
Reference: http://en.wikipedia.org/wiki/Lambda_calculus
CSE 341 -- S. Tanimoto
Lambda Calculus
2
Syntax of the Lambda Calculus
expr
expr
expr
expr
var
var
::=
::=
::=
::=
::=
::=
var
 var . expr
expr expr
( expr )
x|y|z|f|n
var 
CSE 341 -- S. Tanimoto
Lambda Calculus
3
Examples
x
x.x
x.x y
x.y.y
A variable by itself
An identity function
The identity function applied to y
A function that returns the identify function
CSE 341 -- S. Tanimoto
Lambda Calculus
4
Constants in Lambda Calculus
Constants can be added to the syntax of the Lambda
Calculus: e.g., 0, 1, 2, …
However, in principle they are not necessary, because
certain expressions can be used to represent them.
0 = x.y.y
1 = x.y. (x y)
2 = x.y. (x (x y))
Etc.
A fn that returns an ident. fun.
A fn that applies a given fn. 1 time.
A fn that applies a given fn. twice.
These are called Church numerals.
Other, symbolic, constants can also be added with the
understanding that they, too, can be represented by some arbitrary
L.C. expressions.
E.g., Apple = x.y.y (x.y.y)
CSE 341 -- S. Tanimoto
Lambda Calculus
5
The Successor Function
Arithmetic operations can be defined using Lambda Calculus
primitives. As an example, here is the function to return one plus a
given number.
SUCCESSOR(x)
=
x. y. z. (y (x y z))
CSE 341 -- S. Tanimoto
Lambda Calculus
6
Booleans in Lambda Calculus
Boolean constants can be added to the syntax of the
Lambda Calculus: i.e., true, false
However, in principle they are not necessary, because
certain expressions can be used to represent them.
False = 0 = x.y.y
True
= x.y.x
A fn that returns an ident. fun.
A fn that returns its “first arg.”
CSE 341 -- S. Tanimoto
Lambda Calculus
7
Evaluation of Expressions
Alpha conversion:
Renaming of a bound variable and its bound
occurrences.
x.y.y
 x.z.z
CSE 341 -- S. Tanimoto
Lambda Calculus
8
Evaluation of Expressions
Beta reduction:
Application of a function to an expression by means of a
substitution of the expression for all bound occurrences
of the argument variable in the body of the function.
x.x y
 y
Beta reduction must not be permitted to do variable
capture. If capture would occur, use alpha conversion
first to rename variables.
When as many beta reductions as possible have been
applied, the resulting expression is in normal form.
CSE 341 -- S. Tanimoto
Lambda Calculus
9
Simulating multiple arguments
As defined, a Lambda Calculus function can only take
one argument.
However, the effect of allowing multiple arguments to a
function can be obtained using “currying”.
(x. y. (+ x y)) 3 5
A 2-parameter function is simulated with two 1parameter functions. The result of applying the first
function is another function. This new function accepts
the second argument and produces the result of the
simulated 2-parameter function.
More parameters can be handled similarly.
CSE 341 -- S. Tanimoto
Lambda Calculus
10
Combinators
A combinator is a function in the Lambda Calculus
having no free variables.
x. y. (x y)
x. y. (x z)
is a combinator
is not a combinator
Combinators can serve nicely as modular building
blocks for more complex expressions.
The Church numerals and simulated booleans are examples of
useful combinators.
CSE 341 -- S. Tanimoto
Lambda Calculus
11
Conditionals in Lambda Calculus
IF-THEN-ELSE:
x. y. z. ((x y) z)
Example evaluation of a conditional
(a case where the condition is true).
if true then apple else pear
x. y. z. ((x y) z) true apple pear
x. y. z. ((x y) z) x.y.x apple pear
y. z. ((x.y.x y) z) apple pear
z. ((x.y.x apple ) z) pear
((x.y.x apple ) pear)
(y. apple ) pear)
apple
CSE 341 -- S. Tanimoto
Lambda Calculus
12
Conditionals in Lambda Calculus
Example evaluation of a conditional
(a case where the condition is false).
if false then apple else pear
x. y. z. ((x y) z) false apple pear
x. y. z. ((x y) z) x.y.y apple pear
y. z. ((x.y.y y) z) apple pear
z. ((x.y.y apple ) z) pear
((x.y.y apple ) pear)
(y.y pear)
pear
CSE 341 -- S. Tanimoto
Lambda Calculus
13
Recursion in Lambda Calculus
Recursion is not directly supported in the Lambda
Calculus.
However, it can be simulated using Church’s Y
combinator:
Y = (x . (y. x (y y)) (y. x (y y)))
This has the unusual property of being a “fixed-point” combinator:
 f (Y f)
Here  means reduces via some number of beta reduction steps.
Yf
CSE 341 -- S. Tanimoto
Lambda Calculus
14
Factorial in the Lambda Calculus
Define H as follows, to represent 1 step of recursion.
Note that ISZERO, MULT, and PRED represent particular
combinators that accomplish these functions.
H = ( f.  n.(ISZERO n) 1 (MULT n (f (PRED n))))
Then we can create
FACTORIAL = Y H
= (x . (y. x (y y)) (y. x (y y))) ( f.  n.(ISZERO n) 1 (MULT n (f (PRED n))))
Reference: http://en.wikipedia.org/wiki/Y_combinator
CSE 341 -- S. Tanimoto
Lambda Calculus
15
Comment
The use of the Lambda Calculus to express recursion in
this manner is more a theoretical exercise to show the
power of combinators. Few programmers would want to
write software directly in the pure Lambda Calculus!
CSE 341 -- S. Tanimoto
Lambda Calculus
16
Church’s Thesis
If a function can be computed, then it can be
expressed in the pure lambda calculus.
Also, Turing Machines and the Lambda Calculus
have equivalent expressive power, in terms of
which functions can or cannot be expressed.
CSE 341 -- S. Tanimoto
Lambda Calculus
17
Concluding Remarks
It is easy to implement a lambda calculus system
in Lisp (esp. Scheme) or ML. The LET construct in
these languages provides a binding method
similar to that of LAMBDA, except for a change in
order.
Both Lisp and ML themselves can be viewed as
extensions of the Lambda Calculus.
Functional abstraction is an important
programming concept not only in theory, but also
in practice.
CSE 341 -- S. Tanimoto
Lambda Calculus
18