Proof-Refactor: Refactoring Generated Formal Proofs into Modular Artifacts

Yiming Fu, Peixuan Liu, Zichen Wang, Kun Yuan

TL;DR

Proof-Refactor is an agentic framework designed to transform messy, monolithic AI-generated formal proofs into clean, modular, and human-readable mathematical artifacts. By decomposing the refactoring process into four distinct stages—extraction, design, proving, and repair—it moves beyond simple "proof golfing" to produce library-quality code that is easier to maintain and reuse.

What Problem Does This Paper Address?

Large Language Models (LLMs) are increasingly capable of generating formal proofs in languages like Lean that successfully compile. However, these "verified" proofs are often the mathematical equivalent of spaghetti code: they are long, repetitive, and rely on heavy automation that obscures the underlying logic.

While existing tools focus on "golfing"—shortening a proof to its minimum length—shorter isn't always better. In formal mathematics, quality is defined by modularity (breaking logic into reusable lemmas) and readability (aligning with library conventions like Lean’s mathlib). The authors address the gap between a proof that merely "passes the compiler" and one that a human mathematician would actually want to read or use.

Methods

The authors introduce a four-phase agentic pipeline that decouples high-level mathematical reasoning from low-level interaction with the Lean proof assistant:

  1. Extraction: The system identifies meaningful proof fragments and uses a custom extract tactic to lift them into "scaffolds"—temporary top-level theorems that record local assumptions and goals.
  2. Helper Design: An external model (Gemini 3.1 Pro) analyzes these scaffolds to design clean, generalized helper declarations. Instead of just copying the code, it attempts to create "library-style" signatures that could be useful in other contexts.
  3. Proving: A Claude-based agent proves these new helper lemmas. This is usually straightforward because the original proof fragment already contains the necessary logic.
  4. Proof Repair: The original monolithic proof is rewritten to call the newly verified helpers, and the temporary scaffolds are removed.

The framework is built on Claude Code and lean-lsp-mcp, allowing the agent to inspect file structures, search for existing library declarations, and resolve Lean error messages in real-time.

Datasets

The researchers evaluated Proof-Refactor on two primary datasets of university-level mathematics:

  • PutnamBench: 96 proofs generated by LLMs, ranging from 50 to 500 lines of code.
  • Putnam2025: 12 high-quality proofs generated by the Axiom Math Team. The evaluation used an "LLM-as-a-judge" protocol with a detailed rubric, supplemented by human review to ensure the scores reflected genuine mathematical quality.

Key Insights and Ideas

  • Process Over Proxy: Instead of optimizing for a single proxy metric like proof length, the authors argue for a process-guided approach. By mimicking the workflow of human formalizers, the system naturally produces better structure.
  • The Power of Decoupling: A major "aha moment" was the necessity of separating high-level architectural decisions (what should be a lemma?) from low-level engineering (how do I fix this syntax error?). When a single agent tries to do both, it becomes "overburdened" and produces lower-quality signatures.
  • Generalization is Quality: A key finding was that Proof-Refactor doesn't just move code around; it abstracts it. For example, it might turn a specific step in a topology proof into a general lemma about metric spaces, making the proof more robust and aligned with mathematical best practices.

Key Results

Proof-Refactor significantly outperformed the strong lean4-skills baseline across several qualitative dimensions:

  • Overall Quality: Improved average scores by +0.31 on Putnam2025 and +0.45 on PutnamBench.
  • Signature Quality & Readability: These saw the largest gains (+0.68 and +0.50 respectively), proving the system's ability to create natural-looking mathematical statements.
  • Efficiency vs. Golfing: Interestingly, while the system often shortened proofs (achieving a 6.5% word reduction on PutnamBench), it actually increased the word count on Putnam2025. This highlights that the system prioritizes clarity and modularity over raw compression.

Limitations and Future Work

  • Scope: The current study is limited to self-contained Lean files. It does not yet handle multi-file projects or the complex task of editing existing library declarations.
  • Sequential Fragility: Because the pipeline is sequential, a failure or "bad scaffold" in the extraction phase can propagate through the later stages, making repair difficult.
  • Cost: Using four sequential phases and multiple external model calls makes this approach more computationally expensive than a single-pass generation.
  • Future Directions: The authors suggest extending the framework to handle project-level refactoring and better integration with mathlib naming conventions, with the ultimate goal of making AI-generated proofs ready for inclusion in major formal math libraries.

Summary updated

BibTeX
@inproceedings{fu2026proof,
  author = {Yiming Fu and Peixuan Liu and Zichen Wang and Kun Yuan},
  title = {{Proof-Refactor: Refactoring Generated Formal Proofs into Modular Artifacts}},
  booktitle = {AI4Math @ ICML},
  year = {2026},
  url = {https://openreview.net/forum?id=FjD3rZPEET},
}
Figure