arXiv:2610.32053cs.SEOct 2026

Manuscript Offprint

arXiv:2610.32053

Are LLM-Generated Formal Specifications Faithful to Their Requirements? A Study on Bimodal Formal Specification Generation

Wenhan Wang, Shiming Chen, Zeyu Sun, Shenghua Feng

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

WENHAN WANG, Institute for Software Chinese Academy of Sciences, China

SHIMING CHEN, Beijing Jiaotong University, China

ZEYU SUN, Institute for Software Chinese Academy of Sciences, China

SHENGHUA FENG, Institute for Software Chinese Academy of Sciences, China

Formal specification generation is a critical step towards formal verification and correctness assurance of software. Recently, large language models have been widely adopted in automatic generation of formal specifications. However, existing LLM-based specification generation approaches often assume that the input program is correct, which deviates from the real purpose of formal verification—to figure out whether the program meets its requirements when its correctness is unknown. Therefore, we conduct an empirical study of LLM-based specification generation under the bimodal setting: given both the natural language requirement and its corresponding code implementation, whose correctness is unknown. We construct a benchmark of 1,403 C programs and evaluate ACSL specification generation with Frama-C verification. Our study reaches several interesting findings: first, when the code implementation is correct, providing the natural language requirement does not further improve the pass rate of generated specifications. Second, when the code implementation is incorrect, LLM-based specification generation may erroneously pass verification by producing weak or implementation-misled specifications, and this cannot be fully alleviated by advanced prompting approaches. Compared with the non-formal HoarePrompt approach, LLM-generated formal specifications are less effective in predicting requirement-code consistency. Our study takes a first look at applying LLM-based formal specification generation to verifying program correctness against requirements, and points to requirement-faithful specification generation as a new direction for future LLM-based formal specification research.

Additional Key Words and Phrases: formal specification, formal verification, large language model

ACM Reference Format:

Wenhan Wang, Shiming Chen, Zeyu Sun, and Shenghua Feng. 2018. Are LLM-Generated Formal Specifications Faithful to Their Requirements? A Study on Bimodal Formal Specification Generation. J. ACM 37, 4, Article 111 (August 2018), 17 pages. https://doi.org/XXXXXXX.XXXXXXX

1 Introduction

Formal specifications connect natural language requirements with machine-checked program verification. In specification languages such as ACSL and JML, these specifications are not merely documentation: they define the properties that a verifier checks against an implementation. Recent advances in large language models (LLMs) make it increasingly plausible to generate such specifications automatically. However, for LLM-generated specifications to support verification in practice,

Authors' Contact Information: Wenhan Wang, wangwenhan@isacas.ac.cn, Institute for Software Chinese Academy of Sciences, China; Shiming Chen, csm76456@163.com, Beijing Jiaotong University, China; Zeyu Sun, zeyu.zys@gmail.com, Institute for Software Chinese Academy of Sciences, China; Shenghua Feng, fengshenghua@isacas.ac.cn, Institute for Software Chinese Academy of Sciences, China.

Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for components of this work owned by others than the author(s) must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from permissions@acm.org.

© 2018 Copyright held by the owner/author(s). Publication rights licensed to ACM.

ACM 1557-735X/2018/8-ART111

https://doi.org/XXXXXXX.XXXXXXX

111:2

Wenhan Wang, Shiming Chen, Zeyu Sun, and Shenghua Feng

they must satisfy two requirements at the same time: they must capture the intended behavior described by the requirement, and they must contain enough implementation-aware detail, such as memory constraints, control-flow conditions, and loop invariants, to be verified by formal tools.

However, existing studies have not fully addressed this bimodal nature of formal specification generation. Code-based specification generation can exploit implementation structure and often produces specifications that are easier to check, but it commonly assumes that the implementation is correct and treats verifier success as a positive outcome. This assumption weakens the connection to the original requirement: if the implementation is wrong, a specification inferred from the implementation alone will simply encode the wrong behavior. Requirement-based generation, in contrast, is closer to the intended behavior, but without the implementation it is difficult to infer proof-relevant specification components needed by verifiers such as loop invariants and assertions other than pre- and post-conditions. Some recent works have explored LLM-based specification generation for defect prediction against natural language requirements, but they either generate simple non-verifiable assertions [7], or generate specifications from correct programs and use them to detect bugs in mutants [17]. These settings do not directly answer whether LLMs can generate formal specifications from both natural language requirements and code implementations whose correctness is unknown.

This missing setting creates a faithfulness gap. In program verification, the semantic oracle should be the requirement, not the implementation. For a correct implementation, a requirement-faithful specification should pass verification. For an incorrect implementation, however, a requirement-faithful specification should fail verification because it exposes the mismatch between the expected behavior and the actual behavior. Therefore, in bimodal specification generation, verification pass is not always a success signal. A specification that passes on an incorrect implementation may be too weak, or it may have been misled by the implementation and captured the wrong behavior. Understanding this gap is essential before LLM-generated specifications can be used as reliable evidence for program correctness.

In this paper, we conduct an empirical study of LLM-based formal specification generation from bimodal sources. We construct a benchmark of 1,403 programs from 500 programming-contest problems, covering both human-written correct and incorrect implementations. Each benchmark instance contains a natural language requirement and a normalized implementation that can be checked with ACSL and Frama-C. Our study asks whether LLMs can generate specifications that are not only syntactically accepted and verifiable, but also faithful to the requirement when the implementation may be wrong.

In our study, we aim to answer the following research questions:

• RQ1: How do bimodal information sources affect the correctness of LLM-generated formal specifications for program verification? For correct implementations, we compare requirement, code, and combined inputs to study which modality better supports verifiable ACSL generation.

• RQ2: When the code implementation is incorrect, can LLMs generate formal specifications that are consistent with the requirements? This RQ addresses the central faithfulness question: generated specifications should capture the expected program behavior in the requirement rather than the actual behavior of the implementation. We therefore analyze verification outcomes and classify specifications as correct, wrong, weak, or misled.

• RQ3: Can advanced prompting and workflow design improve LLM performance on bimodal specification generation? We study whether task decomposition and iterative refinement can mitigate the faithfulness gap by separating requirement reasoning from implementation- and verifier-guided specification refinement.

111:3

• RQ4: How does LLM-based bimodal specification generation compare with non-formal approaches in predicting the coherence between requirements and implementations? We compare our formal specification-based pipeline with HoarePrompt [2], a semi-formal LLM-based approach for requirement-code consistency reasoning.

Our results reveal several important challenges for LLM-based formal specification generation. When the implementation is correct, providing the natural language requirement does not necessarily improve the verification rate of generated specifications. More importantly, when the implementation is incorrect, LLM-generated specifications may pass verification for the wrong reason: they may be too weak to distinguish the bug, or they may overfit to the incorrect implementation. These findings suggest that future LLM-based specification generators should be evaluated not only by syntactic correctness or verifier pass rates, but also by requirement faithfulness.

In summary, our contributions are as follows:

• We conduct an empirical study of LLM-based formal specification generation from bimodal sources, where both natural language requirements and code implementations are provided without assuming implementation correctness.

• We construct a benchmark of 1,403 programs from 500 problems, including both correct and incorrect human-written implementations for requirement-level program verification.

• We analyze the faithfulness of LLM-generated ACSL specifications and study whether advanced prompting and refinement workflows can reduce weak or implementation-misled specifications.

Overall, our study shows that bimodal formal specification generation is not only a generation problem, but also a requirement-faithfulness problem. These findings provide empirical evidence and evaluation guidance for future research on LLM-driven specification generation and program verification.

References (13)