1 00:00:06,120 --> 00:00:10,540 So far, you've seen the basic intuition behind local expressions and you've seen 2 00:00:10,540 --> 00:00:14,830 how the concepts of lexical scoping and scope contours allow us to know exactly 3 00:00:14,830 --> 00:00:19,380 which definition provides the value for a constant reference or a function call But 4 00:00:19,380 --> 00:00:27,060 we don't yet have a precise model for how Local is evaluated. 5 00:00:27,060 --> 00:00:31,260 And without that model, we can't know the exact value of locally defined constants, 6 00:00:31,260 --> 00:00:35,030 When they appear inside of function definitions. 7 00:00:36,600 --> 00:00:39,700 So what I'm going to do in this video is show you the exact evaluation rules for 8 00:00:39,700 --> 00:00:42,312 Local. And we'll just work through that step by 9 00:00:42,312 --> 00:00:44,964 step. And this is going to be really important 10 00:00:44,964 --> 00:00:48,332 for the way that we use locals, when we get to function abstraction. 11 00:00:48,332 --> 00:00:52,724 In this video, I'm going to talk about the actual evaluation rules for local 12 00:00:52,724 --> 00:00:56,349 expressions. And I'm going to do that because it'll 13 00:00:56,349 --> 00:01:02,160 help you understand some of the ways that we're going to be making use of local in. 14 00:01:02,160 --> 00:01:05,690 Function abstraction a little bit later in the course. 15 00:01:05,690 --> 00:01:08,260 So here we go. I've got another example here. 16 00:01:08,260 --> 00:01:13,170 This one is slightly simpler than the super complicated one we saw last time. 17 00:01:13,170 --> 00:01:16,480 but it's complicated enough to talk about what we need to talk about here. 18 00:01:18,270 --> 00:01:22,755 So, you know, intuitively, we could see what's going on here; that b and that b B 19 00:01:22,755 --> 00:01:27,667 are the outer b. And both of those inner b's are the inner 20 00:01:27,667 --> 00:01:29,899 b. So that's plus 1 times 2 2 is 4. 21 00:01:29,899 --> 00:01:39,090 So plus 1 4 and 1. Pretty sure that's 6. 22 00:01:39,090 --> 00:01:42,572 It is. Well let's talk about how, ISL actually 23 00:01:42,572 --> 00:01:48,675 evaluates the local expression. So the first thing to remember is that 24 00:01:48,675 --> 00:01:53,885 Local is an expression. It doesn't affect the evaulation of 25 00:01:53,885 --> 00:01:59,230 expressions outside of it. All expressions are like that. 26 00:01:59,230 --> 00:02:03,040 So let's start by evaluating this whole expression, open paren plus. 27 00:02:03,040 --> 00:02:05,900 Plus is the name of a primative operation. 28 00:02:05,900 --> 00:02:09,080 So this is a call to a primative operation. 29 00:02:09,080 --> 00:02:13,435 We need to reduce the three operands: this b, this local expression, and that b 30 00:02:13,435 --> 00:02:17,619 to values. We start with the first one? 31 00:02:17,619 --> 00:02:19,846 Is it a value? No it's not. 32 00:02:19,846 --> 00:02:25,596 It's a reference to a defined constant. We happen to be using lower-case names 33 00:02:25,596 --> 00:02:29,857 here, but that's a style guide issue, not a language issue. 34 00:02:29,857 --> 00:02:34,631 So B is going to be replaced by ones, so the first evaluation step is just that b 35 00:02:34,631 --> 00:02:40,680 gets replaced by one. Nothing's happened with the local yet. 36 00:02:40,680 --> 00:02:46,100 We haven't seen it yet. Okay now let's do this local. 37 00:02:46,100 --> 00:02:49,882 Now the way local evaluation works is that three things are going to happen in 38 00:02:49,882 --> 00:02:51,667 one step. See? 39 00:02:51,667 --> 00:02:55,470 There's going to be a thing called renaming. 40 00:02:55,470 --> 00:02:56,730 There's going to be a thing called lifting. 41 00:02:56,730 --> 00:03:01,194 And then we're going to replace the entire local expression with its renamed 42 00:03:01,194 --> 00:03:08,910 body. Let's first talk about the renaming. 43 00:03:08,910 --> 00:03:12,780 The first thing we do is we take every definition in the local. 44 00:03:12,780 --> 00:03:17,400 In this case there's just one, define b. And we find all the references to that 45 00:03:17,400 --> 00:03:21,486 definition, in this case there's these two. 46 00:03:21,486 --> 00:03:26,900 We can either do that ourselves or we can use check syntax to help us. 47 00:03:26,900 --> 00:03:33,011 There's those two definitions. And now we come up with a new, globally 48 00:03:33,011 --> 00:03:37,414 unique name for b. What I'll do is I'll make a copy to work 49 00:03:37,414 --> 00:03:40,015 in. Just to keep it simple, what I'm going to 50 00:03:40,015 --> 00:03:43,423 do is, I'm going to rename b to b underscore 0. 51 00:03:44,710 --> 00:03:50,046 And at the time I do that I'm going to rename all the references to be, to be 52 00:03:50,046 --> 00:03:54,275 _0. Now, if the _0 already existed I would 53 00:03:54,275 --> 00:03:58,120 use something else. The key thing is it has to be a unique 54 00:03:58,120 --> 00:04:01,138 name. There can be no other definition in the 55 00:04:01,138 --> 00:04:03,520 world that has this name. Okay. 56 00:04:03,520 --> 00:04:05,628 Technically there could be no other definition in the top level scope that 57 00:04:05,628 --> 00:04:09,299 has this name. And, technically, the actually, the 58 00:04:09,299 --> 00:04:14,693 renaming works in a more subtle way, but this explanation is good enough to 59 00:04:14,693 --> 00:04:21,690 understand the valuation of any program we're going to write. 60 00:04:22,830 --> 00:04:29,340 Now that I've done the renaming, I do another thing, which is that I take the 61 00:04:29,340 --> 00:04:35,850 renamed definition and I lift it out of the local, so that it's now in the top 62 00:04:35,850 --> 00:04:45,000 level scope. And notice I don't lift it just out of 63 00:04:45,000 --> 00:04:47,470 the local. I didn't put it here. 64 00:04:47,470 --> 00:04:51,595 I did not put it there. I have to lift it all the way to top 65 00:04:51,595 --> 00:04:54,715 level. I lifted it all the way to top level. 66 00:04:54,715 --> 00:04:58,710 If you want you could go put it all the way up here with the other definitions. 67 00:04:58,710 --> 00:05:03,494 You don't have to do that. It really just has to go to top level. 68 00:05:05,650 --> 00:05:07,720 So I did the renaming. I did the lifting. 69 00:05:07,720 --> 00:05:14,415 Now the third step is I replace the local with the body in which the renaming has 70 00:05:14,415 --> 00:05:20,568 happened. And now notice something important. 71 00:05:20,568 --> 00:05:25,175 There's no local anymore. The local is gone. 72 00:05:25,175 --> 00:05:29,900 That's the same kind of thing we saw with if and cod. 73 00:05:29,900 --> 00:05:33,334 If tried to put itself out of the way. It evaluated the question, and then it 74 00:05:33,334 --> 00:05:36,295 replaced itself with either the true answer expression or the false answer 75 00:05:36,295 --> 00:05:40,060 expression. That's what's happening here. 76 00:05:40,060 --> 00:05:44,407 The rules for evaluating local or trying to make the local go away, so that it 77 00:05:44,407 --> 00:05:49,992 will have a program in BSL. This is now a BSL program, it's not an 78 00:05:49,992 --> 00:05:53,520 ISL program anymore. Local is gone. 79 00:05:53,520 --> 00:05:57,360 And so now we just keep evaluating and of course, since it's a BSL program it's 80 00:05:57,360 --> 00:06:01,475 quite straight forward, let's see which B0 is this. 81 00:06:01,475 --> 00:06:05,894 This. Well, it's that b0, so this will become 82 00:06:05,894 --> 00:06:11,510 in the next step, that b0 will become two. 83 00:06:13,670 --> 00:06:18,980 In the step after that, the next b0 will become two. 84 00:06:18,980 --> 00:06:29,250 In the step after that The times 2 2 will become 4. 85 00:06:29,250 --> 00:06:32,270 And the step after that, this b, well which b is that? 86 00:06:32,270 --> 00:06:37,750 That's this one way up at the top here. That's going to become 1. 87 00:06:37,750 --> 00:06:42,092 Whoops, I need to actually do the step. Yeah, that's going to become 1. 88 00:06:42,092 --> 00:06:52,380 And in the final step, that whole thing is going to become six. 89 00:06:52,380 --> 00:06:56,924 The key thing about the step by step evaluation or about the evaluation rules 90 00:06:56,924 --> 00:07:01,184 for local expression is, when you get to the local, first you rename Each 91 00:07:01,184 --> 00:07:07,840 definition and all of the references to a globally unique name. 92 00:07:07,840 --> 00:07:10,780 So if there were multiple definitions here, you would do this for all the 93 00:07:10,780 --> 00:07:16,000 definitions. For this case, we renamed B to B zero. 94 00:07:16,000 --> 00:07:20,697 We renamed the references also to B zero. Then you lift the definition to top 95 00:07:20,697 --> 00:07:22,970 level. There it is. 96 00:07:22,970 --> 00:07:25,170 And then you replace the local by it's body. 97 00:07:25,170 --> 00:07:29,934 So that's the rules for evaluating a local. 98 00:07:29,934 --> 00:07:33,530 Rename, lift, replace the local with it's body. 99 00:07:34,850 --> 00:07:38,555 You should practice that because understanding how we're going to use 100 00:07:38,555 --> 00:07:42,826 local in abstract functions. Is going to require having this 101 00:07:42,826 --> 00:07:45,829 understanding of local. You need to understand how to form a 102 00:07:45,829 --> 00:07:48,078 local. You need to understand the rules of 103 00:07:48,078 --> 00:07:51,406 lexical scoping, the contour rules, and you need to understand the rules for 104 00:07:51,406 --> 00:07:56,134 evaluating a local. Just practice it and do these exercises 105 00:07:56,134 --> 00:07:57,490 coming up.