Professional Documents
Culture Documents
Andrzej S. Murawski
University of eicester
http://www.cs.le.ac.uk/people/amurawski/mgs2011-tlc.pdf
THE COURSE
The aim of the course is to provide an introduction to
the lambda calculus along with a selection of results on
its operational and denotational semantics.
ONLINE RESOURCES
Online Resources
http://www.lfcs.inf.ed.ac.uk/reports/98/ECS-LFCS-98-381/
http://www.paultaylor.eu/stable/prot.pdf
ftp://ftp.cs.ru.nl/pub/CompMath.Found/HBK.ps
H. Barendregt. The impact of the lambda calculus in logic and computer science (1997)
ftp://ftp.cs.ru.nl/pub/CompMath.Found/church.ps
R. Cardone, J. R. Hindley. History of Lambda-Calculus and Combinatory Logic (2006)
http://www-maths.swan.ac.uk/staff/jrh/papers/JRHHislamWeb.pdf
SOME HISTORY
The lambda-calculus originated in order
to study functions more carefully. Rosser
The untyped -calculus was originally part of a
formal system proposed by Alonzo Church in
1928 with the intention of providing improved
foundations of mathematics in turbulent times.
A. Church. A set of postulates for the foundation of logic. Annals of
Mathematics 33:346-366 (1932)
A. Church. A set of postulates for the foundation of logic (second paper).
Annals of Mathematics 34:839-864 (1933)
1903-1995
MORE HISTORY
Initially the -calculus was seen as a poor relative and was
not the primary focus of research in Churchs group.
1909-1994
1907-1989
FUNCTION ABSTRACTION
f (x) = x + 1
x(x + 1)
x => x+1
fn x => x+1
fn x -> x+1
[ :x | x+1 ]
Function(x) x+1
WHY LAMBDA?
Class-abstraction [Russell and Whitehead, 1913]
x
((x))
x((x))
x((x))
UNTYPED LAMBDA-TERMS
Let V be a countably infinite set of variables.
Variables
Any x V is a -term.
Function
Application If M, NAny
are -terms,
(M N ) is a -term.
Variables
x V isthen
a -term.
Function
Abstraction
If x VIfand
then
(x.M
) is) ais-term.
Function
Application
M, M
N is
area -term,
-terms,
then
(M N
a -term.
Function Abstraction
Notational conventions
It is common to omit brackets or s on the understanding that
Notational conventions
For example,
xyz.xyz
is meant to abbreviate (x.(y.(z.((xy)z)))). We
x
1 x2 xn .M stands for (x1 .(x2 . (xn .M ) )).
will also write xyz.(xy)z.
SOME EXAMPLES
Some Examples
yx
f.x.f x
xx
y(y(yx))
f x.f x
yyyx
f x.f (f x)
((yy)y)x
f x.f (f (f x))
BINDING
Binding
Like a quantifier in logic, is a binding construct with scope that
extends over the body of the abstraction (indeed Church wrote x(M )
instead of x.M ).
An occurrence of x in M is free if it is not in scope of any x. The
free occurrences of x in f xg(x.hzx)(hx) have been underlined below.
Note that there can be occurrences of x that are not free (we call them
bound ).
f xg(x.hzx)(hx)
The outermost x in x.M binds only free occurrences of x in M .
ALPHA-EQUIVALENCE
alpha-equivalence
Names of bound variables are just a notational device to represent the binding
structure within terms.
a.a
b.b
c.c
x.f xg(x.hzx)(hx)
d.d
e.e
x.f xg(y.hzy)(hx)
f.f
g.g
h.h
y.f yg(x.hzx)(hy)
Dierent variable names can be used to convey the same information about
binding. Then we talk of -equivalent terms, written M N .
Nominal set theory allows for an elegant formalization of -equivalence!
ALPHA-EQUIVALENCE
Any term in the -equivalence class of M can be obtained from M by a
renaming of bound variables. The renaming must not modify the internal
binding structure (the same occurrences of variables must be bound by the
same occurrences of s).
x.x.x x.y.y x.y.x
x.f (y.y) y.f (x.y)
f.f (x.f (y.y)) f.f (x.f (y.x))
y.yx x.xy y.yy
DE BRUIJN INDICES
de Bruijn indices
An alternative representation of binding: replace each bound occurrence of a
variable with a numerical index that indicates the exact position of its binder
relative to all potential binders (e.g. 1 means the innermost whose scope
covers the relevant occurrence).
x.y.z.xz(yz)
31(21)
Two terms are -equivalent if their de Bruijn representations are the same.
EQUIVALENCE CLASSES
Some examples
x.x.x x.y.y x.y.x
1 = 2
f.f (x.f (y.y)) f.f (x.f (y.x))
1(2(1)) = 1(2(2))
In what follows we shall identify terms with their -equivalence class without
using the explicit notation [ ] . Care must then be taken to ensure that
all definitions respect -equivalence.
-rule
BETA RULE
=
=
=
=
N
y
x = y
(M1 [N/x] M2 [N/x])
(y.M [N/x])
Then
((x.y.f xy) y) z (y.f yy)z f zz
During the substitution of y for x in (y.f xy) the substituted occurrence y
becomes captured by y!
CAPTURE
AVOIDANCE
Capture-avoiding substitution
x[N/x]
y[N/x]
(M1 M2 )[N/x]
(y.M )[N/x]
=
=
=
=
N
y
x = y
(M1 [N/x] M2 [N/x])
(y.M [N/x])
x=
y, y fv (N )
Note that (y.f xy)[y/x] is now undefined. But there exist terms M such
that (y.f xy) M and M [y/x] is defined. For example
(z.f xz)[y/x] = (w.f yw).
This is no coincidence: whenever M [N/x] is not defined, it will be possible
to find M M such that M [N/x] is defined. For instance, one can simply
refresh the bound variables in M , i.e. rename them using variable names
not occurring in M or N .
This is the notion of substitution we shall rely on in the course!
BETA-STEP
Beta-reduction revisited
Basic -step
(x.M )N b M [N/x]
provided M M and M [N/x] is defined. Note that M [N/x] is determined
uniquely up to -equivalence, so the above definition gives rise to a welldefined operation on -equivalence classes of terms.
(x.y.f xy)y b w.f yw
(x.M )N is called a -redex.
ONE-STEP REDUCTION
one-step reduction
We write M 1 M if M is obtained from M by carrying out one basic step inside M (one-step -reduction). Formally, 1 is the smallest relation
satisfying the following rules.
M b M
M 1 M
M 1 M
M N 1 M N
M 1 M
x.M 1 x.M
M 1 M
N M 1 N M
BETA-REDUCTION
Beta-reduction
The reflexive and transitive closure of 1 , written 1 or or
TERMINOLOGY
Terminology
A term M is normal (in normal form) if there is no N such
that M 1 N .
A term M is weakly normalizing if there exists normal N
such that M N . We call N a normal form of M . If
a term is not weakly normalizing, we write M .
A term M is strongly normalizing if there is no infinite
sequence of terms M1 , M2 , such that
M 1 M1 1 M2 1 .
CHURCH-ROSSER
Confluence
If a term is weakly normalizing we can reduce it to some normal form. Are
normal forms of terms determined uniquely (up to -equivalence)? Is equality a consistent theory of -terms?
The two questions can be answered in the positive thanks to the following
confluence property of (for -equivalence classes of terms): whenever
M
M1
M2
M2
UNIQUENESS OF NF
Corollaries
N1
N2
CONSISTENCY OF BETA
1
beta-equality
Recall that -equivalence was defined the smallest equivalence relation containing 1 . More precisely, M = N i there exist Q1 , , Qk such that
M
Q1
...
Qk
where S T stands for S 1 T or T 1 S. Thanks to the ChurchRosser Theorem we can conclude that M = N if and only if there exists Q
such that
M
N
CHURCH NUMERALS
Church numerals
Natural numbers can be represented inside the -calculus using the idea of
function iteration.
f
fn = f . . . f
n
REPRESENTABILITY
Representing functions
One can use the above representation to represent numerical functions (even
partial ones).
ENCODINGS
someSOME
encodings
successor
n. f x. f (nf x)
addition
n1 n2 . f x. (n1 f )(n2 f x)
multiplication
n1 n2 . f x. n1 (n2 f )x
exponentiation
n1 n2 . f x. n1 n2 f x
CONDITIONAL
nal
if 0 x y y
if n + 1 x y x
nxy.n(v.x)y
KLEENES PREDECESSOR
Kleenes predecessor
n. 2 (n Mg 0, 0)
CHURCHS THESIS
lambda-definable functions
The -definable functions turned out to coincide with known classes of (recursive) functions.
A. Church. An unsolvable problem of elementary number theory. American Journal of Mathematics 58:345363 (1936)
S. C. Kleene. -definability and recursiveness. Duke Mathematical
Journal 2:340353 (1936)
This made Church conjecture that -definability captures the (informal) concept of eective calculability. This is known as Churchs Thesis.
ENTSCHEIDUNGPROBLEM
Church would use a dierent language as at the time people were only
beginning to realize that various notions of eective calculability coincide.
Turing arrived on the scene at the same time with his Turing machines,
which he showed equivalent to the lambda calculus.
A. Church. A note on the Entscheidungsproblem. Journal of Symbolic
Logic 1: 40-41 (1936)
A. M. Turing. On computable numbers, with an application to the
Entscheidungsproblem. Proceedings of the London Mathematical Society 42: 230-265 (1936)
A. M. Turing. Computability and -definability. Journal of Symbolic
Logic 2:153-163 (1937)
=
Both also presented solutions to a long-standing open problem about
validity of first-order logic.
TURING
1912-1954
FIXED POINTS
fixed points
1
fact(n) =
n fact(n 1)
n=0
n>0
1
n=0
G(f )(n) =
n f (n 1)
n>0
Observe that G(fact) = fact, i.e. fact is a fixed point.
Recursive definitions can be viewed as fixed points of operators on functions!
TURINGS COMBINATOR
Does any function have a fixed point in the -calculus? Given f , can we
always find M such that
M f (M )?
= xy.y(xxy)
Y =
Note the following
Y f = ()f (y.y(y))f f (f ) = f (Y f )
Y f is the fixed point of f : f (Y f ) = Y f . Y is a fixed-point combinator.
LAMBDA FACTORIAL
defining the factorial function
Recall that fact is a fixed point of G : (N N) (N N).
1
n=0
G(f )(n) =
n f (n 1)
n>0
Consider the -term corresponding to G:
f. n. if n (n f (n 1)) 1.
Apply the fixed point combinator
Y (f. n. if n (n f (n 1)) 1)
to obtain a term that represents the factorial function on Church numerals.
CURRYS COMBINATOR
currys combinator
Observe that
Y f = f (Y f ).
This time we do not have Y f f (Y f )!
MGS COMBINATOR
MGS combinator
T = abc xyz.z(midlands graduate school rulez)
Y =T
T
26
Observe that
Y f = f (Y f ).
SCOTT NUMERALS
t numerals
rations
0 = xy.x
1 = xy.y(xy.x)
2 = xy.y(xy.y(xy.x))
3 = xy.y(xy.y(xy.y(xy.x)))
SOME ENCODINGS
Operations
successor
n. xy. yn
predecessor
n. n (x.x)
conditional
n. xy. n x y
UNDECIDABLE PROBLEMS
Undecidable problems
Is a given term weakly normalizing?
Is a given term strongly normalizing?
Does M N hold?
Does M = N hold?
EXTENSIONALITY
Extensionality
Extensionality is a fundamental property of functions.
Mx = Nx
M =N
x fv (M ) fv (N )
x fv (M ) fv (N )
x fv (M )
ETA-REDUCTION
-reduction
FURTHER LAWS?
How about adding the following law?
xy.x =law xy.y
Equating any dierent -normal forms M, N leads to inconsistency!
B
ohms Theorem: There exists a context C such that
C[M ] = x
C[N ] = y.
TYPES
Types
Typesand
and preterms
preterms
In In
thetheuntyped
unconstrained.
Terms
untyped-calculus
-calculus application
application isisunconstrained.
Terms
can can
eveneven
be be
appliedtotothemselves!
themselves! This
This flexibility
bebe
a source
of computaapplied
flexibilityturns
turnsout
outtoto
a source
of computational
expressiveness. In
In what
what follows
constraints
on the
tional
expressiveness.
followswe
weshall
shallimpose
impose
constraints
on use
the use
application,using
using types.
types.
of of
application,
Let G be the set of ground types. For the time being, let G = {o}.
Let G be the set of ground types. For the time being, let G = {o}.
Any A G is a type.
Any A G is a type.
PRE-TERMS
Towards typed terms
In order define typed terms, we slightly modify the previous definition of
-terms and add type annotations to -abstractions.
Variables
Any x V is a pre-term.
Function Application If M, N are pre-terms, then (M N ) is a pre-term.
Function Abstraction If x V, M is a pre-term, A is a type
then (xA .M ) is a pre-term.
As before we shall identify -equivalent pre-terms, i.e. we consider -equivalence
classes (even though we shall not write [ ] explicitly).
Pre-terms are candidates for typed terms. Next we define when a pre-term
can become a typed term. This will be the case if it can pass a typing test.
TYPING
Typing judgments
TYPING RULES
Typing rules
x1 : A1 , , xn : An xi : Ai
1in
M :AB N :A
MN : B
, x : A M : B
xA .M : A B
PROPERTIES
Some properties
WEAK NORMALIZABILITY
Weak normalizability
In the untyped -calculus it is undecidable whether a term is weakly normalizing, i.e. whether it reduces to a normal term. This changes dramatically
with types: all terms have a normal form.
Some useful definitions.
The degree of a type is defined by
d(o) = 0
d(A B) = 1 + max(d(A), d(B))
The degree of a redex (xA .M )N is the degree of the type of xA .M .
The degree of a term is equal to the highest degree of a redex occurring
in it.
A NORMALIZATION ROUTINE
Normalizability procedure
Given a term M proceed as follows.
Let d be the degree of M .
Consider all redexes of degree d in M and pick from among these a
redex
(xA .M )N
such that all redexes in M and N have degree less than d, i.e. innermost redex of the highest degree.
Fire it!
WHATS HAPPENED
what happened?
(xA .M )N
Thanks to our choice (innermost), there is now one fewer redex of
degree d.
There may be new redexes, but their degree will be that of the type of
M or N , both of which are strictly smaller than d.
The procedure can be iterated until there are no redexes of degree d
left.
The whole routine can be repeated for the resultant term. Note that
the degree of the term will now be strictly smaller than d. This implies
termination.
ONE-STEP BOUNDS
some bounds
The length of a term is defined as follows.
l(x) = 1
l(M N ) = 1 + l(M ) + l(N )
l(x.M ) = 1 + l(M )
Let M be a term whose degree is d. There exists a term M of degree strictly
smaller than d such that:
M M in at most l(M ) steps,
l(M ) 2l(M ) .
UPPER BOUNDS
Let 2m
n represent the following tower of exponentials
22
with n occurrences of 2.
.
l(M )
STRONG NORMALIZABILITY
Strong normalizability
Weak normalizability was initially regarded as more interesting, as it was all
one needed to demonstrate consistency. In the context of computers, strong
normalizability becomes a safety guarantee.
In the untyped -calculus it is undecidable whether a term is strongly
normalizing. This also changes with types: all typed terms are strongly
normalizing. It is not so easy to come up with a proof of this fact.
Exercise: Try a naive inductive proof to see what happens!
REDUCIBILITY (TAIT)
Reducibility
We define reducibility sets RedA as follows.
Redo
= { (, M ) | M : o, M is strongly normalizing}
RedAB = { (, M ) | M : A B,
( , M N ) RedB for all ( , N ) RedA
Then it is not hard to show that
If (, M ) RedA then M is strongly normalizing.
For all , if (x : A) then (, x) RedA .
STRONG NORMALIZABILITY
By induction on the structure of M one can show the following.
Given x1 : A1 , , xn : An M : A, if (, Ni ) RedAi for any
i = 1, , n then (, M [N1 /x1 , , Nn /xn ]) RedA .
By setting Ni = xi we finally arrive at:
if M : A then (, M ) RedA .
Since being in RedA implies strong normalizability (previous slide), we can
conclude that any term is strongly normalizing.
REPRESENTABILITY
Representability
Church numerals can be typed. Let us write n for f oo .xo .f n (x) and
observe that
n : (o o) (o o).
Let us write N for (o o) (o o). As in the untyped case, we can define
a notion of representability for (total) numeric functions:
M :N
N N
k
represents f : Nk N i
M n1 nk = f (n1 , , nk )
for all (n1 , , nk ) Nk . In the untyped case we could represent all recursive functions in the same way. Now only the extended polynomials can be
represented (Schwichtenberg).
EXTENDED POLYNOMIALS
Extended polynomials E
0, 1 E
n VN
nE
P1 , P2 E
(P1 + P2 ) E
P1 , P2 E
(P1 P2 ) E
ifzero(m, m0 , m1 ) =
Examples
1. (n1 + (n1 n2 ))
2. (n1 ifzero(n1 + n2 , 0, n3 (1 + 1)))
P1 , P2 , P3 E
ifzero(P1 , P2 , P3 ) E
m0
m1
m=0
m>0
ALTERNATIVES
Other notions of representability
The previous notion uses the type N for representing both the arguments and
the result. A more general notion is possible. Let
NA = (A A) (A A)
so that No = N. We have f AA .xA .f n (x) : NA .
One can use dierent As for arguments and the result. This turns out to
extend the class of functions that can be represented, e.g. predecessor and
exponentiation become representable. Equality and substraction are not,
though.
S. Fortune, D. Leivant, M. ODonnell. The Expressiveness of Simple and
Second-Order Type Structures. Journal of the ACM 30(1): 151-185 (1983)
LOWER
BOUNDS
Lower bounds
nA = f AA xA .f n x : NA
Note that
mAA nA = (nm )A
Models
MODELS
stands for the Cartesian product of sets. Note that a closed term
M : A will be interpreted by a function in 1 A, i.e. an element
of A.
SET-THEORETIC MODEL
where stands for the set-theoretic function space, i.e. the set of all functions from A to B.
SOUNDNESS
Soundness
If M, N : B and M = N , then M : B = N : B.
How about the converse? Consider the following cases.
1. o = {}
2. o = {0, , n}
3. o = N
Dierent Church numerals are not -equivalent, so for the converse to hold
N must be infinite.
COMPLETENESS
Completeness
We write M X for M obtained by setting o = X.
Friedman: If M N = N N then M = N .
DEFINABILITY
Definability
If o is finite, so is A for any type.
The only normal form (with respect to ) at this type is xo .x, which
is interpreted by the identity function.
The other three elements are not -definable, i.e. they are not interpretations of any terms.
In general it is undecidable whether a function is -definable, a surprising
result due to Loader.
CARTESIAN-CLOSED CATS
The set-theoretic interpretation and the associated soundness result are instances of a more general category-theoretic interpretation in Cartesian Closed
Categories, i.e. categories with products and function spaces. The typed calculus can itself be organized into such a category.
Objects are types.
Morphisms are terms.
A morphism between A and B is a = -equivalence class
of x : A M : B. The identity morphism is defined by
x : A x : A.
Composition is substitution.
Any (standard) interpretation of the typed lambda calculus in a cartesian
closed category can be factorized through the syntactic interpretation above.
CURRY-HOWARD
Curry-Howard Correspondence
Let us consider a few selected terms and their types.
Term
Type
xA .x
AA
xA .y B .x
A (B A)
LOGIC OF TYPING
Curry-Howard Correspondence
Let us revisit the typing rules, but with terms erased.
A1 , , An Ai
1in
AB A
B
, A B
AB
The rules turn out to correspond to provability in (implication-only) intuitionistic logic, which is a proper subset of classical logic.
CORRESPONDENCE
The Correspondence
LOGIC
intuitionistic logic
PROGRAMMING
-calculus
formulas
proofs
simplification
provability
types
terms
reduction
inhabitation
PEIRCES LAW
Peirces law
A = (( ) ) is provable in classical logic. Let us see through
the lens of the -calculus whether it is provable in intuitionistic logic. For a
change we need to set G = {, }.
Assume that A is provable in intuitionistic logic. Then there exists a
term M such that M : A. Hence, there exists a -normal term M
such that M : A.
M must have the shape x() .N , i.e. x : ( ) N : .
N must then be of the form xQ, where x Q : . Thus, Q must
have the shape y .R and
x : ( ) , y : R : .
Is this possible with R in normal form?
PCF
PCF
Ground types
G = {nat}
Constants
nN
n : nat
BETA RECAP
beta-recap
Basic rule
(x.M )N M [N/x]
Contextual rules
M M
x.M x.M
M M
M N M N
M M
N M N M
A basic step can be made anywhere inside the term (this will be restricted
in what follows in favour of the leftmost-outermost strategy).
CALL-BY-NAME EVALUATION
CBN Evaluation
Basic rules
pred 0 0
pred (n + 1) n
succ n n + 1
ifzeroA 0 M0 M1 M0
ifzeroA (n + 1) M0 M1 M1
(x.M )N M [N/x]
YA M M (YA M )
Contextual rules
M M
pred M pred M
M M
succ M succ M
M M
M N M N
M M
ifzeroA M ifzeroA M
CALL-BY-VALUE
EVALUATION
CBV Evaluation
Let V stand for n, x.M or YAB .
Basic rules
pred 0 0
pred (n + 1) n
succ n n + 1
(x.M )V
YAB V
ifzeroA 0 M0 M1 M0
ifzeroA (n + 1) M0 M1 M1
M [V /x]
xA .V (YAB V )x
Contextual rules
M M
pred M pred M
M M
M N M N
M M
succ M succ M
M M
V M V M
M M
ifzeroA M ifzeroA M
YAB
M M
M YAB M