Equations and normalization¶
A syntax declaration may close with an equations block, which turns its terms into the terms of an
equational theory: two terms are equal when the declared equations prove them equal. The block holds
two kinds of equation. An oriented rule name : l ~> r is used left to right, as a step of
computation; a law name : l = r is used in both directions. A theory decides each kind with the
procedure that suits it:
- oriented rules by checked rewriting to normal form (§2 to §4);
- type-directed laws (eta, surjective pairing, the monad laws) by normalization by evaluation over a typing judgment (§5);
- every other law, such as associativity and commutativity, by equality saturation (§6).
SignatureTheory holds the checked theory and its
procedures, and §8 describes the checked computations generated from the rules.
syntax Lambda
sorts
Term : object
Name : data
constructors
Const : Name -> Term
Lam : (x : Term).Term -> Term
App : Term, Term -> Term
Pair : Term, Term -> Term
Fst : Term -> Term
equations [strategy=innermost]
beta : App(Lam(x. M[x]), N) ~> M[N]
fst : Fst(Pair(A, B)) ~> A
assume terminating
1. Equations over second-order terms¶
Each side of an equation is a term of the syntax with metavariables, in the sense of second-order
equational logic (Fiore and Hur, 2010; Fiore and Mahmoud, 2010). In App(Lam(x. M[x]), N), x. M[x] is the
scope of Lam, M is a metavariable of arity one applied to the bound variable x, and N is a
metavariable of arity zero. M[N] on the right instantiates the scope of M with N: it is
substitution, carried out on de Bruijn terms as in
Tree and graph autoencoders §1.2.1. A bare name is a
nullary constructor when the syntax declares one, a bound variable when an enclosing scope binds it,
and a metavariable otherwise.
Each metavariable gets a sort and an arity from its occurrences: the sort of the position it fills and the sorts of the variables it is applied to. An equation whose metavariable is used at two sorts, or whose sides have different sorts, is refused at the equation. Every metavariable of the right side must occur on the left.
The left side of an oriented rule must lie in Miller's pattern fragment (Miller, 1991): it is not a bare metavariable, and every metavariable in it is applied to distinct bound variables only. Matching a pattern against a term then has at most one solution, and a solution is found by abstracting the term over those variables, so rewriting with such rules is the higher-order pattern rewriting of Mayr and Nipkow (Mayr and Nipkow, 1998).
2. Rewriting and strategies¶
A rule \(l \leadsto r\) rewrites a term \(t\) at a position \(p\) when \(t|_p\) is an instance \(l\theta\) of its left side; the result replaces \(t|_p\) by \(r\theta\). A term no rule rewrites is a normal form. When the rules terminate (§3) and are confluent (§4), every term has exactly one normal form, and two terms are equal in the theory of the rules exactly when their normal forms are identical (Baader and Nipkow, 1998; Hamana, 2017).
The option strategy picks where rewriting happens first. It does not change the normal form of a
confluent terminating system; it changes the work done to reach it.
strategy |
Order |
|---|---|
innermost (the default) |
normalize the arguments of a node, then contract at the node, and normalize the result |
outermost |
contract the leftmost-outermost redex, then start again |
nodewise |
normalize each node as it is built from normal arguments, so a term assembled bottom up, such as the meaning of a derivation, is never stored unreduced |
The host normalizer memoizes normal forms in a table keyed by the term, whose hash is structural and
cached, so a shared subterm is normalized once. The benchmark tests/benchmarks/rewriting_strategies.py
measures the three strategies on the meanings of CCG derivations.
3. Termination¶
Termination is established by a recursive path order (Dershowitz, 1982): a precedence on the constructors under which every left side is greater than its right side. The check searches for such a precedence, lexicographic or multiset, over a first-order reading of each rule, in which a constructor with a scope is an ordinary symbol, every bound variable is one least constant, and a metavariable is a variable.
This reading is sound for rules whose right-side metavariables are applied to bound variables only.
Instantiating such a metavariable renames variables and never copies a term into a scope, so a
decrease in the order on the first-order reading is a decrease on every instance. A rule whose right
side instantiates a metavariable with a term, as beta does, is not ordered by this argument, and
the theory refuses it with the rule's name unless the block declares assume terminating. That
declaration records termination as an Assumed law of the syntax
instead of a Proved one. The law registry thus distinguishes a proved
property from a declared one, and a non-terminating system declared terminating may loop at run
time; the host normalizer bounds the number of steps and raises
RewriteLimitExceeded when the bound is reached.
4. Confluence¶
Two rules overlap when the left side of one unifies with a non-metavariable subterm of the other. Each overlap gives a critical pair: the two results of rewriting the overlapping term, the peak, with either rule. The overlaps are computed by higher-order pattern unification, which is decidable and has most general unifiers on the pattern fragment (Miller, 1991). Every metavariable left unsolved by the unifier is replaced by a fresh generic constant, both results are normalized, and the pair joins when the normal forms are identical.
By the critical pair lemma for pattern rewrite systems, a terminating system is confluent when every critical pair joins (Mayr and Nipkow, 1998). The check therefore records:
- confluence as proved, when termination is proved and every critical pair joins;
- confluence as assumed, when termination is assumed and every critical pair joins, since joinable critical pairs give only local confluence and Newman's lemma needs termination;
- a refusal, when some critical pair does not join. The message names the two rules and prints the peak and the two normal forms, so a non-confluent rule set is reported with its witness.
5. Type-directed laws: normalization by evaluation¶
Eta for functions, surjective pairing for products, and the monad laws relate terms whose shape
depends on their type, such as F = Lam(x. App(F, x)) at a function type. No terminating
orientation of these laws decides them in general, so a theory decides them by normalization by
evaluation (Berger and Schwichtenberg, 1991): a term is evaluated into a semantic domain built from host
functions, pairs, and computations, and read back at its type, which yields its beta-eta-long normal
form. Computations use Filinski's residualizing monad (Filinski, 2001), which validates
the three monad laws.
The structures are recognized from the declared laws, never from constructor names. Each law is
matched against the schemas of a function structure (beta and eta), a product structure (two
projections and surjective pairing), and a monad (two unit laws and associativity), with the
constructors as unknowns. A structure is used only when every one of its laws is declared, since
normalizing with a missing law would identify terms the theory does not. The type former of each
structure is read from the typing judgment the option typing=J names (see
Judgments), and the types of a term's parts come from a derivation of the term in
that judgment. A theory with type-directed laws and no typing option is refused, naming the laws.
6. Laws by equality saturation¶
The remaining laws are decided by equality saturation (Willsey et al., 2021). An
e-graph holds a set of terms closed under congruence; each
law is applied in both directions until no new equality is found or a limit is reached; and a term of
least cost is extracted from the class of the input. The option cost=size sums one per node and
cost=depth takes the deepest path; a theory with saturation laws and no cost option is refused.
Terms with binders are stored in a slotted e-graph (Schneider et al., 2025). A class abstracts the free variables of its terms into slots, so the body of one abstraction placed at two depths is one class, and a class records the slot permutations it is invariant under, so a commutative operation is a symmetry of one class. The backend accepts a law when it is regular and renaming: both sides are patterns whose metavariables are applied to bound variables only, and they use the same metavariables. Instantiating such a law renames and never substitutes a term, which is what slots express. A law outside this class is refused with the property it lacks.
7. Deciding the theory¶
SignatureTheory.normalize applies rewriting,
then normalization by evaluation, then saturation, and repeats until the term no longer changes.
Each step replaces a term by one the theory proves equal, so the result is equal to the input.
SignatureTheory.equal_modulo compares the
normal forms of two terms and, when the theory has saturation laws, also saturates both terms
together.
The procedure is sound: it reports equal only terms that the theory proves equal. It is complete for the theory of a confluent terminating rule set, by §2, and for the type-directed structures over the rules, by the correctness of normalization by evaluation. It is not complete in general once saturation laws are present, since saturation stops at its limit; a pair it cannot relate is reported unequal.
8. Generated computations¶
Oriented rules also compile to checked computations of the module, one set per object sort s of a
syntax Σ, beside the operations of
Tree and graph autoencoders §1.2.1:
| Computation | Type | Meaning |
|---|---|---|
Σ.s.rewrite |
Σ.s -> Σ.s.Step |
contract the root by the first rule, in declaration order, that matches: Σ.s.Step.some(u), or Σ.s.Step.none |
Σ.s.step |
Σ.s -> Σ.s.Step |
under strategy=outermost: contract the leftmost-outermost redex |
Σ.s.normalize |
Σ.s -> Σ.s |
the normal form under the declared strategy |
Σ.s.build.C |
the fields of C -> Σ.s |
under strategy=nodewise: build C from normal arguments and normalize at the root |
Σ.s.equal_modulo |
Σ.s, Σ.s -> Bool |
equality of normal forms |
Matching compiles to nested case analysis on the left side. A repeated metavariable compares its
occurrences with Σ.s.eq, and a metavariable that does not take the binders around it must not
use them, which Σ.s.occurs checks, and is lowered out of them with Σ.s.shift. The right side
instantiates a metavariable with Σ.s.shift and Σ.s.subst.v. The generated computations cover the
rule sets whose left-side metavariables take all of the binders around them, innermost last, or none
of them, over a syntax whose data sorts each hold one type of value, so that terms can be compared. A
syntax whose rules fall outside this fragment declares none of these computations, and its theory is
decided on the host by Program.theories. They agree with the host normalizer on every term under each strategy, which the
test suite checks differentially.
Cited sources¶
Marcelo Fiore and Chung-Kil Hur. Second-Order Equational Logic (Extended Abstract). In Computer Science Logic (CSL 2010), volume 6247 of Lecture Notes in Computer Science, 320–335. Springer, 2010. doi:10.1007/978-3-642-15205-4_26.
Marcelo Fiore and Ola Mahmoud. Second-Order Algebraic Theories. In Mathematical Foundations of Computer Science (MFCS 2010), volume 6281 of Lecture Notes in Computer Science, 368–380. Springer, 2010. doi:10.1007/978-3-642-15155-2_33.
Dale Miller. A Logic Programming Language with Lambda-Abstraction, Function Variables, and Simple Unification. Journal of Logic and Computation, 1(4):497–536, 1991. doi:10.1093/logcom/1.4.497.
Richard Mayr and Tobias Nipkow. Higher-Order Rewrite Systems and Their Confluence. Theoretical Computer Science, 192(1):3–29, 1998. doi:10.1016/S0304-3975(97)00143-6.
Franz Baader and Tobias Nipkow. Term Rewriting and All That. Cambridge University Press, 1998. doi:10.1017/CBO9781139172752.
Makoto Hamana. How to Prove Your Calculus Is Decidable: Practical Applications of Second-Order Algebraic Theories and Computation. Proceedings of the ACM on Programming Languages, 1(ICFP):1–28, 2017. doi:10.1145/3110266.
Nachum Dershowitz. Orderings for Term-Rewriting Systems. Theoretical Computer Science, 17(3):279–301, 1982. doi:10.1016/0304-3975(82)90026-3.
Ulrich Berger and Helmut Schwichtenberg. An Inverse of the Evaluation Functional for Typed Lambda-Calculus. In Proceedings of the Sixth Annual IEEE Symposium on Logic in Computer Science, 203–211. IEEE Computer Society Press, 1991. doi:10.1109/LICS.1991.151645.
Andrzej Filinski. Normalization by Evaluation for the Computational Lambda-Calculus. In Typed Lambda Calculi and Applications (TLCA 2001), volume 2044 of Lecture Notes in Computer Science, 151–165. Springer, 2001. doi:10.1007/3-540-45413-6_15.
Max Willsey, Chandrakana Nandi, Yisu Remy Wang, Oliver Flatt, Zachary Tatlock, and Pavel Panchekha. egg: Fast and Extensible Equality Saturation. Proceedings of the ACM on Programming Languages, 5(POPL):1–29, 2021. doi:10.1145/3434304.
Rudi Schneider, Marcus Rossel, Amir Shaikhha, Andrés Goens, Thomas Kœhler, and Michel Steuwer. Slotted E-Graphs: First-Class Support for (Bound) Variables in E-Graphs. Proceedings of the ACM on Programming Languages, 9(PLDI):1888–1910, 2025. doi:10.1145/3729326.