Counterexample-Guided Polynomial Loop Invariant Generation by Lagrange Interpolation

Yu-Fang Chen, Chih-Duo Hong, Bow-Yaw Wang, Lijun Zhang

Introduction

Verifying the correctness of probabilistic programs presents a unique challenge in formal methods. Unlike classical deterministic programs, where properties are typically binary (true or false), probabilistic programs require reasoning about quantitative specifications. These specifications are numerical functions that describe the expected values of variables—for instance, determining the expected number of iterations before a program terminates or the probability of reaching a specific state.

To formally prove these properties, researchers rely on quantitative loop invariants. While automated techniques for finding linear invariants (e.g., $x + y \leq 10$) are relatively mature, many fundamental probabilistic algorithms—such as the Miller-Rabin primality test or simulations of the Gambler’s Ruin—require polynomial invariants (e.g., $xy - x^2$). This paper introduces a novel method to automate the generation of these non-linear invariants, overcoming the computational bottlenecks that have hindered previous approaches.

The Challenge: Combinatorial Explosion

The standard approach to invariant synthesis involves a "template-based" method, where a polynomial is treated as a linear combination of all possible monomials. However, this approach suffers from a severe combinatorial explosion. For example, a quadratic polynomial over just five variables requires solving for 21 unknown coefficients. As the degree of the polynomial or the number of variables increases, the search space for automated tools becomes massive and often intractable, leading to timeouts or failures in existing tools like Prinsys.

Proposed Methodology

The authors propose a three-pronged strategy to make polynomial invariant generation scalable and efficient by moving away from traditional coefficient-heavy templates.

1. Multivariate Lagrange Interpolation

Instead of attempting to solve for every possible monomial coefficient simultaneously, the framework utilizes multivariate Lagrange interpolation. This technique represents a polynomial through its values at specific sampling points rather than its coefficients. By strategically choosing sampling points—particularly those that coincide with program boundaries—the solver can directly determine several parts of the invariant, significantly reducing the number of unknowns the SMT (Satisfiability Modulo Theory) solver must manage.

2. Counterexample-Guided Refinement (CEGAR)

The system operates within a Counterexample-Guided Refinement loop. It begins with a simplified set of constraints and uses an SMT solver to propose candidate invariants. If a candidate is found to be invalid, the system generates a "counterexample" (a specific program state where the invariant fails). This counterexample is then used to generate a new linear constraint, narrowing the search space for the next iteration and ensuring the solver progressively moves toward a valid solution.

3. Heuristic Verification Stack

To optimize performance, the tool employs a tiered verification strategy. Rather than invoking expensive formal proofs for every candidate, it first performs random experiments to quickly discard obviously incorrect invariants. Only candidates that pass these initial tests are passed to a formal verification engine, which uses Universal Quantifier Elimination (via tools like Redlog) to provide a rigorous proof that the invariant holds for all possible program states.

Experimental Evaluation

The authors evaluated their prototype on a benchmark of ten annotated loops derived from classic probabilistic problems, including:

  • Gambler’s Ruin: A foundational probability puzzle.
  • Statistical Distributions: Loops generating Geometric and Binomial distributions.
  • Fair Coin Simulation: Algorithms simulating fair outcomes from biased sources.
  • Non-linear Interactions: Loops involving products of dependent variables.

Key Results

  • Efficiency: The prototype synthesized polynomial invariants in an average of 15 seconds across 1,000 random runs.
  • Robustness: The tool achieved a 97.5% success rate within a 300-second timeout window.
  • Comparative Advantage: The method successfully generated non-linear invariants that previous state-of-the-art tools, such as Prinsys, struggled to find without manual assistance.

Discussion and Limitations

While this interpolation-based method represents a significant step forward, several areas for future work remain:

  • Fixed Degree Requirement: Users must currently specify the maximum degree of the polynomial (e.g., degree 2). If the required invariant is of a higher degree, the search will fail.
  • Constant Probabilities: The current implementation requires fixed numerical probabilities (e.g., 0.5). It cannot yet handle symbolic parameters (e.g., a variable $p$), as these introduce non-linearities that exceed the capabilities of current SMT solvers.
  • Non-determinism: The prototype does not yet support non-deterministic choices, which are common in complex concurrent models.

Conclusion

By reframing the invariant generation problem through the lens of Lagrange interpolation and integrating it into a CEGAR loop, the authors have demonstrated a way to bypass the combinatorial explosion of template-based synthesis. This approach allows for the automated verification of complex probabilistic programs that were previously beyond the reach of fully automated tools.

Summary updated

BibTeX
@inproceedings{chen2015counterexample,
  author = {Yu-Fang Chen and Chih-Duo Hong and Bow-Yaw Wang and Lijun Zhang},
  title = {{Counterexample-Guided Polynomial Loop Invariant Generation by Lagrange Interpolation}},
  booktitle = {CAV},
  year = {2015},
  doi = {10.1007/978-3-319-21690-4\_44},
}
Figure