Loop Invariants: Part 2 - Computer...

20
Loop Invariants: Part 2 7 January 2019 OSU CSE 1

Transcript of Loop Invariants: Part 2 - Computer...

Page 1: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

Loop Invariants: Part 2

7 January 2019 OSU CSE 1

Page 2: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

Maintaining the Loop Invariant

• A claimed loop invariant is valid only if the loop body actually maintains the property, i.e., the loop invariant remains true at the end of each execution of the loop body

• To show this, you may assume:– The loop invariant is valid at the start of the

loop body– The loop condition is true

7 January 2019 OSU CSE 2

Page 3: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

The Loop Invariant Picture

test false

true

loop-body

while (test) {

loop-body

}

7 January 2019 OSU CSE 3

To show the loop invariant trueat this red point...

...assume the invariant is trueat this green point.

Page 4: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

Isn’t This Reasoning Circular?

• To justify the assumption that the loop invariant holds just after the loop condition test, didn’t we argue that assumption was valid because the loop invariant holds just before the test?

• This is not circular reasoning but rather mathematical induction– See the confidence-building approach for

reasoning about why recursion works7 January 2019 OSU CSE 4

Page 5: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

The Loop Invariant Picture

test false

true

loop-body

while (test) {

loop-body

}

7 January 2019 OSU CSE 5

Show the loop invariant is trueat this red point...

...which means it is trueat this green point

on the first iteration...

Page 6: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

The Loop Invariant Picture

test false

true

loop-body

while (test) {

loop-body

}

7 January 2019 OSU CSE 6

Show the loop invariant is trueat this red point

at the end of the first iteration...

...which means it is trueat this green point

on the second iteration...

Page 7: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

The Loop Invariant Picture

test false

true

loop-body

while (test) {

loop-body

}

7 January 2019 OSU CSE 7

Show the loop invariant is trueat this red point

at the end of the k-th iteration...

...which means it is trueat this green point

on the (k+1)-st iteration...

Page 8: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

The Loop Invariant Picture

test false

true

loop-body

while (test) {

loop-body

}

7 January 2019 OSU CSE 8

Show the loop invariant is trueat this red point

at the end of the last iteration...

...which means it is trueat this green point

when the loop terminates.

Page 9: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

Example #2

double power(double x, int p)

• Returns x to the power p.• Requires:

p > 0

• Ensures:power = x^(p)

7 January 2019 OSU CSE 9

Page 10: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

Example #2: Method Bodydouble result = 1.0;double factor = x;int pLeft = p;/*** @updates result, factor, pLeft* @maintains* pLeft >= 0 and* result * factor^(pLeft) = x^(p)* @decreases* pLeft*/

while (pLeft > 0) {...

}return result;

7 January 2019 OSU CSE 10

Page 11: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

7 January 2019 OSU CSE 11

x = 3.0p = 5result = 1.0factor = 3.0pLeft = 5

/*** @maintains* pLeft >= 0 and* result * factor^(pLeft) = x^(p)*/while (pLeft > 0) {

...

}

x = 3.0p = 5result = factor = pLeft =

What are the values of the

other variables here?

Page 12: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

What Loop Body Would Work?

• Observation: pLeft is positive at the start of the loop body, and the loop body has to decrease it

• How could you decrease pLeft?

7 January 2019 OSU CSE 12

Page 13: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

Idea 1: Decrement pLeft/*** @updates result, factor, pLeft* @maintains* pLeft >= 0 and* result * factor^(pLeft) = x^(p)* @decreases* pLeft*/while (pLeft > 0) {...pLeft--;

}

7 January 2019 OSU CSE 13

Page 14: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

The Rest of the Loop Body

• This is true at the start of the loop body (for each clause: why?):pLeft >= 0 andresult * factor^(pLeft) = x^(p) andpLeft > 0

• This has to be true at the end of the loop body (for each clause: why?):pLeft - 1 >= 0 andresult * factor^(pLeft - 1) = x^(p)

7 January 2019 OSU CSE 14

Page 15: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

The Rest of the Loop Body

• This is true at the start of the loop body (why?):pLeft >= 0 andresult * factor^(pLeft) = x^(p) andpLeft > 0

• This has to be true at the end of the loop body (why?):pLeft - 1 >= 0 andresult * factor^(pLeft - 1) = x^(p)

7 January 2019 OSU CSE 15

Since x and p do not change in the loop (why?), the two circledexpressions must be equal at

the end of the loop body.

Page 16: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

The Rest of the Loop Body

• We need to update result from resulti to resultf, and/or update factor from factori to factorf, to make this true:resulti * factori^(pLeft) =

resultf * factorf^(pLeft - 1)

• How could you do that?

7 January 2019 OSU CSE 16

Page 17: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

The Rest of the Loop Body

• We need to update result from resulti to resultf, and/or update factor from factori to factorf, to make this true:resulti * factori^(pLeft) =

resultf * factorf^(pLeft - 1)

• How could you do that?

7 January 2019 OSU CSE 17

One line of code that updates result:result *= factor;

Page 18: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

Idea 2: Halve pLeft/*** @updates result, factor, pLeft* @maintains* pLeft >= 0 and* result * factor^(pLeft) = x^(p)* @decreases* pLeft*/while (pLeft > 0) {...pLeft /= 2;

}

7 January 2019 OSU CSE 18

Page 19: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

The Rest of the Loop Body

• This is true at the start of the loop body (for each clause: why?):pLeft >= 0 andresult * factor^(pLeft) = x^(p) andpLeft > 0

• This has to be true at the end of the loop body (for each clause: why?):pLeft/2 >= 0 andresult * factor^(pLeft/2) = x^(p)

7 January 2019 OSU CSE 19

Page 20: Loop Invariants: Part 2 - Computer Scienceweb.cse.ohio-state.edu/.../38.Loop-Invariants-2.pdfMaintaining the Loop Invariant • A claimed . loop invariant . is valid only if the loop

The Rest of the Loop Body

• We need to update result from resulti to resultf, and/or update factor from factori to factorf, to make this true:resulti * factori^(pLeft) =

resultf * factorf^(pLeft/2)

• How can you do that?– Remember: pLeft may be even or odd, but

start with the simpler case where it is even7 January 2019 OSU CSE 20