Download presentation
Presentation is loading. Please wait.
1
Predicate Transforms I
Software Testing and Verification Lecture Notes 19 Prepared by Stephen M. Thebaut, Ph.D. University of Florida
2
Predicate Transforms I
Introduction: weakest pre-conditions (wp’s) weakest liberal pre-conditions (wlp’s) strongest post-conditions (sp’s) Proving {P} S {Q} using predicate transforms Transform rules for: assignment statements sequencing selection statements
3
Predicate Transforms I
Introduction: weakest pre-conditions (wp’s) weakest liberal pre-conditions (wlp’s) strongest post-conditions (sp’s) Proving {P} S {Q} using predicate transforms Transform rules for: assignment statements sequencing selection statements
4
Introduction What are Predicate Transforms†?
Rules for transforming post-conditions into pre-conditions or vice-versa. They provide algorithms to reduce the problem of verifying Hoare triples to proving predicate calculus formulas. Thus, predicate transforms operationalize Hoare Logic. † Also known as “Predicate Transformers”
5
Introduction (cont’d)
What is a “weakest pre-condition”? It is the weakest condition on the initial state of program S ensuring termination in state Q. It is denoted wp(S,Q) and read, “the weakest pre-condition of S with respect to Q.”
6
Introduction (cont’d)
What is a “weakest liberal pre-condition”? It is the weakest condition on the initial state of program S ensuring state Q on termination if S terminates. It is denoted wlp(S,Q) and read, “the weakest liberal pre-condition of S with respect to Q.”
7
Introduction (cont’d)
What is a “strongest post-condition”? It is the strongest condition on the final state of program S given that P holds initially and given that S terminates. It is denoted sp(S,P) and read, “the strongest post-condition of S with respect to P.”
8
Predicate Transforms I
Introduction: weakest pre-conditions (wp’s) weakest liberal pre-conditions (wlp’s) strongest post-conditions (sp’s) Proving {P} S {Q} using predicate transforms Transform rules for: assignment statements sequencing selection statements
9
ROI’s (algorithms) for proving program correctness using predicate transforms
P wp(S,Q) {P} S {Q} strongly P wlp(S,Q) {P} S {Q} sp(S,P) Q
10
ROI’s (algorithms) for proving program correctness (cont’d)
Note the relationship between weakest liberal pre-conditions and strongest post-conditions: P wlp(S,Q) ≡ sp(S,P) Q We now consider rules for computing predi-cate transforms for structured programs comprised of assignment statements, if-then (-else) statements, and (in part II) while loops.
11
ROI’s (algorithms) for proving program correctness (cont’d)
Note the relationship between weakest liberal pre-conditions and strongest post-conditions: P wlp(S,Q) ≡ sp(S,P) Q We now consider rules for computing predi-cate transforms for structured programs comprised of assignment statements, if-then (-else) statements, and (in part II) while loops.
12
Predicate Transforms I
Introduction: weakest pre-conditions (wp’s) weakest liberal pre-conditions (wlp’s) strongest post-conditions (sp’s) Proving {P} S {Q} using predicate transforms Transform rules for: assignment statements sequencing selection statements
13
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z)
14
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: w(l)p(x:=y+3, x>0) =
15
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: w(l)p(x:=y+3, x>0) = y+3>0
16
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: w(l)p(x:=y+3, x>0) = y+3>0 = y>-3
17
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: w(l)p(x:=y+3, x>0) = y+3>0 = y>-3 w(l)p(x:=x+1, xn+1) =
18
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: w(l)p(x:=y+3, x>0) = y+3>0 = y>-3 w(l)p(x:=x+1, xn+1) = x+1n+1
19
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: w(l)p(x:=y+3, x>0) = y+3>0 = y>-3 w(l)p(x:=x+1, xn+1) = x+1n+1 = xn
20
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: w(l)p(x:=y+3, x>0) = y+3>0 = y>-3 w(l)p(x:=x+1, xn+1) = x+1n+1 = xn w(l)p(x:=7, x=7) =
21
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: w(l)p(x:=y+3, x>0) = y+3>0 = y>-3 w(l)p(x:=x+1, xn+1) = x+1n+1 = xn w(l)p(x:=7, x=7) = (7=7)
22
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: w(l)p(x:=y+3, x>0) = y+3>0 = y>-3 w(l)p(x:=x+1, xn+1) = x+1n+1 = xn w(l)p(x:=7, x=7) = (7=7) = true (cont’d)
23
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: (cont’d) w(l)p(x:=7, x=6) =
24
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: (cont’d) w(l)p(x:=7, x=6) = (7=6)
25
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: (cont’d) w(l)p(x:=7, x=6) = (7=6) = false
26
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: (cont’d) w(l)p(x:=7, x=6) = (7=6) = false w(l)p(x:=7, y=7) =
27
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: (cont’d) w(l)p(x:=7, x=6) = (7=6) = false w(l)p(x:=7, y=7) = y=7
28
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: (cont’d) w(l)p(x:=7, x=6) = (7=6) = false w(l)p(x:=7, y=7) = y=7 w(l)p(y:=-x, y=|x|) =
29
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: (cont’d) w(l)p(x:=7, x=6) = (7=6) = false w(l)p(x:=7, y=7) = y=7 w(l)p(y:=-x, y=|x|) = -x=|x|
30
wp and wlp Rule for Assignment Statements
w(l)p(x:=E, Q(x,y,z)) Q(E,y,z) Examples: (cont’d) w(l)p(x:=7, x=6) = (7=6) = false w(l)p(x:=7, y=7) = y=7 w(l)p(y:=-x, y=|x|) = -x=|x| = x0
31
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z)
32
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: sp(x:=y+3, y>-3) =
33
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: sp(x:=y+3, y>-3) = x=y+3 Л y>-3
34
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: sp(x:=y+3, y>-3) = x=y+3 Л y>-3 (=> x>0)
35
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: sp(x:=y+3, y>-3) = x=y+3 Л y>-3 (=> x>0) sp(x:=x+1, x<n) =
36
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: sp(x:=y+3, y>-3) = x=y+3 Л y>-3 (=> x>0) sp(x:=x+1, x<n) = x=x’+1 Л x’<n
37
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: sp(x:=y+3, y>-3) = x=y+3 Л y>-3 (=> x>0) sp(x:=x+1, x<n) = x=x’+1 Л x’<n (=> x-1<n)
38
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: sp(x:=y+3, y>-3) = x=y+3 Л y>-3 (=> x>0) sp(x:=x+1, x<n) = x=x’+1 Л x’<n (=> x-1<n) sp(x:=7, true) =
39
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: sp(x:=y+3, y>-3) = x=y+3 Л y>-3 (=> x>0) sp(x:=x+1, x<n) = x=x’+1 Л x’<n (=> x-1<n) sp(x:=7, true) = x=7 Л true
40
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: sp(x:=y+3, y>-3) = x=y+3 Л y>-3 (=> x>0) sp(x:=x+1, x<n) = x=x’+1 Л x’<n (=> x-1<n) sp(x:=7, true) = x=7 Л true = x=7 (cont’d)
41
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: (cont’d) sp(x:=7, false) =
42
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: (cont’d) sp(x:=7, false) = x=7 Л false
43
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: (cont’d) sp(x:=7, false) = x=7 Л false = false
44
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: (cont’d) sp(x:=7, false) = x=7 Л false = false sp(x:=7, y=7) =
45
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: (cont’d) sp(x:=7, false) = x=7 Л false = false sp(x:=7, y=7) = x=7 Л y=7
46
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: (cont’d) sp(x:=7, false) = x=7 Л false = false sp(x:=7, y=7) = x=7 Л y=7 sp(y:=-x, y=|x|) =
47
sp Rule for Assignment Statements
sp(x:=E, P) x=E(x’,y,z) Л P(x’,y,z) Examples: (cont’d) sp(x:=7, false) = x=7 Л false = false sp(x:=7, y=7) = x=7 Л y=7 sp(y:=-x, y=|x|) = y=-x Л y’=|x|
48
Predicate Transforms I
Introduction: weakest pre-conditions (wp’s) weakest liberal pre-conditions (wlp’s) strongest post-conditions (sp’s) Proving {P} S {Q} using predicate transforms Transform rules for: assignment statements sequencing selection statements
49
wp and wlp Rule for Sequencing
w(l)p(S1;S2;...;Sn-1;Sn, Q) w(l)p(S1, w(l)p(S2,...w(l)p(Sn-1, w(l)p(Sn, Q))…))
50
wp and wlp Rule for Sequencing
w(l)p(S1;S2;...;Sn-1;Sn, Q) w(l)p(S1, w(l)p(S2,...w(l)p(Sn-1, w(l)p(Sn, Q))…))
51
wp and wlp Rule for Sequencing
w(l)p(S1;S2;...;Sn-1;Sn, Q) w(l)p(S1, w(l)p(S2,...w(l)p(Sn-1, w(l)p(Sn, Q))…))
52
wp and wlp Rule for Sequencing
w(l)p(S1;S2;...;Sn-1;Sn, Q) w(l)p(S1, w(l)p(S2,...w(l)p(Sn-1, w(l)p(Sn, Q))…))
53
wp and wlp Rule for Sequencing
w(l)p(S1;S2;...;Sn-1;Sn, Q) w(l)p(S1, w(l)p(S2,...w(l)p(Sn-1, w(l)p(Sn, Q))…))
54
wp and wlp Rule for Sequencing (cont’d)
Example: w(l)p(C:=D+1; B:=C2; A:=B2, A=36)
55
wp and wlp Rule for Sequencing (cont’d)
Example: w(l)p(C:=D+1; B:=C2; A:=B2, A=36) C:=D+1 B:=C2 A:=B2 { A=36 }
56
wp and wlp Rule for Sequencing (cont’d)
Example: w(l)p(C:=D+1; B:=C2; A:=B2, A=36) C:=D+1 B:=C2 { B2 = 36 } A:=B2 { A=36 }
57
wp and wlp Rule for Sequencing (cont’d)
Example: w(l)p(C:=D+1; B:=C2; A:=B2, A=36) C:=D+1 { (2C)2 = 36 } B:=C2 { B2 = 36 } A:=B2 { A=36 }
58
wp and wlp Rule for Sequencing (cont’d)
Example: w(l)p(C:=D+1; B:=C2; A:=B2, A=36) { (2(D+1))2 = 36 } C:=D+1 { (2C)2 = 36 } B:=C2 { B2 = 36 } A:=B2 { A=36 }
59
wp and wlp Rule for Sequencing (cont’d)
Example: w(l)p(C:=D+1; B:=C2; A:=B2, A=36) { (2(D+1))2 = 36 } C:=D+1 { (2C)2 = 36 } B:=C2 { B2 = 36 } A:=B2 { A=36 } C:=D+1 B:=C2 A:=B2 { A=36 }
60
wp and wlp Rule for Sequencing (cont’d)
Example: w(l)p(C:=D+1; B:=C2; A:=B2, A=36) { (2(D+1))2 = 36 } C:=D+1 { (2C)2 = 36 } B:=C2 { B2 = 36 } A:=B2 { A=36 } C:=D+1 B:=C2 { B=6 V B=-6 } A:=B2 { A=36 }
61
wp and wlp Rule for Sequencing (cont’d)
Example: w(l)p(C:=D+1; B:=C2; A:=B2, A=36) { (2(D+1))2 = 36 } C:=D+1 { (2C)2 = 36 } B:=C2 { B2 = 36 } A:=B2 { A=36 } C:=D+1 { C=3 V C=-3 } B:=C2 { B=6 V B=-6 } A:=B2 { A=36 }
62
wp and wlp Rule for Sequencing (cont’d)
Example: w(l)p(C:=D+1; B:=C2; A:=B2, A=36) { (2(D+1))2 = 36 } C:=D+1 { (2C)2 = 36 } B:=C2 { B2 = 36 } A:=B2 { A=36 } { D=2 V D=-4 } C:=D+1 { C=3 V C=-3 } B:=C2 { B=6 V B=-6 } A:=B2 { A=36 }
63
sp Rule for Sequencing Rule: sp(S1;S2;...;Sn-1;Sn, P)
sp(Sn, sp(Sn-1,...sp(S2, sp(S1, P))…))
64
sp Rule for Sequencing Rule: sp(S1;S2;...;Sn-1;Sn, P)
sp(Sn, sp(Sn-1,...sp(S2, sp(S1, P))…))
65
sp Rule for Sequencing Rule: sp(S1;S2;...;Sn-1;Sn, P)
sp(Sn, sp(Sn-1,...sp(S2, sp(S1, P))…))
66
sp Rule for Sequencing Rule: sp(S1;S2;...;Sn-1;Sn, P)
sp(Sn, sp(Sn-1,...sp(S2, sp(S1, P))…))
67
sp Rule for Sequencing Rule: sp(S1;S2;...;Sn-1;Sn, P)
sp(Sn, sp(Sn-1,...sp(S2, sp(S1, P))…))
68
sp Rule for Sequencing (cont’d)
Example 1: sp(C:=D+1; B:=C2; A:=B2, D=1)
69
sp Rule for Sequencing (cont’d)
Example 1: sp(C:=D+1; B:=C2; A:=B2, D=1) { D=1 } C:=D+1 B:=C2 A:=B2
70
sp Rule for Sequencing (cont’d)
Example 1: sp(C:=D+1; B:=C2; A:=B2, D=1) { D=1 } C:=D+1 { C=D+1 Л D=1 } B:=C2 A:=B2
71
sp Rule for Sequencing (cont’d)
Example 1: sp(C:=D+1; B:=C2; A:=B2, D=1) { D=1 } C:=D+1 { C=D+1 Л D=1 } B:=C2 { B=2C Л C=D+1 Л D=1 } A:=B2
72
sp Rule for Sequencing (cont’d)
Example 1: sp(C:=D+1; B:=C2; A:=B2, D=1) { D=1 } C:=D+1 { C=D+1 Л D=1 } B:=C2 { B=2C Л C=D+1 Л D=1 } A:=B2 { A=B2 Л B=2C Л C=D+1 Л D=1 }
73
sp Rule for Sequencing (cont’d)
Example 1: sp(C:=D+1; B:=C2; A:=B2, D=1) { D=1 } C:=D+1 B:=C2 A:=B2
74
sp Rule for Sequencing (cont’d)
Example 1: sp(C:=D+1; B:=C2; A:=B2, D=1) { D=1 } C:=D+1 { C=D+1 Л D=1 } B:=C2 A:=B2
75
sp Rule for Sequencing (cont’d)
Example 1: sp(C:=D+1; B:=C2; A:=B2, D=1) { D=1 } C:=D+1 { C=2 Л D=1 } B:=C2 A:=B2
76
sp Rule for Sequencing (cont’d)
Example 1: sp(C:=D+1; B:=C2; A:=B2, D=1) { D=1 } C:=D+1 { C=2 Л D=1 } B:=C2 { B=2C Л C=2 Л D=1 } A:=B2
77
sp Rule for Sequencing (cont’d)
Example 1: sp(C:=D+1; B:=C2; A:=B2, D=1) { D=1 } C:=D+1 { C=2 Л D=1 } B:=C2 { B=4 Л C=2 Л D=1 } A:=B2
78
sp Rule for Sequencing (cont’d)
Example 1: sp(C:=D+1; B:=C2; A:=B2, D=1) { D=1 } C:=D+1 { C=2 Л D=1 } B:=C2 { B=4 Л C=2 Л D=1 } A:=B2 { A=B2 Л B=4 Л C=2 Л D=1 }
79
sp Rule for Sequencing (cont’d)
Example 1: sp(C:=D+1; B:=C2; A:=B2, D=1) { D=1 } C:=D+1 { C=2 Л D=1 } B:=C2 { B=4 Л C=2 Л D=1 } A:=B2 { A=16 Л B=4 Л C=2 Л D=1 }
80
sp Rule for Sequencing (cont’d)
Compound programs often include multiple assignments to the same variable, e.g., X := X+1; ...; X := Y-X; ...; X := ZY; ... It is sometimes useful to “anchor” the initial values of such variables using some suitable notation such as X0 when applying the sp Rule for Assignment Statements. Consider the following example...
81
sp Rule for Sequencing (cont’d)
Example 2: sp(S, true) where S is X:=X+1; X:=YX; X:=X-1
82
sp Rule for Sequencing (cont’d)
Example 2: sp(S, true) where S is X:=X+1; X:=YX; X:=X-1 { true } (1) X:=X+1 (2) X:=YX (3) X:=X-1
83
sp Rule for Sequencing (cont’d)
Example 2: sp(S, true) where S is X:=X+1; X:=YX; X:=X-1 { true } (1) X:=X+1 { X=X’+1 Л true } = { X=X0+1 } (2) X:=YX (3) X:=X-1
84
sp Rule for Sequencing (cont’d)
Example 2: sp(S, true) where S is X:=X+1; X:=YX; X:=X-1 { true } (1) X:=X+1 { X=X’+1 Л true } = { X=X0+1 } (2) X:=YX (3) X:=X-1 “anchoring” initial value of X to X0:
85
sp Rule for Sequencing (cont’d)
Example 2: sp(S, true) where S is X:=X+1; X:=YX; X:=X-1 { true } (1) X:=X+1 { X=X’+1 Л true } = { X=X0+1 } (2) X:=YX { X=YX’ Л X’=X0+1 } = { X=Y(X0+1) } (3) X:=X-1 “anchoring” initial value of X to X0:
86
sp Rule for Sequencing (cont’d)
Example 2: sp(S, true) where S is X:=X+1; X:=YX; X:=X-1 { true } (1) X:=X+1 { X=X’+1 Л true } = { X=X0+1 } (2) X:=YX { X=YX’ Л X’=X0+1 } = { X=Y(X0+1) } (3) X:=X-1 { X=X’-1 Л X’=Y(X0+1) } = { X=Y(X0+1)-1 } “anchoring” initial value of X to X0:
87
sp Rule for Sequencing (cont’d)
Example 2: sp(S, true) where S is X:=X+1; X:=YX; X:=X-1 { true } (1) X:=X+1 { X=X’+1 Л true } = { X=X0+1 } (2) X:=YX { X=YX’ Л X’=X0+1 } = { X=Y(X0+1) } (3) X:=X-1 { X=X’-1 Л X’=Y(X0+1) } = { X=Y(X0+1)-1 } Therefore, sp(S, true) is X=YX’+Y-1 “anchoring” initial value of X to X0:
88
sp Rule for Sequencing (cont’d)
Example 2: sp(S, true) where S is X:=X+1; X:=YX; X:=X-1 { true } (1) X:=X+1 { X=X’+1 Л true } = { X=X0+1 } (2) X:=YX { X=YX’ Л X’=X0+1 } = { X=Y(X0+1) } (3) X:=X-1 { X=X’-1 Л X’=Y(X0+1) } = { X=Y(X0+1)-1 } Therefore, sp(S, true) is X=YX’+Y-1 “anchoring” initial value of X to X0: reverting to standard X’ notation:
89
Predicate Transforms I
Introduction: weakest pre-conditions (wp’s) weakest liberal pre-conditions (wlp’s) strongest post-conditions (sp’s) Proving {P} S {Q} using predicate transforms Transform rules for: assignment statements sequencing selection statements
90
wp and wlp Rule for if-then-else Statement
w(l)p(if b then S1 else S2, Q) (b Л w(l)p(S1, Q)) V (¬b Л w(l)p(S2, Q))
91
wp and wlp Rule for if-then-else Statement
w(l)p(if b then S1 else S2, Q) (b Л w(l)p(S1, Q)) V (¬b Л w(l)p(S2, Q)) T F b S1 S2 {Q}
92
wp and wlp Rule for if-then-else Statement
w(l)p(if b then S1 else S2, Q) (b Л w(l)p(S1, Q)) V (¬b Л w(l)p(S2, Q)) T F b b Л w(l)p(S1, Q) S1 S2 {Q}
93
wp and wlp Rule for if-then-else Statement
w(l)p(if b then S1 else S2, Q) (b Л w(l)p(S1, Q)) V (¬b Л w(l)p(S2, Q)) T F b b Л w(l)p(S1, Q) ¬b Л w(l)p(S2, Q) S1 S2 {Q}
94
wp and wlp Rule for if-then-else Statement
Example: w(l)p(if x<0 then y:=-x else y:=x, y=|x|)
95
wp and wlp Rule for if-then-else Statement
Example: w(l)p(if x<0 then y:=-x else y:=x, y=|x|) b S1 S2 Q
96
wp and wlp Rule for if-then-else Statement
Example: w(l)p(if x<0 then y:=-x else y:=x, y=|x|) = (b Л w(l)p(S1, Q)) V (¬b Л w(l)p(S2, Q)) b S1 S2 Q
97
wp and wlp Rule for if-then-else Statement
Example: w(l)p(if x<0 then y:=-x else y:=x, y=|x|) = (b Л w(l)p(S1, Q)) V (¬b Л w(l)p(S2, Q)) = (x<0 Л w(l)p(y:=-x, y=|x|) V (x≥0 Л w(l)p(y:=x, y=|x|) b S1 S2 Q
98
wp and wlp Rule for if-then-else Statement
Example: w(l)p(if x<0 then y:=-x else y:=x, y=|x|) = (b Л w(l)p(S1, Q)) V (¬b Л w(l)p(S2, Q)) = (x<0 Л w(l)p(y:=-x, y=|x|) V (x≥0 Л w(l)p(y:=x, y=|x|) = (x<0 Л -x=|x|) V (x≥0 Л x=|x|) b S1 S2 Q
99
wp and wlp Rule for if-then-else Statement
Example: w(l)p(if x<0 then y:=-x else y:=x, y=|x|) = (b Л w(l)p(S1, Q)) V (¬b Л w(l)p(S2, Q)) = (x<0 Л w(l)p(y:=-x, y=|x|) V (x≥0 Л w(l)p(y:=x, y=|x|) = (x<0 Л -x=|x|) V (x≥0 Л x=|x|) = (x<0 Л x≤0) V (x≥0 Л x≥0) b S1 S2 Q
100
wp and wlp Rule for if-then-else Statement
Example: w(l)p(if x<0 then y:=-x else y:=x, y=|x|) = (b Л w(l)p(S1, Q)) V (¬b Л w(l)p(S2, Q)) = (x<0 Л w(l)p(y:=-x, y=|x|) V (x≥0 Л w(l)p(y:=x, y=|x|) = (x<0 Л -x=|x|) V (x≥0 Л x=|x|) = (x<0 Л x≤0) V (x≥0 Л x≥0) = (x<0 V x≥0) b S1 S2 Q
101
wp and wlp Rule for if-then-else Statement
Example: w(l)p(if x<0 then y:=-x else y:=x, y=|x|) = (b Л w(l)p(S1, Q)) V (¬b Л w(l)p(S2, Q)) = (x<0 Л w(l)p(y:=-x, y=|x|) V (x≥0 Л w(l)p(y:=x, y=|x|) = (x<0 Л -x=|x|) V (x≥0 Л x=|x|) = (x<0 Л x≤0) V (x≥0 Л x≥0) = (x<0 V x≥0) = true b S1 S2 Q
102
wp and wlp Rule for if-then Statement
w(l)p(if b then S, Q) (b Л w(l)p(S, Q)) V (¬b Л Q)
103
wp and wlp Rule for if-then Statement
w(l)p(if b then S, Q) (b Л w(l)p(S, Q)) V (¬b Л Q) T b F S {Q}
104
wp and wlp Rule for if-then Statement
w(l)p(if b then S, Q) (b Л w(l)p(S, Q)) V (¬b Л Q) T b b Л w(l)p(S, Q) F S {Q}
105
wp and wlp Rule for if-then Statement
w(l)p(if b then S, Q) (b Л w(l)p(S, Q)) V (¬b Л Q) T b b Л w(l)p(S, Q) F S ¬b Л Q {Q}
106
wp and wlp Rule for if-then Statement (cont’d)
Example: w(l)p(if x<0 then y:=-x, y=|x|)
107
wp and wlp Rule for if-then Statement (cont’d)
Example: w(l)p(if x<0 then y:=-x, y=|x|) b S Q
108
wp and wlp Rule for if-then Statement (cont’d)
Example: w(l)p(if x<0 then y:=-x, y=|x|) = (b Л w(l)p(S, Q)) V (¬b Л Q)) b S Q
109
wp and wlp Rule for if-then Statement (cont’d)
Example: w(l)p(if x<0 then y:=-x, y=|x|) = (b Л w(l)p(S, Q)) V (¬b Л Q)) = (x<0 Л w(l)p(y:=-x, y=|x|) V (x≥0 Л y=|x|) b S Q
110
wp and wlp Rule for if-then Statement (cont’d)
Example: w(l)p(if x<0 then y:=-x, y=|x|) = (b Л w(l)p(S, Q)) V (¬b Л Q)) = (x<0 Л w(l)p(y:=-x, y=|x|) V (x≥0 Л y=|x|) = (x<0 Л -x=|x|) V (x≥0 Л y=|x|) b S Q
111
wp and wlp Rule for if-then Statement (cont’d)
Example: w(l)p(if x<0 then y:=-x, y=|x|) = (b Л w(l)p(S, Q)) V (¬b Л Q)) = (x<0 Л w(l)p(y:=-x, y=|x|) V (x≥0 Л y=|x|) = (x<0 Л -x=|x|) V (x≥0 Л y=|x|) = (x<0 Л x≤0) V (x≥0 Л y=|x|) b S Q
112
wp and wlp Rule for if-then Statement (cont’d)
Example: w(l)p(if x<0 then y:=-x, y=|x|) = (b Л w(l)p(S, Q)) V (¬b Л Q)) = (x<0 Л w(l)p(y:=-x, y=|x|) V (x≥0 Л y=|x|) = (x<0 Л -x=|x|) V (x≥0 Л y=|x|) = (x<0 Л x≤0) V (x≥0 Л y=|x|) = (x<0 V (x≥0 Л y=|x|)) b S Q
113
wp and wlp Rule for if-then Statement (cont’d)
Example: w(l)p(if x<0 then y:=-x, y=|x|) = (b Л w(l)p(S, Q)) V (¬b Л Q)) = (x<0 Л w(l)p(y:=-x, y=|x|) V (x≥0 Л y=|x|) = (x<0 Л -x=|x|) V (x≥0 Л y=|x|) = (x<0 Л x≤0) V (x≥0 Л y=|x|) = (x<0 V (x≥0 Л y=|x|)) = (x<0 V (x≥0 Л y=x)) b S Q
114
wp and wlp Rule for if-then Statement (cont’d)
Example: w(l)p(if x<0 then y:=-x, y=|x|) = (b Л w(l)p(S, Q)) V (¬b Л Q)) = (x<0 Л w(l)p(y:=-x, y=|x|) V (x≥0 Л y=|x|) = (x<0 Л -x=|x|) V (x≥0 Л y=|x|) = (x<0 Л x≤0) V (x≥0 Л y=|x|) = (x<0 V (x≥0 Л y=|x|)) = (x<0 V (x≥0 Л y=x)) = (x<0 V y=x) b S Q
115
Exercise Prove the assertion below using the wlp ROI.
{Z=B} if A>B then Z := A {Z=Max(A,B)}
116
Exercise Prove the assertion below using the wlp ROI.
{Z=B} if A>B then Z := A {Z=Max(A,B)} P S Q
117
Exercise Prove the assertion below using the wlp ROI.
{Z=B} if A>B then Z := A {Z=Max(A,B)} Recall the wlp ROI: P wlp(S,Q) {P} S {Q} P S Q
118
sp Rule for if-then-else Statement
sp(if b then S1 else S2, P) sp(S1, b Л P) V sp(S2, ¬b Л P)
119
sp Rule for if-then-else Statement
sp(if b then S1 else S2, P) sp(S1, b Л P) V sp(S2, ¬b Л P) {P} T F b S1 S2
120
sp Rule for if-then-else Statement
sp(if b then S1 else S2, P) sp(S1, b Л P) V sp(S2, ¬b Л P) {P} T F b S1 S2 sp(S1, b Л P)
121
sp Rule for if-then-else Statement
sp(if b then S1 else S2, P) sp(S1, b Л P) V sp(S2, ¬b Л P) {P} T F b S1 S2 sp(S1, b Л P) sp(S2, ¬b Л P)
122
sp Rule for if-then Statement
sp(if b then S, P) sp(S, b Л P) V (¬b Л P)
123
sp Rule for if-then Statement
sp(if b then S, P) sp(S, b Л P) V (¬b Л P) {P} T b F S
124
sp Rule for if-then Statement
sp(if b then S, P) sp(S, b Л P) V (¬b Л P) {P} T b F S sp(S, b Л P)
125
sp Rule for if-then Statement
sp(if b then S, P) sp(S, b Л P) V (¬b Л P) {P} T b F S ¬b Л P sp(S, b Л P)
126
Example Prove the assertion: {y=x} if x<0 then y:=-x {y=|x|}
using the strongest post-condition (sp) ROI.
127
Example Prove the assertion: {y=x} if x<0 then y:=-x {y=|x|}
using the strongest post-condition (sp) ROI. Recall the sp ROI: sp(S,P) Q {P} S {Q}
128
Example Prove the assertion: {y=x} if x<0 then y:=-x {y=|x|}
using the strongest post-condition (sp) ROI. (1) sp(if x<0 then y:=-x, y=x) = sp(y:=-x, x<0 Л y=x) V (x≥0 Л y=x) = (y=-x Л x<0 Л y’=x) V (x≥0 Л y=x) P
129
Example Prove the assertion: {y=x} if x<0 then y:=-x {y=|x|}
using the strongest post-condition (sp) ROI. (1) sp(if x<0 then y:=-x, y=x) = sp(y:=-x, x<0 Л y=x) V (x≥0 Л y=x) = (y=-x Л x<0 Л y’=x) V (x≥0 Л y=x) P
130
Example Prove the assertion: {y=x} if x<0 then y:=-x {y=|x|}
using the strongest post-condition (sp) ROI. (1) sp(if x<0 then y:=-x, y=x) = sp(y:=-x, x<0 Л y=x) V (x≥0 Л y=x) = (y=-x Л x<0 Л y’=x) V (x≥0 Л y=x) P
131
Example Prove the assertion: {y=x} if x<0 then y:=-x {y=|x|}
using the strongest post-condition (sp) ROI. (1) sp(if x<0 then y:=-x, y=x) = sp(y:=-x, x<0 Л y=x) V (x≥0 Л y=x) = (y=-x Л x<0 Л y’=x) V (x≥0 Л y=x) (2) (y=-x Л x<0 Л y’=x) V (x≥0 Л y=x) => (x<0 Л y=-x) V (x≥0 Л y=x) => y=|x| √ P Q
132
Example Prove the assertion: {y=x} if x<0 then y:=-x {y=|x|}
using the strongest post-condition (sp) ROI. (1) sp(if x<0 then y:=-x, y=x) = sp(y:=-x, x<0 Л y=x) V (x≥0 Л y=x) = (y=-x Л x<0 Л y’=x) V (x≥0 Л y=x) (2) (y=-x Л x<0 Л y’=x) V (x≥0 Л y=x) => (x<0 Л y=-x) V (x≥0 Л y=x) => y=|x| √ P Q
133
Coming Up Next… Transform rules for while loops
134
Predicate Transforms I
Software Testing and Verification Lecture Notes 19 Prepared by Stephen M. Thebaut, Ph.D. University of Florida
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.