COMP 2600: Worked Examples Mon Aug 8 2016 Dirk Pattinson, ANU August 8, 2016 This note collects the examples I have been presenting in today’s lecture using the document camera. The main purpose is to convey a feeling for mathematical induction. 1 Induction on Numbers Consider the following Haskell program: f ::Int -> Int f = 0 f n = 1 + 2 * f (n-1) What does this program do? We try some numbers . . . f (0) = 0 f (1) = 1 + 2 ∗ f (0) = 1 f (2) = 1 + 2 ∗ f (1) = 1 + 2 f (3) = 1 + 2 ∗ f (2) = 1 + 2 + 4 f (4) = 1 + 2 ∗ f (3) = 1 + 2 + 4 + 8 Pn−1 i We therefore hypothesise that f (n) = i=0 2 = 2n − 1. How can we prove this, i.e. how can we assert that this indeed holds for all integers n ≥ 0? One way is to look for the least culprit. Let’s call a natural number n a culprit if it doesn’t satisfy the equation. That is f (n) 6= 2n − 1. Let n be the smallest culprit, i.e. the least natural number that does not satisfy the equation. 1 • Then n > 0 as surely f (0) = 0 = 20 − 1, i.e. 0 is not a culprit. • As n > 0 we know that n − 1 is still a natural number, and n − 1 is not a culprit since n was the smallest such. Therefore we have that f (n − 1) = 2n−1 − 1. But then f (n) = 1 + 2f (n − 1) = 1 + 2(2n−1 − 1) = 2n − 1. That is, from the assumption that n is the smallest culprit, we have concluded that n is not a culprit after all – a logical inconsistency. Therefore no culprits can possibly exist, that is, the equation f (n) = 2n − 1 must necessarily hold for all non-negative integers n. Analysis of Proof. In the proof above, there are only two steps where anything non-trivial has happened: • we have shown that the equation holds for n = 0 • we have assumed that the equation holds for n−1 and have established the equation for n. This is the principle of mathematical induction on natural numbers, phrased generally as follows: To show that a property holds for all natural numbers n, it suffices to show that it holds for 0 and that it holds for n under the assumption that it (already) holds for n − 1). Establishing the property for n = 0 is usually called the base case. The argument that the property holds for n under the assumption that it holds for n − 1 is usually called the induction step or inductive step. In the inductive step, assuming the property for n − 1 is usually called the inductive hypothesis. To see that these two statements indeed suffice, one can replay the above proof with any property of natural numbers in place of the equation f (n) = 2n − 1 in the above proof. 2 Induction on Lists Consider the following two standard Haskell functions (where a and b are arbitrary types and [a] are lists with elements of type a): 2 length :: [a] -> Int length [] = 0 length (x:xs) = 1 + length xs map: (a -> b) -> [a] -> [b] map [] = [] map (x:xs) = (f x):(map f xs) It is clear that mapping over a list doesn’t change its length. But how can we establish that formally? We want to prove that length (map f l) = length l for all lists l. Our argument is the same but this time, we take a culprit to be a list l that doesn’t satisfy the property. I.e. l is a culprit if length (map f l ) 6= length l. Now let l be shortest culprit, again we’re trying to derive that such a list l cannot exist. • For one, we know that l cannot be empty, because length (map f []) = 0 = length [], that is, the empty list is not a culprit. • Since l isn’t empty, we know that it has the form l = x :: xs . And since l is the shortest culprit, we know that the property holds for the (shorter list) xs , that is length (map f xs ) = length xs . But then length (map f x : xs ) = length (f x ) : (map f xs ) = 1 + length (map f xs ) = 1 + length xs = length x : xs . Since l = x : xs we have derived length (map f l ) = length l that is, l is not a culprit after all! As a consequence, culprits cannot exist and therefore the property holds for all lists. Analysis of Proof. Again, there are only two places where something nontrivial happens. • We have shown that the property holds for the empty list • We have shown that it holds for a list of the form x : xs assuming that it holds for the shorter list xs . This is the principle of induction over lists, formulated schematically as follows: 3 To show that a property holds for all lists, it suffices to show that it holds for the empty list, and that it holds for a list of the form x : xs under the assumption that it holds for the list xs . As before, establishing the property for the empty list is called the base case, and the argument that it holds for x : xs assuming that it holds for xs is the inductive step. To see that this in fact suffices, consider to replay the above proof with any property of lists instead of the above equation. 4
© Copyright 2026 Paperzz