SLIDE 1
Hoare Logic Andreas Podelski November 8, 2011 Hoare logic - - PowerPoint PPT Presentation
Hoare Logic Andreas Podelski November 8, 2011 Hoare logic - - PowerPoint PPT Presentation
Hoare Logic Andreas Podelski November 8, 2011 Hoare logic introduced by Hoare in 1969 builds on first-order logic Hoare logic introduced by Hoare in 1969 builds on first-order logic correctness specification = pre- and
SLIDE 2
SLIDE 3
Hoare logic
◮ introduced by Hoare in 1969
builds on first-order logic
◮ correctness specification = pre- and postcondition pair
SLIDE 4
Hoare logic
◮ introduced by Hoare in 1969
builds on first-order logic
◮ correctness specification = pre- and postcondition pair ◮ standard presentation of Hoare logic:
proof uses invariant for every loop in program
SLIDE 5
Hoare logic
◮ introduced by Hoare in 1969
builds on first-order logic
◮ correctness specification = pre- and postcondition pair ◮ standard presentation of Hoare logic:
proof uses invariant for every loop in program
◮ here:
invariants are given as part of correctness specification
SLIDE 6
Hoare logic
◮ introduced by Hoare in 1969
builds on first-order logic
◮ correctness specification = pre- and postcondition pair ◮ standard presentation of Hoare logic:
proof uses invariant for every loop in program
◮ here:
invariants are given as part of correctness specification
◮ correctness proof possible only if invariants are adequate for
pre- and postcondition pair
SLIDE 7
Programs
◮ (program) expression
e ::= x | f (e1, . . . , en) where f maps into domain of values
SLIDE 8
Programs
◮ (program) expression
e ::= x | f (e1, . . . , en) where f maps into domain of values
◮ Boolean expression
b ::= x | f (e1, . . . , en) where f maps into Boolean domain
SLIDE 9
Programs
◮ (program) expression
e ::= x | f (e1, . . . , en) where f maps into domain of values
◮ Boolean expression
b ::= x | f (e1, . . . , en) where f maps into Boolean domain
◮ command
C ::= skip | x: =e | C1 ; C2 | if b then C1 else C2 | while b do C
SLIDE 10
Semantics of Expression e
◮ state s = function from program variables to value,
s : Var → Val
SLIDE 11
Semantics of Expression e
◮ state s = function from program variables to value,
s : Var → Val
◮ program expression e in state s evaluates to value
[ |e| ](s) ∈ Val
SLIDE 12
Semantics of Expression e
◮ state s = function from program variables to value,
s : Var → Val
◮ program expression e in state s evaluates to value
[ |e| ](s) ∈ Val
◮ semantics of program expressions e
= function from set of states to set of values [ |e| ] : States → Val
SLIDE 13
Semantics of Expression e
◮ state s = function from program variables to value,
s : Var → Val
◮ program expression e in state s evaluates to value
[ |e| ](s) ∈ Val
◮ semantics of program expressions e
= function from set of states to set of values [ |e| ] : States → Val
◮ interpretation of function symbol f in expression f (e1, . . . , en)
depends on logical first-order model (“+” interpreted over model of unbounded integers or in model for modulo arithmetic?)
SLIDE 14
Semantics of Boolean Expression b
◮ state s = function from program variables to values,
s : Var → Val
SLIDE 15
Semantics of Boolean Expression b
◮ state s = function from program variables to values,
s : Var → Val
◮ Boolean expression b in state s evaluates to Boolean truth
value [ |b| ](s) ∈ {T, F}
SLIDE 16
Semantics of Boolean Expression b
◮ state s = function from program variables to values,
s : Var → Val
◮ Boolean expression b in state s evaluates to Boolean truth
value [ |b| ](s) ∈ {T, F}
◮ semantics of Boolean expression b
= function from set of states to set of Boolean truth values [ |b| ] : States → {T, F}
SLIDE 17
Semantics of Boolean Expression b
◮ state s = function from program variables to values,
s : Var → Val
◮ Boolean expression b in state s evaluates to Boolean truth
value [ |b| ](s) ∈ {T, F}
◮ semantics of Boolean expression b
= function from set of states to set of Boolean truth values [ |b| ] : States → {T, F}
◮ evaluation of Boolean expression b depends on logical
first-order model (“x ≤ x + 1” true in model of unbounded integers but false in model for modulo arithmetic)
SLIDE 18
Semantics of Commands C (1)
◮ semantics of command C
= functions from set of states to set of states [ |C| ] : States → States, s → s′
SLIDE 19
Semantics of Commands C (1)
◮ semantics of command C
= functions from set of states to set of states [ |C| ] : States → States, s → s′
◮ execution of command C starting in state s ends in state s′
(C, s) s′
SLIDE 20
Semantics of Commands C (1)
◮ semantics of command C
= functions from set of states to set of states [ |C| ] : States → States, s → s′
◮ execution of command C starting in state s ends in state s′
(C, s) s′
◮ execution of update statement
= update of function s : Var → Val (x: =e, s) s′ where s′(x) = [ |e| ](s) and s′(y) = s(y) for x ≡ y
SLIDE 21
Semantics of Commands C (1)
◮ semantics of command C
= functions from set of states to set of states [ |C| ] : States → States, s → s′
◮ execution of command C starting in state s ends in state s′
(C, s) s′
◮ execution of update statement
= update of function s : Var → Val (x: =e, s) s′ where s′(x) = [ |e| ](s) and s′(y) = s(y) for x ≡ y
◮ execution of update depends on logical first-order model
SLIDE 22
Semantics of Commands C (2)
◮ execution of sequence of commands C ≡ C1 ; C2
= execution of first command C1 followed by execution of second command C2 (C, s) s′′ if (C1, s) s′ and (C2, s′) s′′
SLIDE 23
Semantics of Commands C (2)
◮ execution of sequence of commands C ≡ C1 ; C2
= execution of first command C1 followed by execution of second command C2 (C, s) s′′ if (C1, s) s′ and (C2, s′) s′′
◮ execution of command skip does not change state
(skip, s) s (“empty sequence of commands”)
SLIDE 24
Semantics of Commands C (3)
◮ execution of conditional command C ≡ if b then C1 else C2
= execution of then-command C1 if expression b evaluates to true (C, s) s′ if [ |b| ](s) = T and (C1, s) s′
SLIDE 25
Semantics of Commands C (3)
◮ execution of conditional command C ≡ if b then C1 else C2
= execution of then-command C1 if expression b evaluates to true (C, s) s′ if [ |b| ](s) = T and (C1, s) s′
◮ execution of conditional command C ≡ if b then C1 else C2
= execution of then-command C2 if expression b evaluates to false (C, s) s′ if [ |b| ](s) = F and (C2, s) s′
SLIDE 26
Semantics of Commands C (3)
◮ execution of conditional command C ≡ if b then C1 else C2
= execution of then-command C1 if expression b evaluates to true (C, s) s′ if [ |b| ](s) = T and (C1, s) s′
◮ execution of conditional command C ≡ if b then C1 else C2
= execution of then-command C2 if expression b evaluates to false (C, s) s′ if [ |b| ](s) = F and (C2, s) s′
◮ execution of conditional depends on logical first-order model
SLIDE 27
Semantics of Commands C (4)
◮ execution of while command C ≡ while b do C0
= execution of body C0 followed by execution of while command C if expression b evaluates to true (C, s) s′′ if [ |b| ](s) = T and (C0, s) s′ and (C, s′) s′′
SLIDE 28
Semantics of Commands C (4)
◮ execution of while command C ≡ while b do C0
= execution of body C0 followed by execution of while command C if expression b evaluates to true (C, s) s′′ if [ |b| ](s) = T and (C0, s) s′ and (C, s′) s′′
◮ execution of while command C ≡ while b do C0
= execution of skip if expression b evaluates to false (C, s) s if [ |b| ](s) = F
SLIDE 29
Semantics of Commands C (4)
◮ execution of while command C ≡ while b do C0
= execution of body C0 followed by execution of while command C if expression b evaluates to true (C, s) s′′ if [ |b| ](s) = T and (C0, s) s′ and (C, s′) s′′
◮ execution of while command C ≡ while b do C0
= execution of skip if expression b evaluates to false (C, s) s if [ |b| ](s) = F
◮ execution of while loop depends on logical first-order model
SLIDE 30
Hoare Triple {φ} C {ψ}
◮ {φ} C {ψ} valid in given logical first-order model if
SLIDE 31
Hoare Triple {φ} C {ψ}
◮ {φ} C {ψ} valid in given logical first-order model if
for all states s if [ |φ| ](s) = T and
SLIDE 32
Hoare Triple {φ} C {ψ}
◮ {φ} C {ψ} valid in given logical first-order model if
for all states s if [ |φ| ](s) = T and if (C, s) s′ then
SLIDE 33
Hoare Triple {φ} C {ψ}
◮ {φ} C {ψ} valid in given logical first-order model if
for all states s if [ |φ| ](s) = T and if (C, s) s′ then [ |ψ| ](s′) = T
◮ {φ} C {ψ} valid if valid in every logical first-order model ◮ Γ |
= {φ} C {ψ} if {φ} C {ψ} valid in every logical first-order model of set of assertions Γ
SLIDE 34
Variables in Hoare Triple {φ} C {ψ}
◮ program variables: occur in commands in program C
SLIDE 35
Variables in Hoare Triple {φ} C {ψ}
◮ program variables: occur in commands in program C
may occur (free) in φ and ψ
◮ auxiliary variables: occur (free) in φ and/or ψ
but do not occur in commands in program C
SLIDE 36
Variables in Hoare Triple {φ} C {ψ}
◮ program variables: occur in commands in program C
may occur (free) in φ and ψ
◮ auxiliary variables: occur (free) in φ and/or ψ
but do not occur in commands in program C
◮ needed, e.g., for specification of in-place sort program
if x ≤ y then skip else z: =y ; y: =x ; x: =z
SLIDE 37