Introduction
As Large Language Models (LLMs) increasingly assist in software engineering, their role in formal verification has become a focal point of research. Formal verification offers the promise of mathematically proven software correctness, yet it relies on the existence of precise formal specifications. While LLMs show promise in generating these specifications from natural language, evaluating the quality of their output remains a significant hurdle. This paper, presented at FMCAD 2024, introduces a framework for automatically evaluating LLM-generated formal specifications in verification-aware languages like Dafny.
The Specification Evaluation Problem
In traditional software development, code is evaluated against test cases. However, formal specifications in languages such as Dafny or F* often utilize non-executable constructs, including universal quantifiers ($\forall$) and ghost variables. These cannot be "run" in the traditional sense.
Furthermore, a specification can be technically "correct" while being functionally useless. For example, a specification that simply states ensures true will always be verified as correct for any program, but it fails to describe any meaningful behavior. This "weakness" or "vacuousness" makes manual review tedious and error-prone, necessitating an automated approach to measure both correctness and precision.
Methodology: Symbolic Testing of Specifications
The author proposes a method to "symbolically test" specifications by treating them as logical predicates to be verified against specific instances. The framework evaluates specifications based on two primary metrics:
1. Correctness (Soundness)
To determine if a specification accurately describes a valid program state, the tool checks it against a "hidden" reference input-output pair $(i, o)$. This is formulated as a Hoare-triple: {true} x := i; y := o; {phi(x, y)}. If the Dafny verifier confirms that the specification $\phi$ holds for the known correct values, the specification is considered sound for that instance.
2. Completeness (Precision)
To ensure a specification is not overly permissive (i.e., too weak), the framework employs mutation testing. The tool generates "mutants"—slightly altered, incorrect versions of the expected output. A robust, complete specification should be strong enough to allow the verifier to prove that these buggy outputs violate the specification.
For instance, if a function is intended to return the intersection of two sets, a weak specification might only verify that the result contains some common elements. The completeness check would identify this weakness by providing a result missing a required element; a strong specification would reject this mutant, while a weak one would pass it.
Implementation and Dataset
The methodology was implemented in a 400-line Python script that automates the generation of temporary Dafny programs and invokes the Dafny verifier. The study utilized MBPP-DFY, a dataset created by porting the "Mostly Basic Python Programs" (MBPP) benchmark to Dafny. This dataset includes 153 problems with human-labeled specifications categorized as WRONG_SPEC, WEAK_SPEC, or STRONG_SPEC. The evaluation focused on a subset of 64 problems where the prototype's regex-based parser could successfully process the files.
Key Insights
The automated evaluation yielded several critical insights into the nature of LLM-generated formalisms:
- Alignment with Human Judgment: The automated completeness scores showed high correlation with human labels. Specifications labeled "Strong" by humans typically scored above 0.66, while "Weak" specs scored below that threshold.
- Discovery of Benchmark Errors: The tool proved more reliable than human reviewers in several instances, identifying three cases where the "ground truth" human-provided test cases in the MBPP-DFY benchmark were actually incorrect (e.g., a test case expecting $5^3$ to be 25 instead of 125).
- Logical Nuance: A frequent failure mode for LLMs is the confusion between implication ($==>$) and equivalence ($<==>$). The symbolic testing approach effectively highlights these gaps by passing mutants that a more rigorous "if and only if" constraint would have caught.
Limitations and Future Work
While the framework demonstrates the feasibility of automated spec evaluation, several challenges remain:
- Parsing: The current reliance on regex limits the tool's ability to handle complex data structures like 2D arrays. Future iterations will utilize a proper Abstract Syntax Tree (AST) parser.
- Solver Constraints: Recursive specifications require "fuel" (unrolling depth) for the SMT solver to prove properties. Additionally, quantifier instantiation remains a hurdle, where solvers may fail to connect a general rule to a specific instance without redundant assertions.
Conclusion
This research provides a scalable, automated alternative to the manual review of formal specifications. By quantifying both soundness and precision through symbolic testing and mutation, the framework allows for the curation of higher-quality datasets and provides a more rigorous metric for the development of LLM-driven formal methods.
Summary updated