Automatic Generation of Formal Specification and Verification Annotations Using LLMs and Test Oracles
João Pascoal Faria, Emanuel Trigo, Vinicius Honorato, Rui Abreu
TL;DR
This work tackles the manual burden of producing formal specifications for Dafny by introducing a multimodel LLM workflow that generates pre/postconditions, loop invariants, and proof helpers from conventional code and test assertions. It leverages a generate–check–repair–minimize loop, with test assertions acting as specification oracles and the Dafny verifier providing automatic feedback, achieving a 98.2% success rate on 110 programs. The authors contribute TESTDAFNY110, a Dafny-focused annotation pipeline, and the Dafny AI Assistant IDE plugin, demonstrating strong specification quality (≈96% logical equivalence to expert specs) and practical usability improvements. The results suggest a viable, scalable path toward broader adoption of formal methods by integrating AI-assisted specification generation into typical developer workflows. Future work will broaden pattern catalogs, explore divide-and-conquer strategies for larger codebases, and evaluate smaller, open-source models for cost-performance trade-offs.
Abstract
Recent verification tools aim to make formal verification more accessible to software engineers by automating most of the verification process. However, annotating conventional programs with the formal specification and verification constructs (preconditions, postconditions, loop invariants, auxiliary predicates and functions and proof helpers) required to prove their correctness still demands significant manual effort and expertise. This paper investigates how LLMs can automatically generate such annotations for programs written in Dafny, a verification-aware programming language, starting from conventional code accompanied by natural language specifications (in comments) and test code. In experiments on 110 Dafny programs, a multimodel approach combining Claude Opus 4.5 and GPT-5.2 generated correct annotations for 98.2% of the programs within at most 8 repair iterations, using verifier feedback. A logistic regression analysis shows that proof-helper annotations contribute disproportionately to problem difficulty for current LLMs. Assertions in the test cases served as static oracles to automatically validate the generated pre/postconditions. We also compare generated and manual solutions and present an extension for Visual Studio Code to incorporate automatic generation into the IDE, with encouraging usability feedback.
