Loop invariant

Loop invariant

In computer science, a loop invariant is an invariant used to prove properties of loops.

Specifically in Floyd-Hoare logic, the partial correctness of a while loop is governed by the following rule of inference:

:frac{{Cland I};mathrm{body};{I {{I};mathbf{while} (C) mathrm{body};{lnot Cland I

This means:
* A while loop does not have the side effect of falsifying I—if the loop's body does not change an invariant I from true to false given the condition C, then I will still be true after the loop has run as long as it was true before.
* while(C) ... runs as long as the condition C is true—after the loop has run, if it terminates, C is false.

The rule above is a deductive step that has as its premise the Hoare triple {Cland I};mathrm{body};{I}. This triple is actually a relation on machine states. It holds whenever starting from a state in which the boolean expression Cland I is true and successfully executing some program called "body", the machine ends up in a state in which I is true. If this relation can be proven, the rule then allows us to conclude that successful execution of the program while (C) body will lead from a state in which I is true to a state in which lnot Cland I holds. The boolean formula "I" in this rule is known as the loop invariant.

The following example illustrates how this rule works. Consider the program

while (x<10) x:= x+1;

One can then prove the following Hoare triple:

:{xleq10}; mathbf{while} (x<10) x := x+1;{x=10}

The condition "C" of the while loop is x<10. A useful loop invariant "I" is xleq10. Under these assumptions it is possible to prove the following Hoare triple:

:{x<10 land xleq10}; x := x+1 ;{xleq10}

While this triple can be derived formally from the rules of Floyd-Hoare logic governing assignment, it is also intuitively justified: Computation starts in a state where x<10 land xleq10 is true, which means simply that x<10 is true. The computation adds 1 to x, which means that xleq10 is still true (for integer x).

Under this premise, the rule for while loops permits the following conclusion:

:{xleq10}; mathbf{while} (x<10) x := x+1 ;{lnot(x<10) land xleq10}

However, the post-condition lnot(x<10)land xleq10 ("x" is less than or equal to 10, but it is not less than 10) is logically equivalent to x=10, which is what we wanted to show.

The loop invariant plays an important role in the intuitive argument for soundness of the Floyd-Hoare rule for while loops. The loop invariant has to be true before each iteration of the loop body, and also after each iteration of the loop body. Since a while loop is precisely the repeated iteration of the loop body, it follows that if the invariant is true before entering the loop, it must also be true after exiting the loop.

Because of the fundamental similarity of loops and recursive programs, proving partial correctness of loops with invariants is very similar to proving correctness of recursive programs via induction. In fact, the loop invariant is often the inductive property one has to prove of a recursive program that is equivalent to a given loop.

The for loop is the much safer construct to use, as it establishes the initial value of the loop variable, an exit test condition, and an invariant increment for the loop variable, rendering much of this moot when used correctly.

See also

* Loop variant

References

* Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. "Introduction to Algorithms", Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Pages 17&ndash;19 of section 2.1: Insertion sort.
* R. W. Floyd. " [http://laser.cs.umass.edu/courses/cs521-621.Spr06/readlings/Floyd.pdf Assigning meanings to programs.] " Proceedings of the American Mathematical Society Symposia on Applied Mathematics. Vol. 19, pp. 19–31. 1967.
* David Gries. "A note on a standard strategy for developing loop invariants and loops." "Science of Computer Programming", vol 2, pp.207&ndash;214. 1984.
* C. A. R. Hoare. " [http://sunnyday.mit.edu/16.355/Hoare-CACM-69.pdf An axiomatic basis for computer programming] ". "Communications of the ACM", 12(10):576&ndash;585, October 1969. doi|10.1145/363235.363259
* Michael D. Ernst, Jake Cockrell, William G. Griswold, David Notkin. " [http://citeseer.ist.psu.edu/292512.html Dynamically Discovering Likely Program Invariants to Support Program Evolution] ." "International Conference on Software Engineering", pp.213&ndash;224. 1999.
* Robert Paige. "Programming with Invariants." "IEEE Software", 3(1):56&ndash;69. January 1986.
* Yanhong A. Liu, Scott D. Stoller, and Tim Teitelbaum. [Strengthening Invariants for Efficient Computation http://www.cs.sunysb.edu/~stoller/SIEC-SCP.html] . "Science of Computer Programming", 41(2):139&ndash;172. October 2001.
* Michael Huth, Mark Ryan. "Logic in Computer Science.", Second Edition.


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • loop invariant — ciklo invariantas statusas T sritis informatika apibrėžtis ↑Teiginys, kuris yra teisingas prieš atliekant ciklą, nesikeičia (t. y. lieka teisingas) pakartojus kiekvieną ciklo veiksmą ir taip pat lieka teisingas baigus vykdyti ciklą. Plačiau žr.… …   Enciklopedinis kompiuterijos žodynas

  • Loop-invariant code motion — Loop invariant code in an imperative programming language consists of statements which could be moved to before the loop (if the loop always terminates), or after the loop, without affecting the semantics of the program. As a result it is… …   Wikipedia

  • Loop inversion — is a compiler optimization, a loop transformation, which replaces a while loop by an if block containing a do..while loop. Example in C int i, a [100] ; i = 0; while (i < 100) { a [i] = 0; i++; }is equivalent to: int i, a [100] ; i = 0; if (i …   Wikipedia

  • Loop variant — In computer science, a loop variant is a mathematical function defined on the state space of a computer program having the property that each iteration of a loop (given its invariant) strictly decreases its value with respect to a well founded… …   Wikipedia

  • Invariant (computer science) — In computer science, a predicate that, if true, will remain true throughout a specific sequence of operations, is called (an) invariant to that sequence.UseAlthough computer programs are typically mainly specified in terms of what they change, it …   Wikipedia

  • Loop optimization — In compiler theory, loop optimization plays an important role in improving cache performance, making effective use of parallel processing capabilities, and reducing overheads associated with executing loops. Most execution time of a scientific… …   Wikipedia

  • Loop quantum gravity — Not to be confused with the path integral formulation of LQG, see spin foam. This article is about LQG in its Canonical formulation.. Beyond the Standard Model …   Wikipedia

  • Loop-erased random walk — In mathematics, loop erased random walk is a model for a random simple path with important applications in combinatorics and, in physics, quantum field theory. It is intimately connected to the uniform spanning tree, a model for a random tree.… …   Wikipedia

  • Wilson loop — In gauge theory, a Wilson loop (named after Kenneth Wilson) is a gauge invariant observable obtained from the holonomy of the gauge connection around a given loop. In the classical theory, the collection of all Wilson loops contains sufficient… …   Wikipedia

  • History of loop quantum gravity — General relativity is the theory of gravitation published by Albert Einstein in 1915. According to it, the force of gravity is a manifestation of the local geometry of spacetime. Mathematically, the theory is modelled after Bernhard Riemann s… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”