AI & Formal Verification
New framework automatically streamlines Lean proofs across library versions
Lean Refactor uses AI agents to compress proofs and boost compatibility with different Lean library releases without needing model retraining.
Researchers have introduced Lean Refactor, a retrieval-augmented system that optimizes formal proofs for both readability and compatibility across different Lean library versions. The approach tackles a persistent gap in using large language models for mathematical proof writing: generated proofs tend to be unnecessarily long, computationally expensive to compile, and fragile when underlying libraries update.
The core problem#
LLM-generated proofs have accuracy advantages but suffer from two interrelated weaknesses. First, they often contain redundant steps that inflate token count and compilation time without adding logical value. Second, they break unpredictably when the Lean theorem prover or its standard library (Mathlib) changes versions—a particularly thorny issue since language model releases and Lean updates follow unrelated schedules.
Existing refactoring methods typically rely on fine-tuning models with each new release, a costly approach that doesn't scale alongside the rapid churn in both LLM capability releases and Lean ecosystem changes.
How the system works#
Rather than retraining, Lean Refactor freezes an LLM agent and steers it through retrieval-augmented generation from a curated database of refactoring strategies. Each strategy in the database carries metadata describing which Lean and Mathlib versions it supports, along with expected improvements to compilation time. The agent can then apply version-aware tactics tailored to the target environment.
This design separates concerns: the underlying language model remains constant, while version-specific knowledge lives in the retrieval layer.
Demonstrated results#
In benchmarks testing across competitive programming and research repositories, Lean Refactor achieved:
- Over 70% token-level compression on competition problem sets
- 20%+ compression on real-world research codebases
- Up to 60% reduction in compilation time
The method outperformed prior refactoring approaches and Claude Code's built-in optimization. Critically, proofs refactored by the system showed stronger zero-shot transfer to future Lean versions compared to their unoptimized originals—suggesting the compression process actually makes proofs more resilient to library evolution.

