2.4 Proving While Programs Correct Flashcards

1
Q

What does correctness mean?

A
Syntactic well-formedness
Properly typed
Well-behaved
Semantic correctness
Termination
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are intended or desired behaviours captured

A

Specification

Two Predicates: Precondition and post-condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do we test correctness?

A

Specification(Pre, Post) ⊆ Actual(P)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do we prove partial correctness?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a loop invariant?

A

Something that is always true of states before and after the while loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do we prove Total correctness?

A

We must show each while-loop will always terminate. To do this we find a loop variant. Combine with partial correctness proof

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a loop variant?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a hoare triple?

A

Two predicates and a statement.

partial correctness: {P}S{Q}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an auxiliary variable?

A

keeps a record of the initial values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

{false} S {Q} is always ….

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

P S {false} is always false if … and always true if …..

A

always false if S terminates

always true if S does no terminate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Proof of Hoare triple (no loops)?

A

{P} S {Q}

  1. Apply the rules to decompose into smaller triples to get a sequence of assignments
  2. If program contains if statements, we get multiple triples
  3. Apply assignment rule backwards to get new precondition Q’
  4. Try to prove implications P → Q’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Proof of Hoare triple (loops)?

A
  1. Find loop invariant
  2. Show it was the invariant for the loop via rule for while
  3. Connect loop invariant to pre and post conditions using rule of consequence
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Describe the rule of consequence

A

if P → P’
{P’} S {Q} = {P} S {Q}
if Q’ → Q
{P} S {Q’} = {P} S {Q}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do we find a loop invariant?

A
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}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the loop variant?
x counts up to 100 and stops
What is the loop invariant?
E1 gets smaller and E2 gets bigger

A

-x

E1 - E2

17
Q

Give the Hoare triple for total correctness

A

[P] S [Q]