Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is...

45
Loop Invariants: Part 1 7 January 2019 OSU CSE 1

Transcript of Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is...

Page 1: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Loop Invariants: Part 1

7 January 2019 OSU CSE 1

Page 2: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Reasoning About Method Calls• What a method call does is described by

its contract– Precondition: a property that is true before the

call is made– Postcondition: a property that is true after the

call returns

7 January 2019 OSU CSE 2

Page 3: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Reasoning About Loops• What a while loop does is described by

its loop invariant– Invariant: a property that is true every time the

code reaches a certain point—in the case of a loop invariant, the loop condition test

7 January 2019 OSU CSE 3

Page 4: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Reasoning About Loops• What a while loop does is described by

its loop invariant– Invariant: a property that is true every time the

code reaches a certain point—in the case of a loop invariant, the loop condition test

7 January 2019 OSU CSE 4

Why is a loop treated differently than a method call? Simply put, experience shows this is a good

way to think about loops.

Page 5: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Reasoning About Loops• What a while loop does is described by

its loop invariant– Invariant: a property that is true every time the

code reaches a certain point—in the case of a loop invariant, the loop condition test

7 January 2019 OSU CSE 5

Just while loops?Yes; the same idea can be applied

to for loops, but some modifications are required.

Page 6: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Reasoning About Loops• What a while loop does is described by

its loop invariant– Invariant: a property that is true every time the

code reaches a certain point—in the case of a loop invariant, the loop condition test

7 January 2019 OSU CSE 6

Since a loop invariant is true every time through the loop, it says what does not change;

hence it really says what the loop does not do.

Page 7: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

while Statement Control Flow

test false

true

loop-body

while (test) {

loop-body

}

7 January 2019 OSU CSE 7

Page 8: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

while Statement Control Flow

test false

true

loop-body

while (test) {

loop-body

}

7 January 2019 OSU CSE 8

The loop invariant is a property that is true both here, just before the loop begins...

Page 9: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

while Statement Control Flow

test false

true

loop-body

while (test) {

loop-body

}

7 January 2019 OSU CSE 9

... and here, just after everyexecution of the loop body.

Page 10: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Example #1

void append(Queue<T> q)

• Concatenates (“appends”) q to the end of this.

• Updates: this• Clears: q• Ensures:this = #this * #q

7 January 2019 OSU CSE 10

Page 11: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Example #1: Method Body

while (q.length() > 0) {

T x = q.dequeue();

this.enqueue(x);}

7 January 2019 OSU CSE 11

Page 12: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Example #1: Method Body

while (q.length() > 0) {

T x = q.dequeue();

this.enqueue(x);}

7 January 2019 OSU CSE 12

What is true every time we test the loop condition?

Lots of things... such as?

Page 13: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

7 January 2019 OSU CSE 13

this = < 1, 2, 3 >q = < 4, 5, 6 >

while (q.length() > 0) {

T x = q.dequeue();

this.enqueue(x);

}

Page 14: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

7 January 2019 OSU CSE 14

this = < 1, 2, 3 >q = < 4, 5, 6 >

while (q.length() > 0) {

T x = q.dequeue();

this.enqueue(x);

}

What is true the first time we test the loop condition?

Lots of things... such as?

Page 15: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

7 January 2019 OSU CSE 15

this = < 1, 2, 3 >q = < 4, 5, 6 >

while (q.length() > 0) {

this = < 1, 2, 3 >q = < 4, 5, 6 >

T x = q.dequeue();

this = < 1, 2, 3 >q = < 5, 6 >x = 4

this.enqueue(x);

this = < 1, 2, 3, 4 >q = < 5, 6 >x = 4

}

Page 16: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

7 January 2019 OSU CSE 16

this = < 1, 2, 3 >q = < 4, 5, 6 >

while (q.length() > 0) {

this = < 1, 2, 3 >q = < 4, 5, 6 >

T x = q.dequeue();

this = < 1, 2, 3 >q = < 5, 6 >x = 4

this.enqueue(x);

this = < 1, 2, 3, 4 >q = < 5, 6 >x = 4

}

What is true the first and second times we test the loop

condition?Fewer things... such as?

Page 17: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

7 January 2019 OSU CSE 17

this = < 1, 2, 3 >q = < 4, 5, 6 >

while (q.length() > 0) {

this = < 1, 2, 3 >q = < 4, 5, 6 >

T x = q.dequeue();

this = < 1, 2, 3 >q = < 5, 6 >x = 4

this.enqueue(x);

this = < 1, 2, 3, 4 >q = < 5, 6 >x = 4

}

The value of x is not involved in the loop invariant because there is no x when we first hit

the loop!

Page 18: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

7 January 2019 OSU CSE 18

this = < 1, 2, 3 >q = < 4, 5, 6 >

while (q.length() > 0) {

this = < 1, 2, 3, 4 >q = < 5, 6 >

T x = q.dequeue();

this = < 1, 2, 3, 4 >q = < 6 >x = 5

this.enqueue(x);

this = < 1, 2, 3, 4, 5 >q = < 6 >x = 5

}

Page 19: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

7 January 2019 OSU CSE 19

this = < 1, 2, 3 >q = < 4, 5, 6 >

while (q.length() > 0) {

this = < 1, 2, 3, 4 >q = < 5, 6 >

T x = q.dequeue();

this = < 1, 2, 3, 4 >q = < 6 >x = 5

this.enqueue(x);

this = < 1, 2, 3, 4, 5 >q = < 6 >x = 5

}

What is true the first, second, and third times we test the

loop condition?Fewer things still... such as?

Page 20: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

7 January 2019 OSU CSE 20

this = < 1, 2, 3 >q = < 4, 5, 6 >

while (q.length() > 0) {

this = < 1, 2, 3, 4, 5 >q = < 6 >

T x = q.dequeue();

this = < 1, 2, 3, 4, 5 >q = < >x = 6

this.enqueue(x);

this = < 1, 2, 3, 4, 5, 6 >q = < >x = 6

}

Page 21: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

7 January 2019 OSU CSE 21

this = < 1, 2, 3 >q = < 4, 5, 6 >

while (q.length() > 0) {

this = < 1, 2, 3, 4, 5 >q = < 6 >

T x = q.dequeue();

this = < 1, 2, 3, 4, 5 >q = < >x = 6

this.enqueue(x);

this = < 1, 2, 3, 4, 5, 6 >q = < >x = 6

}

What is true the first, second, third, and fourth times we test

the loop condition?Fewer things still... such as?

Page 22: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

7 January 2019 OSU CSE 22

this = < 1, 2, 3 >q = < 4, 5, 6 >

while (q.length() > 0) {

this = < 1, 2, 3, 4, 5 >q = < 6 >

T x = q.dequeue();

this = < 1, 2, 3, 4, 5 >q = < >x = 6

this.enqueue(x);

this = < 1, 2, 3, 4, 5, 6 >q = < >x = 6

}

Whatever is true the last time we test the loop condition is also true here, after the loop

finally terminates.

Page 23: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Some Things That Do Not Change• “The lengths of the strings are non-

negative” does not change– |this| >= 0 and |q| >= 0 does not

change– True, but this literally goes without saying; the

length of any string is always non-negative– It is no more useful than saying, e.g., “17 < 42 does not change”, because it is a mathematical fact, not something about this loop in particular

7 January 2019 OSU CSE 23

Page 24: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Some Things That Do Not Change

• “The sum of the lengths of the strings” does not change– |this| + |q| does not change– True, and a useful observation about this

particular loop; but one can say more

7 January 2019 OSU CSE 24

Page 25: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Some Things That Do Not Change

• “The concatentation of the strings” does not change– this * q does not change– True, and a stronger useful observation

about this loop because it implies the previous observation about the sum of the lengths

• In other words, if this * q does not change, then |this| + |q| also does not change; but not vice versa

7 January 2019 OSU CSE 25

Page 26: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

How To Express an Invariant

• How do we say “the concatenation of the strings does not change”?– We need to talk about both:

• The current values of the variables• The original values of the variables, just before

the loop condition was first tested (variable names prefixed with #)

this * q = #this * #q

7 January 2019 OSU CSE 26

Page 27: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Example #1: Method Body/**

* @updates this, q* @maintains

* this * q = #this * #q

*/

while (q.length() > 0) {

T x = q.dequeue();

this.enqueue(x);}

7 January 2019 OSU CSE 27

Page 28: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Example #1: Method Body/**

* @updates this, q* @maintains

* this * q = #this * #q

*/

while (q.length() > 0) {

T x = q.dequeue();

this.enqueue(x);}

7 January 2019 OSU CSE 28

This Javadoc tag introduces the list of

variables whose values might change in some

iteration.

Page 29: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Example #1: Method Body/**

* @updates this, q* @maintains

* this * q = #this * #q

*/

while (q.length() > 0) {

T x = q.dequeue();

this.enqueue(x);}

7 January 2019 OSU CSE 29

Any variable in scope that is not listed as an updates-

mode variable is, by default, a restores-mode variable,

meaning the loop body does not change its value.

Page 30: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Example #1: Method Body/**

* @updates this, q* @maintains

* this * q = #this * #q

*/

while (q.length() > 0) {

T x = q.dequeue();

this.enqueue(x);}

7 January 2019 OSU CSE 30

This Javadoc tag introduces the claim that

the following loop “maintains” the property, i.e., it is a loop invariant.

Page 31: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Example #1: Method Body/**

* @updates this, q* @maintains* this * q = #this * #q

*/

while (q.length() > 0) {

T x = q.dequeue();

this.enqueue(x);}

7 January 2019 OSU CSE 31

Page 32: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Using a Loop Invariant

• If you have a strong enough loop invariant, you can trace over a loop in a single step, and predict the values of the variables when it terminates—without tracing through the loop body even once

7 January 2019 OSU CSE 32

Page 33: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Example #1: Method Body/**

* @updates this, q* @maintains* this * q = #this * #q

*/

while (q.length() > 0) {

...

}

7 January 2019 OSU CSE 33

Pretend you cannot see the loop body. Can you still trace

over this loop?

Page 34: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

7 January 2019 OSU CSE 34

this = < 1, 2, 3 >q = < 4, 5, 6 >

/*** @maintains* this * q = #this * #q*/

while (q.length() > 0) {

...

}

this = q =

When execution reaches this point, we know two

things...

Page 35: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

7 January 2019 OSU CSE 35

this = < 1, 2, 3 >q = < 4, 5, 6 >

/*** @maintains* this * q = #this * #q*/

while (q.length() > 0) {

...

}

this = q =

We know (1) the loop invariant is true, so:

this * q = #this * #q

Page 36: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

7 January 2019 OSU CSE 36

this = < 1, 2, 3 >q = < 4, 5, 6 >

/*** @maintains* this * q = #this * #q*/

while (q.length() > 0) {

...

}

this = q =

We also know (2) the loop condition is false, so:

|q| <= 0

Page 37: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

7 January 2019 OSU CSE 37

this = < 1, 2, 3 >q = < 4, 5, 6 >

/*** @maintains* this * q = #this * #q*/

while (q.length() > 0) {

...

}

this = < 1, 2, 3, 4, 5, 6 >q = < >

Combining (1) and (2), the only values the variables can possibly have at this

point are these.

Page 38: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Justification for (1)

• The loop invariant is true just after the loop terminates—if the code that tests the loop condition does not change the value of any variable appearing in the loop invariant

7 January 2019 OSU CSE 38

Page 39: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

The Loop Invariant Picture

test false

true

loop-body

while (test) {

loop-body

}

7 January 2019 OSU CSE 39

If the loop invariant is trueat the two red points,

and “test” updates nothing...

...then the loop invariant is trueat the two green points.

Page 40: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Justification for (1)

• Best practice: Code that tests the loop condition should not update any variables appearing in the loop invariant– Easy way to achieve this: the test should not

update any variables at all

7 January 2019 OSU CSE 40

Page 41: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Justification for (2)

• The loop does not terminate until and unless the loop condition is false– However, a loop might never terminate; so

you need to show that it does– This is similar to how you show a recursive

method terminates

7 January 2019 OSU CSE 41

Page 42: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Loop Termination

• To show that a loop terminates, it is sufficient to provide a progress metric (a.k.a. termination function, a.k.a. variant function)– An integer-valued function of the variables in

scope (where the loop appears in the code)– Always non-negative– Always decreases when the loop body is

executed once

7 January 2019 OSU CSE 42

Page 43: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Example #1: Method Body/**

* @updates this, q* @maintains* this * q = #this * #q

* @decreases

* |q|

*/

while (q.length() > 0) {

T x = q.dequeue();

this.enqueue(x);}

7 January 2019 OSU CSE 43

Page 44: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Example #1: Method Body/**

* @updates this, q* @maintains* this * q = #this * #q

* @decreases* |q|

*/

while (q.length() > 0) {

T x = q.dequeue();

this.enqueue(x);}

7 January 2019 OSU CSE 44

This Javadoc annotation claims the loop that

follows “decreases” the stated progress metric.

Page 45: Loop Invariants: Part 1web.cse.ohio-state.edu/.../37.Loop-Invariants-1.pdf · loop does is described by its . loop invariant – Invariant: a property that is true . every time. the

Conclusion

• Even if you do not choose to write down a loop invariant or progress metric, if you think about loops in these terms it can help you avoid errors and bad practices in loop code– Off-by-one errors– Wrong/missing code in the loop body– Declarations of variables outside the loop that

are only used inside the loop body

7 January 2019 OSU CSE 45