- 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 awhile loop is governed by the following rule of inference::
This means:
* A while loop does not have the side effect of falsifying —if the loop's body does not change an invariant from true to false given the condition , then will still be true after the loop has run as long as it was true before.
* runs as long as the condition is true—after the loop has run, if it terminates, is false.The rule above is a deductive step that has as its premise the
Hoare triple . This triple is actually a relation on machine states. It holds whenever starting from a state in which the boolean expression is true and successfully executing some program called "body", the machine ends up in a state in which is true. If this relation can be proven, the rule then allows us to conclude that successful execution of the programwhile (C) body
will lead from a state in which is true to a state in which 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:
:
The condition "C" of the
while
loop is . A useful loop invariant "I" is . Under these assumptions it is possible to prove the following Hoare triple::
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 is true, which means simply that is true. The computation adds 1 to x, which means that is still true (for integer x).
Under this premise, the rule for
while
loops permits the following conclusion::
However, the post-condition ("x" is less than or equal to 10, but it is not less than 10) is logically equivalent to , 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 awhile
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 , andClifford Stein . "Introduction to Algorithms ", Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Pages 17–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–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–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–224. 1999.
* Robert Paige. "Programming with Invariants." "IEEE Software", 3(1):56–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–172. October 2001.
* Michael Huth, Mark Ryan. "Logic in Computer Science.", Second Edition.
Wikimedia Foundation. 2010.