Learning Nonlinear Loop Invariants with Gated Continuous Logic Networks

Jianan Yao, Gabriel Ryan, Justin Wong, Suman Jana, Ronghui Gu

Abstract

Discovering loop invariants—the logical properties that remain true throughout a loop’s execution—is a fundamental challenge in automated program verification. This paper introduces Gated Continuous Logic Networks (G-CLNs), a neural architecture designed to learn complex, nonlinear loop invariants directly from program execution data. By combining gating mechanisms, fractional sampling, and a specialized activation function, G-CLNs overcome traditional hurdles such as search space explosion and data scarcity. The approach achieved state-of-the-art results, solving 26 out of 27 difficult nonlinear benchmark problems and demonstrating perfect generalization to linear benchmarks.

The Challenge of Nonlinear Invariants

To prove a program is correct, formal verification tools require loop invariants to explain the loop's behavior at every step. While linear invariants (e.g., $x + y < 10$) are relatively easy to find, nonlinear invariants involving powers like $x^2$ or $x^3$ are significantly more difficult. These invariants are critical in fields like robotics and avionics but present three major challenges:

  1. Search Space Explosion: The number of possible polynomial terms grows exponentially with the degree.
  2. Data Scarcity: Programs often provide very few discrete data points during execution.
  3. The Tightness Problem: Verification requires the tightest possible logical bound, but neural networks often struggle to converge on exact boundaries.

Methodology: Gated Continuous Logic Networks

The authors build upon Continuous Logic Networks (CLNs), which use differentiable "fuzzy logic" to allow logical formulas to be learned via gradient descent. The G-CLN architecture introduces three key innovations to automate the discovery of formula structures.

The G-CLN framework: Trace collection, neural invariant generation, and formal verification with Z3.

As shown in the framework above, the system operates in a loop. It collects execution traces from source code, uses a G-CLN to generate candidate invariants, and then verifies these candidates using the Z3 SMT solver. If a candidate is rejected, Z3 provides counterexamples that are fed back into the trace collection phase to refine the model.

Key Innovations

  • Gating Units and Term Dropout: G-CLNs use gating units to act as switches for specific terms. Combined with Term Dropout—randomly ignoring variables during training—the model is forced to prune away unnecessary complexity and find the simplest possible invariant structure.
  • Fractional Sampling: To solve the data scarcity problem, the authors relax discrete integer logic into the continuous real domain. This allows the model to sample the loop's behavior at "fractional" steps (e.g., iteration 0.5), providing the dense data needed for effective neural training.
  • Piecewise Biased Quadratic Unit (PBQU): This custom activation function is designed to punish the model for "loose" fits, forcing it to converge on the tightest possible inequality required for successful verification.
Illustration of the tightness problem and fractional sampling in a square root computation loop.

Experimental Results

The researchers evaluated G-CLN on the Nonlinear Loop Invariant (NLA) benchmark and the Code2Inv linear benchmark.

  • State-of-the-Art Performance: G-CLN solved 26 out of 27 nonlinear problems, including complex algorithms for modular division and geometric series. This outperformed prior specialized tools like NumInv (23/27).
  • Efficiency and Generalization: The average time to find an invariant was just 53.3 seconds. Despite its focus on nonlinear problems, G-CLN solved 100% (124/124) of the linear problems in the Code2Inv benchmark.
  • Stability: The architecture showed a 97.5% convergence rate on quadratic problems, a 39.2% improvement over standard CLN models, proving that gating and regularization are essential for training stability.

Conclusion and Future Work

G-CLNs represent a significant step forward in bridging the gap between discrete program logic and continuous deep learning. By treating loop invariants as learnable structures, the authors have removed the need for manual templates. While the system currently struggles with exceptionally complex modular arithmetic (like the "Knuth" problem), future work will focus on extending fractional sampling to handle more complex data structures and higher-order polynomials.

Summary updated

BibTeX
@inproceedings{yao2020learning,
  author = {Jianan Yao and Gabriel Ryan and Justin Wong and Suman Jana and Ronghui Gu},
  title = {{Learning Nonlinear Loop Invariants with Gated Continuous Logic Networks}},
  booktitle = {PLDI},
  year = {2020},
  doi = {10.1145/3385412.3385986},
}
Figure