{n > 0} count = n; sum = 0;
P: {n>0 and count = n and sum = 0} while count 0 do sum = sum + count; count = count 1; end Q: {sum = 1 + 2 +… + n}
Please note that you need to do this question in the following steps:
a) Find a candidate loop invariant I. Show the induction steps using which you find I.
Σn k=1 k - Σn k=1k = 0
c)Show {I and B} S {I}
P: {sum + count = n + (n-1) +…. + (count+1) + count} => I: {sum = n + (n-1) +…. + (count+1)} sum = sum + count;
Q: {sum = n + (n-1) +…. + (count+1) + count}
P: {sum = n + (n-1) +…. + ((count-1)+2) + ((count-1)+1) => {sum = n + (n-1) +…. + (count+1) + count} count = count 1;
I: {sum = n + (n-1) +…. + (count+2) + (count+1)}
d)Show (I and (not B)) => Q
(sum = n + (n-1) +…. + (count+2) + (count+1)) and (count = 0) => (sum = n + (n-1) +…. + 2 + 1) = Q
e) Argue that the loop terminates.
Since
1) n > 0 and count = n => count > 0 before the while loop
2) In the while loop, each iteration decrements count by 1
Therefore, count’s value will become 0 after n iterations.
2. Write a denotational semantics mapping function for the following statements in Java
a) (5%) Mi++(++, S)
Mi++(++, S) =
If VARMAP(id, S) = undefined
Then error
Else (V, S’)
Where
V = VARMAP(id, S)
S’ = S[V+1 / id]
b) (5%) M++i(++, S) =
If VARMAP(id, S) = undefined
Then error
Else (V, S’)
Where
V = VARMAP(id, S) + 1
S’ = S[V / id]
c)(10%) Mfor(for ( ; ; ) {}, S)
Assume the semantics mapping functions Massign, Mbexpr, are Mstmts given.
Mfor(for ( ; ; ) {}, S) =
If Massign(, S) = error then error else if Mbexpr(, S’) = error then error else if Mbexpr(, S’) = false then S’ else if Mstmts(, S’) = error then error else if Massign(, S’’) = error then error else Mfor(for( ; ; ) {}, S’’’) where S’ = Massign(, S)
S’’ = Mstmts(, S’)
S’’’ = Massign(, S’’)
3. (15%) Design a state diagram to recognize one form of the comments of the C-based programming languages,