Abstract
Recent advances in large language models (LLMs) offer promising potential for automating formal methods. However, applying them to formal verification remains challenging due to the complexity of specification languages, the risk of hallucinated output, and the semantic gap between natural language and formal logic. We introduce PAT-Agent, an end-to-end framework for natural language autoformalization and formal model repair that combines the generative capabilities of LLMs with the rigor of formal verification to automate the construction of verifiable formal models. In PAT-Agent, a Planning LLM first extracts key modeling elements and generates a detailed plan using semantic prompts, which then guides a Code Generation LLM to synthesize syntactically correct and semantically faithful formal models. The resulting code is verified using the Process Analysis Toolkit (PAT) model checker against user-specified properties, and when discrepancies occur, a Repair Loop is triggered to iteratively correct the model using counterexamples. Experimental results on 40 systems show that PAT-Agent consistently outperforms baselines, achieving high verification success with superior efficiency.
Introduction
Formal methods—mathematical techniques used to prove the correctness of software and hardware—are essential for safety-critical systems but suffer from a notoriously steep learning curve. Writing formal specifications in languages like CSP# requires deep expertise in both the system domain and formal logic. While modern LLMs like GPT-4 or o3 excel at general-purpose coding, they frequently struggle with the precision required for formal verification, often producing syntactically incorrect code or "hallucinating" logic that fails to capture nuanced system behaviors.
PAT-Agent addresses these challenges by providing an end-to-end framework for natural language autoformalization and formal model repair. By combining generative LLM capabilities with the rigor of the Process Analysis Toolkit (PAT) model checker, PAT-Agent enables non-experts to construct verifiable formal models from natural language descriptions, effectively bridging the gap between human intent and mathematical proof.
The PAT-Agent Framework
The framework moves away from "one-shot" code generation in favor of a modular, agentic pipeline designed to minimize errors and maximize semantic faithfulness.
- Planning Stage: A Planning LLM analyzes natural language input to extract key modeling elements such as constants, variables, actions, and guards. This results in a structured Model Generation Plan, ensuring the LLM understands the "world" before writing logic.
- Code Generation: A Code Generation LLM converts the plan into CSP# code. This stage utilizes Retrieval-Augmented Generation (RAG) to incorporate relevant examples and syntax cues, reducing common coding pitfalls.
- Verification and Repair: The generated code is verified using the PAT model checker against user-specified properties. If a discrepancy is found, a Verification-Guided Repair Loop is triggered. The agent uses heuristics to analyze counterexample traces, identifies the likely fault location, and prompts the LLM to iteratively correct the model until it is fully verified.
Experimental Evaluation
PAT-Agent was evaluated on a benchmark of 40 modeling tasks (comprising 133 assertions) drawn from the PAT library, the Alloy4Fun (A4F) dataset, and the "Understanding Concurrent Systems" (UCS) textbook.
The results demonstrate that while standalone state-of-the-art LLMs (like OpenAI's o3 or Anthropic's Claude) achieve only a ~52% full-pass rate on their own, PAT-Agent reaches a 100% verification success rate across all 40 systems after its repair iterations. Efficiency is also a key highlight, with a median generation and verification time of just 4.34 minutes. Furthermore, a user study of 20 participants showed that those using the PAT-Agent interface were 25% faster and achieved nearly perfect assertion accuracy, even with limited prior formal methods experience.
Insights and Conclusion
The success of PAT-Agent stems from two primary innovations: structured planning and the use of the model checker as a "teacher." By defining variables and states before logic, the system avoids the complexity traps of raw generation. Furthermore, by reacting to mathematical proofs of failure (counterexamples), the stochastic nature of the LLM is anchored by deterministic formal tools.
While the system is currently limited by the clarity of initial natural language inputs and specific repair heuristics, it represents a significant step toward democratizing formal methods. Future work will expand the framework to support probabilistic and real-time systems, further lowering the barrier for rigorous system verification.
Summary updated