Hi everyone. Today we are going to learn about names. Identifiers are names. What are they? The detailed explanation is available in this textbook, Chapter 7: Identifiers. Please read Chapter 7. Before diving into the formal definitions and implementations and semantics of identifiers, let's play some game. We're going to have several questions like are the following two programs equivalent game. Yeah, it's a game. It's a lot of games actually. Let's see. Are they equivalent? On the left we have a function named f, on the right we have another function named f. They both have only one parameter. On the left the parameter's name is x, on the right the parameter's name is y, and their types are int. Their return types are int, and the body says what? Add 1 to the parameter and then call the function with the same argument. What do you think? Are they the same or not? It's easy. Yeah, it's easy. They are the same. Parameter is consistently renamed. Even though they have different names of parameters they are named in the same way, so they are just the same. This was the first quiz, so I spent some time with this example but later I'm going to be quicker and you'd better pay attention. How about these two? On the left we have function f on the right we have function f. Their parameters are both x of type int. So far so good. How about the body? On the left we have x plus 1, adding 1 to the parameter. On the right we have y plus 1. What is y? They are different because their uses of parameters are no more in the same way. So they are different. Are you with me? So we are playing these games to have some high level idea about what names are? What do they do in programs? When do we say two names are equivalent or not? Let's see more examples. Now on the left we have the same function, that's good. On the right we have another function f of one parameter, but the parameter's name is z this time. In the body, we use y, which is the same with on the left. Actually, on the left, we have a parameter x and the body uses y. On the right, we have parameter z and body uses y. What is y? I don't know. This maybe does some excerpt, some good fraction of a big program and we may have more definitions there like defining y somewhere here. Who knows? I don't know. But if we just look at those two function definitions, they have the same name, their parameters look differently but in the body, the body are the same. What do you think? Are they the same or not? They're the same. Why? Even though they have different parameter name? Because those parameters are not used. Parameter never used. So almost any name is okay. So those two functions are equivalent. So far, so good? Yeah? Let's play one more time. How about this? We now have the same function again. The parameter is x and the body is y. On the right, body is y but the parameter is y? Are they the same? What do you think? Before listening to the answer in a minute think by yourself. What do you think? This body y is nothing with the parameter, but this time, this y is the parameter. Can you guess? They're different. Because even though the body are exactly the same, they both use y. On the left, y is somewhere else, but on the right y is the parameter. So they are different. Good. How about this? The parameters are the same. The bodies are different. But on the left we have y, on the right we have z. They are similar in the sense that we don't know what y and z are. But what? They are different. Because still y and z are undefined identifiers, but they are different ones. We don't know whether they're the same or not. What's next? Slightly more complex examples. But don't worry, let's spend a bit more time on this example. On the left, this is a function f and we have a body with three expressions. It says, Hey, y's value is 10. Remember that y's value is 10 and we have parameter x. When we have this function f being called, then we're going to have some value for the parameter. The result of this function call is going to be x plus y. If we call function with zero, then zero is going to be bound to x. X's value is going to be 0, y's value is 10, so x plus y is 10. How about on the right? On the right, we have another function f, whose parameter is z, but the others are just the same. You've heard the same thing I said in previous slides. What do you expect? They are just the same because only difference is our parameter names and they are renamed consistently. The parameter name on the left is x, the parameter name on the right is z and when x is being used on the left, z is used on the right, so they are consistent. These two functions are just the same. How about this? How are they different? This time in the body we have local identifier y. On the right, we have our local identifier z and it's being used in a similar way. Can you see that? What do you think then? Are they equivalent or not? They're just equivalent, just like parameters. Remember in the previous slide, two functions were the same except for parameter names, but the pattern names were used consistently. In this example, two functions are exactly the same, except the names of local identifiers. Those local identifiers are used consistently. They're just the same. Just like parameter names and local identifiers. Are you getting ideas? These examples are supposed to help you understand how names are declared, how names are introduced in programs and used. What about this? This time on the left, we have two names, x and y. On the right, we have only one name x. X is everywhere. [Korean] There're multiple x's. [Korean] But they aren't all same.
We have this x 1, 2, 3, 4. Are they all the same? On the left, we have these x and these y. These two are the same. But here we have four x's. Who are what? Are they the same or not? What do you think? They are different? Because local identifiers now hide the parameter, meaning that here we introduced name x as a parameter, and then we introduce another name x here. They are named the same, but they mean different things. [Korean] Names are the same somehow. [Korean] But they mean different things.
[Korean] Just like different people with the same name.
This is the parameter name. This is a local identifier name happened to be the same. In this case, local identifier hides the parameter. Meaning that this one removes this. [Korean] It's hiding. In this area, I am the king. [Korean] I'm the king. [Korean] I'm hiding.
Here, x's parameter's scope is that, but local identifiers scope is this and this one is tighter. In this body, these two x's refer to this x. Okay, one more time. These parameters scope is that and this local identifiers scope is this and this one is closer to this one. This one is more powerful than this one. This one wins over that and these two x's refer to these local identifier instead of this parameter. If you don't get it yet, don't worry we're going to talk about that more in prose and more in code. How about this? This time will be named x to y. But what, just it ain't same. You can see that now. Y scope is this and this local identifiers scope is that and this one is closer to this. That's it. We are going to talk about scope not in the next lecture but after that. We're going to talk about scoping, don't worry. I will explain that in detail later. But at the moment, you do better think like, they are names and they have areas that they have power and those power region may be nested, and inner one wins over the outer one. [Korean] Inner one wins over the outer one. [Korean] So it hides the outer one.
[Korean] I'll explain the detail later. Again, they are different because on the left we have two different names but on the right we have only one name and this one is being hidden. [Korean] It loses its power. All these games were designed to explain some concepts about names. Names are officially called identifier's in programming languages. [Korean] They are just names. [Korean] There's poetry.
[Korean] "When I called their name, they came to me and became a flower."
[Korean] Google it.
[Korean] One gains meaning when they get a name.
[Korean] Without having a name, they're just a stranger. When we give names to something, that means something to me. When we give names to variables, names to functions, then that means that we're going to use them multiple times. We are going to use them later. That's why we give them names. In the programming language community, we call names identifiers. Identifiers may be introduced and used. As I said before, whenever we have some new concepts in this course, the concept is going to be introduced and then it's going to be used. Here, identifiers are introduced as parameters or local identifiers. There could be many more other language features that introduce names. But in this lecture, we're going to focus on function parameters and local identifiers. [Korean] Language constructs introducing names vary [Korean] depending on the programming language.
[Korean] You'll learn more later.
[Korean] To avoid complexity, we consider only
[Korean] function parameters and local identifiers here. We are going to say that here we have two places to introduce identifier's, function parameters, parameter names, and local identifier names. As I said, we can introduce names, declare names. Hey, welcome to these names x and y. These names are a and c, they are called binding occurrence. We call that binding occurrence on identifier for the primary of a function or the name of a local identifier is a binding occurrence. We are binding some value and a name. We introduce this x and y as function parameters and these names are introduced here. These are binding occurrences. When we call this function at your argument value is going to be bound to this, but this place when we define a function, we introduce two names, x and y. In a local body of a function definition, we can introduce local identifiers like this, and their names are a and c, these are binding occurrence. When we introduce identifiers, they are binding occurrences. Then if we introduce the names, what do you expect? We delete it, use them. When we use introduce the names, we call that bound occurrence. This time bound occurrence, or use of a function parameter or a local identifier. A function parameter or a local identifier this names or use of those names is a bound occurrence. Do you see that previously it was binding occurrence, but this time it's going to be bound occurrence. Introduction is a binding occurrence, use is a bound occurrence. Let's look at this, this function f has two parameters x and y. these are binding occurrence, and those names are being used here, they are bound occurrence. Previously, we said that these two occurrences, when we introduce names a and c these are binding occurrences. Because we can call them by name now, we know their names, a and c we can use them like this, these are bound occurrence. Are you with me? Binding and bound. Finally, there's one more kind. Free identifier. What's that? It's free. What's that? It sounds good. Free, meaning that it's an error. Why? Because a use of an identifier that is not a function parameter or a local identifier is a free identifier. Meaning that some name is being used, but the name is not defined. Some name is been used but no one ever introduced the name, so it's free. [Korean] No owner. [Korean] No one has introduced it.
[Korean] It's a free soul.
[Korean] What is it? Who is it?
Let's see, in this function definition f, we know the parameter names are x and y. We know that when we call this function, we're going to get values of x and y. When you use x and y, it's good, it's okay because we're going to get their values later. But what is z? Who are you? I don't know. You are not function parameters, you are not local identifiers, I don't know what z is, meaning that I don't know its value. When we add two argument values with z, I don't know what to do, error. Similarly in this function body, we know that a is value is 3, c values 4, and now that we'd like to add three numbers here, a plus b plus c, we'd like to add 3 plus 4 plus b. What is that? I don't know. It's a free identifier, so I didn't know its value, it's an error. When you have a program, they're going to be some names, then you can identify binding occurrences, bound occurrences and the remaining things are free. This is it for this lecture. Let's look at the quiz to see whether you understood what I just said. Let's look at this code. Consider the following code. The function's name is doit. What am I supposed to do? It has a parameter x. What? Yeah, binding, bound, free, what is it? That's the question. Which of the following is a free identifier? Oh, yeah, there four names x, a, b, c, we have x, a, b, and c. Which of the following is a free identifier? Take your time stuff here, and think, and convey. What's the answer. I will show you the answer c. Why? Look at this, x, a, and b are what? Function parameter, local identifier declarations, they're binding occurrences. This one is that, this one is that, and this one is that. All those blue occurrences are bound occurrences. We got all the binding occurrences, we got all the bound occurrences then what's left? This weard thing, that's a free identifier, which makes this function definition error. Okay, enjoy.