How Powerful are LLMs in Generating Formal Program Specifications?

Fanpeng Yang, Xing Li, Shuling Wang, Jie An, Zeyu Sun, Shenghua Feng, Wenhan Wang, Weiyi Wang, Naijun Zhan, Fanjiang Xu

Formal verification is widely considered the gold standard of software engineering. By using mathematical proofs to guarantee that code behaves exactly as intended, it eliminates entire classes of bugs. However, its widespread adoption is hindered by a steep "onboarding" problem: writing the precise formal specifications—the mathematical rules the code must follow—is often more difficult and expensive than writing the implementation itself.

While Large Language Models (LLMs) have demonstrated impressive capabilities in standard code generation and theorem proving, their ability to synthesize formal program specifications remains largely uncharted.

The "Evaluation Duality" Problem

Historically, evaluating an LLM's ability to generate specifications has been fraught with difficulty. Current evaluation methods typically attempt to prove that a model-generated specification is mathematically equivalent to a human-written ground truth.

This approach creates what researchers call an Evaluation Duality. Equivalence proofs are formidably complex. If an automated prover fails to verify the equivalence, it is impossible to know whether the generated specification is flawed, or if the prover is simply too weak to complete the mathematical reasoning. This ambiguity conflates proof difficulty with actual specification quality.

Introducing COINS

To resolve this ambiguity, researchers have introduced COINS (COq-based INstantiated Specification evaluation). Instead of relying on all-or-nothing abstract equivalence proofs, COINS evaluates specifications by instantiating them with concrete test cases.

The COINS pipeline assesses specification quality through three rigorous stages:

  1. Syntactic Validation: Does the generated Rocq (formerly Coq) code compile without errors?
  2. Positive Test Acceptance (PASSall): Can the model prove that the specification holds true for known, correct inputs and outputs?
  3. Negative Test Rejection (REJECTall): Does the specification correctly reject "mutated" or broken code? A specification fails if it is too loose and accidentally accepts a buggy implementation.

By focusing on concrete instances, COINS leverages the LLM as a "probabilistic prover" to generate proofs that are simpler and more reliable than abstract equivalence proofs.

Rigorous Testing on HumanEval

The researchers applied COINS to the well-known HumanEval benchmark (164 Python problems), elevating it with layers of formal rigor:

  • Human-Written Baselines: Researchers manually authored and cross-reviewed 164 ground-truth Rocq specifications.
  • Extensive Positive Testing: Leveraging HumanEval+, the framework utilized an average of roughly 756 positive test cases per problem.
  • Mutation Testing: The team generated 1,640 negative test cases by intentionally injecting bugs into the Python code (e.g., swapping > for <) to test the specifications' strictness.

Key Insights: Math, Syntax, and Phantom Goals

The large-scale study revealed several fascinating insights into how LLMs handle formal logic:

  • The "Math vs. Code" Divergence: An LLM's ability to write formal specifications correlates much more strongly with its performance on advanced math competitions (like the AIME) than its standard coding abilities. For instance, while DeepSeek-V3.1 excels at standard Python benchmarks, it struggled significantly with the rigid logic required for Rocq specifications.
  • The Syntax Bottleneck: For many models, the primary hurdle is grammar, not logic. Models frequently hallucinate Rocq syntax or fail to import necessary libraries. Earlier models exhibited syntax error rates as high as 92%.
  • Executable Components Matter: Specifications utilizing "Fixpoint" (recursive functions that can be executed) are much easier for LLMs to verify than purely relational, abstract specifications. When forced to rely solely on abstract logic, model success rates plummet.
  • Phantom Goal Errors: Researchers identified a unique failure mode where LLMs lose track of the proof state. If the Rocq proof assistant automatically solves early goals, the model may attempt to apply logic intended for those solved goals to the remaining ones, causing the proof to crash.

Frontier Model Performance

The study evaluated six frontier models, including GPT-5, Claude 4.5 Opus, and Gemini 3 Pro Preview, revealing a massive performance gap between tiers.

Gemini 3 Pro Preview emerged as the strongest model, successfully handling 29.88% of human-written specifications. It achieved a 28.05% REJECTall score, meaning it successfully generated specifications that passed all correct tests and rejected all bugs. In stark contrast, DeepSeek-V3.1 managed a REJECTall score of only 1.22%.

While frontier models are beginning to produce specifications approaching expert human precision, they still struggle with semantic consistency—such as writing a syntactically perfect but logically impossible rule.

Future Directions

Generating precise, bug-free formal specifications remains a massive hurdle, akin to solving high-level mathematics rather than writing standard code. The researchers note that accurate specification evaluation, rather than mere model scaling, is central to understanding and improving LLM capabilities in this domain.

Currently limited to the HumanEval dataset and the Rocq/Python ecosystem, future work will look to expand the COINS framework to other proof assistants like Lean, apply it to C program verification within the CompCert ecosystem, and develop better error feedback loops to help LLMs self-correct using compiler failure messages.

Summary updated

BibTeX
@inproceedings{yang2026how,
  author = {Fanpeng Yang and Xing Li and Shuling Wang and Jie An and Zeyu Sun and Shenghua Feng and Wenhan Wang and Weiyi Wang and Naijun Zhan and Fanjiang Xu},
  title = {{How Powerful are LLMs in Generating Formal Program Specifications?}},
  booktitle = {International Conference on Machine Learning},
  year = {2026},
  url = {https://openreview.net/forum?id=41zGzIEpQd},
}
Figure