Agentic Verified Optimization of Real-World Software
Zhe Ye, Udaya Ghai, Zhizhen Qin, Dawn Song, Soonho Kong
cs.SE
Oct 20, 2026 · v1
cs.AI cs.LG cs.LO cs.PL
TL;DR
Agents write faster Lean 4 implementations with kernel-checked equivalence proofs against reference functions, including C/C++/Go/Rust programs migrated into Lean.
Abstract
Program optimization aims to improve performance while preserving behavior. Coding agents can already make programs faster by changing algorithms, data structures, and execution strategies. However, existing equivalence checks mostly rely on tests, which agents have been shown to exploit, passing the tests without preserving the intended behavior. We propose agentic verified optimization, in which an agent produces a faster implementation together with a machine-checked proof of equivalence to the existing one. Because the existing implementation serves as the specification, the task avoids the specification-writing bottleneck of verified code generation, and recent advances in theorem proving make the proofs attainable. We formulate the task at the function and repository levels in Lean 4, where programs can be both executed and reasoned about. Few real-world applications are written in Lean, so we develop Aqueduct, an agentic framework that migrates C, C++, Go, and Rust programs into native Lean, validates each port by differential testing against its original. On the 189 reference implementations in Verina, agents produce faster proved rewrites for 156, including algorithmic changes such as replacing exponential recursion with dynamic programming. On 17 migrated applications, their 92 proved rewrites speed up 47 of 51 application scenarios. These application-level gains are smaller than the function-level ones, making the choice of functions to optimize an important problem. Current agents can thus already carry out verified optimization on both standalone functions and applications migrated from real programs. We discuss the remaining challenges and the steps toward verified optimization in production.
Problem
Coding agents can speed up programs, but correctness is usually checked with tests. Agents can exploit those tests and produce faster code that does not preserve the intended behavior.
Approach
The task is posed as agentic verified optimization in Lean 4. An agent produces a faster replacement f' together with a machine-checked proof that f = f', with the existing implementation serving as the specification. It is evaluated at function level on all 189 Verina reference implementations. For repository level, the authors build Aqueduct, an agentic framework that migrates C, C++, Go, and Rust programs into native Lean and validates each port by differential testing against the original.
Results
Agents (Codex with GPT-5.6) produce faster proved rewrites for 156 of 189 Verina functions, with a geometric mean speedup of 1.851x. On 17 migrated applications, 92 proved rewrites speed up 47 of 51 scenarios, with a geometric mean of 1.120x over the migrated Lean ports. Application-level gains are smaller than function-level gains.
| Suite | Targets | Improved | Geom. mean | Median |
|---|
| Basic | 108 | 93 | 1.959x | 1.109x |
| Advanced | 81 | 63 | 1.716x | 1.163x |
| All | 189 | 156 | 1.851x | 1.117x |
Function-level verified optimization on Verina