Generating Correct Code with Large Language Models and Formal Verification

Thursday 06 March 2025


The quest for high-quality, automatically generated code has been a longstanding challenge in the realm of artificial intelligence. Researchers have been exploring various approaches to address this issue, and one promising direction involves using large language models (LLMs) as intermediaries between natural language prompts and formal verification languages like Dafny.


In recent years, LLMs have made tremendous progress in generating human-like text based on input prompts. However, when it comes to code generation, these models often struggle with producing correct and efficient implementations that satisfy specific requirements and specifications. To bridge this gap, a team of researchers has proposed using Dafny as an opaque intermediate language for code generation.


The concept is straightforward: instead of generating Python code directly from natural language prompts, LLMs would produce Dafny code that satisfies the given specification. The Dafny compiler would then translate this intermediate representation into target languages like Python, ensuring correctness and efficiency through formal verification.


To test this approach, the researchers developed a chatbot prototype that leverages Claude Sonnet 3.5, an LLM specifically designed for code generation tasks. Users interact with the system by providing natural language prompts, which are then processed to generate Dafny specifications. The chatbot iteratively refines these specifications until it converges on a verified implementation.


The results are promising: in a benchmarking exercise using HumanEval, the prototype successfully generated correct Python implementations for over 77% of the tasks. While this pass rate is lower than that achieved by native Python code generation with Claude Sonnet 3.5, it’s still an impressive achievement considering the complexity and novelty of the approach.


One key challenge facing this system is interoperability between Dafny and native Python code. The generated intermediate representation can sometimes lead to custom Python classes that don’t directly compare to the expected output. To address this issue, the researchers employed targeted interventions to enable interaction with native Python code.


Another area for improvement lies in post-processing the generated code to increase readability and maintainability. While the Dafny compiler produces correct and efficient implementations, the resulting code can be dense and difficult to understand. By applying heuristics or LLM-based transformations, it’s possible to refine the output and make it more suitable for human consumption.


This innovative approach has far-reaching implications for the field of artificial intelligence-assisted code generation.


Cite this article: “Generating Correct Code with Large Language Models and Formal Verification”, The Science Archive, 2025.


Artificial Intelligence, Code Generation, Natural Language Processing, Large Language Models, Formal Verification, Dafny, Python, Chatbot, Interoperability, Readability


Reference: Yue Chen Li, Stefan Zetzsche, Siva Somayyajula, “Dafny as Verification-Aware Intermediate Language for Code Generation” (2025).


Leave a Reply