Download presentation
Presentation is loading. Please wait.
Published byMatilda Curtis Modified over 9 years ago
1
1 The Evaluator
2
2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language Transformation The Programmer T
3
3 A Compiler CPU Machine Level Program High Level Program C Inputs Outputs The Compiler turns the high level program instructions to Instructions understood by the machine.
4
4 An Interpreter CPU High Level Program I Inputs Outputs The Interpreter is a machine level program, which interprets and executes the high level program line after line…
5
5 The Metacircular Evaluator CPU Scheme Program Inputs Outputs The Metacircular Evaluator is an Interpreter for Scheme on a machine whose machine language is Scheme. ME
6
Evaluator 6 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
7
7 The Environments Model Name Value Environment Table 23 score Substitution model: a single global environment Environments model: many environments. Generalizes the substitution model.
8
8 Frame: a table of bindings Binding: a pairing of a name and a value Example: x is bound to 15 in frame A y is bound to (1 2) in frame A the value of the variable x in frame A is 15 2 1 x: 15 A y:
9
9 Environment: a sequence of frames Environment E1 consists of frames A and B z: 10 B E1 E2 x: 15 A 2 1 y: this arrow is called the enclosing environment pointer Environment E2 consists of frame B only A frame may be shared by multiple environments
10
10 Evaluation in the environment model All 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
11
11 The Environment Model A precise, completely mechanical, description of: name-rulelooking up the value of a variable define-rulecreating a new definition of a var lambda-rulecreating a procedure application 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
12
12 Name-rule A name X evaluated in environment E gives the value of X in the first frame of E where X is bound x: 15 A 2 1 z: 10 x: 3 B E1 GE y: In E1, the binding of x in frame A shadows the binding of x in B x | GE ==> 3 z | GE ==> 10 z | E1 ==> 10 x | E1 ==> 15
13
13 Define-rule A 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) | GE z: 25 z: 20 x: 15 A 2 1 z: 10 x: 3 B E1 GE y:
14
14 Double bubble: how to draw a procedure (lambda (x) (* x x)) eval lambda-rule A compound proc that squares its argument #[proc-...] print Environment pointer Code pointer parameters: x body: (* x x)
15
15 Lambda-rule A lambda special form evaluated in environment E creates a procedure whose environment pointer points to E x: 15 A z: 10 x: 3 B E1 parameters: x body: (* x x) square: (define square (lambda (x) (* x x))) | E1 environment pointer points to frame A because the lambda was evaluated in E1 and E1 A Evaluating a lambda actually returns a pointer to the procedure object
16
16 To apply a compound procedure P to arguments (Application Rule) 1. Create a new frame A 2. Make A into an environment E: A's enclosing environment pointer goes to the same frame as the environment pointer of P 3. In A, bind the parameters of P to the argument values 4. Evaluate the body of P with E as the current environment We recommend you memorize these four steps
17
17 (square 4) | GE x: 10 GE parameters: x body: (* x x) square: A E1 x: 4 (* x x) | E1 *: #[prim] ==> 16 * | E1 ==> x | E1 ==> 4 square | GE ==> frame becomes inaccessible
18
18 Example: inc-square GE p: x b: (* x x) square: inc-square: p: y b: (+ 1 (square y)) (define square (lambda (x) (* x x))) | GE (define inc-square (lambda (y) (+ 1 (square y))) | GE
19
19 Example cont'd: (inc-square 4) | GE GE p: x b: (* x x) square: inc-square: p: y b: (+ 1 (square y)) E1 y: 4 (+ 1 (square y)) | E1 + | E1 ==> #[prim] inc-square | GE ==> (square y) | E1
20
20 Example cont'd: (inc-square 4) | E1 E2 x: 4 (* x x) | E2 * | E2 ==> #[prim] x | E2 ==> 4 GE p: x b: (* x x) square: inc-square: p: y b: (+ 1 (square y)) | E1 E1 y: 4 ==> 16 (+ 1 16) ==> 17 y | E1 ==> 4square | E1 ==> frames become inaccessible
21
21 Lessons from the inc-square example EM doesn't show the complete state of the interpreter missing 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.
22
22 Why? To have objects with local state The state is summarized in a set of variables. When the object changes its state the variable changes its value.
23
23 Procedures with local state Suppose we want to implement counters (define ca (make-counter 0)) (ca) ==> (cb) ==> (ca) ==> (define cb (make-counter 0)) 1 2 1 3 How can we define such procedures?
24
24 A counter: an object with state (define make-counter (lambda (n) (lambda ()(set! n (+ n 1)) n ))) set! does assignment to an already defined variable. It is a special form. (define ca (make-counter 0)) ca is a procedure. Variable n is its local state.
25
25 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!
26
26 The Environments Model Name Value Environment Table 23 score Substitution model: a single global environment Environments model: many environments. Generalizes the substitution model.
27
27 Example: explaining make-counter Counter: 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
28
28 (define ca (make-counter 0)) | GE GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 0 (lambda () (set! n (+ n 1)) n) | E1 p: b:(set! n (+ n 1)) n ca: environment pointer points to E1 because the lambda was evaluated in E1 E1 is pointed at so does not disappear!
29
29 (ca) | GE (set! n (+ n 1)) | E2 1 n | E2 ==> 1 E2 Empty (no args) GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 0 p: b:(set! n (+ n 1)) n ca: ==> 1
30
30 E3 (ca) | GE (set! n (+ n 1)) | E3 GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 0 p: b:(set! n (+ n 1)) n ca: 1 n | E3 ==> 2 empty ==> 2 2
31
31 Example: explaining make-counter Counter: 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
32
32 (define ca (make-counter 0)) | GE GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 0 (lambda () (set! n (+ n 1)) n) | E1 p: b:(set! n (+ n 1)) n ca: environment pointer points to E1 because the lambda was evaluated in E1
33
33 (ca) | GE (set! n (+ n 1)) | E2 1 n | E2 ==> 1 E2 empty GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 0 p: b:(set! n (+ n 1)) n ca: ==> 1
34
34 E3 (ca) | GE (set! n (+ n 1)) | E3 GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 0 p: b:(set! n (+ n 1)) n ca: 1 n | E3 ==> 2 empty ==> 2 2
35
35 (define cb (make-counter 0)) | GE (lambda () (set! n (+ n 1)) n) | E4 n: 0 E4 p: b:(set! n (+ n 1)) n cb: GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 2 p: b:(set! n (+ n 1)) n ca: E3
36
36 (cb) | GE 1 n: 0 E4 p: b:(set! n (+ n 1)) n cb: GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 2 p: b:(set! n (+ n 1)) n ca: E2 ==> 1 E5
37
37 Capturing state in local frames & procedures n: 1 E4 p: b:(set! n (+ n 1)) n cb: GE p: n b:(lambda () (set! n (+ n 1)) n) make-counter: E1 n: 2 p: b:(set! n (+ n 1)) n ca: E2
38
38 Lessons from the make-counter example Environment diagrams get complicated very quickly Rules 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 evaluated this effect can be used to store local state
39
39 Explaining Nested Definitions Nested 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))
40
40 The same x in all subprocedures (sqrt 2) | GE GE p: x b :(define good-enou..) (define improve..) (define sqrt-iter..) (sqrt-iter 1.0) sqrt: E1 x: 2 good-enough: p: guess b:(< (abs ….) improve: sqrt-iter: guess: 1 sqrt-iter guess: 1 good-enou?
41
41 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)
42
42 (define a (cons 1 2)) | GE GE p: x y b:(define (dispatch op)..) dispatch cons: p: x b:(x ‘car) p: x b:(x ‘cdr) car: cdr: E1 x: 1 y: 2 dispatch: p: op b:(cond ((eq? op 'car) x).... ) a:
43
43 (car a) | GE GE p: x y b:(define (dispatch op)..) dispatch cons: p: x b:(x ‘car) p: x b:(x ‘cdr) car: cdr: E1 x: 1 y: 2 dispatch: p: op b:(cond ((eq? op 'car) x).... ) a: x: E2 (x ‘car) | E2 op: ‘car E3 (cond..) | E3 ==> 1 ==> 1
44
44 The Environment Evaluation Model 1.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. 2.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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.