Can Large Language Models Reason about Program Invariants?

Kexin Pei, David Bieber, Kensen Shi, Charles Sutton, Pengcheng YinICML 2023

Identifying program invariants—logical rules that always hold true at specific points during execution—is a foundational task in software engineering. Invariants are essential for finding bugs, identifying security vulnerabilities, and conducting formal verification. However, traditional methods for discovering them are fraught with challenges. Static analysis is often mathematically undecidable or computationally exhausting for complex programs, while dynamic analysis relies heavily on execution traces, making it slow and entirely dependent on the quality of test coverage.

Recent research explores a compelling alternative: Can we train Large Language Models (LLMs) to "read" source code and intuitively predict these invariants statically, bypassing the need for expensive execution traces? By employing a "scratchpad" approach that mimics step-by-step reasoning, researchers have demonstrated that fine-tuned LLMs can achieve impressive precision and recall, performing on par with traditional dynamic analysis tools that require multiple real-world execution traces.

Methodology: Teaching LLMs to Reason

To investigate this, researchers fine-tuned a series of Transformer-based models, ranging from 430 million to 5 billion parameters, specifically for invariant prediction. They explored two primary prompting strategies:

  1. Direct Prediction: The model is provided with the source code and a specific line number, then asked to list all invariants for that exact point.
  2. Scratchpad Prediction: Inspired by how humans and abstract interpretation tools reason, the model predicts invariants sequentially. It starts at the beginning of the function and predicts invariants for every major program point (such as loop entries) leading up to the target line. This forces the model to "track" the program state as it moves through the code.

These strategies were compared against an Oracle baseline, where the model is given ground-truth invariants for previous lines to determine the upper bound of performance when perfect context is available.

To train and evaluate these models, the team created the Code Contests Java Invariants dataset. They extracted Java solutions from competitive programming problems and used Daikon—a traditional dynamic analyzer—running on 200 different inputs per program to establish robust ground-truth invariants. The resulting dataset is massive, containing approximately 1.6 million training examples.

Performance and Scaling

The most significant insight from the study is that LLMs can successfully perform invariant prediction as a static analysis task. The models learn to generalize the semantics of the code rather than merely matching text patterns.

Performance metrics across model sizes and prompting strategies.

As shown in the performance metrics, scaling laws clearly apply: larger models (5B parameters) are significantly better at inferring non-trivial invariants involving multiple steps of computation compared to smaller models. Furthermore, the scratchpad approach significantly outperforms direct prediction. By predicting intermediate invariants, the model effectively performs a "mental" version of abstract interpretation, building a logical chain of thought that leads to higher precision and recall. The best fine-tuned models achieved an impressive 86% precision and 86% recall on the test set.

Interestingly, the researchers found that zero-shot prompting with off-the-shelf models like GPT-4 failed completely, highlighting that while LLMs possess the capacity for this reasoning, specific fine-tuning on program semantics is currently essential.

Comparing LLMs to Dynamic Analysis

A critical question is how this static LLM approach compares to traditional dynamic analysis. Because dynamic tools like Daikon rely on execution traces, their performance scales with the number of test cases provided.

Comparison of LLM static prediction versus Daikon dynamic analysis across varying numbers of execution traces.

The results demonstrate that the 5B parameter model using the scratchpad approach produces invariants of comparable quality to Daikon when Daikon is limited to five execution traces. In many real-world scenarios, obtaining even five high-quality, high-coverage traces is difficult. This makes the LLM approach highly competitive, as it requires zero execution traces to achieve the same baseline of performance.

Furthermore, the LLM sometimes outperformed its own "teacher" (Daikon) in terms of generalization. While Daikon might suggest a hyper-specific invariant based on limited traces (e.g., x == 7), the LLM might suggest a more logical, generalized invariant (e.g., x > 0) that holds true even for inputs Daikon never saw during training.

Limitations and Future Directions

While promising, the approach has limitations. Because the training data relies on Daikon, the "ground truth" may contain spurious invariants that hold true for the 200 test cases but not for all possible inputs. Additionally, the current study focused exclusively on Java and a specific set of 18 invariant types (such as arithmetic and nullity).

Future work will likely expand this to other languages like C++ and Python, and tackle more complex heap and pointer invariants. Practically, these models could soon be integrated into automated test generation tools to provide better constraints for finding bugs. Ultimately, using LLM-predicted invariants as "hints" for formal SMT solvers could bridge the gap between the intuitive pattern-matching of AI and the rigorous mathematical proofs of formal logic.

Summary updated

BibTeX
@inproceedings{pei2023can,
  author = {Kexin Pei and David Bieber and Kensen Shi and Charles Sutton and Pengcheng Yin},
  title = {{Can Large Language Models Reason about Program Invariants?}},
  booktitle = {International Conference on Machine Learning},
  year = {2023},
  url = {https://openreview.net/forum?id=ewlorW0Wj5},
}
Figure