1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.

Download 1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.

Post on 04-Jan-2016

212 views

Category:

Documents

0 download

TRANSCRIPT

  • *The Evaluator

  • *Compiler vs. InterpreterCommandProcessing Unit The ComputerProgram in Low Level Machine LanguageProgram in High Level LanguageTransformationThe ProgrammerT

  • *A CompilerCPUMachine Level ProgramHigh Level ProgramCInputsOutputsThe Compiler turns the high level program instructions toInstructions understood by the machine.

  • *An InterpreterCPUHigh Level ProgramIInputsOutputsThe Interpreter is a machine level program, which interprets and executes the high level program line after line

  • *The Metacircular EvaluatorCPUSchemeProgramInputsOutputsThe Metacircular Evaluator is an Interpreter for Scheme on a machine whose machine language is Scheme. ME

  • Evaluator *Packages:

    Core (Evaluation Rules)

    Abstract Syntax Parser

    Data Structures

    First, we introduce the environment model for evaluation of expressions!To be implemented in the Core

  • *The Environments ModelEnvironments model: many environments. Generalizes the substitution model.

  • *Frame: a table of bindingsBinding: a pairing of a name and a value

  • *Environment: a sequence of framesEnvironment E1 consists of frames A and Bthis arrow is called the enclosing environment pointerEnvironment E2 consists of frame B onlyA frame may be shared by multiple environments

  • *Evaluation in the environment modelAll evaluations occur in an environment The current environment changes when the interpreter applies a procedure

    The top environment is called the global environment (GE) Only the GE has no enclosing environment

  • *The Environment ModelA precise, completely mechanical, description of:name-rulelooking up the value of a variabledefine-rulecreating a new definition of a varlambda-rulecreating a procedureapplication rule applying a procedure Basis for implementing a scheme interpreter for now: draw EM state with boxes and pointers later on: implement with code Enables analyzing arbitrary scheme code, including (when we get there) imperative programming

  • *Name-ruleA name X evaluated in environment E gives the value of X in the first frame of E where X is boundz | GE ==> 10 z | E1 ==> 10 x | E1 ==> 15

  • *Define-ruleA define special form evaluated in environment E creates or replaces a binding in the first frame of E(define z 25) | E1(define z 20) | GEz: 25

  • *Double bubble: how to draw a procedure

  • *Lambda-ruleA lambda special form evaluated in environment E creates a procedure whose environment pointer points to E

    (define square (lambda (x) (* x x))) | E1environment pointer points to frame A because the lambda was evaluated in E1 and E1 A

  • *To apply a compound procedure P to arguments (Application Rule)1. Create a new frame A2. Make A into an environment E: A's enclosing environment pointer goes to the same frame as the environment pointer of P3. In A, bind the parameters of P to the argument values4. Evaluate the body of P with E as the current environmentWe recommend you memorize these four steps

  • *(square 4) | GEx: 10 GEparameters: x body: (* x x)square:x: 4(* x x) | E1 *: #[prim]==> 16* | E1==> x | E1==> 4square | GE==>

  • *Example: inc-squareGE(define square (lambda (x) (* x x))) | GE(define inc-square (lambda (y) (+ 1 (square y))) | GE

  • *Example cont'd: (inc-square 4) | GEy: 4(+ 1 (square y)) | E1 + | E1==> #[prim]inc-square | GE ==> (square y) | E1

  • *Example cont'd: (inc-square 4) | E1x: 4(* x x) | E2 * | E2 ==> #[prim] x | E2 ==> 4GEp: x b: (* x x)square:inc-square:p: y b: (+ 1 (square y)) | E1y: 4==> 16(+ 1 16) ==> 17y | E1==> 4square | E1==>

  • *Lessons from the inc-square exampleEM doesn't show the complete state of the interpretermissing the stack of pending operations

    The GE contains all primitive bindings (*, cons, etc)omitted from EM drawings

    Useful to link environment pointer of each frame to the procedure that created it (but not mandatory)

    When a call is completed, the frame created for it is no longer accessible, (unless a pointer to it was kept as we will see in the next example), so in effect it disappears.

  • *Why? To have objects with local stateThe state is summarized in a set of variables.When the object changes its state the variable changes its value.

  • *Procedures with local stateSuppose we want to implement counters(define ca (make-counter 0))(ca) ==> (ca) ==> (cb) ==> (ca) ==> (define cb (make-counter 0))1213How can we define such procedures?

  • *A counter: an object with state(define make-counter (lambda (n) (lambda ()(set! n (+ n 1)) n ))) (define ca (make-counter 0))

  • *Can the substitution model explain this behavior?(define make-counter (lambda (n) (lambda () (set! n (+ n 1)) n ))) (define ca (make-counter 0))ca ==> (lambda () (set! 0 (+ 0 1)) 0)(ca) ((set! 0 (+ 0 1)) 0)==> ?The substitution model is not adequate for assignment!

  • *The Environments ModelEnvironments model: many environments. Generalizes the substitution model.

  • *Example: explaining make-counterCounter: an object that counts up starting from some integer (define make-counter (lambda (n) (lambda () (set! n (+ n 1)) n ; returned and retained value ))) (define ca (make-counter 0)) (ca) ==> 1 (ca) ==> 2 (define cb (make-counter 0)) (cb) ==> 1 (ca) ==> 3 (cb) ==> 2

  • *(define ca (make-counter 0)) | GEn: 0(lambda () (set! n (+ n 1)) n) | E1 environment pointer points to E1 because the lambda was evaluated in E1E1 is pointed at so does not disappear!

  • *(ca) | GE(set! n (+ n 1)) | E2 n | E2 ==> 1 E2 Empty(no args) GEp: n b:(lambda () (set! n (+ n 1)) n)make-counter:n: 0ca:==> 1

  • *E3(ca) | GE(set! n (+ n 1)) | E3 GEp: n b:(lambda () (set! n (+ n 1)) n)make-counter:n: 0ca:n | E3 ==> 2 empty==> 2

  • *Example: explaining make-counterCounter: something which counts up from a number (define make-counter (lambda (n) (lambda () (set! n (+ n 1)) n ))) (define ca (make-counter 0)) (ca) ==> 1 (ca) ==> 2 (define cb (make-counter 0)) (cb) ==> 1 (ca) ==> 3 (cb) ==> 2

  • *(define ca (make-counter 0)) | GEn: 0(lambda () (set! n (+ n 1)) n) | E1 environment pointer points to E1 because the lambda was evaluated in E1

  • *(ca) | GE(set! n (+ n 1)) | E2 n | E2 ==> 1 empty==> 1

  • *(ca) | GE(set! n (+ n 1)) | E3 n | E3 ==> 2 empty==> 2

  • *(define cb (make-counter 0)) | GE(lambda () (set! n (+ n 1)) n) | E4 n: 0

  • *(cb) | GE==> 1

  • *Capturing state in local frames & procedures

  • *Lessons from the make-counter exampleEnvironment diagrams get complicated very quicklyRules are meant for the computer to follow, not to help humans

    A lambda inside a procedure body captures the frame that was active when the lambda was evaluatedthis effect can be used to store local state

  • *Explaining Nested DefinitionsNested definitions : block structure

    (define (sqrt x) (define (good-enough? guess) (< (abs (- (square guess) x)) 0.001)) (define (improve guess) (average guess (/ x guess))) (define (sqrt-iter guess) (if (good-enough? guess) guess (sqrt-iter (improve guess)))) (sqrt-iter 1.0))

  • *(sqrt 2) | GEguess: 1sqrt-iterguess: 1good-enou?

  • *message passing example(define (cons x y) (define (dispatch op) (cond ((eq? op 'car) x) ((eq? op 'cdr) y) (else (error "Unknown op -- CONS" op)))) dispatch)

    (define (car x) (x 'car)) (define (cdr x) (x 'cdr)) (define a (cons 1 2)) (car a)

  • *(define a (cons 1 2)) | GEp: x b:(x car)p: x b:(x cdr)car:cdr:a:

  • *(car a) | GEp: x b:(x car)p: x b:(x cdr)car:cdr:E1x: 1y: 2 dispatch:a:(x car) | E2 (cond ..) | E3 ==> 1==> 1

  • *The Environment Evaluation Model To evaluate a combination (a compound expression other than a special form), evaluate the subexpressions and then apply the value of the operator subexpression to the values of the operand subexpressions.To apply a compound procedure to a set of arguments, evaluate the body of the procedure in a new environment. To construct this environment, extend the environment part of the procedure object by a frame in which the formal parameters of the procedure are bound to the arguments to which the procedure is applied.

    **It might seem circular to think about evaluating Scheme programs in Scheme, but the idea is since we have throughout the course used Scheme as our way of describing processes, and the evaluator is a process, then its only natural that we describe it too using Scheme.

    ****As we noted earlier in the course, we need to change models to explain a more complex universe, in the same way that in Physics one needs to abandon the Newtonian model in order to reason effectively about sub-atomic particles. ****This code was modeled on the board. Recall that we explained that this example shows how Schemes static scoping comes to play. By this we mean that even though the procedure dispatch Pointed to by a is called from within the car procedure, our static scoping model that looks for variables in the env the procedure was defined in, causes us to look for x in the env where dispatch was Created, and thus we find the correct variable x to return, the one with value 1.

    **

Recommended

View more >