Hoare Logic: COMP2111 Lecture 4b Session 1, 2013
Hoare Logic: COMP2111 Lecture 4b Session 1, 2013
Hoare Logic
Kai Engelhardt
Revision: 1.1
Hoare Logic
The above denitions are geared towards simplicity. When programming, we tend to use more familiar constructs such as while do P od for (; P ) ; and if then P else Q for (; P ) + (; Q ).
Hoare Logic
We introduce a family of functions from syntactic entities (programs, arithmetic expressions, and Boolean expressions) to semantic entities. [[.]] : P P( ) E [[.]] : Expr V B [[.]] : BoolExpr P()
Hoare Logic
[[P i ]]
i N
where f [a b ] denotes the function that is the same as f , except for its value for the argument a, which is b .
Hoare Logic
E [[0]]s = 0(I) E [[x ]]s = s (x ) E [[e f ]]s = E [[e ]]s (I) E [[f ]]s B [[true]] = B [[ ]] = B [[]] B [[ ]]
def def def def
def
def
In the above, Ive decorated some entities on the RHS with (I) to indicate that they are semantic objects rather than syntax even though they look the same as some syntactic entities on the LHS. For instance, the symbol on the left is part of the alphabet to form arithmetic expressions, and the symbol (I) represents the multiplication function known from maths.
5
Hoare Logic
Predicates on states suce to express interesting properties of sequential programs. Hoare logic allows to formally derive properties from the program text. The Hoare triple {} P { } means: If program P is started in an initial state satisfying precondition and P terminates then the nal state satises postcondition . Example: {y = 22} x := y 17 {x = 5}
Hoare Logic
Syntax vs Semantics
On the syntactic level, we may axiomatize Hoare logic by giving a set of rules and axioms characterizing Hoare triples. On the semantic level, we may dene mathematically, what it means for a Hoare triple to be valid.
Hoare Logic
The assignment axiom: {[e /x ]} x := e {} where [e /x ] is with x substituted by e e.g. (x = 5)[y 17 /x ] is y 17 = 5 The guard axiom: { } { }
8
ass
grd
Hoare Logic
Hoare Logic
{y 17 = 5} x := y 17 {x = 5} {y = 22} x := y 17 {x = 5}
(1) (2)
10
Hoare Logic
the relational image of through P is contained in : { : B [[]] ((, ) [[P ]]) } B[[ ]]
11
Hoare Logic
the relational image of through P is contained in : { : B [[]] ((, ) [[P ]]) } B[[ ]]
12
Hoare Logic
the relational image of through P is contained in : { : B [[]] ((, ) [[P ]]) } B[[ ]]
13
Hoare Logic
the relational image of through P is contained in : { : B [[]] ((, ) [[P ]]) } B[[ ]]
14
Hoare Logic
Our proof system is sound (w.r.t. the semantics hinted at) because one can deduce only valid Hoare triples with it. Our proof system is complete (w.r.t. the semantics hinted at) if one can deduce all valid Hoare triples with it.
NB: As soon as we have (Peano) arithmetic over integers available in our assertion language, our system can hardly be complete. All one may hope for is relative completeness in the sense of Cook, i.e., completeness using an oracle for theorems from arithmetic. All this should be taught but isnt.
15