You are on page 1of 19

Program correctness and verication

Programs should be:


clear; ecient; robust; reliable; user friendly; well documented; . . .
but rst of all, CORRECT
dont forget though: also, executable. . .
Correctness
_
`

_
`

Program correctness makes sense only


w.r.t. a precise specication of the requirements.
Andrzej Tarlecki: Semantics & Verication - 155 -
Dening correctness
We need:
A formal denition of the programs in use
syntax and semantics of the programming language
A formal denition of the specications in use
syntax and semantics of the specication formalism
A formal denition of the notion of correctness to be used
what does it mean for a program to satisfy a specication
Andrzej Tarlecki: Semantics & Verication - 156 -
Proving correctness
We need:
A formal system to prove correctness of programs w.r.t. specications
a logical calculus to prove judgments of program correctness
A (meta-)proof that the logic proves only true correctness judgements
soundness of the logical calculus
A (meta-)proof that the logic proves all true correctness judgements
completeness of the logical calculus
_
'

_
\

under acceptable technical conditions


Andrzej Tarlecki: Semantics & Verication - 157 -
A specied program
{n 0}
rt := 0; sqr := 1;
while sqr n do
(rt :=rt + 1; sqr :=sqr + 2 rt + 1)
{rt
2
n < (rt + 1)
2
}
If we start with a non-negative n, and execute the program successfully,
then we end up with rt holding the integer square root of n
Andrzej Tarlecki: Semantics & Verication - 158 -
Hoares logic
Correctness judgements:
{} S {}
S is a statement of Tiny
the precondition and the postcondition are rst-order formulae with variables
in Var
Intended meaning:
_
`

_
`

Partial correctness:
termination not guaranteed!
Whenever the program S starts in a state satisfying the precondtion
and terminates successfully, then the nal state satises the postcondition
Andrzej Tarlecki: Semantics & Verication - 159 -
Formal denition
Recall the simplest semantics of Tiny, with
S : Stmt State State
We add now a new syntactic category:
Form ::= b |
1

2
|
1

2
|

| x.

| x.

with the corresponding semantic function:


F: Form State Bool
and standard semantic clauses.
_
`

_
`

Also, the usual denitions of free variables of a formula


and substitution of an expression for a variable
Andrzej Tarlecki: Semantics & Verication - 160 -
More notation
For Form:
{} = {s State | F[[]] s = tt}
For S Stmt, A State:
A [[S]] = {s State | S[[S]] a = s, for some a A}
Andrzej Tarlecki: Semantics & Verication - 161 -
Hoares logic: semantics
|= {} S {}
i
{} [[S]] {}
_
'

_
\

Spelling this out:


The partial correctness judgement {} S {} holds, written |= {} S {},
if for all states s State
if F[[]] s = tt and S[[S]] s State
then F[[]] (S[[S]] s) = tt
Andrzej Tarlecki: Semantics & Verication - 162 -
Hoares logic: proof rules
{[x e]} x:=e {}
{} S
1
{} {} S
2
{}
{} S
1
; S
2
{}
{ b} S {}
{} while b do S { b}
{} skip{}
{ b} S
1
{} { b} S
2
{}
{} if b then S
1
else S
2
{}

{} S {}

} S {

}
Andrzej Tarlecki: Semantics & Verication - 163 -
Example of a proof
We will prove the following partial correctness judgement:
{n 0}
rt := 0;
sqr := 1;
while sqr n do
rt :=rt + 1;
sqr :=sqr + 2 rt + 1
{rt
2
n n < (rt + 1)
2
}
_
`

_
`

Consequence rule will be used implicitly


to replace assertions by equivalent ones of a simpler form
Andrzej Tarlecki: Semantics & Verication - 164 -
Step by step
{n 0} rt := 0 {n 0 rt = 0}
{n 0 rt = 0} sqr := 1 {n 0 rt = 0 sqr = 1}
{n 0} rt := 0; sqr := 1 {n 0 rt = 0 sqr = 1}
{n 0} rt := 0; sqr := 1 {sqr = (rt + 1)
2
rt
2
n}
_

_
`

EUREKA!!!
We have just invented
the loop invariant
Andrzej Tarlecki: Semantics & Verication - 165 -
Loop invariant
{(sqr = (rt + 1)
2
rt
2
n) sqr n} rt :=rt + 1 {sqr = rt
2
sqr n}
{sqr = rt
2
sqr n} sqr :=sqr + 2 rt + 1 {sqr = (rt + 1)
2
rt
2
n}
{(sqr = (rt + 1)
2
rt
2
n) sqr n}
rt :=rt + 1; sqr :=sqr + 2 rt + 1
{sqr = (rt + 1)
2
rt
2
n}
{sqr = (rt + 1)
2
rt
2
n}
while sqr n do
rt :=rt + 1; sqr :=sqr + 2 rt + 1
{(sqr = (rt + 1)
2
rt
2
n) (sqr n)}
Andrzej Tarlecki: Semantics & Verication - 166 -
Finishing up
{sqr = (rt + 1)
2
rt
2
n}
while sqr n do
rt :=rt + 1; sqr :=sqr + 2 rt + 1
{rt
2
n n < (rt + 1)
2
}

{n 0}
rt := 0; sqr := 1;
while sqr n do
rt :=rt + 1; sqr :=sqr + 2 rt + 1
{rt
2
n n < (rt + 1)
2
}
QED
Andrzej Tarlecki: Semantics & Verication - 167 -
A fully specied program
{n 0}
rt := 0;
{n 0 rt = 0}
sqr := 1;
{n 0 rt = 0 sqr = 1}
while {sqr = (rt + 1)
2
rt
2
n} sqr n do
rt :=rt + 1;
{sqr = rt
2
sqr n}
sqr :=sqr + 2 rt + 1
{rt
2
n < (rt + 1)
2
}
Andrzej Tarlecki: Semantics & Verication - 168 -
The rst-order theory in use
In the proof above, we have used quite a number of facts concerning the underlying
data type, that is, Int with the operations and relations built into the syntax of
Tiny. Indeed, each use of the consequence rule requires such facts.
Dene the theory of Int
T H(Int)
to be the set of all formulae that hold in all states.
The above proof shows:
T H(Int)
{n 0}
rt := 0; sqr := 1;
while sqr n do rt :=rt + 1; sqr :=sqr + 2 rt + 1
{rt
2
n n < (rt + 1)
2
}
Andrzej Tarlecki: Semantics & Verication - 169 -
Soundness
Fact: Hoares proof calculus (given by the above rules) is sound, that is:
if T H(Int) {} S {} then |= {} S {}
So, the above proof of a correctness judgement validates the following semantic fact:
|=
{n 0}
rt := 0; sqr := 1;
while sqr n do rt :=rt + 1; sqr :=sqr + 2 rt + 1
{rt
2
n n < (rt + 1)
2
}
Andrzej Tarlecki: Semantics & Verication - 170 -
Proof
(of soundness of Hoares proof calculus)
By induction on the structure of the proof in Hoares logic:
assignment rule: Easy, but we need a lemma (to be proved by induction on the
structure of formulae):
F[[[x e]]] s = F[[]] s[x E[[e]] s]
Then, for s State, if s {[x e]} then S[[x:=e]] s = s[x E[[e]] s] {}.
skip rule: Trivial.
composition rule: Assume {} [[S
1
]] {} and {} [[S
2
]] {}. Then
{} [[S
1
; S
2
]] = ({} [[S
1
]]) [[S
2
]] {} [[S
2
]] {}.
if-then-else rule: Easy.
consequence rule: Again the same, given the obvious observation that {
1
} {
2
}
i
1

2
T H(Int).
Andrzej Tarlecki: Semantics & Verication - 171 -
Soundness of the loop rule
loop rule: We need to show that the least xed point of the operator
(F) = cond(B[[b]], S[[S]];F, id
State
)
satises
x()({}) { b}
Proceed by xed point induction (this is an admissible property!). Suppose that
F({}) { b} for some F : State State, and consider s {} with
s

= (F)(s) State. Two cases are possible:


If B[[b]] s = then s

= s { b}.
If B[[b]] s = tt then s

= F(S[[S]] s). We get s

{ b} by the assumption
on F, since { b} [[S]] {} by the inductive hypothesis, which implies
S[[S]] s {}.
So, (F)({}) { b}, and the proof is completed.
Andrzej Tarlecki: Semantics & Verication - 172 -
Problems with completeness
If T Form is r.e. then the set of all Hoares triples derivable from T is r.e. as
well.
|= {true} S {false} i S fails to terminate for all initial states.
Since the halting problem is not decidable for Tiny, the set of all judgements of
the form {true} S {false} such that |= {true} S {false} is not r.e.
Nevertheless:
T H(Int) {} S {} i |= {} S {}
Andrzej Tarlecki: Semantics & Verication - 173 -

You might also like