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 : a precondition on the states that reach the loop, a guard , a loop-free body , and a postcondition . States map the program variables to integers. Write for the weakest liberal precondition.
Input. A loop and a set of candidate invariants, given as formulas or as sets of states.
Output. An with
- initiation: ,
- consecution: ,
- safety: ,
or the answer that there is none. An that meets 1 and 2 is an inductive invariant; condition 3 makes it adequate for . These are the three verification conditions of the loop.
Fact (semantic completeness). When is the set of all sets of states, an adequate inductive invariant exists exactly when the triple holds for partial correctness. One direction is the soundness of the loop rule of Hoare logic. For the other, take , the set of states that some run from has at the loop head. . A state of runs the body to another state of . A state of is a final state of the loop, so it satisfies because the triple holds. is the least fixed point in the loop rule of the strongest postcondition page.
Variants
- Without a target. Find the strongest invariant in , or any invariant in , with no . Abstract interpretation answers this form.
- Template synthesis. is a fixed shape with unknown coefficients, such as , and the answer is a choice of coefficients. See constraint-based invariant generation.
- Candidate selection. 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 , where linear programming is available. An invariant that is inductive over need not be inductive over , so these methods can miss it.
- Other theories. Arrays and heaps need quantified invariants, such as "every element before is sorted", and pointer programs need separation logic.
Examples
- Division. For with : and : , the invariant is adequate. The weaker is inductive but not adequate. The VC generation page works it out.
- A true fact that is not inductive. For , every head state has . But alone is not inductive: from , one turn gives . The conjunction is inductive. Strengthening a true fact until it is inductive is the core of the problem.
- Strengthening for the exit. For with : and : , the formula is inductive, but at the exit it only gives . The adequate invariant is , and the exit then forces .
- Only a polynomial works. For with : and : , the adequate invariant is . No conjunction of linear inequalities over , , is adequate. Such a set is convex. The run with has the head states and , so a convex invariant also contains their midpoint . Consecution then forces the next state , where the loop exits with . So the methods for linear invariants fail here, and polynomial methods succeed.
- An invariant that is not convex. For with : , the loop never exits, so the triple holds. An adequate invariant must contain and and must not contain the exit state , their midpoint. So no conjunction of linear inequalities works, and " is odd" does.
- No invariant at all. With the body the loop exits at , the triple with : 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 .
- Houdini. Finds the largest inductive conjunction of given candidates with at most 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 , and tests , has an adequate inductive invariant (with 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 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 and go to ", and "if go to , else subtract 1 and go to ", and a halt instruction . Build the loop with variables , and : is , the guard is , the body is a chain of ifs on that runs one instruction, and is . Each turn of the loop is one step of the machine, so the loop exits exactly when the machine halts. The triple with 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.
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 that can define the set of head states, such as first-order arithmetic with and , 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 candidates, Houdini decides it with at most 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 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 , 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
- 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
- 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
- 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 56VVW9 Edit this page History Report an error Markdown