In class we saw another example of correctness proof of a recursive algorithm. Recall the relevant concepts. Let x,m be natural numbers, and m>0. Then there is a unique way of writing x = q*m + r, where q,r are natural and r0 PostCondition : returns x mod m if x < m return x else return MOD ( x-m,m) To show correctness, we first idnetify the relevant parameter that changes through the recursion. This is x, and so we consider the predicate P(n) = " if PreC holds then PostC holds when MOD is run with input x=n " So saying that P(4) holds, say, is saying that whenever MOD (4,m) is run such that preC holds (namely, m is a natural positive number), the PostC holds (namely the returned value is 4 mod m. We show P by complete induction. Let n be a natural number and assume that for all k=m, n-m is a natural number. Therefore, PreC holds when running MOD (n-m,m), and since P(n-m) holds, we get that PostC holds, and so MOD (n-m,m) = n-m mod m. But, n-m mod m - n mod m as they differ by a multiple of m (e.g., 23 mod 7 = 16 mod 7), hecne MOD (n,m) = MOD (n-m,m) = n-m mod m = n mod m, and so P(n) holds (it's important to the same argument can be applied to any m.) We then talked about a program with the same pre and post conditions (and so is supposed to serve precisely the same function), except it works iteratively instead of recursively. We called that program iMOD and you can find the code in Q5 of the exercises on page 71 of the text. We discussed the concept of LOOP INVARIANT which is a statement that must be true at the end of the i'th iteration of the loop (including the case i=0 which means before entering the first iteration). In this case the loop invariant stated that r_i mod m = x mod m. (Notice that in the loop invariant we attach an iteration-index as subscript to the variables that change. So instead of using 'r' which is the variable name of the program, we use r_i, r_0 is the value of r before entering the loop, r_1 is the value afer the first iteration, and so on). Now P(i) : "if the loop is exectuted at least i times then r_i mod m = x mod m" We show P(i) using simple induction. P(0) is true since r_0 = x and so r_0 mod m = x mod m Say P(n) is true (IH), we need to show P(n+1). Of course we assume the loop ran at least n+1 otherwise there is nothing to prove. Now r_{n+1} mod m = r_{n+1}-m mod m = r_n mod m = x mod m. The first (leftmost) equality above is justified since adding/subtracting multiples of m doesnt change the ramainder when dividing by m, the second is since r_{n+1}-m = r_n according to the code, and the third equality is guaranteed by IH. We have thereofre shown P(n+1), and conclude that since P(0), and P(n)->P(n+1), then for all n P(n). ASSUMING termination, we want to show that the PostC holds. Indeed, at exit we have 0<= r < m and so r mod m = r; by the LI x mod m = r mod m, and hence r is indeed x mod m. We are left with showing termination. For that we asociate a quantity t_i to a loop so that the t_i are natural numbers and are DECREASING. Such a sequence of numbers must be finite (follows from principle of well ordering), hence termiantion is guaranteed. In this case that we simply take t_i = r_i. Obviously the t_i are decreasing, and since we only subtract m when r_i>=m, they will remain nonnegative. In the last example we analyzed the function pow(x,y). pow(x,y) { #preC : x any number, y natural number #postC: returns x^y a=1 b=0 while (b != y) { # loop invarinat: a_i = x^b_i and b_i is natural that is at most y a = x*a b = b+1 } return a } As before, we have P(i): if the loop has at least i iterntions, a_i = x^b_i and b_i is natural that is at most y base: P(0) holds since a_0 = 1 = x^0 = x^b_0 Induction step: If P(n) holds needs to show P(n+1) a_{n+1} = x * a_n = x * x^b_n = x^{b_n + 1} = x^{b_{n+1}} using the IH and the code instructions in the loop. Further, Since P(n+1) cn fail only if there are no n+1 iterations. Since by P(n) we have that b_n <= y, and since the loop did not terminate, we know that actually b_n < y. Hence b_{n+1} = b_n + 1 <= y. Obviously b_{n+1} is still integral. Hence P(n+1) holds, and P(n) always hold. To show termination we associate the number t_i = y-b_i. Notice that t_i >= natural numbers as by the loop invariant b_i <= y always. Also, since b_i < b_{i+1} we get that t_i > t_{i+1} and so we get a decreasing sequence of natural numbers, hence the loop terminates. We are left to show the postcondition. We know that the loop will terminate. At exit, b = y. By the LI, a = x^b, and so a=x^y. Notice that I chose to write this without th subscript, s out of the loop we can simply use the nams themselves, Remebering that they will be set to the values at the termination of the last iteration. So PostC holds, and that's it!! The last example was shoing termination of a certin program. The program and the discussion about how to do it is done in section 2.5, pag 58-59 in the text.