Problem Set 6: Predicate Transforms

45
Problem Set 6: Predicate Transforms Hints and Notes

description

Problem Set 6: Predicate Transforms. Hints and Notes. 1. Consider the assertion of weak correctness: {t=5 Λ z- 5. - PowerPoint PPT Presentation

Transcript of Problem Set 6: Predicate Transforms

Page 1: Problem Set 6: Predicate Transforms

Problem Set 6: Predicate Transforms

Hints and Notes

Page 2: Problem Set 6: Predicate Transforms

1. Consider the assertion of weak correctness: {t=5 Λ z<0} s {y=z+1 Λ t=z}. Which of the following observations/facts would allow one to deduce that the assertion is FALSE and which would not?

a. The wp(s, y=z) is z>-5.

b. The wlp(s, y=z) is z>-5.

Page 3: Problem Set 6: Predicate Transforms

1. Consider the assertion of weak correctness: {t=5 Λ z<0} s {y=z+1 Λ t=z}. Which of the following observations/facts would allow one to deduce that the assertion is FALSE and which would not?

a. The wp(s, y=z) is z>-5.

b. The wlp(s, y=z) is z>-5.

Page 4: Problem Set 6: Predicate Transforms

1. Consider the assertion of weak correctness: {t=5 Λ z<0} s {y=z+1 Λ t=z}. Which of the following observations/facts would allow one to deduce that the assertion is FALSE and which would not?

a. The wp(s, y=z) is z>-5.Would. [wp(s, y=z) = z>-5] => [{t=5 Λ -5<z<0} s

{y=z} strongly] => [{t=5 Λ z<0} s {y=z+1 Λ t=z} is false] since we know s will halt for at least four initial (integer) values

of z<0 with post condition (y=z+1 Λ t=z) being false.

b. The wlp(s, y=z) is z>-5.

Page 5: Problem Set 6: Predicate Transforms

1. Consider the assertion of weak correctness: {t=5 Λ z<0} s {y=z+1 Λ t=z}. Which of the following observations/facts would allow one to deduce that the assertion is FALSE and which would not?

a. The wp(s, y=z) is z>-5.Would. [wp(s, y=z) = z>-5] => [{t=5 Λ -5<z<0} s

{y=z} strongly] => [{t=5 Λ z<0} s {y=z+1 Λ t=z} is false] since we know s will halt for at least four initial (integer) values

of z<0 with post condition (y=z+1 Λ t=z) being false.

b. The wlp(s, y=z) is z>-5.

Page 6: Problem Set 6: Predicate Transforms

1. Consider the assertion of weak correctness: {t=5 Λ z<0} s {y=z+1 Λ t=z}. Which of the following observations/facts would allow one to deduce that the assertion is FALSE and which would not?

a. The wp(s, y=z) is z>-5.Would. [wp(s, y=z) = z>-5] => [{t=5 Λ -5<z<0} s {y=z} strongly] => [{t=5 Λ z<0} s {y=z+1 Λ t=z} is false] since we know s will halt for at least four initial (integer) valuesof z<0 with post condition (y=z+1 Λ t=z) being false.

b. The wlp(s, y=z) is z>-5.Would not. [wlp(s, y=z) = z>-5] => [{t=5 Λ z>-5} s {y=z}]weakly. But this does not imply that {t=5 Λ z<0} s {y=z+1Λ t=z} is FALSE since s may not terminate for -5<z<0.

Page 7: Problem Set 6: Predicate Transforms

2. Consider the program, S, below:

temp := x x := y y := temp if temp>z then y := z z := temp if x>y then temp := x x := y y := temp end_if end_if

Under what circumstances will the program result in {x≤y<z}? (Hint: determine the wp of the program w.r.t. the desired result.)

Page 8: Problem Set 6: Predicate Transforms

2. Consider the program, S, below:

temp := x x := y y := temp if temp>z then y := z z := temp if x>y then temp := x x := y y := temp end_if end_if

Under what circumstances will the program result in {x≤y<z}? (Hint: determine the wp of the program w.r.t. the desired result.)

S1

S2

Page 9: Problem Set 6: Predicate Transforms

2. Consider the program, S, below:

temp := x x := y y := temp if temp>z then y := z z := temp if x>y then temp := x x := y y := temp end_if end_if

Under what circumstances will the program result in {x≤y<z}? (Hint: determine the wp of the program w.r.t. the desired result.)

S1

S2

S2,1

S2,2

Page 10: Problem Set 6: Predicate Transforms

2. Consider the program, S, below:

temp := x x := y y := temp if temp>z then y := z z := temp if x>y then temp := x x := y y := temp end_if end_if

Under what circumstances will the program result in {x≤y<z}? (Hint: determine the wp of the program w.r.t. the desired result.)

S1

S2

S2,1

S2,2 S2,2,1

Page 11: Problem Set 6: Predicate Transforms

2. Consider the program, S, below:

temp := x x := y y := temp if temp>z then y := z z := temp if x>y then temp := x x := y y := temp end_if end_if

wp(S, x≤y<z) = wp(S1; S2, x≤y<z) 

S1

S2

S2,1

S2,2 S2,2,1

Page 12: Problem Set 6: Predicate Transforms

2. Consider the program, S, below:

temp := x x := y y := temp if temp>z then y := z z := temp if x>y then temp := x x := y y := temp end_if end_if

wp(S, x≤y<z) = wp(S1; S2, x≤y<z)  = wp(S1, wp(S2, x≤y<z))

S1

S2

S2,1

S2,2 S2,2,1

Page 13: Problem Set 6: Predicate Transforms

2. Consider the program, S, below:

temp := x x := y y := temp if temp>z then y := z z := temp if x>y then temp := x x := y y := temp end_if end_if

wp(S, x≤y<z) = wp(S1; S2, x≤y<z)  = wp(S1, wp(S2, x≤y<z)) = wp(S1, wp(if temp>z then S2.1;S2.2, x≤y<z))

S1

S2

S2,1

S2,2 S2,2,1

Page 14: Problem Set 6: Predicate Transforms

2. Consider the program, S, below:

temp := x x := y y := temp if temp>z then y := z z := temp if x>y then temp := x x := y y := temp end_if end_if

wp(S, x≤y<z) = wp(S1; S2, x≤y<z)  = wp(S1, wp(S2, x≤y<z)) = wp(S1, wp(if temp>z then S2.1;S2.2, x≤y<z)) etc.

S1

S2

S2,1

S2,2 S2,2,1

Page 15: Problem Set 6: Predicate Transforms

3. We have learned that P => wp(s,Q) implies {P} s {Q}. However, {P} s {Q} does NOT imply P => wp(s,Q). Why?

Page 16: Problem Set 6: Predicate Transforms

3. We have learned that P => wp(s,Q) implies {P} s {Q}. However, {P} s {Q} does NOT imply P => wp(s,Q). Why?

Because:

P => wp(s,Q) <=> “{P} s {Q} strongly”

Page 17: Problem Set 6: Predicate Transforms

3. We have learned that P => wp(s,Q) implies {P} s {Q}. However, {P} s {Q} does NOT imply P => wp(s,Q). Why?

Because:

P => wp(s,Q) <=> “{P} s {Q} strongly”and

“{P} s {Q} strongly” => {P} s {Q}

Page 18: Problem Set 6: Predicate Transforms

3. We have learned that P => wp(s,Q) implies {P} s {Q}. However, {P} s {Q} does NOT imply P => wp(s,Q). Why?

Because:

P => wp(s,Q) <=> “{P} s {Q} strongly”and

“{P} s {Q} strongly” => {P} s {Q}but

{P} s {Q} ≠> “{P} s {Q} strongly”.

Page 19: Problem Set 6: Predicate Transforms

4. a. Use the wlp rule for while-do statements given in Lecture Notes 20 to find the weakest liberal pre- condition of the following program with respect to the post-condition Z=XY.

while J<>Y do Z := Z+X; J := J+1 end_while

b. Consider the invariant, I: Z=XJ, used with the while- loop ROI to prove the assertion given on slide 11 of Lecture Notes 18. How does it compare in "strength“ to the weakest liberal pre-condition from part (a) above? (In particular, is one STRONGER than the other?) Briefly explain your answer.

Page 20: Problem Set 6: Predicate Transforms

a. wlp rule for while-do statements: 

wlp(while b do S, Q) ≡ wp(while b do S, Q) V ¬wp(while b do S, true)

Page 21: Problem Set 6: Predicate Transforms

a. wlp rule for while-do statements: 

wlp(while b do S, Q) ≡ wp(while b do S, Q) V ¬wp(while b do S, true)

  i. determining wp(while b do S, Q):  H0: J=Y Л Z=XY

while J<>Y do Z := Z+X; J := J+1 end_while

Page 22: Problem Set 6: Predicate Transforms

a. wlp rule for while-do statements: 

wlp(while b do S, Q) ≡ wp(while b do S, Q) V ¬wp(while b do S, true)

  i. determining wp(while b do S, Q):  H0: J=Y Л Z=XY

H1: J<>Y Л wp(s, J=Y Л Z=XY) = J=Y-1 Л Z=X(Y-1)

while J<>Y do Z := Z+X; J := J+1 end_while

Page 23: Problem Set 6: Predicate Transforms

a. wlp rule for while-do statements: 

wlp(while b do S, Q) ≡ wp(while b do S, Q) V ¬wp(while b do S, true)

  i. determining wp(while b do S, Q):  H0: J=Y Л Z=XY

H1: J<>Y Л wp(s, J=Y Л Z=XY) = J=Y-1 Л Z=X(Y-1)

H2: J<>y Л wp(s, J=Y-1 Л Z=X(Y-1)) = J=Y-2 Л Z=X(Y-2)

while J<>Y do Z := Z+X; J := J+1 end_while

Page 24: Problem Set 6: Predicate Transforms

a. wlp rule for while-do statements: 

wlp(while b do S, Q) ≡ wp(while b do S, Q) V ¬wp(while b do S, true)

  i. determining wp(while b do S, Q):  H0: J=Y Л Z=XY

H1: J<>Y Л wp(s, J=Y Л Z=XY) = J=Y-1 Л Z=X(Y-1)

H2: J<>y Л wp(s, J=Y-1 Л Z=X(Y-1)) = J=Y-2 Л Z=X(Y-2)

Hk: J=Y-k Л Z=X(Y-k) 

while J<>Y do Z := Z+X; J := J+1 end_while

Page 25: Problem Set 6: Predicate Transforms

a. wlp rule for while-do statements: 

wlp(while b do S, Q) ≡ wp(while b do S, Q) V ¬wp(while b do S, true)

  i. determining wp(while b do S, Q):  H0: J=Y Л Z=XY

H1: J<>Y Л wp(s, J=Y Л Z=XY) = J=Y-1 Л Z=X(Y-1)

H2: J<>y Л wp(s, J=Y-1 Л Z=X(Y-1)) = J=Y-2 Л Z=X(Y-2)

Hk: J=Y-k Л Z=X(Y-k)  Therefore, H0 V H1 V H2 V ... V Hk V ... simplifies to:

(J=Y Л Z=XY) V (J<Y Л Z=XJ) = (J≤Y Л Z=XJ)

while J<>Y do Z := Z+X; J := J+1 end_while

Page 26: Problem Set 6: Predicate Transforms

ii. determining wp(while b do S, true): 

Page 27: Problem Set 6: Predicate Transforms

ii. determining wp(while b do S, true):  H0: J=Y Л true

while J<>Y do Z := Z+X; J := J+1 end_while

Page 28: Problem Set 6: Predicate Transforms

ii. determining wp(while b do S, true):  H0: J=Y Л true

H1: J<>Y Л wp(s, J=Y) = J=Y-1

while J<>Y do Z := Z+X; J := J+1 end_while

Page 29: Problem Set 6: Predicate Transforms

ii. determining wp(while b do S, true):  H0: J=Y Л true

H1: J<>Y Л wp(s, J=Y) = J=Y-1

H2: J<>y Л wp(s, J=Y-1) = J=Y-2

while J<>Y do Z := Z+X; J := J+1 end_while

Page 30: Problem Set 6: Predicate Transforms

ii. determining wp(while b do S, true):  H0: J=Y Л true

H1: J<>Y Л wp(s, J=Y) = J=Y-1

H2: J<>y Л wp(s, J=Y-1) = J=Y-2

Hk: J=Y-k 

while J<>Y do Z := Z+X; J := J+1 end_while

Page 31: Problem Set 6: Predicate Transforms

ii. determining wp(while b do S, true):  H0: J=Y Л true

H1: J<>Y Л wp(s, J=Y) = J=Y-1

H2: J<>y Л wp(s, J=Y-1) = J=Y-2

Hk: J=Y-k  Therefore, H0 V H1 V H2 V ... V Hk V ... simplifies to: J≤Y 

while J<>Y do Z := Z+X; J := J+1 end_while

Page 32: Problem Set 6: Predicate Transforms

ii. determining wp(while b do S, true):  H0: J=Y Л true

H1: J<>Y Л wp(s, J=Y) = J=Y-1

H2: J<>y Л wp(s, J=Y-1) = J=Y-2

Hk: J=Y-k  Therefore, H0 V H1 V H2 V ... V Hk V ... simplifies to: J≤Y  Thus, wlp(while b do S, Q) = (J≤Y Л Z=XJ) V J>Y

= Z=XJ V J>Y

while J<>Y do Z := Z+X; J := J+1 end_while

Page 33: Problem Set 6: Predicate Transforms

6. a. Identify H1, H2, H3, and Hk such that wp(Repeat s Until c, Q) = H1 V H2 V H3 V...V Hk V... where Hi represents the necessary and sufficient condition that

"Repeat s Until c“ terminates in state Q after i executions of s. For i>1, Hi should be expressed as a function of Hi-1.

Page 34: Problem Set 6: Predicate Transforms

6. a. Identify H1, H2, H3, and Hk such that wp(Repeat s Until c, Q) = H1 V H2 V H3 V...V Hk V... where Hi represents the necessary and sufficient condition that

"Repeat s Until c“ terminates in state Q after i executions of s. For i>1, Hi should be expressed as a function of Hi-1.

c

s

F

T

{wp}

{Q}

Page 35: Problem Set 6: Predicate Transforms

6. a. Identify H1, H2, H3, and Hk such that wp(Repeat s Until c, Q) = H1 V H2 V H3 V...V Hk V... where Hi represents the necessary and sufficient condition that

"Repeat s Until c“ terminates in state Q after i executions of s. For i>1, Hi should be expressed as a function of Hi-1.

c

s

F

T

{wp}

{Q}

H1 = ? (s executes once only)

Page 36: Problem Set 6: Predicate Transforms

6. a. Identify H1, H2, H3, and Hk such that wp(Repeat s Until c, Q) = H1 V H2 V H3 V...V Hk V... where Hi represents the necessary and sufficient condition that

"Repeat s Until c“ terminates in state Q after i executions of s. For i>1, Hi should be expressed as a function of Hi-1.

c

s

F

T

{wp}

{Q}

H1 = wp(s, ?)

Page 37: Problem Set 6: Predicate Transforms

6. a. Identify H1, H2, H3, and Hk such that wp(Repeat s Until c, Q) = H1 V H2 V H3 V...V Hk V... where Hi represents the necessary and sufficient condition that

"Repeat s Until c“ terminates in state Q after i executions of s. For i>1, Hi should be expressed as a function of Hi-1.

c

s

F

T

{wp}

{Q}

H1 = wp(s, c Л Q)

Page 38: Problem Set 6: Predicate Transforms

6. a. Identify H1, H2, H3, and Hk such that wp(Repeat s Until c, Q) = H1 V H2 V H3 V...V Hk V... where Hi represents the necessary and sufficient condition that

"Repeat s Until c“ terminates in state Q after i executions of s. For i>1, Hi should be expressed as a function of Hi-1.

c

s

F

T

{wp}

{Q}

H1 = wp(s, c Л Q)H2 = ? (s executes twice only)

Page 39: Problem Set 6: Predicate Transforms

6. a. Identify H1, H2, H3, and Hk such that wp(Repeat s Until c, Q) = H1 V H2 V H3 V...V Hk V... where Hi represents the necessary and sufficient condition that

"Repeat s Until c“ terminates in state Q after i executions of s. For i>1, Hi should be expressed as a function of Hi-1.

c

s

F

T

{wp}

{Q}

H1 = wp(s, c Л Q)H2 = wp(s, ¬c Л ?)

Page 40: Problem Set 6: Predicate Transforms

6. a. Identify H1, H2, H3, and Hk such that wp(Repeat s Until c, Q) = H1 V H2 V H3 V...V Hk V... where Hi represents the necessary and sufficient condition that

"Repeat s Until c“ terminates in state Q after i executions of s. For i>1, Hi should be expressed as a function of Hi-1.

c

s

F

T

{wp}

{Q}

H1 = wp(s, c Л Q)H2 = wp(s, ¬c Л wp(s, c Л Q))

Page 41: Problem Set 6: Predicate Transforms

6. a. Identify H1, H2, H3, and Hk such that wp(Repeat s Until c, Q) = H1 V H2 V H3 V...V Hk V... where Hi represents the necessary and sufficient condition that

"Repeat s Until c“ terminates in state Q after i executions of s. For i>1, Hi should be expressed as a function of Hi-1.

c

s

F

T

{wp}

{Q}

H1 = wp(s, c Л Q)H2 = wp(s, ¬c Л wp(s, c Л Q))

Page 42: Problem Set 6: Predicate Transforms

6. a. Identify H1, H2, H3, and Hk such that wp(Repeat s Until c, Q) = H1 V H2 V H3 V...V Hk V... where Hi represents the necessary and sufficient condition that

"Repeat s Until c“ terminates in state Q after i executions of s. For i>1, Hi should be expressed as a function of Hi-1.

c

s

F

T

{wp}

{Q}

H1 = wp(s, c Л Q)H2 = wp(s, ¬c Л wp(s, c Л Q)) = wp(s, ¬c Л H1)

Page 43: Problem Set 6: Predicate Transforms

6. a. Identify H1, H2, H3, and Hk such that wp(Repeat s Until c, Q) = H1 V H2 V H3 V...V Hk V... where Hi represents the necessary and sufficient condition that

"Repeat s Until c“ terminates in state Q after i executions of s. For i>1, Hi should be expressed as a function of Hi-1.

c

s

F

T

{wp}

{Q}

H1 = wp(s, c Л Q)H2 = wp(s, ¬c Л wp(s, c Л Q)) = wp(s, ¬c Л H1)H3 = wp(s, ¬c Л H2)

Page 44: Problem Set 6: Predicate Transforms

6. a. Identify H1, H2, H3, and Hk such that wp(Repeat s Until c, Q) = H1 V H2 V H3 V...V Hk V... where Hi represents the necessary and sufficient condition that

"Repeat s Until c“ terminates in state Q after i executions of s. For i>1, Hi should be expressed as a function of Hi-1.

c

s

F

T

{wp}

{Q}

H1 = wp(s, c Л Q)H2 = wp(s, ¬c Л wp(s, c Л Q)) = wp(s, ¬c Л H1)H3 = wp(s, ¬c Л H2)---------------------------Hk = wp(s, ¬c Л Hk-1)

Page 45: Problem Set 6: Predicate Transforms

Problem Set 6: Predicate Transforms

Hints and Notes