CS 334
|
a) Describe in detail the effect of executing the Pascal assignment
a[a[i]] := a[i] + 1b) Is it always the case (in Pascal) that the value of L after executing assignment L := E is equal to the value of E before executing the command? I.e. if you execute the following code:
write(E); L := E; writeln(L);will the two values printed always be the same? For the purposes of this problem assume that the evaluation of E has no side effects. Hint : consider the assignment in part (a) with i = 2, a[2] = 2, a[3] = 0.
c) What does this tell you about the validity of the axiomatic rule for assignment,{P [E / L]} L := E {P}, (e.g., if L = a[a[i]], E = a[i] + 1, and P is a[a[i]] = 3)? Suggest a restriction which makes it valid.
{Precondition: n > 0} i <- n fact <- 1 while i > 0 do {assert: ...} fact <- fact * i i <- i - 1 end while {Postcondition: fact = 1*2*...*n}Hint: You need to figure out the loop invariant before you can complete the proof. Your proof should take the same form as the one in the lecture notes - hand-waving is NOT acceptable. Be sure to prove the entire algorithm is correct with respect to the precondition and postcondition. The lecture notes example only includes showing the loop invariant is correct. You must prove the entire program correct with respect to the precondition and postcondition!
Note: If you prefer to use the weakest precondition rules in the text, be my guest, but I suspect you will find it easier to use those given in class instead.