Notes on induction

In CS 211, you saw induction on the integers. In the context of proving program properties, we usually use induction on data structures like lists and trees. The basic idea is still the same. If we want to show that some property P is true for all lists for example, the induction usually proceeds as follows:

As in the case of integers, the base case is sometimes not the empty list, but it may be lists of length 1 for example.

Similarly, we can prove properties of trees by induction. The base case is usually the empty tree; for the inductive step, assume the property is true for the sub-trees of a tree, and show that the property is true for that tree.

Here is an important theorem about foldl and foldr that we will prove using induction. To lead up to this theorem, let us prove a simple result.

Lemma 0: If f is a commutative and associative function, then f(x,f(y,z)) = f(y,f(x,z)).

Proof: left to you. More generally, you can show that the three values x,y,z can be combined together in any order; for example, f(z,f(y,x)) computes the same value.

We will call a commutative and associative function a reduction operator. Examples of reduction operators are integer addition, and multiplication, boolean conjunction and disjunction etc.

Theorem 1: If f is a commutative and associative function, then for all lists l, foldl f a l = foldr f a l.

Before trying to prove anything, it always helps to understand intuitively the result you want to prove. We know that informally,

foldr f a [e1,e2,...,en-1,en] = f(e1, f(e2, ...f(en,a)))
and
foldl f a [e1,e2,...,en-1,en] = f(en, f(en-1, ...f(e1,a)))

If f is a reduction operator, then the reductions can be performed in any order, so in particular, the two expressions shown above for foldl and foldr are equal. Thus, Theorem 1 can be viewed as a generalization of Lemma 0.

To prove this theorem, it turns out to be useful to prove an auxiliary theorem.

Theorem 0: If f is a commutative and associative function, then for all lists l, foldr f f(x,a) l = foldr f a (x::l).

As before, it helps to think intuitively about this theorem.

foldr f f(x,a) l = f(e1, f(e2, ... f(en, f(x,a))))
and
foldr f a (x::l) = f(x, f(e1, f(e2,... f(en,a))))

If f is commutative and associative, we can perform the reductions in any order, so these two expressions are obviously equal.

Let us prove Theorem 0 by induction of the length of list l.

Conclusion: Theorem 0 is true for the empty list. If Theorem 0 is true for lists of length k, it is true for lists of length (k+1). By induction, we can assert that Theorem 0 is true for lists of any length.

Now let us prove Theorem 1 by induction.

Proof:

Conclusion: Theorem 1 is true for the empty list. If Theorem 1 is true for lists of length k, it is true for lists of length (k+1). By induction, we can assert that Theorem 1 is true for lists of any length.