NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Towards AI-Assisted Synthesis of Verified Dafny Methods

Md Rakib Hossain Misu, Cristina V. Lopes, Iris Ma, James Noble

Abstract

While Large Language Models (LLMs) have demonstrated remarkable proficiency in code generation, their outputs often lack the rigorous correctness required for high-stakes applications. This paper investigates the potential of LLMs to synthesize provably correct code in Dafny, a verification-aware programming language. By implementing a sophisticated pipeline combining Chain-of-Thought (CoT) prompting with Retrieval-Augmented Generation (RAG), we demonstrate that GPT-4 can act as a "verification apprentice," successfully generating verified methods for 58% of a benchmark dataset. Our findings highlight the importance of formal verification as a ground truth for LLM outputs and the effectiveness of semantic retrieval in assisting with complex proof construction.

Introduction

In modern software engineering, the "mostly correct" code produced by Large Language Models (LLMs) presents a significant risk in domains such as aerospace, cryptography, and medical systems. While LLMs can generate logic quickly, they frequently "hallucinate" bugs that are difficult to detect through traditional testing. Formal verification offers a mathematically rigorous alternative, proving that code adheres to its specifications. However, verification-aware languages like Dafny require developers to provide not only the implementation but also formal preconditions, postconditions, and loop invariants—a task often more challenging than writing the code itself.

This research explores whether LLMs can bridge this gap by automating the synthesis of both the implementation and the formal proofs required for verification. By treating the Dafny static verifier as the ultimate arbiter of correctness, we move beyond heuristic-based evaluation toward a paradigm of guaranteed software reliability.

Methodology

Prompting Strategies

The study evaluated the performance of GPT-4 and PaLM-2 across three distinct prompting strategies designed to test the models' reasoning capabilities:

  1. Contextless Prompt: A baseline approach using only a natural language description of the task.
  2. Signature Prompt: A more structured approach providing the task description, a specific method signature, and three unit tests.
  3. Dynamic Few-Shot (CoT + RAG): A two-phase pipeline designed to mimic human reasoning. In Phase 1, the LLM generates formal specifications. In Phase 2, the system utilizes the OpenAI embedding API to retrieve the five most semantically similar examples from a "gold standard" dataset. These examples are then integrated into a Chain-of-Thought (CoT) prompt, guiding the model through a step-by-step problem decomposition before final code generation.

The Verification Pipeline

All generated code was processed through the Dafny static verifier, which utilizes the Z3 SMT solver. This process ensures that any code marked as "successful" is mathematically guaranteed to meet its defined specifications. To ensure the quality of these specifications, the researchers conducted a manual qualitative analysis to distinguish between "strong" specifications (which capture the true intent of the problem) and "weak" specifications (which are easy to prove but lack meaningful constraints).

Datasets

To facilitate this research, the authors developed several new benchmarks:

  • MBPP-san-DFY: A translation of 178 algorithmic problems from the "Mostly Basic Python Problems" (MBPP) dataset into Dafny.
  • MBPP-DFY-50: A curated "gold standard" dataset of 50 problems manually solved and verified by experts to serve as the RAG knowledge base.
  • MBPP-DFY-153: A final released dataset containing 153 verified Dafny solutions, comprising the 50 manual solutions and 103 synthesized by GPT-4.

Key Results and Insights

Model Performance

GPT-4 significantly outperformed PaLM-2 across all metrics. Using the CoT + RAG approach, GPT-4 achieved a raw verification success rate of 64%. After rigorous manual filtering to ensure "strong" specifications, the final success rate was established at 58% (103 out of 178 problems). This represents a substantial improvement over the 19% success rate achieved with contextless prompting.

The Role of Context and Similarity

The success of the RAG approach suggests that verification patterns—particularly loop invariants—are often shared among semantically similar problems. Providing the LLM with "near neighbor" solutions allowed it to adapt existing proof structures to new tasks. Interestingly, the "Signature Prompt" performed worse than the "Contextless Prompt" for GPT-4. This suggests that providing a fixed signature may prematurely constrain the model's reasoning, whereas a more open-ended prompt allows the model to formulate the problem in a way that is more amenable to formal proof.

Error Reduction

The CoT + RAG pipeline also proved highly effective at reducing syntax and parse errors. By guiding the model through a structured reasoning process and providing relevant examples, the system minimized the common pitfalls associated with Dafny’s strict syntax and version-specific requirements.

Discussion and Future Work

While these results are promising, the study identified several limitations. The current benchmarks focus on self-contained algorithmic tasks; real-world software involves complex state management and inter-method dependencies that remain difficult for current LLMs to navigate. Furthermore, LLMs are sensitive to version changes in Dafny, often generating deprecated syntax that leads to compilation failures.

Future research should explore the "Programmer’s Verification Apprentice" model, where the LLM focuses on generating the most difficult components of a proof—such as complex loop invariants—for human-written code, rather than attempting full synthesis. Additionally, applying this "generate-and-verify" pipeline to other domains, such as legal reasoning or structural engineering, could provide a template for high-assurance AI applications across various industries.

Summary updated

BibTeX
@article{misu2024towards,
  author = {Md Rakib Hossain Misu and Cristina V. Lopes and Iris Ma and James Noble},
  title = {{Towards AI-Assisted Synthesis of Verified Dafny Methods}},
  journal = {Proc. ACM Softw. Eng.},
  year = {2024},
  doi = {10.1145/3643763},
}
Figure