CSSE3100/7100 Reasoning about Programs Assignment 1
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Assignment 1
CSSE3100/7100 Reasoning about Programs
Due: 4pm on 31 March, 2023
The aim of this assignment is to consolidate your understanding of the course's material on weakest precondition reasoning. It is worth 15% of your final mark for the course.
Instructions: Upload a pdf document with your solution to Gradescope by the due date and time. You may use the Word template A1-template.docx as your starting point, or copy the format of the template in A1-template.pdf to use with an editor other than Word. For Question 2, add your wp reasoning in the spaces between the lines of code.
1. Prove the following justifying each line of your proof with a law from Appendix A of Programmingfrom Specifications.
a) A ==> A && B = A ==> B (1 mark)
b) (A ==> B) && (!A ==> B) = B (4 marks)
You do not need to state the laws for commutativity of && and || (A.2 and A.3) if you use them, but all other laws used must be stated.
2. The method DivMod below calculates the quotient and modulus of its inputs a and b using only addition, subtraction and multiplication and division by 2. Use weakest precondition reasoning to show that it is partially correct. (10 marks)
function Power(k: nat): nat {
if k == 0 then 1 else 2*Power(k - 1)
}
method DivMod(a: int, b: int) returns (q: int, r: int)
requires a >= 0 && b > 0
ensures a == q*b + r && 0 <= r < b
{
var c := b;
var k := 0;
while c <= a
invariant k >= 0 && c == Power(k)*b && a >= 0
{
c, k := 2*c, k + 1;
}
q, r := 0, a;
while c != b
invariant k >= 0 && c == Power(k)*b && a == q*c + r && 0 <= r < c
{
q, c, k := q*2, c/2, k - 1;
if r >= c {
q, r := q + 1, r - c;
}
}
}
Hints: Be careful with integer division: dividing 2*c by 2 is c, but multiplying c/2 by 2 is only equal to c when c is even.
The laws proved in Question 1 will be helpful in simplifying your predicates. If you use them refer to them as (1a) and (1b). Also, don't forget you can strengthen your predicates whenever that will help. For example, if you can't apply the laws from Question 1 where you think they would be useful, try strengthening your predicate.
Don't forget a <= b < c is equivalent to a <= b && b < c!
Marking
You will get marks for
• the application of the appropriate weakest precondition rules for each line of code (Question 2),
• stating why the entire method is correct (Question 2), and
• correct and, where necessary, justified predicate transformations (Questions 1 and 2).
You must justify all predicate transformations which are not due to either simple arithmetic or commutativity of && or ||. Justification must be either
• a law from Appendix A of Programmingfrom Specification (Questions 1 and 2),
• a law proved in Question 1 using "(1a)" or "(1b)" (Question 2),
• a brief explanation for any non-obvious arithmetic step (Question 2), or
• "strengthening" when you have strengthened a predicate (Question 2). Strengthening steps must be obvious, e.g., adding one or more conjuncts, or restricting the range of values of one or more variable.
School Policy on Student Misconduct
This assignment is to be completed individually. You are required to read and understand the School Statement on Misconduct, available on the School's website at:
http://www.itee.uq.edu.au/itee-student-misconduct-including-plagiarism
2023-03-31