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 :
Free variables keep their names. The number means "the variable bound by the -th enclosing binder, counting from the innermost at ". Recall from Chapter 11 that for a list of names , is the position of the last occurrence of in , counted from the right starting at , and is undefined if .
For a list of names , the nameless form of a term that occurs under the binders is defined by
Equality of nameless terms is plain syntactic equality. Note that has the same outermost shape as , 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 , , , and : the judgment is derivable if and only if .
By induction on , for all , , and . For each shape of , exactly one rule of Figure 11.1 can conclude the judgment, and its premises match the equations for .
If , both sides hold exactly when , by the rule -Sort and by the equation for universes.
If , the nameless forms can be equal only if is a variable , since a nameless variable (a number or a name) never equals a , a , an application, or a universe. If , then , and equality holds exactly when and , which is the premise of -Bound. If , then , and equality holds exactly when and , which is the premise of -Free. A number never equals a name, matching the absence of a rule for a variable that is bound on one side only.
If , equality holds exactly when with and . By the induction hypothesis, applied to and to , these two equations are exactly the premises of -Pi. The cases and application are the same, using -Lam and -App.
Two facts about renaming
The definition of in Chapter 11 involves substitution: it allows replacing by when . To relate it to nameless forms we need to know how behaves when a bound variable is renamed. Two lemmas supply this.
The first says that depends on only through the free variables of . 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 for every , with both sides possibly undefined, then .
By induction on , for all and . The variable case is exactly the assumption: a free variable becomes the same number, or stays the same name, on both sides. Universes are unaffected by , and applications follow from the induction hypothesis for both parts.
Under a binder , the lists and again agree on the free variables of the body. The variable has position in both. Any other free variable of the body is a free variable of , so by assumption its position is the same in and ; appending 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 before it and 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 , , and . Then for every list .
By induction on the size of , for all and . Write and ; they differ in exactly one entry, at the same position.
If , then . Since , the last occurrence of in and the last occurrence of in are both the distinguished entry, so and both equal the length of , and the nameless forms are the same number.
If with , then because , and . The position of is the same in and , since 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 ; the case of is the same. The annotation 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 . The binder shadows , so , and we need . We apply Lemma B.2. The variable has position in both lists, because it was just appended. Any other differs from , since , so its position is the same in both lists, or undefined in both.
Case . No renaming is needed, and . The induction hypothesis for , with extended by (which is allowed since is neither nor ), gives , which is what the body requires.
Case . The binder would capture , so substitution renames it to a fresh :
We need , and we get there in two steps. First, the induction hypothesis for , renaming its last binder to (with taken as and empty; the hypotheses hold because , , and the list after the binder is empty), gives . Second, the induction hypothesis for , which has the size of and has no free , with extended by , gives . 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 also amounts to equality of nameless forms. The two directions are proved differently: one by the minimality of as a congruence, the other by induction on size.
Lemma B.4 (The definition compares nameless forms). If in the sense of the definition in Chapter 11, then for every . Conversely, if for some , then .
First part. Let mean that for every . This relation is an equivalence, since equality is. It is a congruence, because is defined constructor by constructor: if the parts of two terms have equal nameless forms under the appropriate lists, so do the terms. Finally, contains the defining equation of : if , then
and the bodies are equal by Lemma B.3 with empty; the case is trivial, since then the two terms are identical. The same holds for . Since is the smallest congruence containing that equation, and is a congruence containing it, implies .
Second part. We use induction on the size of , for all and , assuming .
If is a variable, so is , since nameless forms of other shapes are never a number or a name. If both nameless forms are the number , both variables are the entry at position of , so . If both are names, then directly. In either case by reflexivity.
Universes are equal outright. For applications, the nameless forms of the two parts agree, so the induction hypothesis gives for each part, and the congruence property of gives it for the whole.
Let , so that with and . The induction hypothesis gives . For the bodies, choose . Lemma B.3 with empty gives and , so
Since has the size of , which is smaller than the size of , the induction hypothesis gives . Then
by the defining equation (valid since ), by congruence, and by the defining equation read backwards (valid since ). The case of is the same.
Proof of Theorem 11.1
Theorem 11.1 (Correctness of algorithmic alpha-equivalence). For all terms and , in the sense of the definition in Chapter 11 if and only if is derivable by the rules of Figure 11.1.
If , then by the first part of Lemma B.4 (taking empty), and then by Lemma B.1.
Conversely, if , then by Lemma B.1, and then 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.