Introduction To The Lambda Calculus
Introduction To The Lambda Calculus
Roadmap
>
>
>
>
O. Nierstrasz
6.2
References
>
>
>
O. Nierstrasz
6.3
Roadmap
>
>
>
>
O. Nierstrasz
6.4
What is Computable?
Computation is usually modelled as a mapping from inputs to outputs,
carried out by a formal machine, or program, which processes its
input in a sequence of steps.
yes
input
Effectively
computable
function
no
output
6.5
Churchs Thesis
Effectively computable functions [from positive integers to positive
integers] are just those definable in the lambda calculus.
Or, equivalently:
It is not possible to build a machine that is more powerful than a
Turing machine.
Churchs thesis cannot be proven because effectively computable is
an intuitive notion, not a mathematical one. It can only be refuted by
giving a counter-example a machine that can solve a problem not
computable by a Turing machine.
So far, all models of effectively computable functions have shown to be
equivalent to Turing machines (or the lambda calculus).
O. Nierstrasz
6.6
Uncomputability
A problem that cannot be solved by any Turing machine in finite time (or
any equivalent formalism) is called uncomputable.
Assuming Churchs thesis is true, an uncomputable problem cannot be
solved by any real computer.
The Halting Problem:
Given an arbitrary Turing machine and its input tape, will the machine
eventually halt?
O. Nierstrasz
6.7
2.
O. Nierstrasz
6.8
Intensional view:
A function f: A B is an abstraction x.e, where x is a
variable name, and e is an expression, such that when a
value a A is substituted for x in e, then this expression
(i.e., f(a)) evaluates to some (unique) value b B.
O. Nierstrasz
6.9
Roadmap
>
>
>
>
O. Nierstrasz
6.10
6.11
f.(x y)
Application is left-associative
xyz
(x y) z
O. Nierstrasz
f . g.x
6.12
conversion
(renaming):
reduction
(application):
reduction:
x . e y . [ y/x ] e
( x . e1) e2 [ e2/x ] e1
x.ex e
6.13
Beta Reduction
Ix.x
Now consider:
I I = ( x . x) ( x . x )
O. Nierstrasz
[ x . x / x] x reduction
= x.x
substitution
= I
6.14
i=\x>x
?i5
5
(2reductions,6cells)
?ii5
5
(3reductions,7cells)
O. Nierstrasz
6.15
O. Nierstrasz
6.16
A Few Examples
(x.x) y
2. (x.f x)
3. x y
4. (x.x) (x.x)
5. (x.x y) z
6. (x y.x) t f
7. (x y z.z x y) a b (x y.x)
8. (f g.f g) (x.x) (x.x) z
9. (x y.x y) y
10. (x y.x y) (x.x) (x.x)
11. (x y.x y) ((x.x) (x.x))
1.
O. Nierstrasz
6.17
{x}
fv(e1 e2)
fv(e1) fv(e2)
fv( x . e)
fv(e) - { x }
6.18
hello world
O. Nierstrasz
6.19
Roadmap
>
>
>
>
O. Nierstrasz
6.20
[ y / x] ( y . x y)
( y . y y )
reduction
incorrect substitution!
O. Nierstrasz
6.21
Substitution
We must define substitution carefully to avoid name capture:
[e/x] x = e
[e/x] y = y
if x y
if x y and y fv(e)
if x y and
z fv(e) fv(e1)
Consider:
( x . (( y . x) ( x . x)) x ) y [y / x] (( y . x) ( x . x)) x
O. Nierstrasz
= (( z . y ) ( x . x)) y
6.22
Alpha Conversion
O. Nierstrasz
( x z . x z) y
[ y / x] ( z . x z)
( z . y z)
= y
conversion
reduction
reduction
6.23
Eta Reduction
Eta reductions allow one to remove redundant lambdas.
Suppose that f is a closed expression (i.e., there are no free variables in
f).
Then:
( x . f x ) y f y
reduction
6.24
( x y . x y) ( x . x y) ( a b . a b)
( x z . x z) ( x . x y) ( a b . a b)
( z . ( x . x y) z) ( a b . a b)
( x . x y) ( a b . a b)
( a b . a b) y
( b . y b)
y
O. Nierstrasz
6.25
Normal Forms
[ ( x . x x) / x ] ( x x )
=
( x . x x) ( x . x x)
reduction
( x . x x) ( x . x x)
reduction
( x . x x) ( x . x x)
reduction
...
Reduction of a lambda expression to a normal form is analogous to a
Turing machine halting or a program terminating.
O. Nierstrasz
6.26
Evaluation Order
Most programming languages are strict, that is, all expressions passed
to a function call are evaluated before control is passed to the function.
Most modern functional languages, on the other hand, use lazy
evaluation, that is, expressions are only evaluated when they are
needed.
Consider:
sqrn=n*n
Applicative-order reduction:
sqr(2+5)sqr77*749
Normal-order reduction:
sqr(2+5)(2+5)*(2+5)7*(2+5)7*749
O. Nierstrasz
6.27
O. Nierstrasz
6.28
Roadmap
>
>
>
>
O. Nierstrasz
6.29
Non-termination
However, applicative order reduction may not terminate, even if a
normal form exists!
( x . y) ( ( x . x x) ( x . x x) )
Applicative order reduction
( x . y) ( ( x . x x) ( x . x x) )
( x . y) ( ( x . x x) ( x . x x) )
6.30
Currying
O. Nierstrasz
= x.y.x
= b.x.y.(bx)y
6.31
Representing Booleans
Many programming concepts can be directly expressed in the lambda
calculus. Let us define:
True x y . x
False x y . y
not b . b False True
if b then x else y b x y . b x y
then:
not True = ( b . b False True ) ( x y . x )
( x y . x ) False True
O. Nierstrasz
False
6.32
Representing Tuples
Although tuples are not supported by the lambda calculus, they can
easily be modelled as higher-order functions that wrap pairs of values.
n-tuples can be modelled by composing pairs ...
Define:
then:
pair
( x y z . z x y)
first
( p . p True )
second
( p . p False )
(1, 2)
pair 1 2
(pair 1 2) True
True 1 2
first (pair 1 2)
O. Nierstrasz
( z . z 1 2)
6.33
Tuples as functions
In Haskell:
t
f
pair
first
second
=\x>\y>x
=\x>\y>y
=\x>\y>\z>zxy
=\p>pt
=\p>pf
?first(pair12)
1
?first(second(pair1(pair23)))
2
O. Nierstrasz
6.34
O. Nierstrasz
6.35
language?
What happens if you try to program in Haskell? Why?
What do you get when you try to evaluate (pred 0)?
What does this mean?
How would you model numbers in the lambda calculus?
Fractions?
O. Nierstrasz
6.36
ST Introduction
License
http://creativecommons.org/licenses/by-sa/3.0/
1.37