Download presentation

Presentation is loading. Please wait.

Published byJulian Hewitt Modified over 2 years ago

1
Program Verification Using Hoares Logic Book: Chapter 7

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

3
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 fi od {y1=gcd(x1,x2)}

4
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

5
Assignment axiom {p[t/y]} y:=t {p} For example: {y+5=10} y:=y+5 {y=10} {y+y

6
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

7
Composition rule {p} S1 {r}, {r} S2 {q} {p} S1;S2 {q} For example: if the antecedents are 1. {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}

8
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}

9
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>y2 S1 is y1:=y1-y2 S2 is y2:=y2-y1 q is gcd(y1,y2)=gcd(x1,x2)/\y1>0/\y2>0

10
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

11
Consequence rules Strengthen a precondition r p, {p} S {q} {r} S {q} Weaken a postscondition {p} S {q}, q r {p} S {r}

12
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

13
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!

14
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)}

15
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)

16
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 fi od {y1=gcd(x1,x2)}

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

18
Invariant How to start the proof? Heuristics: Find invariant for each loop. For this example: x=a*y+b/\x>=0 Note: total correctness does not hold for y=0. Total correctness (with y>0) to be proved separately.

19
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}

20
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}

21
While rule {p/\e} S {p} {p} while e do S od {p/\¬e}

22
Consequence rules Strengthen a precondition r p, {p} S {q} {r} S {q} Weaken a postcondition {p} S {q}, q r {p} S {r}

23
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

24
Proof (cont.) (10) {x=0*y+x/\x>=0} Prog {x=a*y+b/\b>=0/\b

25
Soundness Hoare logic is sound in the sense that everything that can be proved is correct! This follows from the fact that each axiom and proof rule preserves soundness.

26
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).

27
And for Hoares 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, Hoares logic cannot be complete.

28
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 p pre(S,q).

29
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 the proof system stem from the weakness of the (FO) logic, not of Hoares proof system.

30
Extensions Many extensions for Hoares proof rules: Total correctness Arrays Subroutines Concurrent programs Fairness

31
Proof rule for total correctness {p/\e/\t=z} S {p/\t =0 {p} while e do S od {p/\¬e} where z - an int. variable, not appearing in p,t,e,S. t - an int. expression.

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google