Lean Refactor: Multi-Objective Controllable Proof Optimization via Agentic Strategy Search

Jialin Lu, Soonho Kong, Rodrigo Stehling, Kaiyu Yang, Zhangyang Wang, Weiran Sun, Wuyang Chen

TL;DR

Lean Refactor is a plug-and-play, retrieval-augmented agentic framework that optimizes formal Lean proofs for multiple objectives, such as length, compilation speed, and version compatibility. By steering "frozen" large language models (LLMs) with a curated database of annotated refactoring strategies, it achieves over 70% proof compression and significantly improves the resilience of proofs against the rapid release cycles of the Lean ecosystem.

What Problem Does This Paper Address?

In the world of formal mathematics, the Lean theorem prover is a gold standard. While LLMs have become remarkably good at generating correct Lean proofs, they suffer from three major practical issues:

  1. Verbosity: LLM-generated proofs are often "correct-but-verbose," filled with redundant steps that make them hard for humans to read and computationally expensive to compile.
  2. Objective Tension: Shorter proofs aren't always better. Sometimes a "short" proof uses a "heavy" tactic that actually makes compilation much slower. Existing tools usually only optimize for one metric at a time.
  3. Version Fragility: Lean and its mathematical library, Mathlib, evolve almost weekly. LLMs, which are pinned to a specific training data cutoff, often hallucinate stale tactics or removed APIs, causing proofs to break when the library updates.

Lean Refactor moves away from the expensive "fine-tune a new model for every update" approach, instead using an agentic framework that can be updated simply by refreshing its external knowledge base.

Methods

The authors introduce a three-part system designed to work with any high-performing "frozen" LLM (like Gemini, Claude, or GPT-4):

  • Densely Annotated Strategy Bank: The core of the system is a database of over 9,000 unique refactoring strategies distilled from 200,000 "long-short" proof pairs. Each strategy is annotated with metadata: how much it reduces length, how much it speeds up compilation, and which versions of Lean it supports.
  • Multi-Objective Retrieval: When a user wants to refactor a proof, the system doesn't just look for "similar" code. It uses a retrieve-and-rerank pipeline. If a user says, "I want a proof that compiles fast," the system filters for strategies known to reduce compilation time.
  • The Agentic Loop: The framework uses a three-role agent loop:
  • Planner: Analyzes the proof and the retrieved strategies to create a step-by-step optimization plan.
  • Refactorer: Executes the plan, rewriting specific segments of the proof.
  • Debugger: If the new proof fails to compile, the debugger uses compiler feedback to fix errors in a localized way.

Datasets

The researchers evaluated Lean Refactor across two distinct regimes:

  • Competition Benchmarks: miniF2F, PutnamBench, and the new Putnam2025. These consist of self-contained math competition problems.
  • Research-Level Repositories: To test "real-world" complexity, they sampled theorems from four major Lean projects: Analysis (Tao), FLT (Fermat’s Last Theorem), PFR (Polynomial Freiman-Ruzsa Conjecture), and PhysLean. These are significantly harder because they rely on a dense web of external library dependencies.

Key Insights and Ideas

  • Length $\neq$ Speed: One of the most striking findings is the weak correlation between proof length and compilation time (Pearson correlation < 0.13). A single "short" line of code can trigger a massive search in the Lean compiler, making it 20x slower than a 130-line explicit proof.
  • Retrieval over Fine-Tuning: By externalizing refactoring knowledge into a strategy bank, the system remains "future-proof." When a new version of Lean is released, you don't need to retrain the LLM; you just re-profile the strategy bank.
  • Synergy of Planning and Retrieval: The authors found that a "Base Agent" gets overwhelmed by long proofs. The "Planner" is critical because it breaks the task into manageable chunks, while "Retrieval" provides the specific tactical knowledge the LLM might lack due to its training cutoff.

Key Results

  • Compression: Lean Refactor achieved over 70% token-level compression on competition benchmarks and over 20% on complex research repositories.
  • Efficiency: It reduced compilation time by up to 60%, outperforming specialized baselines like ProofOptimizer.
  • Version Transfer: Refactored proofs were significantly more robust. For example, on the miniF2F benchmark, proofs optimized by Lean Refactor were more likely to continue working ("type-check") across future releases of Mathlib compared to the original unrefactored proofs.
  • LLM-Agnostic: The framework proved effective across different backbones, even improving the performance of general-purpose coding agents like Claude Code when given access to the strategy retrieval tool.

Limitations and Future Work

  • Research-Level Versioning: While the system handles versioning well for self-contained problems, research-level projects are often "pinned" to very specific toolchains due to deep dependencies. Creating version-portable benchmarks for these complex projects remains an open challenge.
  • Joint-Effect Attribution: Currently, the system uses median speedups for strategies. The authors suggest that using more complex attribution methods (like Shapley values) to understand how multiple strategies interact could further improve optimization.
  • Dependency on the Bank: The system's power is tied to the quality and diversity of the Strategy Bank. While 9,000 strategies is a lot, extremely niche mathematical domains might still require manual strategy entry.

Summary updated

BibTeX
@inproceedings{lu2026lean,
  author = {Jialin Lu and Soonho Kong and Rodrigo Stehling and Kaiyu Yang and Zhangyang Wang and Weiran Sun and Wuyang Chen},
  title = {{Lean Refactor: Multi-Objective Controllable Proof Optimization via Agentic Strategy Search}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=qwMCaWPqxQ},
}
Figure