Formalizing CPython

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.

David Andrews and Chao Zhang9 November 2026Code and data

CPython · Objects/rangeobject.c

static unsigned long
get_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