SpecGen: Automated Generation of Formal Program Specifications via Large Language Models
Lezhi Ma, Shangqing Liu, Yi Li, Xiaofei Xie, Lei Bu
Abstract
In the software development process, formal program specifications play a crucial role in various stages, including requirement analysis, software testing, and verification. However, manually crafting formal program specifications is rather difficult, making the job time-consuming and labor-intensive. Moreover, it is even more challenging to write specifications that correctly and comprehensively describe the semantics of complex programs. To reduce the burden on software developers, automated specification generation methods have emerged. However, existing methods usually rely on predefined templates or grammar, making them struggle to accurately describe the behavior and functionality of complex real-world programs. To tackle this challenge, we introduce SpecGen, a novel technique for formal program specification generation based on Large Language Models (LLMs). Our key insight is to overcome the limitations of existing methods by leveraging the code comprehension capability of LLMs. The process of SpecGen consists of two phases. The first phase employs a conversational approach that guides the LLM in generating appropriate specifications for a given program, aiming to utilize the ability of LLM to generate high-quality specifications. The second phase, designed for where the LLM fails to generate correct specifications, applies four mutation operators to the model-generated specifications and selects verifiable specifications from the mutated ones through a novel heuristic selection strategy by assigning different weights of variants in an efficient manner. We evaluate SpecGen on two datasets, including the SV-COMP Java category benchmark and a manually constructed dataset containing 120 programs. Experimental results demonstrate that SpecGen succeeds in generating verifiable specifications for 279 out of 385 programs, outperforming the existing LLM-based approaches and conventional specification generation tools like Houdini and Daikon. Further investigations on the quality of generated specifications indicate that SpecGen can comprehensively articulate the behaviors of the input program.
Ask about this paper
Your agent reads all of it.
Lune indexed this paper to the last equation, along with the top-tier papers that cite it. Ask a question and the answer quotes them.
Your agent calls
Luneget_paper_fulltext
Free to start. No credit card required.
Terminal
Install the CLIlune papers fulltext 448eefce-9d9c-4d69-973c-a5b06f344ad6Cited by top-tier papers28
- SpecRover: Code Intent Extraction via LLMsHaifeng Ruan, Yuntong Zhang, Abhik RoychoudhuryICSE 2025 · 12 citations
- OSVBench: Benchmarking LLMs on Specification Generation Tasks for Operating System VerificationShangyu Li, Juyong Jiang, Tiancheng Zhao, Jiasi ShenAAAI 2026 · 10 citations
- Sharpen the Spec, Cut the Code: A Case for Generative File System with SYSSPECQingyuan Liu, Mo Zou, Hengbin Zhang, Dong Du et al.FAST 2026 · 9 citations
- Intention is All you Need: Refining your Code from your IntentionQi Guo, Xiaofei Xie, Shangqing Liu, Ming Hu et al.ICSE 2025 · 7 citations
- Automated Repair of Ambiguous Problem Descriptions for LLM-Based Code GenerationHaoxiang Jia, Robbie Morris, He Ye, Federica Sarro et al.ASE 2025 · 6 citations
Builds on13
- Language Models are Few-Shot LearnersTom B. Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah et al.NeurIPS 2020 · 64,255 citations
- Is Your Code Generated by ChatGPT Really Correct? Rigorous Evaluation of Large Language Models for Code GenerationJiawei Liu, Chunqiu Steven Xia, Yuyao Wang, Lingming ZhangNeurIPS 2023 · 2,317 citations
- Prompting Large Language Model for Machine Translation: A Case StudyBiao Zhang, Barry Haddow, Alexandra BirchICML 2023 · 402 citations
- Retrieval-Augmented Generation for Code Summarization via Hybrid GNNShangqing Liu, Yu Chen, Xiaofei Xie, Jing Kai Siow et al.ICLR 2021 · 194 citations
- An extensive study on pre-trained models for program understanding and generationZhengran Zeng, Hanzhuo Tan, Haotian Zhang, Jing Li et al.ISSTA 2022 · 142 citations
Related papers
- Aligning Requirement for Large Language Model's Code GenerationZhao Tian, Junjie ChenICSE 2026
- SpecMind: Cognitively Inspired, Interactive Multi-Turn Framework for Postcondition InferenceCuong Chi Le, Minh V. T. Pham, Tung Duy Vu, Cuong Duc Van et al.ACL 2026 · 2 citations
- Enchanting Program Specification Synthesis by Large Language Models Using Static Analysis and Program VerificationCheng Wen, Jialun Cao, Jie Su, Zhiwu Xu et al.CAV 2024 · 60 citations
- Can LLMs Reason About Program Semantics? A Comprehensive Evaluation of LLMs on Formal Specification InferenceThanh Le-Cong, Bach Le, Toby MurrayACL 2025
- VeriEquivBench: An Equivalence Score for Ground-Truth-Free Evaluation of Formally Verifiable CodeLingfei Zeng, Fengdi Che, Xuhan Huang, Fei Ye et al.ICLR 2026 · 8 citations
