Lambda Calculus and Combinatory Logic
Lambda Calculus and Combinatory Logic
Andrés Sicard-Ramı́rez
Universidad EAFIT
Semester 2009-2
Introduction
References
Textbook: Hindley, J. R. and Seldin, J. [2008]. Lambda-Calculus and
Combinators. An Introduction. Cambridge University Press.
Barendregt, Henk and Barendsen, Erik [2000]. Introduction to
Lambda Calculus. Revisited edition, Mar. 2000.
Barendregt, H. P. [2004]. The Lambda Calculus. Its Syntax and
Semantics. Revised edition, 6th impression. Vol. 103. Studies in
Logic and the Foundations of Mathematics. Elsevier.
Introduction 3/106
Lambda Calculus and Combinatory Logic
“Two systems of logic which can also serve as abstract programming
languages.” [Hindley and Seldin 2008, p. ix]
The goal was to use them in the foundation of mathematics.
Introduction 4/106
Lambda Calculus and Combinatory Logic
λ-calculus
Invented by Alonzo Church (around
1930s). Intended for studying func-
tions and recursion.
Introduction 5/106
Lambda Calculus and Combinatory Logic
Combinatory logic
Introduction 6/106
Lambda Calculus
Introduction
λ-calculus is a collection of several formal systems
λ-notation
Anonymous functions
Currying
Definition (λ-terms)
v ∈ V ⇒ v ∈ λ-terms (atom)
c ∈ C ⇒ c ∈ λ-terms (atom)
M, N ∈ λ-terms ⇒ (M N ) ∈ λ-terms (application)
M ∈ λ-terms, x ∈ V ⇒ (λx.M ) ∈ λ-terms (abstraction)
Example
(λxyz.xz(yz))uvw ≡ ((((λx.(λy.(λz.((xz)(yz)))))u)v)w).
Definition (scope)
In λx.M , M is called the scope of λx.
[N/x]x ≡ N
[N/x]a ≡ a for all atoms a 6≡ x
[N/x](P Q) ≡ ([N/x]P [N/x]Q)
[N/x](λx.P ) ≡ (λx.P )
[N/x](λy.P ) ≡ (λy.P ) y 6≡ x, x 6∈ FV(P )
[N/x](λy.P ) ≡ λy.[N/x]P y 6≡ x, x ∈ FV(P ), y 6∈ FV(N )
[N/x](λy.P ) ≡ λz.[N/x][z/y]P y 6≡ x, x ∈ FV(P ), y ∈ FV(N )
Example
Whiteboard.
Example
(λx.(λy.yx)z)v .β zv.
Example
Whiteboard.
} !
M N
! }
∃T
Corollary
If P has a β-normal form, it is unique modulo ≡α ; that is, if P has β-normal
forms M and N , then M ≡α N .
Proof.
Whiteboard.
Corollary
The relation =β is non-trivial (not all terms are β-convertible to each other).
Proof.
Whiteboard.
Combinators
Operators which manipulate expressions by cancellation, duplication, brack-
eting and permutation.
∀xy.x + y = y + x,
can be written as
A = CA,
where Axy represents x + y and C is a combinator with the property
Cf xy = f yx.
v ∈ V ⇒ v ∈ CL-terms
c ∈ C ⇒ c ∈ CL-terms
X, Y ∈ CL-terms ⇒ (XY ) ∈ CL-terms
where
V : Set of variables
C = {I, K, S, . . . } : Set of atomic constants
[U/x]x ≡ U
[U/x]a ≡ a for all atoms a 6≡ x
[U/x](V W ) ≡ ([U/x]V [U/x]W )
IX by X,
KXY by X,
SXY Z by XZ(Y Z).
Example
Let W ≡ SS(KI). Then
i) WXY .w XY Y and
ii) WWW .w WWW .w · · ·
X .w Y ⇒ [U/x]X .w [U/x]Y.
([x].M )N .w [N/x]M.
Definition (abstraction)
For every term M and every variable x
Example
[x].xy ≡ SI(Ky) (whiteboard).
Combinatory Logic 30/106
Abstraction
Theorem
For every term M and every variable x, [x].M is always defined, does not
contain x and ([x].M )x .w M .
Proof.
Whiteboard.
Theorem
For every term M and every variable x,
([x].M )N .w [N/x]M.
Notation
[x1 , x2 , . . . , xn ].M ≡ [x1 ].([x2 ].(. . . ([xn ].M ) . . . )).
Example
[x, y].xyy ≡ SS(KI) ≡ W (whiteboard).
Combinatory Logic 31/106
Weak Equality
Definition (weak equality or weak convertibility (X =w Y ))
Exist X0 , . . . , Xn such that
i) X0 ≡ X
ii) Xn ≡ Y
iii) (∀i ≤ n − 1)(Xi .1w Xi+1 ∨ Xi+1 .1w Xi )
Corollary
If X and Y are distinct weak normal forms, them X 6=w Y ; in particular
S 6=w K. Hence =w is non-trivial in the sense that not all terms are weakly
equal.
X =β Y ⇒ λx.X =β λx.Y,
but
X =w Y 6⇒ [x].X =w [x].Y.
Example
Let X ≡ Sxyz and Y ≡ xz(yz). Then X =w Y , but [x].X 6=w [x].Y ,
where
[x].X ≡ S(SS(Ky))(Kz),
[x].Y ≡ S(SI(Kz))(K(yz)).
XP =β,w P.
Proof.
YTuring ≡ U U , where U ≡ λux.x(uux) (whiteboard).
xy1 . . . yn = Z
Proof.
X ≡ Y(λxy1 . . . yn .Z) (whiteboard).
Example
The λ-term λux.ux is in β-nf but not in βη-nf.
M L1 . . . Ln xy .β,w x,
N L1 . . . Ln xy .β,w y.
Corollary
Let M and N be distinct combinators in βη-normal form (in λ) or in strong
normal form (in CL). If we add the equation M = N as a new axiom to
the definition =β or =w , then all terms become equal.
Proof.
Whiteboard.
The Power of λ and Combinators 41/106
Leftmost-Reduction
Idea
Proving that a given term has no normal form.
Definition (contraction (X .R Y ) )
(X .R Y ): R is an redex in X and Y is the result of contracting R in X.
Example
(λx.(λy.yx)z)v .(λy.yx)z (λx.zx)v.
Definition (reduction)
A reduction ρ is
CL : X1 .R1 X2 .R2 . . .
λ: X1 .R1 Y1 ≡α X2 .R2 . . .
S(I(Kxy))(Iz) .1w
S(Kxy)(Iz) .1w
Sx(Iz) .1w
Sxx
X 0 Y ≡ Y,
X n+1 Y ≡ X(X n Y ).
For λ: n ≡ λxy.xn y,
For CL: n ≡ (SB)n (KI) (B ≡ S(KS)K).
In λ: σ ≡ λuxy.x(uxy) (whiteboard)
In CL: σ ≡ SB
D ≡ λxyz.z(Ky)x
For all X, Y
fac ≡ Yh
fac 1 ≡ Yh 1
=β,w h(Yh) 1
≡ (λf n.(. . . f . . . ))(Yh) 1
.β,w if 1 = 0 then 1 else 1 ∗ (Yh 0)
.β,w 1 ∗ (Yh 0)
=β,w 1 ∗ (h(Yh) 0)
≡ 1 ∗ ((λf n.(. . . f . . . ))(Yh)0)
.β,w 1 ∗ (if 0 = 0 then 1 else 1 ∗ (Yh (−1)))
.β,w 1 ∗ 1
.β,w 1
Rules of inference:
(µ) M = M0
NM = NM0 M = M 0 (τ ) M = N N =P
(ξ) M = P
M = M0 λx.M = λx.M 0
(ν)
M N = M 0N (σ) M = N
N =M
Remark
λβ is a equational theory and it is a logic-free theory (there are not logical
connectives or quantifiers in its formulae).
M .β M 0 M .β M 0
(µ) (ξ)
N M .β N M 0 λx.M .β λx.M 0
M .β M 0 M .β N N .β P
(ν) (τ )
M N .β M 0 N M .β P
Theorem
M .β N ⇐⇒ λβ ` M .β N,
M =β N ⇐⇒ λβ ` M = N.
(I) IX = X,
(K) KXY = X,
(S) SXY Z = XZ(Y Z),
(ρ) X = X.
Rules of inference: The same rules than the theory λβ except the rule (ξ).
Deductions: CLw, A1 , . . . , An ` B.
Theorems: CLw ` B.
Theorem
M .w N ⇐⇒ CLw ` M .w N,
M =w N ⇐⇒ CLw ` M = N.
A1 , . . . , A n
B
T ` C.
The Formal Theories λβ and CLw 61/106
Equivalence of Theories
Theorem
If R is derivable in T , then R is admissible in T . The implication in the
opposite direction does not hold in general.
X =T Y ⇐⇒ T ` X = Y.
Theorem
The theory λβ is not extensional.
Proof.
Let F ≡ y and G ≡ λx.yx. Then for all X
λβ ` F X = GX,
but
λβ 6` F = G.
(ζ) Mx = Nx if x 6∈ FV(M N ),
M =N
(η) λx.M x = M if x 6∈ FV(M ).
Proof.
1. (ζ) is derivable in λβη, i.e.
λβη, M x = N x ` M = N (with x 6∈ FV(M N )). (Whiteboard).
2. (η) is derivable in λβζ, i.e.
λβζ ` λx.M x = M (with x 6∈ FV(M )). (Whiteboard).
M =λext N ⇐⇒ λβζ ` M = N.
Corollary
The relation =λext is non-trivial (not all terms are βη-convertible to each
other).
Proof.
Let X ≡ S(Ku)I and Y ≡ u, then for all M
CLw ` XM = Y M,
but
CLw 6` X = Y.
(ζ) Xx = Y x if x 6∈ FV(XY ),
X=Y
(ξ) X=Y
[x].X = [x].Y
(η) [x].U x = U if x 6∈ FV(U ).
CLζ : CL + (ζ),
CLξ : CL + (ξ).
Exercise
Probe that neither (ζ) nor (ξ) is admissible in CLw. (Whiteboard).
Extensionality in Combinatory Logic 74/106
Extensional Equality
Definition (extensional equality in CL)
X =Cext Y ⇐⇒ CLζ ` X = Y.
Example
SK =Cext KI. (Whiteboard).
Proof.
1. (ζ) is derivable in CLξ, i.e.
CLξ, Xx = Y x ` X = Y (with x 6∈ FV(XY )). (Whiteboard).
2. (ξ) is derivable in CLζ, i.e.
CLζ, X = Y ` [x].X = [x].Y . (Whiteboard).
Motivation
We are looking axioms which will make (ξ) admissible in CLextax :
Theorem
The theory CLextax is theorem-equivalent to CLξ.
Extensionality in Combinatory Logic 78/106
Strong Reduction
Definition (strong reduction )
The formal theory of strong reduction:
Formulas: X Y , where X, Y ∈ CL-terms
Axiom-schemes and rules: The same than CLw changed ‘=’ by ‘ ’ and
the rule (σ) omitted.
New rule added:
(ξ) X Y
[x].X [x].Y
Example
SK KI. (Whiteboard).
X Y =⇒ Y ≡ X.
Theorem
The strongly irreducible CL-terms are exactly the terms in the strong nf
class.
Models of CL 82/106
Combinatory Algebras
Definition (combinatory algebra)
A combinatory algebra is a structure (convention: association to the left
for ·) D = hD, ·i where
1. |D| ≥ 2.
2. · : D2 → D.
3. There are two elements k, s ∈ D such that for all a, b, c ∈ D,
k · a · b = a, (5)
s · a · b · c = a · c · (b · c). (6)
Models of CL 83/106
Combinatory Algebras
Definition (model of CLw)
A model of CLw is a structure hD, ·, i, k, si where
1. hD, ·i is a combinatory algebra.
2. The elements k and s satisfy (5) and (6).
3. The element i satisfies i = s · k · k.
Models of CL 84/106
Combinatory Algebras
Definition (interpretation of a term)
Let D = hD, ·, i, k, si where hD, ·i is a combinatory algebra and ρ a valua-
tion. The interpretation of X in D under ρ, denoted JXKD ρ , is defined by
JXKD
ρ : CL-term → D
JxKD
ρ = ρ(x),
JIKD
ρ = i,
JKKD
ρ = k,
JSKD
ρ = s,
JXY KD D D
ρ = JXKρ · JY Kρ .
Models of CL 85/106
Combinatory Algebras
Definition (satisfaction)
D, ρ |= X = Y ⇐⇒ JXKD D
ρ = JY Kρ ,
D |= X = Y ⇐⇒ (∀ρ)(D, ρ |= X = Y ).
Models of CL 86/106
Combinatory Algebras
Example (term model)
Let T ∈ {CLw, CLextax }. For each CL-term X,
[X] = {Y : T ` X = Y }.
D = {[X] : X is a CL-term},
[X] · [Y ] = [XY ],
i = [I],
k = [K],
s = [S].
Models of CL 87/106
Combinatory Algebras
Example (cont.)
In this model, interpretation is the same as substitution
where
FV(X) = {x1 , . . . , xn },
∀xi ∈ FV(X).ρ(xi ) = Yi .
Models of CL 88/106
Combinatory Algebras
Theorem (submodel theorem)
Let T ∈ {CLw, CLextax }. If hD, ·, i, k, si is a model of T and D0 is a subset
of D which contains i, k and s and is closed under ·, then hD0 , ·, i, k, si is a
model of T .
Definition (interiors)
Let T ∈ {CLw, CLextax } and D = hD, ·, i, k, si a model of T . The interior
of D is
D◦ = {JXK : X closed}.
Theorem (interiors)
Let T ∈ {CLw, CLextax }. The interior of a model of T is also a model
of T .
Models of CL 89/106
Models of Lambda Calculus
The Definition of Lambda Model
Definition (λ-model)
A λ-model is a triple D = hD, ·, J Ki where
1. hD, ·i is an applicative structure.
2. J K : λ-terms → D is a mapping such that for each valuation ρ
JxKρ = ρ(x),
JP QKρ = JP KD
ρ · JQKρ .
Jλx.P Kρ · d = JP K[d/x]ρ , for all d ∈ D,
JM Kρ = JM Kσ if ∀x ∈ FVM.ρ(x) = σ(x),
Jλx.P Kρ = Jλy.[y/x]P Kρ if y 6∈ FV(M ),
[M ] = {N : T ` M = N }.
D = {[M ] : M is a λ-term},
[P ] · [Q] = [P Q],
JM Kρ = [[N1 /x1 , . . . , Nn /xn ]M ],
where
FV(M ) = {x1 , . . . , xn },
∀xi ∈ FV(M ).ρ(xi ) = Ni .
Scott’s solution
D∞ : complete partial order
[D∞ → D∞ ]: continuous
functions (under the Scott’s
topology)
D∞ ∼= [D∞ → D∞ ].
∀a ∈ X.a v b.
∀c ∈ D.c is a u.b. of X =⇒ b v c.
∀x ∈ D.⊥ v x.
N+ = N ∪ {⊥} (⊥ 6∈ N),
+
∀a, b ∈ N .a v b ⇐⇒ (a = ⊥ ∧ b ∈ N) ∨ a = b.
0 1 2 3 ...
Motivation
The element ⊥ represents an undefined value (partial functions).
a v b represents that b “is more defined” than a or both are equals
(semantic approximation order).
Models of Lambda Calculus 98/106
Scott’s D∞ : Complete Partial Orders
Let hD, vi and hD0 , v0 i be c.p.o.s and ϕ a function ϕ : D → D0 .
Definition (monotonicity)
The function ϕ is monotonic iff
a v b =⇒ ϕ(a) v0 ϕ(b).
Example
Let ϕ : N+ → N+ be a monotonic function. If ϕ(⊥) = 1 then ϕ is a
constant function, i.e. ∀n ∈ N+ .ϕ(n) = 1.
where
ϕ v ψ ⇐⇒ ∀d ∈ D.ϕ(d) v0 ψ(d).
Theorem
The function ∀d ∈ D.⊥(d) = ⊥0 is the bottom of [D → D0 ].
Theorem
[D → D0 ] is a c.p.o.
D0 = N+ ,
Dn+1 = [Dn → Dn ].
Theorem
Every Dn is a c.p.o.
1 if n is 0
1 if n is 0
1 if n is 1
f2 (n) = 1 if n is 1 , f3 (n) = , ...
2 if n is 2
⊥ else
⊥ else
Then, ⊥= f0 v f1 v f2 v . . . .
The idea is
G
∀n. (f0 (n) v f1 (n) v f2 (n) v . . . ) = f (n).
Application
ϕ · ψ = hϕ1 (ψ0 ), ϕ2 (ψ1 ), . . .i
Self-application