Contents:
You will need to follow a couple of steps to set up for the homework sequence this quarter. Complete these steps by the end of the week so the staff can help you work through any problems.
Directions:
Find the strongest postcondition of each code sequence by inserting the appropriate assertion in each blank. The first assertion in part (a) is supplied as an example.
{ x > 0 }
x = 10;
{ x = 10 }
y = 2 * x;
{_______________________________}
z = y + 4;
{_______________________________}
x = z / 2;
{_______________________________}
y = 0;
{_______________________________}
{ x > 0 } y = x; {_______________________________} y = y + 2; {_______________________________}
{ |x| > 10 } x = -x; {_______________________________} x = x / 2; {_______________________________} x = x + 1; {_______________________________}
{ y > 2x } y = y * 2; {_______________________________} x = x + 1; {_______________________________}
Find the weakest precondition of each code sequence by inserting the appropriate assertion in each blank.
{___________________________} x = x + 5; {___________________________} y = 2 * x; { y > 10 }
{___________________________} y = x + 6; {___________________________} z = x + y; { z ≤ 0 }
{___________________________} y = w - 10; {___________________________} x = 2 * x; { x > y }
{___________________________} t = 2 * s; {___________________________} r = w + 4; {___________________________} s = 2*s + w; { r > s ∧ s > t}
Find the weakest precondition for the following conditional statement using backward reasoning, inserting the appropriate assertion in each blank. Be sure to explicitly verify that the intermediate postconditions for the two cases imply the total postcondition, i.e. show that (Q1 ∨ Q2) ⇒ Q.
{___________________________} if (x >= 0) {___________________________} z = x; {___________________________} else {___________________________} z = x + 1; {___________________________} {z ≠ 0 }
Willy Wazoo says that the CSE 331 formula for wp(“If C, S1 else S2”, Q) is too hard to remember. He suggests this simpler definition:
wp(“If C, S1 else S2”, Q) = wp(S1, Q) ∨ wp(S2, Q)
Consider the following Hoare triple:
P: {___________________________} if (x == 0) { x = _______________; // S1 } else { x = _______________; // S2 } Q: {___________________________}
Willy accepts your counterexample, but he still believes he can simplify the formula. He proposes to try the equation:
wp(“If C, S1 else S2”, Q) = wp(S1, Q) ∧ wp(S2, Q)
Consider the following Hoare triple:
P: {___________________________} if (x == 0) { x = _______________; // S1 } else { x = _______________; // S2 } Q: {___________________________}
Indicate the weakest condition in each set.
State whether each Hoare triple is valid. If it is invalid, explain why and show how you would modify the precondition or postcondition to make it valid.
{ x < 0 } y = 2 * x; { y ≤ 0 }
{ x ≥ y } z = x - y; { z > 0 }
{ true } if (x % 2 == 0) y = x; else y = x + 1; { y is even }
{ x < 0 } if (x < 100) x = -1; else x = 1; { x < 0 }
For each block of code, fill in the intermediate assertions, then use them to state whether the precondition is sufficient to guarantee the postcondition. If the precondition is insufficient, explain why and indicate where the assertions don't match up.
Hint: for assignment statements, use backward reasoning to find the weakest precondition that guarantees the postcondition, then see if the given precondition is weaker than the weakest precondition. For if/else statements, you may find a combination of forward and backward reasoning most useful. Follow the rules given in class for what assertion to insert at each point.
{ x > 0 } y = x - 1; {___________________________} z = 2 * y; {___________________________} z = z + 1; {z > 1}
{ 2x ≥ w } y = w - 2; {___________________________} x = 2 * x; {___________________________} z = x - 2; { z ≥ y }
{ y > 0 } if (x == y) {___________________________} x = -1; {___________________________} else {___________________________} x = y - 1; {___________________________} { x < y }
Write a block of code that calculates the smallest even number greater than or equal to x and stores it in y. In other words, y will be assigned either x or x+1. Assume x and y have already been initialized, and annotate your code with assertions before and after each statement to prove that it is correct. At the end of the block, it should be true that y is even and that y = x or y = (x+1).
Please answer the following questions in a file named collaboration.txt in your hw1/answers/ directory.
The standard collaboration policy applies to this homework.
State whether or not you collaborated with other students. If you did collaborate with other students, state their names and a brief description of how you collaborated.
Please answer the following questions in a file named reflection.txt in your hw1/answers/ directory. Answer briefly, but in enough detail to help you improve your own practice via introspection and to enable the course staff to improve CSE 331 in the future.
Tell us how long you spent on this homework via this catalyst survey: https://catalyst.uw.edu/webq/survey/mernst/198071. (Only include productive time; discount time you worked in your noisy dorm lounge, and don't count time if you were periodically reading email or IMs.)
Problem 6b has been retracted; it didn't have an answer.
This section will list clarifications and answers to common questions about homeworks. We'll try to keep it as up-to-date as possible, so this should be the first place to look (after carefully rereading the homework handout and the specifications) when you have a problem.