Intent-aligned Formal Specification Synthesis via Traceable Refinement

Zhe Ye, Aidan Z.h. Yang, Huangyuan Su, Zhenyu Liao, Samuel Tenka, Zhizhen Qin, Udaya Ghai, Dawn Song, Soonho Kong

TL;DR

Researchers have introduced VERISPECGEN, a framework that uses "traceable refinement" to help AI models write accurate formal specifications (mathematical contracts for code) in the Lean 4 language. By breaking down natural language into atomic requirements and mapping test failures directly to those requirements, the system achieves state-of-the-art performance and creates a massive 343K-example dataset that improves general reasoning in LLMs.

What Problem Does This Paper Address?

We are increasingly using Large Language Models (LLMs) to write code, but how do we know that code is actually correct? Formal verification is the gold standard for safety: you write a "formal specification" (a mathematical contract of what the code should do), and a theorem prover checks if the code matches the math.

The bottleneck is that writing these specifications is incredibly hard. Natural language is messy and ambiguous, while formal logic is rigid. If a model generates a "weak" specification, buggy code might pass; if it's "incorrect," perfect code might be rejected. Current AI methods for writing these specs often fail because they receive "coarse" feedback—they are told the spec is wrong, but not why or where.

Methods

The authors propose VERISPECGEN, which moves away from "one-shot" generation toward an iterative, "traceable" process. The workflow consists of three main stages:

  1. Atomic Requirement Decomposition: Instead of looking at a prompt as one big block, an LLM breaks it into "Atomic Requirements" (ARs). For example, a sorting task might be split into: "Input must be a list" (Precondition), "Output must contain the same elements," and "Output must be in ascending order" (Postconditions).
  2. Requirement-Targeted Test Generation: The system generates three types of tests for each AR:
  3. Positive tests: Valid input-output pairs.
  4. Negative-input tests: Inputs that should be rejected by the precondition.
  5. Negative-output tests: Valid inputs paired with incorrect outputs that violate a specific requirement.
  6. Traceable Refinement: This is the "secret sauce." The system creates a traceability map that links every test to the specific requirement it validates. When a test fails in the Lean prover, the system knows exactly which clause in the specification is at fault. Instead of rewriting the whole thing, the LLM performs a localized repair on the specific failing clause.

Finally, the system uses Adversarial Testing, where an LLM specifically tries to find edge cases to "break" the newly generated specification.

Datasets

  • Source Data: The authors used TACO-verified, a collection of competitive programming problems.
  • Trajectory Distillation: By running VERISPECGEN on thousands of problems, they recorded the "trajectories" (the step-by-step reasoning, failures, and repairs). They distilled these into a massive dataset of 343,827 instruction-response pairs.
  • Evaluation: They tested their models on the VERINA benchmark, specifically the SpecGen (specification generation) and CodeGen (code generation) tasks.

Key Insights and Ideas

  • Traceability is Transformative: The biggest "aha moment" is that knowing why a spec failed is more important than just knowing that it failed. Localized repair prevents the model from "breaking" parts of the spec that were already correct.
  • Process-Level Supervision: The paper argues that training models on the process of refinement (the trajectories) is far more effective than training them on just the final, correct answer.
  • The "Formal Reasoning" Dividend: One of the most surprising findings is that training a model to write formal specifications actually makes it better at general math (GSM8K) and general coding (HumanEval). Reasoning about the "contract" of a program seems to improve the model's underlying logic.
  • Decomposition is the Foundation: Ablation studies showed that requirement decomposition was the most critical component; without it, the refinement process often confused the models rather than helping them.

Key Results

  • State-of-the-Art Performance: Using Claude Opus 4.5, VERISPECGEN achieved 86.6% on the VERINA SpecGen task, outperforming baselines by up to 31.8 points.
  • Massive Gains for Open Models: Mid-tier open-weight models (like Qwen3-30B) saw relative performance jumps of over 70% when using the traceable refinement framework.
  • Fine-tuning Success: Models fine-tuned on the new trajectory dataset saw a 62–106% relative improvement in specification synthesis.
  • Out-of-Domain Transfer: A Qwen-4B model fine-tuned on this data improved its score on the GSM8K math benchmark by 21.7%, despite the training data being focused on formal logic and Lean code.

Limitations and Future Work

  • Oracle Dependency: The system currently requires a "test oracle" (usually a reference implementation of the code) to label the test cases. This might not be available for brand-new, unwritten software.
  • Prover Timeouts: In about 11.6% of cases, the Lean "grind" tactic couldn't decide if a spec was correct within 120 seconds, forcing the system to fall back on an LLM judge.
  • Language Specificity: While the principles are general, the current implementation is specific to Lean 4. The authors suggest extending this to other frameworks like Dafny or Coq in the future.

Summary updated

BibTeX
@inproceedings{ye2026intent,
  author = {Zhe Ye and Aidan Z.H. Yang and Huangyuan Su and Zhenyu Liao and Samuel Tenka and Zhizhen Qin and Udaya Ghai and Dawn Song and Soonho Kong},
  title = {{Intent-aligned Formal Specification Synthesis via Traceable Refinement}},
  booktitle = {AI4Math @ ICML},
  year = {2026},
  url = {https://openreview.net/forum?id=i9MTMRHHG7},
}
Figure