You are on page 1of 7

Running head: Computational Theory

Computational Theory

Course

Professor's Name

University

City

Date

Question 5:
Computational Theory
2
Verify the validity of the following correctness statements by adding all the intermediate
assertions (that is, give the proof tableau). All variables are of type int. Clearly state any
mathematical facts and inference rules used
As) ASSERT (x > 4 && y == 7 ) then
X =X- Y;
y=y-X;
x =x+y;
ASSERT (x == 7 && y<10)

Answer
We will replace all the occurrence of x with x+y in the the next ASSERT

x= x-y;
y= y-x;
ASSERT (x+y == 7 && y <10)
x = x+y;
ASSERT (x == 7 && y <10)

Now, we will apply the same method as previous one, we replace all the occurrence of y with y-
x in the the next ASSERT

x= x-y;
ASSERT (x+y-x == 7 && y-x <10)
y= y-x;
ASSERT (x+y == 7 && y <10)
x = x+y;
ASSERT (x == 7 && y <10)

it is equivalent to:

x= x-y;
ASSERT (y == 7 && y-x <10)
y= y-x;
ASSERT (x+y == 7 && y <10)
x = x+y;
ASSERT (x == 7 && y <10)

Now again we will apply the same method as previous one and we replace all the occurrence of
x with x-y in the the next ASSERT

ASSERT (y == 7 && y-x+y <10)


x= x-y;
Computational Theory
3
ASSERT (y == 7 && y-x <10)
y= y-x;
ASSERT (x+y == 7 && y <10)
x = x+y;
ASSERT (x == 7 && y <10)

which is equivalent to:

ASSERT (y == 7 && 2y-x <10)


x= x-y;
ASSERT (y == 7 && y-x <10)
y= y-x;
ASSERT (x+y == 7 && y <10)
x = x+y;
ASSERT (x == 7 && y <10)

which is equivalent to:

ASSERT (14-x <10) (since y=7 so, 2y=14)


x= x-y;
ASSERT (y == 7 && y-x <10)
y= y-x;
ASSERT (x+y == 7 && y <10)
x = x+y;
ASSERT (x == 7 && y <10)

which is equivalent to:

ASSERT (y == 7 && x >4) (14-x <10 -x<-4  x>4)


x= x-y;
ASSERT (y == 7 && y-x <10)
y= y-x;
ASSERT (x+y == 7 && y <10)
x = x+y;
ASSERT (x == 7 && y <10)

which is equivalent to:

ASSERT (x >4 && y == 7) 


x= x-y;
ASSERT (y == 7 && y-x <10)
y= y-x;
ASSERT (x+y == 7 && y <10)
x = x+y;
ASSERT (x == 7 && y <10)
Computational Theory
4
Hence, we discover the pre-condition. Thus, it is verified.

B) ASSERT (y >= 0 && z >= 0)


if (y>z) {
y=y- z} s
else {z= z- y;}
ASSERT (y >= 0 && z >= 0

Answer
we are conditioned that if
y>z then Y=y-z
and since all variables are greater than 0 let us take all to be positive numbers
using the condition
y>z
hence
y=3
z=2
thus
y=3-2
y=1 which is TRUE
0r else
Z==z-y which will be
Z=2-3
Z=-1 since we are using condition else it will be opposite of our ASSERT
Hence will be z <= 0
And since z=-1
then
the pre-condition is verified.
Computational Theory
5

Question 6:
What should the pre-condition P be in each of the following correctness statements for the
statement to be an instance of Hoare’s assignment axiom? All variables are of type int.
A) P {x=y+z;} Exists (x = 0; x <2*y) u*x<=v+z

Answer

The condition P will not be executed it will give out an error and this is because it does not
satisfy the conditionss

B) P{x=y+z;} ForAll (y = 0; y <x) Exists (z= 0; z<u)x+z>=y

Answer
The condition P will be executed and return the value this is because it satisfies the conditions

Question 7:
Use the array-component assignment axiom (two times) to find the most general sufficient pre-
condition P for the code fragment below. A is an array of integers, x is an integer variable, and
we assume that all the subscripts are within the range of subscripts for A.
ASSERT(P) /* P is to be determined */
A[m] = 5;
A[i] =x – 1;
ASSERT(A[k] > A[i])
A) Complete the tableau and thus write the assertion P using the notation from the array-
component assignment axiom.

Answer
Here we are finding the pre-condition so we retrace our steps starting from the post
condition using the arrays given which is
We replace
A[i] in the ASSERT with x – 1;
hence
Computational Theory
6
=ASSERT(A[k] > x – 1) && A[m] = = 5

B) Rewrite the assertion P obtained in Question a in a logically equivalent form that does not
contain any notation (A | I > E). Explain your rewriting.

Answer
We start from
ASSERT(A[k] > x – 1) && A[m] = = 5
A[m] = 5;
A[i] =x – 1; since array A has k,m and I it will be
A (5; x – 1; k)
Hence
ASSERT A (5; x – 1; k)
s

Question 8:
Assume a declarative interface where n and max are constant integers, and A is an array of
integers of size max. For non-negative integers x and i, power(x,i) denotes x! (the i-th power of
x), that is, power(x,0) = 1 and power (x, i+1) = x * power(x,i). Consider the following (partial)
correctness statement:
ASSERT (1 <= n < max)
int j;
j=1;
A[o] = 1;
while (j <n) {
A[j] = A[j-1] + power (2, j);
j=j+1}
ASSERT (ForAll (k = 0; k < n) A[k] == power (2, k+1) - 1 )
A) Choose a loop invariant for the while loop in the code and explain your choice.

Answer
Computational Theory
7
After m iterations of the loop, for all i > n-m,

We have for all ∀i >n-m = A[2*i] = A0[i] + A0[2*i], k = n-m.

B) Give a complete proof tableau for the above correctness statement by adding all the
intermediate assertions. State all the mathematical facts that are used in the proof.

Answer
We know that the invariant is valid as m = n in the beginning loop, so the invariant of the loop is
true. 
Now, let's assume that the loop invariant holds for the loop's first m iterations, we prove that for
the first m+1 iteration the loop invariant is valid.

 then for (m+1)th iteration, we have

k = n-m, A[2*k] = A[k] + A[2*k] = A0[k] + A0[2*k], k=j-1 = n-m-1

Hence the loop invariant holds.

C) Does the loop terminate? Explain.

Answer
In the termination, loop invariant is vacuously true, as k=0
And Yes, the loop terminates because k goes from n to 0.

Hence the assertion holds

You might also like