WybeCoder: Verified Imperative Code Generation

Fabian Gloeckle, Mantas Baksys, Darius Feher, Kunhao Zheng, Amaury Hayat, Sean B. Holden, Gabriel Synnaeve, Peter O'HearnarXiv 2026

TL;DR

WybeCoder is a novel multi-agent framework that bridges the gap between automated SMT solvers and interactive theorem provers to automatically generate and formally verify imperative code. By co-evolving code, loop invariants, and proofs in a hybrid Lean 4 environment, it achieves state-of-the-art verification rates on complex algorithms like Heapsort, overcoming performance plateaus seen in prior fully automated or purely interactive methods.

What Problem Does This Paper Address?

While Large Language Models (LLMs) have dramatically advanced automated code generation and mathematical theorem proving, formal software verification remains a stubborn bottleneck. Existing approaches typically fall into two camps, both with significant drawbacks:

  1. Functional Verification in Interactive Theorem Provers (ITPs): Purely functional code (like in Lean or Coq) is mathematically elegant to prove but introduces performance overheads—such as linear or logarithmic array access times—known as the "Log N penalty."
  2. Auto-Active Verification of Imperative Code: Imperative code uses mutable state and arrays for constant-time operations, which matches real-world software. However, verifying it usually relies entirely on automated SMT solvers (like Dafny). When an SMT solver fails, it provides opaque error messages, leaving the LLM agent with no clear path to debug or refine its loop invariants.

Furthermore, existing benchmarks suffer from specification and functional leakage, where an LLM can "cheat" by wrapping a purely functional library call inside a thin imperative wrapper, trivializing the verification task without actually generating real imperative code.

WybeCoder addresses these challenges by creating a hybrid verification system for true imperative code that gracefully falls back to interactive proof tactics when automated solvers hit their limits.

Methods

WybeCoder introduces a subgoal-decomposition multi-agent architecture built on top of the Loom/Velvet verification framework inside the Lean 4 theorem prover. Velvet allows the agent to write standard imperative syntax (loops, mutable variables) alongside Hoare-logic annotations (pre-conditions, post-conditions, and loop invariants).

The core workflow operates as follows:

  • Subgoal Decomposition: Instead of trying to verify an entire program at once, WybeCoder uses Lean's metaprogramming utilities to extract individual proof obligations (e.g., loop entry, loop preservation, loop exit) as independent mathematical theorems.
  • Hybrid Verification Loop: It dispatches these subgoals to parallel prover agents. The agents first attempt to solve them using the CVC5 SMT solver. If CVC5 fails, the system falls back to interactive Lean tactics (like grind, omega, or simp_all).
  • Conflict-Driven Refinement: If a subgoal is determined to be unprovable, it signals a conflict, prompting the agent to modify the implementation or strengthen the loop invariants.
  • Proof Transfer: To prevent wasting compute, WybeCoder uses a deterministic naming scheme for invariants, allowing successful subgoal proofs from previous iterations to be matched and reused even after the underlying code is modified.
  • Robust Leakage Mitigation: To prevent the model from cheating via functional shortcuts, the authors implement an LLM-based Imperativeness Judge that enforces strict algorithmic rules (e.g., ensuring only constant-time primitives are used on critical execution paths).
Figure 1. WybeCoder: Subgoal decomposition multi-agent system. Starting from a problem specification, the agent generates an imple- mentation and attempts to discharge verification conditions using CVC5. Remaining goals are tackled interactively in Lean, driving iterative implementation refinement or completing the proof.

Datasets

To evaluate imperative code generation systematically, the authors translated two prominent functional verification benchmarks into Velvet/Loom specifications:

  • Verina-Loom: Automatically translated from the Verina benchmark, preserving its structured metadata (separated function signatures, pre-conditions, and post-conditions) to maintain strict comparability.
  • Clever-Loom: Translated from the Clever benchmark using a semi-automated pipeline involving Gemini 2.5 Pro translation followed by rigorous human review to correct original dataset specification errors.

Key Insights and Ideas

  • The Power of the Hybrid Approach: Pure SMT-based verification often plateaus because the agent cannot interpret solver failures. By exposing the remaining goals interactively within Lean, WybeCoder allows the agent to apply targeted mathematical tactics, unlocking continuous scaling with compute budget.
  • Proof Reuse is Essential for Scale: Code generation is highly iterative. The ability to anchor subgoal proofs to specific named invariants means the agent doesn't have to re-prove the entire program from scratch every time it fixes a minor bug.
  • The Illusion of High Performance: Without an imperativeness judge, models achieved a nominal success rate of 75.1% on Verina by exploiting functional leakage. Enforcing true imperative constraints dropped the score to 51.9%, highlighting how critical strict environment design is for evaluating real-world coding capabilities.

Key Results

WybeCoder achieves substantial performance gains over previous state-of-the-art verification frameworks:

  • Benchmark Success: Utilizing Claude 4.5 Opus, WybeCoder successfully solved 74.1% of Verina-Loom tasks and 62.1% of Clever-Loom tasks, vastly outperforming prior baselines like COPRA (8.7%).
  • Consistent Compute Scaling: Unlike previous methods that hit performance ceilings, WybeCoder shows a steady, near-linear improvement in success rate as the computational budget scales up to thousands of LLM calls.
Figure 2. Inference scaling for sequential agents and multi- agent system on Verina. Different pass@k settings entail different compute-performance scalings for sequential agents which we compare to a multi-agent system. The optimal mechanism depends on the specific model in use, with Gemini-3 benefiting from the multi-agent scaffold while Claude 4.5 Opus does not.
  • Full Verification of Heapsort: In a notable qualitative triumph, the framework successfully generated and fully verified an in-place, array-based Heapsort algorithm. The final verified implementation spanned approximately 2,000 lines of Lean code and required managing dozens of subgoals over hundreds of LLM calls—a task that typically takes human verification experts multiple days to complete.

Limitations and Future Work

  • Experimental Toolchain: The framework relies heavily on Loom/Velvet, which is an actively developing, experimental package inside the Lean 4 ecosystem.
  • Managed Memory Constraints: WybeCoder targets Lean's managed memory environment. It does not yet support unmanaged production languages like C++ or Rust, which require reasoning about complex pointer arithmetic, ownership types, or separation logic.
  • Upstream Manual Engineering: While code and proof generation are fully automated, the high-level specification engineering and the decomposition of complex algorithms into sub-routines (like breaking Heapsort into heapify and maxheap) still required manual human intervention. Automating this upstream "spec engineering" is a prime target for future work.

Summary updated

BibTeX
@misc{gloeckle2026wybecoder,
  author = {Fabian Gloeckle and Mantas Baksys and Darius Feher and Kunhao Zheng and Amaury Hayat and Sean B. Holden and Gabriel Synnaeve and Peter O'Hearn},
  title = {{WybeCoder: Verified Imperative Code Generation}},
  year = {2026},
  eprint = {2603.29088},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2603.29088},
}
Figure