Pre and Post Condition Rules PowerPoint PPT Presentation

presentation player overlay
1 / 5
About This Presentation
Transcript and Presenter's Notes

Title: Pre and Post Condition Rules


1
Pre and Post Condition Rules
  • Definition If R and S are two assertions, then
    R is said to be stronger than S if R gt S (R
    implies S).
  • Example
  • the assertion i lt 0 is stronger than the
    assertion i lt 1 because i lt 0 implies that i lt
    1
  • Note that if R is stronger than S, then all
    states that satisfy R will satisfy S. But there
    is at least one state that satisfies S which will
    not satisfy R. So the number of states that
    satisfies S is larger than that of R. Thus one
    may view the notion of stronger as more
    selective because less states will satisfy the
    stronger condition.
  • Of course if R is stronger than S, then S is
    weaker than R

2
Precondition Strengthening
  • If P is stronger than P and if PCQ is
    correct, then with the strengthened precondition
    assertion the PCQ is correct
  • Example
  • if PCQ is correct, for P which is (xgt0), then
    for P, which asserts (xgt2), the triple PCQ
    will also be correct.
  • More formally
  • P gt P
  • PCQ
  • PCQ
  • Example if x lt5 x x1 x lt 6 is correct,
    then strengthening P to x lt 3 should also
    give us xlt3 xx1 xlt6 as also correct
    because
  • xlt3 gt xlt5
  • xlt5 x x1 xlt6
  • xlt3 x x1 xlt6

3
Post Condition Weakening
  • If Q gt Q and PCQ is correct then PCQ
    is correct
  • Formally we have
  • PCQ
  • Q gt Q
  • PCQ
  • Example
  • if max b maxb then show maxb
    max gt b
  • max b max b
  • max b gt max gt b
  • maxb max gtb

4
Conjunction (AND) and Disjunction (OR) Rules
  • If C is a piece of code, PCQ AND PCQ
    (note that both of the conditions have to be True
    simlutaneously), then P
    AND P C Q AND Q
  • Formally
  • P C Q
  • P C Q
  • P AND P C Q AND Q
  • If C is a piece of code, PCQ AND PCQ,
    then P OR P C Q OR Q
  • Formally
  • P C Q
  • P C Q
  • P OR P C Q OR Q

5
Example with Conjunction Rule
  • Problem given the following Hoare Triples
  • x x1 x x1 and
  • xgt0 x x 1 x gt 0
  • show that xgt0 xx1 xgt-1
  • Proof (a little more detailed than needed)
  • a) using conjunction rule, we get xgt0 x x
    1xx1 AND xgt0
  • b) using the weakening the post-condition rule,
    we have xx1 and xgt0 gt xgt0, and from second
    triple xgt0 xx1 xx1 AND xgt0 also imply
    xgt0 xx1 xgt0 by weakening the post
    condition
  • Furthermore x gt 0 gt x gt - 1
  • Therefore we have xgt0 x x1 xgt-1
  • Alternatively note that the weakening of post
    condition can be achieved through dropping xx1
    and also directly weakening xgt0 to xgt-1.
Write a Comment
User Comments (0)
About PowerShow.com