The Evaluator 1 Compiler vs Interpreter The Programmer

  • Slides: 26
Download presentation
The Evaluator 1

The Evaluator 1

Compiler vs. Interpreter The Programmer The Computer Transformation Program in High Level Language T

Compiler vs. Interpreter The Programmer The Computer Transformation Program in High Level Language T Command Processing Unit Program in Low Level Machine Language 2

A Compiler Inputs High Level Program C Machine Level Program The Compiler turns the

A Compiler Inputs High Level Program C Machine Level Program The Compiler turns the high CPU level program instructions to Instructions understood by the machine. Outputs 3

An Interpreter Inputs I High Level Program CPU Outputs The Interpreter is a machine

An Interpreter Inputs I High Level Program CPU Outputs The Interpreter is a machine level program, which interprets and executes the high level program line after line… 4

The Metacircular Evaluator Inputs Scheme Program ME CPU Outputs The Metacircular Evaluator is an

The Metacircular Evaluator Inputs Scheme Program ME CPU Outputs The Metacircular Evaluator is an Interpreter for Scheme on a machine whose machine language is Scheme. 5

Evaluator Packages: Core (Evaluation Rules) Abstract Syntax Parser Data Structures First, we introduce the

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 6

The Environments Model Substitution model: a single global environment Environment Table Name score Value

The Environments Model Substitution model: a single global environment Environment Table Name score Value 23 Environments model: many environments. Generalizes the substitution model. 7

Frame: a table of bindings • Binding: Example: A a pairing of a name

Frame: a table of bindings • Binding: Example: A a pairing of a name and a value 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 x: 15 y: 1 2 8

Environment: a sequence of frames • Environment E 1 consists of frames A and

Environment: a sequence of frames • Environment E 1 consists of frames A and B • Environment E 2 consists of frame B only • A frame may be shared by multiple environments B z: 10 E 2 A E 1 this arrow is called the enclosing environment pointer x: 15 y: 1 2 9

Evaluation in the environment model • All evaluations occur in an environment • The

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 10

The Environment Model • A precise, completely mechanical, description of: • name-rule looking up

The Environment Model • A precise, completely mechanical, description of: • name-rule looking up the value of a variable • define-rule creating a new definition of a var • lambda-rule creating a procedure • application rule applying a procedure • Enables analyzing arbitrary scheme code, including (when we get there) imperative programming • Basis for implementing a scheme interpreter • for now: draw EM state with boxes and pointers • later on: implement with code 11

Name-rule • A name X evaluated in environment E gives the value of X

Name-rule • A name X evaluated in environment E gives the value of X in the first frame of E where X is bound • z | GE ==> 10 z | E 1 ==> 10 x | E 1 ==> 15 • In E 1, the binding of x in frame A shadows the binding of x in B B GE A E 1 • x | z: 10 x: 3 GE ==> 3 x: 15 y: 1 2 12

Define-rule • A define special form evaluated in environment E creates or replaces a

Define-rule • A define special form evaluated in environment E creates or replaces a binding in the first frame of E (define z 20) | B z: 10 x: 3 z: 20 A x: 15 y: z: 25 GE E 1 (define z 25) | GE 1 2 E 1 13

Double bubble: how to draw a procedure (lambda (x) (* x x)) t prin

Double bubble: how to draw a procedure (lambda (x) (* x x)) t prin l l eva bda-ru lam #[proc-. . . ] e A compound proc that squares its argument Environment pointer Code pointer parameters: x body: (* x x) 14

Lambda-rule • A lambda special form evaluated in environment E creates a procedure whose

Lambda-rule • A lambda special form evaluated in environment E creates a procedure whose environment pointer points to E (define square (lambda (x) (* x x))) | E 1 B A E 1 z: 10 x: 3 x: 15 square: Evaluating a lambda actually returns a pointer to the parameters: procedure object x body: (* x x) environment pointer points to frame A because the lambda was evaluated in E 1 and E 1 A 15

To apply a compound procedure P to arguments (Application Rule) 1. Create a new

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 16

(square 4) | GE x: 10 square: GE *: #[prim] parameters: x body: (*

(square 4) | GE x: 10 square: GE *: #[prim] parameters: x body: (* x x) A x: 4 E 1 square | (* x x) | * | E 1 GE E 1 ==> ==> 16 frame becomes inaccessible x | E 1 ==> 4 17

Example: inc-square GE inc-square: p: x b: (* x x) p: y b: (+

Example: inc-square GE inc-square: p: x b: (* x x) p: y b: (+ 1 (square y)) (define square (lambda (x) (* x x))) | GE (define inc-square (lambda (y) (+ 1 (square y))) | GE 18

Example cont'd: (inc-square 4) | GE GE inc-square: E 1 y: 4 p: x

Example cont'd: (inc-square 4) | GE GE inc-square: E 1 y: 4 p: x b: (* x x) p: y b: (+ 1 (square y)) inc-square | GE ==> (+ 1 (square y)) | + | E 1 ==> #[prim] E 1 (square y) | E 1 19

Example cont'd: (inc-square 4) | GE E 1 inc-square: E 1 E 2 y:

Example cont'd: (inc-square 4) | GE E 1 inc-square: E 1 E 2 y: 4 p: x b: (* x x) p: y b: (+ 1 (square y)) | square | E 1 ==> (* x x) | E 2 ==> 16 * | E 2 ==> #[prim] x: 4 frames become inaccessible E 1 y | E 1 ==> 4 (+ 1 16) ==> 17 x | E 2 ==> 4 20

Explaining Nested Definitions • Nested definitions : block structure (define (sqrt x) (define (good-enough?

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)) 39

(sqrt 2) | GE GE sqrt: p: x b: (define E 1 x: 2

(sqrt 2) | GE GE sqrt: p: x b: (define E 1 x: 2 good-enough: improve: p: guess. . ) sqrt-iter: b: (< (abs …. ) good-enou (define improve. . ) (define sqrt-iter. . ) (sqrt-iter 1. 0) guess: 1 The same x in all subprocedures guess: 1 sqrt-iter good-enou? 40

message passing example (define (cons x y) (define (dispatch op) (cond ((eq? op 'car)

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) 41

(define a (cons 1 2)) | GE cons: p: x y b: (define (dispatch

(define a (cons 1 2)) | GE cons: p: x y b: (define (dispatch op). . ) dispatch a: E 1 x: 1 y: 2 dispatch: GE car: p: x b: (x ‘car) cdr: p: x b: (x ‘cdr) p: op b: (cond ((eq? op 'car) x). . ) 42

(car a) | GE cons: p: x y b: (define (dispatch op). . )

(car a) | GE cons: p: x y b: (define (dispatch op). . ) dispatch GE ==> 1 car: a: E 1 x: 1 y: 2 dispatch: E 2 p: op b: (cond ((eq? op 'car) x). . ) (x ‘car) | E 2 (cond. . ) | E 3 ==> 1 p: x b: (x ‘car) E 3 cdr: p: x b: (x ‘cdr) x: op: ‘car 43

The Environment Evaluation Model 1. To evaluate a combination (a compound expression other than

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. 44