warm-up problemcbruni/cs245resources/lectures/2018_spri… · warm-up problem 1. what is the...

37
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment: [/] (x = ) (assignment) Why is this the correct rule and not the following rule? (x = ) [/] (assignment) 1/25

Upload: others

Post on 20-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Warm-Up Problem

1. What is the definition of a Hoare triple satisfying partial correctness?2. Recall the rule for assignment:

⦇ 𝑄[𝐸/𝑥] ⦈ (x = 𝐸) ⦇ 𝑄 ⦈ (assignment)

Why is this the correct rule and not the following rule?

⦇ 𝑄 ⦈ (x = 𝐸) ⦇ 𝑄[𝐸/𝑥] ⦈ (assignment)

Solution: With the second rule, triples like⦇ 𝑥 = 3 ⦈ x=2 ⦇ 2 = 3 ⦈ are provable which would allow us toprove false statements.

1/25

Page 2: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Warm-Up Problem

1. What is the definition of a Hoare triple satisfying partial correctness?2. Recall the rule for assignment:

⦇ 𝑄[𝐸/𝑥] ⦈ (x = 𝐸) ⦇ 𝑄 ⦈ (assignment)

Why is this the correct rule and not the following rule?

⦇ 𝑄 ⦈ (x = 𝐸) ⦇ 𝑄[𝐸/𝑥] ⦈ (assignment)

Solution: With the second rule, triples like⦇ 𝑥 = 3 ⦈ x=2 ⦇ 2 = 3 ⦈ are provable which would allow us toprove false statements.

1/25

Page 3: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Program VerificationConditionals

Carmen BruniLecture 19

Based on slides by Jonathan Buss, Lila Kari, Anna Lubiw and SteveWolfman with thanks to B. Bonakdarpour, A. Gao, D. Maftuleac, C.

Roberts, R. Trefler, and P. Van Beek

2/25

Page 4: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Last Time

• Give reasons for performing formal verification vs testing.• Define a Hoare Triple.• Define Partial Correctness.• Prove that a Hoare triple is satisfied under partial correctness for a

program containing assignment statements.

3/25

Page 5: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Learning Goals

• Understand and use implied statements as needed.• Prove that a Hoare triple is satisfied under partial correctness for a

program containing assignment and conditional statements.

4/25

Page 6: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Question

How do we show that the following Hoare triple is satisfied under partialcorrectness?⦇ (𝑦 = 6) ⦈x = y + 1;⦇ (𝑥 = 7) ⦈

Assignment here only gives us that⦇ ((𝑦 + 1) = 7) ⦈x = y + 1;⦇ (𝑥 = 7) ⦈

However, we know that ((𝑦 + 1) = 7) implies that (𝑦 = 6). How do wewrite this?

5/25

Page 7: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Question

How do we show that the following Hoare triple is satisfied under partialcorrectness?⦇ (𝑦 = 6) ⦈x = y + 1;⦇ (𝑥 = 7) ⦈

Assignment here only gives us that⦇ ((𝑦 + 1) = 7) ⦈x = y + 1;⦇ (𝑥 = 7) ⦈

However, we know that ((𝑦 + 1) = 7) implies that (𝑦 = 6). How do wewrite this?

5/25

Page 8: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Inference Rules with Implications

Rule of “Precondition strengthening”:

(𝑃 → 𝑃 ′) ⦇ 𝑃 ′ ⦈ 𝐶 ⦇ 𝑄 ⦈⦇ 𝑃 ⦈ 𝐶 ⦇ 𝑄 ⦈ (implied)

Rule of “Postcondition weakening”:

⦇ 𝑃 ⦈ 𝐶 ⦇ 𝑄′ ⦈ (𝑄′ → 𝑄)⦇ 𝑃 ⦈ 𝐶 ⦇ 𝑄 ⦈ (implied)

Example of use: ⦇ (𝑦 = 6) ⦈⦇ ((𝑦 + 1) = 7) ⦈ impliedx = y + 1;⦇ (𝑥 = 7) ⦈ assignment

6/25

Page 9: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

More Questions

What if my code has multiple lines? Why are we allowed to correctlycompose annotations?

7/25

Page 10: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Inference Rule for Sequences of Instructions

⦇ 𝑃 ⦈ 𝐶1 ⦇ 𝑄 ⦈, ⦇ 𝑄 ⦈ 𝐶2 ⦇ 𝑅 ⦈⦇ 𝑃 ⦈ 𝐶1; 𝐶2 ⦇ 𝑅 ⦈ (composition)

In order to prove ⦇ 𝑃 ⦈ 𝐶1; 𝐶2 ⦇ 𝑅 ⦈, we need to find a midcondition 𝑄for which we can prove ⦇ 𝑃 ⦈ 𝐶1 ⦇ 𝑄 ⦈ and ⦇ 𝑄 ⦈ 𝐶2 ⦇ 𝑅 ⦈.

(In our examples, the midcondition will usually be determined by a rule, such asassignment. But in general, a midcondition might be very difficult to determine.)

8/25

Page 11: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Proof Format: Annotated Programs

Interleave program statements with assertions, each justified by aninference rule.

The composition rule is implicit.

Assertions should hold true whenever the program reaches that point in itsexecution.

9/25

Page 12: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Proof Format: Annotated Programs

If implied inference rule is used, we must supply a proof of the implication.

• We’ll do these proofs after annotating the program.

Each assertion should be an instance of an inference rule.Normally,

• Don’t simplify the assertions in the annotated program.• Do the simplification while proving the implied conditions.

10/25

Page 13: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example: Composition of Assignments

To show: the following is satisfied under partial correctness.

We work bottom-up for assignments…

⦇ ((𝑥 = 𝑥0) ∧ (𝑦 = 𝑦0)) ⦈

⦇ ((𝑦 = 𝑦0) ∧ (𝑥 = 𝑥0)) ⦈

t = x ;

⦇ ((𝑦 = 𝑦0) ∧ (𝑡 = 𝑥0)) ⦈

x = y ;

⦇ ((𝑥 = 𝑦0) ∧ (𝑡 = 𝑥0)) ⦈

y = t ;⦇ ((𝑥 = 𝑦0) ∧ (𝑦 = 𝑥0)) ⦈

assignment

Finally, show ⦇ ((𝑥 = 𝑥0) ∧ (𝑦 = 𝑦0)) ⦈ implies ⦇ ((𝑦 = 𝑦0) ∧ (𝑥 = 𝑥0)) ⦈.

11/25

Page 14: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example: Composition of Assignments

To show: the following is satisfied under partial correctness.

We work bottom-up for assignments…

⦇ ((𝑥 = 𝑥0) ∧ (𝑦 = 𝑦0)) ⦈

⦇ ((𝑦 = 𝑦0) ∧ (𝑥 = 𝑥0)) ⦈

t = x ;

⦇ ((𝑦 = 𝑦0) ∧ (𝑡 = 𝑥0)) ⦈

x = y ;⦇ ((𝑥 = 𝑦0) ∧ (𝑡 = 𝑥0)) ⦈ 𝑃2 is ⦇ 𝑃 [𝑡/𝑦] ⦈y = t ;⦇ ((𝑥 = 𝑦0) ∧ (𝑦 = 𝑥0)) ⦈ assignment ⦇ 𝑃 ⦈

Finally, show ⦇ ((𝑥 = 𝑥0) ∧ (𝑦 = 𝑦0)) ⦈ implies ⦇ ((𝑦 = 𝑦0) ∧ (𝑥 = 𝑥0)) ⦈.

11/25

Page 15: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example: Composition of Assignments

To show: the following is satisfied under partial correctness.

We work bottom-up for assignments…

⦇ ((𝑥 = 𝑥0) ∧ (𝑦 = 𝑦0)) ⦈

⦇ ((𝑦 = 𝑦0) ∧ (𝑥 = 𝑥0)) ⦈

t = x ;⦇ ((𝑦 = 𝑦0) ∧ (𝑡 = 𝑥0)) ⦈ 𝑃3 is ⦇ 𝑃2[𝑦/𝑥] ⦈x = y ;⦇ ((𝑥 = 𝑦0) ∧ (𝑡 = 𝑥0)) ⦈ assignmenty = t ;⦇ ((𝑥 = 𝑦0) ∧ (𝑦 = 𝑥0)) ⦈ assignment

Finally, show ⦇ ((𝑥 = 𝑥0) ∧ (𝑦 = 𝑦0)) ⦈ implies ⦇ ((𝑦 = 𝑦0) ∧ (𝑥 = 𝑥0)) ⦈.

11/25

Page 16: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example: Composition of Assignments

To show: the following is satisfied under partial correctness.

We work bottom-up for assignments…

⦇ ((𝑥 = 𝑥0) ∧ (𝑦 = 𝑦0)) ⦈⦇ ((𝑦 = 𝑦0) ∧ (𝑥 = 𝑥0)) ⦈ ⦇ 𝑃3[𝑥/𝑡] ⦈t = x ;⦇ ((𝑦 = 𝑦0) ∧ (𝑡 = 𝑥0)) ⦈ assignmentx = y ;⦇ ((𝑥 = 𝑦0) ∧ (𝑡 = 𝑥0)) ⦈ assignmenty = t ;⦇ ((𝑥 = 𝑦0) ∧ (𝑦 = 𝑥0)) ⦈ assignment

Finally, show ⦇ ((𝑥 = 𝑥0) ∧ (𝑦 = 𝑦0)) ⦈ implies ⦇ ((𝑦 = 𝑦0) ∧ (𝑥 = 𝑥0)) ⦈.

11/25

Page 17: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example: Composition of Assignments

To show: the following is satisfied under partial correctness.

We work bottom-up for assignments…

⦇ ((𝑥 = 𝑥0) ∧ (𝑦 = 𝑦0)) ⦈⦇ ((𝑦 = 𝑦0) ∧ (𝑥 = 𝑥0)) ⦈ implied [proof required]t = x ;⦇ ((𝑦 = 𝑦0) ∧ (𝑡 = 𝑥0)) ⦈ assignmentx = y ;⦇ ((𝑥 = 𝑦0) ∧ (𝑡 = 𝑥0)) ⦈ assignmenty = t ;⦇ ((𝑥 = 𝑦0) ∧ (𝑦 = 𝑥0)) ⦈ assignment

Finally, show ⦇ ((𝑥 = 𝑥0) ∧ (𝑦 = 𝑦0)) ⦈ implies ⦇ ((𝑦 = 𝑦0) ∧ (𝑥 = 𝑥0)) ⦈.

11/25

Page 18: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example

Show that the following Hoare triple is satisfied under partial correctness.⦇ ((((2 ⋅ 𝑥) + (3 ⋅ 𝑦)) ≥ 7) ∧ ((𝑥 + (2 ⋅ 𝑦)) ≥ 0)) ⦈x = x + y;y = x + y;x = x + y;⦇ ((𝑥 ≥ 5) ∧ (𝑦 ≥ 0)) ⦈

Why is it that 𝑥 = 1 and 𝑦 = 2 satisfies the pre-condition but not the postcondition and yet the above Hoare triple is satisfied under partialcorrectness?

Answer: The code will actually mutate 𝑥 and 𝑦! The 𝑥 and 𝑦 at the endis not the same as the original.

12/25

Page 19: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example

Show that the following Hoare triple is satisfied under partial correctness.⦇ ((((2 ⋅ 𝑥) + (3 ⋅ 𝑦)) ≥ 7) ∧ ((𝑥 + (2 ⋅ 𝑦)) ≥ 0)) ⦈x = x + y;y = x + y;x = x + y;⦇ ((𝑥 ≥ 5) ∧ (𝑦 ≥ 0)) ⦈

Why is it that 𝑥 = 1 and 𝑦 = 2 satisfies the pre-condition but not the postcondition and yet the above Hoare triple is satisfied under partialcorrectness?

Answer: The code will actually mutate 𝑥 and 𝑦! The 𝑥 and 𝑦 at the endis not the same as the original.

12/25

Page 20: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example

Show that the following Hoare triple is satisfied under partial correctness.⦇ ((((2 ⋅ 𝑥) + (3 ⋅ 𝑦)) ≥ 7) ∧ ((𝑥 + (2 ⋅ 𝑦)) ≥ 0)) ⦈x = x + y;y = x + y;x = x + y;⦇ ((𝑥 ≥ 5) ∧ (𝑦 ≥ 0)) ⦈

Why is it that 𝑥 = 1 and 𝑦 = 2 satisfies the pre-condition but not the postcondition and yet the above Hoare triple is satisfied under partialcorrectness?

Answer: The code will actually mutate 𝑥 and 𝑦! The 𝑥 and 𝑦 at the endis not the same as the original.

12/25

Page 21: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Programs with Conditional Statements

Conditionals 13/25

Page 22: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Deduction Rules for Conditionals

if-then-else:

⦇ (𝑃 ∧ 𝐵) ⦈ C1 ⦇ 𝑄 ⦈ ⦇ (𝑃 ∧ (¬𝐵)) ⦈ C2 ⦇ 𝑄 ⦈⦇ 𝑃 ⦈ if (B) C1 else C2 ⦇ 𝑄 ⦈ (if-then-else)

if-then (without else):

⦇ (𝑃 ∧ 𝐵) ⦈ C ⦇ 𝑄 ⦈ ((𝑃 ∧ (¬𝐵)) → 𝑄)⦇ 𝑃 ⦈ if (B) C ⦇ 𝑄 ⦈ (if-then)

Conditionals 14/25

Page 23: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Template for Conditionals With else

Annotated program template for if-then-else:

⦇ 𝑃 ⦈if ( 𝐵 ) {

⦇ (𝑃 ∧ 𝐵) ⦈ if-then-else𝐶1⦇ 𝑄 ⦈ (justify depending on 𝐶1—a “subproof”)

} else {⦇ (𝑃 ∧ (¬𝐵)) ⦈ if-then-else𝐶2⦇ 𝑄 ⦈ (justify depending on 𝐶2—a “subproof”)

}⦇ 𝑄 ⦈ if-then-else [justifies this 𝑄, given previous two]

Conditionals 15/25

Page 24: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Template for Conditionals Without else

Annotated program template for if-then:

⦇ 𝑃 ⦈if ( 𝐵 ) {

⦇ (𝑃 ∧ 𝐵) ⦈ if-then𝐶⦇ 𝑄 ⦈ [add justification based on 𝐶]

}⦇ 𝑄 ⦈ if-then

Implied: Proof of ((𝑃 ∧ (¬𝐵)) → 𝑄)

Conditionals 16/25

Page 25: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example: Conditional Code

Example: Prove the following is satisfied under partial correctness.

⦇ true ⦈ ⦇ 𝑃 ⦈

if ( max < x ) { if ( 𝐵 ) {max = x ; 𝐶

} }

⦇ (max ≥ 𝑥) ⦈ ⦇ 𝑄 ⦈

First, let’s recall our proof method….

Conditionals 17/25

Page 26: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

The Steps of Creating a Proof

Three steps in doing a proof of partial correctness:

1. First annotate using the appropriate inference rules.2. Then ”back up” in the proof: add an assertion before each assignment

statement, based on the assertion following the assignment.3. Finally prove any “implieds”:

• Annotations from (1) above containing implications• Adjacent assertions created in step (2).

Proofs here are written in Math 135 style. They can use PredicateLogic, basic arithmetic, or any other appropriate reasoning.

Conditionals 18/25

Page 27: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Doing the Steps

1. Add annotations for the if-then statement.

2. “Push up” for the assignments.3. Identify “implieds” to be proven.

⦇ true ⦈if ( max < x ) {

⦇ (true ∧ (max < 𝑥)) ⦈ if-then

⦇ (𝑥 ≥ 𝑥) ⦈

max = x ;⦇ (max ≥ 𝑥) ⦈ ⟵ to be shown

}

⦇ (max ≥ 𝑥) ⦈ if-thenImplied: (( �true ∧ ¬((max < 𝑥))) � → (max ≥ 𝑥))

Conditionals 19/25

Page 28: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Doing the Steps

1. Add annotations for the if-then statement.2. “Push up” for the assignments.

3. Identify “implieds” to be proven.

⦇ true ⦈if ( max < x ) {

⦇ (true ∧ (max < 𝑥)) ⦈ if-then⦇ (𝑥 ≥ 𝑥) ⦈max = x ;⦇ (max ≥ 𝑥) ⦈ assignment

}

⦇ (max ≥ 𝑥) ⦈ if-thenImplied: (( �true ∧ ¬((max < 𝑥))) � → (max ≥ 𝑥))

Conditionals 19/25

Page 29: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Doing the Steps

1. Add annotations for the if-then statement.2. “Push up” for the assignments.3. Identify “implieds” to be proven.

⦇ true ⦈if ( max < x ) {

⦇ (true ∧ (max < 𝑥)) ⦈ if-then⦇ (𝑥 ≥ 𝑥) ⦈ Implied (a)max = x ;⦇ (max ≥ 𝑥) ⦈ assignment

}

⦇ (max ≥ 𝑥) ⦈ if-thenImplied (b): (( �true ∧ ¬((max < 𝑥))) � → (max ≥ 𝑥))

Conditionals 19/25

Page 30: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Proving “Implied” Conditions

The auxiliary “implied” proofs can be done in Math 135 style (andassuming the necessary arithmetic properties). We will write theminformally, but clearly.

Proof of Implied (a):

∅ ⊢ ((true ∧ (max < 𝑥)) → (𝑥 ≥ 𝑥)).

• The statement (𝑥 ≥ 𝑥) is a tautology since ≥ is reflexive and so therequired implication holds.

Conditionals 20/25

Page 31: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Implied (b)

Proof of Implied (b): Show ∅ ⊢ ((𝑃 ∧ (¬𝐵)) → 𝑄), which in this case is

∅ ⊢ ((true ∧ (¬(max < 𝑥))) → (max ≥ 𝑥)).

• The hypothesis, (true ∧ (¬(max < 𝑥))) can be simplified to(¬(max < 𝑥)).

• Then by properties of ¬, < and ≥, the conclusion, (max ≥ 𝑥), follows.

Conditionals 21/25

Page 32: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example 2 for Conditionals

Prove the following is satisfied under partial correctness.

⦇ true ⦈if ( x > y ) {

max = x;} else {

max = y;}⦇ (((𝑥 > 𝑦) ∧ (max = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (max = 𝑦))) ⦈

Conditionals 22/25

Page 33: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example 2: Annotated Code

⦇ true ⦈if ( x > y ) {

⦇ (𝑥 > 𝑦) ⦈ if-then-else

⦇ (((𝑥 > 𝑦) ∧ (𝑥 = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (𝑥 = 𝑦))) ⦈

implied (a)

max = x ;⦇ (((𝑥 > 𝑦) ∧ (max = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (max = 𝑦))) ⦈

assignment

} else {⦇ (¬(𝑥 > 𝑦)) ⦈ if-then-else

⦇ (((𝑥 > 𝑦) ∧ (𝑦 = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (𝑦 = 𝑦))) ⦈

implied (b)

max = y ;⦇ (((𝑥 > 𝑦) ∧ (max = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (max = 𝑦))) ⦈

assignment

}⦇ (((𝑥 > 𝑦) ∧ (max = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (max = 𝑦))) ⦈ if-then-else

Conditionals 23/25

Page 34: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example 2: Annotated Code

⦇ true ⦈if ( x > y ) {

⦇ (𝑥 > 𝑦) ⦈ if-then-else⦇ (((𝑥 > 𝑦) ∧ (𝑥 = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (𝑥 = 𝑦))) ⦈

implied (a)

max = x ;⦇ (((𝑥 > 𝑦) ∧ (max = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (max = 𝑦))) ⦈ assignment

} else {⦇ (¬(𝑥 > 𝑦)) ⦈ if-then-else⦇ (((𝑥 > 𝑦) ∧ (𝑦 = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (𝑦 = 𝑦))) ⦈

implied (b)

max = y ;⦇ (((𝑥 > 𝑦) ∧ (max = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (max = 𝑦))) ⦈ assignment

}⦇ (((𝑥 > 𝑦) ∧ (max = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (max = 𝑦))) ⦈ if-then-else

Conditionals 23/25

Page 35: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example 2: Annotated Code

⦇ true ⦈if ( x > y ) {

⦇ (𝑥 > 𝑦) ⦈ if-then-else⦇ (((𝑥 > 𝑦) ∧ (𝑥 = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (𝑥 = 𝑦))) ⦈ implied (a)max = x ;⦇ (((𝑥 > 𝑦) ∧ (max = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (max = 𝑦))) ⦈ assignment

} else {⦇ (¬(𝑥 > 𝑦)) ⦈ if-then-else⦇ (((𝑥 > 𝑦) ∧ (𝑦 = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (𝑦 = 𝑦))) ⦈ implied (b)max = y ;⦇ (((𝑥 > 𝑦) ∧ (max = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (max = 𝑦))) ⦈ assignment

}⦇ (((𝑥 > 𝑦) ∧ (max = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (max = 𝑦))) ⦈ if-then-else

Conditionals 23/25

Page 36: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Example 2: Implied Conditions

(a) Prove ((𝑥 > 𝑦) → (((𝑥 > 𝑦) ∧ (𝑥 = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (𝑥 = 𝑦)))).

• From (𝑥 > 𝑦) and from the trivially true (𝑥 = 𝑥), it follows that((𝑥 > 𝑦) ∧ (𝑥 = 𝑥)).

• From this it follows that (((𝑥 > 𝑦) ∧ (𝑥 = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (𝑥 = 𝑦))).

(b) Prove ((𝑥 ≤ 𝑦) → (((𝑥 > 𝑦) ∧ (𝑦 = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (𝑦 = 𝑦)))).

• From (𝑥 ≤ 𝑦) and from the trivially true (𝑦 = 𝑦), it follows that((𝑥 > 𝑦) ∧ (𝑦 = 𝑥)).

• From this it follows that (((𝑥 > 𝑦) ∧ (𝑦 = 𝑥)) ∨ ((𝑥 ≤ 𝑦) ∧ (𝑦 = 𝑦))).

Conditionals 24/25

Page 37: Warm-Up Problemcbruni/CS245Resources/lectures/2018_Spri… · Warm-Up Problem 1. What is the definition of a Hoare triple satisfying partial correctness? 2. Recall the rule for assignment:

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

...

.

Try Some on Your Own!

Show that the following two Hoare triples are satisfied under partialcorrectness:

⦇ true ⦈x = y ;if (y > 1){

x = x - 1;} else {

x = x * x + 1;}⦇ (𝑥 > 0) ⦈

⦇ (𝑥 ≥ −7) ⦈w = x + 1 ;if (w >= 0){

w = (w+1)*(w+1)+ 1;}⦇ (¬(∃𝑢 ((𝑢 ⋅ 𝑢) = 𝑤))) ⦈

Is the left hand Hoare triple still satisfied under partial correctness if wereplace 𝑥 = 𝑦 with 𝑦 = 𝑥?

Conditionals 25/25