# CMPSCI 250 Discussion #6: Naturals and Strings

#### 27/29 March 2006

Here we proved correctness and termination for two pseudo-Java methods (similar to those on pages 4-37 and 4-38 of the text):

Definition:

1. The value of the string λ is the natural 0.
2. If n is the value of w, then the value of w0 is 2n and the value of w1 is 2n+1.

``````
public natural value (string w)
{// Returns the natural number value of the given binary string
if (w == emptystring) return 0;
string abl = allButLast (w);
if (last(w) == '0')
return 2 * value (abl);
else return 2 * value (abl) + 1;}
``````

Writing Exercise: Prove by induction on all strings w that `value(w)` terminates and returns the correct value according to the definition.

Solution: The base case is w = λ. Here the defined value is 0, and the computed value is clearly 0 from line 3 of the code.

For the inductive case, assume that `value(x)` terminates with the right answer, and we will prove that each of `value(x0)` and `value(x1)` terminate with the right answer.

Let v be the defined value of x, which by the IH is equal to the output of `value(x)`. For x0, the defined value is 2v by the definition. Tracing the code on input x0, we see that it sets `abl` to x, then goes down the if branch of the if statement and returns 2 * value (x), which is 2v as desired. We can see that it terminates under the assumption that the recursive call terminates. For x1, the defined value is 2v+1 by the definition. The code sets `abl` to be x, then goes down the else branch of the if statement, where it returns 2 * value (x) + 1, which is 2v+1 as desired. Again we see that it terminates under the assumption that the recursive call terminates.

Definition:

1. The natural 0 is represented by the string "0".
2. The natural 1 is represented by the string "1".
3. If n > 1, we divide n by two, let w represent n/2, let a represent n%2, and then represent n by wa.

``````
public string rep (natural n)
{// Returns canonical string (no leading 0's) representing n.
if (n == 0) return "0";
if (n == 1) return "1";
string w = rep (n/2);
if (n%2 == 0)
return append (w, '0');
else return append (w, '1');}
``````

Writing Exercise: Prove by (strong) induction on all naturals n that `rep(n)` terminates and returns the correct value according to the definition.

Solution: The base case is n = 0. Here the defined value is "0", and the computed value is clearly "0" from line 3 of the code.

For the strong inductive case, we assume that `rep(j)` terminates with the right answer for all j such that j ≤ n, and we will prove that `rep(n+1)` terminates with the right answer.

If n+1 happens to be 1, the defined value is "1" and we can see from line 4 of the code that the output value is also "1". So now assume that n ≥ 1. The defined value is the string xa, where x is the defined value for (n+1)/2 and a is the defined value of (n+1)%2. Let's look at the code on input n+1. The variable w is set on line 5 to the result of the recursive call to rep((n+1)/2). But since 1 ≤ n, we know that n+1 ≤ 2n and hence that (n+1)/2 ≤ n, by halving both sides. Thus by the strong IH, we know that the recursive call terminates with an output that is equal to the defined value for (n+1)/2, that is, the string we have called x.

Now the code reaches the if statement and checks the value of (n+1)%2. If this is 0, it goes down the if branch and terminates with output x0. If this is 1, it goes down the else branch and terminates with output x1. In each case this is equal to the defined value for n+1, so we have completed the strong induction.