Top Banner
Program Verification Using Hoare’s Logic Book: Chapter 7
31

Program Verification Using Hoares Logic Book: Chapter 7.

Mar 28, 2015

Download

Documents

Julian Hewitt
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Program Verification Using Hoares Logic Book: Chapter 7.

Program VerificationUsing Hoare’s LogicBook: Chapter 7

Page 2: Program Verification Using Hoares Logic Book: Chapter 7.

While programs

Assignments y:=t Composition S1; S2 If-then-else if e the S1 else S2 fi While while e do S od

Page 3: Program Verification Using Hoares Logic Book: Chapter 7.

Greatest common divisor

{x1>0/\x2>0}y1:=x1;y2:=x2;while ¬(y1=y2) do if y1>y2 then y1:=y1-y2 else y2:=y2-y1 fiod{y1=gcd(x1,x2)}

Page 4: Program Verification Using Hoares Logic Book: Chapter 7.

Why it works?

Suppose that y1,y2 are both positive integers. If y1>y2 then gcd(y1,y2)=gcd(y1-y2,y2) If y2>y1 then gcd(y1,y2)=gcd(y1,y2-y1) If y1-y2 then gcd(y1,y2)=y1=y2

Page 5: Program Verification Using Hoares Logic Book: Chapter 7.

Assignment axiom

{p[t/y]} y:=t {p}

For example:{y+5=10} y:=y+5 {y=10}{y+y<z} x:=y {x+y<z}{2*(y+5)>20} y:=2*(y+5) {y>20}Justification: write p with y’ instead of y,

and add the conjunct y’=t. Next, eliminate y’ by replacing y’ by t.

Page 6: Program Verification Using Hoares Logic Book: Chapter 7.

Why axiom works backwards?

{p} y:=t {?}

Strategy: write p and the conjunct y=t, where y’ replaces y in both p and t. Eliminate y’.

{y>5} y:=2*(y+5) {?} {p} y:=t {y’ (p[y’/y] /\ t[y’/y]=y)}y’>5 /\ y=2*(y’+5) y>20

Page 7: Program Verification Using Hoares Logic Book: Chapter 7.

Composition rule

{p} S1 {r}, {r} S2 {q}

{p} S1;S2 {q}For example: if the antecedents are1. {x+1=y+2} x:=x+1 {x=y+2}2. {x=y+2} y:=y+2 {x=y}Then the consequent is {x+1=y+2} x:=x+1; y:=y+2 {x=y}

Page 8: Program Verification Using Hoares Logic Book: Chapter 7.

More examples

{p} S1 {r}, {r} S2 {q}

{p} S1;S2 {q}{x1>0/\x2>0} y1:=x1

{gcd(x1,x2)=gcd(y1,x2)/\y1>0/\x2>0}{gcd(y1,x2)=gcd(y1,x2)/\y1>0/\x2>0}

y2:=x2 {gcd(x1,x2)=gcd(y1,y2)/\y1>0/\y2>0}

{x1>0/\x2>0} y1:=x1 ; y2:=x2 {gcd(x1,x2)=gcd(y1,y2)/\y1>0/\y2>0}

Page 9: Program Verification Using Hoares Logic Book: Chapter 7.

If-then-else rule

{p/\e} S1 {q}, {p/\¬e} S2 {q}

{p} if e then S1 else S2 fi {q}For example: p is gcd(y1,y2)=gcd(x1,x2) /\y1>0/\y2>0/\¬(y1=y2)e is y1>y2S1 is y1:=y1-y2S2 is y2:=y2-y1q is gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0

Page 10: Program Verification Using Hoares Logic Book: Chapter 7.

While rule

{p/\e} S {p} {p} while e do S od {p/\¬e}Example:p is {gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0}e is (y1=y2)S is if y1>y2 then y1:=y1-y2 else y2:=y2-y1 fi

Page 11: Program Verification Using Hoares Logic Book: Chapter 7.

Consequence rules

Strengthen a precondition rp, {p} S {q} {r} S {q} Weaken a postscondition {p} S {q}, qr {p} S {r}

Page 12: Program Verification Using Hoares Logic Book: Chapter 7.

Use of first consequence rule

Want to prove{x1>0/\x2>0} y1:=x1

{gcd(x1,x2)=gcd(y1,x2)/\y1>0/\x2>0}By assignment rule:{gcd(x1,x2)=gcd(x1,x2)/\x1>0/\x2>0}

y1:=x1 {gcd(x1,x2)=gcd(y1,x2)/\y1>0/\x2>0}x1>0/\x2>0 gcd(x1,x2)=gcd(x1,x2)/\

x1>0/\x2>0

Page 13: Program Verification Using Hoares Logic Book: Chapter 7.

Combining program

{x1>0 /\ x2>0} y1:=x1; y2:=x1;{gcd(x1,x2)=gcd(y1,y2)/\y1>0/\y2>0} while S do if e then S1 else S2 fi od{gcd(x1,x2)=gcd(y1,y2)/\y1>0/\y2>0}Combine the above using concatenation

rule!

Page 14: Program Verification Using Hoares Logic Book: Chapter 7.

Not completely finished

{x1>0/\x2>0} y1:=x1; y2:=x1; while ~(y1=y2) do if e then S1 else S2 fi od{gcd(x1,x2)=gcd(y1,y2)/\y1>0/\y2>0/\

y1=y2}But we wanted to prove:{x1>0/\x1>0} Prog {y1=gcd(x1,x2)}

Page 15: Program Verification Using Hoares Logic Book: Chapter 7.

Use of secend consequence rule

{x1>0/\x2>0} Prog{gcd(x1,x2)=gcd(y1,y2)/\y1>0/\y2>0/\y1=y2}And the implication{gcd(x1,x2)=gcd(y1,y2)/\y1>0/\y2>0/\y1=y2} y1=gcd(x1,x2)Thus,{x1>0/\x2>0} Prog {y1=gcd(x1,x2)

Page 16: Program Verification Using Hoares Logic Book: Chapter 7.

Annotating a while program

{x1>0/\x2>0}y1:=x1; {gcd(x1,x2)=gcd(y1,x2

) /\y1>0/\x2>0}y2:=x2; {gcd(x1,x2)=gcd(y1,y2

) /\y1>0/\y2>0}

while ¬(y1=y2) do{gcd(x1,x2)=gcd(y1,y2)/\

y1>0/\y2>0/\¬(y1=y2)}

if y1>y2 then y1:=y1-y2 else y2:=y2-y1 fiod{y1=gcd(x1,x2)}

Page 17: Program Verification Using Hoares Logic Book: Chapter 7.

Another example

{x>=0 /\ y>=0}a:=0;b:=x;while b>=y do b:=b-y; a:=a+1od.{x=a*y+b/\b>=0/\

b<y}

Invariant:x=a*y+b /\ b>=0

Page 18: Program Verification Using Hoares Logic Book: Chapter 7.

Invariant

How to start the proof?Heuristics: Find invariant for each loop.

For this example: x=a*y+b/\x>=0Note: total correctness does not hold for y=0.Total correctness (with y>0) to be proved

separately.

Page 19: Program Verification Using Hoares Logic Book: Chapter 7.

Proof

(1) {x=a*y+x/\x>=0} b:=x {x=a*y+b/\

b>=0} (Assignment)(2) {x=0*y+x/\x>=0} a:=0 {x=a*y+x/\x>=0} (Assignment)(3){x=0*y+x/\x>=0}a:=0;b:=x{x=a*y+b/\

x>=0} (Composition (2), (1))

{p[t/y]} y:=t {p}

{p}S1{r}, {r} S2{q}

{p} S1;S2 {q}

Page 20: Program Verification Using Hoares Logic Book: Chapter 7.

Proof (cont.)

(4){x=(a+1)*y+b/\b>=0}a:=a+1{x=a*y+b/\b>=0} (Assignment) (5){x=(a+1)*y+b-y/\b-y>=0}b:=b-y{x=(a+1)*y+b/\b>=0} (Assignment)(6){x=(a+1)*y+b-y/\b-y>=0}b:=b-y;a:=a+1{x=a*y+b/\

b>=0} (Composition (5), (4))

{p[t/y]} y:=t {p}

{p}S1{r}, {r} S2{q}

{p} S1;S2 {q}

Page 21: Program Verification Using Hoares Logic Book: Chapter 7.

While rule

{p/\e} S {p} {p} while e do S od {p/\¬e}

Page 22: Program Verification Using Hoares Logic Book: Chapter 7.

Consequence rules

Strengthen a precondition rp, {p} S {q} {r} S {q} Weaken a postcondition {p} S {q}, qr {p} S {r}

Page 23: Program Verification Using Hoares Logic Book: Chapter 7.

Proof (cont.)

(7) x=a*y+b/\b>=0/\b>=y x=(a+1)*y+b-y/\b-y>=0 (Logic)(8) {x=a*y+b/\b>=0/\b>=y} b:=b-y; a:=a+1 {x=a*y+b/\b>=0} (Consequence (6), (7))(9) {x=a*y+b/\b>=0}while b>=y do

b:=b-y; a:=a+1 od {x=a*y+b/\b>=0/\b<y} (while (8))

Page 24: Program Verification Using Hoares Logic Book: Chapter 7.

Proof (cont.)

(10) {x=0*y+x/\x>=0} Prog {x=a*y+b/\b>=0/\b<y} (Composition (3), (9))(11) x>=0/\y>=0 x=0*y+x/\x>=0 (Logic)(12) {x>=0/\y>=0} Prog {x=a*y+b/\b>=0/\b<y}

(Consequence)

Page 25: Program Verification Using Hoares Logic Book: Chapter 7.

Soundness

Hoare logic is sound in the sense thateverything that can be proved is correct!

This follows from the fact that each axiomand proof rule preserves soundness.

Page 26: Program Verification Using Hoares Logic Book: Chapter 7.

Completeness

A proof system is called complete if every

correct assertion can be proved.

Propositional logic is complete. No deductive system for the

standard arithmetic can be complete (Godel).

Page 27: Program Verification Using Hoares Logic Book: Chapter 7.

And for Hoare’s logic?

Let S be a program and p its precondition.

Then {p} S {false} means that S never terminates when started from p. This is undecideable. Thus, Hoare’s logic cannot be complete.

Page 28: Program Verification Using Hoares Logic Book: Chapter 7.

Weakest prendition, Strongest postcondition

For an assertion p and code S, let post(p,S) be the strongest assertion such that {p}S{post(p,S)}

That is, if {p}S{q} then post(p,S)q. For an assertion q and code S, let

pre(S,q) be the weakest assertion such that {pre(S,q)}S{q}

That is, if {p}S{q} then ppre(S,q).

Page 29: Program Verification Using Hoares Logic Book: Chapter 7.

Relative completeness

Suppose that either post(p,S) exists for each p, S, or pre(S,q) exists for each S, q.

Some oracle decides on pure implications.Then each correct Hoare triple can be proved.What does that mean? The weakness of theproof system stem from the weakness of the

(FO) logic, not of Hoare’s proof system.

Page 30: Program Verification Using Hoares Logic Book: Chapter 7.

Extensions

Many extensions for Hoare’s proof rules:

Total correctness Arrays Subroutines Concurrent programs Fairness

Page 31: Program Verification Using Hoares Logic Book: Chapter 7.

Proof rule for total correctness

{p/\e/\t=z} S {p/\t<z}, pt>=0 {p} while e do S od {p/\¬e}

wherez - an int. variable, not appearing in

p,t,e,S.t - an int. expression.