h5i

Appendix B

Correctness of Alpha-Equivalence

Chapter 11 gave two definitions of alpha-equivalence: a mathematical one (the smallest congruence that allows renaming a bound variable) and an algorithmic one (the rules of Figure 11.1, which walk two terms in lockstep and compare bound variables by position). Theorem 11.1 says that they agree. This appendix proves it.

The idea

Both definitions turn out to say the same thing: the two terms have the same de Bruijn representation, that is, the same shape once every bound variable is replaced by the distance to its binder. So the proof goes through a third, nameless representation of terms.

  • Lemma B.1 shows that the algorithm compares nameless forms. This is almost immediate, because the rules of Figure 11.1 compare positions exactly as de Bruijn indices do.
  • Lemmas B.2 and B.3 establish two facts about how nameless forms behave under renaming. The key one is that renaming a bound variable does not change the nameless form.
  • Lemma B.4 shows that the mathematical definition also compares nameless forms, using those two facts.
  • Theorem 11.1 then follows by chaining the two lemmas through the nameless form.

The proofs are inductions on terms, with one subtlety: substitution may rename a binder, so some inductions must be on the size of a term rather than on its structure.

Nameless terms

A nameless term is built like a term, except that binders carry no names and a bound variable is written as a number #i\#i:

d::=Sort u∣#i∣x∣Π d. d∣λ d. d∣d  dd ::= \Sort{u} \mid \#i \mid x \mid \Pi\,d.\,d \mid \lambda\,d.\,d \mid d\;d

Free variables keep their names. The number #i\#i means "the variable bound by the ii-th enclosing binder, counting from the innermost at 00". Recall from Chapter 11 that for a list of names EE, idx(E,x)\mathrm{idx}(E, x) is the position of the last occurrence of xx in EE, counted from the right starting at 00, and is undefined if x∉Ex \notin E.

For a list of names EE, the nameless form ⌊t⌋E\db{t}{E} of a term tt that occurs under the binders EE is defined by

⌊Sort u⌋E=Sort u⌊x⌋E={#idx(E,x)if x∈E,xotherwise,⌊Π(x:A). B⌋E=Π ⌊A⌋E. ⌊B⌋E,x⌊λ(x:A). t⌋E=λ ⌊A⌋E. ⌊t⌋E,x⌊t1  t2⌋E=⌊t1⌋E  ⌊t2⌋E.\begin{array}{rlrl} \db{\Sort{u}}{E} &= \Sort{u} & \qquad \db{x}{E} &= \begin{cases} \#\mathrm{idx}(E, x) & \text{if } x \in E, \\ x & \text{otherwise,} \end{cases} \\[12pt] \db{\tPi{x}{A}{B}}{E} &= \Pi\,\db{A}{E}.\,\db{B}{E, x} & \qquad \db{\tLam{x}{A}{t}}{E} &= \lambda\,\db{A}{E}.\,\db{t}{E, x} \\[4pt] \db{t_1\;t_2}{E} &= \db{t_1}{E}\;\db{t_2}{E}. & & \end{array}

Equality of nameless terms is plain syntactic equality. Note that ⌊t⌋E\db{t}{E} has the same outermost shape as tt, except that a variable may become either a number or a name. Constants, introduced in Chapter 7, are left unchanged like universes and are compared by name; they add only trivial cases below and are omitted.

The algorithm computes nameless forms

The first lemma relates the rules of Figure 11.1 to nameless forms. Its role is to replace the algorithmic judgment, which is defined by rules, by an equation between two terms, which is easier to reason about.

Lemma B.1 (The algorithm compares nameless forms). For all E1E_1, E2E_2, ss, and tt: the judgment E1;E2⊢s=αtE_1; E_2 \vdash s \alphaeq t is derivable if and only if ⌊s⌋E1=⌊t⌋E2\db{s}{E_1} = \db{t}{E_2}.

By induction on ss, for all tt, E1E_1, and E2E_2. For each shape of ss, exactly one rule of Figure 11.1 can conclude the judgment, and its premises match the equations for ⌊⋅⌋\db{\cdot}{}.

If s=Sort us = \Sort{u}, both sides hold exactly when t=Sort ut = \Sort{u}, by the rule α\alpha-Sort and by the equation for universes.

If s=xs = x, the nameless forms can be equal only if tt is a variable yy, since a nameless variable (a number or a name) never equals a Π\Pi, a λ\lambda, an application, or a universe. If x∈E1x \in E_1, then ⌊x⌋E1=#idx(E1,x)\db{x}{E_1} = \#\mathrm{idx}(E_1, x), and equality holds exactly when y∈E2y \in E_2 and idx(E2,y)=idx(E1,x)\mathrm{idx}(E_2, y) = \mathrm{idx}(E_1, x), which is the premise of α\alpha-Bound. If x∉E1x \notin E_1, then ⌊x⌋E1=x\db{x}{E_1} = x, and equality holds exactly when y∉E2y \notin E_2 and y=xy = x, which is the premise of α\alpha-Free. A number never equals a name, matching the absence of a rule for a variable that is bound on one side only.

If s=Π(x:A). Bs = \tPi{x}{A}{B}, equality holds exactly when t=Π(y:A′). B′t = \tPi{y}{A'}{B'} with ⌊A⌋E1=⌊A′⌋E2\db{A}{E_1} = \db{A'}{E_2} and ⌊B⌋E1,x=⌊B′⌋E2,y\db{B}{E_1, x} = \db{B'}{E_2, y}. By the induction hypothesis, applied to AA and to BB, these two equations are exactly the premises of α\alpha-Pi. The cases λ\lambda and application are the same, using α\alpha-Lam and α\alpha-App.

Two facts about renaming

The definition of =α\alphaeq in Chapter 11 involves substitution: it allows replacing Π(x:A). B\tPi{x}{A}{B} by Π(y:A). B[x↦y]\tPi{y}{A}{\subst{B}{x}{y}} when y∉FV(B)y \notin \FV(B). To relate it to nameless forms we need to know how ⌊⋅⌋\db{\cdot}{} behaves when a bound variable is renamed. Two lemmas supply this.

The first says that ⌊t⌋E\db{t}{E} depends on EE only through the free variables of tt. Its role is technical: it lets us swap one binder list for another as long as they agree on the variables that matter.

Lemma B.2 (Agreement). If idx(E1,v)=idx(E2,v)\mathrm{idx}(E_1, v) = \mathrm{idx}(E_2, v) for every v∈FV(t)v \in \FV(t), with both sides possibly undefined, then ⌊t⌋E1=⌊t⌋E2\db{t}{E_1} = \db{t}{E_2}.

By induction on tt, for all E1E_1 and E2E_2. The variable case is exactly the assumption: a free variable vv becomes the same number, or stays the same name, on both sides. Universes are unaffected by EE, and applications follow from the induction hypothesis for both parts.

Under a binder xx, the lists E1,xE_1, x and E2,xE_2, x again agree on the free variables of the body. The variable xx has position 00 in both. Any other free variable vv of the body is a free variable of tt, so by assumption its position is the same in E1E_1 and E2E_2; appending xx increases that position by one on both sides, or leaves it undefined on both sides. The induction hypothesis for the body then applies, and the annotation is handled by the induction hypothesis with the original lists.

The second lemma does the main work of the appendix: renaming a binder does not change the nameless form. It is stated for a binder sitting at an arbitrary depth, with binder lists EE before it and E′E' after it, because the induction has to go under further binders.

We use that substituting a variable for a variable preserves the size of a term, even when binders are renamed along the way; this is a routine induction on the definition of substitution.

Lemma B.3 (Renaming). Let y≠xy \neq x, y∉FV(B)y \notin \FV(B), and x,y∉E′x, y \notin E'. Then ⌊B⌋E,x,E′=⌊B[x↦y]⌋E,y,E′\db{B}{E, x, E'} = \db{\subst{B}{x}{y}}{E, y, E'} for every list EE.

By induction on the size of BB, for all EE and E′E'. Write Lx=E,x,E′L_x = E, x, E' and Ly=E,y,E′L_y = E, y, E'; they differ in exactly one entry, at the same position.

If B=xB = x, then B[x↦y]=y\subst{B}{x}{y} = y. Since x,y∉E′x, y \notin E', the last occurrence of xx in LxL_x and the last occurrence of yy in LyL_y are both the distinguished entry, so idx(Lx,x)\mathrm{idx}(L_x, x) and idx(Ly,y)\mathrm{idx}(L_y, y) both equal the length of E′E', and the nameless forms are the same number.

If B=vB = v with v≠xv \neq x, then v≠yv \neq y because y∉FV(B)y \notin \FV(B), and B[x↦y]=v\subst{B}{x}{y} = v. The position of vv is the same in LxL_x and LyL_y, since vv is neither of the entries that differ, or it is undefined in both.

Universes and applications are immediate from the definitions and the induction hypothesis.

Let B=Π(w:C). DB = \tPi{w}{C}{D}; the case of λ\lambda is the same. The annotation CC is handled by the induction hypothesis in each case below, so we consider only the body. There are three cases, following the definition of substitution in Chapter 11.

Case w=xw = x. The binder shadows xx, so B[x↦y]=Π(x:C[x↦y]). D\subst{B}{x}{y} = \tPi{x}{\subst{C}{x}{y}}{D}, and we need ⌊D⌋Lx,x=⌊D⌋Ly,x\db{D}{L_x, x} = \db{D}{L_y, x}. We apply Lemma B.2. The variable xx has position 00 in both lists, because it was just appended. Any other v∈FV(D)v \in \FV(D) differs from yy, since y∉FV(B)y \notin \FV(B), so its position is the same in both lists, or undefined in both.

Case w∉{x,y}w \notin \{x, y\}. No renaming is needed, and B[x↦y]=Π(w:C[x↦y]). D[x↦y]\subst{B}{x}{y} = \tPi{w}{\subst{C}{x}{y}}{\subst{D}{x}{y}}. The induction hypothesis for DD, with E′E' extended by ww (which is allowed since ww is neither xx nor yy), gives ⌊D⌋Lx,w=⌊D[x↦y]⌋Ly,w\db{D}{L_x, w} = \db{\subst{D}{x}{y}}{L_y, w}, which is what the body requires.

Case w=yw = y. The binder would capture yy, so substitution renames it to a fresh z∉FV(D)∪{x,y}z \notin \FV(D) \cup \{x, y\}:

B[x↦y]=Π(z:C[x↦y]). (D[y↦z])[x↦y].\subst{B}{x}{y} = \tPi{z}{\subst{C}{x}{y}}{\subst{(\subst{D}{y}{z})}{x}{y}}.

We need ⌊D⌋Lx,y=⌊(D[y↦z])[x↦y]⌋Ly,z\db{D}{L_x, y} = \db{\subst{(\subst{D}{y}{z})}{x}{y}}{L_y, z}, and we get there in two steps. First, the induction hypothesis for DD, renaming its last binder yy to zz (with EE taken as LxL_x and E′E' empty; the hypotheses hold because z≠yz \neq y, z∉FV(D)z \notin \FV(D), and the list after the binder is empty), gives ⌊D⌋Lx,y=⌊D[y↦z]⌋Lx,z\db{D}{L_x, y} = \db{\subst{D}{y}{z}}{L_x, z}. Second, the induction hypothesis for D[y↦z]\subst{D}{y}{z}, which has the size of DD and has no free yy, with E′E' extended by zz, gives ⌊D[y↦z]⌋Lx,z=⌊(D[y↦z])[x↦y]⌋Ly,z\db{\subst{D}{y}{z}}{L_x, z} = \db{\subst{(\subst{D}{y}{z})}{x}{y}}{L_y, z}. Chaining the two equations gives the claim.

The definition computes nameless forms

With the two renaming facts in hand, we can show that the mathematical definition of =α\alphaeq also amounts to equality of nameless forms. The two directions are proved differently: one by the minimality of =α\alphaeq as a congruence, the other by induction on size.

Lemma B.4 (The definition compares nameless forms). If s=αts \alphaeq t in the sense of the definition in Chapter 11, then ⌊s⌋E=⌊t⌋E\db{s}{E} = \db{t}{E} for every EE. Conversely, if ⌊s⌋E=⌊t⌋E\db{s}{E} = \db{t}{E} for some EE, then s=αts \alphaeq t.

First part. Let s∼ts \sim t mean that ⌊s⌋E=⌊t⌋E\db{s}{E} = \db{t}{E} for every EE. This relation is an equivalence, since equality is. It is a congruence, because ⌊⋅⌋\db{\cdot}{} is defined constructor by constructor: if the parts of two terms have equal nameless forms under the appropriate lists, so do the terms. Finally, ∼\sim contains the defining equation of =α\alphaeq: if y∉FV(B)y \notin \FV(B), then

⌊Π(x:A). B⌋E=Π ⌊A⌋E. ⌊B⌋E,xand⌊Π(y:A). B[x↦y]⌋E=Π ⌊A⌋E. ⌊B[x↦y]⌋E,y,\db{\tPi{x}{A}{B}}{E} = \Pi\,\db{A}{E}.\,\db{B}{E, x} \qquad\text{and}\qquad \db{\tPi{y}{A}{\subst{B}{x}{y}}}{E} = \Pi\,\db{A}{E}.\,\db{\subst{B}{x}{y}}{E, y},

and the bodies are equal by Lemma B.3 with E′E' empty; the case y=xy = x is trivial, since then the two terms are identical. The same holds for λ\lambda. Since =α\alphaeq is the smallest congruence containing that equation, and ∼\sim is a congruence containing it, s=αts \alphaeq t implies s∼ts \sim t.

Second part. We use induction on the size of ss, for all tt and EE, assuming ⌊s⌋E=⌊t⌋E\db{s}{E} = \db{t}{E}.

If ss is a variable, so is tt, since nameless forms of other shapes are never a number or a name. If both nameless forms are the number #i\#i, both variables are the entry at position ii of EE, so s=ts = t. If both are names, then s=ts = t directly. In either case s=αts \alphaeq t by reflexivity.

Universes are equal outright. For applications, the nameless forms of the two parts agree, so the induction hypothesis gives =α\alphaeq for each part, and the congruence property of =α\alphaeq gives it for the whole.

Let s=Π(x:A). Bs = \tPi{x}{A}{B}, so that t=Π(y:A′). B′t = \tPi{y}{A'}{B'} with ⌊A⌋E=⌊A′⌋E\db{A}{E} = \db{A'}{E} and ⌊B⌋E,x=⌊B′⌋E,y\db{B}{E, x} = \db{B'}{E, y}. The induction hypothesis gives A=αA′A \alphaeq A'. For the bodies, choose z∉FV(B)∪FV(B′)∪{x,y}z \notin \FV(B) \cup \FV(B') \cup \{x, y\}. Lemma B.3 with E′E' empty gives ⌊B⌋E,x=⌊B[x↦z]⌋E,z\db{B}{E, x} = \db{\subst{B}{x}{z}}{E, z} and ⌊B′⌋E,y=⌊B′[y↦z]⌋E,z\db{B'}{E, y} = \db{\subst{B'}{y}{z}}{E, z}, so

⌊B[x↦z]⌋E,z=⌊B′[y↦z]⌋E,z.\db{\subst{B}{x}{z}}{E, z} = \db{\subst{B'}{y}{z}}{E, z}.

Since B[x↦z]\subst{B}{x}{z} has the size of BB, which is smaller than the size of ss, the induction hypothesis gives B[x↦z]=αB′[y↦z]\subst{B}{x}{z} \alphaeq \subst{B'}{y}{z}. Then

Π(x:A). B  =α  Π(z:A). B[x↦z]  =α  Π(z:A′). B′[y↦z]  =α  Π(y:A′). B′,\tPi{x}{A}{B} \;\alphaeq\; \tPi{z}{A}{\subst{B}{x}{z}} \;\alphaeq\; \tPi{z}{A'}{\subst{B'}{y}{z}} \;\alphaeq\; \tPi{y}{A'}{B'},

by the defining equation (valid since z∉FV(B)z \notin \FV(B)), by congruence, and by the defining equation read backwards (valid since z∉FV(B′)z \notin \FV(B')). The case of λ\lambda is the same.

Proof of Theorem 11.1

Theorem 11.1 (Correctness of algorithmic alpha-equivalence). For all terms ss and tt, s=αts \alphaeq t in the sense of the definition in Chapter 11 if and only if ⋅ ;⋅⊢s=αt\cdot\,;\cdot \vdash s \alphaeq t is derivable by the rules of Figure 11.1.

If s=αts \alphaeq t, then ⌊s⌋⋅=⌊t⌋⋅\db{s}{\cdot} = \db{t}{\cdot} by the first part of Lemma B.4 (taking EE empty), and then ⋅ ;⋅⊢s=αt\cdot\,;\cdot \vdash s \alphaeq t by Lemma B.1.

Conversely, if ⋅ ;⋅⊢s=αt\cdot\,;\cdot \vdash s \alphaeq t, then ⌊s⌋⋅=⌊t⌋⋅\db{s}{\cdot} = \db{t}{\cdot} by Lemma B.1, and then s=αts \alphaeq t by the second part of Lemma B.4.

What to remember

  • Both definitions of alpha-equivalence compute the same thing: equality of de Bruijn (nameless) forms. The nameless form is the bridge between them.
  • The algorithm matches nameless forms directly (Lemma B.1), rule by rule.
  • The key fact about the mathematical definition is that renaming a bound variable does not change the nameless form (Lemma B.3), which needs an induction on size because substitution can itself rename binders.
  • A kernel that uses de Bruijn indices gets this theorem for free: alpha-equivalent terms are syntactically identical.