Learning Loop Invariants for Program Verification

Xujie Si, Hanjun Dai, Mukund Raghothaman, Mayur Naik, Le SongNeurIPS 2018

TL;DR

CODE2INV is a reinforcement learning framework that automates the "black art" of finding loop invariants—the logical keys needed to prove a program is correct. By representing programs as graphs and learning through trial-and-error with a theorem prover, it outperforms traditional tools and, for the first time, demonstrates that an AI can learn a generalizable strategy for program verification.

What Problem Does This Paper Address?

In the world of software verification, proving that a program is "correct" (i.e., it won't crash or behave unexpectedly) often hinges on loop invariants. A loop invariant is a logical statement that remains true before, during, and after a loop runs. If you can find the right invariant, a computer can automatically prove the rest of the program's properties.

However, finding these invariants is mathematically undecidable—there is no universal algorithm that works for every program. Traditionally, this required human experts or "search-based" tools that guess formulas blindly. These traditional tools have two major flaws:

  1. They don't learn: If a tool fails on one program, it doesn't get any smarter for the next one.
  2. They rely on "hand-crafted" features: They can only find invariants that fit a very specific, human-defined template.

Methods

The authors propose CODE2INV, an end-to-end neural reasoning framework. It mimics how a human expert works: first "reading" the code to understand its structure, then "writing" the invariant step-by-step.

CODE2INV Reasoning Framework
  • Structured External Memory (The "Reader"): Instead of treating code like a simple string of text, CODE2INV uses a Graph Neural Network (GNN). It converts the program into a graph that captures control flow (how the program jumps around) and data flow (how variables change). This graph acts as a memory block the model can query.
  • Multi-step Decision Making (The "Writer"): The model builds the invariant (a logical tree) one piece at a time using an autoregressive TreeLSTM. At each step, it uses an attention mechanism to "look" at relevant parts of the program graph.
  • Reinforcement Learning (The "Teacher"): Since there is no "correct" answer key for invariants, the model uses Advantage Actor-Critic (A2C). It proposes an invariant to an automated theorem prover (Z3).
  • If Z3 says "Correct," the model gets a reward.
  • If Z3 says "Wrong," it provides a counterexample (a specific case where the invariant failed). CODE2INV uses these counterexamples as fine-grained feedback to refine its strategy.

Datasets

The researchers evaluated the system on:

  • 133 Benchmark Problems: A suite of challenging loops pulled from prior verification literature and the 2017 SyGuS (Syntax-Guided Synthesis) competition.
  • Synthetic Generalization Dataset: To test if the model actually "learned" to verify rather than just memorizing, they created thousands of variations of these programs by adding "confounding variables"—extra, irrelevant code that makes the problem look different but keeps the underlying logic the same.

Key Insights and Ideas

  • Programs are Graphs, not Sequences: The jump in performance from using a standard LSTM (text-based) to a GNN (graph-based) was massive. Capturing the semantic structure of code is non-negotiable for complex reasoning.
  • Learning from Failure: Most search tools throw away failed attempts. CODE2INV treats every failed check by the theorem prover as a valuable data point. By accumulating counterexamples, the model builds a "mental map" of the state space it needs to cover.
  • Generalization is Possible: This is a major breakthrough. The authors showed that a model pre-trained on one set of programs could solve new, unseen programs much faster. This suggests the model isn't just searching; it's learning the logic of how invariants relate to code patterns.

Key Results

  • Superior Solving Power: CODE2INV solved 106 out of 133 problems. For context, the previous state-of-the-art stochastic search (C2I) solved 73, and a specialized heuristic search (LoopInvGen) solved 77.
Performance and Efficiency of CODE2INV
  • Efficiency: To solve the same number of problems, CODE2INV required orders of magnitude fewer queries to the theorem prover than its competitors. This is crucial because theorem proving is computationally expensive.
  • Sample Efficiency: When pre-trained, the agent was significantly more efficient at finding solutions for new instances compared to starting from scratch, proving the transferability of its learned strategies.

Limitations and Future Work

  • The "DNF" Problem: CODE2INV currently generates invariants in Conjunctive Normal Form (CNF). Some complex invariants are much smaller and easier to find in Disjunctive Normal Form (DNF). The current model can struggle with the "exponential blowup" when trying to force a DNF-style logic into a CNF shape.
  • Confounding Variables: While the model generalizes well, its performance degrades as the amount of "noise" (irrelevant code) increases. Improving the attention mechanism to filter out this noise is a key next step.
  • Beyond Arithmetic: The current work focuses on loops with integers and arithmetic. The authors aim to extend this to pointer-manipulating programs (like linked lists or trees) and proving program termination.

Summary updated

BibTeX
@inproceedings{si2018learning,
  author = {Xujie Si and Hanjun Dai and Mukund Raghothaman and Mayur Naik and Le Song},
  title = {{Learning Loop Invariants for Program Verification}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2018},
  url = {https://openreview.net/forum?id=BJEAEuZOWS},
}
Figure