Definitions Definitions - SICP Comparison Edition"/>
| Original | Python | |
|
Our environment model of evaluation and our metacircular evaluator execute definitions in sequence, extending the environment frame one definition at a time. This is particularly convenient for interactive program development, in which the programmer needs to freely mix the application of procedures definition procedures. However, if we think carefully about the internal definitions used to implement block structure (introduced in section 1.1.8), we will find that name-by-name extension of the environment may not be the best way to define local variables. Consider a procedure with internal definitions, such as (define (f x) (define (even? n) (if (= n 0) true (odd? (- n 1)))) (define (odd? n) (if (= n 0) false (even? (- n 1)))) $\langle$ rest of body of$\rangle$ f) Our intention here is that the name odd? in the body of the procedure even? should refer to the procedure odd? that is defined after even?. The scope of the name odd? is the entire body of f, not just the portion of the body of f starting at the point where the define for odd? occurs. Indeed, when we consider that odd? is itself defined in terms of even?—so that even? and odd? are mutually recursive procedures—we see that the only satisfactory interpretation of the two defines is to regard them as if the names even? and odd? were being added to the environment simultaneously. More generally, in block structure, the scope of a local name is the entire procedure body in which the define is evaluated.
As it happens, our interpreter will evaluate calls to
f correctly, but for an
There is, however, a simple way to treat definitions
so that internally defined names have truly simultaneous
scope—just create all local variables that will be in the
current environment before evaluating any of the value expressions.
One way to do this is by a syntax transformation on
lambda expressions. Before evaluating the
body of a lambda expression, we
An alternative strategy for scanning out internal definitions is shown in exercise . Unlike the transformation shown above, this enforces the restriction that the defined variables' values can be evaluated without using any of the variables' values.[2] |
In Python, a name is declared in the body of a function by an assignment or by a function definition. As we saw in sections 1.1.8 and 3.2.4, the scope of such a name is the entire body of the function, not just the portion of the body starting at the point where the declaration occurs. This section takes a closer look at how our evaluator implements this scope, and at an alternative design. Recall the pair of mutually recursive functions is_even and is_odd from section 3.2.4, defined locally in the body of a function f. def f(x): def is_even(n): return True if n == 0 else is_odd(n - 1) def is_odd(n): return False if n == 0 else is_even(n - 1) return is_even(x) The name is_odd in the body of the function is_even refers to the function is_odd that is defined after is_even: The only satisfactory interpretation of the two definitions is to regard them as if the names is_even and is_odd were being added to the environment simultaneously. The metacircular evaluator of section 4.1.1 achieves such a simultaneous scope for local names by scanning out the local names of the function body when the function is defined (see local_variables), and by extending the environment of the function, when the function is applied, with a single frame containing bindings for the parameters and for all local names before evaluating the body. The local names are initially bound to the value '*unassigned*'. Thus the new environment in which the function body is evaluated already contains bindings for is_even and is_odd, and any occurrence of one of these names refers to the correct binding. Once their definitions are evaluated, these names are assigned their function objects, which have the extended environment as their environment part. Thus, for example, by the time is_even gets applied in the body of f, its environment already contains the correct binding for the symbol is_odd, and the evaluation of the name is_odd in the body of is_even retrieves the correct value.
There is currently no solution available for this exercise. This textbook adaptation is a community effort. Do consider contributing by providing a solution for this exercise, using a Pull Request in Github.
Why can't the evaluator take care of this chore, and hoist all function definitions to the beginning of the function body in which they appear? Function definitions outside of function bodies should be hoisted to the beginning of the program.
There is currently no solution available for this exercise. This textbook adaptation is a community effort. Do consider contributing by providing a solution for this exercise, using a Pull Request in Github.
|
| Original | Python | |
| Original | Python | |
|
The design of our evaluator of section 4.1.1 imposes a burden on the definition and the application of functions: The definition of a function needs to scan its body for local names, and each application needs to extend the environment of the function with a frame that binds the parameters and those names, and evaluate the body in this extended environment. Alternatively, the application of a function could extend the environment of the function with a frame that binds only the parameters. The evaluation of each assignment and each function definition in the body would then add a new binding to that frame, unless the frame already binds the name. With this design, a function definition no longer needs to scan the body of the function for local names. For now, we ignore global and nonlocal declarations; exercise 4.16 takes care of them. To implement the design, we simplify apply: def apply(fun, arguments): if is_built_in_function(fun): return apply_built_in_function(fun, arguments) elif is_compound_function(fun): result = evaluate(function_body(fun), extend_environment(function_parameters(fun), arguments, function_environment(fun))) if is_return_value(result): return return_value_content(result) else: return None else: return error('unknown function type -- apply', fun) The function assign_symbol_value can no longer assume that the environment already has a binding for the name, and it no longer searches the enclosing frames for a binding to change. Instead, an assignment always concerns the first frame of the environment, which is the frame of the current function application. If that frame binds the name, the binding gets the new value. Otherwise, the function add_binding_to_frame adds a binding of the name to the new value to the first frame. The same function works at the top level of a program, where the first frame is the global frame, and we no longer need to test for the global environment. def assign_symbol_value(symbol, val, env): def scan(symbols, vals): if is_none(symbols): add_binding_to_frame(symbol, val, first_frame(env)) elif symbol == head(symbols): set_head(vals, val) else: scan(tail(symbols), tail(vals)) frame = first_frame(env) scan(frame_symbols(frame), frame_values(frame)) The function lookup_symbol_value remains unchanged: it still searches the enclosing frames. As a consequence, an assignment in a function body creates a new local binding even if an enclosing frame already binds the name. This is correct for assignments to names that the function does not declare as global or nonlocal. |
| Original | Python | |
| Original | Python | |
|
With sequential definition processing, the scope of a local name is
no longer the entire body of the function, but rather just the portion
of the evaluation starting at the point where
the name is declared: Before that point, the name
refers to a binding in an enclosing frame, if there is one.
Although we no longer have simultaneous scope, sequential
definition processing
will evaluate calls to the function
f at the beginning of this section
correctly, but for an
Sequential definition processing is more efficient than scanning out names, and, once extended as in exercise 4.16, also easier to implement. However, with sequential processing, the binding to which a name refers may depend on the order in which the statements in the body of a function are evaluated. In exercise 4.15, we see that views may differ as to whether that is desirable. There is currently no solution available for this exercise. This textbook adaptation is a community effort. Do consider contributing by providing a solution for this exercise, using a Pull Request in Github.
There is currently no solution available for this exercise. This textbook adaptation is a community effort. Do consider contributing by providing a solution for this exercise, using a Pull Request in Github.
|
| Original | Python | |
management is not responsibleremark in footnote 4 of chapter 1. By insisting that internal definitions come first and do not use each other while the definitions are being evaluated, the IEEE standard for Scheme leaves implementors some choice in the mechanism used to evaluate these definitions. The choice of one evaluation rule rather than another here may seem like a small issue, affecting only the interpretation of
badly formedprograms. However, we will see in section that moving to a model of simultaneous scoping for internal definitions avoids some nasty difficulties that would otherwise arise in implementing a compiler.
pure $\lambda$-calculusimplementation of recursion. (See Stoy 1977 for details on the lambda calculus, and Gabriel 1988 for an exposition of the $Y$ operator in the language Scheme.)