TL;DR
While Large Language Models (LLMs) have made impressive strides in automated theorem proving, their ability to generate formal specifications for model checking has remained a major bottleneck. This paper introduces Model-Bench, a new benchmark of 400 Python programs designed to evaluate how well LLMs can translate source code into verification-ready TLA+ specifications. The study reveals significant limitations in current LLMs but demonstrates that restructuring code into a state-machine format can drastically improve the fidelity of the generated formal models.
What Problem Does This Paper Address?
Software testing can reveal the presence of bugs, but it can never prove their absolute absence. For safety-critical systems, developers turn to formal verification. This field is broadly split into two approaches: theorem proving (deductive logical derivations) and model checking (exhaustively exploring a system's state space against formal specifications).
While LLM-assisted theorem proving has advanced rapidly, automated model checking lags behind due to the "automodeling bottleneck." It is incredibly difficult to automatically translate real-world code—especially dynamic languages like Python with mutable aliases, recursion, and complex libraries—into accurate, abstract, and tractable formal models (like TLA+). If the generated model is too concrete, the model checker runs out of memory; if it is too abstract, the verification becomes unsound. This paper establishes a systematic way to evaluate and improve LLMs on this exact task.
Methods
The authors constructed an automated pipeline to evaluate LLMs and help them bridge the gap between high-level code and low-level formal specifications:
- Simplification and Rewriting Pipeline: To ensure LLMs focus on core program logic rather than fighting complex syntax, the authors used GPT-4o to equivalently rewrite advanced Python features (like list comprehensions, slices, and recursion) into explicit, traditional loops.
- Code Transformation (The State Machine Trick): TLA+ specifications are fundamentally state machines that describe flat, discrete transitions using a program counter (
pc). Standard Python code does not look like this. To help LLMs match this execution model, the authors built a transformer that lowers Python code into a Control-Flow Graph (CFG) and regenerates it as a state-machine-pattern Python program. This structurally aligns the code with TLA+ before the LLM even attempts translation. - Rigorous Evaluation Metrics: Beyond checking if the generated TLA+ code compiles and runs (
Runnable@k), the authors introduced a novel State Similarity metric. By comparing the execution traces of the LLM's model against a human-verified "oracle" model, they measure whether the LLM actually preserved the true semantics and variable states of the program.
Datasets
The authors introduced Model-Bench, which consists of:
- 400 Python programs carefully filtered and derived from three premier code-generation datasets: HumanEval, MBPP, and LiveCodeBench.
- 1,639 test cases used to execute and verify the functional correctness of the programs and their corresponding formal models.
Key Insights and Ideas
- Zero-Shot is a No-Go: For formal modeling tasks, zero-shot prompting completely fails for almost all models, yielding near 0% success rates for smaller LLMs. Few-shot examples are absolutely mandatory to ground the model in TLA+ syntax and constraints.
- Syntactic vs. Algorithmic Difficulty: An LLM's ability to formally model a program does not correlate with how algorithmically difficult the problem is. Instead, it is heavily degraded by syntactic complexity—specifically, deeply nested loops, high variable counts, and complex cyclomatic complexity.
- The Structure vs. Length Trade-off: Transforming Python code into an explicit state-machine format significantly boosts the similarity of the generated model to the ground truth (making it a better translation blueprint). However, because it increases the overall code length, it occasionally triggers the "lost in the middle" phenomenon in LLMs, slightly lowering raw compilation success.
- Common LLM Blind Spots: LLMs frequently fail due to language mismatches. They often assume TLA+ possesses Python-like built-in functions (e.g., trying to use an un-implemented
Sort()operator) or forget that TLA+ uses 1-based array indexing rather than Python's 0-based indexing.
Key Results
- Top Performer: DeepSeek-V3 demonstrated the best overall capability, achieving a
Runnable@1rate of 51.75% and an average state similarity of 49.55% under the few-shot setting. - Few-Shot Impact: Across all tested models, moving from zero-shot to few-shot prompts improved the average runnability rate from 9.25% to 26.82% and skyrocketed average state similarity from 2.42% to 33.86%.
- Code Transformation Power: Implementing the state-machine code transformation successfully raised the average state similarity across all models from 33.86% to 54.20%, proving to be a highly effective complementary technique.
Limitations and Future Work
- Context Inflation: The CFG-based code transformation increases the token length of the prompt, which can overwhelm the attention mechanisms of smaller models.
- Coarse Error Feedback: The TLC model checker provides coarse exit codes rather than granular semantic error messages, limiting how effectively an LLM can self-correct during multi-turn debugging chats.
- Scope Expansion: Model-Bench is currently restricted to sequential Python programs and TLA+. Future work is needed to expand this pipeline to concurrent, distributed systems and other formal specification languages like Coq or Lean4.
Summary updated