1 00:00:06,010 --> 00:00:09,727 In the last video, you saw the type comments for an arbitrary arity tree, and 2 00:00:09,727 --> 00:00:14,320 you saw how the mutual reference works in those type comments. 3 00:00:15,370 --> 00:00:19,213 Because of the two cycles, the tree can be arbitrary wide at any point, and it 4 00:00:19,213 --> 00:00:25,966 can also be arbitrarily deep. You have now seen the most important part 5 00:00:25,966 --> 00:00:30,682 about arbitrary arity trees. In this video, we're going to do the 6 00:00:30,682 --> 00:00:34,360 templates, and there won't be any new template rules. 7 00:00:34,360 --> 00:00:36,630 The templates are just going to fall out from what we already know. 8 00:00:38,410 --> 00:00:43,534 So, now I'm back in fs starter.racket. And again, we've got the two types which 9 00:00:43,534 --> 00:00:47,920 have mutual recursion, and we're going to do the templates. 10 00:00:47,920 --> 00:00:51,504 And the interesting thing we're going to see about the templates is, we already 11 00:00:51,504 --> 00:00:55,741 know how to do them. Just a reminder, when we have types that 12 00:00:55,741 --> 00:01:00,061 involve mutual recursion, what I do is put each type and interpretation 13 00:01:00,061 --> 00:01:03,582 together. So, there's element and its 14 00:01:03,582 --> 00:01:07,292 interpretation, list of element and its interpretation. 15 00:01:07,292 --> 00:01:10,712 Then I put all the examples, and now I'm going to put all of the templates 16 00:01:10,712 --> 00:01:14,700 together. And that's going to be much easier for us 17 00:01:14,700 --> 00:01:19,980 when we go to design the functions, and it also helps us see the mutual reference 18 00:01:19,980 --> 00:01:26,114 structure better. So, here we go, there's going to be two 19 00:01:26,114 --> 00:01:31,920 templates, because there's two mutually recursive types. 20 00:01:31,920 --> 00:01:36,003 So, there's going to be that template and that template. 21 00:01:36,003 --> 00:01:41,171 And let's see, notice I'm not putting the template rules used, because you're not 22 00:01:41,171 --> 00:01:47,331 required to do that anymore, but I'll talk about them as we do it. 23 00:01:47,331 --> 00:01:58,059 This is a compound data, so, there's all three selectors. 24 00:01:58,059 --> 00:02:06,507 And then I'm going to decorate that with the types of the value that comes back 25 00:02:06,507 --> 00:02:14,340 from the selector. So, in the case of main, it's a string. 26 00:02:14,340 --> 00:02:20,720 In the case of data, it's an integer. And in the case of subs, it's a 27 00:02:20,720 --> 00:02:27,204 ListOfElement. Now, ListOfElement is a non-primitive 28 00:02:27,204 --> 00:02:32,305 type, it's a type that we define. So, the reference rule here says that we 29 00:02:32,305 --> 00:02:37,270 should wrap this selector into a call for fn-for-loe loe. 30 00:02:37,270 --> 00:02:40,739 And I'm going to go ahead and do that, like that. 31 00:02:42,490 --> 00:02:48,985 Now, we're done with fun for element. For fun for loe, well, let's see, this is 32 00:02:48,985 --> 00:02:56,409 one of the two cases. So, there's a con with the two cases, 33 00:02:56,409 --> 00:03:05,898 first case is [UNKNOWN] distinct empty. Second case is the second case so, we'll 34 00:03:05,898 --> 00:03:13,152 say else, then it's a compound cons, first loe, rest loe, second we'll just 35 00:03:13,152 --> 00:03:21,389 decorate those. Well, this is an element, and this is a 36 00:03:21,389 --> 00:03:30,022 ListOfElement. Reference here says that we'll wrap this 37 00:03:30,022 --> 00:03:44,442 in fn-for-element, and the self reference rule here says we'll go fn-for-loe. 38 00:03:44,442 --> 00:03:54,110 So, there we go, we've go the template for both types. 39 00:03:54,110 --> 00:03:56,420 And we didn't really need a new template rule. 40 00:03:56,420 --> 00:04:00,190 The reference rule and the self reference rule, just work to produce the right 41 00:04:00,190 --> 00:04:03,305 answer. In some sense, the only new rule for 42 00:04:03,305 --> 00:04:07,465 templating types involving mutual reference, is to do both templates at the 43 00:04:07,465 --> 00:04:11,814 same time. That'll help you see the mutual recursion 44 00:04:11,814 --> 00:04:16,574 structure in the templates corresponding to the mutual reference structure in the 45 00:04:16,574 --> 00:04:19,886 types. And just to draw that out for you, let me 46 00:04:19,886 --> 00:04:23,006 switch again to this kind of figure that we've seen before, where I've got both 47 00:04:23,006 --> 00:04:28,529 the type comments and the templates. And you could see where there's a self 48 00:04:28,529 --> 00:04:33,980 reference, there's a natural recursion, where there's a mutual reference cycle, 49 00:04:33,980 --> 00:04:41,384 there is a natural mutual recursion. So fn-for-loe calls fn-for-element, and 50 00:04:41,384 --> 00:04:46,790 fn-for-element calls fn-for-loe. In the next video, we'll see how that 51 00:04:46,790 --> 00:04:50,540 plays out as we're designing the functions.