Notes on Simply Typed Lambda Calculus^{∗}

Ralph Loader^{†}

February, 1998

The purpose of this course is to provide an introduction to λ-calculi, specifically the simply typed lambda calculus (λ^{→}).

λ-calculi are formalisms that are useful in computer science. They are languages that express both computational and logical information.

Computational information in that they can be see as functional programming languages, or more realistically, a solid core on which to build a functional language.

Logical information in two ways. First, typed λ-calculi can be used directly as logics—these are ‘intuitionistic type theories’ such as the calculus of constructions (e.g., used in the ‘Lego’ theorem proving software developed in this department). Second, typed λ-calculi underly the term structure of higher order logics.

There are many different λ-calculi—everyone working in the field has their own pet language(s)—but they generally fall into a few classes of broadly similar languages. This course does not provide a wide ranging survey, but instead aims to explain a good range of theoretical techniques, most of which are applicable to a much wider range of languages than we will actually study.

The first half of the course gives a fairly thorough introduction to fairly traditional and fundamental results about λ-calculi. The second half of the course gives a detailed, quantitative analysis of λ^{→}.

The book [3] covers much of the material in this course, especially sections 1–3. In section 1, we look at the untyped λ-calculus; [1] is a fairly comprehensive work on this. The article [2] is a fairly comprehensive work on pure type systems, which include the simply typed λ-calculus and impredicative intuitionistic type theories.

∗Version 1.8 dated 1998/02/16 15:10:19.

†Department of Computer Science, University of Edinburgh. ⟨loader@dcs.ed.ac.uk⟩

References

[1] Hendrik P. Barendregt. The Lambda Calculus, volume 103 of Studies in Logic and the Foundations of Mathematics. North-Holland, 1981 (revised edition 1984).

[2] Hendrik P. Barendregt. Lambda calculi with types. In S. Abramsky, D. M. Gabbai, and T. S. E. Maibaum, editors, Handbook of Logic in Computer Science, volume II. Oxford University Press, 199?

[3] Jean-Yves Girard, Yves Lafont, and Paul Taylor. Proofs and Types, volume 7 of Cambridge Tracts in Theoretical Computer Science. Cambridge University Press, Cambridge, 1989.

Contents

1 Untyped Lambda Calculus 4 1.1 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.2 Occurrences . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.3 Alpha Equivalence . . . . . . . . . . . . . . . . . . . . . . . . 6 1.4 Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.5 Beta Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.6 Some Examples—Recursive Functions . . . . . . . . . . . . . . 9 1.7 Church-Rosser (Confluence) Property . . . . . . . . . . . . . . 12

2 Simply Typed Lambda Calculus 14 2.1 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.2 Basic Properties . . . . . . . . . . . . . . . . . . . . . . . . . . 16 2.3 Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

3 Strong Normalisation 17 3.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3.2 The Main Induction . . . . . . . . . . . . . . . . . . . . . . . 20 3.3 A Combinatorial Proof . . . . . . . . . . . . . . . . . . . . . . 22

4 Quantitative Normalisation 23 4.1 Complete Developments . . . . . . . . . . . . . . . . . . . . . 23 4.2 Redex Depths . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 4.3 Calculating Upper Bounds . . . . . . . . . . . . . . . . . . . . 25

5 Quantitative Strong Normalisation 26 5.1 Translations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 5.2 Reduction of Translations . . . . . . . . . . . . . . . . . . . . 27 5.3 Translations and Strong Normalisation . . . . . . . . . . . . . 28

6 Lower Bounds for Normalisation 30 6.1 Higher Order Propositional Classical Logic . . . . . . . . . . . 31 6.2 Encoding the Boolean Hierarchy . . . . . . . . . . . . . . . . . 32 6.3 Encoding the Logic . . . . . . . . . . . . . . . . . . . . . . . . 35

A PCL_{ω} is not Elementary Recursive 35 A.1 Basic Encoding in PCL_{ω} . . . . . . . . . . . . . . . . . . . . . 35 A.2 Encoding Turing Machines . . . . . . . . . . . . . . . . . . . . 37

1 Untyped Lambda Calculus

A typed λ-calculus has a language in two parts: the language of types, and the language of terms. In order to introduce some basic notions, we shall first look at the untyped λ-calculus Λ, so that we can consider terms independently of types.

1.1 Syntax

The set T terms of the untyped λ-calculus is given by the following:

• A countably infinite set V of variables. Each variable is a term.

• If t and r are terms, then so is t r.

• If s is a term, and x is a variable, then λx. s is a term.

Each term has a unique representation in one of the above forms, and the set of terms is the smallest set closed under the three clauses above.

In short, this is given by the following grammar:

T := V | T T | λV. T

Induction and unique reading of terms are given as follows.

Induction: Suppose that S is a set (of terms) such that (a) x ∈ S for each variable x, (b) if t, r ∈ S then t r ∈ S and (c) if s ∈ S then λx. s ∈ S. Then S contains all terms.

Unique reading: Each term is exactly one of the following: a variable, an application, or an abstraction. If t r = t′ r′ then t = t′ and r = r′. If λx. s = λx′. s′ then x = x′ and s = s′. (Also, if two variables are equal as terms, then they are equal as variables—this one is confusing.)

These two principles enable us to define functions on T by recursion: Recursion: Let S be a set, and F_{var} : V ⟶ S, F_{app} : S × S ⟶ S and

F_{abs} : V ×S ⟶ S be functions. Then there is a unique function F : T ⟶ S such that

F (x) = F_{var}(x), F (t r) = F_{app}(F (t), F (r)), F (λx. s) = F_{abs}(x, F (s)).

As an application of the above, take S to be the set of natural numbers, and let l_{var}(x) = 1, l_{app}(m, n) = n + m + 1 and l_{abs}(x, m) = m + 1. Then the function l given by recursion satisfies:

l(x) = 1,  l(t r) = l(t) + l(r) + 1,  l(λx. s) = l(s) + 1.

The number l(r) is the length of the term r.

Another instance of recursion: Let S = P_{fin}(V), the set of finite sets of variables. Let FV_{var}(x) = {x}, FV_{app}(c, a) = c ∪ a and FV_{abs}(x, b) = b − {x}. Then the function FV given by recursion satisfies:

FV(x) = {x}, FV(t r) = FV(t) ∪ FV(r), FV(λx. s) = FV(s) − {x}.

The finite set FV(r) is called the set of free variables occurring in r. A term t is closed if FV(t) = ∅.

The bound variables of a term are given by the following equations:

BV(x) = ∅, BV(t r) = BV(t) ∪ BV(r), BV(λx. s) = {x} ∪ BV(s).

The set of sub-terms of a term r is the set defined by:

sub(x) = {x}, sub(λx. s) = {λx. s} ∪ sub(s),
sub(t r) = sub(t) ∪ sub(r) ∪ {t r}.

1.2 Occurrences

Consider the term r = xx. The variable x occurs twice in r. In order to reason about such situations, we shall give a formal definition of the notion of occurrence of a sub-term or variable, in terms of a path π telling us how to descend into a term. The reader who has an intuitive grasp of the notion of occurrence may wish to skip its formalisation…

The set of paths is {R, L, ∗}^{<ω}, the set of finite sequences of Rs, Ls and ∗s. For each term t we define path(t), the set of paths in t, by

path(x) = \{⟨⟩\} path(λx. s) = \{⟨∗, \overline{σ}⟩ | ⟨\overline{σ}⟩ ∈ path(s)\} ∪ \{⟨⟩\}

path(t r) = \{⟨L, \overline{σ}⟩ | ⟨\overline{σ}⟩ ∈ path(t)\} ∪ \{⟨R, \overline{σ}⟩ | ⟨\overline{σ}⟩ ∈ path(r)\} ∪ \{⟨⟩\}

We define a function occ such that if t ∈ T and σ ∈ path(t), then occ(t, σ) is a sub-term of t.

occ(t, ⟨⟩) = t
occ(t r, ⟨L, \overline{σ}⟩) = occ(t, ⟨\overline{σ}⟩) occ(t r, ⟨R, \overline{σ}⟩) = occ(r, ⟨\overline{σ}⟩)
occ(λx. s, ⟨∗, \overline{σ}⟩) = occ(s, ⟨\overline{σ}⟩)

A path σ is an occurrence of s in r if occ(r, σ) = s.

When it is convenient (i.e., nearly always) we shall abuse our terminology, confusing a sub-term with a particular sub-term occurrence. E.g., referring to an “occurrence s in r”, rather than a “sub-term s of r and some occurrence σ of s in r”.

Exercise 1.1 Refine the notions of free and bound variables of a term to free occurrences and bound occurrences of a variable in a term.

Suppose that σ is a path in r, that τ is a path in s, where s is the subterm at σ in r, and t is the sub-term at τ in s. Show that the concatenation στ is a path in r, and that t is the sub-term of r at στ.

Given a bound occurrence ρ of a variable x in r, the binder of ρ is the longest initial sub-sequence σ of ρ, such that occ(r, σ) is the form λx. s.

Exercise 1.2 Give a definition of the binder of a bound variable occurrence by recursion on the term r.

1.3 Alpha Equivalence

One of the most basic operations we need to consider is the replacement of a variable y in a term r by another term s. This operation is called substitution, and is denoted r[s/y].

Unfortunately, there is an immediate complication in the definition of this. Consider the terms r = λx. xy and s = xx. Then simply replacing y by s in r gives the term r′ = λx. x (xx). The free variable x in s has been ‘captured’ by the λ-abstraction, making it a bound variable in r′. This is undesirable for our purposes.

The solution is to first rename the bound variable x in r to some other variable, before carrying out the replacement. E.g., renaming x to z in r gives the term λz. zy. Then replacing y by s = xx gives λz. z (xx). The variable x is free in this term, as desired.

The formalisation of the ‘renaming of bound variables in a term’ is called α-equivalence. Two terms r_{1} and r_{2} are α-equivalent (r_{1} ≡_{α} r_{2}) if one can be obtained from the other by (a sensible) renaming of bound variables.

We could define α-equivalence in terms of a step-by-step renaming of bound variables in a term. Instead, we define α-equivalence in terms of occurrences. Two terms r_{1} and r_{2} are α-equivalent if the following hold:

1. path(r_{1}) = path(r_{2}).

2. The sets of free variable occurrences in r_{1} and r_{2} are equal. The sets of bound variable occurrences in r_{1} and r_{2} are equal.

3. If σ is a free variable occurrence in r_{1} (and by 2., in r_{2} also), then occ(r_{1}, σ) = occ(r_{2}, σ).

4. If σ is a bound variable occurrence in r_{1} and r_{2}, then the binder of σ in r_{1} is equal to the binder of σ in r_{2}.

Exercise 1.3 Show that α-equivalence is an equivalence relation.

Let r_{1} and r_{2} be terms with path(r_{1}) = path(r_{2}). (1) Take σ ∈ path(r_{1}) = path(r_{2}) and let s_{1} and s_{2} be the corresponding sub-terms occurring at σ. Show that s_{1} and s_{2} are either both variables, or both applications, or both abstractions. Show that the two parts of 2. in the definition of ≡_{α} are equivalent for r_{1} and r_{2}.

Exercise 1.4 Give a formal definition of what it means to replace a bound variable x by y in a term r. Give a definition of α-equivalence in terms of this replacement, and prove that it gives the same relation as our definition. You must be careful to avoid variable capture (e.g., avoid renaming λx. λy. x to λx. λx. x).

The exercise below shows that every term has plenty of nice α-equivalents.

Exercise 1.5 A term t is regular if it satisfies the condition that for every variable x, there is at most one occurrence in t of a sub-term in the form λx. s, and no variable is both free and bound in t.

Let t_{0} be a term, and let S be a finite set of variables (more generally, such that V − S is infinite). Show that there is a regular t ≡_{α} t_{0} such that no member of S is bound in t.

When it is convenient, we consider the set T /≡_{α} of α-equivalence classes of terms, rather than the set T of terms. Often we will not distinguish between a term and its α-equivalence class, and write such things as λx. x = λy. y.

Observe that the basic syntactical operations of abstraction and application are well-defined on α-classes, and that we may prove that a predicate holds for all α-classes by structural induction. However, the unique-reading property does not hold for α-classes, as the α-class of a λ-abstraction λx. s does not determine the variable x. Hence, to define a function on α-classes by structural recursion, we must carry out the recursion on terms, and verify that the function on terms leads to a well-defined function on α-classes.

1.4 Substitution

Let r and s be terms, and y be a variable, such that no variable is both free in s and bound in r, and y is not bound in r. The substitution r[s/y] is defined by recursion on r, as follows:

• y[s/y] = s, and x[s/y] = x for x ≠ y.

• (t r)[s/y] = (t[s/y])(r[s/y]).

• (λx. r)[s/y] = λx.(r[s/y]).

The following exercise shows that the substitution above gives a well defined, and total, operation on α-equivalence classes.

Exercise 1.6 Show that the paths in r[s/y] are (a) the paths ρ in r, (b) concatenations ρσ where ρ is a free occurrence of y in r and σ is a path in s. Express the sub-term at a path in r[s/y] in terms of sub-terms at paths in r and s.

Show that if r_{1} ≡_{α} r_{2} and s_{1} ≡_{α} s_{2} are such that r_{1}[s_{1}/y] and r_{2}[s_{2}/y] are both defined, then these two terms are α-equivalent.

Show that for any r, s and y, there is r′ ≡_{α} r such that r′[s/y] is defined.

Exercise 1.7 Show that if a variable x′ is neither free nor bound in s′, then s ≡_{α} s′ iff λx. s ≡_{α} λx′. s′[x′/x].

Exercise 1.8 Formulate a notion of simultaneous substitution, carrying out several substitutions at once: s[r_{1}/x_{1} … r_{m}/x_{m}]. Note that in general s[r_{1}/x_{1}, r_{2}/x_{2}] ≠ s[r_{1}/x_{1}][r_{2}/x_{2}].

1.5 Beta Conversion

The intuition behind the syntactical constructs of abstraction and application, is that λx. s is the function F defined by F x = s (i.e., F (r) is given by replacing x by r in s), and that t r is the result of applying the function t to argument r. This intuition is captured by the notion of β-reduction.

A redex is a term in the form (λx. s) r. The basic β-reduction relation is given by (λx. s) r ⟶_{b} s′[r/x], whenever s′ ≡_{α} s is such that s′[r/x] is defined.

The one step β-reduction relation ⟶_{β1} is given by allowing a basic β-reduction of redex occurrence. In other words, ⟶_{β1} is the least relation containing ⟶_{b} and closed under

\frac{t ⟶_{β1} t′}{t r ⟶_{β1} t′ r},  \frac{r ⟶_{β1} r′}{t r ⟶_{β1} t r′} ,  \frac{s ⟶_{β1} s′}{λx. s ⟶_{β1} λx. s′} .

The n step β-reduction relations ⟶_{β n} are given inductively by r ⟶_{β 0} r and, if r ⟶_{β n} r′ ⟶_{β1} r′′, then r ⟶_{β n+1} r′′. β-reduction ⟶_{β} is the least pre-order containing ⟶_{β1}, so that r ⟶_{β} r′ iff there is n such that r ⟶_{β n} r′. β-conversion (≡_{β}) is the least equivalence relation containing ⟶_{β1}.

Exercise 1.9 For each of the reduction relations ⟶_{•}, give an analogous definition of ⟶_{•} as a relation on α-equivalence classes. Show that you have made the correct definition, by showing that for α-classes r and r′, the following are equivalent (a) r ⟶_{•} r′, (b) there are r_{0} ∈ r and r′_{0} ∈ r′ such that r_{0} ⟶_{•} r′_{0}, and (c) for every r_{0} ∈ r there is r′_{0} ∈ r′ such r_{0} ⟶_{•} r′_{0}.

Show that (a) if s ⟶_{β1} s′ then s[r/x] ⟶_{β1} s′[r/x], and (b) if r ⟶_{β} r′, then s[r/x] ⟶_{β} s[r′/x].

A term is called normal if no redexes occur in it. A term is weakly normalising (WN) if it β-reduces to a normal term. A term r is strongly normalising (SN) if there is no infinite sequence r = r_{0} ⟶_{β} r_{1} ⟶_{β} · · ·. Given a term t, we define sn(t) = sup{n | ∃t′ s.t. t ⟶_{β n} t′} ∈ N ∪ {∞}.

Exercise 1.10 Show that every strongly normalising term is weakly normalising, but not conversely. Show that sn(t) < ∞ iff t is SN.^{1}

1.6 Some Examples—Recursive Functions

We give some examples of terms, and their behaviour under reduction.

The Church Numerals are an encoding of the natural numbers, given by

⌜n⌝ = λf. λx. f^{n}(x)

where f^{n}(x) indicates the usual iterated application: f^{0}(x) = x, f^{n+1}(x) = f (f^{n}(x)). A term F encodes a function f if F ⌜i⌝ ⟶_{β} ⌜f(i)⌝ for each n.^{2} More generally, F encodes a function f of N arguments if

F ⌜i_{1}⌝ … ⌜i_{N}⌝ ⟶_{β} ⌜f(i_{1} … i_{N})⌝

for all i_{1} … i_{N}.

The successor function n ↦ n+1 is encoded by

S = λn. λf. λx. f (nfx),

so that S ⌜n⌝ ⟶_{β} ⌜n+1⌝. Addition is given by

add = λm. λn. λf. λx. mf (nfx),

so that add ⌜n⌝ ⌜m⌝ ⟶_{β} ⌜n + m⌝, and multiplication by

mult = λm. λn. λf. m (nf).

1Note that one direction of this is not constructive. Everything else in these notes is constructive.

2By theorem 1.18 (Church-Rosser), a term encodes at most one function.

There is a zero test

?_{0} = λn. λx. λy. n (λz. y) x

with the property that ?_{0} ⌜0⌝ r s ⟶_{β} r and ?_{0} ⌜n+1⌝ r s ⟶_{β} s. There is the Church pairing, with

pair = λx. λy. λz. zxy π_{1} = λp. p (λx. λy. x) π_{2} = λp. p (λx. λy. y)

so that

π_{1} (pair r s) ⟶_{β} r and π_{2} (pair r s) ⟶_{β} s.

This enables us to encode primitive recursion. Let

step = λp. p (λx. λy. pair (S x) (f x y))

so that step (pair ⌜n⌝ a) ⟶_{β} pair ⌜n+1⌝ (f a). Defining

rec_{p} = λn. n step (pair ⌜0⌝ x)

we have

rec_{p} ⌜0⌝ ⟶_{β} pair ⌜0⌝ x

and, if rec_{p} ⌜n⌝ ⟶_{β} pair ⌜n⌝ a, then

rec_{p} ⌜n+1⌝ ⟶_{β} pair ⌜n+1⌝ (f ⌜n⌝ a).

Letting rec = λf. λx. λn. π_{1}(rec_{p} fxn), we have that

rec f a ⌜0⌝ ≡_{β} a rec f a ⌜n+1⌝ ≡_{β} f ⌜n⌝ (rec f a ⌜n⌝).

Not every term has a normal form. For example, letting

Ω = (λx. xx)(λx. xx),

we have that Ω ⟶_{β1} Ω ⟶_{β1} · · ·. Some terms without normal forms are useful, as the example following shows. Programming languages usually allow recursive function definitions: F x = t, where t is some expression containing both x and F. In λ-calculus notation, F = λx. t. Of course, we cannot solve this up to equality; the next best thing is to find a term F such that F ⟶_{β} λx. t. More generally, letting G = λF. λx. t, it suffices to find Y_{G} such that Y_{G} ⟶_{β} G Y_{G}. For example, take

Y_{G} = (λx. G (xx))(λx. G (xx)).

Exercise 1.11 Check all claims made above.

Exercise 1.12 Find a fixed-point combinator Y that β-reduces to the term λf. f (Y f). (Given Y, we could have taken the Y_{G} above to be Y G. You can also go the other way round, and find G such that Y = Y_{G} has the required property.)

Using Y, we can encode minimisation. If f is a function on the natural numbers such that f(i) = 0 for some i, then μf is the least such i. Similarly, let μ_{⩾n}f be the least i ⩾ n such that f(i) = 0, if such exists. This is given recursively by:

μ_{⩾n}f = \{{n if f(n) = 0 \atop μ_{⩾n+1}f if f(n) ≠ 0}

Hence, we want a term μ_{⩾}, such that μ_{⩾} n f ⟶_{β} ?_{0} (f n) n (μ_{⩾} (S n) f). Letting

μ_{⩾} = Y (λM. λn. λf.?_{0} (fn) n (M (S n) f))

does the trick, and then let μ = μ_{⩾} ⌜0⌝. With these definitions, if F encodes a function f, and f has a zero, then

μ F ⟶_{β} ⌜μf⌝.

Exercise 1.13 The total recursive functions are the least set of functions (of arbitrary arity) on the natural numbers, that

1. Contains the constant functions, the successor function and the projections (x_{1} … x_{m}) ↦ x_{i} (whenever 1 ⩽ i ⩽ m).

2. Is closed under composition: h(\overline{x}) = f(g_{1}(\overline{x}) … g_{n}(\overline{x})).

3. Is closed under primitive recursion: h(\overline{x}, 0) = f(\overline{x}), h(\overline{x}, n + 1) = g(\overline{x}, n, f(\overline{x}, n)).

4. Is closed under minimisation, whenever this gives a total function: if for each \overline{x} there is n such that f(\overline{x}, n) = 0, then take g(\overline{x}) to be the least such n.

Show that every total recursive function can be encoded by a λ-term.

It was conjectured by Church that the computable functions on the natural numbers are precisely those that can be encoded by a λ-term. This is the famous Church’s Thesis. The exercise above is one half of showing that the functions encodable in Λ coincide with the recursive functions. Unsurprisingly, the functions on the natural numbers encodable in Λ coincide with the functions computable in lots of other mathematical models of computability.

Theorem 1.14 The predicate of being weakly normalising is not decidable.

Proof Sketch: Suppose that WN were decidable. Then we could define a computable function F on λ-terms such that F (t) is the normal form of t if there is one, and F (t) = λx. x otherwise. Then F would be a total computable function majorising every total recursive function, which is not possible. □

1.7 Church-Rosser (Confluence) Property

Our reduction relation is not deterministic—given a term r, it may contain several redexes, and hence several different ways of reducing it.

However, there is a certain determinism to the intuition behind our calculus—function application gives a unique value. This is expressed locally in the fact that there is (up to ≡_{α}) only one way of reducing a given redex. It can also be expressed globally in the confluence, or Church-Rosser, property: if r ⟶_{β} r_{1} and r ⟶_{β} r_{2}, then there is r′ such that r_{1} ⟶_{β} r′ and r_{2} ⟶_{β} r′. In particular, a term has at most one normal form.

The proof of Church-Rosser is based on a quite simple idea—if r contains two redexes, then the reduction of one redex does not stop the reduction of the other redex, and it does not matter in what order the reductions are carried out. This makes it quite simple to show that if r ⟶_{β1} r_{1}, r_{2}, then there is r′ such that r_{1}, r_{2} ⟶_{β} r′. However, note the β1- and β- reductions in this. To extend this result to confluence for β-reduction, we must be careful to avoid an infinite regress. The proof we give (due to Hyland) replaces ⟶_{β1} with a ‘parallel’ reduction, which simultaneously reduces any collection of redexes in a term.

Let R be a relation, and ⩽ be the least pre-order containing R. R is confluent, if whenever a R a_{1} and a R a_{2}, there is a′ such that a_{1} R a′ and a_{2} R a′. R is weakly confluent, if whenever a R a_{1}, a_{2}, there is a′ such that a_{1}, a_{2} ⩽ a′ .

Exercise 1.15 Let R be a relation and ⩽ the least pre-order containing R. Show that if R is confluent, then so is ⩽. Find a weakly confluent R such that ⩽ is not confluent.

The parallel reduction relation, ⟶_{p}, is the relation on α-classes given inductively by:

\overline{x ⟶_{p} x}    \frac{t ⟶_{p} t′ r ⟶_{p} r′}{t r ⟶_{p} t′ r′}  \frac{s ⟶_{p} s′}{λx. s ⟶_{p} λx. s′}

\frac{s ⟶_{p} s′ r ⟶_{p} r′}{(λx. s′) r ⟶_{p} s′[r′/x]}

The lemma below follows easily from the definitions of the various reduction relations.

Lemma 1.16 If r ⟶_{β1} r′ then r ⟶_{p} r′. If r ⟶_{p} r′, then r ⟶_{β} r′. The least pre-order containing ⟶_{p} is ⟶_{β}. If s ⟶_{p} s′ and r ⟶_{p} r′, then s[r/x] ⟶_{p} s′[r′/x]. □

Lemma 1.17 ⟶_{p} is confluent.

Proof: We show that if t, t_{1} and t_{2} are α-classes, with t ⟶_{p} t_{1} and t ⟶_{p} t_{2}, then, then there is t′ such that t_{1} ⟶_{p} t′ and t_{2} ⟶_{p} t′. We perform the construction by induction on t.

The non-trivial step is for t in the form (λx. s) r. Then (for i = 1, 2) there are s_{i} and r_{i} such that s ⟶_{p} s_{i}, r ⟶_{p} r_{i}, and either t_{i} = (λx. s_{i}) r_{i} or t_{i} = s_{i}[r_{i}/x]. By the induction hypothesis, there are r′ and s′ such that (for i = 1, 2) r_{i} ⟶_{p} r′ and s_{i} ⟶_{p} s′. Let t′ = s′[r′/x]. Then we have that both (λx. s_{i}) r_{i} ⟶_{p} t′ and s_{i}[r_{i}/x] ⟶_{p} t′, so that t_{1}, t_{2} ⟶_{p} t′.

The other cases are easy: e.g. if t is in the form s r, but is not a redex, then the t_{i} are in the form s_{i} r_{i} where s ⟶_{p} s_{1}, s_{2} and r ⟶_{p} r_{1}, r_{2}. Applying the induction hypothesis, there are s′ and r′ such that s_{1}, s_{2} ⟶_{p} s′ and r_{1}, r_{2} ⟶_{p} r′. Letting t′ = s′ r′, we have t_{1}, t_{2} ⟶_{p} t′, as required. □

Theorem 1.18 (Church-Rosser) The relation ⟶_{β} is confluent (on αclasses). □

Corollary 1.19 If a term β-reduces to a normal form, then the normal form is unique (up to ≡_{α}). □

Exercise 1.20 (Standardisation) The standard reduction relation ⟶_{s} is given by:

\frac{r_{1} ⟶_{s} r′_{1} … r_{m} ⟶_{s} r′_{m}}{x r_{1} … r_{m} ⟶_{s} x r′_{1} … r′_{m}}  \frac{s[r_{0}/x] r_{1} … r_{m} ⟶_{s} t}{(λx. s) r_{0} r_{1} … r_{m} ⟶_{s} t}

\frac{s ⟶_{s} s′ r_{1} ⟶_{s} r′_{1} … r_{m} ⟶_{s} r′_{m}}{(λx. s) r_{1} … r_{m} ⟶_{s} (λx. s) r′_{1} … r′_{m}}

Show that if r ⟶_{s} r′, then r ⟶_{β} r′, and give a characterisation of the reduction sequences obtained (in terms of what redexes are reduced in what order). Show that if r′ is normal, then r ⟶_{β} r′ by a reduction sequence that always reduces the leftmost redex (appropriately defined).

Show that ⟶_{s} is closed under the following:

\frac{s ⟶_{s} s′ r ⟶_{s} r′}{s[r/x] ⟶_{s} s′[r′/x]}  \frac{t ⟶_{s} (λx. s′) r′}{t ⟶_{s} s′[r′/x]}  \frac{t ⟶_{s} t′ ⟶_{β1} t′′}{t ⟶_{s} t′′}

and that ⟶_{s} and ⟶_{β} are the same relation.

Conclude that if r ⟶_{β} r′, then r reduces to r′ with a reduction sequence in some special form. In particular, if r ⟶_{β} r′, and r′ is normal, then leftmost reduction reduces r to r′. These are the standardisation theorems.

The notion of a term F encoding a (total) function f, is extended to F encoding a partial function f, by requiring that if f(n) is undefined, then F ⌜n⌝ does not β-reduce to a λ-abstraction.^{3}

In exercise 1.13, we referred to only total recursive functions. This is because we didn’t have results useful for showing partiality as encoded above. The standardisation results in the previous exercise are such results.

Exercise 1.21 The partial recursive functions are given by removing the condition (in exercise 1.13) that functions given by minimisation are total. Show that every partial recursive function can be encoded by a λ-term. You will need to take care of the fact that mathematical functions are strict, in that f(x_{1} … x_{m}) is undefined if any of the x_{i} are, while the obvious encodings in Λ are not strict.

2 Simply Typed Lambda Calculus

So far, we have dealt with the untyped λ-calculus. There is no restriction on how we can use objects—for example, any value can be used as a function. In real life, this is undesirable; what does it mean to apply the number 5 to the function cos? Type systems are one way of (statically) ensuring that operations are only applied to appropriate objects. We shall introduce a type system for the λ-calculus, obtaining the simply typed λ-calculus, λ^{→} for short.

2.1 Syntax

The syntax of λ^{→} is presented in stages.

3Perhaps not having a normal form would seem more intuitive. However, the stronger condition given is easier to work with.

1. The types are defined.

2. The pre-terms are defined. Pre-terms are essentially terms of Λ, and include things that we do not wish to consider in λ^{→}, such as λx:o. xx.

3. A system of typing judgements is given, that identifies the terms—those pre-terms that we wish to consider in λ^{→}.

We take an arbitrary non-empty set G of ground types. For concreteness and simplicity, we shall take G = {o}; there is no reason why some other (larger) set should not be used. Types are then given by the following grammar:

τ := G | τ → τ.

In order to avoid ambiguity in λ^{→}, we decorate λ-abstractions with types. The grammar for pre-terms of λ^{→} is:

T := V | T T | λV:τ. T .

The notions such as α-equivalence, substitution and reduction can be carried across more or less verbatim from Λ to λ^{→}. The definition of α-equivalence must be modified to ensure that the type decorations are preserved, e.g., so that λx:A. x ≡_{α} λy:B. y if and only if A = B. Our proof of the Church-Rosser theorem still works for the pre-terms of the typed calculus.

A context is a function mapping a finite set of variables into the set of types. Contexts will be written in the form x_{1}:A_{1} … x_{m}:A_{m}. They should be read as a typing declaration: the context x_{1}:A_{1} … x_{m}:A_{m} indicates that each variable x_{i} will be considered to have the corresponding type A_{i}. We present a ternary relation Γ ⊢ s : B on contexts Γ, pre-terms s and types B by the following clauses:

\overline{x_{1}:A_{1} … x_{m}:A_{m} ⊢ x_{i} : A_{i}} for 1 ⩽ i ⩽ m

\frac{Γ ⊢ f : A → B Γ ⊢ r : A}{Γ ⊢ f r : B}

\frac{Γ x:A ⊢ s : B}{Γ ⊢ λx. s : A → B}

A pre-term s is said to be a term (of type B in context Γ) if Γ ⊢ s : B.

We also define the relation Γ ⊢ s : B for α-classes s of pre-terms, using identical clauses.

Exercise 2.1 Show that the relations ⊢ for pre-terms, and for α-classes, are related as follows: an α-class s has Γ ⊢ s : B derivable if and only if there is s_{0} ∈ s such that Γ ⊢ s_{0} : B is derivable.

2.2 Basic Properties

Lemma 2.2 (Renaming) If Γ z:C Δ ⊢ s : B for some α-class s, and z′ is not in the context, then Γ z′:C Δ ⊢ s[z′/z] : B.

Proof: We in fact show that simultaneous renaming is possible, in that if x_{1}:A_{1} … x_{m}:A_{m} ⊢ s : B, and x′_{1} … x′_{m} is a sequence of distinct variables, then x′_{1}:A_{1} … x′_{m}:A_{m} ⊢ s[x′_{1}/x_{1} … x′_{m}/x_{m}] : B.

We show this by induction on derivations. We do the case of a derivation ending

\frac{x_{1}:A_{1} … x_{m}:A_{m} x:A ⊢ s : B}{x_{1}:A_{1} … x_{m}:A_{m} ⊢ λx:A. s : A → B.}

Let x′ be a variable distinct from the x′_{i}. By the induction hypothesis,

x′_{1}:A_{1} … x′_{m}:A_{m} x′:A ⊢ s[x′_{1}/x_{1} … x′_{m}/x_{m}, x′/x] : B

and hence

x′_{1}:A_{1} … x′_{m}:A_{m} ⊢ λx′:A.(s[x′_{1}/x_{1} … x′_{m}/x_{m}, x′/x]) : B

are derivable. The equation

λx′:A.(s[x′_{1}/x_{1} … x′_{m}/x_{m}, x′/x]) = (λx:A. s)[x′_{1}/x_{1} … x′_{m}/x_{m}]

holds, and the induction step follows. □

Lemma 2.3 (Weakening) Let s be an α-class such that ΓΔ ⊢ s : B. Let z be a variable not in ΓΔ. Then Γ z:C Δ ⊢ s : B

Proof: By the renaming lemma, it suffices to show that there is z such that Γ z:C Δ ⊢ s : B. This can be shown by an easy induction on derivations. □

Lemma 2.4 (Uniqueness of Derivations) If s is a pre-term, with Γ ⊢ s : B, then the derivation of this fact is unique. □

Lemma 2.5 (Unicity of Typing) If s is an α-class, with Γ ⊢ s : B and Γ ⊢ s : B′, then B = B′. □

Exercise 2.6 Prove the two lemmas above.

Lemma 2.7 (Substitution) Let r and s be α-classes such that Γ ⊢ r : A and Γ x:A Δ ⊢ s : B. Then ΓΔ ⊢ s[r/x] : B. □

Exercise 2.8 Prove it. Use induction on a derivation of Γ x:A Δ ⊢ s : B.

Lemma 2.9 (Subject Reduction) Let s be an α-class with Γ ⊢ s : B, and s ⟶_{β} s′. Then Γ ⊢ s′ : B also.

Proof: The case when s ⟶_{b} s′ reduces to the substitution lemma above. Closing under the term constructs of application and abstraction shows that the lemma holds for s ⟶_{β1} s′. An induction shows that the lemma holds for ⟶_{β} also. □

2.3 Models

We give a notion of model of λ^{→}. What is given here is far from the most general notion of model possible. A model is a map M assigning a set M(o) to each ground type. We define the interpretation ⟦·⟧, first for types, and then for terms. Put ⟦o⟧ = M(o), and ⟦A → B⟧ = ⟦B⟧^{⟦A⟧} (the set of all functions from ⟦A⟧ to ⟦B⟧).

Given a context Γ = x_{1}:A_{1} … x_{m}:A_{m}, we define a valuation σ on Γ to be a function defined on the x_{i} such that σ(x_{i}) ∈ ⟦A_{i}⟧ for 1 ⩽ i ⩽ m. Given another variable x and a ∈ ⟦A⟧, we define σ[x := a](x_{i}) = σ(x_{i}) and σ[x := a](x) = a, so that σ[x := a] is a valuation on Γ x:A.

We define ⟦s⟧_{σ} ∈ ⟦B⟧ whenever Γ ⊢ s : B and σ is a valuation on Γ, by

⟦x⟧_{σ} = σ(x) ⟦t r⟧_{σ} = ⟦t⟧_{σ}(⟦r⟧_{σ})

and, for a ∈ ⟦A⟧,

⟦λx:A. s⟧_{σ}(a) = ⟦s⟧_{σ[x:=a]}.

The following lemma shows that our semantics gives some meaning to βreduction, as well as to syntax.

Lemma 2.10 If Γ ⊢ r : A and Γ x:A ⊢ s : B, then

⟦s[r/x]⟧σ = ⟦s⟧σ[x := ⟦r⟧_{σ}].

If Γ ⊢ s : B and s ⟶_{β} s′, then ⟦s⟧_{σ} = ⟦s′⟧_{σ}. □

Exercise 2.11 Prove the lemma. Be careful about contexts!

3 Strong Normalisation

The set-theoretic semantics of λ^{→} of section 2.3 gives a sense in which λ^{→} meaningful—it formalises the intuition that A → B is a type of functions, that terms of a function type are functions, and that terms are values of some sort. The semantics gives meaning to β-reduction, by lemma 2.10.

The meaning given to λ^{→} by the semantics is static and external. Static in the sense that terms are represented by values, rather than by computations; it gives no sense of the term t computing the value ⟦t⟧. External in the sense that terms are given meaning in terms of some structure outside of the syntax.

The normalisation theorem proved in this section gives meaning to λ^{→} in a way that is dynamic and internal, by showing that every term is strongly normalising. This is dynamic in the sense that the meaning of a term is given by a computational process: the reduction of a term t to a normal form v is in a series of steps t = t_{0} ⟶_{β1} t_{1} ⟶_{β1} ⋯ ⟶_{β1} t_{n} = v. These steps can be seen as the successive states of a computer running the program t to reach a value v. Strong normalisation is internal, in that it gives meaning to the calculus without reference to another structure.

A common way to prove the termination of some sequence t_{0}, t_{1}, … is to find some measure |t_{i}| ∈ N that is strictly decreasing in i. Later, we will carry out such a construction, in order to give a quantitative analysis of strong normalisation for λ^{→}. However, such a proof is non-trivial. A first attempt might be to take |t| to be the length of the term t. This fails as reduction of a redex (λx. s) r to s[r/x] can increase length. In the proof of strong normalisation that follows, we avoid this problem by taking into account the meaning of terms: we show that a term f : A → B is not just SN, but that f maps SN terms of type A to SN terms of type B.

Normalisation for λ^{→} was first proved by Turing, although Gentzen had earlier given similar cut-elimination results for sequent calculi. The proof method we use is due to Tait. Girard extended it to prove normalisation for F, the second order λ-calculus, and since then it has been used to prove normalisation for just about everything.

3.1 Preliminaries

We start by noting some properties of SN, the set of strong normalising terms of Λ. We leave it as an exercise to rewrite these for λ^{→}.

Exercise 3.1 If t ∈ SN and t ⟶_{β} t′, then t′ ∈ SN. If t′ ∈ SN for all t′ such that t ⟶_{β1} t′, then t′ ∈ SN. If t ∈ SN and t′ is a sub-term of t, then t′ ∈ SN.

Lemma 3.2 The set SN is closed under the following:

\frac{r_{1} … r_{m}}{x r_{1} … r_{m}}  \frac{s}{λx. s}  \frac{s[r_{0}/x] r_{1} … r_{m} r_{0}}{(λx. s) r_{0} r_{1} … r_{m}}  (1)

Proof: The first two clauses are more or less trivial. We do the last clause. Recall that sn(t) is the l.u.b. of the length of the reduction sequences starting from t. Suppose that s[r_{0}/x] r_{1} … r_{m} ∈ SN and r_{0} ∈ SN.

We show by induction on the number sn(s[r_{0}/x] r_{1} … r_{m}) + sn(r_{0}), that (λx. s) r_{0} r_{1} … r_{m} ∈ SN.

Suppose (λx. s) r_{0} r_{1} … r_{m} ⟶_{β1} t. We show that t is SN by considering the possibilities for t.

If t = s[r_{0}/x] r_{1} … r_{m}, then t ∈ SN.

If t = (λx. s) r′_{0} r_{1} … r_{m} where r_{0} ⟶_{β1} r′_{0}, then we get the β-reduction s[r_{0}/x] r_{1} … r_{m} ⟶_{β} s[r′_{0}/x] r_{1} … r_{m}. This gives the inequalities

sn(s[r′_{0}/x] r_{1} … r_{m}) ⩽ sn(s[r_{0}/x] r_{1} … r_{m}) and sn(r′_{0}) < sn(r_{0}).

By the induction hypothesis, t ∈ SN.

Otherwise, the redex reduced is in s or one of r_{1} … r_{m}, and we have t = (λx. s′) r_{0} r′_{1} … r′_{m} where s[r_{0}/x] r_{1} … r_{m} ⟶_{β1} s′[r_{0}/x] r′_{1} … r′_{m}, and applying the induction hypothesis gives t ∈ SN. □

Exercise 3.3 Refine the proof of lemma 3.2 to show that if t can be derived by using the clauses of (1) n times, then sn(t) ⩽ n.

Show that SN is the least set closed under the three clauses (1)—i.e., that every SN term can be derived using those clauses.

Exercise 3.4 Show that if x is free in s, and s[r_{0}/x] r_{1} … r_{m} ∈ SN, then (λx. s) r_{0} … r_{m} ∈ SN.

The λI-calculus Λ_{I} is given in exactly the same way as Λ, except that in all λ-abstractions λx. s, the variable x is required to be free in s. Show that every weakly normalising Λ_{I} term is strongly normalising. Hint: use the standardisation results of exercise 1.20 and the first part of this exercise.

We wish to make the following definition, recursive in types:

• Red_{o} is the set of strongly normalising terms of type o, for any ground type o.

• Red_{A→B} is the set of terms f of type A→B, such that for any r ∈ Red_{A}, we have f r ∈ Red_{B}.

Unfortunately, this does not quite work for our purposes, as the contexts of f and r might be different. One way of avoiding this issue is to reformulate λ^{→} slightly, so that variables have types pre-assigned, rather than being declared in contexts. Another way is to simply keep track of contexts; we therefore make the following definition of the sets of reducible terms:

Red_{o} = {(Γ, r) | r ∈ SN, Γ ⊢ r : o}

for ground types o, and

Red_{A→B} = {(Γ, f) | Γ ⊢ f : A → B, (ΓΔ, f r) ∈ Red_{B} ∀(ΓΔ, r) ∈ Red_{A}}.

Lemma 3.5 1. If (Γ, s) ∈ Red_{B} then s ∈ SN.

2. If Γ ⊢ x r_{1} … r_{m} : B, and r_{1}, …, r_{m} ∈ SN, then

(Γ, x r_{1} … r_{m}) ∈ Red_{B}.

3. If (Γ, s[r_{0}/x] r_{1} … r_{m}) ∈ Red_{B}, Γ ⊢ r_{0} : A and r_{0} ∈ SN, then

(Γ, (λx:A. s) r_{0} r_{1} … r_{m}) ∈ Red_{B}.

Proof: We prove the properties simultaneously by induction on the type B. For B = o, 1. is trivial, and 2. and 3. are properties of SN. Suppose that B = C → D.

For 1., let z be a variable not in Γ, so that by the induction hypothesis of 2. for C, (Γ z:C, z) ∈ Red_{C}. As (Γ, s) ∈ Red_{C→D}, we have (Γ z:C, s z) ∈ Red_{D}, so that by the induction hypothesis of 1. for D, s z ∈ SN and hence s ∈ SN also.

For 2., take any (ΓΔ, r_{m+1}) ∈ Red_{C}. By the IH 1. for C, we have that r_{m+1} ∈ SN, so that by the IH 2. for D, (ΓΔ, x r_{1} … r_{m+1}) ∈ Red_{D}. The definition of Red_{C→D} now gives (Γ, x r_{1} … r_{m}) ∈ Red_{B}.

For 3., take any (ΓΔ, r_{m+1}) ∈ Red_{C}. Then, (ΓΔ, s[r_{0}/x] r_{1} … r_{m+1}) ∈ Red_{D}, so that by the IH, (ΓΔ, (λx:A. s) r_{0} r_{1} … r_{m+1}) ∈ Red_{D}. This shows that (Γ, (λx:A. s) r_{0} r_{1} … r_{m}) ∈ Red_{B}. □

Exercise 3.6 Check that if (Γ, s) ∈ Red_{B}, then (Γ x:A, s) ∈ Red_{B} also.

3.2 The Main Induction

We prove that terms of λ^{→} are SN, by showing that they are members of the reducibility sets defined above. The definition of Red_{A→B} as the set of those terms which give a function from Red_{A} to Red_{B}, means that the sets Red_{A} form something like a model of λ^{→}. Our proof that every term is reducible will be a structural induction bearing a more than passing similarity to the definition of the interpretation of a term in a model.

Lemma 3.7 Suppose y_{1}:B_{1} … y_{n}:B_{n} ⊢ r : A. For any (Γ, s_{j}) ∈ Red_{B_{j}} (1 ⩽ j ⩽ n), we have (Γ, r[s_{1}/y_{1} … s_{n}/y_{n}]) ∈ Red_{A}.

Proof: We write \overline{s}/\overline{y} for s_{1}/y_{1} … s_{n}/y_{n}. The proof is by induction on the term r.

For r a variable, the lemma is trivial.

Consider an application t r where r : A and t : A → B. The induction hypotheses are that (Γ, t[\overline{s}/\overline{y}]) ∈ Red_{A→B} and (Γ, r[\overline{s}/\overline{y}]) ∈ Red_{A}. The definition of Red_{A→B} gives (Γ, (t r)[\overline{s}/\overline{y}]) ∈ Red_{B}, as required.

Consider a λ-abstraction, λx:A. b : A → B. Take any (ΓΓ′, a) ∈ Red_{A}. For each j, (ΓΓ′, s_{j}) ∈ Red_{B_{j}}, so that by the induction hypothesis,

(ΓΓ′, b[\overline{s}/\overline{y}, a/x]) ∈ Red_{B}.

Also, a ∈ SN as a ∈ Red_{A}, so that by 3. of lemma 3.5, we have

(ΓΓ′, (λx:A. b)[\overline{s}/\overline{y}] a) ∈ Red_{B}.

By the definition of Red_{A→B}, we get (Γ, (λx:A. b)[\overline{s}/\overline{y}]) ∈ Red_{A→B}. □

Theorem 3.8 Every term of λ^{→} is SN.

Proof: Given y_{1}:B_{1} … y_{n}:B_{n} ⊢ r : A, take Γ = y_{1}:B_{1} … y_{n}:B_{n} and s_{j} = y_{j} in the lemma above. This gives (Γ, r) = (Γ, r[\overline{y}/\overline{y}]) ∈ Red_{A}, and so r ∈ SN.

□

Exercise 3.9 Gödel’s system T is the following extension of λ^{→}: take the ground type to be N, and introduce constructs 0, S and R with the following rules:

\overline{Γ ⊢ 0 : N}  \frac{Γ ⊢ n : N}{Γ ⊢ S(n) : N}

\frac{Γ ⊢ n : N Γ ⊢ a : A Γ, x:A ⊢ r : A}{Γ ⊢ R(n, a, x. r)}

The type N is intended to represent the natural numbers, with 0 and S being zero and the successor function. The operator R gives definitions by recursion: R(n, a, x. f x) is intended to be f^{n}(a). This is expressed by the following reduction rules:

R(0, a, x. r) ⟶ a R(S(n), a, x. r) ⟶ r[R(n, a, x. r)/x].

Adapt the proof of theorem 3.8 to prove SN for Gödel’s T.

3.3 A Combinatorial Proof

We give another proof of SN for λ^{→} that is less abstract in the sense that it doesn’t require the predicates Red_{A}. The combinatorial methods we use here are less general—for instance, cannot be applied to Gödel’s T —but can be formalised in a logic of first order arithmetic, such as Peano Arithmetic. The technique is in essence that used by Gentzen to prove cut-elimination for LK, the classical sequent calculus. Later, we shall give an even less abstract proof, one that is formalisable in primitive recursive arithmetic.

We shall call a term inductively normalising (IN) if it is derivable by the three clauses (1). By lemma 3.2, the inductively normalising terms are strongly normalising.

We shall call a type A substitutive if IN is closed under the following clause:

\frac{Γ ⊢ r : A Γ x:A Δ ⊢ s : B r ∈ IN s ∈ IN}{s[r/x] ∈ IN} ∀ Γ, r, x, Δ, s, B. (2)

The type A is called applicative if IN is closed under:

\frac{Γ ⊢ t : A → B Γ ⊢ r : A t ∈ IN r ∈ IN}{t r ∈ IN} ∀ Γ, t, B, r.  (3)

Lemma 3.10 Every substitutive type is applicative.

Proof: Let A be a substitutive type. We show that IN is closed under (3) by induction on the fact that t ∈ IN.

If t = x r_{1} … r_{m} with r_{1}, …, r_{m} ∈ IN, then as r ∈ IN also, we have t r = x r_{1} … r_{m} r ∈ IN.

If t = λx:A. s with s ∈ IN, then as A is substitutive, we have s[r/x] ∈ IN and hence t r = (λx:A. s) r ∈ IN.

If t = (λz:C. s) r_{0} … r_{m} where s[r_{0}/z] r_{1} … r_{m} ∈ IN and r_{0} ∈ IN, then by the induction hypothesis, s[r_{0}/z] r_{1} … r_{m} r ∈ IN. As also r_{0} ∈ IN, we get t r = (λz:C. s) r_{0} … r_{m} r ∈ IN. □

Lemma 3.11 Suppose that A = A_{1} → ⋯ → A_{M} → o, where each A_{i} is applicative. Then A is substitutive.

Proof: We show that IN is closed under (2) by induction on the fact that s ∈ IN.

If s = x s_{1} … s_{m} where s_{1}, …, s_{m} ∈ IN, then by the induction hypothesis, s_{i}[r/x] ∈ IN (1 ⩽ i ⩽ m). The term s_{i}[r/x] has type A_{i}, which is applicative, so that s[r/x] = r s_{1}[r/x] … s_{m}[r/x] ∈ IN.

If s = y s_{1} … s_{m} where s_{1}, …, s_{m} ∈ IN and y ≠ x, then by the induction hypothesis, s_{i}[r/x] ∈ IN (1 ⩽ i ⩽ m). Then s[r/x] = y s_{1}[r/x] … s_{m}[r/x] ∈ IN.

If s = λy:B. s′ where s′ ∈ IN, then by the induction hypothesis, s′[r/x] ∈ IN so that s[r/x] = λy:B. s′[r/x] ∈ IN.

If s = (λy:B. s′) r_{0} … r_{m} where s′[r_{0}/y] r_{1} … r_{m} ∈ IN and r_{0} ∈ IN, then by the induction hypothesis,

s′[\frac{r}{x}][r_{0}[\frac{r}{x}]/y] (r_{1}[\frac{r}{x}]) … (r_{m}[\frac{r}{x}]) = (s′[r_{0}/y] r_{1} … r_{m})[r/x] ∈ IN,

and r_{0}[r/x] ∈ IN, so that s[r/x] = (λy:B. s′[\frac{r}{x}])(r_{0}[\frac{r}{x}]) … (r_{m}[\frac{r}{x}]) ∈ IN. □

Theorem 3.12 Every term of λ^{→} is SN.

Proof: An induction on types using lemmas 3.10 and 3.11 shows that every type is applicative. Together with the definition of IN, this shows that IN is closed under all the typing rules of λ^{→}, so that induction on the derivation of Γ ⊢ s : B gives s ∈ IN. □

4 Quantitative Normalisation

In this section we give a quantitative analysis of normalisation in λ^{→}. We shall give an upper bound for these questions: given a term t, how many reduction steps need to be carried out to reach normal form? how big is the normal form? The results we give were originally proven by Statman.

Later, we will also give lower bounds, showing that any algorithm that gives any information about the normal form of a term t, has a run-time comparable to that needed to reduce terms to normal form.

4.1 Complete Developments

Our analysis starts with the following notion of the complete development D(t) of a term t. Intuitively, D(t) is given by reducing every redex in t, but not any of the new redexes created by reducing the original redexes. Formally,

D(x) = x D(λx:A. s) = λx:A. D(s) D((λx:A. s) r) = D(s)[D(r)/x]

and

D(x r) = x D(r) D(t s r) = D(t s) D(r).

The last two clauses are equivalent to D(t r) = D(t) D(r) for t not a λabstraction.

Exercise 4.1 Show that if r ⟶_{β1} s, then s ⟶_{β} D(r) ⟶_{β} D(s). Show that if r ⟶_{β n} s, then s ⟶_{β} D^{n}(s), and (a) give another proof of the Church-Rosser property, and (b) show that if r is weakly normalising, then D^{N}(r) is normal for some r.

4.2 Redex Depths

Given Γ ⊢ s : B, and a sub-term occurrence t in the term s, there is a corresponding sequent in the form Γ Δ ⊢ t : C in the (unique) derivation of Γ ⊢ s : B. We call C the type of t (or t : C).

Exercise 4.2 Formalise the notion of the sequent corresponding to an occurrence above. Show that for an α-class s, and an occurrence in s_{0} ∈ s, that the type of the occurrence does not depend on s_{0}.

Given a redex (λx:A. s) r in t, its redex type is the type of λx:A. s.^{4}

If t ⟶_{β1} t′ by reducing a redex (λx:A. s) r with redex type A → B, then the redexes in t′ can be classified as follows:

1. Those redexes in t′ which correspond to redexes in t.

2. If s is in the form λx′:A′. s′, and the redex occurs in a position in the form (λx:A. s) r r′ in t, then s[r/x] r′ = (λx′:A′. s′[r/x]) r′ is a redex in t′, with redex type B.

3. If r is in the form λz:C. r′, and there is an occurrence x s′ in s, then there is the redex r s′[r/x] = (λz:C. r′)(s′[r/x]) in t′, with redex type A.

The redex types in t′ are thus among: 1. the redex types of redexes in t,

2. the type A, and 3. the type B. Considering instead D(t), we don’t get any redexes of class 1., and so:

Lemma 4.3 Let Γ ⊢ t : C. The redex type of a redex in D(t) is either A or B, where A → B is a redex type in t.

Proof: A redex in s[r/x] has redex type which is either a redex type in s or r, or type A, where r : A.

We use induction over the clauses defining D. Only two of the clauses can create new redexes. For, D((λx:A. s) r) = D(s)[D(r)/x], the redexes of D((λx:A. s) r) are either from D(s), from D(r), or are created by the substitution and have redex type A. For D(t s r) = D(t s) D(r), each redex of D(t s r) is either from D(t s), D(r), or is D(t s) D(r). In the latter case, D(t s) must be λ-abstraction of some type B, so that t s must be a redex with redex type A → B. □

4This is confusing, because the redex is a sub-term, and the type of the sub-term is not the redex type.

The depth d(A) of a type A is defined by d(o) = 0 and d(A → B) = 1 + max(d(A), d(B)). The redex depth of a term t is the maximum of the depths of the redex types in t. The lemma above gives us another proof that every term is weakly normalising:

Corollary 4.4 Let Γ ⊢ s : B, and s have redex depth N. Then D^{N}(s) is normal.

4.3 Calculating Upper Bounds

Recall that the length l(s) of a term is defined by l(x) = 1, l(λx:A. s) = 1 + l(s) and l(t r) = 1 + l(t) + l(r). We also define the depth d(s) of a term s by d(x) = 1, d(λx:A. s) = 1 + d(s) and d(t r) = 1 + max(d(t), d(r)).

Lemma 4.5 1. l(s) < 2^{d(s)}. d(s[r/x]) < d(s) + d(r).

2. d(D(s)) ⩽ l(s). l(D(s)) ⩽ 2^{l(s)}.

3. If s has m redexes, then s ⟶_{β} D(s) by a sequence of m one-step reductions.

4. s has at most \frac{l(s)}{2} redexes.

□

Exercise 4.6 Prove the above.

We define the repeated exponential 2_{N}^{m} by 2_{0}^{m} = m and 2_{N+1}^{m} = 2^{2_{N}^{m}}.

Theorem 4.7 For Γ ⊢ s : B, let N be the redex depth of s. Then the normal form of s has length ⩽ 2_{N}^{l(s)}, and s (if not normal) has a reduction sequence to normal form with length ⩽ 2_{N−1}^{l(s)}.

Proof: By corollary 4.4, D^{N}(s) is the normal form of s. That l(D^{N}(s)) ⩽ 2_{N}^{l(s)} follows by induction using 2. of the previous lemma.

We show that s ⟶_{β} D^{N}(s) in ⩽ 2_{N−1}^{l(s)} steps by induction on N. For N = 1, we have that s ⟶_{β} D(s) in ⩽ \frac{1}{2}l(s) ⩽ 2_{0}^{l(s)} steps. By 3. and 4. of the lemma above, we have that D^{N}(s) ⟶_{β} D^{N+1}(s) in ⩽ \frac{1}{2}l(D^{N}(s)) ⩽ \frac{1}{2}2_{N}^{l(s)} steps. Then s ⟶_{β} D^{N}(s) ⟶_{β} D^{N+1}(s) in at most

2_{N−1}^{l(s)} + \frac{1}{2}2_{N}^{l(s)} = \frac{1}{2}(2 · 2_{N−1}^{l(s)} + 2_{N}^{l(s)}) ⩽ \frac{1}{2}(2^{2_{N−1}^{l(s)}} + 2_{N}^{l(s)}) = 2_{N}^{l(s)}

steps. □

Exercise 4.8 The rank of a type is defined by rank(o) = 0, rank(A → B) = max(rank(A) + 1, rank(B)). The redex rank of a term is the maximum of the ranks of the redex types in the term. Modify the definition of D so that

D((λx_{1} … λx_{m}. s) r_{1} … r_{m}) = D(s)[D(r_{1})/x_{1} … D(r_{m})/x_{m}].

Refine theorem 4.7 to have N the redex rank instead of the redex depth.

5 Quantitative Strong Normalisation

In the previous section we gave an upper bound for the lengths of reduction sequences for a particular reduction strategy. In this section we will give an upper bound for sn(t), the maximum lengths of any reduction sequences starting from t.

We will do this by a method of translations. Give a term t, we will find a new term |t| : o, such that sn(t) is in some sense computed by |t|. Combined with the results of the previous section, this will give an upper bound for sn(t). In order to make the translation work, given a term f of function type A→B, we need not only |f|, but also a term mapping |r| to |f r|. We do this by first defining functionals ⦇r⦈ : A whenever r : A, and operators ℵ_{A} : A → o mapping ⦇r⦈ to |r|. The technique is due to Loader, who used it to prove normalisation results. Gandy earlier gave a somewhat similar translation, in order to derive strong normalisation from weak.

5.1 Translations

We introduce quite a lot of notation. Take two arbitrary variables Σ and C. Let Ξ be the context Σ:o→o→o C:o. We define operators Σ_{A}(·, ·) and ℵ_{A}(·), and terms C_{A} by recursion on types A:

Σ_{o}(i, a) = Σ i a, ℵ_{o}(i) = i, C_{o} = C

and, taking x to avoid variable capture,

Σ_{A→B}(i, f) = λx:A. Σ_{B}(i, f x),
ℵ_{A→B}(f) = ℵ_{B}(f C_{A}).
C_{A→B} = λx:A. Σ_{B}(ℵ_{A}x, C_{B}),

We have the following derived rules:

\frac{Γ ⊢ i : o Γ ⊢ r : A}{ΞΓ ⊢ Σ_{A}(i, r) : A}  \frac{Γ ⊢ r : A}{ΞΓ ⊢ ℵ_{A}(r) : A}  \overline{ΞΓ ⊢ C_{A} : A}.

Given a term r, we define ⦇r⦈ by recursion on r:

⦇x⦈ = x ⦇t r⦈ = ⦇t⦈⦇r⦈ ⦇λx:A. s⦈ = λx:A. Σ_{A}(ℵ_{A}x, ⦇s⦈).

This gives us the following derived rule:

\frac{Γ ⊢ s : B}{ΞΓ ⊢ ⦇s⦈ : B.}

We also have that ⦇·⦈ commutes with substitution:

⦇s[r/x]⦈ = ⦇s⦈[⦇r⦈/x].

Given x_{1}:A_{1} … x_{m}:A_{m} ⊢ s : B, we define

|s| = ℵ_{B}⦇s⦈[C_{A_{1}}/x_{1} … C_{A_{m}}/x_{m}],

so that Ξ ⊢ |s| : o.

5.2 Reduction of Translations

The relationship between the term |s| and the length of reduction sequences starting from s is derived by examining the behaviour of |s| under reduction.

Lemma 5.1

ℵ_{A}(Σ_{A}(i, r)) ⟶_{β} Σ i (ℵ_{A}r)  (4)
|λx:A. s| ⟶_{β} Σ (ℵ_{A}C_{A}) |s|  (5)

If Γ ⊢ x : A_{1} → ⋯ → A_{m} → B and Γ ⊢ r_{i} : A_{i} (1 ⩽ i ⩽ m), then

|x r_{1} … r_{m}| ⟶_{β} Σ |r_{1}| (… (Σ |r_{m}| (ℵ_{B}C_{B})) …).  (6)
|(λx:A. s) r_{0} … r_{m}| ⟶_{β} Σ |r_{0}| |s[r_{0}/x] r_{1} … r_{m}|  (7)

Proof: For (4), we use induction on types. The base case holds by definition, and the induction step is

ℵ_{A→B}(Σ_{A→B}(i, f)) = ℵ_{B}(Σ_{A→B}(i, f) C_{A})
⟶_{β} ℵ_{B}(Σ_{B}(i, f C_{A}))
⟶_{β} Σ i (ℵ_{B}(f C_{A})) = Σ i (ℵ_{A→B}f)

We denote the substitution in the definition of |·| by σ, so that |s| = ℵ_{B}⦇s⦈[σ]. For (5), we have

| λx:A. s| = ℵ_{A→B}(λx:A. Σ_{B}(ℵ_{A}x, ⦇s⦈)[σ])
⟶_{β} ℵ_{B}(Σ_{B}(ℵ_{A}C_{A}, ⦇s⦈[σ, C_{A}/x]))
⟶_{β} Σ (ℵ_{A}C_{A}) (ℵ_{B}⦇s⦈[σ, C_{A}/x])
= Σ (ℵ_{A}C_{A}) |s|.

For (6),

|x r_{1} … r_{m}| = ℵ_{B}(C_{A} ⦇r_{1}⦈[σ] … ⦇r_{m}⦈[σ])
⟶_{β} ℵ_{B}(Σ_{B}(ℵ_{A_{1}}⦇r_{1}⦈[σ], …, Σ_{B}(ℵ_{A_{m}}⦇r_{m}⦈[σ], C_{B}) …))
= ℵ_{B}(Σ_{B}(|r_{1}|, …, Σ_{B}(|r_{m}|, C_{B}) …))
⟶_{β} Σ |r_{1}| (… (Σ |r_{m}| (ℵ_{B}C_{B})) …),

where the last step uses (4) m times. For (7),

⦇(λx:A. s) r_{0} … r_{m}⦈ = (λx:A. Σ_{C}(ℵ_{A}x, ⦇s⦈)) ⦇r_{0}⦈ … ⦇r_{m}⦈
⟶_{β} Σ_{C}(ℵ_{A}⦇r_{0}⦈, ⦇s⦈[⦇r_{0}⦈/x]) ⦇r_{1}⦈ … ⦇r_{m}⦈
⟶_{β} Σ_{B}(ℵ_{A}⦇r_{0}⦈, ⦇s⦈[⦇r_{0}⦈/x] ⦇r_{1}⦈ … ⦇r_{m}⦈)
= Σ_{B}(ℵ_{A}⦇r_{0}⦈, ⦇s[r_{0}/x] r_{1} … r_{m}⦈).

where C = A_{1} → ⋯ → A_{m} → B. Applying the definition |t| = ℵ_{B}⦇t⦈[σ] to the above gives:

|(λx:A. s) r_{0} … r_{m}| ⟶_{β} ℵ_{B}(Σ_{B}(ℵ_{A}⦇r_{0}⦈, ⦇s[r_{0}/x] r_{1} … r_{m}⦈))[σ]
⟶_{β} Σ (ℵ_{A}⦇r_{0}⦈[σ]) (ℵ_{B}⦇s[r_{0}/x] r_{1} … r_{m}⦈[σ])
= Σ |r_{0}| |s[r_{0}/x] r_{1} … r_{m}|

as required. □

For us, the crucial facts in the lemma above are that |x r_{1} … r_{m}| is expressed in terms of the |r_{i}|, |λx:A. s| in terms of |s|, and |(λx:A. s) r_{0} … r_{m}| in terms of |r_{0}| and |s[r_{0}/x] r_{1} … r_{m}|. This will enable us to relate the size of the normal form of |t| to the number of instances of the clauses of (1) needed to derive t, and hence, by exercise 3.3, to sn(t).

5.3 Translations and Strong Normalisation

Lemma 5.2 Let μ be a natural number valued function defined on those terms n with Ξ ⊢ n : o, such that (a) if n ⟶_{β} n′, then μ(n) = μ(n′), and (b) μ(Σ n m) ⩾ μ(n) + μ(m) + 1. Then sn(t) ⩽ μ|t| for any term t.

Proof: By exercise 3.3, it suffices to show that

μ|x r_{1} … r_{m}| ⩾ μ|r_{1}| + ⋯ + μ|r_{m}| + 1, μ|λx:A. s| ⩾ μ|s| + 1

and

μ|(λx:A. s) r_{0} … r_{m}| ⩾ μ|s[r_{0}/x] r_{1} … r_{m}| + |r_{0}| + 1.

These three inequalities follow immediately from the two properties of μ and 2., 3. and 4. of lemma 5.1. □

One such μ is given by letting μ(s) be the length of the normal form of s. Combined with theorem 4.7, this gives:

Theorem 5.3 There is a constant K such that, for any term s of λ^{→}, we have sn(s) ⩽ 2_{N}^{K·M·l(s)}, where M (resp. N) is the maximum of the lengths (resp. depths) of the types of the sub-terms of s.

Proof: By inspection, there is K such that l|s| ⩽ K · M · l(s). The redexes in |s| are sub-types of the types of sub-terms of s. Letting μ be the length of normal form function, and applying theorem 4.7 and lemma 5.2 gives the result. □

Exercise 5.4 Let M be the model of λ^{→} with M(o) the set of natural numbers. By choosing a suitable valuation on Ξ, reprove strong normalisation using lemma 5.2. This is a quite general technique for proving strong normalisation.

Exercise 5.5 Refine theorem 5.3 to give sn(s) ⩽ 2_{N}^{K·l(s)}, where N is redex rank of s. Hints:

The translation we used is more sophisticated than is required if we just wish to bound sn(t). Show that taking the C_{A} to be variables, and ℵ_{A}(r) = C_{A→o} r, in fact gives sn(t) bounded by the length the normal form of ⦇t⦈.

Additionally, modify Σ_{A→B} so that Σ_{A→B}(i, λx:A. s) = λx:A. Σ_{B}(i, s) (the original definition β-reduces to this). Also, check that, if the t for which we wish to bound sn(t) has no sub-term of type A, then we can take Σ_{A→B}(i, f) = f.

These modifications ensure that the types of redexes in ⦇t⦈ are exactly the types of redexes in t, so that the N of our bound can be taken to be the redex rank of t (using exercise 4.8).

To show that the exponent K·M·l(t) can be reduced to K·l(t), carefully βexpand (i.e., replace s[r/x] with (λx:A. s)r) the term ⦇t⦈. Doing this correctly should give a term with length bounded by K ·l(t), and hence give the bound sn(t) ⩽ 2_{N}^{K·l(t)}.

6 Lower Bounds for Normalisation

The upper bounds given in the previous two sections are not too bad, as the following example shows.

For any number n and type A, let n_{A} be the term λf:A→A. λx:A. f^{n}x of type (A→A) → A → A. It is easy to show that

m_{A→A} n_{A} = (n^{m})_{A}.

Let the term big_{A,n} be given by

big_{A,0} = 0_{A} big_{A,n+1} = big_{A→A,n} 2_{A}.

By induction on n, we see that

big_{A,n} ⟶_{β} (2_{n}^{0})_{A}.

The term big_{o,n} has redex rank n + 2, and length O(n), so that the upper bound on the size of the normal form of big_{o,n} given in section 4 is 2_{n+2}^{O(n)}.

A program P that takes a natural number as input is called elementary recursive (or Kalmar elementary) if there is K such that P (n) has run time ⩽ 2_{K}^{n}. A function f is called elementary recursive if it is computed by an elementary recursive program.^{5}

The example above shows that the function computing the normal form of a typed λ-term is not elementary recursive, while the bounds of the previous sections show that that function is only just outside of elementary recursive.

In the following, we shall prove stronger versions of this, showing that any function giving non-trivial information about normal forms of terms is not elementary recursive.

For example, consider the closed terms of type o → o → o. There are just two closed normal forms of this type: λx:o. λy:o. x and λx:o. λy:o. y. Computing the normal form of a closed term of type o→o→o by β-reduction is not elementary recursive. However, this leaves the possibility that there is an elementary recursive program computing the same function by a different method. We shall show that this is not the case:

Theorem 6.1 There is no elementary recursive program computing the normal form of closed terms of type o → o → o. □

This extends easily to give the no-non-trivial-information result:

5Note that due to the size of the functions 2_{K}^{n}, it is pretty much irrelevant what formalism is used to express programs and their run-times—even an exponential difference in efficiency between two formalisms can be accommodated by changing K.

Corollary 6.2 Suppose that Γ ⊢ s_{1} : A, Γ ⊢ s_{2} : A. Let P be a program such that if Γ ⊢ t : A and t ⟶_{β} s_{i} then P (t) returns i (i = 1, 2). Then P is not elementary recursive.

Proof (of corollary from theorem): Let P be a such a program. We find a program Q computing the normal of closed terms of type o → o → o as follows.

Define the type substitution B[A/o] by o[A/o] = A, (C → D)[A/o] = C[A/o] → D[A/o]. Extend to terms by performing the type substitution in each λ-abstraction. Define Q(t) to return λx:o. λy:o. x if P (t[A/o] s_{1} s_{2}) returns 1, and to return λx:o. λy:o. y if P (t[A/o] s_{1} s_{2}) returns 2.

Clearly, if ⊢ t : o → o → o, then Q(t) computes the normal form of t, and by theorem 6.1, cannot be elementary recursive. The run-time of Q is only a little more than that of P: if P were elementary recursive then so would be Q. Hence P is not elementary recursive. □

6.1 Higher Order Propositional Classical Logic

Our proof of theorem 6.1 will proceed via encoding higher order propositional classical logic (PCL_{ω}). The language of PCL_{ω} is as follows. There are countably many variables V^{N} = {x_{0}^{N}, x_{1}^{N}, … } of each rank N. Atomic formulas are x_{i}^{0} and x_{i}^{N} ∈ x_{j}^{N+1}, connectives are ∨ (binary) and ¬ (unary), and we have quantifiers ∃^{N} of each rank. As a grammar:

F := V^{0} | V^{N} ∈ V^{N+1} | F ∨ F | ¬F | ∃^{N}V^{N}. F.

The logic is classical, so we define other connectives in the usual way, e.g.,

ϕ ∧ ψ := ¬(¬ϕ ∨ ¬ψ) ϕ ⇒ ψ := ¬ϕ ∨ ψ
ϕ ⇔ ψ := (ϕ ⇒ ψ) ∧ (ψ ⇒ ϕ) ϕ ⊕ ψ := (ϕ ∧ ¬ψ) ∨ (¬ϕ ∧ ψ)
x^{N} ∉ y^{N+1} := ¬(x^{N} ∈ y^{N+1}) ∀^{N}x^{N}. ϕ := ¬∃^{N}x^{N}. ¬ϕ.

The semantics of PCL_{ω} is given as follows. Let B = {tt, ff}, and let B_{0} = B, B_{N+1} = B^{B_{N}}. Then the rank N variables x^{N} are interpreted as ranging over B_{N}. The atomic formula x^{N} ∈ y^{N+1} is interpreted as y^{N+1}(x^{N}). The connectives are interpreted in the usual way.

Exercise 6.3 Formalise the semantics. Define a valuation to be a function σ mapping each variable x^{N} into B_{N}, and then define ⟦ϕ⟧_{σ} ∈ B for formulae ϕ and valuations σ.

Show that ⟦ϕ⟧_{σ} depends only on ϕ and σx for variables x free in ϕ. In particular, ⟦ϕ⟧ is well-defined for closed ϕ.

The set B_{N} has 2_{N}^{2} elements, so that the obvious algorithm computing ⟦ϕ⟧ is non-elementary. In the appendix, we prove that:

Theorem 6.4 The function ⟦ϕ⟧ for closed ϕ is not elementary. □

We shall encode B_{N} in λ^{→}, in such a way that we can give a reasonably short encoding of iteration over the members of the B_{N}, and then of the semantics ⟦ϕ⟧. The function giving the encoding will be elementary recursive, which together with theorem 6.4 will give a proof of theorem 6.1.

6.2 Encoding the Boolean Hierarchy

We encode the elements of the sets B_{N} as closed terms of λ^{→}. The encoding is 1-many rather than 1-1. We define the types B = o → o → o, and B_{0} = B, B_{N+1} = B_{N} → B. The intention is that elements of the set B_{N} will be represented as closed type B_{N} terms. We define the relations ∼_{N} as follows.

For a ∈ B and Γ ⊢ r : B, put a ∼ r iff either a = tt and r ⟶_{β} λx:o. λy:o. x or a = ff and r ⟶_{β} λx:o. λy:o. y.

Let ∼_{0} be ∼, and put b ∼_{N+1} s iff b(a) ∼ s r whenever a ∼_{N} r. We let tt, ff : B be given by tt = λx:o. λy:o. x and ff = λx:o. λy:o. y, so that tt ∼ tt and ff ∼ ff.

Exercise 6.5 Show that for each a ∈ B_{N} there is r such that a ∼_{N} r.

If ⊢ R : (B_{N}→A→A) → A → A, then R is called an N-A-iterator if it β-reduces to

λf:B_{N}→A→A. λx:A. f r_{0} (… (f r_{2_{N}^{2} −1} x) …)  (8)

where the r_{i} enumerate B_{N}, in the sense that for each a ∈ B_{N}, there is exactly one i such that a ∼_{N} r_{i}.

We define r ∨ r′ = λx:o. λy:o. r x (r′ xy), ¬r = λx:o. λy:o. r yx for terms r, r′ : o, so that r ∨ r′, ¬r : o also. Other Boolean connectives are defined in terms of these.

Given a N-B-iterator R, we define ∃^{N}, ∀^{N} : B_{N+1} → B by

∃^{N} = λf:B_{N+1}. R (λx:B_{N}. λy:B. y ∨ fx) ff

and

∀^{N} = λf:B_{N+1}. R (λx:B_{N}. λy:B. y ∧ fx) tt.

We will write ∃_{R}^{N} and ∀_{R}^{N} when it is desired to emphasise the iterator.

Exercise 6.6 Let R be a N-B-iterator. Show that if a ∼_{N+1} r then tt ∼ ∃_{R}^{N}r iff a(b) = tt for some b ∈ B_{N}, and ff ∼ ∃_{R}^{N}r iff a(b) = ff for all b ∈ B_{N}. Show the corresponding property of ∀_{R}^{N}.

An N-equality test is a term ⊢ eq : B_{N} → B_{N} → B such that if a ∼ r, a′ ∼ r′, then tt ∼ eq r r′ if a = a′, and ff ∼ eq r r′ if a ≠ a′. Given an N-B-iterator R, we define an N+1-equality test

eq_{R} = λx, y:B_{N+1}. ∀_{R}^{N} (λz:B_{N}. xz ⇔ yz).

Exercise 6.7 Prove that eq_{R} really is an equality test.

Lemma 6.8 Given a type A, let I_{N,A} be the type (B_{N} → A → A) → A → A of N-A-iterators. Suppose that R is an N-I_{N+1,A}-iterator, and that eq is an N-equality test. Then there is an N+1-A-iterator.

In fact, the existence of N-A-iterators is trivial, since for each a ∈ B_{n} there is r such that a ∼ r. However, the obvious construction gives iterators of size O(2_{N}^{2}) or worse. The construction we give in the proof below will lead to an N-A-iterator that is much smaller.

Observe that a N-I_{A}-iterator iterates over a |B_{N}| element set, while a N+1-A-iterator iterates over a |B_{N+1}| = 2^{|B_{N} |} element set. The idea of the proof below is to define an operator Φ such that if G iterates over a m element set, then Φ G iterates over a 2m element set. Then, applying R to Φ, we obtain a term that iterates over a 2^{|B_{N} |} = |B_{N+1}| element set.

Proof: Given a set S ⊂ B_{N+1}, an S-A-iterator is a term of type I_{N+1,A} which β-reduces to

λf:B_{N}→A→A. λx:A. f s_{0} (… (f s_{m−1} x) …)

where s_{0} … s_{m−1} enumerates S.

Given a set T ⊂ B_{N}, a T -based A-iterator is a S-A-iterator where

S = {b ∈ B_{N+1} | b(x) ≠ ff implies x ∈ T }.

A B_{N}-based A-iterator is just an N+1-A-iterator. We will find a term

Φ : B_{N} → I_{N+1,A} → I_{N+1,A}

such that if a ∼_{N} r, and F is a T -based A-iterator, with a ∉ T, then Φ r F is a T ∪{a}-based A-iterator.

Given b ∈ B_{N+1} and a ∈ B_{N}, define b ∪ {a} ∈ B_{N+1} by (b ∪ {a})(a) = tt, and (b ∪ {a})(x) = b(x) for x ≠ a. Given terms s : B_{N+1} and r : B_{N}, define s ∪ {r} = λx:B_{N}.(s x) ∨ (eq r x). If b ∼_{N+1} s and a ∼_{N} r, then b ∪ {a} ∼_{N+1} s ∪ {r}.

If G is an S-A-iterator, for some S ⊂ B_{N+1}, β-reducing to

λf:B_{N+1}→A→A. λx:A. f s_{0} (…(f s_{m−1} x) …),  (9)

then

λf:B_{N+1}→A→A. λx:A. G (λy:B_{N+1}. f (y ∪ {r})) x  (10)

reduces to

λf:B_{N+1}→A→A. λx:A. f s′_{0} (…(f s′_{m−1} x) …),  (11)

where s′_{i} = s_{i} ∪ {r}. If a ∼ r, then the s′_{i} enumerate S′ = {b ∪ {a} | b ∈ S}, so that the term (10) is a S′-A-iterator.

Let Φ be

λz:B_{N}. λG:I_{N+1,A}.
λf:B_{N+1}→A→A. λx:A. G (λy:B_{N+1}. f (y ∪ {z}))(Gfx).

Given a ∼_{N} r and an S-A-iterator G with normal form (9), combining (9) and (11) gives that the term Φ r G reduces to

λf:B_{N+1}→A→A. λx:A. f s′_{0} (…(f s′_{m−1} (f s_{0} (… (f s_{m−1} x) …))) …).

This shows that Φ r G is an S′′-A-iterator where S′′ = S ∪ S′. In particular, if G is a T -based A-iterator, then Φ r G is a T ∪{a}-A-iterator, which is the property we desire from Φ.

A ∅-based A-iterator is given by

G_{0} = λf:B_{N+1}→A→A. λx:A. f (λz:B_{N}. ff) x.

Let R′ be the term R Φ G_{0}. As R is an N-I_{A}-iterator, R′ reduces to

Φ r_{0} (… (Φ r_{m−1} G_{0}) …)

where (for some a_{i}) a_{i} ∼ r_{i} and the a_{i} enumerate B_{N}. As B_{N} = ∅ ∪ {a_{m−1}} ∪ ⋯ ∪ {a_{0}}, we have that R′ is a B_{N}-based A-iterator, or in other words, a N+1-A-iterator. □

Lemma 6.9 There is an elementary recursive function that given N and A, gives a N-A-iterator.

Proof: A 0-A-iterator is given explicitly by

λf:B→A→A. λx:A. ftt (fffx)

and λx:B. λy:B. x ⇔ y is a 0-equality test. Induction with lemma 6.8 gives N-A-iterators and N-equality tests for all N. We leave it to the reader to check that these constructions are actually elementary recursive. □

6.3 Encoding the Logic

We encode formulae of PCL_{ω} as λ^{→} terms, in such a way that the terms compute the semantics. Specifically, if ϕ is a closed formula, then ⟦ϕ⟧ ∼ ⌜ϕ⌝. The encoding is elementary recursive, so this will derive theorem 6.1 from theorem 6.4. For each N, let R_{N} be a N-B-iterator as given by 6.9. The iterators R_{N} give us ∃^{N} : B_{N+1} → B.

Given a formula ϕ with free variables among x_{1}^{N_{1}} … x_{m}^{N_{m}}, we define ⌜ϕ⌝ such that

x_{1}:B_{N_{1}} … x_{m}:B_{N_{m}} ⊢ ⌜ϕ⌝ : B

by induction on ϕ. We let ⌜x⌝ = x for any variable x. Let

⌜x_{i}^{0}⌝ = x_{i} ⌜x_{i}^{N_{i}} ∈ x_{j}^{N+1}⌝ = x_{j}(x_{i})
⌜ϕ ∨ ψ⌝ = ⌜ϕ⌝ ∨ ⌜ψ⌝ ⌜¬ϕ⌝ = ¬⌜ϕ⌝

and

⌜∃^{N}x^{N}. ϕ⌝ = ∃^{N} (λx:B_{N}.⌜ϕ⌝).

Theorem 6.1 follows by checking that ⌜ϕ⌝ is well-behaved:

Exercise 6.10 Suppose that σ is a valuation, such that σ(x_{i}^{N_{i}}) ∼_{N_{i}} r_{i} (1 ⩽ i ⩽ m). Show that ⟦ϕ⟧_{σ} ∼ ⌜ϕ⌝[r_{1}/x_{1} … r_{m}/x_{m}].

Check that ⌜ϕ⌝ is an elementary recursive function of ϕ.

A PCL_{ω} is not Elementary Recursive

We outline a proof of theorem 6.4 that the valuation ⟦·⟧ for the logic PCL_{ω} is not elementary recursive. We do this by encoding Turing machines with a bound 2_{N}^{2} on their behaviour.

A.1 Basic Encoding in PCL_{ω}

We first examine the structure of the sets B_{N}. B_{N} has 2_{N}^{2} elements. We define a bijection seq_{N} : {0 … 2_{N}^{2}−1} ⟶ B_{N} in a natural way. Let seq(0) = ff and seq(1) = tt and seq_{0} = seq. Given seq_{N}, and a number ∑_{i<2_{N}^{2}} d_{i} · 2^{i} in binary notation, put

seq_{N+1}( ∑_{i<2_{N}^{2}} d_{i} · 2^{i})(seq_{N}(m)) = seq(d_{m}).

This considers elements of B_{N+1} as numbers in binary notation, the order of the digits given by the numbering of B_{N}.

We represent a series of predicates as PCL_{ω} formulae.

Take two binary numbers d = ∑_{i<n} d_{i} · 2^{i} and e = ∑_{i<n} e_{i} · 2^{i}. We have that d < e iff and only if there is a ‘pivot’ i_{0} < n such that d_{i_{0}} < e_{i_{0}} and d_{i} = e_{i} for i_{0} < i < n. This inspires the following definition of the formulae a <_{N} a′: let a <_{0} a′ be ¬a ∧ a′, and let b <_{N+1} b′ be

∃^{N}x.(¬x ∈ b ∧ x ∈ b′ ∧ ∀^{N}x′.(x <_{N} x′ ⇒ (x′ ∈ b ⇔ x′ ∈ b′))).

Defining S_{N}(a, a′) := ∀^{N}x.((a <_{N} x) ⊕ (x <_{N} a′)) encodes the successor relation a′ = a + 1.

We turn to encoding combinatorial notions. Equality a =_{0} b, at rank 0, is given by a⇔b. Equality a =_{N+1} b at rank N +1, is given by ∀^{N}x. x∈a⇔x∈b.

We encode finite sets as follows. For a_{1} … a_{m} rank N and b rank N + 1, the relation b = {a_{1} … a_{m}} is given by

∀^{N}x.(x ∈ b ⇔ (x =_{N} a_{1} ∨ ⋯ ∨ x =_{N} a_{m})).

The Kuratowski pairing, c = (a, b) (where (a, b) = {{a}, {a, b}}) is given by

∃^{N+1}x. ∃^{N+1}y.(c = {x, y} ∧ x = {a} ∧ y = {a, b}).

where c has rank N + 2 and a, b have rank N.

We can give an efficient encoding of a =_{N} n, where n is some given number, and a has rank N. a =_{0} 0 is ¬a, a =_{0} 1 is a, and a =_{0} n is a ∧ ¬a for n ⩾ 2. Given n = 2^{n_{1}} + ⋯ + 2^{n_{m}} with the n_{i} distinct, b =_{N+1} n is

∃^{N}x_{1} … ∃^{N}x_{m}.(b = {x_{1} … x_{m}} ∧ x_{1} = n_{1} ∧ ⋯ ∧ x_{m} = n_{m}).

Finally, b = ⟨a_{0} … a_{m−1}⟩, encoding small tuples, is

∃^{N+2}p_{0} … ∃^{N+2}p_{m−1}. ∃^{N}j_{0} … ∃^{N}j_{m−1}.

b = {p_{0} … p_{m−1}} ∧ ⋀_{i<m} p_{i} = (a_{i}, j_{i}) ∧ ⋀_{i<m} j_{i} = i

for b rank N + 3 and the a_{i} rank N.

Exercise A.1 Check that all the encodings above do what they should.

Exercise A.2 For a, b, c rank N +1, encode the predicates 0∈a, 2_{N+1}^{2}−1∈a, and c = a + b.

A.2 Encoding Turing Machines

We apply the constructs above to encode Turing machines. Since we are encoding in the decidable logic PCL_{ω}, we cannot encode arbitrary runs of Turing machines; instead we must consider a tape of some finite length, fixed for that run.

We will use a slight variant on the usual notion of Turing machine. Instead of the head being at a particular cell on the tape at each step, the head is between two adjacent cells. The action taken depends on the state, and the two cells adjacent to the head; the actions taken are to move either left or right, updating the cell traversed.

Tapes are encoded as two tape halves—one to the left of the head and one to the right of the head. In both directions, positions are numbered by some B_{N}, starting with 0 adjacent to the head, so that the two tape halves l and r are encoded by elements of B_{N+1}. The state s is encoded by another element of B_{N+1}, and the overall configuration of the machine by ⟨l, s, r⟩.

We encode various operations. We give four adjacency predicates of a pair of tape halves:

adj_{tt,tt}(l, r) := 0 ∈ l ∧ 0 ∈ r adj_{tt,ff}(l, r) := 0 ∈ l ∧ 0 ∉ r
adj_{ff,tt}(l, r) := 0 ∉ l ∧ 0 ∈ r adj_{ff,ff}(l, r) := 0 ∉ l ∧ 0 ∉ r

These check for the four possibilities of the cells next to the tape head. The shift predicate sh(t, t′) is given by

(∀^{N}i. ∀^{N}j. S(i, j) ⇒ (i ∈ t ⇔ j ∈ t′)) ∧ 2_{N}^{2}−1 ∉ t.

This shifts a tape half along by one, and checks that at the far end, a ff comes into, or goes out of, view.

There are four possible actions on tapes: moving either left or right, while updating the cell traversed with either tt or ff. These are given by the action predicates below:

act_{L,tt}(l, r, l′, r′) := sh(l′, l) ∧ 0 ∈ r′ ∧ sh(r, r′)
act_{R,tt}(l, r, l′, r′) := sh(l, l′) ∧ 0 ∈ l′ ∧ sh(r′, r)
act_{L,ff}(l, r, l′, r′) := sh(l′, l) ∧ 0 ∉ r′ ∧ sh(r, r′)
act_{R,ff}(l, r, l′, r′) := sh(l, l′) ∧ 0 ∉ l′ ∧ sh(r′, r)

A rule of the Turing machine is given by a sextuple (n, α, β, d, γ, n′), which is read as follows: if we are in a configuration with state n and with α and β to the left and right, respectively, of the head, then move in direction d ∈ {L, R}, update the cell traversed with γ, and transfer to state n′. The update predicate tr_{n,α,β,d,γ,n′}(l, r, s, l′, r′, s′) saying that this rule updates the configuration (l, s, r) to give (l′, s′, r′) is

s = n ∧ adj_{α,β}(l, r) ∧ act_{d,γ}(l, r, l′, r′) ∧ s′ = n′.

Encoding tuples, we also let tr_{n,α,β,d,γ,n′}(c, c′) be

∃^{N+1}lsrl′ s′ r′. c = ⟨l, s, r⟩ ∧ c′ = ⟨l′, r′, s′⟩ ∧ tr_{n,α,β,d,γ,n′}(l, s, r, l′, s′, r′).

Taking the disjunction over a set of rules R = {ρ_{1} … ρ_{M}} gives us the transition relation trans_{R}(c, c′):

tr_{ρ_{1}}(c, c′) ∨ … ∨ tr_{ρ_{M}} (c, c′).

The initial configuration of the machine is defined to have ff everywhere on the tape, and state 0. Hence init(c) is

∃^{N+1}x. x = 0 ∧ c = ⟨x, x, x⟩.

A set S of states is run closed if it contains the initial configuration, and whenever c ∈ S, there is c′ ∈ S updated from c by some rule. This is given by closed_{R}(S):

∃^{N+4}c.(init(c) ∧ c ∈ S) ∧ ∀^{N+4}c.(c ∈ S ⇒ ∃^{N+4}c′.(c′ ∈ S ∧ trans(c, c′))).

There can fail to be a run closed set for a Turing machine under the following two conditions: either the machine reaches a state to which no rule applies, or the machine reaches a state with a tt on the tape 2_{N}^{2} + 1 cells away from the head. In these cases we say the machine 2_{N}^{2}-terminates, and otherwise the machine loops.

Lemma A.3 There is an algorithm that given a Turing machine and N, decides whether or not the machine 2_{N}^{2}-terminates.

Proof: There are only finitely many configurations with no tt more than 2_{N}^{2} cells from the head. These can be enumerated, and the machine run until either it terminates, or the machine loops. □

Exercise A.4 Check that ⟦∃^{N+5}S. closed_{R}(S)⟧ = ff if and only if R 2_{N}^{2}- terminates.

This gives a proof of theorem 6.4: if there were an elementary recursive decision procedure for PCL_{ω}, then the encoding above would contradict the following:

Lemma A.5 There is no elementary recursive algorithm that given a Turing machine and N, decides whether or not the machine 2_{N}^{2}-terminates.

Proof Sketch: Suppose there was such an algorithm.

Let P (ρ, c, N) to be the predicate of a Turing machine ρ terminating from initial configuration c in time 2_{N}^{2}. Then P (ρ, c, N) is elementary recursive. By the usual sort of diagonalisation argument, this is impossible. □