COMP2111 Week 8 Term 1, 2020 Hoare Logic 1 Sir Tony Hoare - - PowerPoint PPT Presentation

comp2111 week 8 term 1 2020 hoare logic
SMART_READER_LITE
LIVE PREVIEW

COMP2111 Week 8 Term 1, 2020 Hoare Logic 1 Sir Tony Hoare - - PowerPoint PPT Presentation

COMP2111 Week 8 Term 1, 2020 Hoare Logic 1 Sir Tony Hoare Pioneer in formal verification Invented: Quicksort, the null reference (called it his billion dollar mistake) CSP (formal specification language), and Hoare Logic 2 Summary L


slide-1
SLIDE 1

COMP2111 Week 8 Term 1, 2020 Hoare Logic

1

slide-2
SLIDE 2

Sir Tony Hoare

Pioneer in formal verification Invented: Quicksort, the null reference (called it his “billion dollar mistake”) CSP (formal specification language), and Hoare Logic

2

slide-3
SLIDE 3

Summary

L: A simple imperative programming language Hoare triples (SYNTAX) Hoare logic (PROOF) Semantics for Hoare logic

3

slide-4
SLIDE 4

Summary

L: A simple imperative programming language Hoare triples (SYNTAX) Hoare logic (PROOF) Semantics for Hoare logic

4

slide-5
SLIDE 5

Imperative Programming

imper¯

  • Definition

Imperative programming is where programs are described as a series of statements or commands to manipulate mutable state or cause externally observable effects. States may take the form of a mapping from variable names to their values, or even a model of a CPU state with a memory model (for example, in an assembly language).

5

slide-6
SLIDE 6

L: A simple imperative programming language

Consider the vocabulary of basic arithmetic: Constant symbols: 0, 1, 2, . . . Function symbols: +, ∗, . . . Predicate symbols: <, ≤, ≥, |, . . . An (arithmetic) expression is a term over this vocabulary. A boolean expression is a predicate formula over this vocabulary.

6

slide-7
SLIDE 7

L: A simple imperative programming language

Consider the vocabulary of basic arithmetic: Constant symbols: 0, 1, 2, . . . Function symbols: +, ∗, . . . Predicate symbols: <, ≤, ≥, |, . . . An (arithmetic) expression is a term over this vocabulary. A boolean expression is a predicate formula over this vocabulary.

7

slide-8
SLIDE 8

L: A simple imperative programming language

Consider the vocabulary of basic arithmetic: Constant symbols: 0, 1, 2, . . . Function symbols: +, ∗, . . . Predicate symbols: <, ≤, ≥, |, . . . An (arithmetic) expression is a term over this vocabulary. A boolean expression is a predicate formula over this vocabulary.

8

slide-9
SLIDE 9

The language L

The language L is a simple imperative programming language made up of four statements: Assignment: x :=e where x is a variable and e is an arithmetic expression. Sequencing: P;Q Conditional: if g then P else Q fi where g is a boolean expression. While: while g do P od

9

slide-10
SLIDE 10

The language L

The language L is a simple imperative programming language made up of four statements: Assignment: x:=e where x is a variable and e is an arithmetic expression. Sequencing: P;Q Conditional: if g then P else Q fi where g is a boolean expression. While: while g do P od

10

slide-11
SLIDE 11

The language L

The language L is a simple imperative programming language made up of four statements: Assignment: x:=e where x is a variable and e is an arithmetic expression. Sequencing: P;Q Conditional: if g then P else Q fi where g is a boolean expression. While: while g do P od

11

slide-12
SLIDE 12

The language L

The language L is a simple imperative programming language made up of four statements: Assignment: x:=e where x is a variable and e is an arithmetic expression. Sequencing: P;Q Conditional: if g then P else Q fi where g is a boolean expression. While: while g do P od

12

slide-13
SLIDE 13

Factorial in L

Example i := 0; m := 1; while i < N do i := i + 1; m := m ∗ i

  • d

13

slide-14
SLIDE 14

Summary

L: A simple imperative programming language Hoare triples (SYNTAX) Hoare logic (PROOF) Semantics for Hoare logic

14

slide-15
SLIDE 15

Summary

L: A simple imperative programming language Hoare triples (SYNTAX) Hoare logic (PROOF) Semantics for Hoare logic

15

slide-16
SLIDE 16

Hoare Logic

To give you a taste of axiomatic semantics, and also how formal verification works, we are going to define what’s called a Hoare Logic for L to allow us to prove properties of our program. We write a Hoare triple judgement as:

{ϕ} P {ψ}

Where ϕ and ψ are logical formulae about state variables, called assertions, and P is a program. This triple states that if the program P terminates and it successfully evaluates from a starting state satisfying the precondition ϕ, then the result state will satisfy the postcondition ψ.

16

slide-17
SLIDE 17

Hoare triple: Examples

Example {(x = 0)} x := 1 {(x = 1)} {(x = 499)} x := x + 1 {(x = 500)} {(x > 0)} y := 0 − x {(y < 0) ∧ (x = y)}

17

slide-18
SLIDE 18

Hoare triple: Examples

Example {(x = 0)} x := 1 {(x = 1)} {(x = 499)} x := x + 1 {(x = 500)} {(x > 0)} y := 0 − x {(y < 0) ∧ (x = y)}

18

slide-19
SLIDE 19

Hoare triple: Examples

Example {(x = 0)} x := 1 {(x = 1)} {(x = 499)} x := x + 1 {(x = 500)} {(x > 0)} y := 0 − x {(y < 0) ∧ (x = y)}

19

slide-20
SLIDE 20

Hoare triple: Factorial Examples

Example {N ≥ 0} i := 0; m := 1; while i < N do i := i + 1; m := m ∗ i

  • d

{m = N!}

20

slide-21
SLIDE 21

Summary

L: A simple imperative programming language Hoare triples (SYNTAX) Hoare logic (PROOF) Semantics for Hoare logic

21

slide-22
SLIDE 22

Motivation

Question We know what we want informally; how do we establish when a triple is valid? Develop a semantics, OR Derive the triple in a syntactic manner (i.e. Hoare proof) Hoare logic consists of one axiom and four inference rules for deriving Hoare triples.

22

slide-23
SLIDE 23

Motivation

Question We know what we want informally; how do we establish when a triple is valid? Develop a semantics, OR Derive the triple in a syntactic manner (i.e. Hoare proof) Hoare logic consists of one axiom and four inference rules for deriving Hoare triples.

23

slide-24
SLIDE 24

Motivation

Question We know what we want informally; how do we establish when a triple is valid? Develop a semantics, OR Derive the triple in a syntactic manner (i.e. Hoare proof) Hoare logic consists of one axiom and four inference rules for deriving Hoare triples.

24

slide-25
SLIDE 25

Assignment

(assign) {ϕ[e/x]} x := e {ϕ} Intuition: If x has property ϕ after executing the assignment; then e must have property ϕ before executing the assignment

25

slide-26
SLIDE 26

Assignment: Example

Example {(y = 0)} x := y {(x = 0)} {(y = y)} x := y {(x = y)} {(1 < 2)} x := 1 {(x < 2)} {(y = 3)} x := y {(x > 2)} Problem!

26

slide-27
SLIDE 27

Assignment: Example

Example {(y = 0)} x := y {(x = 0)} {(y = y)} x := y {(x = y)} {(1 < 2)} x := 1 {(x < 2)} {(y = 3)} x := y {(x > 2)} Problem!

27

slide-28
SLIDE 28

Assignment: Example

Example {(y = 0)} x := y {(x = 0)} {(y = y)} x := y {(x = y)} {(1 < 2)} x := 1 {(x < 2)} {(y = 3)} x := y {(x > 2)} Problem!

28

slide-29
SLIDE 29

Assignment: Example

Example {(y = 0)} x := y {(x = 0)} {(y = y)} x := y {(x = y)} {(1 < 2)} x := 1 {(x < 2)} {(y = 3)} x := y {(x > 2)} Problem!

29

slide-30
SLIDE 30

Assignment: Example

Example {(y = 0)} x := y {(x = 0)} {(y = y)} x := y {(x = y)} {(1 < 2)} x := 1 {(x < 2)} {(y = 3)} x := y {(x > 2)} Problem!

30

slide-31
SLIDE 31

Assignment: Example

Example {(y = 0)} x := y {(x = 0)} {(y = y)} x := y {(x = y)} {(1 < 2)} x := 1 {(x < 2)} {(y = 3)} x := y {(x > 2)} Problem!

31

slide-32
SLIDE 32

Sequence

{ϕ} P {ψ} {ψ} Q {ρ} (seq) {ϕ} P; Q {ρ} Intuition: If the postcondition of P matches the precondition of Q we can sequentially combine the two program fragments

32

slide-33
SLIDE 33

Sequence: Example

Example

{(0 = 0)} x := 0 {(x = 0)} {(x = 0)} y := 0 {(x = y)} (seq) {(0 = 0)} x := 0; y := 0 {(x = y)}

33

slide-34
SLIDE 34

Sequence: Example

Example

{(0 = 0)} x := 0 {(x = 0)} {(x = 0)} y := 0 {(x = y)} (seq) {(0 = 0)} x := 0; y := 0 {(x = y)}

34

slide-35
SLIDE 35

Sequence: Example

Example

{(0 = 0)} x := 0 {(x = 0)} {(x = 0)} y := 0 {(x = y)} (seq) {(0 = 0)} x := 0; y := 0 {(x = y)}

35

slide-36
SLIDE 36

Conditional

{ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} (if) {ϕ} if g then P else Q fi {ψ} Intuition: When a conditional is executed, either P or Q will be executed. If ψ is a postcondition of the conditional, then it must be a postcondition of both branches Likewise, f ϕ is a precondition of the conditional, then it must be a precondition of both branches Which branch gets executed depends on g, so we can assume g to be a precondition of P and ¬g to be a precondition of Q (strengthen the preconditions).

36

slide-37
SLIDE 37

While

{ϕ ∧ g} P {ϕ} (loop) {ϕ} while g do P od {ϕ ∧ ¬g} Intuition: ϕ is a loop-invariant. It must be both a pre- and postcondition of P so that sequences of Ps can be run together. If the while loop terminates, g cannot hold.

37

slide-38
SLIDE 38

Consequence

There is one more rule, called the rule of consequence, that we need to insert ordinary logical reasoning into our Hoare logic proofs: ϕ′ → ϕ {ϕ} P {ψ} ψ → ψ′ (cons) {ϕ′} P {ψ′} This is the only rule that is not directed entirely by syntax. This means a Hoare logic proof need not look like a derivation tree. Instead we can sprinkle assertions through our program and specially note uses of the consequence rule. Intuition: Adding assertions to the precondition makes it more likely the postcondition will be reached Removing assertions to the postcondition makes it more likely the postcondition will be reached If you can reach the postcondition initially, then you can reach it in the more likely scenario

38

slide-39
SLIDE 39

Consequence

There is one more rule, called the rule of consequence, that we need to insert ordinary logical reasoning into our Hoare logic proofs: ϕ′ → ϕ {ϕ} P {ψ} ψ → ψ′ (cons) {ϕ′} P {ψ′} This is the only rule that is not directed entirely by syntax. This means a Hoare logic proof need not look like a derivation tree. Instead we can sprinkle assertions through our program and specially note uses of the consequence rule. Intuition: Adding assertions to the precondition makes it more likely the postcondition will be reached Removing assertions to the postcondition makes it more likely the postcondition will be reached If you can reach the postcondition initially, then you can reach it in the more likely scenario

39

slide-40
SLIDE 40

Consequence

There is one more rule, called the rule of consequence, that we need to insert ordinary logical reasoning into our Hoare logic proofs: ϕ′ → ϕ {ϕ} P {ψ} ψ → ψ′ (cons) {ϕ′} P {ψ′} This is the only rule that is not directed entirely by syntax. This means a Hoare logic proof need not look like a derivation tree. Instead we can sprinkle assertions through our program and specially note uses of the consequence rule. Intuition: Adding assertions to the precondition makes it more likely the postcondition will be reached Removing assertions to the postcondition makes it more likely the postcondition will be reached If you can reach the postcondition initially, then you can reach it in the more likely scenario

40

slide-41
SLIDE 41

Back to Assignment Example

Example {(y = 3)} x := y {(x > 2)} Problem!

41

slide-42
SLIDE 42

Back to Assignment Example

Example {(y = 3)} x := y {(x > 2)} Problem! {(y > 2)}x := y{(x > 2)}(assign)

42

slide-43
SLIDE 43

Back to Assignment Example

Example {(y = 3)} x := y {(x > 2)} Problem! {(y = 3)}x := y{(x > 2)}(assign, cons) {(y > 2)}x := y{(x > 2)}(assign)

43

slide-44
SLIDE 44

Factorial Example

Let’s verify the Factorial program using our Hoare rules:

{N ≥ 0} i := 0; m := 1; while i < N do i := i + 1; m := m × i

  • d

{m = N!} {ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} {ϕ} if g then P else Q fi {ψ} {ϕ[x := e]} x := e {ϕ} {ϕ ∧ g} P {ϕ} {ϕ} while g do P od {ϕ ∧ ¬g} {ϕ} P {α} {α} Q {ψ} {ϕ} P; Q {ψ} ϕ′ ⇒ ϕ {ϕ} P {ψ} ψ ⇒ ψ′ {ϕ′} P {ψ′}

44

slide-45
SLIDE 45

Factorial Example

Let’s verify the Factorial program using our Hoare rules:

{N ≥ 0} i := 0; m := 1; while i < N do i := i + 1; m := m × i

  • d {m = i! ∧ N ≥ 0 ∧ i = N}

{m = N!} {ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} {ϕ} if g then P else Q fi {ψ} {ϕ[x := e]} x := e {ϕ} {ϕ ∧ g} P {ϕ} {ϕ} while g do P od {ϕ ∧ ¬g} {ϕ} P {α} {α} Q {ψ} {ϕ} P; Q {ψ} ϕ′ ⇒ ϕ {ϕ} P {ψ} ψ ⇒ ψ′ {ϕ′} P {ψ′}

45

slide-46
SLIDE 46

Factorial Example

Let’s verify the Factorial program using our Hoare rules:

{N ≥ 0} i := 0; m := 1; {m = i! ∧ N ≥ 0} while i < N do i := i + 1; m := m × i

  • d {m = i! ∧ N ≥ 0 ∧ i = N}

{m = N!} {ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} {ϕ} if g then P else Q fi {ψ} {ϕ[x := e]} x := e {ϕ} {ϕ ∧ g} P {ϕ} {ϕ} while g do P od {ϕ ∧ ¬g} {ϕ} P {α} {α} Q {ψ} {ϕ} P; Q {ψ} ϕ′ ⇒ ϕ {ϕ} P {ψ} ψ ⇒ ψ′ {ϕ′} P {ψ′}

46

slide-47
SLIDE 47

Factorial Example

Let’s verify the Factorial program using our Hoare rules:

{N ≥ 0} i := 0; m := 1; {m = i! ∧ N ≥ 0} while i < N do i := i + 1; m := m × i {m = i! ∧ N ≥ 0}

  • d {m = i! ∧ N ≥ 0 ∧ i = N}

{m = N!} {ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} {ϕ} if g then P else Q fi {ψ} {ϕ[x := e]} x := e {ϕ} {ϕ ∧ g} P {ϕ} {ϕ} while g do P od {ϕ ∧ ¬g} {ϕ} P {α} {α} Q {ψ} {ϕ} P; Q {ψ} ϕ′ ⇒ ϕ {ϕ} P {ψ} ψ ⇒ ψ′ {ϕ′} P {ψ′}

47

slide-48
SLIDE 48

Factorial Example

Let’s verify the Factorial program using our Hoare rules:

{N ≥ 0} i := 0; m := 1; {m = i! ∧ N ≥ 0} while i < N do {m = i! ∧ N ≥ 0 ∧ i < N} i := i + 1; m := m × i {m = i! ∧ N ≥ 0}

  • d {m = i! ∧ N ≥ 0 ∧ i = N}

{m = N!} {ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} {ϕ} if g then P else Q fi {ψ} {ϕ[x := e]} x := e {ϕ} {ϕ ∧ g} P {ϕ} {ϕ} while g do P od {ϕ ∧ ¬g} {ϕ} P {α} {α} Q {ψ} {ϕ} P; Q {ψ} ϕ′ ⇒ ϕ {ϕ} P {ψ} ψ ⇒ ψ′ {ϕ′} P {ψ′}

48

slide-49
SLIDE 49

Factorial Example

Let’s verify the Factorial program using our Hoare rules:

{N ≥ 0} i := 0; m := 1; {m = i! ∧ N ≥ 0} while i < N do {m = i! ∧ N ≥ 0 ∧ i < N} i := i + 1; {m × i = i! ∧ N ≥ 0} m := m × i {m = i! ∧ N ≥ 0}

  • d {m = i! ∧ N ≥ 0 ∧ i = N}

{m = N!} {ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} {ϕ} if g then P else Q fi {ψ} {ϕ[x := e]} x := e {ϕ} {ϕ ∧ g} P {ϕ} {ϕ} while g do P od {ϕ ∧ ¬g} {ϕ} P {α} {α} Q {ψ} {ϕ} P; Q {ψ} ϕ′ ⇒ ϕ {ϕ} P {ψ} ψ ⇒ ψ′ {ϕ′} P {ψ′}

49

slide-50
SLIDE 50

Factorial Example

Let’s verify the Factorial program using our Hoare rules:

{N ≥ 0} i := 0; m := 1; {m = i! ∧ N ≥ 0} while i < N do {m = i! ∧ N ≥ 0 ∧ i < N} {m × (i + 1) = (i + 1)! ∧ N ≥ 0} i := i + 1; {m × i = i! ∧ N ≥ 0} m := m × i {m = i! ∧ N ≥ 0}

  • d {m = i! ∧ N ≥ 0 ∧ i = N}

{m = N!} {ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} {ϕ} if g then P else Q fi {ψ} {ϕ[x := e]} x := e {ϕ} {ϕ ∧ g} P {ϕ} {ϕ} while g do P od {ϕ ∧ ¬g} {ϕ} P {α} {α} Q {ψ} {ϕ} P; Q {ψ} ϕ′ ⇒ ϕ {ϕ} P {ψ} ψ ⇒ ψ′ {ϕ′} P {ψ′}

50

slide-51
SLIDE 51

Factorial Example

Let’s verify the Factorial program using our Hoare rules:

{N ≥ 0} i := 0; m := 1; {m = i! ∧ N ≥ 0} while i < N do {m = i! ∧ N ≥ 0 ∧ i < N} {m × (i + 1) = (i + 1)! ∧ N ≥ 0} i := i + 1; {m × i = i! ∧ N ≥ 0} m := m × i {m = i! ∧ N ≥ 0}

  • d {m = i! ∧ N ≥ 0 ∧ i = N}

{m = N!} {ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} {ϕ} if g then P else Q fi {ψ} {ϕ[x := e]} x := e {ϕ} {ϕ ∧ g} P {ϕ} {ϕ} while g do P od {ϕ ∧ ¬g} {ϕ} P {α} {α} Q {ψ} {ϕ} P; Q {ψ} ϕ′ ⇒ ϕ {ϕ} P {ψ} ψ ⇒ ψ′ {ϕ′} P {ψ′}

note: (i + 1)! = i! × (i + 1)

51

slide-52
SLIDE 52

Factorial Example

Let’s verify the Factorial program using our Hoare rules:

{N ≥ 0} i := 0; m := 1;{m = i! ∧ N ≥ 0} {m = i! ∧ N ≥ 0} while i < N do {m = i! ∧ N ≥ 0 ∧ i < N} {m × (i + 1) = (i + 1)! ∧ N ≥ 0} i := i + 1; {m × i = i! ∧ N ≥ 0} m := m × i {m = i! ∧ N ≥ 0}

  • d {m = i! ∧ N ≥ 0 ∧ i = N}

{m = N!} {ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} {ϕ} if g then P else Q fi {ψ} {ϕ[x := e]} x := e {ϕ} {ϕ ∧ g} P {ϕ} {ϕ} while g do P od {ϕ ∧ ¬g} {ϕ} P {α} {α} Q {ψ} {ϕ} P; Q {ψ} ϕ′ ⇒ ϕ {ϕ} P {ψ} ψ ⇒ ψ′ {ϕ′} P {ψ′}

note: (i + 1)! = i! × (i + 1)

52

slide-53
SLIDE 53

Factorial Example

Let’s verify the Factorial program using our Hoare rules:

{N ≥ 0} i := 0; {1 = i! ∧ N ≥ 0} m := 1;{m = i! ∧ N ≥ 0} {m = i! ∧ N ≥ 0} while i < N do {m = i! ∧ N ≥ 0 ∧ i < N} {m × (i + 1) = (i + 1)! ∧ N ≥ 0} i := i + 1; {m × i = i! ∧ N ≥ 0} m := m × i {m = i! ∧ N ≥ 0}

  • d {m = i! ∧ N ≥ 0 ∧ i = N}

{m = N!} {ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} {ϕ} if g then P else Q fi {ψ} {ϕ[x := e]} x := e {ϕ} {ϕ ∧ g} P {ϕ} {ϕ} while g do P od {ϕ ∧ ¬g} {ϕ} P {α} {α} Q {ψ} {ϕ} P; Q {ψ} ϕ′ ⇒ ϕ {ϕ} P {ψ} ψ ⇒ ψ′ {ϕ′} P {ψ′}

note: (i + 1)! = i! × (i + 1)

53

slide-54
SLIDE 54

Factorial Example

Let’s verify the Factorial program using our Hoare rules:

{N ≥ 0} i := 0;{1 = i! ∧ N ≥ 0} {1 = i! ∧ N ≥ 0} m := 1;{m = i! ∧ N ≥ 0} {m = i! ∧ N ≥ 0} while i < N do {m = i! ∧ N ≥ 0 ∧ i < N} {m × (i + 1) = (i + 1)! ∧ N ≥ 0} i := i + 1; {m × i = i! ∧ N ≥ 0} m := m × i {m = i! ∧ N ≥ 0}

  • d {m = i! ∧ N ≥ 0 ∧ i = N}

{m = N!} {ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} {ϕ} if g then P else Q fi {ψ} {ϕ[x := e]} x := e {ϕ} {ϕ ∧ g} P {ϕ} {ϕ} while g do P od {ϕ ∧ ¬g} {ϕ} P {α} {α} Q {ψ} {ϕ} P; Q {ψ} ϕ′ ⇒ ϕ {ϕ} P {ψ} ψ ⇒ ψ′ {ϕ′} P {ψ′}

note: (i + 1)! = i! × (i + 1)

54

slide-55
SLIDE 55

Factorial Example

Let’s verify the Factorial program using our Hoare rules:

{N ≥ 0} {1 = 0! ∧ N ≥ 0} i := 0;{1 = i! ∧ N ≥ 0} {1 = i! ∧ N ≥ 0} m := 1;{m = i! ∧ N ≥ 0} {m = i! ∧ N ≥ 0} while i < N do {m = i! ∧ N ≥ 0 ∧ i < N} {m × (i + 1) = (i + 1)! ∧ N ≥ 0} i := i + 1; {m × i = i! ∧ N ≥ 0} m := m × i {m = i! ∧ N ≥ 0}

  • d {m = i! ∧ N ≥ 0 ∧ i = N}

{m = N!} {ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} {ϕ} if g then P else Q fi {ψ} {ϕ[x := e]} x := e {ϕ} {ϕ ∧ g} P {ϕ} {ϕ} while g do P od {ϕ ∧ ¬g} {ϕ} P {α} {α} Q {ψ} {ϕ} P; Q {ψ} ϕ′ ⇒ ϕ {ϕ} P {ψ} ψ ⇒ ψ′ {ϕ′} P {ψ′}

note: (i + 1)! = i! × (i + 1)

55

slide-56
SLIDE 56

Practice Exercise

Example m := 1; n := 1; i := 1; while i < N do t := m; m := n; n := m + t; i := i + 1

  • d

What does this L program P compute? What is a valid Hoare triple {ϕ}P{ψ} of this program? Prove using the inference rules and consequence axiom that this Hoare triple is valid.

56

slide-57
SLIDE 57

Practice Exercise

Example m := 1; n := 1; i := 1; while i < N do t := m; m := n; n := m + t; i := i + 1

  • d

What does this L program P compute? What is a valid Hoare triple {ϕ}P{ψ} of this program? Prove using the inference rules and consequence axiom that this Hoare triple is valid.

57

slide-58
SLIDE 58

Summary

L: A simple imperative programming language Hoare triples (SYNTAX) Hoare logic (PROOF) Semantics for Hoare logic

58

slide-59
SLIDE 59

Recall

If R and S are binary relations, then the relational composition

  • f R and S, R; S is the relation:

R; S := {(a, c) : ∃b such that (a, b) ∈ R and (b, c) ∈ S} If R ⊆ A × B is a relation, and X ⊆ A, then the image of X under R, R(X) is the subset of B defined as: R(X) := {b ∈ B : ∃a inX such that (a, b) ∈ R}.

59

slide-60
SLIDE 60

Informal semantics

Hoare logic gives a proof of {ϕ} P {ψ}, that is: ⊢ {ϕ} P {ψ} (axiomatic semantics) How do we determine when {ϕ} P {ψ} is valid, that is: | = {ϕ} P {ψ}? If ϕ holds in a state of some computational model then ψ holds in the state reached after a successful execution of P.

60

slide-61
SLIDE 61

Informal semantics

Hoare logic gives a proof of {ϕ} P {ψ}, that is: ⊢ {ϕ} P {ψ} (axiomatic semantics) How do we determine when {ϕ} P {ψ} is valid, that is: | = {ϕ} P {ψ}? If ϕ holds in a state of some computational model then ψ holds in the state reached after a successful execution of P.

61

slide-62
SLIDE 62

Informal semantics: Programs

What is a program? A partial function mapping system states to system states

62

slide-63
SLIDE 63

Informal semantics: Programs

What is a program? A partial function mapping system states to system states

63

slide-64
SLIDE 64

Informal semantics: Programs

What is a program? A partial function mapping system states to system states

64

slide-65
SLIDE 65

Informal semantics: Programs

What is a program? A relation between system states

65

slide-66
SLIDE 66

Informal semantics: States

What is a state of a computational model? Two approaches: Concrete: from a physical perspective

States are memory configurations, register contents, etc. Store of variables and the values associated with them

Abstract: from a mathematical perspective

The pre-/postcondition predicates hold in a state ⇒ States are logical interpretations (Model + Environment) There is only one model of interest: standard interpretations of arithmetical symbols ⇒ States are fully determined by environments ⇒ States are functions that map variables to values

66

slide-67
SLIDE 67

Informal semantics: States

What is a state of a computational model? Two approaches: Concrete: from a physical perspective

States are memory configurations, register contents, etc. Store of variables and the values associated with them

Abstract: from a mathematical perspective

The pre-/postcondition predicates hold in a state ⇒ States are logical interpretations (Model + Environment) There is only one model of interest: standard interpretations of arithmetical symbols ⇒ States are fully determined by environments ⇒ States are functions that map variables to values

67

slide-68
SLIDE 68

Informal semantics: States

What is a state of a computational model? Two approaches: Concrete: from a physical perspective

States are memory configurations, register contents, etc. Store of variables and the values associated with them

Abstract: from a mathematical perspective

The pre-/postcondition predicates hold in a state ⇒ States are logical interpretations (Model + Environment) There is only one model of interest: standard interpretations of arithmetical symbols ⇒ States are fully determined by environments ⇒ States are functions that map variables to values

68

slide-69
SLIDE 69

Informal semantics: States

What is a state of a computational model? Two approaches: Concrete: from a physical perspective

States are memory configurations, register contents, etc. Store of variables and the values associated with them

Abstract: from a mathematical perspective

The pre-/postcondition predicates hold in a state ⇒ States are logical interpretations (Model + Environment) There is only one model of interest: standard interpretations of arithmetical symbols ⇒ States are fully determined by environments ⇒ States are functions that map variables to values

69

slide-70
SLIDE 70

Informal semantics: States

What is a state of a computational model? Two approaches: Concrete: from a physical perspective

States are memory configurations, register contents, etc. Store of variables and the values associated with them

Abstract: from a mathematical perspective

The pre-/postcondition predicates hold in a state ⇒ States are logical interpretations (Model + Environment) There is only one model of interest: standard interpretations of arithmetical symbols ⇒ States are fully determined by environments ⇒ States are functions that map variables to values

70

slide-71
SLIDE 71

Informal semantics: States

What is a state of a computational model? Two approaches: Concrete: from a physical perspective

States are memory configurations, register contents, etc. Store of variables and the values associated with them

Abstract: from a mathematical perspective

The pre-/postcondition predicates hold in a state ⇒ States are logical interpretations (Model + Environment) There is only one model of interest: standard interpretations of arithmetical symbols ⇒ States are fully determined by environments ⇒ States are functions that map variables to values

71

slide-72
SLIDE 72

Informal semantics: States and Programs

State space (Env)

x ← 1 y ← 1 z ← 2 x ← 0 y ← 0 z ← 0 x ← 0 y ← 1 z ← 2 x ← 3 y ← 2 z ← 1 x ← 0 y ← 1 z ← 0 x ← 1 y ← 1 z ← 1 x ← 2 y ← 2 z ← 2

72

slide-73
SLIDE 73

Informal semantics: States and Programs

State space (Env)

x ← 1 y ← 1 z ← 2 x ← 0 y ← 0 z ← 0 x ← 0 y ← 1 z ← 2 x ← 3 y ← 2 z ← 1 x ← 0 y ← 1 z ← 0 x ← 1 y ← 1 z ← 1 x ← 2 y ← 2 z ← 2

73

slide-74
SLIDE 74

Informal semantics: States and Programs

74

slide-75
SLIDE 75

Semantics for L

An environment or state is a function from variables to numeric

  • values. We denote by Env the set of all environments.

NB An environment, η, assigns a numeric value [ [e] ]η to all expressions e, and a boolean value [ [b] ]η to all boolean expressions b. Given a program P of L, we define [ [P] ] to be a binary relation on Env in the following manner...

75

slide-76
SLIDE 76

Semantics for L

An environment or state is a function from variables to numeric

  • values. We denote by Env the set of all environments.

NB An environment, η, assigns a numeric value [ [e] ]η to all expressions e, and a boolean value [ [b] ]η to all boolean expressions b. Given a program P of L, we define [ [P] ] to be a binary relation on Env in the following manner...

76

slide-77
SLIDE 77

Assignment

(η, η′) ∈ [ [x := e] ] if, and only if η′ = η[x → [ [e] ]η]

77

slide-78
SLIDE 78

Assignment: [ [z := 2] ]

State space (Env)

x ← 1 y ← 1 z ← 2 x ← 0 y ← 0 z ← 0 x ← 0 y ← 1 z ← 2 x ← 3 y ← 2 z ← 1 x ← 0 y ← 1 z ← 0 x ← 1 y ← 1 z ← 1 x ← 2 y ← 2 z ← 2

78

slide-79
SLIDE 79

Sequencing

[ [P; Q] ] = [ [P] ]; [ [Q] ] where, on the RHS, ; is relational composition.

79

slide-80
SLIDE 80

Conditional, first attempt

[ [if b then P else Q fi] ] = [ [P] ] if [ [b] ]η = true [ [Q] ]

  • therwise.

80

slide-81
SLIDE 81

Detour: Predicates as programs

A boolean expression b defines a subset (or unary relation) of Env: b = {η : [ [b] ]η = true} This can be extended to a binary relation (i.e. a program): [ [b] ] = {(η, η) : η ∈ b} Intuitively, b corresponds to the program if b then skip else ⊥ fi

81

slide-82
SLIDE 82

Detour: Predicates as programs

A boolean expression b defines a subset (or unary relation) of Env: b = {η : [ [b] ]η = true} This can be extended to a binary relation (i.e. a program): [ [b] ] = {(η, η) : η ∈ b} Intuitively, b corresponds to the program if b then skip else ⊥ fi

82

slide-83
SLIDE 83

Conditional, better attempt

[ [if b then P else Q fi] ] = [ [b; P] ] ∪ [ [¬b; Q] ]

83

slide-84
SLIDE 84

While

while b do P od Do 0 or more executions of P while b holds Terminate when b does not hold How to do “0 or more” executions of (b; P)?

84

slide-85
SLIDE 85

While

while b do P od Do 0 or more executions of (b; P) Terminate with an execution of ¬b How to do “0 or more” executions of (b; P)?

85

slide-86
SLIDE 86

While

while b do P od Do 0 or more executions of (b; P) Terminate with an execution of ¬b How to do “0 or more” executions of (b; P)?

86

slide-87
SLIDE 87

Transitive closure

Given a binary relation R ⊆ E × E, the transitive closure of R, R∗ is defined to be the limit of the sequence R0 ∪ R1 ∪ R2 · · · where R0 = ∆, the diagonal relation Rn+1 = Rn; R NB R∗ is the smallest transitive relation which contains R Related to the Kleene star operation seen in languages: Σ∗ Technically, R∗ is the least-fixed point of f (X) = X ∪ X; R

87

slide-88
SLIDE 88

Transitive closure

Given a binary relation R ⊆ E × E, the transitive closure of R, R∗ is defined to be the limit of the sequence R0 ∪ R1 ∪ R2 · · · where R0 = ∆, the diagonal relation Rn+1 = Rn; R NB R∗ is the smallest transitive relation which contains R Related to the Kleene star operation seen in languages: Σ∗ Technically, R∗ is the least-fixed point of f (X) = X ∪ X; R

88

slide-89
SLIDE 89

While

[ [while b do P od] ] = [ [b; P] ]∗; [ [¬b] ] Do 0 or more executions of (b; P) Conclude with an execution of ¬b

89

slide-90
SLIDE 90

Validity

A Hoare triple is valid, written | = {ϕ} P {ψ} if [ [P] ](ϕ) ⊆ ψ. That is, the relational image under [ [P] ] of the set of states where ϕ holds is contained in the set of states where ψ holds.

90

slide-91
SLIDE 91

Validity

91

slide-92
SLIDE 92

Validity

ϕ

92

slide-93
SLIDE 93

Validity

ϕ ψ

93

slide-94
SLIDE 94

Validity

ϕ ψ

[ [P] ]

94

slide-95
SLIDE 95

Validity

ϕ ψ

[ [P] ](ϕ)

[ [P] ]

95

slide-96
SLIDE 96

Soundness of Hoare Logic

Hoare Logic is sound with respect to the semantics given. That is, Theorem If ⊢ {ϕ} P {ψ} then | = {ϕ} P {ψ}

96

slide-97
SLIDE 97

Summary

Set theory revisited Soundness of Hoare Logic Completeness of Hoare Logic

97

slide-98
SLIDE 98

Summary

Set theory revisited Soundness of Hoare Logic Completeness of Hoare Logic

98

slide-99
SLIDE 99

Some results on relational images

Lemma For any binary relations R, S ⊆ X × Y and subsets A, B ⊆ X:

(a)

If A ⊆ B then R(A) ⊆ R(B)

(b)

R(A) ∪ S(A) = (R ∪ S)(A)

(c)

R(S(A)) = (S; R)(A) Proof (a):

99

slide-100
SLIDE 100

Some results on relational images

Lemma For any binary relations R, S ⊆ X × Y and subsets A, B ⊆ X:

(a)

If A ⊆ B then R(A) ⊆ R(B)

(b)

R(A) ∪ S(A) = (R ∪ S)(A)

(c)

R(S(A)) = (S; R)(A) Proof (a):

100

slide-101
SLIDE 101

Some results on relational images

Lemma For any binary relations R, S ⊆ X × Y and subsets A, B ⊆ X:

(a)

If A ⊆ B then R(A) ⊆ R(B)

(b)

R(A) ∪ S(A) = (R ∪ S)(A)

(c)

R(S(A)) = (S; R)(A) Proof (a): y ∈ R(A) ⇔ ∃x ∈ A such that (x, y) ∈ R ⇒ ∃x ∈ B such that (x, y) ∈ R ⇔ y ∈ R(B)

101

slide-102
SLIDE 102

Some results on relational images

Lemma For any binary relations R, S ⊆ X × Y and subsets A, B ⊆ X:

(a)

If A ⊆ B then R(A) ⊆ R(B)

(b)

R(A) ∪ S(A) = (R ∪ S)(A)

(c)

R(S(A)) = (S; R)(A) Proof (b):

102

slide-103
SLIDE 103

Some results on relational images

Lemma For any binary relations R, S ⊆ X × Y and subsets A, B ⊆ X:

(a)

If A ⊆ B then R(A) ⊆ R(B)

(b)

R(A) ∪ S(A) = (R ∪ S)(A)

(c)

R(S(A)) = (S; R)(A) Proof (b): y ∈ R(A) ∪ S(A) ⇔ y ∈ R(A) or y ∈ S(A) ⇔ ∃x ∈ A s.t. (x, y) ∈ R or ∃x ∈ A s.t. (x, y) ∈ S ⇔ ∃x ∈ A s.t. (x, y) ∈ R or (x, y) ∈ S ⇔ ∃x ∈ A s.t. (x, y) ∈ (R ∪ S) ⇔ y ∈ (R ∪ S)(A)

103

slide-104
SLIDE 104

Some results on relational images

Lemma For any binary relations R, S ⊆ X × Y and subsets A, B ⊆ X:

(a)

If A ⊆ B then R(A) ⊆ R(B)

(b)

R(A) ∪ S(A) = (R ∪ S)(A)

(c)

R(S(A)) = (S; R)(A) Proof (c):

104

slide-105
SLIDE 105

Some results on relational images

Lemma For any binary relations R, S ⊆ X × Y and subsets A, B ⊆ X:

(a)

If A ⊆ B then R(A) ⊆ R(B)

(b)

R(A) ∪ S(A) = (R ∪ S)(A)

(c)

R(S(A)) = (S; R)(A) Proof (c): z ∈ R(S(A)) ⇔ ∃y ∈ S(A) s.t. (y, z) ∈ R ⇔ ∃x ∈ A, y ∈ S(A) s.t. (x, y) ∈ S and (y, z) ∈ R ⇔ ∃x ∈ A s.t. (x, z) ∈ (S; R) ⇔ z ∈ (S; R)(A)

105

slide-106
SLIDE 106

Some results on relational images

Corollary If R(A) ⊆ A then R∗(A) ⊆ A Proof: R(A) ⊆ A ⇒ Ri+1(A) = Ri(R(A)) ⊆ Ri(A) ⇒ Ri+1(A) ⊆ R(A) ⊆ A So R∗(A) = ∞

  • i=0

Ri

  • (A)

=

  • i=0

Ri(A) ⊆ A

106

slide-107
SLIDE 107

Some results on relational images

Corollary If R(A) ⊆ A then R∗(A) ⊆ A Proof: R(A) ⊆ A ⇒ Ri+1(A) = Ri(R(A)) ⊆ Ri(A) ⇒ Ri+1(A) ⊆ R(A) ⊆ A So R∗(A) = ∞

  • i=0

Ri

  • (A)

=

  • i=0

Ri(A) ⊆ A

107

slide-108
SLIDE 108

Some results on relational images

Corollary If R(A) ⊆ A then R∗(A) ⊆ A Proof: R(A) ⊆ A ⇒ Ri+1(A) = Ri(R(A)) ⊆ Ri(A) ⇒ Ri+1(A) ⊆ R(A) ⊆ A So R∗(A) = ∞

  • i=0

Ri

  • (A)

=

  • i=0

Ri(A) ⊆ A

108

slide-109
SLIDE 109

Summary

Set theory revisited Soundness of Hoare Logic Completeness of Hoare Logic

109

slide-110
SLIDE 110

Soundness of Hoare Logic

Theorem If ⊢ {ϕ} P {ψ} then | = {ϕ} P {ψ} Proof: By induction on the structure of the proof.

110

slide-111
SLIDE 111

Soundness of Hoare Logic

Theorem If ⊢ {ϕ} P {ψ} then | = {ϕ} P {ψ} Proof: By induction on the structure of the proof.

111

slide-112
SLIDE 112

Soundness of Hoare Logic

Theorem If ⊢ {ϕ} P {ψ} then | = {ϕ} P {ψ} Proof: By induction on the structure of the proof.

112

slide-113
SLIDE 113

Base case: Assignment rule

(ass) {ϕ[e/x]} x := e {ϕ} Need to show {ϕ[e/x]} x := e {ϕ} is always valid. That is, [ [x := e] ](ϕ[e/x]) ⊆ ϕ. Observation: [ [ϕ[e/x]] ]η = [ [ϕ] ]η′ where η′ = η[x → [ [e] ]η] So if η ∈ ϕ[e/x] then η′ ∈ ϕ Recall: (η, η′′) ∈ [ [x := e] ] if and only if η′′ = η[x → [ [e] ]η], So [ [x := e] ](η) ∈ ϕ for all η ∈ ϕ[e/x] So [ [x := e] ](ϕ[e/x]) ⊆ ϕ

113

slide-114
SLIDE 114

Base case: Assignment rule

(ass) {ϕ[e/x]} x := e {ϕ} Need to show {ϕ[e/x]} x := e {ϕ} is always valid. That is, [ [x := e] ](ϕ[e/x]) ⊆ ϕ. Observation: [ [ϕ[e/x]] ]η = [ [ϕ] ]η′ where η′ = η[x → [ [e] ]η] So if η ∈ ϕ[e/x] then η′ ∈ ϕ Recall: (η, η′′) ∈ [ [x := e] ] if and only if η′′ = η[x → [ [e] ]η], So [ [x := e] ](η) ∈ ϕ for all η ∈ ϕ[e/x] So [ [x := e] ](ϕ[e/x]) ⊆ ϕ

114

slide-115
SLIDE 115

Base case: Assignment rule

(ass) {ϕ[e/x]} x := e {ϕ} Need to show {ϕ[e/x]} x := e {ϕ} is always valid. That is, [ [x := e] ](ϕ[e/x]) ⊆ ϕ. Observation: [ [ϕ[e/x]] ]η = [ [ϕ] ]η′ where η′ = η[x → [ [e] ]η] So if η ∈ ϕ[e/x] then η′ ∈ ϕ Recall: (η, η′′) ∈ [ [x := e] ] if and only if η′′ = η[x → [ [e] ]η], So [ [x := e] ](η) ∈ ϕ for all η ∈ ϕ[e/x] So [ [x := e] ](ϕ[e/x]) ⊆ ϕ

115

slide-116
SLIDE 116

Base case: Assignment rule

(ass) {ϕ[e/x]} x := e {ϕ} Need to show {ϕ[e/x]} x := e {ϕ} is always valid. That is, [ [x := e] ](ϕ[e/x]) ⊆ ϕ. Observation: [ [ϕ[e/x]] ]η = [ [ϕ] ]η′ where η′ = η[x → [ [e] ]η] So if η ∈ ϕ[e/x] then η′ ∈ ϕ Recall: (η, η′′) ∈ [ [x := e] ] if and only if η′′ = η[x → [ [e] ]η], So [ [x := e] ](η) ∈ ϕ for all η ∈ ϕ[e/x] So [ [x := e] ](ϕ[e/x]) ⊆ ϕ

116

slide-117
SLIDE 117

Base case: Assignment rule

(ass) {ϕ[e/x]} x := e {ϕ} Need to show {ϕ[e/x]} x := e {ϕ} is always valid. That is, [ [x := e] ](ϕ[e/x]) ⊆ ϕ. Observation: [ [ϕ[e/x]] ]η = [ [ϕ] ]η′ where η′ = η[x → [ [e] ]η] So if η ∈ ϕ[e/x] then η′ ∈ ϕ Recall: (η, η′′) ∈ [ [x := e] ] if and only if η′′ = η[x → [ [e] ]η], So [ [x := e] ](η) ∈ ϕ for all η ∈ ϕ[e/x] So [ [x := e] ](ϕ[e/x]) ⊆ ϕ

117

slide-118
SLIDE 118

Base case: Assignment rule

(ass) {ϕ[e/x]} x := e {ϕ} Need to show {ϕ[e/x]} x := e {ϕ} is always valid. That is, [ [x := e] ](ϕ[e/x]) ⊆ ϕ. Observation: [ [ϕ[e/x]] ]η = [ [ϕ] ]η′ where η′ = η[x → [ [e] ]η] So if η ∈ ϕ[e/x] then η′ ∈ ϕ Recall: (η, η′′) ∈ [ [x := e] ] if and only if η′′ = η[x → [ [e] ]η], So [ [x := e] ](η) ∈ ϕ for all η ∈ ϕ[e/x] So [ [x := e] ](ϕ[e/x]) ⊆ ϕ

118

slide-119
SLIDE 119

Base case: Assignment rule

(ass) {ϕ[e/x]} x := e {ϕ} Need to show {ϕ[e/x]} x := e {ϕ} is always valid. That is, [ [x := e] ](ϕ[e/x]) ⊆ ϕ. Observation: [ [ϕ[e/x]] ]η = [ [ϕ] ]η′ where η′ = η[x → [ [e] ]η] So if η ∈ ϕ[e/x] then η′ ∈ ϕ Recall: (η, η′′) ∈ [ [x := e] ] if and only if η′′ = η[x → [ [e] ]η], So [ [x := e] ](η) ∈ ϕ for all η ∈ ϕ[e/x] So [ [x := e] ](ϕ[e/x]) ⊆ ϕ

119

slide-120
SLIDE 120

Inductive case 1: Sequence rule

{ϕ} P {ψ} {ψ} Q {ρ} (seq) {ϕ} P; Q {ρ} Assume {ϕ} P {ψ} and {ψ} Q {ρ} are valid. Need to show that {ϕ} P; Q {ρ} is valid. Recall: [ [P; Q] ] = [ [P] ]; [ [Q] ] So: [ [P; Q] ](ϕ) = [ [Q] ]

  • [

[P] ](ϕ)

  • (see Lemma 1(c))

By IH: [ [P] ](ϕ) ⊆ ψ and [ [Q] ](ψ) ⊆ ρ So: [ [Q] ]

  • [

[P] ](ϕ)

  • ⊆ [

[Q] ]

  • ψ
  • ⊆ ρ

(see Lemma 1(a))

120

slide-121
SLIDE 121

Inductive case 1: Sequence rule

{ϕ} P {ψ} {ψ} Q {ρ} (seq) {ϕ} P; Q {ρ} Assume {ϕ} P {ψ} and {ψ} Q {ρ} are valid. Need to show that {ϕ} P; Q {ρ} is valid. Recall: [ [P; Q] ] = [ [P] ]; [ [Q] ] So: [ [P; Q] ](ϕ) = [ [Q] ]

  • [

[P] ](ϕ)

  • (see Lemma 1(c))

By IH: [ [P] ](ϕ) ⊆ ψ and [ [Q] ](ψ) ⊆ ρ So: [ [Q] ]

  • [

[P] ](ϕ)

  • ⊆ [

[Q] ]

  • ψ
  • ⊆ ρ

(see Lemma 1(a))

121

slide-122
SLIDE 122

Inductive case 1: Sequence rule

{ϕ} P {ψ} {ψ} Q {ρ} (seq) {ϕ} P; Q {ρ} Assume {ϕ} P {ψ} and {ψ} Q {ρ} are valid. Need to show that {ϕ} P; Q {ρ} is valid. Recall: [ [P; Q] ] = [ [P] ]; [ [Q] ] So: [ [P; Q] ](ϕ) = [ [Q] ]

  • [

[P] ](ϕ)

  • (see Lemma 1(c))

By IH: [ [P] ](ϕ) ⊆ ψ and [ [Q] ](ψ) ⊆ ρ So: [ [Q] ]

  • [

[P] ](ϕ)

  • ⊆ [

[Q] ]

  • ψ
  • ⊆ ρ

(see Lemma 1(a))

122

slide-123
SLIDE 123

Inductive case 1: Sequence rule

{ϕ} P {ψ} {ψ} Q {ρ} (seq) {ϕ} P; Q {ρ} Assume {ϕ} P {ψ} and {ψ} Q {ρ} are valid. Need to show that {ϕ} P; Q {ρ} is valid. Recall: [ [P; Q] ] = [ [P] ]; [ [Q] ] So: [ [P; Q] ](ϕ) = [ [Q] ]

  • [

[P] ](ϕ)

  • (see Lemma 1(c))

By IH: [ [P] ](ϕ) ⊆ ψ and [ [Q] ](ψ) ⊆ ρ So: [ [Q] ]

  • [

[P] ](ϕ)

  • ⊆ [

[Q] ]

  • ψ
  • ⊆ ρ

(see Lemma 1(a))

123

slide-124
SLIDE 124

Inductive case 1: Sequence rule

{ϕ} P {ψ} {ψ} Q {ρ} (seq) {ϕ} P; Q {ρ} Assume {ϕ} P {ψ} and {ψ} Q {ρ} are valid. Need to show that {ϕ} P; Q {ρ} is valid. Recall: [ [P; Q] ] = [ [P] ]; [ [Q] ] So: [ [P; Q] ](ϕ) = [ [Q] ]

  • [

[P] ](ϕ)

  • (see Lemma 1(c))

By IH: [ [P] ](ϕ) ⊆ ψ and [ [Q] ](ψ) ⊆ ρ So: [ [Q] ]

  • [

[P] ](ϕ)

  • ⊆ [

[Q] ]

  • ψ
  • ⊆ ρ

(see Lemma 1(a))

124

slide-125
SLIDE 125

Inductive case 1: Sequence rule

{ϕ} P {ψ} {ψ} Q {ρ} (seq) {ϕ} P; Q {ρ} Assume {ϕ} P {ψ} and {ψ} Q {ρ} are valid. Need to show that {ϕ} P; Q {ρ} is valid. Recall: [ [P; Q] ] = [ [P] ]; [ [Q] ] So: [ [P; Q] ](ϕ) = [ [Q] ]

  • [

[P] ](ϕ)

  • (see Lemma 1(c))

By IH: [ [P] ](ϕ) ⊆ ψ and [ [Q] ](ψ) ⊆ ρ So: [ [Q] ]

  • [

[P] ](ϕ)

  • ⊆ [

[Q] ]

  • ψ
  • ⊆ ρ

(see Lemma 1(a))

125

slide-126
SLIDE 126

Two more useful results

Lemma For R ⊆ Env × Env, predicates ϕ and ψ, and X ⊆ Env:

(a)

[ [ϕ] ](X) = ϕ ∩ X

(b)

R(ϕ ∧ ψ) = ([ [ϕ] ]; R)(ψ)) Proof (a):

126

slide-127
SLIDE 127

Two more useful results

Lemma For R ⊆ Env × Env, predicates ϕ and ψ, and X ⊆ Env:

(a)

[ [ϕ] ](X) = ϕ ∩ X

(b)

R(ϕ ∧ ψ) = ([ [ϕ] ]; R)(ψ)) Proof (a):

127

slide-128
SLIDE 128

Two more useful results

Lemma For R ⊆ Env × Env, predicates ϕ and ψ, and X ⊆ Env:

(a)

[ [ϕ] ](X) = ϕ ∩ X

(b)

R(ϕ ∧ ψ) = ([ [ϕ] ]; R)(ψ)) Proof (a): η′ ∈ [ [ϕ] ](X) ⇔ ∃η ∈ X s.t. (η, η′) ∈ [ [ϕ] ] ⇔ ∃η ∈ X s.t. η = η′ and η ∈ ϕ ⇔ η′ ∈ X ∩ ϕ

128

slide-129
SLIDE 129

Two more useful results

Lemma For R ⊆ Env × Env, predicates ϕ and ψ, and X ⊆ Env:

(a)

[ [ϕ] ](X) = ϕ ∩ X

(b)

R(ϕ ∧ ψ) = ([ [ϕ] ]; R)(ψ)) Proof (b): ϕ ∧ ψ = ϕ ∩ ψ = [ [ϕ] ](ψ) So R(ϕ ∧ ψ) = R

  • [

[ϕ] ](ψ)

  • =

([ [ϕ] ]; R)(ψ) (see Lemma 1(b))

129

slide-130
SLIDE 130

Inductive case 2: Conditional rule

{ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} (if) {ϕ} if g then P else Q fi {ψ} Assume {ϕ ∧ g} P {ψ} and {ϕ ∧ ¬g} Q {ψ} are valid. Need to show that {ϕ} if g then P else Q fi {ψ} is valid. Recall: [ [if g then P else Q fi] ] = [ [g; P] ] ∪ [ [¬g; Q] ] [ [if g then P else Q fi] ](ϕ) = [ [g; P] ](ϕ) ∪ [ [¬g; Q] ](ϕ) (see Lemma 1(b)) = [ [P] ](g ∧ ϕ) ∪ [ [Q] ](¬g ∧ ϕ) (see Lemma 2(b)) ⊆ ψ (by IH)

130

slide-131
SLIDE 131

Inductive case 2: Conditional rule

{ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} (if) {ϕ} if g then P else Q fi {ψ} Assume {ϕ ∧ g} P {ψ} and {ϕ ∧ ¬g} Q {ψ} are valid. Need to show that {ϕ} if g then P else Q fi {ψ} is valid. Recall: [ [if g then P else Q fi] ] = [ [g; P] ] ∪ [ [¬g; Q] ] [ [if g then P else Q fi] ](ϕ) = [ [g; P] ](ϕ) ∪ [ [¬g; Q] ](ϕ) (see Lemma 1(b)) = [ [P] ](g ∧ ϕ) ∪ [ [Q] ](¬g ∧ ϕ) (see Lemma 2(b)) ⊆ ψ (by IH)

131

slide-132
SLIDE 132

Inductive case 2: Conditional rule

{ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} (if) {ϕ} if g then P else Q fi {ψ} Assume {ϕ ∧ g} P {ψ} and {ϕ ∧ ¬g} Q {ψ} are valid. Need to show that {ϕ} if g then P else Q fi {ψ} is valid. Recall: [ [if g then P else Q fi] ] = [ [g; P] ] ∪ [ [¬g; Q] ] [ [if g then P else Q fi] ](ϕ) = [ [g; P] ](ϕ) ∪ [ [¬g; Q] ](ϕ) (see Lemma 1(b)) = [ [P] ](g ∧ ϕ) ∪ [ [Q] ](¬g ∧ ϕ) (see Lemma 2(b)) ⊆ ψ (by IH)

132

slide-133
SLIDE 133

Inductive case 2: Conditional rule

{ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} (if) {ϕ} if g then P else Q fi {ψ} Assume {ϕ ∧ g} P {ψ} and {ϕ ∧ ¬g} Q {ψ} are valid. Need to show that {ϕ} if g then P else Q fi {ψ} is valid. Recall: [ [if g then P else Q fi] ] = [ [g; P] ] ∪ [ [¬g; Q] ] [ [if g then P else Q fi] ](ϕ) = [ [g; P] ](ϕ) ∪ [ [¬g; Q] ](ϕ) (see Lemma 1(b)) = [ [P] ](g ∧ ϕ) ∪ [ [Q] ](¬g ∧ ϕ) (see Lemma 2(b)) ⊆ ψ (by IH)

133

slide-134
SLIDE 134

Inductive case 2: Conditional rule

{ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} (if) {ϕ} if g then P else Q fi {ψ} Assume {ϕ ∧ g} P {ψ} and {ϕ ∧ ¬g} Q {ψ} are valid. Need to show that {ϕ} if g then P else Q fi {ψ} is valid. Recall: [ [if g then P else Q fi] ] = [ [g; P] ] ∪ [ [¬g; Q] ] [ [if g then P else Q fi] ](ϕ) = [ [g; P] ](ϕ) ∪ [ [¬g; Q] ](ϕ) (see Lemma 1(b)) = [ [P] ](g ∧ ϕ) ∪ [ [Q] ](¬g ∧ ϕ) (see Lemma 2(b)) ⊆ ψ (by IH)

134

slide-135
SLIDE 135

Inductive case 2: Conditional rule

{ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} (if) {ϕ} if g then P else Q fi {ψ} Assume {ϕ ∧ g} P {ψ} and {ϕ ∧ ¬g} Q {ψ} are valid. Need to show that {ϕ} if g then P else Q fi {ψ} is valid. Recall: [ [if g then P else Q fi] ] = [ [g; P] ] ∪ [ [¬g; Q] ] [ [if g then P else Q fi] ](ϕ) = [ [g; P] ](ϕ) ∪ [ [¬g; Q] ](ϕ) (see Lemma 1(b)) = [ [P] ](g ∧ ϕ) ∪ [ [Q] ](¬g ∧ ϕ) (see Lemma 2(b)) ⊆ ψ (by IH)

135

slide-136
SLIDE 136

Inductive case 2: Conditional rule

{ϕ ∧ g} P {ψ} {ϕ ∧ ¬g} Q {ψ} (if) {ϕ} if g then P else Q fi {ψ} Assume {ϕ ∧ g} P {ψ} and {ϕ ∧ ¬g} Q {ψ} are valid. Need to show that {ϕ} if g then P else Q fi {ψ} is valid. Recall: [ [if g then P else Q fi] ] = [ [g; P] ] ∪ [ [¬g; Q] ] [ [if g then P else Q fi] ](ϕ) = [ [g; P] ](ϕ) ∪ [ [¬g; Q] ](ϕ) (see Lemma 1(b)) = [ [P] ](g ∧ ϕ) ∪ [ [Q] ](¬g ∧ ϕ) (see Lemma 2(b)) ⊆ ψ (by IH)

136

slide-137
SLIDE 137

Inductive case 3: While rule

{ϕ ∧ g} P {ϕ} (loop) {ϕ} while g do P od {ϕ ∧ ¬g} Assume {ϕ ∧ g} P {ϕ} is valid. Need to show that {ϕ} while g do P od {ϕ ∧ ¬g}is valid. Recall: [ [while g do P od] ] = [ [g; P] ]∗; [ [¬g] ] [ [g; P] ](ϕ) = [ [P] ](g ∧ ϕ) (see Lemma 2(b)) ⊆ ϕ (IH) So [ [g; P] ]∗(ϕ) ⊆ ϕ (see Corollary) So [ [g; P] ]∗; [ [¬g] ](ϕ) = [ [¬g] ]

  • [

[g; P] ]∗(ϕ)

  • (see Lemma 1(c))

⊆ [ [¬g] ](ϕ) (see Lemma 1(a)) = ¬g ∧ ϕ (see Lemma 2(a))

137

slide-138
SLIDE 138

Inductive case 3: While rule

{ϕ ∧ g} P {ϕ} (loop) {ϕ} while g do P od {ϕ ∧ ¬g} Assume {ϕ ∧ g} P {ϕ} is valid. Need to show that {ϕ} while g do P od {ϕ ∧ ¬g}is valid. Recall: [ [while g do P od] ] = [ [g; P] ]∗; [ [¬g] ] [ [g; P] ](ϕ) = [ [P] ](g ∧ ϕ) (see Lemma 2(b)) ⊆ ϕ (IH) So [ [g; P] ]∗(ϕ) ⊆ ϕ (see Corollary) So [ [g; P] ]∗; [ [¬g] ](ϕ) = [ [¬g] ]

  • [

[g; P] ]∗(ϕ)

  • (see Lemma 1(c))

⊆ [ [¬g] ](ϕ) (see Lemma 1(a)) = ¬g ∧ ϕ (see Lemma 2(a))

138

slide-139
SLIDE 139

Inductive case 3: While rule

{ϕ ∧ g} P {ϕ} (loop) {ϕ} while g do P od {ϕ ∧ ¬g} Assume {ϕ ∧ g} P {ϕ} is valid. Need to show that {ϕ} while g do P od {ϕ ∧ ¬g}is valid. Recall: [ [while g do P od] ] = [ [g; P] ]∗; [ [¬g] ] [ [g; P] ](ϕ) = [ [P] ](g ∧ ϕ) (see Lemma 2(b)) ⊆ ϕ (IH) So [ [g; P] ]∗(ϕ) ⊆ ϕ (see Corollary) So [ [g; P] ]∗; [ [¬g] ](ϕ) = [ [¬g] ]

  • [

[g; P] ]∗(ϕ)

  • (see Lemma 1(c))

⊆ [ [¬g] ](ϕ) (see Lemma 1(a)) = ¬g ∧ ϕ (see Lemma 2(a))

139

slide-140
SLIDE 140

Inductive case 3: While rule

{ϕ ∧ g} P {ϕ} (loop) {ϕ} while g do P od {ϕ ∧ ¬g} Assume {ϕ ∧ g} P {ϕ} is valid. Need to show that {ϕ} while g do P od {ϕ ∧ ¬g}is valid. Recall: [ [while g do P od] ] = [ [g; P] ]∗; [ [¬g] ] [ [g; P] ](ϕ) = [ [P] ](g ∧ ϕ) (see Lemma 2(b)) ⊆ ϕ (IH) So [ [g; P] ]∗(ϕ) ⊆ ϕ (see Corollary) So [ [g; P] ]∗; [ [¬g] ](ϕ) = [ [¬g] ]

  • [

[g; P] ]∗(ϕ)

  • (see Lemma 1(c))

⊆ [ [¬g] ](ϕ) (see Lemma 1(a)) = ¬g ∧ ϕ (see Lemma 2(a))

140

slide-141
SLIDE 141

Inductive case 3: While rule

{ϕ ∧ g} P {ϕ} (loop) {ϕ} while g do P od {ϕ ∧ ¬g} Assume {ϕ ∧ g} P {ϕ} is valid. Need to show that {ϕ} while g do P od {ϕ ∧ ¬g}is valid. Recall: [ [while g do P od] ] = [ [g; P] ]∗; [ [¬g] ] [ [g; P] ](ϕ) = [ [P] ](g ∧ ϕ) (see Lemma 2(b)) ⊆ ϕ (IH) So [ [g; P] ]∗(ϕ) ⊆ ϕ (see Corollary) So [ [g; P] ]∗; [ [¬g] ](ϕ) = [ [¬g] ]

  • [

[g; P] ]∗(ϕ)

  • (see Lemma 1(c))

⊆ [ [¬g] ](ϕ) (see Lemma 1(a)) = ¬g ∧ ϕ (see Lemma 2(a))

141

slide-142
SLIDE 142

Inductive case 3: While rule

{ϕ ∧ g} P {ϕ} (loop) {ϕ} while g do P od {ϕ ∧ ¬g} Assume {ϕ ∧ g} P {ϕ} is valid. Need to show that {ϕ} while g do P od {ϕ ∧ ¬g}is valid. Recall: [ [while g do P od] ] = [ [g; P] ]∗; [ [¬g] ] [ [g; P] ](ϕ) = [ [P] ](g ∧ ϕ) (see Lemma 2(b)) ⊆ ϕ (IH) So [ [g; P] ]∗(ϕ) ⊆ ϕ (see Corollary) So [ [g; P] ]∗; [ [¬g] ](ϕ) = [ [¬g] ]

  • [

[g; P] ]∗(ϕ)

  • (see Lemma 1(c))

⊆ [ [¬g] ](ϕ) (see Lemma 1(a)) = ¬g ∧ ϕ (see Lemma 2(a))

142

slide-143
SLIDE 143

Inductive case 3: While rule

{ϕ ∧ g} P {ϕ} (loop) {ϕ} while g do P od {ϕ ∧ ¬g} Assume {ϕ ∧ g} P {ϕ} is valid. Need to show that {ϕ} while g do P od {ϕ ∧ ¬g}is valid. Recall: [ [while g do P od] ] = [ [g; P] ]∗; [ [¬g] ] [ [g; P] ](ϕ) = [ [P] ](g ∧ ϕ) (see Lemma 2(b)) ⊆ ϕ (IH) So [ [g; P] ]∗(ϕ) ⊆ ϕ (see Corollary) So [ [g; P] ]∗; [ [¬g] ](ϕ) = [ [¬g] ]

  • [

[g; P] ]∗(ϕ)

  • (see Lemma 1(c))

⊆ [ [¬g] ](ϕ) (see Lemma 1(a)) = ¬g ∧ ϕ (see Lemma 2(a))

143

slide-144
SLIDE 144

Inductive case 3: While rule

{ϕ ∧ g} P {ϕ} (loop) {ϕ} while g do P od {ϕ ∧ ¬g} Assume {ϕ ∧ g} P {ϕ} is valid. Need to show that {ϕ} while g do P od {ϕ ∧ ¬g}is valid. Recall: [ [while g do P od] ] = [ [g; P] ]∗; [ [¬g] ] [ [g; P] ](ϕ) = [ [P] ](g ∧ ϕ) (see Lemma 2(b)) ⊆ ϕ (IH) So [ [g; P] ]∗(ϕ) ⊆ ϕ (see Corollary) So [ [g; P] ]∗; [ [¬g] ](ϕ) = [ [¬g] ]

  • [

[g; P] ]∗(ϕ)

  • (see Lemma 1(c))

⊆ [ [¬g] ](ϕ) (see Lemma 1(a)) = ¬g ∧ ϕ (see Lemma 2(a))

144

slide-145
SLIDE 145

Inductive case 3: While rule

{ϕ ∧ g} P {ϕ} (loop) {ϕ} while g do P od {ϕ ∧ ¬g} Assume {ϕ ∧ g} P {ϕ} is valid. Need to show that {ϕ} while g do P od {ϕ ∧ ¬g}is valid. Recall: [ [while g do P od] ] = [ [g; P] ]∗; [ [¬g] ] [ [g; P] ](ϕ) = [ [P] ](g ∧ ϕ) (see Lemma 2(b)) ⊆ ϕ (IH) So [ [g; P] ]∗(ϕ) ⊆ ϕ (see Corollary) So [ [g; P] ]∗; [ [¬g] ](ϕ) = [ [¬g] ]

  • [

[g; P] ]∗(ϕ)

  • (see Lemma 1(c))

⊆ [ [¬g] ](ϕ) (see Lemma 1(a)) = ¬g ∧ ϕ (see Lemma 2(a))

145

slide-146
SLIDE 146

Inductive case 4: Consequence rule

ϕ′ → ϕ {ϕ} P {ψ} ψ → ψ′ (cons) {ϕ′} P {ψ′} Assume {ϕ} P {ψ} is valid and ϕ′ → ϕ and ψ → ψ′. Need to show that {ϕ′} P {ψ′} is valid. Observe: If ϕ′ → ϕ then ϕ′ ⊆ ϕ [ [P] ](ϕ′) ⊆ [ [P] ](ϕ) (see Lemma 1(a)) ⊆ ψ (IH) ⊆ ψ′

146

slide-147
SLIDE 147

Inductive case 4: Consequence rule

ϕ′ → ϕ {ϕ} P {ψ} ψ → ψ′ (cons) {ϕ′} P {ψ′} Assume {ϕ} P {ψ} is valid and ϕ′ → ϕ and ψ → ψ′. Need to show that {ϕ′} P {ψ′} is valid. Observe: If ϕ′ → ϕ then ϕ′ ⊆ ϕ [ [P] ](ϕ′) ⊆ [ [P] ](ϕ) (see Lemma 1(a)) ⊆ ψ (IH) ⊆ ψ′

147

slide-148
SLIDE 148

Inductive case 4: Consequence rule

ϕ′ → ϕ {ϕ} P {ψ} ψ → ψ′ (cons) {ϕ′} P {ψ′} Assume {ϕ} P {ψ} is valid and ϕ′ → ϕ and ψ → ψ′. Need to show that {ϕ′} P {ψ′} is valid. Observe: If ϕ′ → ϕ then ϕ′ ⊆ ϕ [ [P] ](ϕ′) ⊆ [ [P] ](ϕ) (see Lemma 1(a)) ⊆ ψ (IH) ⊆ ψ′

148

slide-149
SLIDE 149

Inductive case 4: Consequence rule

ϕ′ → ϕ {ϕ} P {ψ} ψ → ψ′ (cons) {ϕ′} P {ψ′} Assume {ϕ} P {ψ} is valid and ϕ′ → ϕ and ψ → ψ′. Need to show that {ϕ′} P {ψ′} is valid. Observe: If ϕ′ → ϕ then ϕ′ ⊆ ϕ [ [P] ](ϕ′) ⊆ [ [P] ](ϕ) (see Lemma 1(a)) ⊆ ψ (IH) ⊆ ψ′

149

slide-150
SLIDE 150

Inductive case 4: Consequence rule

ϕ′ → ϕ {ϕ} P {ψ} ψ → ψ′ (cons) {ϕ′} P {ψ′} Assume {ϕ} P {ψ} is valid and ϕ′ → ϕ and ψ → ψ′. Need to show that {ϕ′} P {ψ′} is valid. Observe: If ϕ′ → ϕ then ϕ′ ⊆ ϕ [ [P] ](ϕ′) ⊆ [ [P] ](ϕ) (see Lemma 1(a)) ⊆ ψ (IH) ⊆ ψ′

150

slide-151
SLIDE 151

Soundness of Hoare Logic

Theorem If ⊢ {ϕ} P {ψ} then | = {ϕ} P {ψ}

151

slide-152
SLIDE 152

Summary

Set theory revisited Soundness of Hoare Logic Completeness of Hoare Logic

152

slide-153
SLIDE 153

Incompleteness

Theorem (G¨

  • del’s Incompleteness Theorem)

There is no proof system that can prove every valid first-order sentence about arithmetic over the natural numbers. ⇒ There are true statements that do not have a proof. ⇒ Because of (cons) there are valid triples that result from valid, but unprovable, consequences. ⇒ Hoare Logic is not complete.

153

slide-154
SLIDE 154

Incompleteness

Theorem (G¨

  • del’s Incompleteness Theorem)

There is no proof system that can prove every valid first-order sentence about arithmetic over the natural numbers. ⇒ There are true statements that do not have a proof. ⇒ Because of (cons) there are valid triples that result from valid, but unprovable, consequences. ⇒ Hoare Logic is not complete.

154

slide-155
SLIDE 155

Incompleteness

Theorem (G¨

  • del’s Incompleteness Theorem)

There is no proof system that can prove every valid first-order sentence about arithmetic over the natural numbers. ⇒ There are true statements that do not have a proof. ⇒ Because of (cons) there are valid triples that result from valid, but unprovable, consequences. ⇒ Hoare Logic is not complete.

155

slide-156
SLIDE 156

Incompleteness

Theorem (G¨

  • del’s Incompleteness Theorem)

There is no proof system that can prove every valid first-order sentence about arithmetic over the natural numbers. ⇒ There are true statements that do not have a proof. ⇒ Because of (cons) there are valid triples that result from valid, but unprovable, consequences. ⇒ Hoare Logic is not complete.

156

slide-157
SLIDE 157

Relative completeness of Hoare Logic

Theorem (Relative completeness of Hoare Logic) With an oracle that decides the validity of predicates, if | = {ϕ} P {ψ} then ⊢ {ϕ} P {ψ} .

157

slide-158
SLIDE 158

Need to know for this course

Write programs in L. Give proofs using the Hoare logic rules (full and outline) Definition of [ [·] ] Definition of composition and transitive closure

158