======================================== | Example | Board 2 | Announcements | ======================================== | Subst. | Board 5 | Env Diagram | ======================================== | Free vbls. | Board 8 | Example Env. | ======================================== Note: Write Announcements and Example before class starts! SLIDE 1 0. ON BOARD 1: Example (define make-adder (lambda (x) (lambda (y) (+ x y)))) (define add-2 (make-adder 2)) (define add-3 (make-adder 3)) ON BOARD 3: Quiz, Wed. March 19, 5-7pm xor 7-9pm J++ soon; start looking on your own 1. Introduction, 5 mins., 10:05 - 10:10 Three ways to understand names in Scheme a) Mathematical: Quantifiers, Free Variables, Bound Variables b) PASCAL: Frames and Block Structure c) Implementation: Environments and Procedures SLIDE 2 Mantras: 1. Every expression* has a value. *: Except for errors, infinite loops, and the define special form. 2. To find the value of a combination, a. Find the values of all of the subexpressions, in any order * b. Apply the value of the first to the values of the rest * 3. The value of a lambda expression is a procedure. 4. The value of a quote expression is the thing quoted. SLIDE 3 2. Today's Example, 5 mins., 10:10 - 10:15 ADD TO BOARD 1 (add-2 5) ; ==> 7 (add-3 5) ; ==> 8 (add-2 12) ; ==> 14 ON BOARD 4 Substitution model yields (define add-2 (lambda (y) (+ 2 y)) (define add-3 (lambda (y) (+ 3 y)) ((lambda (y) (+ 2 y)) 5) --> (+ 2 5) ==> 7 ((lambda (y) (+ 3 y)) 5) --> (+ 3 5) ==> 8 ((lambda (y) (+ 2 y)) 14) --> (+ 2 14) ==> 16 3. Mathematics, 5 mins., 10:15 - 10:20 ON BOARD 7 Start with (+ x y): a. There are three variables b. They must be defined or we get an error c. The value of the expression depends on their values Contrast this with (lambda (x) x) a. There is one variable b. It doesn't matter if we've defined it SLIDE 4 LAMBDA is able to give values to names for a specified duration. It "controls" some variables -- its parameters -- inside its body. This makes it a QUANTIFIER, the SCOPE of the lambda is its BODY, and the BOUND VARIABLES of the lambda are its PARAMETERS. We say that the lambda expression BINDS its parameters to the arguments with which it is called. Other quantifiers from mathematics are summation, integration, and differentiation. The important thing to notice about quantifiers is that, with a certain technical restriction, you can rename the controlled variables to anything you like as long as you do it uniformly. Variables that aren't controlled by a quantifier are said to be "free variables." Our example has only one free variable: + Notice: it isn't a coincidence that in the substitution model the variables that disappear when you substitute into the body of a LAMBDA are precisely the variables that the lambda binds! SLIDE 5 The slide shows the two LAMBDA expressions in our example. 4. Scope Lines, 3 mins., 10:20 - 10:23 a. Scheme, like PASCAL, C, and Java, is BLOCK STRUCTURED. That means that there are parts of the syntax of the language that introduce new variables, and those variables have the given value only within the visible program text. In Scheme, LAMBDA and LET create blocks. SLIDE 6 ON BOARD 1 b. Draw the scope lines, one for each LAMBDA, then show the names introduced with each (define make-adder (lambda (x) (lambda (y) (+ x y)))) c. Scheme is LEXICALLY SCOPED. This means that at any point in a program, the variables that can be used can be determined by the nesting of the scope blocks. d. Show the lookup pattern for all variables: +, x, and y. Point out that the lookup stops at the nearest quantifier for the given name. Admit handwaving over DEFINE -- both internal and top-level. 5. Environment Model, 10 mins., 10:23 - 10:33 a. How do the next two lines in our example work? (define add-2 (make-adder 2)) (define add-3 (make-adder 3)) We need to make two procedures, each of which has the code to evaluate (lambda (y) (+ x y)), but with different values of X. b. Substitution model would require us to copy the entire thing over. That's works now, but not next week. Besides, it is inefficient for large procedures. c. Let's just remember what we were supposed to substitute, and do the actual substitution only when we want to find the values of the variables! d. We need a data structure to remember what we want to substitute. We've seen two dictionaries before (my algebra language, Prof. Grimson's pattern matcher). A FRAME contains the names of variables and their corresponding values. BOARD 6 (leave lots of room above and below) /============\ /============\ | X = 2 | | X = 3 | \============/ \============/ e. We want to make the frames and the scope lines match up, so we need to create a chain of frames, from innermost to outermost. An ENVIRONMENT is a frame with a pointer to a parent frame. /============\ /============\ | X = 2 | | X = 3 | \============/ \============/ ^ ^ /|\ /|\ | | /============\ /============\ | Y = 5 | | Y = 5 | \============/ \============/ f. There is a special frame, called THE GLOBAL ENVIRONMENT, that has no parent but has all of the globally defined variables and their values. /===============================\G.E. | + = procedure that adds | | * = procedure that multiplies | | make-adder = procedure ... | \===============================/ ^ ^ /|\ /|\ | | /============\ /============\ | X = 2 | | X = 3 | \============/ \============/ ^ ^ /|\ /|\ | | /============\ /============\ | Y = 5 | | Y = 5 | \============/ \============/ g. How does add-2 "know" it should use the environment on the left while add-3 "knows" it should use the one on the right? i. When we call add-2 or add-3, we give the LAMBDA expression the number 5, so it can construct the bottom frame in each case. ii. But how can it know about X=2 vs X=3? What happened when we evaluated the expression (LAMBDA (Y) (+ X Y)) to create add-2 vs. add-3? iii. We need two important new ideas. 6. New ideas, 5 minutes, 10:33 - 10:38 a. You can't just find the value of an expression. You must *always* specify what environment to use for finding the values of variables in the expression. Show lookup of X and Y in both of the environments. b. A procedure doesn't just have the code it needs to run but also the environment that should be used for the free variables. Draw the double bubble. c. Since "The value of a lambda expression is a procedure," somehow evaluating a LAMBDA must figure out both the code part of the new procedure and the environment part. Lexical scoping says that the environment part must be the environment you are using to evaluate the LAMBDA expression. d. To find the value of a combination, a. Find the values of all of the subexpressions, in any order b. Apply the value of the first to the values of the rest But APPLY must somehow figure out the correct expression and environment to use in place of the original 7. New Rules, 7 minutes, 10:38 - 10:45 SLIDE 7 Rule 1: You must always have both an expression and an environment. Rule 2: Procedures have three parts: parameters, bodies, and an environment. Rule 3: When you apply a procedure to arguments, be sure to use the arguments and all three parts of the procedure. SLIDE 8 a. To find the value of a variable in a given environment, look it up in that environment. b. To compute the value of a LAMBDA expression in a given environment, draw a double-bubble with the left half pointing to the text (parameters + body) and with the right pointing to the given environment. c. To apply a procedure (double bubble) to arguments, i. CREATE a new frame with one entry for each of the parameters to the LAMBDA expression that's in the left half of the bubble. ii. FILL the frame's value slots with the arguments. iii. LINK the frame to the environment that's in the right half of the bubble. The converts the frame to an environment. iv. EVALUATE the body of the LAMBDA expression, using the newly constructed environment to find the values of the variables. SLIDE 9 ON BOARD 9 8. Walk through our example, 5 minutes, 10:45 - 10:50 SLIDE 10 9. Review, 5 minutes, 10:50 - 10:55 Mantras: 1. Every expression* has a value. *: Except for errors, infinite loops, and the define special form. * 2. To find the value of a combination, a. Find the values of all of the subexpressions, in any order b. Apply the value of the first to the values of the rest(1) * 3. The value of a lambda expression is a procedure.(2) 4. The value of a quote expression is the thing quoted. Notes: 1. To apply a compound procedure to arguments use the arguments and all three parts of the procedure (create, fill, link, evaluate). 2. A compound procedure ("double bubble") has two parts: the text and the environment. The text also has two parts: parameters and body.