Hoare Logic Andreas Podelski November 8, 2011 Hoare logic - - PowerPoint PPT Presentation

hoare logic
SMART_READER_LITE
LIVE PREVIEW

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-1
SLIDE 1

Hoare Logic

Andreas Podelski November 8, 2011

slide-2
SLIDE 2

Hoare logic

◮ introduced by Hoare in 1969

builds on first-order logic

slide-3
SLIDE 3

Hoare logic

◮ introduced by Hoare in 1969

builds on first-order logic

◮ correctness specification = pre- and postcondition pair

slide-4
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
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
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
SLIDE 7

Programs

◮ (program) expression

e ::= x | f (e1, . . . , en) where f maps into domain of values

slide-8
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
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
SLIDE 10

Semantics of Expression e

◮ state s = function from program variables to value,

s : Var → Val

slide-11
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
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
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
SLIDE 14

Semantics of Boolean Expression b

◮ state s = function from program variables to values,

s : Var → Val

slide-15
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
SLIDE 30

Hoare Triple {φ} C {ψ}

◮ {φ} C {ψ} valid in given logical first-order model if

slide-31
SLIDE 31

Hoare Triple {φ} C {ψ}

◮ {φ} C {ψ} valid in given logical first-order model if

for all states s if [ |φ| ](s) = T and

slide-32
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
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
SLIDE 34

Variables in Hoare Triple {φ} C {ψ}

◮ program variables: occur in commands in program C

slide-35
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
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
SLIDE 37

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

◮ take precondition φ ≡ x = x0 ∧ y = y0 ∧ x0 > y0

and postcondition ψ ≡ x = y0 ∧ y = x0