1 00:00:01,050 --> 00:00:03,680 PROFESSOR: Recursive data play a key role in programming, 2 00:00:03,680 --> 00:00:08,050 so let's take a mathematical look at what goes on. 3 00:00:08,050 --> 00:00:09,910 So the basic idea of recursive data 4 00:00:09,910 --> 00:00:11,940 is roughly that you're going to define 5 00:00:11,940 --> 00:00:16,099 a class of objects in terms of the simpler 6 00:00:16,099 --> 00:00:17,265 versions of the same object. 7 00:00:21,030 --> 00:00:23,120 With a little more precision, the idea 8 00:00:23,120 --> 00:00:25,165 is that you [? can ?] build up a recursive data 9 00:00:25,165 --> 00:00:27,790 type by starting with some stuff that you understand that's not 10 00:00:27,790 --> 00:00:32,189 recursive and you give me some base objects that I can begin 11 00:00:32,189 --> 00:00:35,700 with and declare that they belong to this recursive datum, 12 00:00:35,700 --> 00:00:39,740 and then you give me some rules called constructor rules which 13 00:00:39,740 --> 00:00:43,850 enable me to build new objects of the recursive type 14 00:00:43,850 --> 00:00:46,900 by applying these constructors to objects that I've already 15 00:00:46,900 --> 00:00:47,516 built up. 16 00:00:47,516 --> 00:00:48,890 There's nothing circular about it 17 00:00:48,890 --> 00:00:51,040 because I'm always building up new stuff 18 00:00:51,040 --> 00:00:53,070 from stuff I already have. 19 00:00:53,070 --> 00:00:54,770 Let's look at an example. 20 00:00:54,770 --> 00:00:57,000 I'm going to define a set E that's 21 00:00:57,000 --> 00:00:58,550 a subset of the integers, and I'm 22 00:00:58,550 --> 00:01:02,610 going to give you a recursive definition of E. The base 23 00:01:02,610 --> 00:01:05,850 case is that I'm going to tell you that 0 is an E 24 00:01:05,850 --> 00:01:08,140 and I'm going to give you two constructors. 25 00:01:08,140 --> 00:01:11,220 The first one says that if you have an n that's an E, 26 00:01:11,220 --> 00:01:14,260 you can add 2 to it and get a new element in E, 27 00:01:14,260 --> 00:01:16,660 providing that n is not negative. 28 00:01:16,660 --> 00:01:19,900 The second constructor is that if you have an n that's an E, 29 00:01:19,900 --> 00:01:20,760 you can negate it. 30 00:01:20,760 --> 00:01:24,864 You can take minus n, providing that n is positive, 31 00:01:24,864 --> 00:01:26,530 and those are the two constructor rules. 32 00:01:26,530 --> 00:01:28,350 Well, let's look at what goes on here. 33 00:01:28,350 --> 00:01:30,100 What is this telling us? 34 00:01:30,100 --> 00:01:32,230 Well, let's just use the first constructor rule 35 00:01:32,230 --> 00:01:33,890 and use it repeatedly. 36 00:01:33,890 --> 00:01:34,910 I can start off with 0. 37 00:01:34,910 --> 00:01:37,326 That's the base case, and then I can apply the constructor 38 00:01:37,326 --> 00:01:38,650 to add 2 to it. 39 00:01:38,650 --> 00:01:45,260 Then I can apply the constructor again to add 2 to 0 plus 2, 40 00:01:45,260 --> 00:01:48,110 and then I can apply the third time to get add 2 to 0 41 00:01:48,110 --> 00:01:49,650 plus 2 plus 2. 42 00:01:49,650 --> 00:01:52,950 And it's clear what I'm getting is 0, 2, 4, 6, and so on, 43 00:01:52,950 --> 00:01:56,420 and I'm going to get all of the non-negative even numbers 44 00:01:56,420 --> 00:01:58,770 in that way. 45 00:01:58,770 --> 00:02:02,640 Now, I can apply to these, the positive numbers, 46 00:02:02,640 --> 00:02:05,290 I can apply the negation constructor. 47 00:02:05,290 --> 00:02:08,250 So I can get minus 2, minus 4, minus 6, 48 00:02:08,250 --> 00:02:10,520 and it becomes apparent then that I can 49 00:02:10,520 --> 00:02:12,593 get all of the even numbers. 50 00:02:15,470 --> 00:02:19,230 So we just figured out that E contains the even numbers. 51 00:02:19,230 --> 00:02:21,180 Is there anything else in E? 52 00:02:21,180 --> 00:02:24,290 And the answer is no, and the reason 53 00:02:24,290 --> 00:02:28,820 is that an implicit part of the understanding of a definition 54 00:02:28,820 --> 00:02:33,900 like this is that the only way that things can get into E 55 00:02:33,900 --> 00:02:37,560 is by being a base case or by being constructed 56 00:02:37,560 --> 00:02:39,680 from previously constructed elements 57 00:02:39,680 --> 00:02:41,150 by applying the constructor rules. 58 00:02:41,150 --> 00:02:43,180 In other words, there's an implicit clause here 59 00:02:43,180 --> 00:02:45,070 that says that's all. 60 00:02:45,070 --> 00:02:48,180 That implicit clause is called the extremal clause. 61 00:02:48,180 --> 00:02:51,820 And it's taken for granted and rarely mentioned explicitly 62 00:02:51,820 --> 00:02:53,600 as part of a recursive definition, 63 00:02:53,600 --> 00:02:55,396 but it's always to be understood. 64 00:02:58,870 --> 00:03:00,370 So what we can conclude from this 65 00:03:00,370 --> 00:03:02,370 is that E is exactly the even integers 66 00:03:02,370 --> 00:03:05,240 because there's nothing else there except those ones that 67 00:03:05,240 --> 00:03:08,070 were built up in the way indicated. 68 00:03:08,070 --> 00:03:11,290 So let's look at a slightly more interesting example now. 69 00:03:11,290 --> 00:03:14,330 I want to define the set of strings that consists 70 00:03:14,330 --> 00:03:17,550 only of left and right parentheses 71 00:03:17,550 --> 00:03:20,040 such that the left and right parentheses match up. 72 00:03:20,040 --> 00:03:21,890 Well, writing parentheses on the slide 73 00:03:21,890 --> 00:03:24,930 turns out to be confusing with parentheses that are actually 74 00:03:24,930 --> 00:03:28,290 used to delimit things, so I'm going to replace parentheses 75 00:03:28,290 --> 00:03:33,000 by brackets in blue-- a right bracket and a left bracket. 76 00:03:33,000 --> 00:03:35,360 This notation here, by the way, stands 77 00:03:35,360 --> 00:03:40,490 for the set of finite strings of right and left brackets. 78 00:03:40,490 --> 00:03:41,827 It's a general notation. 79 00:03:41,827 --> 00:03:43,410 If you have some collection of objects 80 00:03:43,410 --> 00:03:45,400 which you think of as letters and you write 81 00:03:45,400 --> 00:03:47,680 an asterisk as a superscript, that 82 00:03:47,680 --> 00:03:51,420 means the finite strings of those letters. 83 00:03:51,420 --> 00:03:55,830 So these are the finite strings of right and left brackets, 84 00:03:55,830 --> 00:03:58,610 and I want to give a recursive definition of a set 85 00:03:58,610 --> 00:04:02,870 M which I plan will be precisely those strings where 86 00:04:02,870 --> 00:04:06,234 the left and right brackets match up appropriately. 87 00:04:06,234 --> 00:04:07,650 The way to think about matching up 88 00:04:07,650 --> 00:04:11,370 is take let's say a standard arithmetic expression involving 89 00:04:11,370 --> 00:04:13,530 plus and times and so on, and make sure 90 00:04:13,530 --> 00:04:14,980 that it's fully parenthesized. 91 00:04:14,980 --> 00:04:16,190 So whenever you add two things, there's 92 00:04:16,190 --> 00:04:17,731 parentheses around that, and whenever 93 00:04:17,731 --> 00:04:19,790 you multiply two things, there's parentheses 94 00:04:19,790 --> 00:04:23,480 around that, meaning brackets. 95 00:04:23,480 --> 00:04:27,080 Then if you erased everything but the brackets, what 96 00:04:27,080 --> 00:04:30,110 you'd be left with would be a set of matched brackets. 97 00:04:30,110 --> 00:04:32,880 Actually, it would be a set of matched brackets 98 00:04:32,880 --> 00:04:35,310 or you could have several of them next to each other. 99 00:04:35,310 --> 00:04:37,440 Those would still could be considered matched, 100 00:04:37,440 --> 00:04:39,920 so that's the way our definition is going to behave. 101 00:04:39,920 --> 00:04:42,330 Let's give it. 102 00:04:42,330 --> 00:04:44,990 So the base case is about the simplest it could be. 103 00:04:44,990 --> 00:04:46,810 I'm going to start with the empty string. 104 00:04:46,810 --> 00:04:49,690 An empty string is this thing that acts like a zero 105 00:04:49,690 --> 00:04:51,660 under putting strings next to each other, 106 00:04:51,660 --> 00:04:53,450 or the concatenation operation. 107 00:04:53,450 --> 00:04:56,820 If you concatenate the empty string with any string, 108 00:04:56,820 --> 00:04:59,550 it doesn't change the string. 109 00:04:59,550 --> 00:05:01,480 And by definition, then, the empty string 110 00:05:01,480 --> 00:05:04,090 is a string with no characters, has length zero, 111 00:05:04,090 --> 00:05:07,810 and it acts as an identity under putting strings 112 00:05:07,810 --> 00:05:10,100 next to each other. 113 00:05:10,100 --> 00:05:13,160 There's going to be one constructor in M that's 114 00:05:13,160 --> 00:05:14,440 slightly ingenious. 115 00:05:14,440 --> 00:05:17,090 There's other maybe simpler or more natural 116 00:05:17,090 --> 00:05:19,890 ways to make up constructors that would define M, 117 00:05:19,890 --> 00:05:21,430 but this one is particularly nice 118 00:05:21,430 --> 00:05:22,980 because I can get away with just one, 119 00:05:22,980 --> 00:05:25,970 and it has some nice properties that we'll explore later. 120 00:05:25,970 --> 00:05:28,430 So here's the rule-- if I've built up 121 00:05:28,430 --> 00:05:32,860 two strings s and t of matched brackets that are in M, 122 00:05:32,860 --> 00:05:35,920 then I can build a new one by putting matched brackets 123 00:05:35,920 --> 00:05:39,590 around s and concatenating it with t-- that 124 00:05:39,590 --> 00:05:43,490 is, if s and t are strings of brackets in M, 125 00:05:43,490 --> 00:05:46,410 then if I start with a left bracket followed 126 00:05:46,410 --> 00:05:49,860 by the brackets in s followed by a right bracket followed 127 00:05:49,860 --> 00:05:55,250 by the brackets in t, that new string is yet another element 128 00:05:55,250 --> 00:05:57,890 that I've built up in M. 129 00:05:57,890 --> 00:06:00,110 Let's practice this to see how it works. 130 00:06:00,110 --> 00:06:02,170 So there's the constructor again. 131 00:06:02,170 --> 00:06:03,930 Well, how do I get started? 132 00:06:03,930 --> 00:06:06,230 To start, all I have is the base case. 133 00:06:06,230 --> 00:06:09,440 s and t have to both be the empty string because that's 134 00:06:09,440 --> 00:06:12,110 the only thing available to apply the constructor to. 135 00:06:12,110 --> 00:06:14,530 So if I do that, basically the s and the t 136 00:06:14,530 --> 00:06:17,410 disappear in this constructor expression 137 00:06:17,410 --> 00:06:21,360 and all I'm left with is a matching left and right 138 00:06:21,360 --> 00:06:23,660 bracket. 139 00:06:23,660 --> 00:06:26,100 But now I've got a matching left and right bracket, 140 00:06:26,100 --> 00:06:29,350 so I can use that to apply the constructor to, 141 00:06:29,350 --> 00:06:32,680 so I could let s be the matching brackets 142 00:06:32,680 --> 00:06:34,990 and t still be the empty string. 143 00:06:34,990 --> 00:06:37,130 Now when I plug into the constructor, 144 00:06:37,130 --> 00:06:41,940 the t still disappears, but I find brackets within brackets, 145 00:06:41,940 --> 00:06:44,840 and that's another string that I've built up in M. 146 00:06:44,840 --> 00:06:47,560 Now, being methodical, I could let s 147 00:06:47,560 --> 00:06:49,620 be empty and t be the brackets. 148 00:06:49,620 --> 00:06:52,020 And if I do that, then the s goes away 149 00:06:52,020 --> 00:06:54,550 and the t becomes the matched pair of brackets 150 00:06:54,550 --> 00:06:58,970 and I wind up with a matched pair next to a matched pair. 151 00:06:58,970 --> 00:07:01,020 Then, of course, I could let both of them 152 00:07:01,020 --> 00:07:06,030 be the matched brackets, and then I get a nested pair 153 00:07:06,030 --> 00:07:08,220 next to a matched pair. 154 00:07:08,220 --> 00:07:11,886 And now that I've got also going back to the very beginning 155 00:07:11,886 --> 00:07:13,510 the next most complicated string that I 156 00:07:13,510 --> 00:07:17,390 had was the nested pair of brackets, 157 00:07:17,390 --> 00:07:19,600 I could let s be that and t be empty, 158 00:07:19,600 --> 00:07:23,180 and then I would get brackets nested to depth three, 159 00:07:23,180 --> 00:07:24,010 and so on. 160 00:07:24,010 --> 00:07:24,840 That's the idea. 161 00:07:28,550 --> 00:07:30,720 Now, it may or may not be clear that you 162 00:07:30,720 --> 00:07:34,750 get exactly the strings of matched brackets in this way. 163 00:07:34,750 --> 00:07:38,260 That's taken up further in the notes and in some problems, 164 00:07:38,260 --> 00:07:39,880 but we're just trying to understand 165 00:07:39,880 --> 00:07:42,990 how this definition works and take it for granted 166 00:07:42,990 --> 00:07:45,040 that, in fact, it's right. 167 00:07:45,040 --> 00:07:48,470 Let's use that definition to prove some things about M, 168 00:07:48,470 --> 00:07:52,320 but I want to prove the things based on the definition of M 169 00:07:52,320 --> 00:07:54,830 not assuming that it works as intended. 170 00:07:54,830 --> 00:07:58,700 So I'm going to claim based on the definition 171 00:07:58,700 --> 00:08:02,430 that it's impossible to find a string in M that 172 00:08:02,430 --> 00:08:03,720 starts with a right bracket. 173 00:08:03,720 --> 00:08:05,220 Now, of course, since we're assuming 174 00:08:05,220 --> 00:08:07,840 M is the right definition of matched brackets, 175 00:08:07,840 --> 00:08:10,810 it's clear that a string that starts with a right bracket 176 00:08:10,810 --> 00:08:13,770 already has nothing to match-- no left bracket matching it-- 177 00:08:13,770 --> 00:08:15,420 so it shouldn't be in there, but let's 178 00:08:15,420 --> 00:08:17,680 just make sure that the definition behaves in the way 179 00:08:17,680 --> 00:08:19,920 that we intend or it might be wrong. 180 00:08:19,920 --> 00:08:22,550 So how do I prove that no string in M 181 00:08:22,550 --> 00:08:23,950 starts with a right bracket? 182 00:08:23,950 --> 00:08:26,290 Well, let's look at the definition. 183 00:08:26,290 --> 00:08:28,770 The base case doesn't have any brackets at all, 184 00:08:28,770 --> 00:08:31,770 so it certainly doesn't start with a right bracket. 185 00:08:31,770 --> 00:08:35,210 And looking at the constructor rule, all the strings 186 00:08:35,210 --> 00:08:38,120 that you can construct start with a left bracket. 187 00:08:38,120 --> 00:08:42,929 And so we're really appealing to the implicit that's 188 00:08:42,929 --> 00:08:44,770 all clause, the extremal clause that 189 00:08:44,770 --> 00:08:47,610 says that since the only way to get things in M 190 00:08:47,610 --> 00:08:50,590 is by applying the constructor, you're 191 00:08:50,590 --> 00:08:52,720 not going to be able to get anything that 192 00:08:52,720 --> 00:08:54,076 starts with a right bracket. 193 00:08:56,710 --> 00:08:59,700 One more example of a recursively-defined data type 194 00:08:59,700 --> 00:09:04,610 that's interesting, and we'll be doing some lovely class 195 00:09:04,610 --> 00:09:09,000 problems with, is the class that I call F18 functions. 196 00:09:09,000 --> 00:09:11,956 These are the functions from a first term of calculus, 197 00:09:11,956 --> 00:09:15,240 like as you study in 18.01, functions 198 00:09:15,240 --> 00:09:18,930 of a single real variable, and here's a recursive definition 199 00:09:18,930 --> 00:09:21,270 that I think covers all of the functions that 200 00:09:21,270 --> 00:09:24,444 are considered in 18.01. 201 00:09:24,444 --> 00:09:26,110 I'm going to start off with the identity 202 00:09:26,110 --> 00:09:29,400 function and any constant function 203 00:09:29,400 --> 00:09:31,870 and the function sine of x, and declare 204 00:09:31,870 --> 00:09:33,569 that those are the base cases. 205 00:09:33,569 --> 00:09:34,860 Those are the functions in F18. 206 00:09:37,510 --> 00:09:39,330 Then here are the constructor rules. 207 00:09:39,330 --> 00:09:41,890 If I have two functions that are in F18, 208 00:09:41,890 --> 00:09:45,810 I can add and multiply them or take 2 to the f 209 00:09:45,810 --> 00:09:48,370 where f is in there, and those will all also 210 00:09:48,370 --> 00:09:49,870 be functions in F18. 211 00:09:49,870 --> 00:09:52,480 So I can start building up a bunch 212 00:09:52,480 --> 00:09:55,880 of interesting stuff like polynomials and exponentials. 213 00:09:55,880 --> 00:09:58,850 In addition, if I have a function that's in F18, 214 00:09:58,850 --> 00:10:01,260 I can take it's inverse-- at least, insofar 215 00:10:01,260 --> 00:10:04,130 as the inverse is defined in the function-- 216 00:10:04,130 --> 00:10:07,560 and I can also compose two functions that 217 00:10:07,560 --> 00:10:10,590 are in F18 to get another one. 218 00:10:10,590 --> 00:10:12,460 Let's look at how this definition works. 219 00:10:12,460 --> 00:10:16,010 I claim that, in fact, the function minus x is in F18. 220 00:10:16,010 --> 00:10:20,840 How do I build up minus x from the rules? 221 00:10:20,840 --> 00:10:25,860 Well, minus 1 is a constant function, so I have that. 222 00:10:25,860 --> 00:10:28,040 And x is just the identity function, 223 00:10:28,040 --> 00:10:30,620 and I can multiply two functions that I have. 224 00:10:30,620 --> 00:10:33,420 So if I multiply minus 1 times x, guess what? 225 00:10:33,420 --> 00:10:35,790 I got minus x, so I've just figured out 226 00:10:35,790 --> 00:10:38,800 that that function is in F18. 227 00:10:38,800 --> 00:10:40,420 What about the square root of x? 228 00:10:40,420 --> 00:10:43,910 Well, if I multiply the identity by itself, 229 00:10:43,910 --> 00:10:46,250 I get the function x squared. 230 00:10:46,250 --> 00:10:50,860 And then if I take its inverse, that's square root of x. 231 00:10:50,860 --> 00:10:54,610 Well, I gave you sine x, but not cosine x or any other trig 232 00:10:54,610 --> 00:10:55,430 functions. 233 00:10:55,430 --> 00:10:56,360 Why not? 234 00:10:56,360 --> 00:10:59,970 Well, I want them all, but I can get them by the rules already. 235 00:10:59,970 --> 00:11:01,140 So how do you get cosine x? 236 00:11:01,140 --> 00:11:04,510 Well, cosine x is just sine of x plus pi. 237 00:11:04,510 --> 00:11:06,250 Well, why is that in there? 238 00:11:06,250 --> 00:11:09,080 Pi is a constant, x is the identity. 239 00:11:09,080 --> 00:11:12,750 So the sum is a function that's in F18. 240 00:11:12,750 --> 00:11:16,490 And then if I compose that function with sine, 241 00:11:16,490 --> 00:11:18,870 I get sine of x plus pi, which is cosine x. 242 00:11:18,870 --> 00:11:20,290 So cosine x is there. 243 00:11:20,290 --> 00:11:23,190 Now, this was actually pointed out to me 244 00:11:23,190 --> 00:11:25,960 by students, this simple way of getting cosine x. 245 00:11:25,960 --> 00:11:27,830 The original way that I thought, and I 246 00:11:27,830 --> 00:11:30,200 was using that square root operation where 247 00:11:30,200 --> 00:11:32,340 I was going to use the identity that cosine squared 248 00:11:32,340 --> 00:11:34,360 plus sine squared is equal to 1. 249 00:11:34,360 --> 00:11:37,690 So if I take 1 minus sine squared and then take 250 00:11:37,690 --> 00:11:41,600 the square root, that's another way to get cosine x, the point 251 00:11:41,600 --> 00:11:44,930 being that there's a lot of ways to derive 252 00:11:44,930 --> 00:11:48,940 the same function as being in F18 built up 253 00:11:48,940 --> 00:11:51,960 from the operations applied to other functions. 254 00:11:51,960 --> 00:11:52,910 What about log of x? 255 00:11:52,910 --> 00:11:54,290 Let's just close with that. 256 00:11:54,290 --> 00:11:55,370 How do I get log of x? 257 00:11:55,370 --> 00:11:58,200 Well, log of x is the inverse of e of the x. 258 00:11:58,200 --> 00:11:59,540 How do I get e to the x? 259 00:11:59,540 --> 00:12:03,535 Well, e to the x is what you get by taking 2 260 00:12:03,535 --> 00:12:08,470 to the log to the base 2 of e, which is e, 261 00:12:08,470 --> 00:12:14,310 and then raising that to the power x. 262 00:12:14,310 --> 00:12:18,903 So if I take log e, which is a constant log to the base 2 263 00:12:18,903 --> 00:12:20,570 of e, which is a constant, I multiply it 264 00:12:20,570 --> 00:12:24,430 by x the identity function and I take 2 to that power, 265 00:12:24,430 --> 00:12:28,835 I'm composing, in other words, x log x with the constructor 2 266 00:12:28,835 --> 00:12:32,790 to the F, then I wind up with the function e to the x. 267 00:12:32,790 --> 00:12:38,940 And when I take its inverse, I get log of x, as claimed.