1 Introduction
1 Introduction
Department of Informatics
University of Fribourg
I N F O R M AT I C S
Some General Information
University of Fribourg
I N F O R M AT I C S
Today . . .
University of Fribourg
I N F O R M AT I C S
Hoare Logic?
x = 10,
variable x must have value 10. All other variables can have
arbitrary values.
University of Fribourg
I N F O R M AT I C S
Pre- and Postconditions
We write
{P}S{Q}
to indicate that, if P is true, then executing S will make Q true.
University of Fribourg
I N F O R M AT I C S
Some Examples
University of Fribourg
I N F O R M AT I C S
Primed and Unprimed Variables
For example:
{true} x = x + 1; {x 0 > x}
Or even more precisely:
{true} x = x + 1; {x 0 = x + 1}
University of Fribourg
I N F O R M AT I C S
Program Annotations
University of Fribourg
I N F O R M AT I C S
A Little more “Complex” Example
{x 6= 0} x = 1/x;
x = 1/x; {x 0 = x}
University of Fribourg
I N F O R M AT I C S
Formal Proof
Well, for the tiny examples we considered, we just saw that they
were correct.
University of Fribourg
I N F O R M AT I C S
Programs as Formulas
{P}S{Q}
P ∧ ΦS → Q.
ΦS → (P → Q).
University of Fribourg
I N F O R M AT I C S
A Totally Trivial Example
x = 10.
Therefore we get
true ∧ x = 10 → x > 0,
{x 6= 0} x = 1/x;
x = 1/x; {x 0 = x}
To turn the program into a formula, we must refer to the value of
x after the first “x = 1/x” statement and before the second
“x = 1/x” statement.
University of Fribourg
I N F O R M AT I C S
The Little more “Complex” Example
{x 6= 0} x = 1/x;
x = 1/x; {x 0 = x}
To turn the program into a formula, we must refer to the value of
x after the first “x = 1/x” statement and before the second
“x = 1/x” statement. We use double primed notation x 00 , and we
get the formula
x 00 = 1/x ∧ x 0 = 1/x 00 .
University of Fribourg
I N F O R M AT I C S
The Little more “Complex” Example
{x 6= 0} x = 1/x;
x = 1/x; {x 0 = x}
To turn the program into a formula, we must refer to the value of
x after the first “x = 1/x” statement and before the second
“x = 1/x” statement. We use double primed notation x 00 , and we
get the formula
x 00 = 1/x ∧ x 0 = 1/x 00 .
So finally, we must verify
x 6= 0 ∧ x 00 = 1/x ∧ x 0 = 1/x 00 → x 0 = x,
University of Fribourg
I N F O R M AT I C S
If Clauses
University of Fribourg
I N F O R M AT I C S
If Clauses
University of Fribourg
I N F O R M AT I C S
If Clauses
University of Fribourg
I N F O R M AT I C S
If Clauses
We turn
{P} if (condition) {prog 1}
else {prog 2}; {Q}
into
{P ∧ condition}prog 1{Q}
and
{P ∧ ¬condition}prog 2{Q}.
Both these Hoare triple must be true for the if clause to be correct.
University of Fribourg
I N F O R M AT I C S
An “If” Example
{true ∧ x < 0} x = −x {x 0 ≥ 0}
and
{true ∧ ¬(x < 0)} x = x {x 0 ≥ 0}.
University of Fribourg
I N F O R M AT I C S
Program Loops
University of Fribourg
I N F O R M AT I C S
Program Loops
University of Fribourg
I N F O R M AT I C S
Program Loops
When the loop definitely will come to an end (we call this
termination).
University of Fribourg
I N F O R M AT I C S
Program Loops
When the loop definitely will come to an end (we call this
termination).
University of Fribourg
I N F O R M AT I C S
While Loops
University of Fribourg
I N F O R M AT I C S
While Loops
University of Fribourg
I N F O R M AT I C S
While Loops
University of Fribourg
I N F O R M AT I C S
Partial Correctness: (Loop) Invariant
University of Fribourg
I N F O R M AT I C S
Invariant inv
From
{P} initialisation;
while (condition) {loop body }; {Q}
we get
{P} initialisation; {inv }
{inv ∧ condition} loop body ; {inv }
{inv ∧ ¬condition} skip; {Q}
University of Fribourg
I N F O R M AT I C S
Termination: (Loop) Variant
University of Fribourg
I N F O R M AT I C S
Variant var
From
{P} initialisation;
while (condition) {loop body }; {Q}
we get
University of Fribourg
I N F O R M AT I C S
Example (Whiteboard)
{n > 0 ∧ x = 1} sum = 1;
while (x < n) {
x = x + 1;
sum = sum + x;
}; {sum = n(n + 1)/2}
University of Fribourg
I N F O R M AT I C S
Weakness and Strength of Predicates
Q→P
University of Fribourg
I N F O R M AT I C S
Weakening a Precondition
{P}S{Q}
University of Fribourg
I N F O R M AT I C S
Strengthening a Postcondition
{P}S{Q}
University of Fribourg
I N F O R M AT I C S
Example (Whiteboard)
{x > 0} x = 1/x;
if (x < 0) {x = −x}
else {x = x} {x 0 > 0} ?
University of Fribourg
I N F O R M AT I C S
That’s all about Hoare Logic for the Moment
University of Fribourg
I N F O R M AT I C S
That’s all about Hoare Logic for the Moment
University of Fribourg
I N F O R M AT I C S