Weakest Preconditions

  1. Let R stand for wp(S, Q). R is an assertion/ predicate/ condition.

    For all states s that satisfy R, executing S on s guarantees that S terminates, and the resulting state s' is such that s' staisfies Q.

    {wp(S, Q)} S {Q}

    Note that wp() is about total correctness: It includes termination.

  2. wp(S, False) = False is impossible. True/False? Explain.

    This is known as the Law of Excluded Miracle. The phrase "is impossible" apparently confuses some of you. Understand it as "we cannot do it" as in we cannot satisfy the precondition False and then execute S. So, for any Q, wp(S, Q) = False is impossible.

  3. wp(S, True ) = False defines liveness of S. True/False? Explain.

    As stated above, it "is impossible". But, what about wp(S, True ) = True? Dos this define liveness of S?

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

Reference

Dijkstra, Edsger Wybe, A Discipline of Programming. Englewood Cliffs: prentice-hall, 217pp, 1976. A classic. Dijkstra is a Turing award winner.

Copyright © 2016 pmateti@wright.edu www.wright.edu/~pmateti 2015-09-03