Today's topic: more structural induction See lecture notes for 2/24 for more on structural induction. Today, I just want to show you a proof that, for instance, append is associative on lists. Recall that our induction principle for lists is as follows: To prove the statement: For all lists l.P(l) is true, it suffices to: (1) prove P('()) is true. (2) assume that P(q) is true, where q is a list, then prove that for all objects x, P((cons x q)) is true. ==================================================================== Suppose we have the following definition for append: (define (append ) (method ((x ) (y )) (if (null? x) y (cons (head x) (append (tail x) y))))) We wish to prove that append is associative. That is, we wish to prove the following theorem: Thm: For all lists l1, l2, and l3, (append (append l1 l2) l3) = (append l1 (append l2 l3)). We are going to prove this by structural induction on the list l1. So, we restate the theorem as a property that is parameterized by an arbitrary list l: Let P(l) be the property: For all lists l2,l3.(append (append l l2) l3) = (append l (append l2 l3)). Thm [restated]: For all lists l1.P(l1) The proof is as follows. Notice that we've very carefully stated the property (P) that we're trying to prove, that the proof uses the substitution model (or the induction hypothesis) to justify each step. Proof: by structural induction on l1. Base case: l1 = '(). --------------------- We must show P('()). That is, we must show: For all lists l2, l3.(append (append '() l2) l3) = (append '() (append l2 l3)). Pick arbitrary lists a and b (for l2 and l3). We must now show (append (append '() a) b) = (append '() (append a b)). (append (append '() a) b) = (append (if (null? '()) a (cons (head '()) (append (tail '()) a))) b) = (append a b) where all of the equalities hold by the substitution model. Thus we can conclude that I. (append (append '() a) b) = (append a b). Now we will show that (append '() (append a b)) = (append ab): (append '() (append a b)) = (if (null? '()) (append a b) (cons (head '()) (append (tail '() (append a b))))) = (append a b) again where all of the equalities hold by the substitution model. Thus we can conclude that II. (append '() (append a b)) = (append a b). By I and II, we can conclude that: (append (append '() a) b) = (append '() (append a b)). Since a and b were chosen as arbitrary lists, we can therefore conclude that For all lists l2 and l3.(append (append '() a) b) = (append '() (append a b)) Hence, we have shown P('()). Inductive case: --------------- We must show For all lists q.P(q) => (For all objects x.P((cons x q))). Let c be an arbitrary list. We must show that P(c) => (For all objects x.P((cons x c))). That is, we must show that P(c) implies P((cons x c)) for any object x. Induction Hypothesis: --------------------- Assume P(c). That is, assume: For all l2,l3.(append (append c l2) l3) = (append c (append l2 l3)) We must show that for all objects x, P((cons x c)). Let d be an arbitrary object. We must now show that For all l2,l3.(append (append (cons d c) l2) l3) = (append (cons d c) (append l2 l3)) Let a and b be arbitrary lists. We must now show that: (append (append (cons d c) a) b) = (append (cons d c) (append a b)) (append (cons d c) a) = (if (null? (cons d c)) a (cons (head (cons d c)) (append (tail (cons d c) a)))) = (cons (head (cons d c)) (append (tail (cons d c)) a)) = (cons d (append c a)) where all of the equalities hold by the substitution model. Substituting equals for equals, we can therefore conclude that I. (append (append (cons d c) a) b) = (append (cons d (append c a)) b). But: (append (cons d (append c a)) b) = (if (null? (cons d (append c a))) b (cons (head (cons d (append c a)) (append (tail (cons d (append c a))) b)))) = (cons (head (cons d (append c a)) (append (tail (cons d (append c a))) b)))) = (cons d (append (append c a) b)) Therefore: II. (append (cons d (append c a)) b) = (cons d (append (append c a) b)) Now, by the induction hypothesis: (append (append c a) b) = (append c (append a b)) Substituting equals for equals in equation II we get: III. (append (cons d (append c a)) b) = (cons d (append c (append a b))) But then by equations I and III, we can conclude: IV. (append (append (cons d c) a) b) = (cons d (append c (append a b))) Hence, it suffices to show that (cons d (append c (append a b))) = (append (cons d c) (append a b)) Starting from the right-hand-side of the equation: (append (cons d c) (append a b)) = (if (null? (cons d c)) (append a b) (cons (head (cons d c)) (append (tail (cons d c)) (append a b)))) = (cons (head (cons d c)) (append (tail (cons d c)) (append a b)))) = (cons d (append c (append a b))). Therefore V. (cons d (append c (append a b))) = (append (cons d c) (append a b)) Consequently, by IV and V we can conclude: VI. (append (append (cons d c) a) b) = (append (cons d c) (append a b)) Since we picked arbitrary lists a and b, we can conclude: VII. For all lists l1,l2. (append (append (cons d c) l1) l2) = (append (cons d c) (append l1 l2)) That is, we have shown P((cons d c)) holds. Since we picked an arbitrary object d, we can conclude: VIII. For all objects x.P((cons x c)) Now, discharging our induction hypothesis, we can conclude: IX. P(c) => (For all objects x.P((cons x c))). Since c was chosen as an arbitrary list, we can conclude: X. For all lists c.P(c) => (For all objects x.P((cons x c))). Finally, from the proof of the base case and the prove of the inductive case, using the structural induction principle on lists, we can conclude that: For all lists l1.P(l1) QED.