arXiv:2610.34205cs.PLOct 2026

Manuscript Offprint

arXiv:2610.34205

PastaLean: Deterministic transpilation from Python to Lean for Verification

Anirudh Gupta, Petros Markopoulos, Siddhartha Gadgil

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

We present PastaLean, an extensible system for deterministic translation from Python to Lean via the Abstract Syntax Tree of Python. This allows for verification of Python code, especially auxiliary code used by AI. Every program is translated into a provable twin written in the form Lean's verification tooling can reason about, and a runnable twin that gives a fast and correct computation support. We also introduce our Type Inference engine TypeInfer which provides static typing support for untyped Python code. Our tool provides coverage of more than 92% for LeetCode and HumanEval +^{+} and more than 98% for LiveCodeBench ground truth Python files. Further, at least 97% of each dataset's (numerical) test cases pass, with 100% on HumanEval +^{+} and LiveCodeBench. The TypeInfer engine is the best-scoring tool on the TypeEvalPy static leaderboard, and on a repository-level TypyBench benchmark it scores above every competing repository-level inference tool.

1 Introduction

The rapid evolution of large language models (LLMs) has transformed them from systems primarily designed for natural-language understanding into increasingly capable programming agents that can generate, reason over, and manipulate executable code [18]. Agentic Artificial Intelligence (AI) systems make extensive use of Python for calculations, numerical experiments, data analysis, and other tasks. However, if the generated code is incorrect, the conclusions drawn from it will also likely be incorrect. Formal verification promises something stronger, a machine-checked guarantee that a program meets a specification on all inputs. Yet the languages in which such guarantees are routinely established such as Lean 4 [7], Coq, Dafny [24], F* [52], Verus [21] are not the languages practitioners actually use. The code that needs checking is usually written in Python.

To address this issue, we introduce PastaLean 1^{1}, a system that provides deterministic translation from Python to Lean. This enables verification of Python code.

PastaLean implements a recursive, extensible framework for transpiling Python code to Lean. Python code is first parsed into an Abstract Syntax Tree (AST) using Python's built-in ast module. The AST is then traversed recursively, and each node is translated into JSON using a set of translation functions with dynamic dispatch. The JSON is then converted into Lean code recursively using an extensible collection of translation functions based on Lean meta-programming. Each of these functions has

1^{1}The name “PastaLean” is derived from Py + AST + Lean.

Preprint.

output Typed Syntax in Lean, so the compiler ensures that the output is syntactically correct. Further, a front end is used to verify that the output compiles.

The specification is written as a contract in the Python source itself, by a developer or by a model, and the same deterministic translator turns it into a Lean theorem alongside the program. We write contracts in Python rather than directly in Lean because both authoring and auditing a contract is easier in Python than in Lean. Contracts are meant to be written by agentic loops as well as developers, and both are considerably more capable in Python than in Lean, especially smaller models; a contract authored directly in Lean is also harder to check against the intended property, since doing so means reading Lean rather than the language the developer already knows. Writing the contract in Python instead and passing it through the same fixed, independently verified translator used for the program body means that trusting the resulting Lean theorem reduces to trusting one translation step we have already vetted, rather than trusting freeform Lean text directly.

To ensure correctness of the Python code, it is crucial that the translation is deterministic – an LLM may translate incorrect Python code to correct Lean code. The verification depends on the correctness of the translation code, but this is a fixed, open-source code base that can be verified independently. We also use differential testing to enhance trust in the translation. Further, an error in translation is highly unlikely to translate incorrect Python code to correct Lean code, and our main goal is to ensure that auxiliary code used by LLMs is correct.

Agentic AI systems typically generate and run a lot of exploratory code, only a small fraction of which is used in the final output. Only the code that is used in the final output needs to be verified. This means the verification adds only a small overhead to the overall computation. Furthermore, verification and exploration can be parallelized, so it does not slow down the overall computation.

Our use of Python, rather than directly generated Lean code, is motivated by its familiarity to both developers and LLMs. An alternative approach is to use a system like Dafny to generate code directly in a verified language. However, in that case if a specification is mistranslated, the generated code may be correct with respect to the specification, but incorrect with respect to the intended behavior.

Contributions. These include: A deterministic framework for translating Python into Lean, emitting two definitions per function: a provable twin in the form Lean's verification tooling reasons about, and a computable twin that runs at speed and agrees with CPython; Verification machinery for Python, in which contracts written in the source become Hoare-triple [15] theorems over the translated program; TypeInfer, a high performance type inference engine for untyped Python whose lattice and fixpoint are machine-checked in Lean; and a type checker that shows its work, emitting for every file a certificate of the evidence behind each verdict, re-checkable by a validator that runs none of the inference engine and backed by a Lean theorem saying what an accepted certificate means.

Limitations We do not support all of Python during transpilation. Further, our proof automation is best-effort and may need manual interventions or may fail. Limitations are stated where they arise: Sections 2.2, 2.6; Appendices A.3, A.9, A.11, A.12, A.13, B.3, B.5, B.8, B.9, C.3, C.6, D.1, D.3, D.1.1, D.4.

Related Work. The work closest to PastaLean is Aeneas, which translates safe Rust, via the LLBC intermediate representation, into pure functional models for verification in Lean and backends [14]. PastaLean follows the same broad strategy of verifying source programs through functional translation. However, Python lacks the static type and ownership information exploited by Aeneas and requires support for dynamic typing, mutation, exceptions, and its object model. The rocq-of-python project also translates Python into a proof assistant, targeting Rocq and using a dedicated representation of Python semantics [10]. PastaLean instead produces verification-friendly shallow embeddings in Lean, allowing generated definitions to use Lean and Mathlib [56] libraries directly.

Other systems verify Python without translating it into an interactive theorem prover. Nagini translates statically typed Python into Viper and uses SMT-based automation for modular verification [9], while CrossHair uses symbolic execution to search for counterexamples to Python contracts [47]. Dafny takes the complementary approach of writing programs directly in a verification-aware language [24]. Strata, also built on Lean 4, instead compiles a Python front end through its intermediate Laurel language into verification conditions discharged by SMT solvers [51]. PastaLean is distinguished by starting from ordinary Python and producing Lean code through a deterministic, extensible translator.

Inferring types for (untyped) Python is a classical problem: whole-program inference underpins the Python compilers Starkiller [46] and Shed Skin [8] and PyPy's RPython annotator [44], all descendants of Agesen's Cartesian Product Algorithm [1], while recent work is based on learning rather than analytic [33] or call-graph driven [48]. TypeInfer differs in being written and proved inside the prover it feeds. Recent LLM-based systems further motivate this design. PyVeritas uses an LLM to translate Python to C before applying bounded model checking [37], while VeriBench evaluates agents that generate Lean implementations, specifications, and proofs from Python [34]. In PastaLean, translation of the executed program is deliberately kept outside the LLM: the specification and proof may come from a person or a model, drawing on the growing body of work on autoformalization and neural proof search [17, 43, 62, 64], but a fixed translator preserves the connection to the original Python code, and Lean checks the resulting proof.

References (38)