Hoare
Hoare
Hoare Logic
Anders Mller
University of Aarhus
http://www.brics.dk/~amoeller/talks/hoare.pdf
Hoare Logic
Hoare, 1969: use Floyds ideas to define axiomatic semantics (i.e., define the programming language semantics as a proof system)
program statement
Hoare triple:
{P} S {Q}
postcondition - using some predicate logic
precondition
partial correctness: if S is executed in a store initially satisfying P and it terminates, then the final store satisfies Q total correctness: as partial, but also requires termination (we ignore termination and definedness...)
3
{P} S1 {R}
{R} S2 {Q}
{P} S1 S2 {Q}
{PE} S1 {Q}
{PE} S2 {Q}
P is the loop invariant - this is where the main difficulty is! This rule can be extended to handle total correctness...
PP
Hoare logic is both sound and complete, provided that the underlying logic is! often, the underlying logic is sound but incomplete (e.g. Peano arithmetic)
10
Example: factorial
a logical variable, remembers the initial value
{n {n0 t=n} r=1; {P1} (n while (n0){P2} { r=r*n; {P3} n=nn=n-1; } {r=t!}
11
12
Functions?
require pre/post-conditions at function declaration the frame problem: to be useful, the pre/post-conditions also need to specify which things do not change
Pointers?
the heap-as-array trick: model *x=y as H[x]=y the global reasoning problem: in the proofs, each heap write appears to affect every heap read
13
this shows that the intermediate assertion comes for free in the sequence rule in Hoare Logic
Strongest Postcondition
WP is a backward predicate transformer SP (strongest postcondition) is forward: SP(P, id=E;) = v: P[v/id] id=E[v/id] ...
iff
SP(P,S)Q
15
17
Example: red_black_insert.pale
t,root ,root:Node):Node proc redblackinsert(data t,root:Node):Node { pointer y,x:Node; x = t; treeinsert(x,root ,root); root = treeinsert(x,root); x.color = false; while (x!=root & x.p.color=false) { if (x.p=x.p.p.left) { y = x.p.p.right; if (y!=null & y.color=false) { x.p.color = true; y.color = true; x.p.p.color = false; x = x.p.p; } else { if (x=x.p.right) { x = x.p; ,root); root = leftrotate(x,root); leftrotate(x,root } x.p.color = true; x.p.p.color = false; rightrotate(x.p.p,root ,root); root = rightrotate(x.p.p,root); root.color = true; }} else { x.p.p.left left; y = x.p.p.left; if (y!=null & y.color=false) { x.p.color = true; y.color = true; x.p.p.color = false; x = x.p.p; } else { (x=x.p.left left) if (x=x.p.left) { x = x.p; rightrotate(x,root); rotate(x,root root = rightrotate(x,root); } x.p.color = true; x.p.p.color = false; leftrotate(x.p.p,root); rotate(x.p.p,root root = leftrotate(x.p.p,root); root.color = true; }} root.color = true; return root; }
+ auxiliary procedures leftrotate, rightrotate, and treeinsert (total ~135 lines of program code) Program Verification with Hoare Logic 18
1. Require invariants at all while-loops and procedure calls (extra assertions are allowed) 2. Split the program into Hoare triples: {P} S {Q} 3. Verify each triple separately (only loop/call-free code left)
including check for null-pointer dereferences and other memory errors
19
Looks like an interpreter, but is essentially Weakest Precondition Sound and complete for individual Hoare triples!
20
Result: after 9000 tree automaton operations and 50 seconds, PALE replies that
all assertions are valid there can be no memory-related errors
21
PALE Experiments
Benchmark reverse search zip delete insert rotate concat bubblesort_simple bubblesort_boolean bubblesort_full orderedreverse recreverse doublylinked leftrotate rightrotate treeinsert redblackinsert threaded
Program Verification with Hoare Logic
Lines of code 16 12 33 22 33 11 24 43 43 43 24 15 72 30 30 36 57 54
Invariants 1 1 1 0 0 0 0 1 2 2 1 2 1 0 0 1 7 4
Time (sec.) 0.52 0.25 4.58 1.36 2.66 0.22 0.47 2.86 3.37 4.13 0.46 0.34 9.43 4.62 4.68 8.27 35.04 3.38
22
References
An Axiomatic Basis for Computer Programming C.A.R. Hoare, CACM 12(10), 1969 The Science of Programming D. Gries, Springer-Verlag, 1981 The Pointer Assertion Logic Engine A. Mller and M.I. Schwartzbach, PLDI 2001
23