ASSIGNMENT 10

Transcription

ASSIGNMENT 10
CS 245, Winter 2015
A. Lubiw
ASSIGNMENT 10
Due Wednesday, April 1, 4:30pm, in the drop box near MC 4065.
Surname:
Personal name:
ID#:
Which tutorial do you attend:
F 10:30am
F 12:30pm
F 1:30pm
Assignment guidelines:
• Attach this sheet as a cover page, fill in your name and ID# above, and include
your name and ID# on every page of your assignment.
• Staple your pages together.
• Please type your assignment or write neatly in ink.
• The work you hand in must be your own. Acknowledge any sources you have
used. You may discuss the assignment questions verbally with others, but you
should come away from these discussions with no written or electronic records.
Write your solutions in your own words, from your own head.
Mark
Marker
Q1:
Q2:
total:
1
CS 245, Winter 2015
A. Lubiw
ASSIGNMENT 10
In this assignment you will prove partial correctness of some code fragments. To do that,
annotate the program with the proof rules for partial correctness. Include a justification beside
each condition. Add proofs for any “implied” rules afterwards—in these proofs you may freely use
algebra, but you should use natural deduction when the premise and/or conclusion involve logical
connectives.
1. [8 marks] Program Verification Part I
(a) [3 marks] Prove partial correctness for the following Hoare triple.
{
x
y
{
x=y
= x +
= y +
x=y
}
1;
1;
}
(b) [5 marks] Prove partial correctness for the following Hoare triple.
{ true }
if (x < y) {
min = x;
} else {
min = y;
}
{ (x < y ∧ min = x) ∨ (x ≥ y ∧ min = y)
}
2. [12 marks] Program Verification Part II
(a) [7 marks] This question is about the downward factorial example.
{ x ≥ 0 ∧ x = x0 }
y = 1;
while (x != 0) {
y = y * x;
x = x - 1;
}
{ y = x0 ! }
i. [5 marks] Prove partial correctness. Hint: Try the invariant y · x! = x0 ! and note
that 0! = 1.
ii. [2 marks] What variant could you use to prove total correctness? Argue (just in
English) why your variant is always non-negative, and why it decreases each time
through the while-loop.
2
P
(b) [5 marks] Prove partial correctness of the following code to compute ni=0 2i . Clearly
state your choice of invariant before your proof (you do not need to show any work
involved in your choice).
{ n≥1 }
x = 1;
y = 0;
for i = 0 to n {
y = y + x;
x = 2 * x;
}
{ y = 2n+1 − 1 }
3