Professional Documents
Culture Documents
Weakest Precondition1
Weakest Precondition1
Edsko de Vries
Introduction
Dijkstras Weakest Precondition
Notation
Dijkstras Weakest Precondition
If the program is denoted by S (for System) and the postcondition is denoted by R, then the corresponding weakest
precondition is given by
wp(S, R)
Thus, if a program S is started in a state satisfying wp(S, R),
it is guaranteed to terminate in a state satisfying R.
We introduce two constant predicates:
Properties of wp
Dijkstras Weakest Precondition
Property (1)
Properties of wp (2)
Dijkstras Weakest Precondition
Property (2)
When S is started in a state satisfying wp(S, Q), it will terminate in a state satisfying Q.
But Q R, so when S is started in a state satisfying wp(S, Q)
it will also terminate in a state satisfying R.
wp(S, R) is the weakest precondition for states that will result in R; so the set of states that satisfy wp(S, Q) must be
a subset of the set of states that satisfy wp(S, R).
Properties of wp (3)
Dijkstras Weakest Precondition
Properties (3) and (4) deal with conjunction (and) and disjunction (or) of wp respectively. I.e., for a program S and
postconditions Q and R, we are interested in
wp(S, Q) wp(S, R)
(conjunction)
wp(S, Q) wp(S, R)
(disjunction)
and
Properties of wp (4)
Dijkstras Weakest Precondition
wp(S, R)
Properties of wp (5)
Dijkstras Weakest Precondition
Property (3)
wp(S, Q) wp(S, R)
wp(S, R)
wp(S, Q)
QR
Properties of wp (6)
Dijkstras Weakest Precondition
Property (4)
wp(S, Q) wp(S, R)
wp(S, Q)
Q
QR
wp(S, R)
Properties of wp (7)
Dijkstras Weakest Precondition
wp(S, Q R)
wp(S, Q) wp(S, R)
wp(S, Q)
wp(S, R)
Properties of wp (8)
Dijkstras Weakest Precondition
wp(S, R) wp(S, Q)
wp(S, Q)
wp(S, R)
(skip)
Next we introduce two programs with a constant precondition (one that does not depend on the postcondition).
wp(JAbortK, R) = F
(abort)
(miracle)
So, Miracle is guaranteed to terminate in a state that satisfies any postcondition you want, independent of the precondition.
(assignment)
For example
wp(Ja := 7K, a = 7) = (7 = 7) = T
wp(Ja := 7K, a = 6) = (7 = 6) = F
So, a is always 7 after executing a := 7, and never 6, as one
would expect.
(sequencing)
Example
wp(Ja := a + b; b := a bK, b > a) =
( b > 1 a 0) ( b < 1 a 0)
( b > 1 ( a + b ) 0) ( b < 1 ( a + b ) 0)
= (b > 1 a b) (b < 1 a b)
(if)
Thus, at least one of the guards must be true, and the weakest precondition of all selected statements must be satisfied.
(The ifstatement will abort if all guards are false.)
IF =
fi
x > y x > z x x x y x z
x > y x > z x y x z
T
IF =
fi
we have
(do)
Invariant Properties
Dijkstras Weakest Precondition
(if-invariant)
i ( P Bi ) wp(S j , P)
(do-invariant)
Loop Termination
Dijkstras Weakest Precondition
(t-bounded)
i P Bi (t t0 + 1) wp(Si , t t0)
Consider wp(Si , t t0). Calculating this yields
wp(Si , t t0) = t0 t0
where both t0 and t are functions of the current state. We
must show that t0 t 1, i.e., Si decreases t by at least
one. This is denoted by wdec:
wdec(Si , t) = t0 t 1
(wdec)
Loop Example
Dijkstras Weakest Precondition
10 10 = 0 10 0 10 0
T
Now lets verify that P is invariant in the loop. For the first
branch in the loop:
( y x = x 0 y 0) = 0 x > 0
wp(J, x := 1, x 1K, y x = x 0 y 0)
(y x = 0) x > 0 y 0
( y ( x 1) = 1) ( x 1) 0 y 0
T
By a similar argument, P is also invariant in the second
branch in the loop. Thus, P is invariant throughout the program.
( y x = x 0 y 0) = 0 x > 0 ( x + y ) > 0
T
( y x = x 0 y 0) = 1 y > 0 ( x + y ) > 0
T
(t = 0 x > 0) (t = 1 y > 0) (y x = x 0 y 0)
There are only two possibilities for ; either = 0 or = 1.
We cannot have = 1, because then y 0, y x = 1, so
x < 0 which contradicts the invariant.
So, = 0, therefore x 0. But x 0 x 0 x = 0. We
know y x = ; y 0 = 0, so y = 0.