TL;DR
This paper introduces VerMCTS, a novel approach that combines Large Language Models (LLMs) with formal verification to synthesize provably correct programs. By using a verifier to guide a Monte Carlo Tree Search (MCTS), VerMCTS significantly improves the success rate of generating verified code, especially for complex multi-step programming tasks.
What Problem Does This Paper Address?
Large Language Models (LLMs) are increasingly adept at generating code, but a critical challenge remains: ensuring the generated code is not only functional but also correct and trustworthy. For many applications, especially in safety-critical domains, simply generating code that passes a few tests isn't enough; formal guarantees of correctness are required. This often involves writing programs in verification-aware languages like Dafny or Coq, which include specifications and proofs. However, LLMs struggle with the intricate, step-by-step reasoning required for formal verification. This paper addresses the problem of bridging this gap, enabling LLMs to generate code that is not only syntactically correct but also formally verified.
Methods
The core innovation is VerMCTS, a modified Monte Carlo Tree Search (MCTS) algorithm. Standard MCTS explores a search tree to find optimal actions. VerMCTS enhances this by:
- LLM as a Prior: The LLM is used to generate candidate code snippets (actions) at each step of the search.
- Verifier as a Heuristic: Crucially, a formal program verifier (like Dafny or Coq's built-in verifier) is used to provide intermediate feedback. Instead of waiting for a complete program to verify, VerMCTS checks partial programs at each step. This feedback acts as an upper bound on the value function, allowing the search to prune branches that are guaranteed to fail early on.
- Progressive Widening: To handle the potentially infinite action space of code generation, VerMCTS employs progressive widening. This technique allows the search tree to grow wider over time, exploring more options without an explosion in complexity.
- Optimistic Estimates: The verifier's feedback is used to provide an optimistic estimate of a partial program's value, encouraging deeper exploration of promising branches.
Datasets
The authors introduce a new suite of 15 multi-step verified programming problems. These problems are designed to test essential skills in verified programming, including:
- Constructing Algebraic Data Types (ADTs).
- Defining functions with pattern matching.
- Writing inductive proofs.
The suite is split into 9 problems in Dafny and 6 in Coq.
Key Insights and Ideas
- Verifier as a Search Guide: The most significant insight is that a formal verifier, even when used on partial programs, can provide a powerful and efficient signal to guide a search algorithm like MCTS. This is more effective than relying solely on LLM rollouts or error messages.
- Early Pruning is Key: By checking partial programs, VerMCTS can quickly identify and discard incorrect paths, preventing wasted computation and significantly improving efficiency. This is especially important for complex, multi-step problems where errors can compound.
- LLMs + Formal Methods Synergy: The paper demonstrates a compelling synergy between the generative capabilities of LLMs and the rigorous reasoning of formal verification. LLMs provide the creative suggestions, while the verifier ensures soundness.
- New Benchmark for Verified Synthesis: The introduction of a challenging, multi-step verified programming benchmark is crucial for advancing research in this area.
Key Results
VerMCTS significantly outperforms several baselines, including repeated LLM sampling, MCTS with rollouts, and a technique called Reflexion.
- 30% Absolute Improvement: On average, VerMCTS achieves a 30% absolute increase in pass@5000 (the rate of successfully generating a verified program within a budget of 5000 tokens) across the new benchmark suite compared to simple repeated sampling from the base LLM.
- Solves Previously Unsolved Problems: For several problems, VerMCTS successfully generates verified solutions that none of the baselines could achieve within the given token budget.
- Coq is More Challenging: The results highlight that Coq is substantially more challenging due to its less automated verifier, yet VerMCTS still shows strong performance.
Limitations and Future Work
- Scoring Granularity: The authors acknowledge that the scoring of partial programs is limited by its coarse granularity (e.g., a whole function in Dafny, a command in Coq) and lack of lookahead. This means feedback can be delayed, especially for larger proofs.
- Future Work - Richer Feedback: A promising direction is to explore ways to incorporate richer feedback into the search beyond just scalar rewards, potentially by allowing the search to rely on more detailed verifier outputs or even costly rollouts/reflection steps when necessary, while still maintaining efficiency.
- Generalizability: The authors suggest that the core idea of using a cheap, provable upper bound on the value function to guide search could be applied beyond verified programming to other domains.
Summary updated