1 00:00:00,966 --> 00:00:07,430 PROFESSOR: So whenever you define a recursive data type, 2 00:00:07,430 --> 00:00:09,280 implicit in the definition is a method 3 00:00:09,280 --> 00:00:13,197 for proving things about it called structural induction. 4 00:00:13,197 --> 00:00:14,780 And the way structural induction works 5 00:00:14,780 --> 00:00:17,730 is that if you want to prove that every element 6 00:00:17,730 --> 00:00:20,800 in some recursively defined data type has a particular property 7 00:00:20,800 --> 00:00:28,970 P, then you proceed by showing that every one of the elements 8 00:00:28,970 --> 00:00:33,510 in the base case that are in R has property P, 9 00:00:33,510 --> 00:00:38,880 and moreover that if you apply a constructor to an element x, 10 00:00:38,880 --> 00:00:44,000 then it has property P whenever x has property P. That is, 11 00:00:44,000 --> 00:00:49,310 you can assume as a structural induction hypothesis P of x 12 00:00:49,310 --> 00:00:52,370 and then you need to show that P of c of x holds. 13 00:00:52,370 --> 00:00:54,254 And this applies for each constructor c. 14 00:00:54,254 --> 00:00:56,170 Some constructors take more than one argument, 15 00:00:56,170 --> 00:00:59,770 but this is meant to illustrate the general pattern. 16 00:00:59,770 --> 00:01:02,250 Let's do an easy example first. 17 00:01:02,250 --> 00:01:03,630 This is what we've actually seen. 18 00:01:03,630 --> 00:01:05,500 And we took for granted this method of proof 19 00:01:05,500 --> 00:01:07,780 without highlighting it when we argued 20 00:01:07,780 --> 00:01:09,620 that the set E that was recursively 21 00:01:09,620 --> 00:01:15,240 defined in the last presentation contained only even numbers. 22 00:01:15,240 --> 00:01:20,756 So remember the definition of E was that the 0 is in E. 23 00:01:20,756 --> 00:01:24,350 And we're going to be proving that x is even by induction. 24 00:01:24,350 --> 00:01:28,030 So we need to check the base case, yes, 0 is even. 25 00:01:28,030 --> 00:01:30,640 And then we need to show that assuming 26 00:01:30,640 --> 00:01:34,160 the structural induction hypothesis that n is even, 27 00:01:34,160 --> 00:01:37,330 then when we apply the constructor n plus 2, 28 00:01:37,330 --> 00:01:38,990 it's even-- well, obviously it is-- 29 00:01:38,990 --> 00:01:43,500 or if we apply the constructor minus n, that's also even. 30 00:01:43,500 --> 00:01:44,760 And it is as well. 31 00:01:44,760 --> 00:01:48,280 And that's why structural induction tells us 32 00:01:48,280 --> 00:01:55,170 that in fact, every string in the set E is even. 33 00:01:55,170 --> 00:01:57,870 Now let's look at a somewhat more interesting example, 34 00:01:57,870 --> 00:02:03,050 which was the set M of matching right and left brackets. 35 00:02:03,050 --> 00:02:05,760 And what I want to prove by structural induction 36 00:02:05,760 --> 00:02:10,660 is that every string in M has the same number 37 00:02:10,660 --> 00:02:13,040 of left brackets and right brackets. 38 00:02:17,660 --> 00:02:20,960 I can restate this by defining EQ 39 00:02:20,960 --> 00:02:24,390 to be the set of strings with the same number 40 00:02:24,390 --> 00:02:26,296 of right and left brackets. 41 00:02:26,296 --> 00:02:27,670 And what I'm really trying to say 42 00:02:27,670 --> 00:02:30,780 is that M is a subset of EQ. 43 00:02:30,780 --> 00:02:31,280 All right. 44 00:02:31,280 --> 00:02:33,240 Now the way I'm going to prove this 45 00:02:33,240 --> 00:02:36,280 is by defining my induction hypothesis P of s 46 00:02:36,280 --> 00:02:40,970 to be that s is in EQ, that is s has an equal number of left 47 00:02:40,970 --> 00:02:43,980 and right brackets. 48 00:02:43,980 --> 00:02:47,000 Well, let's remember what the definition of M was. 49 00:02:47,000 --> 00:02:50,610 The base case of M was the empty string with no brackets at all. 50 00:02:50,610 --> 00:02:54,690 And does the empty string satisfy P of s? 51 00:02:54,690 --> 00:02:55,730 Well, yeah. 52 00:02:55,730 --> 00:02:59,230 It has 0 right brackets and 0 left brackets 53 00:02:59,230 --> 00:03:02,280 so it does have an equal number of left and right brackets. 54 00:03:02,280 --> 00:03:06,830 So we've established that the base case P of the empty string 55 00:03:06,830 --> 00:03:09,606 is true. 56 00:03:09,606 --> 00:03:11,480 Now we have to consider the constructor case. 57 00:03:11,480 --> 00:03:13,438 In the case of M, there's only one constructor, 58 00:03:13,438 --> 00:03:18,760 namely if r and t are in M, then so 59 00:03:18,760 --> 00:03:21,990 is s, which you get by putting brackets around r 60 00:03:21,990 --> 00:03:25,010 and following it by t. 61 00:03:25,010 --> 00:03:26,910 Well, here's the argument. 62 00:03:26,910 --> 00:03:28,810 We're allowed to assume-- we're trying 63 00:03:28,810 --> 00:03:30,690 to prove that s has an equal number of left 64 00:03:30,690 --> 00:03:31,730 and right brackets. 65 00:03:31,730 --> 00:03:36,600 And we're allowed to assume that r does and so does t. 66 00:03:36,600 --> 00:03:40,260 So let's look at the number of right brackets in s. 67 00:03:40,260 --> 00:03:42,040 Well, where they come from? 68 00:03:42,040 --> 00:03:45,140 The right brackets in s consist of-- well, 69 00:03:45,140 --> 00:03:46,970 the first symbol in s is a left bracket, 70 00:03:46,970 --> 00:03:48,360 so that doesn't matter. 71 00:03:48,360 --> 00:03:52,040 Then it's the right brackets in r. 72 00:03:52,040 --> 00:03:55,950 And then there is a new right bracket that gets added. 73 00:03:55,950 --> 00:03:58,370 And then there are the right brackets in t. 74 00:03:58,370 --> 00:04:03,520 So what I can say is that the right brackets in s are simply, 75 00:04:03,520 --> 00:04:06,000 the number of them is the sum of the number 76 00:04:06,000 --> 00:04:07,990 in r plus the number in t plus one 77 00:04:07,990 --> 00:04:10,920 more, cause the constructor threw in one more right 78 00:04:10,920 --> 00:04:11,600 bracket. 79 00:04:11,600 --> 00:04:14,650 By exactly the same reasoning, the number 80 00:04:14,650 --> 00:04:17,290 of left brackets in s is the number 81 00:04:17,290 --> 00:04:21,899 of left in r, left in t plus 1. 82 00:04:21,899 --> 00:04:26,530 Now, because of hypothesis P of r, 83 00:04:26,530 --> 00:04:30,290 the number of right and left brackets in r are equal. 84 00:04:30,290 --> 00:04:33,910 And likewise, by the induction hypothesis P of t, 85 00:04:33,910 --> 00:04:40,490 the number of right and left brackets in t are equal. 86 00:04:40,490 --> 00:04:45,810 And so the right hand sides of both of these equations 87 00:04:45,810 --> 00:04:46,950 are equal. 88 00:04:46,950 --> 00:04:49,420 And that means that the left hand sides are equal. 89 00:04:49,420 --> 00:04:53,700 We've just proved that the number of right brackets in s 90 00:04:53,700 --> 00:04:56,370 and the number of left brackets in s are the same, so P of s 91 00:04:56,370 --> 00:04:56,980 is true. 92 00:04:56,980 --> 00:04:59,920 The constructor case is covered. 93 00:04:59,920 --> 00:05:02,650 And we can conclude by structural induction 94 00:05:02,650 --> 00:05:06,820 that every s in the set M, recursively defined 95 00:05:06,820 --> 00:05:10,550 set of strings of matched brackets, is in fact 96 00:05:10,550 --> 00:05:14,820 has an equal number of left and right brackets, which 97 00:05:14,820 --> 00:05:19,310 means that M is a subset of Q as claimed. 98 00:05:19,310 --> 00:05:22,830 Well, those were pretty easy structural inductions. 99 00:05:22,830 --> 00:05:24,730 And as with regular induction proofs, 100 00:05:24,730 --> 00:05:27,260 when you get the right induction hypothesis, 101 00:05:27,260 --> 00:05:30,180 the proofs tend to be easy. 102 00:05:30,180 --> 00:05:32,390 And we are going to work on an interesting example 103 00:05:32,390 --> 00:05:34,820 having to do with the F18 functions. 104 00:05:34,820 --> 00:05:38,360 One of the reasons why the F18s are 105 00:05:38,360 --> 00:05:40,630 what's considered in first term calculus 106 00:05:40,630 --> 00:05:44,730 is that if you look at all of those functions-- remember, 107 00:05:44,730 --> 00:05:48,110 you've got them by taking constant functions 108 00:05:48,110 --> 00:05:51,759 and the identity function and the function sine x, 109 00:05:51,759 --> 00:05:53,550 then you could combine them in various ways 110 00:05:53,550 --> 00:05:55,440 by adding, multiplying exponentiating, 111 00:05:55,440 --> 00:05:58,950 composing, taking inverses-- that we 112 00:05:58,950 --> 00:06:02,990 didn't need to add a constructor of taking the derivative. 113 00:06:02,990 --> 00:06:04,410 Because it turns out that you can 114 00:06:04,410 --> 00:06:08,300 prove by structural induction that the F18s are 115 00:06:08,300 --> 00:06:10,560 closed under taking derivatives. 116 00:06:10,560 --> 00:06:14,430 And that makes a lovely class problem.