08Program Verification II CS 5209: Foundation in Logic and AI - - PowerPoint PPT Presentation

08 program verification ii
SMART_READER_LITE
LIVE PREVIEW

08Program Verification II CS 5209: Foundation in Logic and AI - - PowerPoint PPT Presentation

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness 08Program Verification II CS 5209: Foundation in Logic and AI Martin


slide-1
SLIDE 1

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

08—Program Verification II

CS 5209: Foundation in Logic and AI

Martin Henz and Aquinas Hobor

March 11, 2010

Generated on Thursday 11th March, 2010, 16:11 CS 5209: Foundation in Logic and AI 08—Program Verification II 1

slide-2
SLIDE 2

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

1

Review

2

Hoare Triples; Partial and Total Correctness

3

Practical Aspects of Correctness Proofs

4

Correctness of the Factorial Function

5

Proof Calculus for Total Correctness

CS 5209: Foundation in Logic and AI 08—Program Verification II 2

slide-3
SLIDE 3

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

1

Review

2

Hoare Triples; Partial and Total Correctness

3

Practical Aspects of Correctness Proofs

4

Correctness of the Factorial Function

5

Proof Calculus for Total Correctness

CS 5209: Foundation in Logic and AI 08—Program Verification II 3

slide-4
SLIDE 4

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Expressions in Core Language

Expressions come as arithmetic expressions E: E ::= n | x | (−E) | (E + E) | (E − E) | (E ∗ E) and boolean expressions B: B ::= true | false | (!B) | (B&B) | (BB) | (E < E) Where are the other comparisons, for example ==?

CS 5209: Foundation in Logic and AI 08—Program Verification II 4

slide-5
SLIDE 5

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Commands in Core Language

Commands cover some common programming idioms. Expressions are components of commands. C ::= x = E | C; C | if B {C} else {C} | while B {C}

CS 5209: Foundation in Logic and AI 08—Program Verification II 5

slide-6
SLIDE 6

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Example

Consider the factorial function: 0!

def

= 1 (n + 1)!

def

= (n + 1) · n! We shall show that after the execution of the following Core program, we have y = x!. y = 1; z = 0; while ( z != x ) { z = z + 1; y = y ∗ z ; }

CS 5209: Foundation in Logic and AI 08—Program Verification II 6

slide-7
SLIDE 7

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

1

Review

2

Hoare Triples; Partial and Total Correctness

3

Practical Aspects of Correctness Proofs

4

Correctness of the Factorial Function

5

Proof Calculus for Total Correctness

CS 5209: Foundation in Logic and AI 08—Program Verification II 7

slide-8
SLIDE 8

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Example

y = 1; z = 0; while ( z != x ) { z = z + 1; y = y ∗ z ; }

CS 5209: Foundation in Logic and AI 08—Program Verification II 8

slide-9
SLIDE 9

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Example

y = 1; z = 0; while ( z != x ) { z = z + 1; y = y ∗ z ; } We need to be able to say that at the end, y is x!, provided that at the beginning, we have x ≥ 0.

CS 5209: Foundation in Logic and AI 08—Program Verification II 9

slide-10
SLIDE 10

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Assertions on Programs

Shape of assertions ( |φ| ) P ( |ψ| ) Informal meaning If the program P is run in a state that satisfies φ, then the state resulting from P’s execution will satisfy ψ.

CS 5209: Foundation in Logic and AI 08—Program Verification II 10

slide-11
SLIDE 11

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Partial Correctness

Definition We say that the triple ( |φ| ) P ( |ψ| ) is satisfied under partial correctness if, for all states which satisfy φ, the state resulting from P’s execution satisfies ψ, provided that P terminates. Notation We write | =par ( |φ| ) P ( |ψ| ).

CS 5209: Foundation in Logic and AI 08—Program Verification II 11

slide-12
SLIDE 12

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Total Correctness

Definition We say that the triple ( |φ| ) P ( |ψ| ) is satisfied under total correctness if, for all states which satisfy φ, P is guaranteed to terminate and the resulting state satisfies ψ. Notation We write | =tot ( |φ| ) P ( |ψ| ).

CS 5209: Foundation in Logic and AI 08—Program Verification II 12

slide-13
SLIDE 13

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Back to Factorial

Consider Fac1: y = 1; z = 0; while ( z != x ) { z = z + 1; y = y ∗ z ; } | =tot ( |x ≥ 0| ) Fac1 ( |y = x!| ) | =tot ( |⊤| ) Fac1 ( |y = x!| )

CS 5209: Foundation in Logic and AI 08—Program Verification II 13

slide-14
SLIDE 14

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Back to Factorial

Consider Fac1: y = 1; z = 0; while ( z != x ) { z = z + 1; y = y ∗ z ; } | =tot ( |x ≥ 0| ) Fac1 ( |y = x!| ) | =par ( |⊤| ) Fac1 ( |y = x!| )

CS 5209: Foundation in Logic and AI 08—Program Verification II 14

slide-15
SLIDE 15

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Rules for Partial Correctness

( |φ| ) C1 ( |η| ) ( |η| ) C2 ( |ψ| ) ( |φ| ) C1; C2 ( |ψ| ) [Composition]

CS 5209: Foundation in Logic and AI 08—Program Verification II 15

slide-16
SLIDE 16

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Rules for Partial Correctness (continued)

( |[x → E]ψ| ) x = E ( |ψ| ) [Assignment]

CS 5209: Foundation in Logic and AI 08—Program Verification II 16

slide-17
SLIDE 17

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Rules for Partial Correctness (continued)

( |φ ∧ B| ) C1 ( |ψ| ) ( |φ ∧ ¬B| ) C2 ( |ψ| ) ( |φ| ) if B { C1 } else { C2 } ( |ψ| ) [If-statement] ( |ψ ∧ B| ) C ( |ψ| ) ( |ψ| ) while B { C } ( |ψ ∧ ¬B| ) [Partial-while]

CS 5209: Foundation in Logic and AI 08—Program Verification II 17

slide-18
SLIDE 18

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Rules for Partial Correctness (continued)

⊢AR φ′ → φ ( |φ| ) C ( |ψ| ) ⊢AR ψ → ψ′ ( |φ′| ) C ( |ψ′| ) [Implied]

CS 5209: Foundation in Logic and AI 08—Program Verification II 18

slide-19
SLIDE 19

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Proof Tableaux

Proofs have tree shape All rules have the structure something something else As a result, all proofs can be written as a tree. Practical concern These trees tend to be very wide when written out on paper. Thus we are using a linear format, called proof tableaux.

CS 5209: Foundation in Logic and AI 08—Program Verification II 19

slide-20
SLIDE 20

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Interleave Formulas with Code

( |φ| ) C1 ( |η| ) ( |η| ) C2 ( |ψ| ) ( |φ| ) C1; C2 ( |ψ| ) [Composition] Shape of rule suggests format for proof of C1; C2; . . . ; Cn: ( |φ0| ) C1; ( |φ1| ) justification C2; . . . ( |φn−1| ) justification Cn; ( |φn| ) justification

CS 5209: Foundation in Logic and AI 08—Program Verification II 20

slide-21
SLIDE 21

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Working Backwards

Overall goal Find a proof that at the end of executing a program P, some condition ψ holds. Common situation If P has the shape C1; . . . ; Cn, we need to find the weakest formula ψ′ such that ( |ψ′| ) Cn ( |ψ| ) Terminology The weakest formula ψ′ is called weakest precondition.

CS 5209: Foundation in Logic and AI 08—Program Verification II 21

slide-22
SLIDE 22

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Example

( |y < 3| ) ( |y + 1 < 4| ) Implied y = y + 1; ( |y < 4| ) Assignment

CS 5209: Foundation in Logic and AI 08—Program Verification II 22

slide-23
SLIDE 23

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Another Example

Can we claim u = x + y after z = x; z = z + y; u = z; ? ( |⊤| ) ( |x + y = x + y| ) Implied z = x; ( |z + y = x + y| ) Assignment z = z + y; ( |z = x + y| ) Assignment u = z; ( |u = x + y| ) Assignment

CS 5209: Foundation in Logic and AI 08—Program Verification II 23

slide-24
SLIDE 24

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

An Alternative Rule for If

We have: ( |φ ∧ B| ) C1 ( |ψ| ) ( |φ ∧ ¬B| ) C2 ( |ψ| ) ( |φ| ) if B { C1 } else { C2 } ( |ψ| ) [If-statement] Sometimes, the following derived rule is more suitable: ( |φ1| ) C1 ( |ψ| ) ( |φ2| ) C2 ( |ψ| ) ( |(B → φ1) ∧ (¬B → φ2)| ) if B { C1 } else { C2 } ( |ψ| ) [If-stmt 2]

CS 5209: Foundation in Logic and AI 08—Program Verification II 24

slide-25
SLIDE 25

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Example

Consider this implementation of Succ: a = x + 1; i f ( a − 1 == 0) { y = 1; } else { y = a ; } Can we prove ( |⊤| ) Succ ( |y = x + 1| ) ?

CS 5209: Foundation in Logic and AI 08—Program Verification II 25

slide-26
SLIDE 26

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Another Example

. . . if ( a − 1 == 0 ) { ( |1 = x + 1| ) If-Statement 2 y = 1; ( |y = x + 1| ) Assignment } else { ( |a = x + 1| ) If-Statement 2 y = a; ( |y = x + 1| ) Assignment } ( |y = x + 1| ) If-Statement 2

CS 5209: Foundation in Logic and AI 08—Program Verification II 26

slide-27
SLIDE 27

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Another Example

( |⊤| ) ( |(x + 1 − 1 = 0 → 1 = x + 1)∧ (¬(x + 1 − 1 = 0) → x + 1 = x + 1)| ) Implied a = x + 1; ( |(a − 1 = 0 → 1 = x + 1)∧ (¬(a − 1 = 0) → a = x + 1)| ) Assignment if ( a − 1 == 0 ) { ( |1 = x + 1| ) If-Statement 2 y = 1; ( |y = x + 1| ) Assignment } else { ( |a = x + 1| ) If-Statement 2 y = a; ( |y = x + 1| ) Assignment

CS 5209: Foundation in Logic and AI 08—Program Verification II 27

slide-28
SLIDE 28

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Recall: Partial-while Rule

( |ψ ∧ B| ) C ( |ψ| ) ( |ψ| ) while B { C } ( |ψ ∧ ¬B| ) [Partial-while]

CS 5209: Foundation in Logic and AI 08—Program Verification II 28

slide-29
SLIDE 29

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Factorial Example

We shall show that the following Core program Fac1 meets this specification: y = 1; z = 0; while ( z != x ) { z = z + 1; y = y ∗ z ; } Thus, to show: ( |⊤| ) Fac1 ( |y = x!| )

CS 5209: Foundation in Logic and AI 08—Program Verification II 29

slide-30
SLIDE 30

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Partial Correctness of Fac1

. . . ( |y = z!| ) while ( z != x ) { ( |y = z! ∧ z = x| ) Invariant ( |y · (z + 1) = (z + 1)!| ) Implied z = z + 1; ( |y · z = z!| ) Assignment y = y ∗ z; ( |y = z!| ) Assignment } ( |y = z! ∧ ¬(z = x)| ) Partial-while ( |y = x!| ) Implied

CS 5209: Foundation in Logic and AI 08—Program Verification II 30

slide-31
SLIDE 31

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Partial Correctness of Fac1

( |⊤| ) ( |(1 = 0!)| ) Implied y = 1; ( |y = 0!| ) Assignment z = 0; ( |y = z!| ) Assignment while ( z != x ) { . . . } ( |y = z! ∧ ¬(z = x)| ) Partial-while ( |y = x!| ) Implied

CS 5209: Foundation in Logic and AI 08—Program Verification II 31

slide-32
SLIDE 32

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

1

Review

2

Hoare Triples; Partial and Total Correctness

3

Practical Aspects of Correctness Proofs

4

Correctness of the Factorial Function

5

Proof Calculus for Total Correctness

CS 5209: Foundation in Logic and AI 08—Program Verification II 32

slide-33
SLIDE 33

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Ideas for Total Correctness

The only source of non-termination is the while command. If we can show that the value of an integer expression decreases in each iteration, but never becomes negative, we have proven termination. Why? Well-foundedness of natural numbers We shall include this argument in a new version of the while rule.

CS 5209: Foundation in Logic and AI 08—Program Verification II 33

slide-34
SLIDE 34

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Rules for Partial Correctness (continued)

( |ψ ∧ B| ) C ( |ψ| ) ( |ψ| ) while B { C } ( |ψ ∧ ¬B| ) [Partial-while] ( |ψ ∧ B ∧ 0 ≤ E = E0| ) C ( |ψ ∧ 0 ≤ E < E0| ) ( |ψ ∧ 0 ≤ E| ) while B { C } ( |ψ ∧ ¬B| ) [Total-while]

CS 5209: Foundation in Logic and AI 08—Program Verification II 34

slide-35
SLIDE 35

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Factorial Example (Again!)

y = 1; z = 0; while ( z != x ) { z = z + 1; y = y ∗ z ; } What could be a good variant E?

CS 5209: Foundation in Logic and AI 08—Program Verification II 35

slide-36
SLIDE 36

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Factorial Example (Again!)

y = 1; z = 0; while ( z != x ) { z = z + 1; y = y ∗ z ; } What could be a good variant E? E must strictly decrease in the loop, but not become negative.

CS 5209: Foundation in Logic and AI 08—Program Verification II 36

slide-37
SLIDE 37

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Factorial Example (Again!)

y = 1; z = 0; while ( z != x ) { z = z + 1; y = y ∗ z ; } What could be a good variant E? E must strictly decrease in the loop, but not become negative. Answer: x − z

CS 5209: Foundation in Logic and AI 08—Program Verification II 37

slide-38
SLIDE 38

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Total Correctness of Fac1

. . . ( |y = z! ∧ 0 ≤ x − z| ) while ( z != x ) { ( |y = z! ∧ z = x ∧ 0 ≤ x − z = E0| ) Invariant ( |y · (z + 1) = (z + 1)! ∧ 0 ≤ x − (z + 1) < E0| ) Implied z = z + 1; ( |y · z = z! ∧ 0 ≤ x − z < E0| ) Assignment y = y ∗ z; ( |y = z! ∧ 0 ≤ x − z < E0| ) Assignment } ( |y = z! ∧ ¬(z = x)| ) Total-while ( |y = x!| ) Implied

CS 5209: Foundation in Logic and AI 08—Program Verification II 38

slide-39
SLIDE 39

Review Hoare Triples; Partial and Total Correctness Practical Aspects of Correctness Proofs Correctness of the Factorial Function Proof Calculus for Total Correctness

Total Correctness of Fac1

( |x ≤ 0| ) ( |(1 = 0! ∧ 0 ≤ x − 0| ) Implied y = 1; ( |y = 0! ∧ 0 ≤ x − 0| ) Assignment z = 0; ( |y = z! ∧ 0 ≤ x − z| ) Assignment while ( z != x ) { . . . } ( |y = z! ∧ ¬(z = x)| ) Total-while ( |y = x!| ) Implied

CS 5209: Foundation in Logic and AI 08—Program Verification II 39