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 · 2026

Read Lean Refactor: Multi-Objective Controllable Proof Optimization via Agentic Strategy Search by Jialin Lu, Soonho Kong, Rodrigo Stehling, Kaiyu Yang, Zhangyang Wang, Weiran Sun, Wuyang Chen (2026) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2605-20244.txt.

Introduction

Jialin Lu and six co-workers present Lean Refactor, a plug-in framework for multi-objective refactoring of Lean proofs. LLM-made proofs are often right but wordy and brittle across library versions. Lean Refactor tackles challenges: proofs balance length, cost, and version fit. It guides a frozen agentic LLM using retrievals from strategies, tagged with versions and compile-cost cuts. Tests show over seventy percent compression on benchmarks, over twenty percent on repos, and up to sixty percent compile-time cuts. Filtered retrieval lifts compression on the target version.

Catalog record

CREATORJialin Lu, Soonho Kong, Rodrigo Stehling, Kaiyu Yang, Zhangyang Wang, Weiran Sun, Wuyang Chen
YEAR2026
COLLECTIONPAPERS
FORMATTXT
FILE SIZE97531
LICENSEhttp://creativecommons.org/licenses/by/4.0/
RIGHTSCC BY
SOURCEarXiv

Source: https://arxiv.org/abs/2605.20244

Browse the full library · About the Tower of Babel

Full text

The opening of the work, from the library's hosted text:

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 Abstract We present Lean Refactor , a plug-and-play retrieval-augmented agentic framework for multi-objective, controllable, and version-robust refactoring of Lean proofs. LLM-generated proofs are notoriously correct-but-verbose and brittle across library versions, yet existing refactoring works overlook three practical challenges : 1) Lean refactoring is natively multi-objective (proof length, compilation cost, and version compatibility are often in tension); 2) Lean repositories have fragile compatibility, whereas LLM releases are unaware of Lean/Mathlib versions; 3) Training-based pipelines require repeated fine-tuning with each new LLM release, scaling neither with model churn nor with Lean’s release cycle. Lean Refactor steers a frozen agentic LLM with retrievals from a curated database of multi-objective refactoring strategies , each densely annotated with metadata such as supported Lean/Mathlib versions and expected compilation-cost reduction. Experiments show over 70% token-level compression on competition benchmarks, over 20% on research repositories, and up to 60% compilation-time reduction , outperforming prior work and Claude Code. Version-filtered retrieval further improves compression on the target Lean version, and refactored miniF2F proofs exhibit stronger zero-shot version transfer to future Lean releases than their unrefactored counterparts. 1 Introduction Lean and Mathlib de2015lean ; moura2021lean enable collaborative, machine-verified mathematics and provide a strong reinforcement learning testbed that grounds large language model (LLM) reasoning in formal proofs, supporting gold-medal International Mathematical Olympiad (IMO) performance trinh2024solving ; achim2025aristotle and rapid progress in LLM-powered provers alphaproof ; xin2024deepseek ; xin2024deepseekv15 ; ren2025deepseek ; lin2025goedel ; lin2025goedelproverv2scalingformaltheorem . However, LLMs trained with reinforcement learning (RL) often produce Lean proofs that are correct but notoriously long and hard to read ahuja2024improver ; gu2025proofoptimizer . Because RL rewards target proof correctness, models are insensitive to redundant steps or heavy automation where simple steps suffice. Such proofs are difficult for humans to understand and computationally expensive to compile, posing particular problems for large libraries like Mathlib. Recent work on LLM-based code refactoring gautam2025refactorbench ; gong2025language pursues either fine-tuning LLMs gu2025proofoptimizer ; yang2025perfcoder ; gong2025tuning or agentic frameworks built on pretrained general-reasoning LLMs ahuja2024improver ; depalma2024exploring ; ospanov2025apollo ; piao2025refactoring ; oueslati2025refagent ; karabiyik2025refactorgpt ; he2025swe ; cui2025large ; bai2025polo ; zhao2025semopt ; wu2025fasterpy . However, LLMs have seen limited adoption for refactoring Lean proofs, and we point out three key limitations . First, Lean refactoring is natively multi-objective. Lean code is often simultaneously optimized along several axes, such as proof length and compilation cost. These objectives are entangled and frequently in tension: shorter proofs may invoke heavier tactics that inflate compilation cost, while cheaper proofs may sprawl in length. Standard pretraining, RLHF, and fine-tuning ouyang2022training ; achiam2023gpt offer no principled mechanism to balance such trade-offs, let alone the unseen objectives users routinely introduce at inference time. Figure 1 : Our Lean Refactor can be controlled to refactor Lean proofs for multiple objectives: proof length, compilation time cost, and compatibility across Lean versions. Second, frontier LLMs are misaligned with the Lean/Mathlib release cycle. Lean and Mathlib evolve on a near-weekly basis, with lemmas renamed, APIs restructured, and new automation landed, while LLMs are pinned to a knowledge cutoff. Even with web-search augmentation, no principled way exists for maintainers to pair a codebase version with a compatible model , leading to pervasive hallucination of stale tactics and removed APIs. Third, paired refactoring data is scarce. Compared with mainstream languages (C/C++, Java, Python), Lean has a far smaller codebase, and existing Mathlib and community-aggregated competition corpora minif2f ; putnam ; li2024numinamath provide neither refactoring trajectories nor noisy-clean code pairs. To address these fundamental bottlenecks, we investigate our core question: Can a frozen LLM be steered at inference time to balance competing refactoring objectives while reliably refactoring code in fast-evolving Lean/Mathlib environments? In this paper, we introduce Lean Refactor , a retrieval-augmented agentic framework that externalizes refactoring knowledge into a structured database. Lean Refactor grounds an agentic LLM with a curated corpus of multi-objective, maintainable refactoring strategies . Each retrieved strategy supplies the LLM with aligned structural context and explicit execution instructions ( when to apply , how to apply , and examples ). Crucially, these strategies are densely annotated with refactoring metadata , tracking signals such as expected compilation-cost reduction and validated Lean/Mathlib version compatibility. By dynamically filtering and reranking these annotated strategies at inference time, our framework steers proof optimization toward user-specified objectives without model fine-tuning, and remains version-robust as Lean/Mathlib evolves beyond the LLM’s knowledge cutoff. We summarize our contributions below: 1. We release the largest Lean refactoring dataset to date: over 200K long-short proof pairs and 9K refactoring strategies, each annotated with compilation-cost reduction and compatible Lean/Mathlib versions. 2. Lean Refactor achieves over 70% compression on competition proofs (miniF2F minif2f , PutnamBench putnam , Putnam2025) and over 20% on research repositories (PFR pfr_formalization , PhysLean physlib_lean , Analysis tao_lean_analysis , FLT FLT_Lean ), outperforming prior work and Claude Code while remaining compatible with Gemini, Claude, and GPT series backbones. 3. Lean Refactor supports multi-objective, controllable refactoring: beyond length, it cuts compilation cost by over 30%, holds compression steady across Lean versions, and produces miniF2F proofs that type-check across more future Lean/Mathlib releases than their unrefactored counterparts. 2 Lean Refactoring is Challenging for LLMs Figure 2 : Weak correlation between proof length and compilation time. Token length fails to predict compilation cost because of short, heavy tactics, a factor often overlooked in previous Lean refactoring research. Beyond the redundancy in code length demonstrated in previous papers ahuja2024improver ; gu2025proofoptimizer , in this section, we motivate our work with more practical challenges of Lean code refactoring with LLMs. Proof Optimization is Multi-Objective. While proof length is the most convenient refactoring objective, as it can be computed during LLM inference, compilation cost is an equally important complexity metric, and the two are not always aligned (Figure 2 ; see Appendix D for an example). Prior Lean refactoring work either ignores compilation cost or swaps it in as a substitute inference-time objective, rather than jointly optimizing it with length ahuja2024improver ; gu2025proofoptimizer . More broadly, while recent formal-math post-training increasingly relies on verifiable rewards (e.g., compiler-judged correctness), continuous runtime signals such as compilation cost remain underused lin2025goedelproverv2scalingformaltheorem ; gu2025proofoptimizer . Figure 3 : Knowledge cutoff lag in LLMs: Most LLMs misalign with Lean/Mathlib versions due to static knowledge cutoff. Lean Repository Compatibility is Fragile. Version sensitivity is a long-standing bottleneck of Lean/Mathlib: Lean and Mathlib evolve rapidly; weekly releases frequently rename declarations and break imports even across patch versions baanen2025growing , forcing downstream projects to pin to Mathlib’s exact toolchain mathlib_breakage 1 1 1 In a Zulip thread on mathlib-dependent projects, the requirement is stated bluntly: “It needs to be the exact same version.” See this discussion . . Agentic LLMs, however, are never aligned with a specific version: as Figure 3 shows, every recent frontier model’s training cutoff lands before recent Mathlib releases, leaving newer APIs unseen and the model’s coverage of older versions opaque. This pushes maintainers into continuous, costly upgrades. Lean Refactor mitigates this misalignment by grounding the agent in a strategy bank whose strategies carry explicit Mathlib-version metadata: retrieval is filtered to the user’s target toolchain, and the bank itself can be re-profiled

Read the complete text