You are on page 1of 3

CS1112

One and a half hours

UNIVERSITY OF MANCHESTER

Reasoning about Programs

Friday 30th May 2003, 14:00 – 16:00

Answer any TWO Questions

The use of electronic calculators is not permitted.

[PTO]
2 CS1112

1. a) In a first-order language, what are the following:

i) an atomic formula;
ii) a logical connective;
iii) a quantifier;
iv) a well-formed formula;
v) a sentence.

Illustrate your answer with examples. (10 marks)

b) Assume that x and y are real numbers, and let P(x,y) be the predicate x + y = 0.

For each of the following:

i) x (y (P(x,y)))
ii) y (x (P(x,y)))
iii) x (y (P(x,y)))

translate it into English and explain what it means. Is it true? Explain why or
why not. (6 marks)

c) Suppose the function abs(x) gives the absolute value of a real number x.
Translate the following sentences:

i) “If the sum of two real numbers is zero, then they have the same
absolute value.”

ii) “It is only possible for the sum of two real numbers to be zero if they
have the same absolute value.”

into first-order logic sentences, using the predicate P(x,y) from (b), and explain if
they are true. (You should explain all the extra symbols that you may have
used.) (4 marks)

2. a) If a program P is specified by {Q} P {R}, where Q and R are predicates:

i) explain what the specification means;


ii) explain what it means to say that Q is a weakest pre-condition.

Illustrate your answer with suitable examples. (10 marks)

b) For each of the following, is the given program correct with respect to its
specification? Explain why or why not.

i) {x = 5} y := x + 1; x := x - 1; {x = y}
ii) {true} if x > 0 then x := -x else x := x - 1; {x < 0}
iii) {n = 1, x = 0} while n < 2 do begin x := x + n; n := n + 1 end; {x = n}
iv) {x < 2} x := x + 8 {x > 10}
v) {x = y} if x > y then z := x else z := y; {z = x} (10 marks)
3 CS1112

3. a) In SPARK, what are verification conditions? How are they generated? (6 marks)

b) Consider the following SPARK procedure:

procedure Max(X,Y : in Integer; Z : out Integer);


--# derives Z from X, Y;
--# post (X > Y -> Z = X) and (X < Y -> Z = Y);
is
begin
if X > Y then
Z := X;
else
Z := Y;
end if;
end max

i) Explain the specification of this procedure. (3 marks)


ii) Work out the verification conditions for this procedure. (8 marks)
iii) Hence explain if the procedure is correct. (3 marks)

You might also like