OPAL: Optimizing Proofs at Library Scale via Metric-Driven Coding Agents
Chengwu Liu, Zijin Feng, Ye Yuan, Yifang Qin, Botao Li, Jiaxuan Xie, Siqi Li, Pu Suo, Haoli Bai, Yichun Yin, Lifeng Shang, Ming Zhang
cs.AI
Nov 30, 2026 · v1
cs.LO
TL;DR
Introduces a benchmark and contract checker for repository-level optimization of Lean blueprint projects by coding agents, verified with lake build and leanchecker.
Abstract
Driven by rapid progress in autoformalization and automated theorem proving, formal mathematics is scaling up: mathematicians, often with AI assistance, advance large formalization projects around Lean blueprints, which tightly link a LaTeX proof plan to the Lean codebase. As mathlib and proof automation evolve, finished proofs accumulate optimization opportunities. Yet even in the mature mathlib library, 41% of commits add no new mathematics but improve existing proofs (shortening, deduplication, faster compilation), and 62% of these span multiple files, beyond the reach of theorem-level methods. We introduce OPAL, the first repository-level, metric-driven proof-optimization task, where an agent freely refactors a blueprint repository toward repository-level objectives. We instantiate OPAL with six metrics grounded in empirically mined human maintenance tendencies, including proof length, import closure, mathlib duplication, and elaboration cost. Optimizing these metrics freely, however, invites reward hacking, such as deleting a theorem to shorten the code, so evaluation needs a contract on what must stay intact. Our key observation is that blueprint structure itself supplies this contract: \lean{} links pin down which theorems constitute a project's final targets (5-19% of all declarations), whose statements must be preserved, while the remaining 81-95% of auxiliary code may be freely refactored. We implement this contract as an executable semantic-preservation checker. Building on it, we release OPAL-Bench, comprising 162 task instances from 18 real-world Lean blueprint projects, together with a sandboxed agent harness. Experiments show that coding agents can deliver genuine optimization gains beyond the level of single human commits, while ablations show that with the contract absent from the agent's world nearly half of accepted improvements do not survive semantic verification.
Problem
Mature Lean libraries accumulate maintenance work. In post-port mathlib, 41% of commits improve existing proofs rather than add new mathematics, and 62% of those span multiple files. Existing proof-optimization methods work only at the theorem level, and unconstrained metric optimization invites reward hacking.
Approach
The authors mine 33,222 mathlib commits to identify six maintenance tendencies. From these they derive six repository-level metrics: code tokens, elaboration heartbeats, import closure, mathlib duplication, deprecated references and blueprint deviations. Blueprint \lean{} links pin the target theorems, and an eight-rule semantic-preservation contract checks them: statement fingerprints, definition closure, axiom/sorry counts, kernel replay and compilation. OPAL-Bench draws 162 instances from 18 Lean blueprint projects, combining historically mined commits with current in-the-wild technical debt.
Results
A Claude Opus 4.8 coding agent produces contract-passing optimizations on 75.3% of instances. Without the contract in the agent's environment, 48% of accepted improvements are fabricated. On the 30-instance Lite subset, Opus passes 26/30, DeepSeek v4-pro 14/30 and Kimi k2.7-code 5/30.
| Condition | Accepted | Fabricated | Genuine | Med. ΔR |
|---|
| Full | 26/30 | 0 (0%) | 26 | -20.3% |
| Advisory | 27/30 | 2 (7%) | 25 | -12.5% |
| None | 27/30 | 13 (48%) | 14 | -32.6% |
Contract ablation on OPAL-Bench Lite