c } false loop body P (postcondition) Loop Invariant Defn : A - - PowerPoint PPT Presentation

c
SMART_READER_LITE
LIVE PREVIEW

c } false loop body P (postcondition) Loop Invariant Defn : A - - PowerPoint PPT Presentation

while (c) { loop body true c } false loop body P (postcondition) Loop Invariant Defn : A boolean condition that is checked immediately before every evaluation of the loop guard . while (c) I //@loop_invariant I; true c { loop


slide-1
SLIDE 1

c

loop body

while (c) { loop body }

false true

P (postcondition)

slide-2
SLIDE 2

Loop Invariant

  • Def’n: A boolean condition that is checked

immediately before every evaluation of the loop guard.

slide-3
SLIDE 3

c

loop body

while (c)

//@loop_invariant I;

{ loop body }

//@assert P;

false true

P (postcondition)

I

slide-4
SLIDE 4

Loop Invariant

  • Def’n: A boolean condition that is checked

immediately before every evaluation of the loop guard.

  • It is true even if the loop runs 0 times (i.e. is

skipped).

  • It is true immediately before each evaluation
  • f the loop guard, including the last evaluation

if the loop terminates.

  • It is true immediately after the loop

terminates, if the loop terminates.

slide-5
SLIDE 5
  • 1. INIT

Show that the loop invariant I is true immediately before the first evaluation of the loop guard C.

c

loop body

false true

P (postcondition)

I

slide-6
SLIDE 6

c

loop body

false true

P (postcondition)

  • 2. PRESERVATION

Show that if the loop invariant I is true immediately before the evaluation of the loop guard C, then I is true immediately before the next evaluation of the loop guard C.

I

slide-7
SLIDE 7
  • 3. EXIT

Once we have a valid loop invariant, we can show that the logical conjunction

  • f the loop invariant

I and the negation of the loop guard C implies the desired postcondition P: I ^ ~ C  P

c

loop body

false true

P (postcondition)

I

slide-8
SLIDE 8
  • 4. TERMINATION

Show that the loop will always terminate (i.e. that C must eventually be false).

c

loop body

false true

P (postcondition)