Summary (Overview)
- Core contribution: The paper presents a tool for detecting duplicate theorem statements in Lean libraries by using a terminating rewrite system with eight rules that normalize theorem statements modulo binder permutation, currying/uncurrying, and structural isomorphisms of dependent types.
- Key innovation: The method combines a syntactic fragment of Lean's type theory (Tm), a graph-based representation with named pointers (G), and a rewrite system (R) that is proven confluent modulo an isomorphism relation (Iso), with all metatheory formally verified in Lean itself.
- Scalability result: The tool was evaluated on 286,264 theorem statements from Lean core, Batteries, and Mathlib, finding 1,747 redundant proofs in 1,687 duplicate classes, with the run taking about half an hour.
- Practical impact: The tool produces kernel-checked equivalence certificates for every identified pair, establishing structural equivalence without referencing either theorem's proof, and identifies 873 maintenance candidates not labeled as deliberate API twins.
- Comparison to existing tools: The approach finds 58.6% more independent pairs than Mathlib's existing duplicate-declarations linter, which only compares syntactically after sorting top-level binders.
Introduction and Theoretical Foundation
The paper addresses the growing problem of technical debt in Lean libraries, particularly duplicate theorems. As Mathlib has grown to over 2.2 million lines of Lean, and AI-generated formalizations (e.g., Anthropic's Fermat's Last Theorem at 13 million lines) become larger, detecting redundancy becomes critical. The authors note that proving arbitrary types equivalent is undecidable in general, so they focus on a deliberately narrow subset of type equivalences that can be found by terminating syntactical rewriting.
The theoretical foundation builds on:
- Complete axiomatizations of provable isomorphism in typed λ-calculi [8, 11, 13]
- Their use in proof assistants [5]
- Extensions to dependent products [20]
- Basin's work on the connection between term equality with commutative structure and graph isomorphism [6]
The key insight is that the relation they compute is strictly finer than logical equivalence—two statements are identified only if a fixed set of isomorphism schemata and definitional unfoldings, applied at proposition positions, rewrites both to the same normal form up to renaming and reordering.
Term Language and Graphs
The term language Tm is a syntactic fragment of Lean covering terms, types, and propositions, with de Bruijn indices for local binders and named pointers to graph nodes. The design is similar to [8] and [21], handling only propositions: a ∀ whose body is a proposition is opened by the rules, while a Π whose codomain is not a proposition is kept as an inert former.
Graphs G are defined as finite products of nodes, where a product is a telescope with named members and pointers between them:
Two graphs are related by Iso if a bijective renaming of names followed by finitely many steps (Swap, Flip, FlipEq) turns one into the other. These steps apply at any nesting depth but never inside a label.
Methodology
The Rewrite System R
The eight rules of R (Figure 4) form the core of the normalization procedure:
- Pi: Lifts a binder out of a label into the surrounding product while choosing fresh names
- Sigma: Decomposes existential types (∃) and conjunctions (∧) into product members
- Conn: Turns ∨, ↔ into nodes for commutativity
- Eq: Turns equations into nodes for commutativity
- Unit: Removes ⊤ (true)
- ArrowMerge: Flattens nested arrows (representing currying)
- ArrowUnit: Removes empty domains
- ArrowErase: Removes arrows with an empty conclusion
The rules apply only to members that no pointer references (i.e., hypotheses used elsewhere are never split), preserving well-formedness. The system is proven terminating via a measure on spine formers and weights.
Example Normalization
For the running example, two statements about divisibility:
After unfolding to and applying R, both normalize to graphs related by permuting domain members:
Metatheory Proved in Lean
The authors formalize Tm, R, G, and Iso in Lean, proving:
- Termination of R on all graphs (without well-formedness hypothesis)
- Iso is a simulation for R
- Confluence modulo Iso in the single-source form (via Newman's argument threaded with Iso)
- Well-formedness is preserved by every R-step
- Decidability of Iso with a reference decider proved total by structural recursion
- The bucketing key is invariant under Iso
All theorems depend only on standard axioms (propext, Classical.choice, Quot.sound).
Implementation Pipeline
The tool's pipeline (Figure 5) consists of:
- Unfolding: Unfold definitions annotated as reducible or instanceReducible (e.g., ), avoiding over-unfolding to prevent term explosion
- Encoding: Embed statements into Tm, then build initial graphs in G
- Normalization: Apply R to normalize
- Bucketing: Use a color-refinement signature (from the 1-dimensional Weisfeiler–Leman algorithm) to form buckets of potentially isomorphic graphs
- Isomorphism decision: Within each bucket, use the proved Iso decision procedure, first checking a witness proposed by a fast matcher, falling back to the complete reference decider
- Certification: Build kernel-checked equivalence proofs from generic transfer lemmas (Iff.refl, Iff.trans, Iff.symm, logical constants, and readbacks of intermediate graphs)
The color refinement assigns colors to nodes based on their labels with pointers blanked, then iteratively refines by hashing old colors with neighbor colors. The resulting signature is order-independent for telescope members.
Empirical Validation / Results
Evaluation Summary
The tool was run on 286,264 theorem statements from Lean core, Batteries, and Mathlib. Key results from Table 1:
| Metric | Count |
|---|---|
| Theorem statements extracted | 286,264 |
| Encoded and normalized within size budget | 286,224 |
| Over budget, never compared | 40 |
| Duplicate classes (≥2 members) | 7,525 |
| Members in these classes | 15,834 |
| Pairs (representative, member) with certificates | 8,309 |
| Member definitionally equal to representative | 6,293 (76%) |
| Member not definitionally equal | 2,016 |
| Independent pairs | 1,742 |
| Redundant proofs | 1,747 in 1,687 classes |
| For definitionally equal statements / equal only through Iso | 965 / 782 |
| Not labeled as deliberate API twins | 873 in 844 classes |
Example Findings
Orientation twin (same file, 31 lines apart):
-- Init/Data/List/Nat/Range.lean
theorem map_zipIdx {f : α → β} {l : List α} {k : Nat} :
map (Prod.map f id) (zipIdx l k) = zipIdx (l.map f) k := by
induction l generalizing k <;> simp_all
theorem zipIdx_map {l : List α} {k : Nat} {f : α → β} :
zipIdx (l.map f) k = (zipIdx l k).map (Prod.map f id) := by
induction l with
| nil => rfl
| cons hd tl IH =>
rw [map_cons, zipIdx_cons', zipIdx_cons', map_cons, map_map, IH, map_map]
rfl
Cross-file pair (identified after re-associating existential and conjunction, reordering hypotheses): Two theorems about tensor products expressing that equal images in the ambient tensor product imply equal images in some larger finitely generated submodule—an example requiring comparison to account for dependencies as well as binder order.
Comparison with Mathlib's Linter
Of the 1,742 independent pairs found:
- 481 (27.6%) found by Mathlib's DuplicateDecls linter
- 241 (13.8%) excluded by the linter's filters
- 1,020 (58.6%) missed entirely
Among the 1,020 missed pairs: 527 are diagnosed as orientation changes, 316 are definitionally equal despite different keys, and 177 involve binder order, unfolding, currying, annotations, or other rewrites.
Theoretical and Practical Implications
Theoretical Significance
The paper demonstrates that a terminating rewrite system with eight rules can find relevant duplicate theorems that a definitional equality check misses. The formal verification of the metatheory (termination, confluence modulo Iso, decidability) directly on the definitions used by the tool ensures correctness without trusting the implementation.
The approach deliberately avoids the complexity of a full theory of type isomorphisms, instead providing:
- Per-pair certificates: Each identified pair comes with a kernel-checked equivalence proof built from generic transfer lemmas, establishing structural equivalence without referencing either theorem's proof
- Soundness by construction: The certificates would type-check even if the statements were unproved, witnessing structural equivalence rather than mere provability
Practical Implications
- Library maintenance: The tool can be integrated into workflows for technical debt tracking, helping maintainers identify proof reuse opportunities
- Scalability: Processing 286,264 statements in ~30 minutes demonstrates practical library-scale detection
- Complementary to existing tools: The approach extends beyond syntactic comparison to structural transformations, catching 58.6% more independent pairs than Mathlib's existing linter
- Naming inconsistencies: The findings expose variations in binder order, auto-implicit binder usage, and inconsistencies in theorem naming conventions
Limitations
The relation is strictly finer than logical equivalence. Not identified include:
- Distributivity of ∀ and → over ∧
- Idempotence
- De Morgan laws
- ↔ as two implications
- The one-point rule
- Unfolding of plain definitions (e.g., Function.Injective)
- Permutation of universe parameters
- Symmetry of HEq
A negative verdict means only that R and Iso do not identify the two encodings, not that the statements are inequivalent.
Conclusion
The paper presents a tool for detecting duplicate theorem statements through structural rewrites and dependency-preserving binder permutations. Key takeaways:
- Machine-checked metatheory: The rewrite system and graph-isomorphism decision procedure have formally verified termination, confluence modulo Iso, and decidability properties
- Library-scale detection: Evaluation on 286,264 theorem statements demonstrates practical scalability
- 873 maintenance candidates were identified as worth inspecting for proof reuse and clearer connections between declarations
- Kernel-checked certificates for every reported pair establish statement equivalence without referencing either theorem's proof
Future directions:
- Integrating certified duplicate reports into library maintenance workflows (e.g., Mathlib's technical debt tracking)
- Extending supported schemata beyond propositions to general type isomorphisms
- Finding not just equivalences but also specializations or implications (e.g., if is proved, a proof of might also be redundant)
The authors note that deciding which proofs to share or declarations to retain requires careful human review—the certificates establish statement equivalence, but the maintenance decisions remain a human judgment call.
Related papers
- An upper bound of 61 for the Ramsey number $R(3,3,3,3)$
The paper proves every 4-coloring of K61 contains a monochromatic triangle, improving the Ramsey number R(3,3,3,3) upper bound from 62 to 61 via SAT solving and Lean verification.
- A Lean Formalization of Hamilton's Three-Manifold Theorem (Chow, Liao, Qin)
Lean formally verifies Hamilton's 1982 theorem that closed 3-manifolds with positive Ricci curvature admit constant positive sectional curvature metrics, using 1.9 million lines of code.
- Long-horizon autoformalization of a core theorem underlying MIP*=RE (FormalFlow)
FormalFlow, a human-supervised multi-agent framework, machine-checked the quantum soundness of LIDT in Lean 4 within 63 days, uncovering 5 errors in the published proof.