Prerequisites. Direct starred prerequisites: The tactic chapter is required only for the tactic wrapper; the simplifier and certificate checker use the smaller routes stated at their definitions. No later core chapter depends on this route.
The kernel reduces (𝜆𝑥.𝑥)𝑎 by definitional computation. It does not reduce 𝑎+0 to 𝑎 when addition recurses on its first argument and 𝑎 is neutral. The equation 𝗉𝗅𝗎𝗌𝖹𝖾𝗋𝗈(𝑎):𝑎+0=𝑎 permits a propositional rewrite, but a tactic that merely replaces the left side has not yet justified the replacement, chosen an orientation, or shown that repeated rewriting terminates. A simplifier must return both a result and a proof connecting it to the input.
A proof-producing simplifier
Fix a first-order family of well-typed expressions generated by variables, constants, and declared function symbols. The family is indexed by object types, so ill-typed applications are not expressions.
A certified rewrite rule over a telescope Δ consists of well-typed expressions 𝑙,𝑟:𝐴, a kernel derivation 𝑞 of Δ⊢𝑙=𝑟, and a natural-number measure 𝜇 satisfying 𝜇(𝑟𝜌)<𝜇(𝑙𝜌) for every well-typed substitution 𝜌. Variables of 𝑟 must occur in 𝑙. A finite ordered list 𝐷 of such rules is a terminating rewrite database when every rule uses the same measure 𝜇, every proper subexpression has smaller measure, and 𝜇 is monotone in arguments: if 𝜇(𝑎𝑖)≤𝜇(𝑏𝑖) for every 𝑖, then 𝜇(𝑓⃗𝑎)≤𝜇(𝑓⃗𝑏).
At a term 𝑡, the function 𝗋𝗈𝗈𝗍𝐷(𝑡) selects the first pair (𝑞,𝜌) for which 𝑡 is syntactically 𝑙𝜌, and returns (𝑟𝜌,𝑞𝜌). It returns failure when no rule matches.
The decrease premise rejects the tempting pair of rules 𝑎+0↦𝑎 and 𝑎↦𝑎+0. Each rule is valid as an equality, but their union admits an infinite simplification sequence.
For each 𝑘-ary symbol 𝑓:𝐴1→⋯→𝐴𝑘→𝐵, the database contains the kernel-derived operation 𝖼𝗈𝗇𝗀𝑓:(𝑎1=𝑏1)→⋯→(𝑎𝑘=𝑏𝑘)→𝑓𝑎1⋯𝑎𝑘=𝑓𝑏1⋯𝑏𝑘. When 𝐴𝑖 depends on earlier arguments, the 𝑖-th equality is a transported equality in the fiber over 𝑏1,…,𝑏𝑖−1; the certificate stores those transports explicitly. A symbol is traversed only when its certificate has the required dependent type.
Without the transported fiber, rewriting 𝑛=𝑚 inside 𝑣:𝖵𝖾𝖼𝐴𝑛 would claim that the unchanged 𝑣 already has type 𝖵𝖾𝖼𝐴𝑚. Dependent congruence instead inserts transport before rebuilding the outer expression.
For 𝐷={𝑥+0↦𝑥,0+𝑥↦𝑥}, ordered as written, bottom-up simplification calculates (𝑎+0)+(0+𝑏)𝑐𝑜𝑛𝑔−+=𝑎+(0+𝑏)𝑐𝑜𝑛𝑔−+=𝑎+𝑏. Both annotations name proof terms constructed by congruence and the selected rule proof. No equation has been added to kernel conversion.
Proof. The recursive calls on immediate arguments strictly decrease 𝜇 by the proper-subexpression premise. By the induction hypotheses and argument monotonicity, rebuilding the head produces 𝑡′ with 𝜇(𝑡′)≤𝜇(𝑡). A root rewrite produces 𝑟𝜌 with 𝜇(𝑟𝜌)<𝜇(𝑡′). Thus the recursive call after a root rewrite also strictly decreases 𝜇, proving termination.
For preservation and the output bound, use well-founded induction on that measure. The induction hypotheses for the arguments return well-typed replacements, equality proofs, and nonincreasing measures. The certificate from definition 115.2 rebuilds a well-typed 𝑡′ and gives 𝑝0:𝑡=𝑡′, including every required transport. In the root case, the rule certificate instantiated by the well-typed substitution gives 𝑞𝜌:𝑡′=𝑟𝜌; the induction hypothesis gives 𝑝1:𝑟𝜌=𝑢 and 𝜇(𝑢)≤𝜇(𝑟𝜌). Equality transitivity gives 𝑡=𝑢, while 𝜇(𝑢)<𝜇(𝑡′)≤𝜇(𝑡). In the no-root case, 𝑝0 is the requested proof and argument monotonicity gives 𝜇(𝑢)=𝜇(𝑡′)≤𝜇(𝑡). These are all clauses of the algorithm. ◻
★☆☆ Take 𝜇(𝑡) to be the number of additions in 𝑡. Determine which orientation of 𝑎+0=𝑎 can occur in a database using 𝜇, and give a three-term loop showing why admitting both orientations destroys the termination proof.
Let 𝐴:U𝑖, 𝐵:𝐴→U𝑗, and 𝑅𝐴:𝐴→𝐴→U𝑘. A heterogeneous fiber relation has type 𝑅𝐵:∏𝑥:𝐴∏𝑦:𝐴𝑅𝐴𝑥𝑦→𝐵(𝑥)→𝐵(𝑦)→Uℓ. For 𝑓,𝑔:∏𝑥:𝐴𝐵(𝑥), define 𝖱𝖾𝗌𝗉(𝑅𝐴,𝑅𝐵)(𝑓,𝑔):=∏𝑥:𝐴∏𝑦:𝐴∏𝑝:𝑅𝐴𝑥𝑦𝑅𝐵𝑥𝑦𝑝(𝑓𝑥)(𝑔𝑦). A dependent function 𝑓 is respectful when it is equipped with 𝗉𝗋𝗈𝗉𝖾𝗋𝑓:𝖱𝖾𝗌𝗉(𝑅𝐴,𝑅𝐵)(𝑓,𝑓). If 𝐵 and 𝑅𝐵 are constant in 𝑥,𝑦,𝑝, this specializes to the ordinary binary relation on functions. It is stronger than the pointwise relation ∏𝑥:𝐴𝑅𝐵𝑥𝑥(𝗋𝖾𝖿𝗅𝖾𝗑𝑅𝐴𝑥)(𝑓𝑥)(𝑔𝑥): the respectful witness also compares outputs in different fibers 𝐵(𝑥) and 𝐵(𝑦). No unmentioned transport is required because its target relation is indexed by the evidence 𝑝:𝑅𝐴𝑥𝑦.
A contextual rewrite is therefore an inductively defined relation, not a traversal. Write 𝖱𝗐(Γ;𝑅;𝑡;𝑢;𝑣) for “in Γ, the term 𝑡 rewrites to 𝑢 at the relation 𝑅, witnessed by the kernel term 𝑣”. The relation is not a function: which respectful morphism and which subrelation step are used is resolved by search, and different searches may return different witnesses.
Fix a certified database 𝐷 and, for every traversed symbol, a respectful-morphism certificate in the sense of definition 115.5. The judgment is generated by five rules.
𝗋𝗈𝗈𝗍𝐷(𝑡)=(𝑟𝜌,𝑞𝜌)
𝖱𝗐(Γ;=;𝑡;𝑟𝜌;𝑞𝜌)
Rw-Root
Γ⊢𝑡:𝐴Γ⊢𝗋𝖾𝖿𝗅𝖾𝗑𝑅:∏𝑥:𝐴𝑅𝑥𝑥
𝖱𝗐(Γ;𝑅;𝑡;𝑡;𝗋𝖾𝖿𝗅𝖾𝗑𝑅𝑡)
Rw-Atom
𝖱𝗐(Γ;𝖱𝖾𝗌𝗉(𝑅𝐴,𝑅𝐵);𝑓;𝑓′;𝑣𝑓)𝖱𝗐(Γ;𝑅𝐴;𝑎;𝑎′;𝑣𝑎)
𝖱𝗐(Γ;𝑅𝐵𝑎𝑎′𝑣𝑎;𝑓𝑎;𝑓′𝑎′;𝑣𝑓𝑎𝑎′𝑣𝑎)
Rw-App
𝖱𝗐(Γ,𝑥:𝐴,𝑦:𝐴,𝑝:𝑅𝐴𝑥𝑦;𝑅𝐵𝑥𝑦𝑝;𝑏;𝑏′;𝑣)
𝖱𝗐(Γ;𝖱𝖾𝗌𝗉(𝑅𝐴,𝑅𝐵);𝜆𝑥.𝑏;𝜆𝑦.𝑏′;𝜆𝑥.𝜆𝑦.𝜆𝑝.𝑣)
Rw-Lam
𝖱𝗐(Γ;𝑅;𝑡;𝑢;𝑣)Γ⊢𝗌𝗎𝖻:∏𝑥:𝐴∏𝑦:𝐴𝑅𝑥𝑦→𝑆𝑥𝑦
𝖱𝗐(Γ;𝑆;𝑡;𝑢;𝗌𝗎𝖻𝑡𝑢𝑣)
Rw-Sub
Here 𝖱𝖾𝗌𝗉(𝑅𝐴,𝑅𝐵) is the dependent respectful relation of definition 115.5, so the witness 𝑣𝑓 in Rw-App is exactly a term of type ∏𝑥:𝐴∏𝑦:𝐴∏𝑝:𝑅𝐴𝑥𝑦𝑅𝐵𝑥𝑦𝑝(𝑓𝑥)(𝑓′𝑦). Rewriting at = is the special case in which every relation is the identity type and every 𝗉𝗋𝗈𝗉𝖾𝗋𝑓 is 𝖼𝗈𝗇𝗀𝑓.
The corresponding constraint-generation rules may instead treat relations and morphisms as metavariables resolved by class search. This architecture does not prove termination of an arbitrary user database. Termination remains the explicit measure premise of definition 115.1.
Proof of Proposition 115.7 — Contextual preservation
Proof. Rule induction on the derivation, one case per rule of definition 115.6.
Rw-Root: definition 115.1 supplies Γ⊢𝑞𝜌:𝑡=𝑟𝜌 as the instantiated rule certificate.
Rw-Atom: the stored reflexivity proof has type 𝑅𝑡𝑡 at the instantiating argument 𝑡.
Rw-App: the induction hypothesis for the function gives Γ⊢𝑣𝑓:∏𝑥:𝐴∏𝑦:𝐴∏𝑝:𝑅𝐴𝑥𝑦𝑅𝐵𝑥𝑦𝑝(𝑓𝑥)(𝑓′𝑦) and the one for the argument gives Γ⊢𝑣𝑎:𝑅𝐴𝑎𝑎′. Three applications derive Γ⊢𝑣𝑓𝑎𝑎′𝑣𝑎:𝑅𝐵𝑎𝑎′𝑣𝑎(𝑓𝑎)(𝑓′𝑎′).
Rw-Lam: the induction hypothesis in the context Γ,𝑥:𝐴,𝑦:𝐴,𝑝:𝑅𝐴𝑥𝑦 gives Γ,𝑥:𝐴,𝑦:𝐴,𝑝:𝑅𝐴𝑥𝑦⊢𝑣:𝑅𝐵𝑥𝑦𝑝𝑏𝑏′. Three Π-introductions produce 𝜆𝑥.𝜆𝑦.𝜆𝑝.𝑣:𝖱𝖾𝗌𝗉(𝑅𝐴,𝑅𝐵)(𝜆𝑥.𝑏)(𝜆𝑦.𝑏′), exactly the conclusion relation.
Rw-Sub: the induction hypothesis gives Γ⊢𝑣:𝑅𝑡𝑢, and the stored implication carries it to 𝑆𝑡𝑢.
These are the five rules of the judgment. ◻
The dependent case is now explicit in Rw-App. Its conclusion relation is 𝑅𝐵𝑎𝑎′𝑣𝑎, whose left and right endpoints have types 𝐵(𝑎) and 𝐵(𝑎′). A symbol without a witness of 𝖱𝖾𝗌𝗉(𝑅𝐴,𝑅𝐵) is not traversed; substituting a merely pointwise witness would leave the application at 𝑎′ untyped.
Reflection with a small checker
Executing a normalizer inside the metaprogram does not justify its answer. Reflection reduces trust by making the kernel evaluate a small verified checker.
For an environment 𝜌:ℕ→𝑀, where (𝑀,0,+) is a commutative monoid, expressions and their denotation are 𝑒::=𝗏𝖺𝗋(𝑖)∣𝗓𝖾𝗋𝗈∣𝖺𝖽𝖽(𝑒,𝑒),𝖾𝗏𝖺𝗅𝜌(𝗏𝖺𝗋(𝑖))=𝜌(𝑖),𝖾𝗏𝖺𝗅𝜌(𝗓𝖾𝗋𝗈)=0,𝖾𝗏𝖺𝗅𝜌(𝖺𝖽𝖽(𝑒1,𝑒2))=𝖾𝗏𝖺𝗅𝜌(𝑒1)+𝖾𝗏𝖺𝗅𝜌(𝑒2). The normal form 𝗇𝖿(𝑒) is the finite vector of variable multiplicities, computed by vector addition. The checker 𝗌𝖺𝗆𝖾𝖭𝖥(𝑒1,𝑒2) compares these vectors.
Proof of Lemma 115.9 — Evaluation of multiplicity normal forms
Proof. By structural induction on 𝑒. A variable contributes the unit vector at its index, and zero contributes the zero vector. For addition, the induction hypotheses give the two finite sums. Associativity and commutativity regroup their concatenation by index, and vector addition adds the two multiplicities. ◻
Proof of Theorem 115.10 — Reflection checker soundness
Proof. The Boolean result means that the two finite multiplicity vectors are equal component by component. Apply lemma 115.9 to both expressions and replace one vector by the other. The resulting finite sums are identical. ◻
Reification itself remains untrusted. The tactic constructs expressions 𝑒1,𝑒2 and kernel proofs that their evaluations equal the original terms. The kernel then combines those proofs with theorem 115.10. A wrong reification proof is rejected.
Programming up to congruence. The simplifier above leaves kernel conversion alone and pays for every replacement with a proof term. A different design changes the conversion relation itself. It is a separate calculus with separate theorems, and it is frozen here so that nothing above is read as applying to it.
Write Zombie for the comparison calculus, which has two layers.
The core language is dependently typed with erasable annotations and no automatic beta conversion. Its evaluation relation is a small-step reduction on erased terms, and beta equalities enter only through the explicit proof form 𝗃𝗈𝗂𝗇, which reduces both sides a bounded number of steps and returns an equation. Recursion is unrestricted, so a well-typed term may diverge; type checking never evaluates a term except under a 𝗃𝗈𝗂𝗇 with its own step bound.
The surface language is bidirectional. Its type equality is defined to be the typed congruence closure Γ⊢𝑎=𝑏 of the equations available in Γ, closed under reflexivity, symmetry, transitivity, congruence for labelled applications, injectivity of type constructors, and an assumption rule. Two types related by that closure are interchangeable without a written cast.
The source separates four boundaries. Theorem 1 proves soundness of elaboration from surface derivations to core terms, including preservation of erasure. Theorem 2 proves completeness with respect to the declarative surface system. Lemma 3 decides the finite, labelled, untyped congruence judgment used by the algorithm; it does not by itself establish a typed equality or construct its core proof. Theorem 4 supplies that missing bridge: from the source’s well-formed typed congruence judgment and its well-formed equality type, the elaborator constructs the corresponding core equality proof. Decidability therefore rests on finite labelled congruence closure together with the separate typing bridge, not on normalization of possibly diverging terms.
The move that this buys is visible in one equation. In the core language, 𝗇𝗉𝗅𝗎𝗌𝗓𝖾𝗋𝗈:∏𝑛:ℕ𝑛+0=𝑛 is proved by induction on 𝑛. In the successor case the hypothesis is 𝗂𝗁:𝑚+0=𝑚 and the goal is 𝗌𝗎𝖼(𝑚+0)=𝗌𝗎𝖼𝑚. Ordinary definitional conversion cannot close it: with addition recursing on its first argument and 𝑚 a variable, 𝑚+0 is a neutral term and 𝗌𝗎𝖼(𝑚+0)≡𝗌𝗎𝖼𝑚 fails. The surface language closes it because 𝗂𝗁 is in the context and congruence under the label 𝗌𝗎𝖼 belongs to the equality relation, so the goal type is reached with no written cast. The same example shows the cost: the base case needs 0+0=0, which no assumption supplies, and the programmer must write 𝗃𝗈𝗂𝗇 to obtain it by reduction, because beta is not part of the surface equality either.
Three boundaries hold. The congruence relation of convention 115.11 is not added to the kernel conversion of this chapter, so theorem 115.4 and proposition 115.7 neither use it nor follow from it; conversely, nothing in that source proves the measure-based termination of definition 115.1. A neutral application may be equal by the generated congruence while remaining definitionally stuck, which is exactly the 𝗌𝗎𝖼(𝑚+0) case above. Finally, the congruence proof terms the algorithm returns are core terms, not kernel derivations of this chapter’s identity type, and Lemma 3 is a decision procedure for that source relation only.
Sources
The generalized-rewriting architecture follows Sozeau [Soz09]; its constraint rules are in Figure 1. The comparison calculus follows Sjöberg and Weirich [SW15].
Suggested first pass.
None of these problems is a prerequisite for a later chapter. Begin with exercise 115.2, then complete exercise 115.4.
★★☆ Extend the opening database by associativity oriented toward right-associated terms. Give a lexicographic measure that proves termination, and simplify (𝑎+0)+(𝑏+(0+𝑐)) with every equality step annotated.
★★★ Write the dependent congruence certificate for 𝗍𝗋𝖺𝗇𝗌𝗉𝗈𝗋𝗍:(𝑛=𝑚)→𝖵𝖾𝖼𝐴𝑛→𝖵𝖾𝖼𝐴𝑚. Exhibit the ill-typed intermediate term produced if the transport is omitted.
★★★Practical project.proof-producing-commutative-monoid-simplifier Implement in Agda or Kappa the expressions, normalizer, and checker of definition 115.8, with no trailing zero multiplicities. Add a finite indexed relation with related indices whose fibers have different shapes, and check that a dependent function respects that relation. Print equal on commute and reassociate, not-equal on different, and dependent-respectful on the heterogeneous fiber case. Deleting the right summand must change the reassociate result. Replacing the heterogeneous fiber relation by a same-fiber test must change the final result to dependent-respectful-failed. These four named results are the acceptance test. They check the finite reflected theory and one finite model of dependent respectfulness, not arbitrary dependent rewriting.