P³ plans code and proof together before writing either, lifting Lean solve rates by up to 11 points

P$^{3}$: Joint Program-and-Proof Planning for Verified Code Generation

Zenan Li, Ziran Yang, Peiyang Song, Zhaoyu Li, Kaiyu Yang

cs.AI, cs.PL

2026-08-10

P³ plans code and proof jointly before writing either. First in all 12 benchmark-model cells, +4.6–11.2 points over the stronger baseline, up to 40% cheaper on hard tasks.

What problem this solves

Verified code generation asks an LLM to produce both an executable program and a machine-checkable proof that it meets a formal specification. Once Lean 4's kernel accepts both, the program carries a correctness guarantee no finite test suite can give.

The de facto workflow is sequential: write the program first, then try to prove it. The catch is that a program written without its proof in mind can be structurally hard to verify. The paper's listMax example is typical. A structural-recursion version admits an induction that mirrors the postcondition directly; the shorter foldl version forces the agent to first prove a lemma generalized over an arbitrary accumulator, something the specification never mentions. Once the fold version is committed, the agent either rediscovers that invariant under a fixed implementation or throws the work away, and the repair loop thrashes between code edits and proof edits.

Method

P³ moves the coupling between program structure and proof structure to before any code is written. The principle is Dijkstra's: a program and its correctness argument should be developed hand in hand. Two stages:

Failures split into two classes. Errors that localize under the current plan get repaired in place; when repeated repairs stall or Lean shows the bridging predicate is too weak, the agent retreats to planning and revises. The plan is the single point of retreat, which is what stops the code-edit/proof-edit thrashing. Planning runs on the main model, and constrained elaboration steps can go to cheaper subagents; the Lean kernel keeps final authority.

Results

Four frontier backends (Codex-GPT-5.5, Gemini-3-Pro, Claude-Sonnet-4.6, Claude-Opus-4.7), three benchmarks (Verina with 189 tasks, AlgoVeri with 77, Lean4Commit0 with 108 libraries), $30 and 180 minutes per task. P³ takes the top solve rate in all 12 benchmark-by-model cells, +4.6 to +11.2 points over the stronger baseline.

SettingStronger baselineP³Gain
Verina + Gemini-3-Pro60.872.0+11.2
AlgoVeri + Claude-Opus-4.740.348.1+7.8
Lean4Commit0 + Claude-Opus-4.717.622.2+4.6

On the hardest quartile of tasks, P³ is also the cheapest and fastest method in every cell: cost down 3.0–39.6%, wall-clock time down 3.1–37.2%. An ablation against PLAN-SEQ, which plans implementation choices but is barred from anticipating invariants or proof structure, gives joint planning another 3.3 to 8.3 points. In the left-leaning red-black-tree deletion case, the sequential baseline wrote 6,344 lines without closing; P³ compared both sketches, chose the rebuild route, and finished in 1,105 lines in about 34 minutes.

Why it matters

For anyone building formal-verification agents, this is an orthogonal workflow improvement: same models, no extra training, only the order of thinking changes, and the gains hold across all four backends while cutting cost and time. The broader pattern, aligning two strongly coupled artifacts at the sketch level before elaborating either, looks transferable to other agent tasks with paired outputs.

Lean4Commit0 fills a real gap. Prior benchmarks draw mostly from textbook algorithms and competitive programming; this one extracts core APIs from 108 real open-source libraries (65 Python, 17 Rust, 15 C/C++, 11 Java), totaling 511 implementation and 519 theorem placeholders, with relational specifications that tie multiple APIs together. Specs pass a three-stage quality gate (reference satisfaction, mutation rejection, LLM review) with a mean score of 90.7%. The difficulty is honest: the best setting solves 22.2%, so library-level verified generation is far from practical.

Limitations

The authors' own list: evidence is limited to Lean 4, closed-source backends, Python-skewed APIs, imperfect specification gates, and single runs with no within-task variance; 92.9% of successful Verina traces kept their initial plan, and the authors concede retention is only a proxy for how well the planner predicts proof burden.

Reading closely adds more. Absolute solve rates on Lean4Commit0 sit at 9–22%, so the 4.6-point gain rides on a small base. The difficult subset is defined by a top-25% cost cut, which leaves room for selection effects. Library specs were translated into Lean with LLM help, so a bad spec can slip through the gates. And $30 per task is not cheap at scale.

Terms

Source

What people are saying

Related papers

All paper explainers