25
Hoare Logic Jari Stenman February 10, 2012 Jari Stenman () Hoare Logic February 10, 2012 1 / 25

Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Hoare Logic

Jari Stenman

February 10, 2012

Jari Stenman () Hoare Logic February 10, 2012 1 / 25

Page 2: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Outline

1 Background

2 Axioms and Rules

3 A note on Weakest Preconditions

Jari Stenman () Hoare Logic February 10, 2012 2 / 25

Page 3: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Outline

1 Background

2 Axioms and Rules

3 A note on Weakest Preconditions

Jari Stenman () Hoare Logic February 10, 2012 3 / 25

Page 4: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Hoare Logic

Hoare - An axiomatic basis for computer programming (1969)Describes a deductive system for proving program correctness.A set of axioms and inference rules about asserted programs.

Jari Stenman () Hoare Logic February 10, 2012 4 / 25

Page 5: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

While Programs

Assume that we have an underlying logic L, e.g. Integer Arithmetic

Defined inductively:

for every variable x and term t, x := t is a programif S1 and S2 are programs, and e is a boolean expression, the followingare also programs

I S1 ; S2I if e then S1 else S2 fiI while e do S1 od

Jari Stenman () Hoare Logic February 10, 2012 5 / 25

Page 6: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

States

We have a set of variables, typically integersA program can be seen as a set of states, and a set of transitionsbetween states

In the case of integers x1, . . . , xn, the state space of the program is Zn.

A predicate on x1, . . . , xn characterizes a set of states, i.e. a subset ofZn.

Jari Stenman () Hoare Logic February 10, 2012 6 / 25

Page 7: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Hoare Triples

The formulas of Hoare Logic are asserted programs

{p} S {q}

Here, S is a program, and p, q are assertions

p is called the preconditionq is called the postcondition

The above formula states that whenever p holds before running S , and Sterminates, then q will hold after running S .

Jari Stenman () Hoare Logic February 10, 2012 7 / 25

Page 8: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Hoare Triples

Example{x < 1} x := x + 1; x = x + 1 {x < 3}

How can we prove this?Hoare Logic provides axioms and inference rules for proving assertedprograms.

Jari Stenman () Hoare Logic February 10, 2012 8 / 25

Page 9: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Outline

1 Background

2 Axioms and Rules

3 A note on Weakest Preconditions

Jari Stenman () Hoare Logic February 10, 2012 9 / 25

Page 10: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Assignment Axiom Schema

Assignment

{p[t/x ]} x := t {p}

p[t/x ] stands for substituting t for free occurences of x in p

Example{y + 5 = 42} x := y + 5 {x = 42}

ExampleWhat is p if the following is an instance?{p} x := x + 1 {x < 10}

Jari Stenman () Hoare Logic February 10, 2012 10 / 25

Page 11: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Composition Rule

Composition{p} S1 {r} {r} S2 {q}{p} S1 ; S2 {q}

Jari Stenman () Hoare Logic February 10, 2012 11 / 25

Page 12: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Composition Rule

ExampleP: {true} x := 2 ; y := x {x > 0 ∧ y = 2}

We can infer P if we can infer{true} x := 2 {ϕ} and{ϕ} y := x {x > 0 ∧ y = 2}

for some predicate ϕ.By Assignment, we can infer

{x > 0 ∧ x = 2} y := x {x > 0 ∧ y = 2} and{2 > 0 ∧ 2 = 2} x := 2 {x > 0 ∧ x = 2}

Since 2 > 0 ∧ 2 = 2 ≡ true, we have proved the asserted program.

Jari Stenman () Hoare Logic February 10, 2012 12 / 25

Page 13: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Conditional Rule

Conditional{p ∧ e} S1 {q} {p ∧ ¬e} S2 {q}{p} if e then S1 else S2 fi {q}

Jari Stenman () Hoare Logic February 10, 2012 13 / 25

Page 14: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Conditional Rule

Conditional{p ∧ e} S1 {q} {p ∧ ¬e} S2 {q}{p} if e then S1 else S2 fi {q}

Example{true} if x < 10 then x := 10 else x := 0 fi {x = 10 ∨ x = 0}

We can infer this if we can infer

{true ∧ x < 10} x := 10 {x = 10 ∨ x = 0}{true ∧ x ≥ 10} x := 0 {x = 10 ∨ x = 0}

Jari Stenman () Hoare Logic February 10, 2012 14 / 25

Page 15: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Iteration Rule

Iteration{p ∧ e} S {p}

{p}while e do S od {p ∧ ¬e}

Jari Stenman () Hoare Logic February 10, 2012 15 / 25

Page 16: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Iteration Rule

Iteration{p ∧ e} S {p}

{p}while e do S od {p ∧ ¬e}

Example{x ≤ 10}while x < 10 do x := x + 1 od {x = 10}

A: {x + 1 ≤ 10} x := x + 1 {x ≤ 10}L: {x ≤ 10 ∧ x + 1 ≤ 10} x := x + 1 {x ≤ 10}L: {x + 1 ≤ 10 ∧ x ≤ 10} x := x + 1 {x ≤ 10}I: {x ≤ 10}while x + 1 ≤ 10 do x := x + 1 od {x ≤ 10∧ x + 1 6≤ 10}L: {x ≤ 10}while x < 10 do x := x + 1 od {x ≤ 10 ∧ x ≥ 10}L: {x ≤ 10}while x < 10 do x := x + 1 od {x = 10}

Jari Stenman () Hoare Logic February 10, 2012 16 / 25

Page 17: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Rule of Consequence

Consequencep ⇒ p′ {p′} S {q′} q′ ⇒ q

{p} S {q}

We can strengthen the precondition, i.e. assume more than we needWe can weaken the postcondition, i.e. conclude less than we areallowed to

Jari Stenman () Hoare Logic February 10, 2012 17 / 25

Page 18: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Rule of Consequence

Consequencep ⇒ p′ {p′} S {q′} q′ ⇒ q

{p} S {q}

Example{true ∧ x < 10} x := 10 {x = 10 ∨ x = 0}

We have{true} x := 10 {x = 10 ∨ x = 0} by Assignmenttrue ∧ x < 10⇒ truex = 10 ∨ x = 0⇒ x = 10 ∨ x = 0

Jari Stenman () Hoare Logic February 10, 2012 18 / 25

Page 19: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Outline

1 Background

2 Axioms and Rules

3 A note on Weakest Preconditions

Jari Stenman () Hoare Logic February 10, 2012 19 / 25

Page 20: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Proofs in Hoare Logic

An asserted program is sequence {p0} S1 ; S2 ; . . . ; Sn {pn}each Si is either an if-statement, a while-statement or an assignment.

By Composition, the problem of proving this program correct amounts tofinding pi s.t. {p0} S1 {p1}, {p1} S2 {p2}, ... , {pn−1} Sn {pn}

Hoare’s paper doesn’t include any way of computing these intermediateassertions.

Jari Stenman () Hoare Logic February 10, 2012 20 / 25

Page 21: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Weakest Preconditions

Dijkstra’s paper “Guarded Commands, Nondeterminancy and FormalDerivation of Programs” introduces the notion of weakest precondition.

DefinitionThe weakest precondition of a predicate q wrt. a program S , denoted bywp(S , q) is the weakest predicate characterizing all states from which a runof S is guaranteed to terminate in q.

Jari Stenman () Hoare Logic February 10, 2012 21 / 25

Page 22: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Weakest Preconditions

Start with postcondition and “push” it backwards

{p} S1 ; S2 ; S3 {q}{p} S1 ; S2 {wp(S3, q)}{p} S1 {wp(S2,wp(S3, q))}p ⇒ wp(S1,wp(S2,wp(S3, q)))?

A kind of symbolic execution of statements in the domain of predicates.

Jari Stenman () Hoare Logic February 10, 2012 22 / 25

Page 23: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Conclusions

Hoare’s paper founded a whole school of research

A swarm of extensions, for e.g.procedure callsarraysgotopointers

Jari Stenman () Hoare Logic February 10, 2012 23 / 25

Page 24: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Conclusions

Hoare’s paper founded a whole school of research

A number of tools work like follows:

1 Use Hoare-style Logic and WP to generate verification conditions2 Use a general-purpose tool to prove these

Verification conditions do not contain program constucts

Jari Stenman () Hoare Logic February 10, 2012 24 / 25

Page 25: Hoare Logic - Uppsala Universityuser.it.uu.se/~jarst116/slides/week1.pdf · Hoare Logic JariStenman February10,2012 Jari Stenman Hoare Logic February 10, 2012 1 / 25

Conclusions

Thank You!Next presentation: Joe Scott on CTL, Friday 17th in P1112

Jari Stenman () Hoare Logic February 10, 2012 25 / 25