You've already seen that local lets you have definitions that are available
inside the local. But what does that mean exactly?
Are they available outside? What if there's a definition outside the
local and the inside the local that has the same name?
What if there's a Local inside a Local, or even one more Local inside of that?
In this video, I'm going to talk about the concept of lexical scoping, and show
how scope contours will let you answer any such question about Local.
In this video, I'm going to talk more precisely about what it means to say that
Local sets up some definitions inside the local expression.
To do that I'm going to introduce an important concept in programming called
lexical scoping. Let me set up a little problem here for
us to think about. Let me get rid of that local expression
and I'll say define t is incendio. Now the question is: If I run this
program what am I going to get? Am I going to get accio port key or
incendio port key? I'll put a space there.
Now that's not key to the problem. Which of the two am I going to get?
Well, I'm going to get Accio port key, which is the one I want of course, but
the question is why? And in order to talk about that, I'm
going to introduce this concept of lexical scoping.
And scope contours. So first thing to understand is that in a
program there's a thing called the top level scope, the global scope, the scope
of the whole program. So the way to think about that is that
there's a box kind of around this whole program, and every definition in that box
is recorded in the box. So in this global or top-level scope, we
have recorded the fact that there's a definition for P.
So there's a little rotation here that says, hey there's a P in this box.
Then whenever we encounter a local, we make another box just around the local.
And every definition that's in the local gets set up in that box, so whereas the
top levels go up, the top level box just has p, this inner scope, this inner box
has p and fetch. Okay.
So that's the first half of Lexical scoping.
The second half is what do we do whenever we see a reference to a constant or a
function. Well, here's what we do, it's easy.
We'd start right at the reference, and we'd go to the innermost enclosing box.
And we'd see if that innermost enclosing box has a definition for that reference.
So starting at this p. We go to the innermost enclosing box
which is the box of that local and we say hey is P defined here?
The answer is yes, it is. And that's it.
We're done. We know which P this is.
It's the P that we find in the innermost enclosing box that defines it So this
thing here is the one from this box so it's accio.
Okay, now, let's look at a little bit more complicated case.
I'm just going to paste it in here. There's a little bit more complicated
case. Well, what's this going to produce?
Before we run it, let's try to use the scope contours to figure it out.
Meaning the global scope there's going to be two definitions a and b, and it's
these definitions here where a is 1 and b is 2.
And way down inside this local, there's going to be one definition for b.
In this definition, b is 3. So now, let's go look at all the
references. What are they each going to be?
Well, this first a, we start at the reference a and we go to the nearest in
closing box, which in this case is the global scope and we say is there an a
there? Yes there is, there's an a here and so
that I would, would go to this a here, let's just draw a little arrow that says
if that a is that a. That reference to a goes to this
definition of a. Okay, now, let's find the next reference
in this program. Well, here it is here after the plus,
it's this a right here. Now, which a is that?
We don't know. Let's go to the nearest enclosing scope
contour. It's this innter box.
Does it define an a? No, it doesn't.
Let's go to the next nearest enclosing box.
It's the outer box, does it define an a? Yes it does.
So this referenced a here goes to the global definition of a there, where the
top level definition of a there. Okay.
What about this b? Well, we do the same thing, we start at
the b, we go to the innermost box, we say, does this box define a b, yes it
does, so this b goes to the inner definition of b.
Let's go to the next reference. Now, let's be careful, this b here, is
outside of the local. So when we go to look at the nearest and
closing box, it's the global scope. So which b did this end up being?
It ends up being the global b. So with these arrows on this, you could
kind of see that, let's see, this is going to be plus 1, also 1, 3 and 2.
1, 1, 3, and 2 is, I think, 7. Sure enough.
Let me suggest that you do some exercises, I'm about to give you some, to
do with the scope contours. And you can kind of check your work
automatically with Dr. Racket but please, please, please,
please, please, please, please, please,please, let me encourage you to
first try to solve these problems on your own before you use this trick I'm about
to show you to check your work. In Dr.
Racket in the intermediate student language when you've got locals If you
click on check syntax, and then you hover over a reference, it will draw a line
from the corresponding definition. If you hover over a definition, it will
draw lines to all the corresponding references.
Okay? So that b is that b.
Right? So you can check your work this way.
It doesn't draw the boxes, it doesn't draw the boxes, but it does draw a line
showing you which a it is. So you can use that to check your work,
but please, please, please practice some of this scope contour stuff without using
that. It's really important to understand the
idea of lexical scoping without using an automatic tool to answer the question for
you.