Proof-Refactor Turns Ugly AI Math Proofs Into Human-Readable Code
Proof-Refactor is a four-phase agentic system that turns monolithic LLM-generated Lean proofs into readable, modular, library-quality artifacts
ProTakeaways
- The problem: LLM-generated Lean proofs compile but are monolithic, unreadable, and hard to reuse -- a side effect of training purely on correctness signals.
- The solution: Proof-Refactor is a four-phase agentic framework that extracts sub-proofs, designs named helper lemmas, proves them independently, and rewires the original proof.
- Key results: Outperforms a Claude Code refactoring baseline on rubric-based scores across PutnamBench and Putnam2025, with the biggest gains in signature quality and human readability.
- Novel approach: Uses a custom
lean_extracttactic to surgically lift tactic blocks into standalone theorems, grounded in the Lean compiler at every step. - Limitation: A post-processing tool only -- requires an already-correct Lean proof as input and depends on external models for sub-goal proving.
- Open source: Code is available at github.com/pelicanhere/proof-refactor.
LLMs have gotten surprisingly good at generating formal proofs in proof assistants like Lean. But there is a catch: the proofs they produce tend to compile and nothing more. They are tangled, monolithic scripts that a human mathematician would never write, let alone want to maintain or reuse. A new paper from researchers at Southern University of Science and Technology and Peking University proposes Proof-Refactor, an agentic framework that takes those working-but-ugly proofs and restructures them into something closer to what you would find in a mature math library.
The compile-first trap
Formal theorem proving with proof assistants such as Lean produces machine-verifiable mathematical proofs. This is useful for verified mathematics and for training LLMs, since proof checking provides a precise reward signal for reinforcement learning. The problem is that this same reward signal creates a perverse incentive.
Proof-assistant verification induces a narrow objective: a generated proof is rewarded primarily for compiling. This binary feedback ensures correctness, but it does not by itself encourage readability, modularity, maintainability, or library integration. The result is what the authors call the "compile-first" trap: the compile-first objective implicit in most proof-generation pipelines encourages monolithic or ad hoc proof scripts rather than library-quality artifacts.
In practice, LLMs trained with reinforcement learning often produce Lean proofs that are correct but notoriously long and hard to read. Prior work tried to fix this, but ran into a measurement problem: the most tractable and experimentally validated objectives are largely length-based, while higher-level qualities such as readability, modularity, maintainability, and reusability are difficult to reduce to reliable automatic metrics.
A process-guided answer
Instead of chasing a single proxy metric, the Proof-Refactor team took a different angle: they take a process-guided approach inspired by human proof-refactoring workflows. The idea is that the right way to improve a proof is not to optimize a score, but to follow the same steps a skilled mathematician would take when cleaning up someone else's draft.
The framework decomposes proof refactoring into four phases: extracting candidate proof fragments, designing helper declarations, formally proving the extracted and designed components, and repairing the original proof using the verified components. Each phase is handled by a dedicated agent that calls into the Lean compiler for feedback, making the whole pipeline grounded in formal verification at every step.

How it works, step by step
The four phases map cleanly onto what a human refactoring session looks like:
- Extraction. A custom Lean tactic called
lean_extractscans the proof and identifies self-contained sub-proofs -- tactic blocks that either close a goal or transform it -- and lifts them out as candidate standalone theorems. The tactic is designed for two main cases: when the tactic block closes the goal or leaves a definitionally equal goal, and when it leaves one remaining goal that is not definitionally equal. The extracted theorem keeps only the variables actually used by the tactic block and removes unused variables. - Helper Design. An LLM agent reviews the extracted fragments and designs formal helper declarations -- named lemmas and definitions -- that give the extracted logic a clean, reusable interface. The resulting helper declarations are inserted into a Lean file, and each declaration is annotated with dependency information to organize the proof. Scaffolds are temporary: no other declaration is allowed to depend on them.
- Proving. Each helper declaration is proved independently by an external model, with Mathlib search available to find relevant library lemmas. This keeps the sub-proofs short and grounded in existing mathematical infrastructure.
- Proof Repair. The original proof is rewritten to call the newly verified helpers, replacing the inlined logic with clean references. The Lean compiler validates the final result end-to-end.

What it was tested on
The evaluation used two challenging benchmarks of competition-level mathematics. PutnamBench consists of formalizations of problems sourced from the William Lowell Putnam Mathematical Competition, the premier undergraduate-level mathematics competition in North America. PutnamBench consists of 1337 hand-written formalizations across three proof assistants, and aims to benchmark the next generation of theorem-proving algorithms for competition mathematics. Putnam2025 is the freshest slice of that benchmark, covering problems from the most recent competition.
These are not easy problems. The problems require the clever application of a wide range of undergraduate topics, including abstract algebra, analysis, number theory, geometry, linear algebra, combinatorics, probability, and set theory. The proofs being refactored were generated by state-of-the-art LLM provers, so the starting material was already correct -- just not clean.
The results
On generated Lean proofs from PutnamBench and Putnam2025, Proof-Refactor improves rubric-based refactoring scores over a strong Claude Code refactoring baseline, with the largest gains in signature quality and human readability. The rubric-based evaluation is key here: rather than measuring proof length, the team scored proofs on dimensions like how well-named and typed the helper signatures are, and how easy the resulting proof is to follow as a human reader.
These results suggest that process-guided refactoring can improve proof structure without treating proof length as the primary objective. That distinction matters: a shorter proof is not always a better proof, and Proof-Refactor is explicitly designed to optimize for structure and clarity rather than character count.
Limitations and what it does not solve
A few important caveats are worth noting:
- Proof-Refactor is a post-processing step. It assumes you already have a working Lean proof to refactor -- it does not help you find proofs in the first place.
- The rubric-based evaluation, while more principled than length, still relies on LLM judges scoring qualities like readability. That introduces its own measurement uncertainty.
- The framework depends on external models for the helper-proving step, so its success rate is bounded by the underlying prover's capability on sub-goals.
- LLMs may generate correct formal proofs that can often be opaque or unintuitive, reducing their explanatory value for human users -- and Proof-Refactor addresses structure but cannot fully substitute for mathematical insight in the naming and framing of lemmas.
The real shift here
The broader significance of this work is not just the tool itself -- it is the framing. The field has spent years optimizing for proof correctness. Proof-Refactor is part of a growing recognition that correctness is necessary but not sufficient for proofs that are actually useful: proofs that can be reviewed, extended, and built upon by humans and future models alike.
The machine learning community has primarily treated formalization as a benchmark for automated reasoning, optimizing for whether a proof successfully compiles, not whether it can be used to help someone understand a written argument. Proof-Refactor pushes back on that framing by treating proof quality as a software engineering problem -- one that benefits from structured, process-driven refactoring rather than metric optimization. The code is available on GitHub, making it straightforward to plug into existing Lean proof generation pipelines.