Homework #1 (Part I): CSE503, Winter 2002
- (20 points total) Determine the weakest logical property Z that will make each
of the following Hoare triples hold true
- {x + y > 10} x := y; y := 3
{Z}
- {Z} x:=3·x {x>0}
- {Z}if a > b then m := a else m := b
{m=max(a,b)}
- {Z} x := 2*x*y {x+y < 64}
- (10 points) Write pre- and postconditions for a specification intended to start with
the integers a and b,
ensuring that the maximum of these two integers is left in variable m
when the program terminates.
- (20 points total) Prove the following program is correct, including termination:
{n > 0}
s := 0;
i := 0;
while i<n do
s := s + i;
i = i + 1
end;
{s = n*(n-1)/2}
- (10 points) Is the following Hoare triple weakly-correct, strongly-correct, or
incorrect? Justify your answer.
{true} while 3 < 4 do x := x + 1
{3 = 4}
- (40 points total) Consider the following abstract data type:
type
Set {
private elements = {};
public void insert (int x) { post == if isMember(x)
then
elements' = elements
else elements' = elements union {x} };
public void delete(int x) { post == if isMember(x)
then
elements' = elements \ {x}
else elements' = elements }
public int size() { post == length(elements) }
}
- (5 points) Define an appropriate abstraction invariant for Set?
- (5 points) Sketch an implementation for Set that uses a (Java-like) vector to
represent the set.
- (5 points) Define an appropriate concrete (implementation) invariant for Set?
- (10 points) Define an appropriate abstraction function that relates the
implementation and the abstraction for Set.
- (15 points) Sketch a proof of correctness of the Set abstract data type as
implemented by your implementation in 5B.