Algorithm Search⌘K

Problemcore

Loop invariant synthesis

Also called invariant generation, invariant inference.

Find an inductive loop invariant strong enough to prove a program's specification. Undecidable in general, solved for restricted invariant shapes.

Loop invariant synthesis asks for a formula that holds at the head of a loop on every visit, is preserved by one turn of the body, and is strong enough to give the postcondition at the exit. Such an inductive invariant is exactly what verification condition generation needs at each loop, and finding it is the creative part of a correctness proof. The problem is undecidable in general, by a reduction from the halting problem. Every practical method restricts the shape of the invariant: conjunctions of given candidates, linear inequalities, polynomial equalities, or predicates from a fixed set.

In plain words

To prove that a loop does its job, you need a fact that is true every time the loop is about to run its body again. The fact must be true the first time. If it is true before one turn, it must be true after that turn. And when the loop stops, the fact must be enough to show the result is right. Finding such a fact is hard. A fact that is true is often not enough, because it is too weak to survive one turn on its own, so you must add more to it. Tools search for these facts by guessing many simple ones and throwing out the bad ones, by solving equations for unknown numbers in a fixed pattern, or by running a rough version of the program that only tracks shapes such as ranges of values.

Definition

A loop is a tuple (P,b,S,Q)(P, b, S, Q): a precondition PP on the states that reach the loop, a guard bb, a loop-free body SS, and a postcondition QQ. States map the program variables to integers. Write wlp(S,I)\mathrm{wlp}(S, I) for the weakest liberal precondition.

Input. A loop (P,b,S,Q)(P, b, S, Q) and a set L\mathcal{L} of candidate invariants, given as formulas or as sets of states.

Output. An I∈LI \in \mathcal{L} with

  1. initiation: P  ⟹  IP \implies I,
  2. consecution: I∧b  ⟹  wlp(S,I)I \wedge b \implies \mathrm{wlp}(S, I),
  3. safety: I∧¬b  ⟹  QI \wedge \neg b \implies Q,

or the answer that there is none. An II that meets 1 and 2 is an inductive invariant; condition 3 makes it adequate for QQ. These are the three verification conditions of the loop.

Fact (semantic completeness). When L\mathcal{L} is the set of all sets of states, an adequate inductive invariant exists exactly when the triple {P} while b do S {Q}\{P\}\ \mathbf{while}\ b\ \mathbf{do}\ S\ \{Q\} holds for partial correctness. One direction is the soundness of the loop rule of Hoare logic. For the other, take I=HI = H, the set of states that some run from PP has at the loop head. P⊆HP \subseteq H. A state of H∧bH \wedge b runs the body to another state of HH. A state of H∧¬bH \wedge \neg b is a final state of the loop, so it satisfies QQ because the triple holds. HH is the least fixed point in the loop rule of the strongest postcondition page.

Variants

  • Without a target. Find the strongest invariant in L\mathcal{L}, or any invariant in L\mathcal{L}, with no QQ. Abstract interpretation answers this form.
  • Template synthesis. L\mathcal{L} is a fixed shape with unknown coefficients, such as c1x+c2y+d≤0c_1 x + c_2 y + d \le 0, and the answer is a choice of coefficients. See constraint-based invariant generation.
  • Candidate selection. L\mathcal{L} is the set of conjunctions of a finite list of candidate formulas. See Houdini.
  • Programs, not single loops. Several loops, nested loops and procedures need an invariant at each cut point at once. The conditions become a system of constrained Horn clauses over unknown predicates.
  • Integers or rationals. Many methods work over Q\mathbb{Q}, where linear programming is available. An invariant that is inductive over Z\mathbb{Z} need not be inductive over Q\mathbb{Q}, so these methods can miss it.
  • Other theories. Arrays and heaps need quantified invariants, such as "every element before ii is sorted", and pointer programs need separation logic.

Examples

  • Division. For q:=0;r:=x;while y≤r do (r:=r−y;q:=q+1)q := 0; r := x; \mathbf{while}\ y \le r\ \mathbf{do}\ (r := r - y; q := q + 1) with PP: x≥0∧y>0x \ge 0 \wedge y > 0 and QQ: x=qy+r∧0≤r<yx = qy + r \wedge 0 \le r < y, the invariant x=qy+r∧r≥0x = qy + r \wedge r \ge 0 is adequate. The weaker x=qy+rx = qy + r is inductive but not adequate. The VC generation page works it out.
  • A true fact that is not inductive. For x:=0;y:=0;while ∗ do (x:=x+y;y:=y+1)x := 0; y := 0; \mathbf{while}\ \ast\ \mathbf{do}\ (x := x + y; y := y + 1), every head state has x≥0x \ge 0. But x≥0x \ge 0 alone is not inductive: from x=0x = 0, y=−5y = -5 one turn gives x=−5x = -5. The conjunction x≥0∧y≥0x \ge 0 \wedge y \ge 0 is inductive. Strengthening a true fact until it is inductive is the core of the problem.
  • Strengthening for the exit. For i:=0;j:=0;while i<n do (i:=i+1;j:=j+2)i := 0; j := 0; \mathbf{while}\ i < n\ \mathbf{do}\ (i := i + 1; j := j + 2) with PP: n≥0n \ge 0 and QQ: j=2nj = 2n, the formula j=2ij = 2i is inductive, but at the exit it only gives i≥n∧j=2ii \ge n \wedge j = 2i. The adequate invariant is j=2i∧i≤nj = 2i \wedge i \le n, and the exit then forces i=ni = n.
  • Only a polynomial works. For x:=0;y:=0;while x<n do (y:=y+2x+1;x:=x+1)x := 0; y := 0; \mathbf{while}\ x < n\ \mathbf{do}\ (y := y + 2x + 1; x := x + 1) with PP: n≥0n \ge 0 and QQ: y=n2y = n^2, the adequate invariant is y=x2∧x≤ny = x^2 \wedge x \le n. No conjunction of linear inequalities over xx, yy, nn is adequate. Such a set is convex. The run with n=2n = 2 has the head states (x,y,n)=(0,0,2)(x, y, n) = (0, 0, 2) and (2,4,2)(2, 4, 2), so a convex invariant also contains their midpoint (1,2,2)(1, 2, 2). Consecution then forces the next state (2,5,2)(2, 5, 2), where the loop exits with y=5≠4y = 5 \ne 4. So the methods for linear invariants fail here, and polynomial methods succeed.
  • An invariant that is not convex. For x:=1;while x≠0 do x:=x−2x := 1; \mathbf{while}\ x \ne 0\ \mathbf{do}\ x := x - 2 with QQ: false\mathit{false}, the loop never exits, so the triple holds. An adequate invariant must contain 11 and −1-1 and must not contain the exit state 00, their midpoint. So no conjunction of linear inequalities works, and "xx is odd" does.
  • No invariant at all. With the body x:=x−1x := x - 1 the loop exits at x=0x = 0, the triple with QQ: false\mathit{false} fails, and no adequate invariant exists in any language.

Known algorithms

  • Abstract interpretation. Computes an inductive invariant without a target, as a fixed point in an abstract domain: intervals, octagons or convex polyhedra (Cousot and Halbwachs), with a widening operator to stop. Sound and terminating, but the result may be too weak for QQ.
  • Houdini. Finds the largest inductive conjunction of cc given candidates with at most c+1c + 1 calls to a validity checker.
  • Constraint-based generation. Fixes a linear template and turns the three conditions into constraints on the coefficients by Farkas' lemma. Complete for the template over the rationals.
  • Predicate abstraction with CEGAR. Finds the strongest invariant that is a boolean combination of a set of predicates, and adds predicates from spurious counterexamples.
  • Interpolation and IC3/PDR. Build the invariant from proofs that short runs cannot reach a bad state; see interpolation-based model checking. k-induction proves the property with a weaker form of induction instead.
  • Polynomial equalities. For affine programs, Müller-Olm and Seidl compute all polynomial equalities up to a given degree by linear algebra, and Hrushovski, Ouaknine, Pouly and Worrell compute the strongest polynomial invariant of any degree. Gröbner bases are the tool for the general case.
  • Learning from data. Sample states, then fit a formula that separates good states from bad ones, and repair it with counterexamples from a solver, in the style of CEGIS. Syntax-guided synthesis has a track for invariant problems in this form.

Bounds

Theorem. It is undecidable whether a loop over integer variables, with assignments of the form x:=x+1x := x + 1, x:=x−1x := x - 1 and tests x=0x = 0, has an adequate inductive invariant (with L\mathcal{L} all sets of states). The set of loops that have one is co-recursively enumerable and not recursively enumerable.

Proof. By the fact of the definition, the question is whether {P} while b do S {Q}\{P\}\ \mathbf{while}\ b\ \mathbf{do}\ S\ \{Q\} holds. We reduce from the halting problem of two-counter machines, which Minsky proved undecidable; such a machine can simulate any Turing machine. A two-counter machine has a finite list of instructions: "add 1 to cic_i and go to kk", and "if ci=0c_i = 0 go to kk, else subtract 1 and go to k′k'", and a halt instruction hh. Build the loop with variables pc\mathit{pc}, c1c_1 and c2c_2: PP is pc=1∧c1=0∧c2=0\mathit{pc} = 1 \wedge c_1 = 0 \wedge c_2 = 0, the guard is pc≠h\mathit{pc} \ne h, the body is a chain of ifs on pc\mathit{pc} that runs one instruction, and QQ is false\mathit{false}. Each turn of the loop is one step of the machine, so the loop exits exactly when the machine halts. The triple with Q=falseQ = \mathit{false} holds exactly when the loop never exits, that is, when the machine does not halt. So deciding the existence of an invariant decides the halting problem. The complement, "the machine halts", is recursively enumerable and undecidable, so the set of loops with an invariant is co-r.e. and not r.e. □\square

This is also an instance of Rice's theorem: "the triple holds" is a property of what the program computes, and it is not trivial. The same proof works for any language L\mathcal{L} that can define the set HH of head states, such as first-order arithmetic with ++ and ×\times, which defines every set that a program reaches.

Decidable cases. The problem becomes decidable when the search space is finite or has a decidable structure, given a decision procedure for the three conditions:

  • For conjunctions of cc candidates, Houdini decides it with at most c+1c + 1 calls, when consecution is checked by a decision procedure, for example for Presburger arithmetic.
  • For a fixed linear template on a loop with linear guards and updates, over the rationals, the Farkas constraints are a formula of the first-order theory of the real numbers, which is decidable by the Tarski-Seidenberg theorem, for example by cylindrical algebraic decomposition.
  • For Presburger invariants of a loop with linear guards and updates, the three conditions are Presburger formulas, so enumerating formulas and checking them is a semi-decision procedure: it finds an invariant when one exists, and may run forever otherwise.

Reductions

  • To validity. Checking a proposed II is three validity questions, so synthesis reduces to search plus a decision procedure, which is the shape of Houdini and of CEGIS.
  • From safety verification. A program is safe exactly when its loops have adequate inductive invariants (semantic completeness), so every model checking method for infinite-state programs is, in effect, an invariant synthesizer. Constrained Horn clause solvers state the problem in this form.
  • From termination. A proof of termination with a ranking function often needs a supporting invariant, such as y>0y > 0, before the ranking function decreases. Tools synthesize both together.

Applications

Deductive verifiers ask the programmer for invariants and use synthesis to fill the easy ones. ESC/Java used Houdini for this. Static analyzers for safety-critical code compute numeric invariants with abstract interpretation to rule out overflow and out-of-bounds access. Compilers use simple invariants, such as the range of an induction variable, to remove bounds checks. Termination provers and proof-carrying code producers need invariants to build their certificates.

References

  1. Patrick Cousot and Nicolas Halbwachs, Automatic discovery of linear restraints among variables of a program, Proceedings of POPL 1978, pp. 84 to 96, the convex polyhedra analysis with widening
  2. Cormac Flanagan and K. Rustan M. Leino, Houdini, an annotation assistant for ESC/Java, Proceedings of FME 2001, LNCS 2021, pp. 500 to 517, the guess-and-check loop over candidate annotations
  3. Michael A. Colón, Sriram Sankaranarayanan and Henny B. Sipma, Linear invariant generation using non-linear constraint solving, Proceedings of CAV 2003, LNCS 2725, pp. 420 to 432, Sections 2 and 3 (inductive assertion maps, templates and the Farkas constraints) and Theorem 2
  4. Markus Müller-Olm and Helmut Seidl, Computing polynomial program invariants, Information Processing Letters 91(5), 2004, pp. 233 to 244, the computation of all polynomial equalities of bounded degree for affine programs
  5. Ehud Hrushovski, Joël Ouaknine, Amaury Pouly and James Worrell, Polynomial invariants for affine programs, Proceedings of LICS 2018, pp. 530 to 539, the algorithm for the strongest polynomial invariant of an affine program

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a known algorithm
  • a contract.yaml