Lectures onType Theory
Chapter 148
Chapter 148Optional

Explicit-Substitution Calculi

Prerequisites. Direct starred prerequisites: none. No later core chapter depends on this route.

In the calculi of this book, substitution is an operation of the metalanguage: β-contraction rewrites (λx.a)b to a[b/x] in one step, and the whole traversal of a happens outside the calculus. An implementation cannot take that step; it must walk the term, and it must be able to stop part-way. Making the traversal into syntax means adding a term former for “a under a pending substitution” and rules that push the pending substitution one layer inward.

Adding those rules costs nothing by itself. The interesting decision is whether to add one more rule, which composes two pending substitutions into one: a[s][t]a[st]. The rule is desirable. It makes substitutions an algebra with an associative composition, it lets a machine merge two environments instead of carrying both, and it is what turns the calculus into the algebraic structure that chapter 54 takes as primitive. It is also what this chapter’s central counterexample destroys: with composition, there is a simply typed term, strongly normalizing under ordinary β-reduction, that admits an infinite reduction (theorem 148.8).

Two calculi are therefore developed separately. The first, λσ, has full composition and is confluent, and the counterexample applies to it. The second, λex, has a composition rule guarded by a side condition together with an explicit rule that deletes a substitution nobody uses, and it preserves strong normalization. No theorem proved for one is transferred to the other.

The λσ-calculus

Definition 148.1 — Syntax

Fix de Bruijn notation: a variable occurrence is a positive integer, and n refers to the variable bound by the n-th enclosing λ. The raw expressions of λσ are two mutually recursive sorts, termsa,b::=1baλaa[s],substitutionss,t::=idasst. A term a[s] is a closure.

The intended readings fix every rule below. The substitution id is {i/i}; the substitution is {i+1/i}, so that 1[]=2 and the index n+1 is written 1[n]; the substitution as is {a/1, s(i)/i+1}; and st is the substitution with a[st]=a[s][t]. Only the index 1 is a primitive term.

Definition 148.2 — The rules

The relation on raw expressions is the compatible closure of Beta(λa)ba[bid]VarId1[id]1VarCons1[as]aApp(ba)[s]b[s]a[s]Abs(λa)[s]λa[1(s)]Closa[s][t]a[st]IdLidssShiftIdidShiftCons(as)sMap(as)ta[t](st)Ass(s1s2)s3s1(s2s3) The ten rules other than Beta are called σ collectively, and σ is the relation they generate.

Beta is the only rule that removes a λ; every other rule removes or relocates a substitution. The rule Abs is not a design choice but a calculation. Pushing s={s(i)/i} under a λ must leave 1 alone and must protect the free indices of each s(i) from the new binder: (λc)[s]=underλλc{1/1, s(i){i+1/i}/i+1}=def. λc{1/1, s(i)[]/i+1}=def. ,λc[1(s)]. This one rule uses every operator except id, which is what makes the four-operator syntax of definition 148.1 the natural choice rather than an arbitrary one.

Example 148.3 — Substitutions intercept indices

In (λ1[2id])[aid] the occurrence of 1 is not bound by the displayed λ. The inner substitution intercepts it and returns 2; crossing the λ renames 2 to 1, which the outer substitution sends to a. Reading binding structure off an explicit-substitution term therefore requires computing, not inspecting.

Proposition 148.4 — σ terminates and is confluent

The relation σ is terminating and confluent, so every raw expression a has a unique σ-normal form, written σ(a).

Proof of Proposition 148.4 — σ terminates and is confluent

Proof. Termination is imported. Merging the two sorts and identifying [] with translates σ into the rewriting system SUBST of categorical combinators, one σ-step to one SUBST-step; termination of SUBST is the theorem of Hardin and Laville cited by the source (section 148.3). Nothing else in this chapter depends on that import.

Given termination, local confluence suffices, and local confluence is checked on critical pairs. For instance the pair 1[id][s]VarId1[s],1[id][s]Clos1[ids]IdL1[s] closes with IdL. The remaining overlaps close in the same way, each by one of IdL, ShiftCons, Map or Ass. ◻

Proposition 148.5 — σ -normal forms are ordinary terms

The σ-normal forms are generated by a,b::=11[n]baλa,s::=idnas. In particular a term in σ-normal form contains no closure except the codings 1[n] of de Bruijn indices, so it is a term of the ordinary λ-calculus.

Proof of Proposition 148.5 — σ -normal forms are ordinary terms

Proof. A normal substitution cannot be a composition: the outermost composition st is a redex for one of IdL, ShiftId, ShiftCons, Map or Ass according to the shape of s, and these five cases exhaust the productions for s. So a normal substitution is id, , or as with a,s normal, and iterating the last clause gives the displayed grammar with n written for the n-fold composition, itself normal only when its associativity has been fixed. A normal term cannot be a closure a[s] unless a=1 and s is a composition of shifts: if a is an application, an abstraction or a closure, one of App, Abs, Clos applies, and if a=1 then VarId or VarCons applies unless s is n. ◻

Theorem 148.6 — Simulation of β

Let a{a1/1,a2/2,}=b be derivable in the metalevel substitution calculus for de Bruijn terms, and suppose there are m and p with am+q=p+q for all q1. Then σ(a[a1a2amp])=b. Consequently one β-step is simulated by one Beta-step followed by σ-reduction to normal form.

Proof of Theorem 148.6 — Simulation of β

Proof. Induction on the derivation, strengthening the claim so that every intermediate expression satisfies the hypothesis on the ai.

Index. For n{a1/1,}=an: if nm then n[a1amp] σ-reduces to an by n1 uses of ShiftCons followed by VarCons; if n>m it reduces to nm+p, and the hypothesis gives an=am+(nm)=nm+p.

Application. Both sides distribute by App and the two induction hypotheses apply to the immediate subterms.

Abstraction. For (λa){a1/1,}=λa, the metalevel rule first lifts each ai, giving ai with σ(ai[])=ai by the induction hypothesis at m=0, p=1. The induction hypothesis for a at m+1 and p+1 gives σ(a[1a1amp+1])=a. On the other side, Abs produces a[1((a1amp))], and repeated Map gives (a1amp)Mapa1[]am[]p+1, whose σ-normal form is a1amp+1. The two sides therefore have the same σ-normal form. ◻

Theorem 148.7 — Confluence of λ σ

The relation generated by Beta together with σ is confluent.

Proof of Theorem 148.7 — Confluence of λ σ

Proof. The proof does not go through the Knuth–Bendix test, which Beta+σ fails, and instead uses Hardin’s interpretation method: if R is terminating and confluent with normal-form map R(), if SR is a relation on R-normal forms contained in (RS), and if S(x,y) implies SR(R(x),R(y)), then confluence of SR gives confluence of RS.

Take R:=σ, which is terminating and confluent by proposition 148.4, and SR:=β on σ-normal forms. Two facts are then needed. First, β is confluent on σ-normal forms: by proposition 148.5 those are ordinary λ-terms, on which β is the ordinary β by theorem 148.6, and ordinary β is confluent; on substitutions in normal form the reductions are independent reductions in the components. Second, a Beta-step projects: if aBetab then σ(a)βσ(b), and likewise for substitutions. That projection is proved by induction on the pair consisting of the length of the longest σ-reduction out of the expression and its size, with cases for applications, abstractions, closures and compositions; the source carries it out in full. Hardin’s method then yields the theorem. ◻

Exercise 148.1

★☆☆ Reduce (λ(21))[aid] to σ-normal form, naming the rule at each step. Recall that 2 abbreviates 1[].

Exercise 148.2

★★☆ Check local confluence for the critical pair between Map and Ass on ((as1)s2)s3, displaying both reducts and their common reduct.

Exercise 148.3

★★☆ Show that σ alone does not prove 1[s](s)=s for an arbitrary substitution variable s, although the equation holds for every closed normal s. Which rule would have to be added, and what does its absence say about the difference between the equational theory and the rewriting system?

Composition destroys strong normalization

λσ is confluent. It does not follow that a term which cannot diverge under ordinary β-reduction cannot diverge in λσ, and it is false.

The failure is easiest to display with named variables, in a calculus with single substitutions. Write tx:=u for a pending substitution and consider the single composition rule tx:=uy:=vtx:=uy:=vif yFV(t). The side condition says the outer substitution is garbage for t: nothing in t uses y. The rule does not delete it; it pushes it into the body of the inner substitution. Equation 148.1 is weaker than the Clos/Map pair of definition 148.2, which implement the parallel form of the same move, so a divergence built from it is a divergence of λσ.

Theorem 148.8 — Composition breaks preservation of strong normalization

There is a term, strongly normalizing under β-reduction, that admits an infinite reduction using β, the substitution-propagation rules, and (148.1).

Proof of Theorem 148.8 — Composition breaks preservation of strong normalization

Proof. Let a,b,y,y be four distinct variables and define S0:=y:=(λy.a)b,Sn+1:=y:=bSn. Start from M:=(λy.(λy.a)((λy.a)b))((λy.a)b) and compute: May:=(λy.a)by:=(λy.a)btwo Beta stepsay:=((λy.a)b)y:=(λy.a)b(148.1), since yFV(a)ay:=(λy.aS0)(bS0)propagation into the applicationay:=aS0y:=bS0Beta, and the last expression is ay:=aS0S1. The same four moves applied to aS0Sm+1 give aS0Sm+1ay:=aSm+1Sm+2, and for the mixed indices aSm+1Sn+1=ay:=bSmy:=bSnay:=bSmy:=bSn=ay:=bSmSn+1. Chaining these three schemes produces MS0S1S1S2S0S2S2S3S1S3S0S3, an infinite reduction, since the index pair strictly increases along the displayed schedule and no expression repeats.

The term M is strongly normalizing under β: every β-redex of M has a body in which the bound variable does not occur, so each contraction erases its argument and strictly decreases the number of λ’s. The original counterexample uses the simply typable term λx.(λy.I(Iy))(Ix) with I:=λz.z, for which the same schedule runs and the type assignment is displayed in the source. ◻

Remark 148.9 — Where the divergence lives

Every step after the first three takes place inside a substitution that no free variable of the body uses. The reduction never produces a term whose σ-normal form is new; by theorem 148.6 the projection of the whole sequence to ordinary λ-terms is finite. What diverges is the bookkeeping, not the computation. Two repairs suggest themselves: forbid pushing a substitution into garbage, or delete garbage instead of pushing it. The calculus of the next section does both, with one side condition and one rule.

Remark 148.10 — Confluence and normalization are independent

Theorem 148.7 and theorem 148.8 hold of the same calculus. Confluence says that the results of terminating computations agree; it says nothing about which computations terminate. Neither theorem may be used to weaken the other, and neither transfers to a different explicit-substitution calculus without a proof for that calculus.

Safe composition: the λex-calculus

Definition 148.11 — Syntax and equation

Terms are generated by T::=xTTλx.TT[x/T], with λx.t and t[x/u] both binding x in t; free variables are as usual, with FV(t[x/u])=(FV(t){x})FV(u). Metalevel substitution t{x/u} is defined as usual and additionally by t[y/u]{x/v}:=t{x/v}[y/u{x/v}]. The equation Ct[x/u][y/v]=t[y/v][x/u]if yFV(u) and xFV(v) together with α-conversion generates an equivalence =e on terms.

Definition 148.12 — The rules

B(λx.t)ut[x/u]Varx[x/u]uGct[x/u]tif xFV(t)App(tu)[x/v]t[x/v]u[x/v]Lamb(λy.t)[x/v]λy.t[x/v]Compt[x/u][y/v]t[y/v][x/u[y/v]]if yFV(u) Write x for the relation generated by the five rules other than B, and let λex be modulo =e: that is, tλext when t=ess=et for some s,s.

Two features distinguish definition 148.12 from definition 148.2. The composition rule Comp fires only when yFV(u), that is, only when the outer substitution has something to do inside the inner one. And Gc deletes a substitution whose variable does not occur. Together they remove exactly the move (148.1) used in theorem 148.8: when yFV(t) and yFV(u) the substitution is deleted, and when yFV(u) it is composed, which is not pushing into garbage.

Theorem 148.13 — Full composition

For all terms t,u, t[x/u]λext{x/u}.

Proof of Theorem 148.13 — Full composition

Proof. Induction on t.

t=x. x[x/u]Varu=x{x/u}.

t=yx. xFV(y), so y[x/u]Gcy=y{x/u}.

t=t1t2. By App, (t1t2)[x/u]t1[x/u]t2[x/u]; the two induction hypotheses then give t1{x/u}t2{x/u}, which is (t1t2){x/u}.

t=λy.s. Choose yFV(u){x} by α-conversion. Then (λy.s)[x/u]Lambλy.s[x/u]IHλy.s{x/u}=(λy.s){x/u}.

t=s[y/v]. Choose yFV(u){x}. Two cases. If xFV(v), then Comp applies to s[y/v][x/u] and gives s[x/u][y/v[x/u]]; the induction hypotheses for s and v reduce this to s{x/u}[y/v{x/u}], which is (s[y/v]){x/u} by definition 148.11. If xFV(v), then v{x/u}=v and the equation C applies, since yFV(u) and xFV(v): s[y/v][x/u]=es[x/u][y/v]s{x/u}[y/v]=(s[y/v]){x/u}. ◻

Corollary 148.14 — Simulation

If tβt for λ-terms t,t, then tλext.

Proof of Corollary 148.14 — Simulation

Proof. A β-step contracts (λx.s)u to s{x/u} in some context. By B the same context reduces to s[x/u], and by theorem 148.13 that reduces to s{x/u}. Reduction is compatible with contexts. ◻

Proposition 148.15 — The counterexample is blocked

In λex the term M of theorem 148.8 reduces in four steps to the normal form a.

Proof of Proposition 148.15 — The counterexample is blocked

Proof. Two B steps are available; take the outer one first: MB((λy.a)((λy.a)b))[y/(λy.a)b]. Now FV((λy.a)((λy.a)b))={a,b}, which does not contain y, so Gc applies and Comp does not: Comp would require a substitution immediately inside, and the body is an application. Hence Gc(λy.a)((λy.a)b)Ba[y/(λy.a)b]Gca, the last step because yFV(a)={a}. The divergence of theorem 148.8 is created by the step from ay:=y:= to ay:=y:=. In λex that redex would be Comp at the term a[y/(λy.a)b][y/(λy.a)b], and its side condition asks for yFV((λy.a)b), a set equal to {a,b}; the condition fails. ◻

Theorem 148.16 — Imported properties of λ ex

The following hold for λex and are imported at the exact signature stated.

  1. Perpetuality. There is a deterministic many-step strategy on terms such that tt implies tλex+t, and such that tt with t strongly normalizing implies t strongly normalizing.

  2. Preservation of strong normalization. If a λ-term t is strongly normalizing for β, then t is strongly normalizing for λex.

  3. Confluence. The relation λex is confluent on metaterms, that is, on terms extended by metavariables carrying a list of delayed substitutions.

  4. Typed strong normalization. Every simply typed λex-term is strongly normalizing.

The proofs are not reproduced. Clause (1) is proved by induction on the strategy and depends on a separate property of the substitution calculus; clause (2) follows from (1) through an inductive characterization of the strongly normalizing terms; clause (3) is proved by exhibiting a superdevelopment map with van Oostrom’s Z-property, and clause (4) by a modular normalization theorem applied to a translation into a calculus with weakening. Locators for all four are in section 148.3. What is proved locally is theorem 148.13, corollary 148.14 and proposition 148.15; these do not depend on clauses (1)–(4), and clauses (1)–(4) are not transferred to λσ, where (2) is false by theorem 148.8.

Exercise 148.4

★★☆ Show that Gc is not derivable from the other five rules of definition 148.12: exhibit a term t[x/u] with xFV(t) that is a normal form for {B,Var,App,Lamb,Comp} modulo =e.

Exercise 148.5

★★☆ Drop the side condition yFV(u) from Comp and show that the resulting calculus admits the infinite reduction of theorem 148.8, by writing the first four steps in the notation of definition 148.12.

Exercise 148.6

★★☆ State and prove the analogue of theorem 148.13 for λσ: for every term a, σ(a[bid]) is the de Bruijn metasubstitution of b into a. Which theorem of section 148.1 is this?

Suggested first pass.

Begin with exercise 148.7 and exercise 148.8, then complete exercise 148.11.

Exercise 148.7

★★☆ Reduce (λ(λ(21)))[aid] to σ-normal form in λσ, naming every rule. Then perform the same computation with named variables in λex and compare the number of steps.

Exercise 148.8

★★★ Verify the schedule displayed at the end of the proof of theorem 148.8 by computing the first three transitions S0S1S1S2S0S2 in full, and prove that no expression occurs twice, using the pair (m,n) of indices as a measure.

Exercise 148.9

★★☆ Theorem 148.7 is proved for the untyped calculus. Explain why adding the simple typing rules of chapter 2 to λσ does not by itself repair theorem 148.8, and identify the exact clause of the counterexample that typing would have to exclude.

Exercise 148.10

★★★ Compare definition 148.2 with the CwF equations of definition 54.16: match id, , as and st with the identity, p, γ,a and composition, and identify which of the ten σ rules become CwF equations and which become derived facts. State precisely one σ rule that is an orientation choice with no counterpart in definition 54.16, and say what is lost by orienting it the other way.

Exercise 148.11

★★★ Practical project.explicit-substitution-reducer Implement a reducer for both calculi and a divergence detector. The program takes a term in one of two input syntaxes — de Bruijn λσ expressions over the grammar of definition 148.1, or named-variable λex terms over the grammar of definition 148.11 — a rule set, a reduction strategy (leftmost-outermost or a supplied rule order), and a step budget.

Invariant. Every state is a well-formed expression of the declared sort: in λσ, terms and substitutions are never confused, and every rule instance is applied at a subexpression of the matching sort; in λex, the side conditions of Gc and Comp are recomputed from the current free-variable sets at each step and never cached across a rewrite. The program checks the invariant before each step and aborts, printing the offending subexpression, rather than continuing.

Concrete result. The program prints the numbered trace, each line carrying the rule name and the redex position, and terminates with normal form: followed by the expression, or with budget exhausted followed by the last expression and the multiset of rule names used.

Acceptance test. In λσ, reducing (λ(21))[aid] with the σ rules must reach a normal form containing no closure other than the codings 1[n], as proposition 148.5 requires. Running the term M of theorem 148.8 in λex under the leftmost-outermost strategy must print normal form: a after exactly four steps, with the rule sequence B, Gc, B, Gc, matching proposition 148.15. Finally, run the same M under the rule order that deletes the side condition yFV(u) from Comp, removes Gc, and prefers Comp to every congruence step; that is the calculus of (148.1), and the run must print budget exhausted. A run that still reports a normal form under that order has cached a free-variable set instead of recomputing it, or has kept Gc; either is the defect this test detects.

The divergence of theorem 148.8 is one reduction sequence, not every one, so no acceptance clause asks a leftmost-outermost reducer to find it in λσ: the rule order is part of the input for exactly this reason.

Sources. Definition 148.1, Definition 148.2, proposition 148.4, proposition 148.5, theorem 148.6 and theorem 148.7 follow M. Abadi, L. Cardelli, P.-L. Curien and J.-J. Lévy, Explicit substitutions, Journal of Functional Programming 1 (1991), 375–416. The de Bruijn discussion and the derivation of Abs are on physical pages 4–7 of the authors’ preprint; the rule table of definition 148.2 is on physical page 12; the simulation statement proved here as theorem 148.6 is their Proposition 3.1 on physical page 13; confluence is their Theorem 3.2 and the termination and confluence of σ their Proposition 3.3, both on physical page 14, the termination being credited there to Hardin and Laville; the normal-form grammar of proposition 148.5 and Hardin’s interpretation method are on physical pages 14–15. Theorem 148.8 is P.-A. Melliès, Typed lambda-calculi with explicit substitutions may not terminate, TLCA 1995, LNCS 902, 328–334 [Mel95]; the simplified form displayed here, together with the schedule and the remark that the original term is the typable λx.(λy.I(Iy))(Ix), is Figure 1.2 and Remark 1.2.1 of K. H. Rose, Explicit substitution: tutorial and survey, BRICS Lecture Series LS-96-3, 1996 [Ros96], physical pages 26–28. Definition 148.11, Definition 148.12 are Figure 1 of D. Kesner, A theory of explicit substitutions with safe and full composition, Logical Methods in Computer Science 5(3:1) (2009), 1–29, physical pages 5–6; theorem 148.13 and corollary 148.14 are her full-composition property and Lemma 2.3 on physical page 7; the four imported clauses of theorem 148.16 are her Theorem 3.3 (physical page 7), Theorem 3.6 (physical page 8), Corollary 8.13 (physical page 24) and the typed strong-normalization result of Section 7 (physical page 20). The algebraic reading of the same operators, in which the σ equations become the equations of a category with families rather than rewrite rules, is chapter 54; the telescopic presentation that preceded both is de Bruijn’s.

Search the book

Type to search the local edition.