Compressing and Optimizing Large-Scale Formal Proofs: A Readability and Verification Guide
This article examines why AI-generated formal proofs can balloon to millions of lines and how engineering teams can shrink them safely. It breaks down the common causes of proof bloat, presents a four-layer compression strategy from mechanical cleanup to dependency convergence, and argues that readability must be treated as an explicit constraint alongside size. It also covers the non-negotiable role of machine verification, statement fingerprinting, and reproducible builds, plus a relay-style collaboration pipeline for large-scale proof reduction. The key takeaway: how small a proof can get is determined by structure, not line count.
1. Why a Proof Balloons to Millions of Lines
A recent, representative case in the technical community involved a formal proof of the Generalized Riemann Hypothesis generated with the help of an AI coding tool. The initial version exceeded 17 million lines. Through a relay of pruning efforts by developers worldwide, it was cut by roughly 2.4 million lines within two days, followed by successive versions of about 300,000, 76,000, and 52,000 lines. Alongside these, there were a 300-plus-page natural-language version, a 40-plus-page condensed natural-language version, and several versions labeled as “conditional refactorings” or “working drafts.”
One clarification is essential up front: a drop in line count does not mean mathematical content was deleted. The vast majority of the reduction comes from engineering-level deduplication, abstraction, and reuse. Understanding this is the prerequisite for every optimization step that follows.
Proof bloat is usually the result of several overlapping factors:
- Inlined duplication: The same lemma is expanded inline in hundreds or thousands of places instead of being referenced.
- Naming and namespace redundancy: Long prefixes and automatic numbering generated to avoid collisions add large amounts of low-information characters and lines.
- Automation tactic residue: Automated reasoning leaves behind many intermediate steps and tentative branches, while the final proof needs only a tiny fraction of them.
- Type and coercion noise: Explicit conversions and helper declarations inserted just to make type checking pass.
- Unconverged dependencies: External theorems are not aligned to a single version, so the same conclusion ends up with multiple near-duplicate implementations.
2. A Layered Compression Strategy: From Mechanical Cuts to Structural Reuse
Treating compression as a single action is a common mistake. A practical approach is to proceed in layers, each with a different goal and a different risk profile.
The first layer is the mechanical layer. This includes deleting commented-out dead code, merging blank lines, removing unused declarations, and standardizing formatting. This layer carries almost zero risk and typically yields noticeable but limited gains.
The second layer is the deduplication layer. The core idea is to identify “isomorphic fragments” and replace repeated expansions with references to a single lemma. A schematic approach looks like this:
# Pseudocode: merging isomorphic fragmentssignature = normalize_ast(fragment) # Normalization: variable renaming, whitespace removalbucket[signature].append(fragment)for sig, group in bucket where len(group) > 1:lemma = synthesize_lemma(sig) # Generate a reusable lemmareplace_all(group, reference(lemma))
The third layer is the abstraction layer. This lifts concrete constructions into parameterized structures—for example, rewriting an argument about a specific constant into a theorem that holds for any object satisfying a given set of axioms, then instantiating it back to the original problem. This layer offers the largest gains but is also the most likely to introduce semantic drift, so it must be guarded by both the type checker and regression comparison.
The fourth layer is the dependency convergence layer. This unifies external theorem versions, locks the dependency graph, and eliminates near-duplicate implementations. This step often determines how small the proof can ultimately get, because cross-version duplicate declarations frequently account for a significant share of the remaining volume.
3. Readability Is the Hard Constraint
One key point from the case deserves emphasis: the 52,000-line and 76,000-line versions were actually harder to read than the 300,000-line version. The reason is that compression often comes at the cost of structure—highly parameterized, auto-generated lemma chains are extremely unfriendly to human readers.
It is therefore advisable to make a “readability budget” an explicit engineering constraint:
- Layered delivery: Maintain both a “machine-optimal version” and a “human-readable version” that share the same core and are derived through a generation pipeline.
- Proof skeleton first: Fix the tree-shaped skeleton of theorem dependencies first, then fill in the leaves one by one, so readers do not get lost in details.
- Natural-language cross-reference: Pair each major lemma with a natural-language explanation, forming bidirectional documentation that maps formal and informal descriptions.
- Use page count and structural metrics together: Page count is only a rough proxy; structural measures such as “number of major lemmas,” “maximum dependency depth,” and “average length per theorem” deserve more attention.
4. Trust: Compression Cannot Bypass Verification
The biggest risk in relay-style collaboration is that “the proposition is quietly changed during compression.” Engineering practice should hold to three bottom lines:
- Machine checking as the sole arbiter: Any reduced version must pass type checking and kernel verification; anything that fails is excluded from the candidate set.
- Statement fingerprint comparison: Compute a normalized hash of the final target statement to ensure all versions are exactly identical, not just “seemingly the same.”
- Reproducible builds: Pin toolchain versions and dependency lockfiles so any historical version can be rebuilt with one command, avoiding “it only passes on my machine.”
In addition, for versions labeled “conditional refactoring” or “working draft,” their internal assumptions and unaligned external references must be clearly marked to prevent them from being mistaken for complete proofs and spread as such.
5. Collaboration Workflow: Turning a Relay into a Pipeline
The large-scale pruning was completed within two days not through individual heroics but through workflow design. Reusable practices include:
- Task partitioning: Assign work leaf-first according to the dependency graph, avoiding multiple people editing the same abstraction layer at once.
- Small-step merges: Each merge performs a single, semantically clear action, making it easy to bisect and locate problems.
- Continuous verification: Wire checking, fingerprint comparison, and size statistics into continuous integration to produce visualized trend curves.
- Size dashboard: Record line counts, declaration counts, and dependency counts at each stage, turning “how much smaller can it get” into an observable question.
If general-purpose cloud capabilities are used to carry this workflow, it typically involves object storage for saving version artifacts, a container platform for running isolated verification tasks, a message queue for distributing compression tasks, and logging and monitoring/alerting services for tracking failure rates and durations. These are all industry-standard categories and do not imply any specific implementation constraint.
6. Conclusion: Structure, Not Line Count, Sets the Floor
Back to the original question—how small can it ultimately get? The answer depends on three variables: how high the abstraction layer can be lifted, whether dependencies can converge to a single version, and how much readability budget is left. Mechanical reduction has a clear lower bound; structural reuse has no fixed lower bound but cannot proceed indefinitely, because over-parameterization strips a proof of its checkability and explainability.
The takeaway for practitioners is clear: treat “smaller,” “more readable,” and “more trustworthy” as a three-objective optimization problem, advance with a layered strategy, back it with machine verification, and amplify the scale effect through workflow-based collaboration. This methodology is not limited to mathematical proofs; it applies equally to any ultra-large-scale code or rule system generated by AI tools and requiring long-term maintenance.
