1 00:00:01,364 --> 00:00:02,780 PROFESSOR: Now, the standard thing 2 00:00:02,780 --> 00:00:05,770 you do with a recursive data type in programming 3 00:00:05,770 --> 00:00:08,500 is you define recursive procedures on them, 4 00:00:08,500 --> 00:00:12,670 so let's look at how that works. 5 00:00:12,670 --> 00:00:17,130 I'm going to define a function f on a recursively defined data 6 00:00:17,130 --> 00:00:19,240 type R, and the way I'm going to do 7 00:00:19,240 --> 00:00:24,420 it is I'm going to define f of b explicitly in terms of b 8 00:00:24,420 --> 00:00:28,900 and operations that are already understood for all of the base 9 00:00:28,900 --> 00:00:34,560 cases of b in R. And then I'm going 10 00:00:34,560 --> 00:00:37,350 to define f of a constructor applied 11 00:00:37,350 --> 00:00:43,090 to x in terms of x and f of x. 12 00:00:43,090 --> 00:00:44,910 And if I keep to that structure, that 13 00:00:44,910 --> 00:00:48,020 gives me a recursive definition of the function 14 00:00:48,020 --> 00:00:51,920 f on the recursively defined data set R. 15 00:00:51,920 --> 00:00:54,540 Let's look at an example to make this recipe 16 00:00:54,540 --> 00:00:56,840 explicit and clearer. 17 00:00:56,840 --> 00:00:58,510 Let's think about a recursive function 18 00:00:58,510 --> 00:01:00,980 on a set of matched brackets. 19 00:01:00,980 --> 00:01:03,900 This is a somewhat interesting one. 20 00:01:03,900 --> 00:01:07,250 Let's define the depth of a string as follows, 21 00:01:07,250 --> 00:01:12,050 and the idea is it's how deeply nested are the successive pairs 22 00:01:12,050 --> 00:01:14,100 of left and right brackets. 23 00:01:14,100 --> 00:01:17,280 Well, the depth of the empty string is 0. 24 00:01:17,280 --> 00:01:21,220 You got to start somewhere, and it's got no brackets, 25 00:01:21,220 --> 00:01:23,780 we'll call it depth 0. 26 00:01:23,780 --> 00:01:28,690 Now, what about the depth of the constructor putting brackets 27 00:01:28,690 --> 00:01:31,690 around s and then following it by t? 28 00:01:31,690 --> 00:01:35,140 Well, putting brackets around s gives you 29 00:01:35,140 --> 00:01:38,410 a string that's 1 deeper than s is, 30 00:01:38,410 --> 00:01:42,130 and then you follow it by t, and it's as deep as t is. 31 00:01:42,130 --> 00:01:45,600 So the result is that the depth of the constructor 32 00:01:45,600 --> 00:01:50,060 is a string which is a number which 33 00:01:50,060 --> 00:01:55,480 is equal to 1 plus the depth of s and the depth of t, 34 00:01:55,480 --> 00:01:56,610 whichever is larger. 35 00:01:56,610 --> 00:02:00,890 The max of 1 plus depth of s and depth of t, and that's 36 00:02:00,890 --> 00:02:05,161 our recursive definition of depth. 37 00:02:05,161 --> 00:02:07,410 Let's look at maybe another even more familiar example 38 00:02:07,410 --> 00:02:08,430 of recursive definition. 39 00:02:08,430 --> 00:02:15,310 Let's define the nth power of an integer or real number k. 40 00:02:15,310 --> 00:02:18,610 The zeroth power k is defined to be 1, 41 00:02:18,610 --> 00:02:20,960 and the n plus first power of k is 42 00:02:20,960 --> 00:02:26,260 defined to be k times the nth power of k, 43 00:02:26,260 --> 00:02:29,110 and this would be an executable definition 44 00:02:29,110 --> 00:02:32,680 of the exponentiation function in a lot of programming 45 00:02:32,680 --> 00:02:35,080 languages. 46 00:02:35,080 --> 00:02:39,130 And my point here is that this familiar definition, 47 00:02:39,130 --> 00:02:41,780 recursive definition on a nonnegative integer n, 48 00:02:41,780 --> 00:02:45,730 is in fact a structural induction using the fact 49 00:02:45,730 --> 00:02:51,530 that the nonnegative integers can be defined recursively 50 00:02:51,530 --> 00:02:52,760 as follows. 51 00:02:52,760 --> 00:02:57,700 0 is a nonnegative integer, and if n is a nonnegative integer, 52 00:02:57,700 --> 00:03:02,100 then n plus 1 is a nonnegative integer. 53 00:03:02,100 --> 00:03:05,650 So to summarize, the recipe for a recursive function definition 54 00:03:05,650 --> 00:03:10,110 is you define f going from the recursive data type 55 00:03:10,110 --> 00:03:11,730 to values-- whatever kind of values 56 00:03:11,730 --> 00:03:14,830 you want to assign to these recursive data-- 57 00:03:14,830 --> 00:03:17,920 f of b is defined directly for the base case b, 58 00:03:17,920 --> 00:03:21,010 of base cases b, and f of the constructor of x 59 00:03:21,010 --> 00:03:26,330 is defined using f of x and x. 60 00:03:26,330 --> 00:03:31,210 Now, once you've gotten a function defined recursively, 61 00:03:31,210 --> 00:03:33,640 you can start proving things about it 62 00:03:33,640 --> 00:03:36,670 by structural induction or by induction 63 00:03:36,670 --> 00:03:41,000 on its own definition, its own recursive definition. 64 00:03:41,000 --> 00:03:43,930 So let's look at an example of that. 65 00:03:43,930 --> 00:03:46,050 I want to prove the following property 66 00:03:46,050 --> 00:03:51,430 of the depth of strings in M, namely 67 00:03:51,430 --> 00:03:56,740 that if I look at the length of a string r plus 2-- 68 00:03:56,740 --> 00:04:03,450 so the vertical bars around r mean the number of brackets 69 00:04:03,450 --> 00:04:08,030 in the string r plus 2 is less than or equal to 2 70 00:04:08,030 --> 00:04:10,970 to the power of depth plus 1-- twice the 2 to 71 00:04:10,970 --> 00:04:14,330 of the depth of the string. 72 00:04:14,330 --> 00:04:17,370 And I want to prove that this holds for all strings 73 00:04:17,370 --> 00:04:20,140 r of matched brackets, and I'm going to prove it 74 00:04:20,140 --> 00:04:22,110 by structural induction. 75 00:04:22,110 --> 00:04:23,660 And just as a walk-through, here's 76 00:04:23,660 --> 00:04:25,230 how the proof is going to go. 77 00:04:25,230 --> 00:04:28,080 Let's suppose that r is the base case. 78 00:04:28,080 --> 00:04:30,700 Is it the case that this inequality 79 00:04:30,700 --> 00:04:32,460 holds for the empty string? 80 00:04:32,460 --> 00:04:39,360 Well, the length of r is 0, so length of r plus 2 is 0 plus 2, 81 00:04:39,360 --> 00:04:41,760 or 2, which is the same as 2 to the 0 82 00:04:41,760 --> 00:04:45,380 plus 1, which is in fact equal to 2 to the depth 83 00:04:45,380 --> 00:04:47,050 of the empty string plus 1. 84 00:04:47,050 --> 00:04:48,790 So this inequality actually holds 85 00:04:48,790 --> 00:04:53,866 as an equality in the base case, and we're good there. 86 00:04:53,866 --> 00:04:57,200 What we next need to show is that this inequality 87 00:04:57,200 --> 00:04:58,860 holds in the constructor case. 88 00:04:58,860 --> 00:05:02,270 So we're looking at an arbitrary string 89 00:05:02,270 --> 00:05:07,810 r that's built out of s and t, and r is left bracket, 90 00:05:07,810 --> 00:05:12,970 s right bracket, t, and I want to show that r 91 00:05:12,970 --> 00:05:15,070 satisfies this inequality. 92 00:05:15,070 --> 00:05:16,920 Well, by induction hypothesis, I can 93 00:05:16,920 --> 00:05:19,450 assume that s and t satisfy the inequality. 94 00:05:19,450 --> 00:05:22,690 So I have that the length of s plus 2 is at most 2 95 00:05:22,690 --> 00:05:26,365 to the depth of s plus 1 and the length of t plus 2 is at most 2 96 00:05:26,365 --> 00:05:29,440 to the depth of t plus 1, and let's just 97 00:05:29,440 --> 00:05:31,870 walk through the proof. 98 00:05:31,870 --> 00:05:33,890 You can slow this down and replay it if need be, 99 00:05:33,890 --> 00:05:36,700 so I'm just going to go through it quickly. 100 00:05:36,700 --> 00:05:40,960 The length of r plus 2-- r is, after all, brackets 101 00:05:40,960 --> 00:05:43,990 s, t, so it's simply the length of that string plus 2 102 00:05:43,990 --> 00:05:46,180 by the definition of r. 103 00:05:46,180 --> 00:05:50,380 The length of brackets s, t is the length of t 104 00:05:50,380 --> 00:05:53,850 plus the length of s plus the 2 for the 2 brackets 105 00:05:53,850 --> 00:05:56,410 that we've added, and so we're plugging that 106 00:05:56,410 --> 00:06:00,420 into the previous term and getting that plus 2. 107 00:06:00,420 --> 00:06:02,050 Then just rearrange the terms. 108 00:06:02,050 --> 00:06:04,690 It's the same as the size of s plus 2 109 00:06:04,690 --> 00:06:06,270 plus the size of t plus 2. 110 00:06:06,270 --> 00:06:08,980 And I arranged it that way because by induction 111 00:06:08,980 --> 00:06:13,220 hypothesis, I know that the size of s plus 2 is less than 112 00:06:13,220 --> 00:06:18,030 or equal to 2 to the depth of s plus 1, and likewise for t. 113 00:06:18,030 --> 00:06:20,880 Now I just play a nice trick to get these 2 114 00:06:20,880 --> 00:06:23,750 exponents to look alike. 115 00:06:23,750 --> 00:06:27,930 I say that the depth of s is less than 116 00:06:27,930 --> 00:06:30,660 or equal to the max of the depth of s and the depth of t, 117 00:06:30,660 --> 00:06:32,560 and likewise for the depth of t. 118 00:06:32,560 --> 00:06:38,100 So in both of those terms here, I can replace the exponent 119 00:06:38,100 --> 00:06:42,780 or replace the depth of s by the max of depth s and t, 120 00:06:42,780 --> 00:06:44,160 and likewise here. 121 00:06:44,160 --> 00:06:46,930 Now I've got the same term twice, 122 00:06:46,930 --> 00:06:53,580 so I can say that it's simply twice the max depth. 123 00:06:53,580 --> 00:06:58,210 And of course, that is equal to, by definition 124 00:06:58,210 --> 00:07:01,090 of the depth of r, twice 2 to the depth of r, 125 00:07:01,090 --> 00:07:04,180 which is of course 2 to the depth of r plus 1. 126 00:07:04,180 --> 00:07:08,070 And I have by more or less automatically plugging 127 00:07:08,070 --> 00:07:12,000 into the definitions and a structural induction, 128 00:07:12,000 --> 00:07:14,460 I've proved that this inequality holds 129 00:07:14,460 --> 00:07:22,070 for the recursively defined depth function, and we're done. 130 00:07:22,070 --> 00:07:24,310 Let's look at one more familiar example. 131 00:07:24,310 --> 00:07:26,360 I want to give [? the ?] recursive definition 132 00:07:26,360 --> 00:07:29,350 of the positive powers of 2. 133 00:07:29,350 --> 00:07:34,010 So the base case is the 2 is a positive power of 2, 134 00:07:34,010 --> 00:07:37,330 and the constructor is just one constructor 135 00:07:37,330 --> 00:07:40,510 I'm going to use-- that if x and y are positive powers of 2, 136 00:07:40,510 --> 00:07:43,950 then their product is a positive power of 2. 137 00:07:43,950 --> 00:07:46,140 So let's look at some examples. 138 00:07:46,140 --> 00:07:48,050 I can start with 2 and then the only thing 139 00:07:48,050 --> 00:07:51,500 I can do as a constructor is multiply 2 by 2 to get 4. 140 00:07:51,500 --> 00:07:55,115 Once I got 4, I can do 4 times 2 to get 8, 141 00:07:55,115 --> 00:07:57,480 and I can do 4 times 4 to get 16, 142 00:07:57,480 --> 00:08:02,690 and I can do 4 times 8 to get 32, and all of these 143 00:08:02,690 --> 00:08:04,570 are positive powers of 2. 144 00:08:07,600 --> 00:08:12,890 Now let's define the log to the base 2 of a positive power of 2 145 00:08:12,890 --> 00:08:14,150 recursively. 146 00:08:14,150 --> 00:08:17,720 Well, the log to the base 2 of 2 is 1. 147 00:08:17,720 --> 00:08:21,200 I'd have to define log to the base 2 in the base case, 148 00:08:21,200 --> 00:08:22,770 and that's easy to do. 149 00:08:22,770 --> 00:08:24,730 What about in the constructor case? 150 00:08:24,730 --> 00:08:28,760 Well, the log to the base 2 of x, y is equal to the log 151 00:08:28,760 --> 00:08:31,660 to the base 2 of x plus the log to the base 152 00:08:31,660 --> 00:08:36,260 2 of y for all the x, y's that are positive powers of 2, 153 00:08:36,260 --> 00:08:40,230 and so I have defined a log of the constructor x, 154 00:08:40,230 --> 00:08:45,410 y in terms of the function log of x and the function log 155 00:08:45,410 --> 00:08:46,320 applied to y. 156 00:08:46,320 --> 00:08:49,950 It conforms to the standard definition 157 00:08:49,950 --> 00:08:54,150 of a recursive function on a recursively defined data 158 00:08:54,150 --> 00:08:56,840 type, [INAUDIBLE] positive powers of 2. 159 00:08:56,840 --> 00:08:58,185 Now, this looks OK. 160 00:08:58,185 --> 00:08:59,920 Well, let's just check it out. 161 00:08:59,920 --> 00:09:04,360 So log of 4 is log of 2 times 2, which 162 00:09:04,360 --> 00:09:12,190 is by the definition of the log of 2 plus the log of 2, 163 00:09:12,190 --> 00:09:15,460 which is 1 plus 1, which is equal to 2, and guess what? 164 00:09:15,460 --> 00:09:16,640 That's correct. 165 00:09:16,640 --> 00:09:18,890 The log of 8-- well, 8 is 2 times 166 00:09:18,890 --> 00:09:23,990 4, so by the recursive definition, that's the log of 2 167 00:09:23,990 --> 00:09:28,590 plus the log of 4, which we previously figured out log of 4 168 00:09:28,590 --> 00:09:31,670 was 2, so we get 3 and the answer comes out right. 169 00:09:31,670 --> 00:09:35,110 Now, remember, you're not supposed to in this reasoning 170 00:09:35,110 --> 00:09:38,040 use the properties that you know that log to the base 2 171 00:09:38,040 --> 00:09:42,310 has because we're defining this function which we're 172 00:09:42,310 --> 00:09:45,010 calling log to the base 2 and implicitly claiming 173 00:09:45,010 --> 00:09:46,050 that it's right. 174 00:09:46,050 --> 00:09:48,650 But in order to prove that it's right, 175 00:09:48,650 --> 00:09:52,870 we need to be using just see the structural definition of log 176 00:09:52,870 --> 00:09:54,950 to the base 2 to prove its properties. 177 00:09:54,950 --> 00:09:56,630 So that was what I was illustrating 178 00:09:56,630 --> 00:10:01,780 with this reasoning, that just plugging in the constructor 179 00:10:01,780 --> 00:10:04,700 case of log of x, y is log of x plus log of y, 180 00:10:04,700 --> 00:10:07,300 I can get these numbers out. 181 00:10:07,300 --> 00:10:10,400 So the point of this is just to make the following definition 182 00:10:10,400 --> 00:10:11,200 look reasonable. 183 00:10:11,200 --> 00:10:12,710 I'm going to define a new function which 184 00:10:12,710 --> 00:10:14,251 I'm going to call the log e function, 185 00:10:14,251 --> 00:10:17,340 and it's another function on the positive powers of 2, 186 00:10:17,340 --> 00:10:20,060 and here's the definition of the log e function. 187 00:10:20,060 --> 00:10:23,980 The log e of 2 is going to be 1, just as the log is, 188 00:10:23,980 --> 00:10:28,060 but the log e of x, y is going to be x plus the log 189 00:10:28,060 --> 00:10:33,110 e of y for all x, y and positive powers of 2. 190 00:10:33,110 --> 00:10:35,920 Well, let's try that definition. 191 00:10:35,920 --> 00:10:40,490 Log e of 4 is log e of 2 times 2, which 192 00:10:40,490 --> 00:10:46,530 according to the recursive definition is 2 plus the log 193 00:10:46,530 --> 00:10:51,200 e of 2, which is 1-- namely, it's 3. 194 00:10:51,200 --> 00:10:56,940 Log e of 8-- well, 8 is 2 times 4, so the log e of 8 195 00:10:56,940 --> 00:10:59,420 is 2 plus the log e of 4. 196 00:10:59,420 --> 00:11:01,620 We just figured out that the log e of 4 was 3, 197 00:11:01,620 --> 00:11:03,480 so it's 2 plus 3 is 5. 198 00:11:03,480 --> 00:11:06,620 Log e of 8 is 5. 199 00:11:06,620 --> 00:11:11,830 And finally, log e of 16-- well, 16 is 8 times 2, 200 00:11:11,830 --> 00:11:15,000 so the log e of 8 times 2 is 8 plus the log 201 00:11:15,000 --> 00:11:16,230 e of 2, which we know is 1. 202 00:11:16,230 --> 00:11:16,740 It's 9. 203 00:11:19,900 --> 00:11:23,940 So we've just figured out the log e of 16 is 9, but now comes 204 00:11:23,940 --> 00:11:26,600 the problem. 205 00:11:26,600 --> 00:11:30,960 16, of course, is not only 8 times 2, but it's 2 times 8, 206 00:11:30,960 --> 00:11:35,410 and so the log e of 2 times 8 is 2 plus the log e of 8. 207 00:11:35,410 --> 00:11:39,150 Log e of 8 we previously figured out was 5, so the log e of 16 208 00:11:39,150 --> 00:11:43,220 is 7, and now I have an inconsistency. 209 00:11:43,220 --> 00:11:46,180 I have used this recursive definition of log 210 00:11:46,180 --> 00:11:50,620 e to conclude that the log e of 16 is both 9 and 7, 211 00:11:50,620 --> 00:11:52,190 and we got a problem. 212 00:11:52,190 --> 00:11:54,690 It's not a good definition of a function. 213 00:11:54,690 --> 00:11:58,090 The problem is a simple one called ambiguity. 214 00:11:58,090 --> 00:11:59,900 There's more than one way to construct 215 00:11:59,900 --> 00:12:03,110 the elements of PP2, of positive powers of 2, 216 00:12:03,110 --> 00:12:07,050 from the constructor x times y. 217 00:12:07,050 --> 00:12:11,180 16 was 8 times 2, but it's also 2 times 8, 218 00:12:11,180 --> 00:12:13,590 and of course, it's also 4 times 4. 219 00:12:13,590 --> 00:12:17,460 And depending on which constructor 220 00:12:17,460 --> 00:12:19,960 you use to construct 16, you're going 221 00:12:19,960 --> 00:12:24,755 to get out different values assigned to the log e function. 222 00:12:27,690 --> 00:12:32,240 So when you have an ambiguously defined recursive data 223 00:12:32,240 --> 00:12:36,810 structure-- for example F18 is very ambiguous-- 224 00:12:36,810 --> 00:12:42,670 then defining recursive functions on that definition 225 00:12:42,670 --> 00:12:44,710 is not going to work well, and you 226 00:12:44,710 --> 00:12:49,150 have to very carefully prove that a recursive definition 227 00:12:49,150 --> 00:12:50,950 actually works in a single value. 228 00:12:50,950 --> 00:12:54,070 So for example, log to the base 2 does work, 229 00:12:54,070 --> 00:12:56,280 but that would require proof it doesn't follow on 230 00:12:56,280 --> 00:13:00,620 general principles [INAUDIBLE] you define a recursive function 231 00:13:00,620 --> 00:13:03,755 on an ambiguous data type. 232 00:13:03,755 --> 00:13:05,130 On the other hand, the reason why 233 00:13:05,130 --> 00:13:10,160 we chose that somewhat unexpected single constructor 234 00:13:10,160 --> 00:13:15,630 for the balanced strings M, the balanced bracket strings, 235 00:13:15,630 --> 00:13:18,190 was that it turns out to be unambiguous. 236 00:13:18,190 --> 00:13:21,980 And so that definition of depth is a good definition, 237 00:13:21,980 --> 00:13:27,710 as is any definition based on the recursive definition 238 00:13:27,710 --> 00:13:29,140 of the set M. 239 00:13:29,140 --> 00:13:31,540 So the general problem we have to watch out for-- 240 00:13:31,540 --> 00:13:34,360 [AUDIO OUT] 241 00:13:38,590 --> 00:13:41,530 Constructor created the datum e. 242 00:13:41,530 --> 00:13:44,330 If there's more than one way to construct e, 243 00:13:44,330 --> 00:13:49,070 then you're not quite sure which case 244 00:13:49,070 --> 00:13:53,130 to use to define the function f, and that's 245 00:13:53,130 --> 00:13:57,430 why this issue of whether or not the data structure's ambiguous 246 00:13:57,430 --> 00:14:00,270 is critical to getting good definitions 247 00:14:00,270 --> 00:14:02,640 of recursive functions.