TL;DR
This paper introduces $P^3$ (Joint Program-and-Proof Planning), an agentic workflow that prompts Large Language Models (LLMs) to plan an executable program and its mathematical correctness proof jointly before writing any code. By ensuring structural alignment from the start, $P^3$ avoids the brittle, expensive code-repair loops common in traditional sequential pipelines. Evaluated across multiple benchmarks—including a newly introduced real-world library benchmark, Lean4Commit0—$P^3$ consistently achieves state-of-the-art verification success rates while significantly lowering API costs and execution time.
What Problem Does This Paper Address?
Verified code generation is the holy grail of software engineering: given a formal specification, an LLM synthesizes both an executable program and a machine-checkable proof ensuring the code behaves exactly as intended.
Historically, AI agents have tackled this sequentially: first write the program, then try to prove it correct. However, the authors observe that this "program-then-proof" pipeline creates a severe bottleneck. A program written without anticipating its proof might be subtly incorrect or structurally nightmarish to verify. When the verifier fails, the LLM gets trapped in a frustrating, token-consuming loop of patching code and proofs post-hoc.
As Dijkstra famously argued, programs and their correctness arguments should be developed hand in hand. This paper brings that philosophy to LLM agents.
Methods
The authors propose $P^3$, a two-stage agentic workflow implemented in Lean 4:
- Joint Program-and-Proof Planning: Before writing concrete code, the agent generates a high-level plan ($\rho$). This plan outlines the program's decomposition (recursion variables, branch structures) alongside its corresponding proof obligations (induction strategies, invariants, and necessary auxiliary lemmas). The agent informally validates this plan, ensuring the code sketch and proof sketch structurally support one another.
- Plan-Guided Elaboration: Conditioned on the frozen plan, the agent fleshes out the actual Lean 4 implementation and proof scaffold. Gaps are left as
sorryplaceholders and filled locally using feedback from the Lean verifier.
If local repairs fail repeatedly, the agent recognizes a plan-level failure, retreats to step one, and completely replans rather than endlessly tweaking flawed code.
Datasets
The framework was evaluated on three benchmarks:
- Verina & AlgoVeri: Existing benchmarks focused on textbook-style algorithms and single-function verification tasks.
- Lean4Commit0 (New): A novel benchmark curated by the authors to test verified code generation in production-like environments. It consists of tasks extracted from 108 real-world open-source libraries across Python, Rust, C/C++, and Java. It features 511 implementation and 519 theorem placeholders, notably including relational specifications that constrain the joint behavior of multiple APIs (e.g., verifying that a
setandgetconfiguration sequence maintains correct precedence).
Key Insights and Ideas
- Code Structure Dictates Proof Complexity: Two programs can compute the exact same output but yield vastly different proof burdens. For instance, computing a list's maximum via structural recursion allows a straightforward inductive proof. Doing the same via an accumulator fold requires discovering a complex, universally quantified invariant. Joint planning allows the LLM to foresee and choose the path of least resistance.
- Decoupling Strategy from Implementation: High-level reasoning (planning invariants and decomposition) is separated from low-level mechanics (writing tactic scripts). This allows the workflow to potentially offload the constrained elaboration phase to cheaper subagents.
- Relational Coordination: In real-world libraries, APIs do not exist in a vacuum. Joint planning is uniquely effective for relational specs because it forces the agent to co-design multiple functions so they successfully interface with a shared theorem.
Key Results
The authors evaluated $P^3$ using four frontier LLMs: Codex-GPT-5.5, Gemini-3-Pro, Claude-Sonnet-4.6, and Claude-Opus-4.7.
- Higher Solve Rates: $P^3$ achieved the highest solve rate in every single benchmark-model combination, outperforming the primary baseline by 4.6 to 11.2 percentage points.
- Resource Efficiency: On the most difficult 25% of tasks, $P^3$ reduced per-task API token costs by up to ~40% and wall-clock execution time by up to ~37% due to fewer full-restart repair loops.
- Value of Joint Planning: A targeted ablation study comparing $P^3$ against an implementation-only planner (PLAN-SEQ) showed that planning the proof alongside the program yields an absolute success boost of 3.3 to 8.3 percentage points.
Limitations and Future Work
- Substrate Scope: The current implementation is tightly coupled with Lean 4. While the principle applies to SMT-backed verifiers (like Dafny or Verus) or other interactive provers (Rocq/Coq), its empirical effectiveness there remains to be proven.
- Benchmark Biases: Lean4Commit0 tasks are heavily skewed toward Python-origin libraries and focus primarily on core APIs rather than full-scale repository compilation or long-horizon dependencies.
- Open-Weight Models: Evaluation was restricted to closed-source frontier models; whether the planning advantages transfer effectively to smaller or specialized open-weight theorem-proving models is an open question.
Summary updated