AI Agents Formalize CPython in Lean and Report 418 Bugs
David Andrews and Chao Zhang report that AI agents rewrote 4,071 CPython 3.15.2 functions in Lean, producing 613,318 lines, 18,898 theorems and lemmas across 1,539 files. Each rewrite was differential-tested against real Python on tens of thousands of inputs, then checked by a separate agent and Lean’s proof checker. The project also generated 418 bug reports, including about 55 cases said to be capable of crashing the interpreter or corrupting memory.
The project demonstrates a large-scale, AI-assisted formal-verification workflow in which machine-checked proofs are also used to uncover edge-case defects in production software.
AI agents wrote 613,318 lines of Lean that copy Python's internals line by line, tested every piece against the real Python, had a computer check every proof, and turned up 418 bug reports in Python along the way.
CPython · Objects/rangeobject.c
static unsigned longget_len_of_range(long lo, long hi, long step){ assert(step != 0); if (step > 0 && lo < hi) return 1UL + (hi - 1UL - lo) / step; else if (step < 0 && lo > hi) return 1UL + (lo - 1UL - hi) / (0UL - step); else return 0UL;}
Lean copy · RangeSlice/Model.lean
def getLenOfRange (lo hi step : Int) : Int := if step > 0 ∧ lo < hi then uadd 1 (usub (usub (toU hi) 1) (toU lo) / toU step) else if step < 0 ∧ lo > hi then uadd 1 (usub (usub (toU lo) 1) (toU hi) / usub 0 (toU step)) else 0 -- step ≠ 0 is assumed by every proof-- uadd, usub: C's unsigned arithmetic,-- which wraps around past 2^64
A function from CPython's C source (left) and the Lean copy an agent wrote of it (right). Hover over a line to see its counterpart.