Weakest Preconditions
Prabhaker Mateti
This page is about assertions, without concurrency related issues.
Weakest Precondition
- Let R stand for wp(S, Q). R is an assertion/ predicate/
condition. R is a weakest precondition if the following holds.
- For all states s that satisfy R, executing S on s always
terminates, and the resulting state s' satisfies Q.
- Hoare's triplet {wp(S, Q)} S {Q} is therefore True.
- Note that wp() is about total correctness: It includes
termination.
wp Defined for a PL
-
wp(skip, R) is R
-
wp(abort, R) is False
-
wp(x := e, R) is R with x replaced by e
-
wp(S1; S2, R) is wp(S1, wp(S2, R))
-
wp(if B then S1 else S2 fi, R) is
((B ⇒ wp(S1, R)) and (not B ⇒ wp(S2, R)))
-
wp(while B do S od) is ...
-
See Andrews, Chapter 1.
-
Also, http://en.wikipedia.org/ wiki/ Predicate_ transformer_ semantics
Law of Excluded Miracle
- wp(S, False) = False is impossible.
-
This is known as the Law of Excluded Miracle. The phrase "is
impossible" can be confusing. Understand it as "we cannot do it" as
in we cannot satisfy the precondition False, execute S, and expect
False to be True. So, for any Q, wp(S, Q) = False is
impossible.
-
wp(S, True) = True? Dos this define liveness of S?
wp Example Answers
Determine weakest preconditions of the following. Show all
intermediate steps. Assume that all variables are of integer type. PL
code is shown enclosed in braces.
Weakest Preconditions-4
wp( {n := n + m; m := n + m; n := m - n}, (n == 6) and (m == 1) )
= wp( {n := n + m; m := n + m}, (m - n == 6) and (m == 1) )
= wp( {n := n + m}, (n + m - n == 6) and (n + m == 1) )
= wp( {n := n + m}, (m == 6) and (n + m == 1) )
= (m == 6) and (n + m + m == 1)
= m == 6 and n == -11
Weakest Preconditions-5
wp( {if i > j then i := i - j else j := i fi}, i == j )
= (i > j => wp({i := i - j},i == j)) and (i <= j => wp({j := i}, i == j))
= (i > j => i - j == j) and (i <= j => i == i)
= (i > j => i == 2*j) and (i > j or true)
= (i <= j or i == 2*j) and (true)
= (i <= j or i == 2*j)
Weakest Preconditions-6
wp( {while i > 3 do i := i - 3 od}, i == 3 )
P0 = (i <= 3) and (i == 3) = (i == 3)
P1 = (i > 3) and wp({i := i - 3}, i==3) = (i > 3) and (i-3 == 3) = (i == 6)
P2 = (i > 3) and wp({i := i - 3}, i==6) = (i > 3) and (i-3 == 6) = (i == 9)
Pk = i == 3*(k+1)
wp( {while i > 3 do i := i - 3 od}, i == 3 ) = (P0 or ∃ k>= 0: Pk)
= (i == 3 or (∃ k>= 0: i == 3*(k+1)))
= (∃ k>= 1: i == 3*k)
= i == 3*k, for some k > 0