MM - Ch2f - Program Verification
MM - Ch2f - Program Verification
Nguyen An Khuong
Chapter 2f
Program Verification
Contents
Mathematical Modeling (CO2011)
Core Programming
Language
Homeworks
Nguyen An Khuong
Faculty of Computer Science and Engineering
University of Technology, VNU-HCM
2f.1
Program Verification
Contents
Nguyen An Khuong
Core Programming
Language
3 Proof Calculus for Partial Correctness Hoare Triples; Partial
and Total Correctness
Practical Aspects of
Correctness Proofs
7 Homeworks
2f.2
Program Verification
Motivation
Nguyen An Khuong
the possible states that a computation system can reach Core Programming
Language
during the execution of the program. Hoare Triples; Partial
and Total Correctness
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.3
Program Verification
Motivation
Nguyen An Khuong
the possible states that a computation system can reach Core Programming
Language
during the execution of the program. Hoare Triples; Partial
and Total Correctness
• Problems with this model checking approach:
Proof Calculus for
• Models become infinite. Partial Correctness
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.3
Program Verification
Motivation
Nguyen An Khuong
the possible states that a computation system can reach Core Programming
Language
during the execution of the program. Hoare Triples; Partial
and Total Correctness
• Problems with this model checking approach:
Proof Calculus for
• Models become infinite. Partial Correctness
Correctness of the
Factorial Function
Homeworks
2f.3
Program Verification
Motivation
Nguyen An Khuong
the possible states that a computation system can reach Core Programming
Language
during the execution of the program. Hoare Triples; Partial
and Total Correctness
• Problems with this model checking approach:
Proof Calculus for
• Models become infinite. Partial Correctness
Homeworks
2f.3
Program Verification
Characteristics of the Approach
Nguyen An Khuong
Contents
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.4
Program Verification
Characteristics of the Approach
Nguyen An Khuong
Contents
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.4
Program Verification
Characteristics of the Approach
Nguyen An Khuong
Contents
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.4
Program Verification
Characteristics of the Approach
Nguyen An Khuong
Contents
Correctness of the
Factorial Function
Homeworks
2f.4
Program Verification
Characteristics of the Approach
Nguyen An Khuong
Contents
Homeworks
2f.4
Program Verification
Reasons for Program Verification
Nguyen An Khuong
Contents
Documentation. Program properties formulated as theorems can
Core Programming
serve as concise documentation Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.5
Program Verification
Reasons for Program Verification
Nguyen An Khuong
Contents
Documentation. Program properties formulated as theorems can
Core Programming
serve as concise documentation Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.5
Program Verification
Reasons for Program Verification
Nguyen An Khuong
Contents
Documentation. Program properties formulated as theorems can
Core Programming
serve as concise documentation Language
Correctness of the
Factorial Function
Homeworks
2f.5
Program Verification
Reasons for Program Verification
Nguyen An Khuong
Contents
Documentation. Program properties formulated as theorems can
Core Programming
serve as concise documentation Language
Homeworks
2f.5
Program Verification
Framework for Software Verification
Nguyen An Khuong
Contents
Core Programming
Convert informal description R of requirements for an Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.6
Program Verification
Framework for Software Verification
Nguyen An Khuong
Contents
Core Programming
Convert informal description R of requirements for an Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.6
Program Verification
Framework for Software Verification
Nguyen An Khuong
Contents
Core Programming
Convert informal description R of requirements for an Language
Correctness of the
Factorial Function
Homeworks
2f.6
Program Verification
Framework for Software Verification
Nguyen An Khuong
Contents
Core Programming
Convert informal description R of requirements for an Language
Homeworks
2f.6
Program Verification
Nguyen An Khuong
Correctness of the
Factorial Function
6 Proof Calculus for Total Correctness Proof Calculus for
Total Correctness
Homeworks
7 Homeworks
2f.7
Program Verification
Motivation of Core Language
Nguyen An Khuong
Core Programming
constructs Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.8
Program Verification
Motivation of Core Language
Nguyen An Khuong
Core Programming
constructs Language
• Verification framework would exceed time we have in CS5209 Hoare Triples; Partial
and Total Correctness
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.8
Program Verification
Motivation of Core Language
Nguyen An Khuong
Core Programming
constructs Language
• Verification framework would exceed time we have in CS5209 Hoare Triples; Partial
and Total Correctness
Correctness of the
Factorial Function
Homeworks
2f.8
Program Verification
Motivation of Core Language
Nguyen An Khuong
Core Programming
constructs Language
• Verification framework would exceed time we have in CS5209 Hoare Triples; Partial
and Total Correctness
Homeworks
2f.8
Program Verification
Motivation of Core Language
Nguyen An Khuong
Core Programming
constructs Language
• Verification framework would exceed time we have in CS5209 Hoare Triples; Partial
and Total Correctness
Homeworks
2f.8
Program Verification
Expressions in Core Language
Nguyen An Khuong
Core Programming
Language
E ::= n | x | (−E) | (E + E) | (E − E) | (E ∗ E)
Hoare Triples; Partial
and Total Correctness
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.9
Program Verification
Expressions in Core Language
Nguyen An Khuong
Core Programming
Language
E ::= n | x | (−E) | (E + E) | (E − E) | (E ∗ E)
Hoare Triples; Partial
and Total Correctness
Practical Aspects of
B ::= true | false | (!B) | (B&B) | (BkB) | (E < E) Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.9
Program Verification
Expressions in Core Language
Nguyen An Khuong
Core Programming
Language
E ::= n | x | (−E) | (E + E) | (E − E) | (E ∗ E)
Hoare Triples; Partial
and Total Correctness
Practical Aspects of
B ::= true | false | (!B) | (B&B) | (BkB) | (E < E) Correctness Proofs
Correctness of the
Factorial Function
Where are the other comparisons, for example ==?
Proof Calculus for
Total Correctness
Homeworks
2f.9
Program Verification
Commands in Core Language
Nguyen An Khuong
Contents
Core Programming
Commands cover some common programming idioms. Expressions Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.10
Program Verification
Example
Nguyen An Khuong
Homeworks
2f.11
Program Verification
Nguyen An Khuong
Correctness of the
Factorial Function
6 Proof Calculus for Total Correctness Proof Calculus for
Total Correctness
Homeworks
7 Homeworks
2f.12
Program Verification
Example
Nguyen An Khuong
Contents
Core Programming
Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.13
Program Verification
Example
Nguyen An Khuong
Contents
Core Programming
y = 1; Language
Correctness of the
Factorial Function
Homeworks
2f.14
Program Verification
Example
Nguyen An Khuong
Contents
y = 1; Core Programming
Language
z = 0;
Hoare Triples; Partial
while ( z != x ) { z = z + 1; y = y * z ; } and Total Correctness
Correctness of the
Factorial Function
Homeworks
2f.15
Program Verification
Example
Nguyen An Khuong
Contents
Core Programming
y = 1; Language
Correctness of the
Factorial Function
Homeworks
2f.16
Program Verification
Example
Nguyen An Khuong
Contents
y = 1;
Core Programming
z = 0; Language
while ( z != x ) { z = z + 1; y = y * z ; }
Hoare Triples; Partial
and Total Correctness
Correctness of the
Example: x > 0 Factorial Function
Homeworks
2f.17
Program Verification
Example
Nguyen An Khuong
y = 1; Contents
z = 0; Core Programming
while ( z != x ) { z = z + 1; y = y * z ; } Language
Homeworks
2f.18
Program Verification
Example
Nguyen An Khuong
y = 1;
z = 0; Contents
while ( z != x ) { z = z + 1; y = y * z ; } Core Programming
Language
2f.19
Program Verification
Assertions on Programs
Nguyen An Khuong
Core Programming
Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.20
Program Verification
Assertions on Programs
Nguyen An Khuong
Core Programming
Language
If the program P is run in a state that satisfies φ, then the state Correctness of the
Factorial Function
resulting from P ’s execution will satisfy ψ.
Proof Calculus for
Total Correctness
Homeworks
2f.20
Program Verification
(Slightly Trivial) Example
Nguyen An Khuong
Informal specification
Given a positive number x, the program P calculates a number y
whose square is less than x.
Contents
Core Programming
Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.21
Program Verification
(Slightly Trivial) Example
Nguyen An Khuong
Informal specification
Given a positive number x, the program P calculates a number y
whose square is less than x.
Contents
Core Programming
Assertion Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.21
Program Verification
(Slightly Trivial) Example
Nguyen An Khuong
Informal specification
Given a positive number x, the program P calculates a number y
whose square is less than x.
Contents
Core Programming
Assertion Language
Practical Aspects of
Correctness Proofs
Example for P
Correctness of the
Factorial Function
y = 0
Proof Calculus for
Total Correctness
Homeworks
2f.21
Program Verification
(Slightly Trivial) Example
Nguyen An Khuong
Informal specification
Given a positive number x, the program P calculates a number y
whose square is less than x.
Contents
Core Programming
Assertion Language
Practical Aspects of
Correctness Proofs
Example for P
Correctness of the
Factorial Function
y = 0
Proof Calculus for
Total Correctness
2f.21
Program Verification
(Slightly Less Trivial) Example
Nguyen An Khuong
Same assertion
Contents
Practical Aspects of
y = 0; Correctness Proofs
while ( y * y < x ) { Correctness of the
y = y + 1; Factorial Function
} Proof Calculus for
Total Correctness
y = y - 1;
Homeworks
2f.22
Program Verification
Recall: Models in Predicate Logic
Nguyen An Khuong
Definition
Contents
Let F contain function symbols and P contain predicate symbols. Core Programming
A model M for (F, P) consists of: Language
2 for each nullary function symbol f ∈ F a concrete element Proof Calculus for
Partial Correctness
f M ∈ A; Practical Aspects of
Correctness Proofs
3 for each f ∈ F with arity n > 0, a concrete function Correctness of the
f M : An → A; Factorial Function
Homeworks
2f.23
Program Verification
Recall: Satisfaction Relation
Nguyen An Khuong
Correctness of the
• in case φ has the form ∃xψ, if the M |=l[x7→a] ψ holds for Factorial Function
Homeworks
2f.24
Program Verification
Recall: Satisfaction Relation (continued)
Nguyen An Khuong
Contents
• in case φ has the form ¬ψ, if M |=l ψ does not hold;
Core Programming
• in case φ has the form ψ1 ∨ ψ2 , if M |=l ψ1 holds or Language
• in case φ has the form ψ1 ∧ ψ2 , if M |=l ψ1 holds and Proof Calculus for
Partial Correctness
M |=l ψ2 holds; and Practical Aspects of
Correctness Proofs
• in case φ has the form ψ1 → ψ2 , if M |=l ψ1 holds whenever
Correctness of the
M |=l ψ2 holds. Factorial Function
Homeworks
2f.25
Program Verification
Hoare Triples
Nguyen An Khuong
Definition
Contents
An assertion of the form (|φ|) P (|ψ|) is called a Hoare triple. Core Programming
Language
• φ is called the precondition, ψ is called the postcondition.
Hoare Triples; Partial
• A state of a Core program P is a function l that assigns each and Total Correctness
Homeworks
2f.26
Program Verification
Example
Nguyen An Khuong
Contents
Core Programming
Let l(x) = −2, l(y) = 5 and l(z) = −1. We have: Language
Correctness of the
Factorial Function
Homeworks
2f.27
Program Verification
Partial Correctness
Nguyen An Khuong
Contents
Definition
Core Programming
We say that the triple (|φ|) P (|ψ|) is satisfied under partial Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.28
Program Verification
Partial Correctness
Nguyen An Khuong
Contents
Definition
Core Programming
We say that the triple (|φ|) P (|ψ|) is satisfied under partial Language
Practical Aspects of
Notation Correctness Proofs
Correctness of the
We write |=par (|φ|) P (|ψ|). Factorial Function
Homeworks
2f.28
Program Verification
Extreme Example
Nguyen An Khuong
Contents
Core Programming
Language
(|φ|) while true { x = 0; } (|ψ|) Hoare Triples; Partial
and Total Correctness
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.29
Program Verification
Total Correctness
Nguyen An Khuong
Contents
Definition
Core Programming
We say that the triple (|φ|) P (|ψ|) is satisfied under total Language
Practical Aspects of
Notation Correctness Proofs
Correctness of the
We write |=tot (|φ|) P (|ψ|). Factorial Function
Homeworks
2f.30
Program Verification
Back to Factorial
Nguyen An Khuong
Contents
Core Programming
Consider Fac1: Language
Correctness of the
Factorial Function
Homeworks
2f.31
Program Verification
Back to Factorial
Nguyen An Khuong
Contents
Practical Aspects of
• |=tot (|x ≥ 0|) Fac1 (|y = x!|) Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.32
Program Verification
Back to Factorial
Nguyen An Khuong
Contents
Consider Fac1:
Core Programming
Language
y = 1;
z = 0; Hoare Triples; Partial
and Total Correctness
while ( z != x ) { z = z + 1; y = y * z ; }
Proof Calculus for
Partial Correctness
Homeworks
2f.33
Program Verification
Back to Factorial
Nguyen An Khuong
Core Programming
y = 1; Language
z = 0;
Hoare Triples; Partial
while ( z != x ) { z = z + 1; y = y * z ; } and Total Correctness
Correctness of the
• |=par (|x ≥ 0|) Fac1 (|y = x!|) Factorial Function
Homeworks
2f.34
Program Verification
Back to Factorial
Nguyen An Khuong
Consider Fac1:
Contents
y = 1; Core Programming
z = 0; Language
while ( z != x ) { z = z + 1; y = y * z ; } Hoare Triples; Partial
and Total Correctness
Homeworks
2f.35
Program Verification
Nguyen An Khuong
Correctness of the
Factorial Function
6 Proof Calculus for Total Correctness Proof Calculus for
Total Correctness
Homeworks
7 Homeworks
2f.36
Program Verification
Strategy
Nguyen An Khuong
Core Programming
`par (|φ|) P (|ψ|) Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.37
Program Verification
Strategy
Nguyen An Khuong
Core Programming
`par (|φ|) P (|ψ|) Language
Practical Aspects of
(correctness) Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.37
Program Verification
Strategy
Nguyen An Khuong
Core Programming
`par (|φ|) P (|ψ|) Language
Practical Aspects of
(correctness), and Correctness Proofs
• `par (|φ|) P (|ψ|) holds whenever |=par (|φ|) P (|ψ|) Correctness of the
Factorial Function
(completeness). Proof Calculus for
Total Correctness
Homeworks
2f.37
Program Verification
Rules for Partial Correctness
Nguyen An Khuong
Contents
Core Programming
Language
Correctness of the
Factorial Function
Homeworks
2f.38
Program Verification
Rules for Partial Correctness (continued)
Nguyen An Khuong
Contents
Core Programming
Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.39
Program Verification
Examples
Nguyen An Khuong
Core Programming
Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.40
Program Verification
Examples
Nguyen An Khuong
Core Programming
[Assignment] Language
Correctness of the
Factorial Function
Homeworks
2f.40
Program Verification
Examples
Nguyen An Khuong
Core Programming
[Assignment] Language
Correctness of the
Factorial Function
Homeworks
2f.40
Program Verification
Examples
Nguyen An Khuong
Core Programming
[Assignment] Language
Correctness of the
• (|2 = 4|) P (|x = 4|) Factorial Function
Homeworks
2f.40
Program Verification
Examples
Nguyen An Khuong
Core Programming
[Assignment] Language
Correctness of the
• (|2 = 4|) P (|x = 4|) Factorial Function
Homeworks
2f.40
Program Verification
Examples
Nguyen An Khuong
Core Programming
[Assignment] Language
Correctness of the
• (|2 = 4|) P (|x = 4|) Factorial Function
2f.40
Program Verification
More Examples
Nguyen An Khuong
Core Programming
Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.41
Program Verification
More Examples
Nguyen An Khuong
Practical Aspects of
we can prove: Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.41
Program Verification
More Examples
Nguyen An Khuong
Practical Aspects of
we can prove: Correctness Proofs
Correctness of the
• (|x + 1 = 2|) P (|x = 2|) Factorial Function
Homeworks
2f.41
Program Verification
More Examples
Nguyen An Khuong
Practical Aspects of
we can prove: Correctness Proofs
Correctness of the
• (|x + 1 = 2|) P (|x = 2|) Factorial Function
Homeworks
2f.41
Program Verification
Rules for Partial Correctness (continued)
Nguyen An Khuong
Core Programming
[If-statement] Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.42
Program Verification
Rules for Partial Correctness (continued)
Nguyen An Khuong
Core Programming
[If-statement] Language
Practical Aspects of
Correctness Proofs
Correctness of the
(|ψ ∧ B|) C (|ψ|) Factorial Function
2f.42
Program Verification
Rules for Partial Correctness (continued)
Nguyen An Khuong
Contents
Core Programming
Language
Correctness of the
Factorial Function
Homeworks
2f.43
Program Verification
Proof Tableaux
Nguyen An Khuong
Core Programming
something Language
Practical Aspects of
Correctness Proofs
As a result, all proofs can be written as a tree. Correctness of the
Factorial Function
Homeworks
These trees tend to be very wide when written out on paper. Thus
we are using a linear format, called proof tableaux.
2f.44
Program Verification
Interleave Formulas with Code
Nguyen An Khuong
Homeworks
Cn ;
(|φn |) justification
2f.45
Program Verification
Working Backwards
Nguyen An Khuong
Overall goal
Find a proof that at the end of executing a program P , some Contents
condition ψ holds. Core Programming
Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.46
Program Verification
Working Backwards
Nguyen An Khuong
Overall goal
Find a proof that at the end of executing a program P , some Contents
condition ψ holds. Core Programming
Language
(|ψ 0 |) Cn (|ψ|)
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.46
Program Verification
Working Backwards
Nguyen An Khuong
Overall goal
Find a proof that at the end of executing a program P , some Contents
condition ψ holds. Core Programming
Language
(|ψ 0 |) Cn (|ψ|)
Correctness Proofs
Correctness of the
Factorial Function
2f.46
Program Verification
Example
Nguyen An Khuong
Contents
Core Programming
(|y < 3|) Language
Correctness of the
Factorial Function
Homeworks
2f.47
Program Verification
Another Example
Nguyen An Khuong
Core Programming
Language
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.48
Program Verification
Another Example
Nguyen An Khuong
z = z + y; Practical Aspects of
Correctness Proofs
(|z = x + y|) Assignment
Correctness of the
u = z; Factorial Function
Homeworks
2f.48
Program Verification
An Alternative Rule for If
Nguyen An Khuong
We have:
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
(|φ1 |) C1 (|ψ|) (|φ2 |) C2 (|ψ|) Proof Calculus for
Total Correctness
[If-stmtHomeworks
2]
(|(B → φ1 ) ∧ (¬B → φ2 )|) if B { C1 } else { C2 } (|ψ|)
2f.49
Program Verification
Example
Nguyen An Khuong
Core Programming
a = x + 1; Language
Correctness of the
Can we prove (|>|) Succ (|y = x + 1|) ? Factorial Function
Homeworks
2f.50
Program Verification
Another Example
Nguyen An Khuong
..
.
if ( a - 1 == 0 ) { Contents
2f.51
Program Verification
Another Example
Nguyen An Khuong
(|>|)
(|(x + 1 − 1 = 0 → 1 = x + 1)∧
(¬(x + 1 − 1 = 0) → x + 1 = x + 1)|) Implied
Contents
a = x + 1; Core Programming
(|(a − 1 = 0 → 1 = x + 1)∧ Language
Homeworks
y = a;
(|y = x + 1|) Assignment
2f.52
Program Verification
Recall: Partial-while Rule
Nguyen An Khuong
Contents
Core Programming
Language
Correctness of the
Factorial Function
Homeworks
2f.53
Program Verification
Factorial Example
Nguyen An Khuong
We shall show that the following Core program Fac1 meets this Contents
Practical Aspects of
Thus, to show: Correctness Proofs
Homeworks
2f.54
Program Verification
Partial Correctness of Fac1
Nguyen An Khuong
..
.
(|y = z!|) Contents
while ( z != x ) { Core Programming
(|y = z! ∧ z 6= x|) Invariant Language
Homeworks
(|y = x!|) Implied
2f.55
Program Verification
Partial Correctness of Fac1
Nguyen An Khuong
(|>|)
(|(1 = 0!)|) Implied
Contents
y = 1;
Core Programming
(|y = 0!|) Assignment Language
2f.56
Program Verification
Nguyen An Khuong
Correctness of the
Factorial Function
6 Proof Calculus for Total Correctness Proof Calculus for
Total Correctness
Homeworks
7 Homeworks
2f.57
Program Verification
Ideas for Total Correctness
Nguyen An Khuong
Contents
• The only source of non-termination is the while command.
Core Programming
Language
• If we can show that the value of an integer expression
Hoare Triples; Partial
decreases in each iteration, but never becomes negative, we and Total Correctness
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.58
Program Verification
Ideas for Total Correctness
Nguyen An Khuong
Contents
• The only source of non-termination is the while command.
Core Programming
Language
• If we can show that the value of an integer expression
Hoare Triples; Partial
decreases in each iteration, but never becomes negative, we and Total Correctness
Correctness of the
Factorial Function
Homeworks
2f.58
Program Verification
Ideas for Total Correctness
Nguyen An Khuong
Contents
• The only source of non-termination is the while command.
Core Programming
Language
• If we can show that the value of an integer expression
Hoare Triples; Partial
decreases in each iteration, but never becomes negative, we and Total Correctness
Correctness of the
Factorial Function
Homeworks
2f.58
Program Verification
Ideas for Total Correctness
Nguyen An Khuong
Contents
• The only source of non-termination is the while command.
Core Programming
Language
• If we can show that the value of an integer expression
Hoare Triples; Partial
decreases in each iteration, but never becomes negative, we and Total Correctness
Homeworks
2f.58
Program Verification
Rules for Partial Correctness (continued)
Nguyen An Khuong
Practical Aspects of
Correctness Proofs
2f.59
Program Verification
Factorial Example (Again!)
Nguyen An Khuong
Contents
Core Programming
Language
y = 1;
Hoare Triples; Partial
z = 0; and Total Correctness
while ( z != x ) { z = z + 1; y = y * z ; } Proof Calculus for
Partial Correctness
What could be a good variant E? Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.60
Program Verification
Factorial Example (Again!)
Nguyen An Khuong
Contents
Core Programming
y = 1; Language
z = 0;
Hoare Triples; Partial
while ( z != x ) { z = z + 1; y = y * z ; } and Total Correctness
Practical Aspects of
Correctness Proofs
E must strictly decrease in the loop, but not become negative. Correctness of the
Factorial Function
Homeworks
2f.61
Program Verification
Factorial Example (Again!)
Nguyen An Khuong
y = 1; Contents
z = 0; Core Programming
while ( z != x ) { z = z + 1; y = y * z ; } Language
Homeworks
2f.62
Program Verification
Total Correctness of Fac1
Nguyen An Khuong
..
.
(|y = z! ∧ 0 ≤ x − z|) Contents
while ( z != x ) { Core Programming
(|y = z! ∧ z 6= x ∧ 0 ≤ x − z = E0 |) Invariant Language
Homeworks
(|y = x!|) Implied
2f.63
Program Verification
Total Correctness of Fac1
Nguyen An Khuong
(|x ≤ 0|)
(|(1 = 0! ∧ 0 ≤ x − 0|) Implied
Contents
y = 1;
Core Programming
(|y = 0! ∧ 0 ≤ x − 0|) Assignment Language
2f.64
Program Verification
HW
Nguyen An Khuong
Contents
Core Programming
Language
Do as much as possible (at least ALL designated) problems given Hoare Triples; Partial
and Total Correctness
in Section 4.6 in [2] Proof Calculus for
Partial Correctness
Practical Aspects of
Correctness Proofs
Correctness of the
Factorial Function
Homeworks
2f.65