2.4 Proving While Programs Correct Flashcards
What does correctness mean?
Syntactic well-formedness Properly typed Well-behaved Semantic correctness Termination
How are intended or desired behaviours captured
Specification
Two Predicates: Precondition and post-condition.
How do we test correctness?
Specification(Pre, Post) ⊆ Actual(P)
How do we prove partial correctness?
Take the loop invariant. Show that it is consistent.
Consider what happens when the loop terminates
Add to loop invariant (Pre-condition). Is this the post-condition? Yes- Partially correct
What is a loop invariant?
Something that is always true of states before and after the while loop
How do we prove Total correctness?
We must show each while-loop will always terminate. To do this we find a loop variant. Combine with partial correctness proof
What is a loop variant?
A quantity that strictly decreases on every iteration of the while loop and is bounded so that when it reaches a certain value, the loop terminates
What is a hoare triple?
Two predicates and a statement.
partial correctness: {P}S{Q}
What is an auxiliary variable?
keeps a record of the initial values.
{false} S {Q} is always ….
true
P S {false} is always false if … and always true if …..
always false if S terminates
always true if S does no terminate
Proof of Hoare triple (no loops)?
{P} S {Q}
- Apply the rules to decompose into smaller triples to get a sequence of assignments
- If program contains if statements, we get multiple triples
- Apply assignment rule backwards to get new precondition Q’
- Try to prove implications P → Q’
Proof of Hoare triple (loops)?
- Find loop invariant
- Show it was the invariant for the loop via rule for while
- Connect loop invariant to pre and post conditions using rule of consequence
Describe the rule of consequence
if P → P’
{P’} S {Q} = {P} S {Q}
if Q’ → Q
{P} S {Q’} = {P} S {Q}
How do we find a loop invariant?
Start with something we already have. Start with precondition and weaken to get something that works take {A} while b do S {A ∧¬B[[b]]} find loop invariant R such that: A → R (R ∧ ¬b) → (A ∧ ¬b) {R ∧ b} S {R}