Cite

This course has no released version yet: the text at this address may change. Cite a released version when one exists.

/courses/models-of-computation/lambda-calculus
@misc{models-of-computation-lambda-calculus,
  title = {{The λ-calculus}},
  howpublished = {Lesson 10 of \emph{Models of computation}, Computability Theory course notes},
  url = {/courses/models-of-computation/lambda-calculus},
  note = {Unreleased working version}
}

To cite a single statement, use the # link that appears next to its heading.

Lesson 10 of 13

The λ-calculus

λ-terms, free variables and substitution, α-equivalence, β-reduction, normal forms, the Church–Rosser theorem, and reduction strategies.

40 min5 questions
Models of computation · lesson 10 of 13
  1. Algorithms and partial functions
  2. Turing machines
  3. Programming Turing machines
  4. Register machines
  5. Primitive recursive functions
  6. Coding finite sequences
  7. The Ackermann function
  8. Minimization and partial recursive functions
  9. LOOP and WHILE programs
  10. The λ-calculus
  11. Computing with λ-terms
  12. Equivalence of the models
  13. The Church–Turing thesis

Church’s λ-calculus (1932–1936) takes a different view of computation. There are no machines and no numbers: the only objects are functions, and the only operations are forming a function by abstracting over a variable, and applying a function to an argument. Computation is the simplification of expressions by substituting arguments for variables. The λ-calculus is the theoretical basis of functional programming languages and of the proofs-as-programs correspondence in logic.

In the λ-calculus, functions take functions as arguments and return functions. Numbers, booleans and data structures must therefore be encoded as functions; this is the subject of the next lesson. Here we study the calculus itself.

10.1 Terms

Fix an infinite set of variables x,y,z,…x, y, z, \dots

Definition 10.1.#

The set Λ\Lambda of λ-terms is defined inductively:

  • every variable xx is a term;
  • if MM and NN are terms, then (M N)(M\, N) is a term, the application of MM to NN;
  • if MM is a term and xx a variable, then (λx. M)(\lambda x.\, M) is a term, the abstraction of MM over xx.

The intended meaning of (λx. M)(\lambda x.\, M) is the function x↦Mx \mapsto M. We use the following conventions to omit parentheses: outermost parentheses are dropped; application associates to the left, so M N PM\, N\, P stands for ((M N) P)((M\, N)\, P); the body of an abstraction extends as far right as possible, so λx. M N\lambda x.\, M\, N stands for λx. (M N)\lambda x.\, (M\, N); and λxy. M\lambda x y.\, M abbreviates λx. λy. M\lambda x.\, \lambda y.\, M.

A function of several arguments is represented as a function returning a function (currying): λxy. M\lambda x y.\, M takes xx and returns the function λy. M\lambda y.\, M.

Some terms that will recur:

I=λx. x,K=λxy. x,S=λxyz. x z (y z),ω=λx. x x,Ω=ω ω.\begin{gathered} \mathsf{I} = \lambda x.\, x, \\ \mathsf{K} = \lambda x y.\, x, \\ \mathsf{S} = \lambda x y z.\, x\, z\, (y\, z), \\ \omega = \lambda x.\, x\, x, \\ \Omega = \omega\, \omega. \end{gathered}

10.2 Free variables and substitution

Definition 10.2.#

The set FV(M)\mathrm{FV}(M) of free variables of MM is defined by

FV(x)={x},FV(M N)=FV(M)∪FV(N),FV(λx. M)=FV(M)∖{x}.\begin{gathered} \mathrm{FV}(x) = \{x\}, \\ \mathrm{FV}(M\, N) = \mathrm{FV}(M) \cup \mathrm{FV}(N), \\ \mathrm{FV}(\lambda x.\, M) = \mathrm{FV}(M) \setminus \{x\}. \end{gathered}

Occurrences of xx in MM within λx. M\lambda x.\, M are bound. A term without free variables is closed, or a combinator.

For instance FV(λx. x y)={y}\mathrm{FV}(\lambda x.\, x\, y) = \{y\}, and I,K,S,Ω\mathsf{I}, \mathsf{K}, \mathsf{S}, \Omega are closed.

Substituting a term NN for a variable xx must not change the meaning of the free variables of NN. In λy. x\lambda y.\, x, substituting yy for xx naively would give λy. y\lambda y.\, y, the identity, whereas λy. x\lambda y.\, x is a constant function. The bound variable must be renamed first.

Definition 10.3.#

The substitution M[N/x]M[N/x] of NN for the free occurrences of xx in MM is defined by induction on the size of MM (renaming a bound variable does not change the size):

  • x[N/x]=Nx[N/x] = N, and y[N/x]=yy[N/x] = y for y≠xy \neq x;
  • (M1 M2)[N/x]=M1[N/x] M2[N/x](M_1\, M_2)[N/x] = M_1[N/x]\ M_2[N/x];
  • (λx. M)[N/x]=λx. M(\lambda x.\, M)[N/x] = \lambda x.\, M;
  • (λy. M)[N/x]=λy. M[N/x](\lambda y.\, M)[N/x] = \lambda y.\, M[N/x] if y≠xy \neq x and (y∉FV(N)y \notin \mathrm{FV}(N) or x∉FV(M)x \notin \mathrm{FV}(M));
  • (λy. M)[N/x]=λz. M[z/y][N/x](\lambda y.\, M)[N/x] = \lambda z.\, M[z/y][N/x] if y≠xy \neq x, y∈FV(N)y \in \mathrm{FV}(N) and x∈FV(M)x \in \mathrm{FV}(M), where zz is a variable not occurring in MM or NN.

The last clause renames the bound variable yy to avoid capturing the free yy of NN.

Definition 10.4.#

Two terms are α-equivalent if one is obtained from the other by renaming bound variables, that is, by replacing subterms λy. M\lambda y.\, M by λz. M[z/y]\lambda z.\, M[z/y] with z∉FV(M)z \notin \mathrm{FV}(M).

From now on, α-equivalent terms are identified: λx. x\lambda x.\, x and λy. y\lambda y.\, y are the same term. We also adopt the variable convention: in any discussion, bound variables are chosen different from the free variables. With this convention the last clause of Definition 10.3 is never needed.

10.3 β-reduction

Definition 10.5.#

A β-redex is a term of the form (λx. M) N(\lambda x.\, M)\, N; its contractum is M[N/x]M[N/x]. The one-step β-reduction M→βM′M \to_\beta M' holds if M′M' is obtained from MM by replacing one occurrence of a redex by its contractum. Formally, →β\to_\beta is the smallest relation such that

  • (λx. M) N→βM[N/x](\lambda x.\, M)\, N \to_\beta M[N/x];
  • if M→βM′M \to_\beta M' then M N→βM′ NM\, N \to_\beta M'\, N, N M→βN M′N\, M \to_\beta N\, M' and λx. M→βλx. M′\lambda x.\, M \to_\beta \lambda x.\, M'.

We write ↠β\twoheadrightarrow_\beta for its reflexive transitive closure (reduction) and =β=_\beta for the equivalence relation it generates (β-conversion).

Definition 10.6.#

A term is in β-normal form if it contains no β-redex. A term MM has a normal form if M↠βNM \twoheadrightarrow_\beta N for some NN in normal form.

Examples:

  1. I M→βM\mathsf{I}\, M \to_\beta M, and K M N→β(λy. M) N→βM\mathsf{K}\, M\, N \to_\beta (\lambda y.\, M)\, N \to_\beta M (with y∉FV(M)y \notin \mathrm{FV}(M)).
  2. S K K↠βλz. K z (K z)↠βλz. z=I\mathsf{S}\, \mathsf{K}\, \mathsf{K} \twoheadrightarrow_\beta \lambda z.\, \mathsf{K}\, z\, (\mathsf{K}\, z) \twoheadrightarrow_\beta \lambda z.\, z = \mathsf{I} (see the first exercise for the individual steps).
  3. Ω=(λx. x x)(λx. x x)→β(λx. x x)(λx. x x)=Ω\Omega = (\lambda x.\, x\, x)(\lambda x.\, x\, x) \to_\beta (\lambda x.\, x\, x)(\lambda x.\, x\, x) = \Omega. The only reduction sequence from Ω\Omega is infinite, and Ω\Omega has no normal form: it is the λ-calculus analogue of a non-terminating computation.
  4. (λx. y) Ω(\lambda x.\, y)\, \Omega reduces to the normal form yy by contracting the outer redex, but also reduces to itself by contracting Ω\Omega. Some reduction sequences from this term are infinite, although it has a normal form.

10.4 Confluence

A term may contain several redexes, so reduction is not deterministic. The fundamental theorem of the λ-calculus states that the choices do not matter for the final result.

Theorem 10.7 (Church–Rosser, 1936).#

The relation ↠β\twoheadrightarrow_\beta is confluent: if M↠βN1M \twoheadrightarrow_\beta N_1 and M↠βN2M \twoheadrightarrow_\beta N_2, there exists PP with N1↠βPN_1 \twoheadrightarrow_\beta P and N2↠βPN_2 \twoheadrightarrow_\beta P.

Proof of Theorem 10.7.

We sketch the proof of Tait and Martin-Löf. The one-step relation →β\to_\beta itself does not satisfy the “diamond property” (a single step on each side need not be joinable in one step each), because contracting a redex may duplicate other redexes. One introduces parallel reduction ⇒\Rightarrow, the smallest relation such that

  • x⇒xx \Rightarrow x;
  • if M⇒M′M \Rightarrow M' then λx. M⇒λx. M′\lambda x.\, M \Rightarrow \lambda x.\, M';
  • if M⇒M′M \Rightarrow M' and N⇒N′N \Rightarrow N' then M N⇒M′ N′M\, N \Rightarrow M'\, N';
  • if M⇒M′M \Rightarrow M' and N⇒N′N \Rightarrow N' then (λx. M) N⇒M′[N′/x](\lambda x.\, M)\, N \Rightarrow M'[N'/x].

Intuitively, M⇒M′M \Rightarrow M' contracts any set of redexes present in MM, simultaneously. One checks that →β⊆⇒⊆↠β{\to_\beta} \subseteq {\Rightarrow} \subseteq {\twoheadrightarrow_\beta}, so the reflexive transitive closure of ⇒\Rightarrow is ↠β\twoheadrightarrow_\beta. The key lemma, proved by induction on the derivation of M⇒M′M \Rightarrow M', is that M⇒M′M \Rightarrow M' and N⇒N′N \Rightarrow N' imply M[N/x]⇒M′[N′/x]M[N/x] \Rightarrow M'[N'/x]. From it one deduces the diamond property: if M⇒N1M \Rightarrow N_1 and M⇒N2M \Rightarrow N_2, then N1⇒PN_1 \Rightarrow P and N2⇒PN_2 \Rightarrow P for some PP — in fact one can take for PP the term M∗M^* obtained by contracting all redexes of MM. A relation with the diamond property has a confluent transitive closure, by a “tiling” argument: given two sequences of ⇒\Rightarrow-steps from MM, complete the grid of diamonds. The details are in Barendregt’s monograph (chapter 3).

MN1N2Pthere exists

Confluence: two reduction sequences from MM can always be completed, by further reductions, to a common term PP.
Corollary 10.8.#
  1. A term has at most one normal form.
  2. If M=βNM =_\beta N, then MM and NN reduce to a common term.
  3. Distinct normal forms are not β-convertible. In particular =β=_\beta is not trivial: K≠βI\mathsf{K} \neq_\beta \mathsf{I}.
Proof of Corollary 10.8.
  1. If M↠βN1M \twoheadrightarrow_\beta N_1 and M↠βN2M \twoheadrightarrow_\beta N_2 with N1,N2N_1, N_2 normal, confluence gives PP with Ni↠βPN_i \twoheadrightarrow_\beta P; since normal forms do not reduce, N1=P=N2N_1 = P = N_2.
  2. By induction on the length of a chain of reductions and expansions connecting MM and NN, using confluence to merge consecutive peaks.
  3. If two normal forms are convertible, by 2 they reduce to a common term, hence are equal.

10.5 Reduction strategies

Which redex should be contracted? Two natural strategies are:

  • normal order: always contract the leftmost outermost redex (the one whose λ appears first from the left);
  • applicative order: always contract a leftmost innermost redex, i.e. evaluate arguments before passing them.

On (λx. y) Ω(\lambda x.\, y)\, \Omega, normal order terminates immediately with yy, while applicative order loops forever on Ω\Omega.

Theorem 10.9 (Standardization, Curry–Feys).#

If a term has a normal form, the normal order reduction sequence starting from it is finite and ends with this normal form.

We admit this theorem; a proof is in Curry and Feys, or in Barendregt’s monograph (chapter 13). It shows that normal order is a deterministic algorithm computing normal forms whenever they exist: a λ-term is evaluated like a program, which runs forever exactly when the term has no normal form.

Remark 10.10 (η-conversion).

The relation λx. M x→ηM\lambda x.\, M\, x \to_\eta M, for x∉FV(M)x \notin \mathrm{FV}(M), expresses extensionality: both terms behave identically when applied to an argument. The theory of βη\beta\eta-conversion also satisfies the Church–Rosser theorem. We will not need it.

10.6 Exercises

Exercise 10.11 ★.

Reduce S K K\mathsf{S}\, \mathsf{K}\, \mathsf{K} to normal form, writing every step, and check that S K K M↠βM\mathsf{S}\, \mathsf{K}\, \mathsf{K}\, M \twoheadrightarrow_\beta M for every MM.

Solution

S K K=(λxyz. x z (y z)) K K→β(λyz. K z (y z)) K→βλz. K z (K z)→βλz. (λy. z) (K z)→βλz. z\mathsf{S}\, \mathsf{K}\, \mathsf{K} = (\lambda x y z.\, x\, z\, (y\, z))\, \mathsf{K}\, \mathsf{K} \to_\beta (\lambda y z.\, \mathsf{K}\, z\, (y\, z))\, \mathsf{K} \to_\beta \lambda z.\, \mathsf{K}\, z\, (\mathsf{K}\, z) \to_\beta \lambda z.\, (\lambda y.\, z)\, (\mathsf{K}\, z) \to_\beta \lambda z.\, z. Then S K K M↠βI M→βM\mathsf{S}\, \mathsf{K}\, \mathsf{K}\, M \twoheadrightarrow_\beta \mathsf{I}\, M \to_\beta M.

Exercise 10.12 ★.

Show that the term (λx. x x x)(λx. x x x)(\lambda x.\, x\, x\, x)(\lambda x.\, x\, x\, x) has no normal form, and that its reducts grow in size.

Solution

Let W=λx. x x xW = \lambda x.\, x\, x\, x. Then W W→βW W W→βW W W W→⋯W\, W \to_\beta W\, W\, W \to_\beta W\, W\, W\, W \to \cdots: the only redex of W W ⋯ WW\, W\, \cdots\, W (nn copies) is the leftmost W WW\, W, whose contraction gives n+1n + 1 copies. Every reduction sequence is therefore infinite.

Exercise 10.13 ★★.

Prove the substitution lemma: if x≠yx \neq y and x∉FV(L)x \notin \mathrm{FV}(L), then M[N/x][L/y]=M[L/y][N[L/y]/x]M[N/x][L/y] = M[L/y][N[L/y]/x].

Solution

By induction on MM, using the variable convention. If M=xM = x, both sides are N[L/y]N[L/y]. If M=yM = y, the left side is LL and the right side is L[N[L/y]/x]=LL[N[L/y]/x] = L since x∉FV(L)x \notin \mathrm{FV}(L). If MM is another variable, both sides are MM. Applications follow directly from the induction hypothesis. If M=λz. M1M = \lambda z.\, M_1 with zz distinct from x,yx, y and not free in N,LN, L, both sides push the substitutions inside the abstraction and the induction hypothesis applies.

10.7 Notes and references

The λ-calculus was introduced by Church in the early 1930s as part of a foundational system that turned out to be inconsistent; the pure calculus survived. The confluence theorem is due to Church and Rosser; the proof by parallel reduction is due to Tait and Martin-Löf. Barendregt’s monograph is the standard reference.

  • A. Church, J. B. Rosser, Some properties of conversion, Transactions of the American Mathematical Society 39 (1936), 472–482.
  • H. B. Curry, R. Feys, Combinatory Logic, volume I, North-Holland, 1958, for the standardization theorem.
  • H. P. Barendregt, The Lambda Calculus: Its Syntax and Semantics, North-Holland, 1984.