Given the evaluator, we have in our hands a description (expressed in Lisp) (expressed in Python) of the process by which Lisp expressions Python statements and expressions are evaluated. One advantage of expressing the evaluator as a program is that we can run the program. This gives us, running within Lisp, Python, a working model of how Lisp Python itself evaluates expressions. This can serve as a framework for experimenting with evaluation rules, as we shall do later in this chapter.
Our evaluator program reduces expressions ultimately to the application of built-in procedures. functions. Therefore, all that we need to run the evaluator is to create a mechanism that calls on the underlying Lisp Python system to model the application of built-in procedures. functions.
There must be a binding for each built-in procedure function name and operator, so that when eval evaluate evaluates the operator function expression of an application of a built-in function, it will find an object to pass to apply. We thus set up a global environment built-in environment that associates unique objects with the names of the built-in procedures functions and operators that can appear in the expressions we will be evaluating.
| Original | Python | |
| The global environment also includes bindings for the symbols true and false, so that they can be used as constants in expressions to be evaluated. | The built-in environment also includes bindings for predefined constants such as math_pi. The environment in which the program is evaluated is the global environment, which is the built-in environment extended by a frame that is initially empty. As we saw in section 4.1.3, assignments and function definitions at the top level of the program add their names to this frame. |
| Original | Python |
| (define (setup-environment) (let ((initial-env (extend-environment (primitive-procedure-names) (primitive-procedure-objects) the-empty-environment))) (define-variable! 'true true initial-env) (define-variable! 'false false initial-env) initial-env)) | def setup_environment(): built_in_environment = extend_environment( append(built_in_function_symbols, built_in_constant_symbols), append(built_in_function_objects, built_in_constant_values), the_empty_environment) return extend_environment(None, None, built_in_environment) |
| Original | Python |
| (define the-global-environment (setup-environment)) | the_global_environment = setup_environment() |
Setup-environment The function setup_environment will get the built-in function names and implementation procedures functions from a listlinked list:[1]
| Original | Python |
| (define primitive-procedures (list (list 'car car) (list 'cdr cdr) (list 'cons cons) (list 'null? null?) (list 'display display) (list 'read read) (list '+ +) (list '- -) (list '* *) ;; more primitives )) (define (primitive-procedure-names) (map car primitive-procedures)) (define (primitive-procedure-objects) (map (lambda (proc) (list 'primitive (cadr proc))) primitive-procedures)) | built_in_functions = llist(llist('head', head), llist('tail', tail), llist('pair', pair), llist('is_none', is_none), llist('+', lambda x, y: x + y), $\langle{}$more built-in functions$\rangle$) built_in_function_symbols = \ map(lambda f: head(f), built_in_functions) built_in_function_objects = \ map(lambda f: llist('built_in', head(tail(f))), built_in_functions) |
To apply a built-in procedure, built-in function, we simply apply the implementation procedure function to the arguments, using the underlying Lisp Python system:[2]
| Original | Python |
| (define (apply-primitive-procedure proc args) (apply-in-underlying-scheme (primitive-implementation proc) args)) | def apply_built_in_function(fun, arg_llist): return apply_in_underlying_python(built_in_implementation(fun), arg_llist) |
| Original | Python | |
| For convenience in running the metacircular evaluator, we provide a driver loop that models the read-eval-print loop of the underlying Lisp system. It prints a prompt, reads an input expression, evaluates this expression in the global environment, and prints the result. We precede each printed result by an output prompt so as to distinguish the value of the expression from other output that may be printed.[3] (define input-prompt ";;; M-Eval input:\n") (define output-prompt ";;; M-Eval value:\n") (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (if (null? input) 'EVALUATOR-TERMINATED (let ((output (eval input the-global-environment))) (announce-output output-prompt) (user-print output) (driver-loop))))) (define (prompt-for-input string) (newline) (display string)) (define (announce-output string) (newline) (display string)) | For convenience in running the metacircular evaluator, we provide a driver loop that repeatedly reads and evaluates programs. It prints a prompt and reads an input program as a string. It transforms the program string into a tagged-linked-list representation of the statement as described in section 4.1.2—a process called parsing and accomplished by the built-in function parse. As when Python runs a program, the only output is what the program prints. The driver loop gets the global environment as argument and evaluates each program with respect to it. Names that the program assigns or defines are added to the global environment and are thus available to the programs that are read in later iterations of the driver loop. input_prompt = 'M-evaluate input: ' def driver_loop(env): input = user_read(input_prompt) if input == '': print('evaluator terminated') else: program = parse(input) evaluate(program, env) return driver_loop(env) |
| Original | Python | |
| We use Python's input function to request and read the input string from the user: def user_read(prompt_string): return input(prompt_string) |
| Original | Python | |
| We use a special printing procedure user-print, to avoid printing the environment part of a compound procedure, which may be a very long list (or may even contain cycles). (define (user-print object) (if (compound-procedure? object) (display (list 'compound-procedure (procedure-parameters object) (procedure-body object) '<-procedure-env->)) (display object))) |
Now all we need to do to run the evaluator is to initialize the global environment and start the driver loop. Here is a sample interaction:
| Original | Python |
| (define the-global-environment (setup-environment)) (driver-loop) | the_global_environment = setup_environment() driver_loop(the_global_environment) |
| Original | Python |
| ;;; M-Eval input: (define (append x y) (if (null? x) y (cons (car x) (append (cdr x) y)))) ;;; M-Eval value: ok | M-evaluate input: def append(xs, ys): if is_none(xs): return ys else: return pair(head(xs), append(tail(xs), ys)) |
| Original | Python |
| ;;; M-Eval input: (append '(a b c) '(d e f)) ;;; M-Eval value: (a b c d e f) | M-evaluate input: print(append(llist('a', 'b', 'c'), llist('d', 'e', 'f'))) ['a', ['b', ['c', ['d', ['e', ['f', None]]]]]] |
| Original | Python | |
| Eva Lu Ator and Louis Reasoner are each experimenting with the metacircular evaluator. Eva types in the definition of map, and runs some test programs that use it. They work fine. Louis, in contrast, has installed the system version of map as a primitive for the metacircular evaluator. When he tries it, things go terribly wrong. Explain why Louis's map fails even though Eva's works. | Eva Lu Ator and Louis Reasoner are each experimenting with the metacircular evaluator. Eva types in the definition of map, and runs some test programs that use it. They work fine. Louis, in contrast, has installed the underlying version of map as a built-in function for the metacircular evaluator. When he tries it, things go terribly wrong. Explain why Louis's map fails even though Eva's works. |
| Original | Python | |
| Apply-in-underlying-scheme is the apply procedure we have used in earlier chapters. The metacircular evaluator's apply procedure (section 4.1.1) models the working of this primitive. Having two different things called apply leads to a technical problem in running the metacircular evaluator, because defining the metacircular evaluator's apply will mask the definition of the primitive. One way around this is to rename the metacircular apply to avoid conflict with the name of the primitive procedure. We have assumed instead that we have saved a reference to the underlying apply by doing (define apply-in-underlying-scheme apply) before defining the metacircular apply. This allows us to access the original version of apply under a different name. | The function apply_in_underlying_python is a built-in function of the language that applies a given function to the elements of a given linked list, which serve as the arguments. We also made use of apply_in_underlying_python to define the function apply_generic in section 2.4.3. |