Semantics of Programming Languages Lecture 6,7
Semantics of Programming Languages Lecture 6,7
Programming Languages
Florian Zuleger
SS 2023
Program with a (Partial) Specification
y := 1;
while ¬(x = 1) do
y := y * x;
x := x - 1;
` : assert (y ≥ 0);
Program with a (Partial) Specification
y := 1;
while ¬(x = 1) do
y := y * x;
x := x - 1;
` : assert (y ≥ 0);
assert (y ≥ 0)
marked with #
Assertion Boxes
Syntax
Examples
Semantics
Definition (Model)
A model M of a formula F comprises
I a (non-empty) domain D, and
I an interpretation function assigning meaning to non-logical
symbols in F .
First Order Logic
Semantics
Definition (Model)
A model M of a formula F comprises
I a (non-empty) domain D, and
I an interpretation function assigning meaning to non-logical
symbols in F .
For example:
I If c is a constant, then c M ∈ D
I If f is a function of arity n, then f M ∈ Dn → D
I Note: (f (t1 , . . . , tn ))M = f M (t1M , . . . , tnM )
First Order Logic
Semantics
Semantics
M |= ∀x . F (x )
I if and only if for every m ∈ D, if we introduce a fresh constant
c and extend M such that c M = m, then M |= F (c )
First Order Logic
Semantics
M |= ∀x . F (x )
I if and only if for every m ∈ D, if we introduce a fresh constant
c and extend M such that c M = m, then M |= F (c )
First Order Logic
Semantics
M |= ∀x . F (x )
I if and only if for every m ∈ D, if we introduce a fresh constant
c and extend M such that c M = m, then M |= F (c )
M |= ∃x . F (x )
I if and only if there is a m ∈ D such that c M = m and
M |= F (c ) for some fresh constant c and extended M
First Order Logic
Semantics
Semantics
Semantics
We have
{x 7→ 3, y 7→ 5} |= y ≥ 0.
First Order Logic Assertions
{s | s |= F }
I For instance:
I (x > 1)∧ 6 ∃i , j .(x = i · j ) ∧ (i > 1) ∧ (j > 1))
First Order Logic Assertions
{s | s |= F }
I For instance:
I (x > 1)∧ 6 ∃i , j .(x = i · j ) ∧ (i > 1) ∧ (j > 1))
(the set of states in which s(x ) is a prime number)
First Order Logic: Inference Rules
premises
conclusion
I For instance:
∀x . P (x ) ∨ ¬∀y . Q (y ) ∀y . Q (y )
∀x . P (x )
¬¬P ¬R ∧ Q
P Q
P∧Q
P`Q P ` ¬Q
(reductio ad absurdum)
¬P
P`Q
(Deduction theorem)
P⇒Q
∀x , y . (x + y ) = (y + x )
∀x . even(x ) ∨ odd(x )
∀x . prime(x ) ⇔ ((x > 1)∧ 6 ∃i , j .(x = i · j ) ∧ (i > 1) ∧ (j > 1)))
First Order Logic: Axioms
∀x , y . (x + y ) = (y + x )
∀x . even(x ) ∨ odd(x )
∀x . prime(x ) ⇔ ((x > 1)∧ 6 ∃i , j .(x = i · j ) ∧ (i > 1) ∧ (j > 1)))
∀x .P
(universal instantiation)
P [t /x ]
∀x .P
(universal instantiation)
P [t /x ]
∀x .P
(universal instantiation)
P [t /x ]
I But not:
∀x . ∃y . x = y
(∃y . x = y )[y + 1/x ]
First Order Logic: Substitution
∀x .P
(universal instantiation)
P [t /x ]
I But not:
∀x . ∃y . x = y
∃y . y + 1 = y
First Order Logic: Substitution
P [c /x ]
(existential generalization)
∃x . P
s |= P [t /x ] s |= t = c
s[x 7→ c ] |= P
Instructions and Assertions
{P } C {Q }
{P } C {Q }
{P } skip {P }
Example:
I {x > 10} skip {x > 10}
I one example should really be enough ;-)
Hoare’s Axioms: Assignment
{Q [E /x ]} x :=E {Q }
Intuition:
I Q holds for new value of x
I E evaluated in old state determines new value of x
I Therefore, Q [E /x ] must hold before execution
We will make this intuition more formal later.
Hoare’s Axioms: Assignment
{Q [E /x ]} x :=E {Q }
Examples:
I {x > 10} y :=10 {x > y }
I {x > y + 1} y :=y + 1 {x > y }
I {∃z . z > x + 1} y :=x + 1 {∃x . x > y }
I Rename quantified x to avoid clash!
Hoare’s Axioms: Composition
{P } C1 {R } { R } C2 {Q }
{P } C1 ;C2 {Q }
Example:
{B ∧ P } C1 {Q } {¬B ∧ P } C2 {Q }
{P } if B then C1 else C2 {Q }
Example:
{B ∧ P } C1 {Q } {¬B ∧ P } C2 {Q }
{P } if B then C1 else C2 {Q }
Example:
P0 ⇒ P {P } S {Q } Q ⇒ Q 0
{P 0 } S {Q 0 }
It is legal to
I strengthen pre-condition, and
I weaken the post-condition
Also allows us to combine Hoare Logic and FOL derivations:
¬ even(x ) ` odd(x )
{even(x )} x := x + 1 {¬ even(x )} ¬ even(x ) ⇒ odd(x )
{even(x )} x := x + 1 {odd(x )}
and
∀n . even(n) ∨ odd(n)
¬ even(x ) even(x ) ∨ odd(x )
odd(x )
While Loops
{P ∧ B } C {P }
{P } while B do C {¬B ∧ P }
I Statement S doesn’t change P (P is invariant)
I P holds upon loop entry and exit (loop invariant)
Example:
{(x 6= 0) ∧ (x ≥ 0)} x := x − 1 {x ≥ 0}
{x ≥ 0} while (x 6= 0) do x := x − 1 {¬(x 6= 0) ∧ (x ≥ 0)}
While Loops
In context of a larger proof:
(x 6= 0) ∧ (x ≥ 0) ` (x − 1 ≥ 0)
(x 6= 0) ∧ (x ≥ 0) ⇒ (x − 1 ≥ 0) {(x − 1 ≥ 0)} x := x − 1 {x ≥ 0}
{(x 6= 0) ∧ (x ≥ 0)} x := x − 1 {x ≥ 0}
{x ≥ 0} while (x 6= 0) do x := x − 1 {¬(x 6= 0) ∧ (x ≥ 0)}
Here, we derive
I {(x − 1 ≥ 0)} x := x − 1 {x ≥ 0} using Hoare’s assignment rule
I {(x 6= 0) ∧ (x ≥ 0)} x := x − 1 {x ≥ 0} using rule of consequence
I {x ≥ 0} while (x 6= 0) do x := x − 1 {¬(x 6= 0) ∧ (x ≥ 0)} using loop
rule
While Loops
In context of a larger proof:
(x 6= 0) ∧ (x ≥ 0) ` (x − 1 ≥ 0)
(x 6= 0) ∧ (x ≥ 0) ⇒ (x − 1 ≥ 0) {(x − 1 ≥ 0)} x := x − 1 {x ≥ 0}
{(x 6= 0) ∧ (x ≥ 0)} x := x − 1 {x ≥ 0}
{x ≥ 0} while (x 6= 0) do x := x − 1 {¬(x 6= 0) ∧ (x ≥ 0)}
Here, we derive
I {(x − 1 ≥ 0)} x := x − 1 {x ≥ 0} using Hoare’s assignment rule
I {(x 6= 0) ∧ (x ≥ 0)} x := x − 1 {x ≥ 0} using rule of consequence
I {x ≥ 0} while (x 6= 0) do x := x − 1 {¬(x 6= 0) ∧ (x ≥ 0)} using loop
rule
(x 6= 0) ∧ (x ≥ 0) ` (x − 1 ≥ 0)
to show that the Hoare triple is valid. Requires the theory of integer
arithmetic (not presented here).
Hoare Rules: Overview
{P } C1 {Q } , {Q } C2 {R }
{P [E /x]} x:=E {P } {P } C1 ; C2 {R }
{B ∧ P } C1 {Q } {¬B ∧ P } C2 {Q }
{P } if B then C1 else C2 {Q }
P0 ⇒ P {P } C {Q } Q ⇒ Q 0
{P 0 } C {Q 0 }
{P ∧ B } C {P }
{P } while B do C {¬B ∧ P }
Greatest Common Divisor
Let
∀x , y , z . divides(x , y , z ) ⇔ (z > 0 ∧ x %z = 0 ∧ y %z = 0)
∀x , y , z . z = GCD (x , y ) ⇔ divides(x , y , z )∧
(6 ∃r . r > z ∧ divides(x , y , r ))
{x ≥ 0 ∧ y ≥ 0}
if (x > y ) then
k := x;
m := y;
else
k := y;
m := x;
while (m 6= 0) do
r := k %m;
k := m;
m := r ;
{GCD (x , y ) = k }
A Hoare Logic Proof of Euclid’s Algorithm
{P }
C1
{Q } {P } C1 {Q } {Q } C2 {R }
C2
{P } C1 ; C2 {R }
{R }
Euclid’s Algorithm – Loop Invariant
Assume we have a predicate GCD with the following axioms
I ∀x , y . GCD (x , y ) = GCD (y , x )
I ∀x . GCD (0, x ) = x
I ∀x . GCD (x , x ) = x
I ∀x , y . (x ≥ y ∧ y > 0) ⇒ GCD (x , y ) = GCD (x %y , y )
We will first show the following:
while (m != 0) {
r = k % m;
k = m;
m = r;
}
Euclid’s Algorithm – Loop Invariant
Assume we have a predicate GCD with the following axioms
I ∀x , y . GCD (x , y ) = GCD (y , x )
I ∀x . GCD (0, x ) = x
I ∀x . GCD (x , x ) = x
I ∀x , y . (x ≥ y ∧ y > 0) ⇒ GCD (x , y ) = GCD (x %y , y )
while (m != 0) {
r = k % m;
k = m;
m = r;
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
}
Euclid’s Algorithm – Loop Invariant
Assume we have a predicate GCD with the following axioms
I ∀x , y . GCD (x , y ) = GCD (y , x )
I ∀x . GCD (0, x ) = x
I ∀x . GCD (x , x ) = x
I ∀x , y . (x ≥ y ∧ y > 0) ⇒ GCD (x , y ) = GCD (x %y , y )
while (m != 0) {
r = k % m;
k = m;
{k ≥ r ∧ r ≥ 0 ∧ GCD (x , y ) = GCD (k , r )}
m = r;
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
}
Euclid’s Algorithm – Loop Invariant
Assume we have a predicate GCD with the following axioms
I ∀x , y . GCD (x , y ) = GCD (y , x )
I ∀x . GCD (0, x ) = x
I ∀x . GCD (x , x ) = x
I ∀x , y . (x ≥ y ∧ y > 0) ⇒ GCD (x , y ) = GCD (x %y , y )
while (m != 0) {
r = k % m;
{m ≥ r ∧ r ≥ 0 ∧ GCD (x , y ) = GCD (m, r )}
k = m;
{k ≥ r ∧ r ≥ 0 ∧ GCD (x , y ) = GCD (k , r )}
m = r;
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
}
Euclid’s Algorithm – Loop Invariant
Assume we have a predicate GCD with the following axioms
I ∀x , y . GCD (x , y ) = GCD (y , x )
I ∀x . GCD (0, x ) = x
I ∀x . GCD (x , x ) = x
I ∀x , y . (x ≥ y ∧ y > 0) ⇒ GCD (x , y ) = GCD (x %y , y )
while (m != 0) {
{m ≥ k %m ∧ k %m ≥ 0 ∧ GCD (x , y ) = GCD (m, (k %m))}
r = k % m;
{m ≥ r ∧ r ≥ 0 ∧ GCD (x , y ) = GCD (m, r )}
k = m;
{k ≥ r ∧ r ≥ 0 ∧ GCD (x , y ) = GCD (k , r )}
m = r;
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
}
Euclid’s Algorithm – Loop Invariant
(m 6= 0 ∧ k ≥ m ∧ m ≥ 0∧ {m ≥ k %m ∧ k %m ≥ 0∧
GCD (x , y ) = GCD (k , m)) GCD (x , y ) = GCD (m, (k %m))}
⇒ r := k %m; k := m; m = r
(m ≥ k % m ∧ m ≥ 0 {k ≥ m ∧ m ≥ 0∧
∧GCD (x , y ) = GCD (m, (k %m)) GCD (x , y ) = GCD (k , m)}
{m 6= 0 ∧ k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
r := k %m; k := m; m = r
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
Proof derivation for the Implication
We still need to show that
∀x , y . (x ≥ y ) ⇔ ((x = y ) ∨ (x > y ))
∀x , y . (x ≥ y ∧ y > 0) ⇒ x %y ≥ 0
∀x , y . (x ≥ y ∧ y > 0) ⇒ y ≥ x %y
Note: Though tedious, this proof was still not entirely formal.
We implicitly applied a number of rules:
I Quantifier instantiation
I Transitivity of equality
I Following propositional rules:
while (m 6= 0) do
{m 6= 0 ∧ k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
{m ≥ k %m ∧ k %m ≥ 0 ∧ GCD (x , y ) = GCD (m, (k %m))}
r:=k%m; k:=m; m=r
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
{P ∧ B } C {P }
{P } while B do C {¬B ∧ P }
with
def
I P= k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)
def
I B= m 6= 0
Euclid’s Algorithm – Loop Invariant
We obtain:
We obtain:
We obtain:
I We have established
def
P = k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)
as a loop invariant
I If P holds after n iterations of the loop, it also holds after n + 1
I We still need to establish the base case n = 0
Euclid’s Algorithm – Induction, Base Case
Does
k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)
hold at the beginning of the loop?
{?}
if (x > y ) then
k := x;
m := y;
else
k := y;
m := x;
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
Euclid’s Algorithm – Induction, Base Case
{?}
if (x > y ) then
k := x;
m := y;
else
k := y;
m := x;
{?}
if (x > y ) then
k := x;
m := y;
else
k := y;
m := x;
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
Euclid’s Algorithm – Induction, Base Case
{?}
if (x > y ) then
k := x;
m := y;
else
k := y;
{k ≥ x ∧ x ≥ 0 ∧ GCD (x , y ) = GCD (k , x )}
m := x;
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
Euclid’s Algorithm – Induction, Base Case
{?}
if (x > y ) then
k := x;
m := y;
else
{y ≥ x ∧ x ≥ 0 ∧ GCD (x , y ) = GCD (y , x )}
k := y;
{k ≥ x ∧ x ≥ 0 ∧ GCD (x , y ) = GCD (k , x )}
m := x;
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
Euclid’s Algorithm – Induction, Base Case
{?}
if (x > y ) then
k := x;
m := y;
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
else
{y ≥ x ∧ x ≥ 0 ∧ GCD (x , y ) = GCD (y , x )}
k := y;
{k ≥ x ∧ x ≥ 0 ∧ GCD (x , y ) = GCD (k , x )}
m := x;
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
Euclid’s Algorithm – Induction, Base Case
{?}
if (x > y ) then
k := x;
{k ≥ y ∧ y ≥ 0 ∧ GCD (x , y ) = GCD (k , y )}
m := y;
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
else
{y ≥ x ∧ x ≥ 0 ∧ GCD (x , y ) = GCD (y , x )}
k := y;
{k ≥ x ∧ x ≥ 0 ∧ GCD (x , y ) = GCD (k , x )}
m := x;
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
Euclid’s Algorithm – Induction, Base Case
{?}
if (x > y ) then
{x ≥ y ∧ y ≥ 0 ∧ GCD (x , y ) = GCD (x , y )}
k := x;
{k ≥ y ∧ y ≥ 0 ∧ GCD (x , y ) = GCD (k , y )}
m := y;
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
else
{y ≥ x ∧ x ≥ 0 ∧ GCD (x , y ) = GCD (y , x )}
k := y;
{k ≥ x ∧ x ≥ 0 ∧ GCD (x , y ) = GCD (k , x )}
m := x;
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
Euclid’s Algorithm – Induction, Base Case
x >y ∧x ≥0∧y ≥0
x ≥y ∧y ≥0 (GCD (x , y ) = GCD (x , y ))
x ≥ y ∧ y ≥ 0 ∧ (GCD (x , y ) = GCD (x , y ))
I else-branch:
Apply
{B ∧ P } C1 {Q } {¬B ∧ P } C2 {Q }
{P } if B then C1 else C2 {Q }
with
def
I B= x >y
def
I P= x ≥0∧y ≥0
def
I Q= k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)
def
I C1 = k := x ; m := y
def
I C2 = k := y ; m := x
Euclid’s Algorithm – Hoare’s Conditional Rule
We obtain
{x ≥ 0 ∧ y ≥ 0 }
if (x > y ) then
k := x;
m := y;
else
k := y;
m := x;
{k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m)}
Euclid’s Algorithm – Hoare’s Compositional Rule
Finally:
{P } C1 {Q } , {Q } C2 {R }
{P } C1 ; C2 {R }
where
def def
C1 = C2 =
if (x > y ) then
k := x; while (m 6= 0) do
m := y; r := k % m;
else k := m;
k := y; m := r;
m := x;
def
P = x ≥ 0 ∧ y ≥ 0,
def
Q = k ≥ m ∧ m ≥ 0 ∧ GCD (x , y ) = GCD (k , m),
def
R = (GCD (x , y ) = k )
Euclid’s Algorithm – Correctness Established
{x ≥ 0 ∧ y ≥ 0}
if (x > y ) then
k := x;
m := y;
else
k := y;
m := x;
while (m 6= 0) do
r := k % m;
k := m;
m := r;
{GCD (x , y ) = k }
Hoare’s Axioms: Summary
{P } C1 {Q } , {Q } C2 {R }
{P [E /x]} x:=E {P } {P } C1 ; C2 {R }
{B ∧ P } C1 {Q } {¬B ∧ P } C2 {Q }
{P } if B then C1 else C2 {Q }
P0 ⇒ P {P } C {Q } Q ⇒ Q 0
{P 0 } C {Q 0 }
{P ∧ B } C {P }
{P } while B do C {¬B ∧ P }
Hoare Logic: Soundness
Recall:
{P } C {Q } = s |= P ∧ hC , si ⇓ s0 ⇒ s0 |= Q
def
We use
I |= {P } C {Q } to denote that {P } C {Q } is valid.
I ` {P } C {Q } to denote that {P } C {Q } can be derived using
Hoare’s Axioms and the axioms and inference rules of
first-order logic with arithmetic over the integers.
Hoare Logic: Soundness
Recall:
{P } C {Q } = s |= P ∧ hC , si ⇓ s0 ⇒ s0 |= Q
def
We use
I |= {P } C {Q } to denote that {P } C {Q } is valid.
I ` {P } C {Q } to denote that {P } C {Q } can be derived using
Hoare’s Axioms and the axioms and inference rules of
first-order logic with arithmetic over the integers.
We need to show
∀s, s0 . s |= P ∧ hC , si ⇓ s0 ∧ ` {P } C {Q } ⇒ s0 |= Q
Hoare Logic: Soundness
Structural induction:
I Reduce problem P to smaller sub-problem(s) Pi : Pi ≺ P
Hoare Logic: Soundness
Structural induction:
I Reduce problem P to smaller sub-problem(s) Pi : Pi ≺ P
Nested structural induction:
I Consider two structures O, P.
I Define ordering:
Structural induction:
I Reduce problem P to smaller sub-problem(s) Pi : Pi ≺ P
Nested structural induction:
I Consider two structures O, P.
I Define ordering:
{P [E /x]} x := E {P }
Corresponding step in big-step semantics (cf. Lecture 2):
JE Ks = n
hx := E , si ⇓ s[x 7→ n]
Using JE Ks = n, show that:
s |= Q [E /x ] ∧ hx := E , si ⇓ s[x 7→ n]
∀s . ∧ ⇒ s[x 7→ n] |= Q
` {Q [E /x ]} x := E {Q }
Hoare Logic: Soundness of Assignment Rule
{P [E /x]} x := E {P }
Corresponding step in big-step semantics (cf. Lecture 2):
JE Ks = n
hx := E , si ⇓ s[x 7→ n]
Using JE Ks = n, show that:
s |= Q [E /x ] ∧ hx := E , si ⇓ s[x 7→ n]
∀s . ∧ ⇒ s[x 7→ n] |= Q
` {Q [E /x ]} x := E {Q }
Follows from substitution lemma
s |= Q [E /x ] s |= E = n
s[x 7→ n] |= Q
P0 ⇒ P {P } C {Q } Q ⇒ Q 0
{P 0 } C {Q 0 }
Induction hypothesis:
hC , si ⇓ s0 , ` {P } C {Q },
∀s, s . s |= P ∧ hC , si ⇓ s0 ∧ ` {P } C {Q } ⇒ (s0 |= Q )
0
{P } C1 {Q } {Q } C2 {R }
{P } C1 ; C2 {R }
Corresponding step in big-step semantics (cf. Lecture 2):
I Start with s |= P.
I Then s0 |= Q (by i.h. hC1 , si ⇓ s0 , {P } C1 {Q })
I Then s00 |= R (by i.h. hC2 , s0 i ⇓ s00 , {Q } C2 {R })
Hoare Logic: Soundness of While Rule
{P ∧ B } C {P }
{P } while B do C {¬B ∧ P }
There are two corresponding big-step rules (cf. Lecture 2):
hC , si ⇓ s1 hwhile B do C , s1 i ⇓ s0
[B- WHILE .T] hB , si ⇓ true
hwhile B do C , si ⇓ s0
We need to consider both. Start with the easier one.
Hoare Logic: Soundness of While Rule
{P ∧ B } C {P }
{P } while B do C {¬B ∧ P }
{P ∧ B } C {P }
{P } while B do C {¬B ∧ P }
{P ∧ B } C {P }
{P } while B do C {¬B ∧ P }
hC , si ⇓ s1 hwhile B do C , s1 i ⇓ s0
[B- WHILE .T] JB Ks = true
hwhile B do C , si ⇓ s0
Remember:
I Nested induction only requires “progress” in one structure.
I We can “put off” descending `
{P ∧ B } C {P }
{P } while B do C {¬B ∧ P }
hC , si ⇓ s1 hwhile B do C , s1 i ⇓ s0
[B- WHILE .T] JB Ks = true
hwhile B do C , si ⇓ s0
Hoare Logic: Soundness of While Rule
Remember:
I Nested induction only requires “progress” in one structure.
I We can “put off” descending `
{P ∧ B } C {P }
{P } while B do C {¬B ∧ P }
hC , si ⇓ s1 hwhile B do C , s1 i ⇓ s0
[B- WHILE .T] JB Ks = true
hwhile B do C , si ⇓ s0
Remember:
I Nested induction only requires “progress” in one structure.
I We can “put off” descending `
{P ∧ B } C {P }
{P } while B do C {¬B ∧ P }
hC , si ⇓ s1 hwhile B do C , s1 i ⇓ s0
[B- WHILE .T] JB Ks = true
hwhile B do C , si ⇓ s0
We proved that ` {P } C {Q } ⇒ |= {P } C {Q }
I under assumption that the derivations using the axioms and
inference rules of first-order logic with arithmetic over the
integers are sound
What about the other direction?
|= {P } C {Q } ⇒ ` {P } C {Q }
{P } C {Q } = (s |= P ) ∧ hC , si ⇓ s0 ⇒ (s0 |= Q )
def
Hoare Logic: Completeness
{P } C {Q } = (s |= P ) ∧ hC , si ⇓ s0 ⇒ (s0 |= Q )
def
{P } C {Q } = (s |= P ) ∧ hC , si ⇓ s0 ⇒ (s0 |= Q )
def
We will use this insight to relate the validity of Hoare triples to the
Halting Problem.
Excursion: Results on the Halting Problem
Theorem
Theorem
Corollary
Theorem
Corollary
Theorem
Corollary
Alternative argument:
Alternative argument:
Observe:
I Let P be an arbitrary formula.
I If P is valid, then {true} skip {P }.
Hoare Logic: Incompleteness
Proof:
We can assume w.l.o.g. that the set of axioms includes the set
{¬(i = j ) | i , j ∈ Z, i 6= j }∪
{i + j = k | i , j , k ∈ Z, i + j = k }∪
{i ∗ j = k | i , j , k ∈ Z, i ∗ j = k },
pre(C , Q ) = {s | ∀s0 . hC , si ⇓ s0 ⇒ s0 |= Q }
def
∀Q ∈ L, ∀ command C . ∃P ∈ L . {s | s |= P } = pre(C , Q )
Hoare Logic: Relative Completeness and Expressiveness
We define the pre operation for an assertion P as follows:
pre(C , Q ) = {s | ∀s0 . hC , si ⇓ s0 ⇒ s0 |= Q }
def
∀Q ∈ L, ∀ command C . ∃P ∈ L . {s | s |= P } = pre(C , Q )
{s | s |= wlp(C , Q )} = pre(C , Q )
Assume we have
I an assertion language L closed under wlp(C , ·) for all
constructs C in our programming language, and
I a proof system powerful enough such that
∀F ∈ L . (|= F ) ⇒ (` F ) ,
|= {P } C {Q } ⇒ ` {P } C {Q }
{Q [E /x]} x := E {Q }
and
wlp(x := E , Q ) ≡ Q [E /x ].
Hoare Logic: ` {wlp(C , Q )} C {Q }
I Composition.
I We consider {wlp(C1 ; C2 , Q )} C1 ; C2 {Q }.
I By the induction hypothesis we have ` {wlp(C2 , Q )} C2 {Q }
and ` {wlp(C1 , wlp(C2 , Q ))} C1 {wlp(C2 , Q )}.
I We then can apply the composition rule:
I Composition.
I We consider {wlp(C1 ; C2 , Q )} C1 ; C2 {Q }.
I By the induction hypothesis we have ` {wlp(C2 , Q )} C2 {Q }
and ` {wlp(C1 , wlp(C2 , Q ))} C1 {wlp(C2 , Q )}.
I We then can apply the composition rule:
I Branching.
Similar. Left as exercise.
Hoare Logic: ` {wlp(C , Q )} C {Q }
I Loops. Recall:
{R ∧ B } C {R }
{R } while B do C {¬B ∧ R }
I We consider {wlp(while B do C , Q )} while B do C {Q }.
I We set R = wlp(while B do C , Q ) (such a predicate exists as
per our assumption).
I We note that
R ≡wlp(if B then (C ; while B do C ) else skip, Q )
≡(¬B ⇒ Q ) ∧ (B ⇒ wlp(C , wlp(while B do C , Q )))
Hoare Logic: ` {wlp(C , Q )} C {Q }
I Loops. Recall:
{R ∧ B } C {R }
{R } while B do C {¬B ∧ R }
I We consider {wlp(while B do C , Q )} while B do C {Q }.
I We set R = wlp(while B do C , Q ) (such a predicate exists as
per our assumption).
I We note that
R ≡wlp(if B then (C ; while B do C ) else skip, Q )
≡(¬B ⇒ Q ) ∧ (B ⇒ wlp(C , wlp(while B do C , Q )))
| {z }
R
Hoare Logic: ` {wlp(C , Q )} C {Q }
I Loops. Recall:
{R ∧ B } C {R }
{R } while B do C {¬B ∧ R }
I We consider {wlp(while B do C , Q )} while B do C {Q }.
I We set R = wlp(while B do C , Q ) (such a predicate exists as
per our assumption).
I We note that
R ≡wlp(if B then (C ; while B do C ) else skip, Q )
≡(¬B ⇒ Q ) ∧ (B ⇒ wlp(C , R ))
Hoare Logic: ` {wlp(C , Q )} C {Q }
I Loops. Recall:
{R ∧ B } C {R }
{R } while B do C {¬B ∧ R }
I We consider {wlp(while B do C , Q )} while B do C {Q }.
I We set R = wlp(while B do C , Q ) (such a predicate exists as
per our assumption).
I We note that
R ≡wlp(if B then (C ; while B do C ) else skip, Q )
≡(¬B ⇒ Q ) ∧ (B ⇒ wlp(C , R ))
I By the induction hypothesis we have ` {wlp(C , R )} C {R }.
From R ∧ B ⇒ wlp(C , R ) and the consequence rule we then
get ` {R ∧ B } C {R }.
Hoare Logic: ` {wlp(C , Q )} C {Q }
I Loops. Recall:
{R ∧ B } C {R }
{R } while B do C {¬B ∧ R }
I We consider {wlp(while B do C , Q )} while B do C {Q }.
I We set R = wlp(while B do C , Q ) (such a predicate exists as
per our assumption).
I We note that
R ≡wlp(if B then (C ; while B do C ) else skip, Q )
≡(¬B ⇒ Q ) ∧ (B ⇒ wlp(C , R ))
I By the induction hypothesis we have ` {wlp(C , R )} C {R }.
From R ∧ B ⇒ wlp(C , R ) and the consequence rule we then
get ` {R ∧ B } C {R }.
I We can then apply the while-loop rule and obtain
` {R } while B do C {¬B ∧ R }.
Hoare Logic: ` {wlp(C , Q )} C {Q }
I Loops. Recall:
{R ∧ B } C {R }
{R } while B do C {¬B ∧ R }
I We consider {wlp(while B do C , Q )} while B do C {Q }.
I We set R = wlp(while B do C , Q ) (such a predicate exists as
per our assumption).
I We note that
R ≡wlp(if B then (C ; while B do C ) else skip, Q )
≡(¬B ⇒ Q ) ∧ (B ⇒ wlp(C , R ))
I By the induction hypothesis we have ` {wlp(C , R )} C {R }.
From R ∧ B ⇒ wlp(C , R ) and the consequence rule we then
get ` {R ∧ B } C {R }.
I We can then apply the while-loop rule and obtain
` {R } while B do C {¬B ∧ R }.
I The claim then follows from ¬B ∧ R ⇒ Q and the
consequence rule.
Weakest Liberal Precondition
I Predicate Transformers
[Dijkstra75]
I Weakest Pre-Condition
I Strongest Post-Condition
I Invariants defined as
Fixed Points
Edsger W. Dijkstra
(1930–2002)
Weakest Liberal Precondition: Predicate Transformers
def
wlp(x := E , Q ) = Q [E /x ]
def
wlp(skip, Q ) = Q
def
wlp(C1 ; C2 , Q ) = wlp(C1 , wlp(C2 , Q ))
B ⇒ wlp(C1 , Q )
def
wlp(if B then C1 else C2 , Q ) = ∧
¬B ⇒ wlp(C2 , Q )
Using the I.H. and our earlier results it is straight-forward to
establish the correctness for these cases.
Weakest Liberal Precondition: Loops
def
wlp(while B do C , Q ) = ?
Weakest Liberal Precondition: Loops
def
wlp(while B do C , Q ) = ?
Idea: Use Gödel’s β -function to encode sequences of numbers.
We will use a variation of the function used by Gödel.
Gödel’s β -function
Lemma
Lemma
Intuition:
We have s |= wlp(while B do C , Q ) if and only if for every finite
sequence of states s0 , s1 , . . . , sk we have that
I s = s0 ,
I si |= B and hC , si i ⇓ si +1 for all 0 ≤ i < k, and
I sk |= ¬B
imply that
I sk |= Q.
Weakest Liberal Precondition: Loops
We state the weakest liberal precondition for commands C and
Boolean conditions B with a single variable x (the generalization to
several variables x1 , . . . , xn is left as an exercise):
def
wlp(while B do C , Q ) =
∀a, b,k . a ≥ 0 ∧ b ≥ 0 ∧ k ≥ 0
⇒ β(a, b, 0) = x
∧ ∀i , y , z . 0 ≤ i < k
⇒ β(a, b, i ) = y ∧ β(a, b, i + 1) = z
⇒ (B ∧ wlp(C , x = z ) ∧ ¬wlp(C , false))[y /x ]
∧¬B [β(a, b, k )/x ]
⇒ Q [β(a, b, k )/x ].
By the I.H. we have that (B ∧ wlp(C , x = z ) ∧ ¬wlp(C , false))[y /x ]
holds iff {x 7→ y } |= B and hC , {x 7→ y }i ⇓ {x 7→ z }.
Hoare’s Axioms: Summary
{P } C1 {Q } , {Q } C2 {R }
{P [E /x]} x:=E {P } {P } C1 ; C2 {R }
{B ∧ P } C1 {Q } {¬B ∧ P } C2 {Q }
{P } if B then C1 else C2 {Q }
P0 ⇒ P {P } C {Q } Q ⇒ Q 0
{P 0 } C {Q 0 }
{P ∧ B } C {P }
{P } while B do C {¬B ∧ P }
Summary
` {P } C {Q } ⇒ |= {P } C {Q }
I (Relative) Completeness
|= {P } C {Q } ⇒ ` {P } C {Q }
References