PastaLean: Deterministic transpilation from Python to Lean for Verification
Code produced by AI agents is mostly Python, while formal verification is done in languages such as Lean 4. Python's dynamic typing, mutable state and object model make translating it into a verifiable form difficult.
PastaLean parses Python with the built-in ast module, serializes the nodes to JSON, and recursively generates typed Lean syntax using extensible translation functions registered in a Lean environment extension. The Lean code is a shallow embedding with two twins: a provable twin for verification and a computable twin for execution. Contracts written in Python are translated into Lean theorems. A type inference engine, TypeInfer, supplies static types for untyped Python.
The generated Lean elaborates for 92.1% of HumanEval+, 92.4% of LeetCode and 98.5% of LiveCodeBench programs. Test agreement with CPython on elaborated programs is 100%, 97.0% and 100% respectively. TypeInfer scores best on the TypeEvalPy static leaderboard and on the repository-level TypyBench benchmark, and the translator outperforms direct LLM translation into Lean.
| Dataset | Problems | Elaborated (%) | Tests | Passed (%) |
|---|---|---|---|---|
| HumanEval+ | 164 | 151 (92.1) | 7,007 | 7,007 (100.0) |
| LeetCode | 2,589 | 2,391 (92.4) | 215,126 | 208,736 (97.0) |
| LiveCodeBench | 476 | 469 (98.5) | 10,213 | 10,213 (100.0) |
