Hoare Logic = Syntax and (Semantics or Calculus) Syntax Semantics Calculus CPL predicates CPL semantics No ND! variables x, y , z ··· arith- metic expressions 1 + 2, x < y , ··· and predi- cates built from them states map variables and expressions to val- ues and predicates to true/false rules of arithmetic e.g 1 + 2 = 3, 2 2 = 4, 6/3 = 2 etc programming language := ; if.then. while as usual one rule for each (seen rule for :=) ··· Hoare Triple {P} S {Q} if pre-state satisfies P and S terminates then post-state satisfies Q proo f COMP 2600 — Hoare Logic 37
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Hoare Logic = Syntax and (Semantics or Calculus)Syntax Semantics Calculus
CPL predicates CPL semantics No ND!
variables x,y,z · · · arith-
metic expressions 1 +
2,x < y, · · · and predi-
cates built from them
states map variables
and expressions to val-
ues and predicates to
true/false
rules of arithmetic e.g
1+2 = 3, 22 = 4, 6/3 =
2 etc
programming language
:= ; if.then. whileas usual
one rule for each (seen
rule for :=) · · ·
Hoare Triple {P} S {Q}if pre-state satisfies Pand S terminates then
post-state satisfies Q
proo f
COMP 2600 — Hoare Logic 37
Summary of Lecture 1
Hoare triple syntax:
{P} S {Q}
Hoare triple Semantics (meaning):
If P is true in the initial state and Sterminates then Q will hold in the final
state.
Proof Rule for Assignment:
{Q(e)} x := e {Q(x)}
One line proof: Any instance of this
triple is (defined to be) provable and
Soundness: is guaranteed to be true
Examples:
Hoare triple true? Provable by assignment axiom?
{x+1 = 3} x := x+1 {x = 3} yes yes
{y > 2} x := y {x > 2} yes yes
{y = 3} x := y {x > 2} yes no ... need more rules ...
COMP 2600 — Hoare Logic 38
Weak and Strong Predicates
A predicate P is stronger than Q if it is the case that P implies Q.
(Similarly Q is weaker than P.)
If P is stronger than Q then P is more likely to be false than Q.
A politician’s example:
• I will keep unemployment below 3% is stronger than
• I will keep unemployment below 15%.
• The strongest possible statement is False, i.e. I will keep unemployment
below 0%.
• The weakest possible statement is True, i.e. I will keep unemployment at
or below 100%.
COMP 2600 — Hoare Logic 39
Strong Postconditions
• (x = 6) =⇒ (x > 0), so (x = 6) is stronger than (x > 0)
• The Hoare triple:
{x = 5} x := x+1 {x = 6}
says more about the code than does:
{x = 5} x := x+1 {x > 0}
If a postcondition Q1 is stronger than Q2,then {P}S{Q1} is a stronger statementthan {P}S{Q2}.
If a postcondition x = 6 is stronger than x > 0,then {x = 5} x := x+1 {x = 6} is a stronger statementthan {x = 5} x := x+1 {x > 0}.
COMP 2600 — Hoare Logic 40
Weak Preconditions
• The condition (x > 0) says less about a state than the condition (x = 5).Thus x > 0 is the weaker condition since x = 5 implies x > 0
• But the Hoare triple
{x > 0} x := x+1 {x > 1}
says more about the code than:
{x = 5} x := x+1 {x > 1}
If a precondition P1 is weaker than P2,then {P1}S{Q} is stronger than {P2}S{Q}.
If a precondition {x > 0} is weaker than {x = 5},then {x > 0} x := x+1 {x > 1} is strongerthan {x = 5} x := x+1 {x > 1}.
COMP 2600 — Hoare Logic 41
Proof rule for Strengthening Preconditions (Rule 2/6)
It is safe (sound) to make a precondition more specific (stronger).
The rule: if the premises are provable then so is the conclusion
Ps ⇒ Pw {Pw} S {Q}
{Ps} S {Q}
An instance: obtained by pattern matching
(y = 2)⇒ (y > 0) {y > 0} x := y {x > 0}
{y = 2} x := y {x > 0}
Precondition equivalence: if Ps⇔Pw i.e. Ps⇒Pw∧Pw⇒Ps
COMP 2600 — Hoare Logic 42
Proof rule for Weakening Postconditions (Rule 3/6)
It is safe (sound) to make a postcondition less specific (weaker).
The rule: if the premises are provable then so is the conclusion
{P} S {Qs} Qs⇒Qw
{P} S {Qw}
An instance: obtained by pattern matching
{x > 2} x := x+1 {x > 3} (x > 3)⇒ (x > 1)
{x > 2} x := x+1 {x > 1}
Postcondition equivalence: if Qs⇔Qw i.e. Qs⇒Qw∧Qw⇒Qs
We will not have need of this postcondition weakening rule for a while...
COMP 2600 — Hoare Logic 43
Proof rule for Sequencing (Rule 4/6)
Imperative programs consist of a sequence of statements, affecting the state
one after the other:
Proof Rule: if the premises are provable then so is the conclusion
{P} S1 {Q} {Q} S2 {R}
{P} S1 ; S2 {R}
Example Instance:
{x > 2} x := x+1 {x > 3} {x > 3} x := x+2 {x > 5}
{x > 2} x := x+1 ; x := x+2 {x > 5}
COMP 2600 — Hoare Logic 44
Laying out a proof
For the sake of the sanity of your markers, it would help if you all used the