The Metacircular Evaluator Chapter 4 Section 4 1

  • Slides: 31
Download presentation
The Metacircular Evaluator Chapter 4 Section 4. 1 We will not cover every little

The Metacircular Evaluator Chapter 4 Section 4. 1 We will not cover every little detail in the lectures, so you MUST read Section 4. 1 in full. http: //mitpress. mit. edu/sicp/fulltext/book-Z-H-26. html 1

High level to Low-level reduction The Programmer The Computer Transformation Program in High Level

High level to Low-level reduction 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

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

The Eval/Apply Cycle 7

The Eval/Apply Cycle 7

The Meta-Circular Evaluator: Eval Special forms (define (eval exp env) (cond ((self-evaluating? exp) ((variable?

The Meta-Circular Evaluator: Eval Special forms (define (eval exp env) (cond ((self-evaluating? exp) ((variable? exp) (lookup-variable-value exp env)) ((quoted? exp) (text-of-quotation exp)) ((assignment? exp) (eval-assignment exp env)) ((definition? exp) (eval-definition exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (make-procedure (lambda-parameters exp) (lambda-body exp) env)) ((begin? exp) (eval-sequence (begin-actions exp) env)) ((cond? exp) (eval (cond->if exp) env)) ((application? exp) (apply (eval (operator exp) env) (list-of-values (operands exp) env))) (else (error "Unknown expression type -- EVAL" exp)))) 8

The Meta-Circular Evaluator: Apply (define (apply procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure arguments)) ((compound-procedure?

The Meta-Circular Evaluator: Apply (define (apply procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure arguments)) ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) arguments (procedure-environment procedure)))) (else (error "Unknown procedure type" procedure)))) 9

3. environment How the Environment Worksmanipulation E 1 • Abstractly – in our environment

3. environment How the Environment Worksmanipulation E 1 • Abstractly – in our environment diagrams: x: 10 E 2 plus: (procedure. . . ) • Concretely – our implementation (as E 2 in SICP) list of variables x plus frame enclosingenvironment list of values 10 procedure 10

Extending the Environment Abstractly • (extend-environment '(x y) (list 4 5) E 2) Concretely

Extending the Environment Abstractly • (extend-environment '(x y) (list 4 5) E 2) Concretely E 2 E 3 E 1 E 2 x: 10 plus: (procedure. . . ) E 3 x: y: 4 5 E 1 frame list of variables x y list of values 4 5 11

Extending the environment (define (extend-environment vars vals base-env) (if (= (length vars) (length vals))

Extending the environment (define (extend-environment vars vals base-env) (if (= (length vars) (length vals)) (cons (make-frame vars vals) base-env) (if (< (length vars) (length vals)) (error "Too many arguments supplied" vars vals) (error "Too few arguments supplied" vars vals)))) 12

"Scanning" the environment • Look for a variable in the environment. . . •

"Scanning" the environment • Look for a variable in the environment. . . • Look for a variable in a frame. . . – loop through the list of vars and list of vals in parallel – detect if the variable is found in the frame • If not found in frame (out of variables in the frame), look in enclosing environment 13

Scanning the environment (define (lookup-variable-value var env) (define (env-loop env) (define (scan vars vals)

Scanning the environment (define (lookup-variable-value var env) (define (env-loop env) (define (scan vars vals) (cond ((null? vars) (env-loop (enclosing-environment env))) ((eq? var (car vars)) (car vals)) (else (scan (cdr vars) (cdr vals))))) (if (eq? env the-empty-environment) (error "Unbound variable" var) (let ((frame (first-frame env))) (scan (frame-variables frame) (frame-values frame))))) (env-loop env)) (define (enclosing-environment env) (cdr env)) (define (frame-variables frame) (car frame)) (define (frame-values frame) (cdr frame)) 14

If predicate (define (eval-if exp) (if (true? (eval (if-predicate exp))) (eval (if-consequent exp)) (eval

If predicate (define (eval-if exp) (if (true? (eval (if-predicate exp))) (eval (if-consequent exp)) (eval (if-alternative exp)))) (define (if-predicate exp) (cadr exp)) (define (if-consequent exp) (caddr exp)) (define (if-alternative exp) (if (not (null? (cdddr exp))) (cadddr exp) 'false)) (define (true? x) (not (eq? x #f))) (define (false? x) (eq? x #f)) 15

Eval-definition (define-variable! var val env) (let ((frame (first-frame env))) (define (scan vars vals) (cond

Eval-definition (define-variable! var val env) (let ((frame (first-frame env))) (define (scan vars vals) (cond ((null? vars) (add-binding-to-frame! var val frame)) ((eq? var (car vars)) (set-car! vals val)) (else (scan (cdr vars) (cdr vals))))) (scan (frame-variables frame) (frame-values frame)))) (define (eval-definition exp env) (let ((name (cadr exp)) (defined-to-be (eval (caddr exp) env))) (define-variable! name defined-to-be env) ‘undefined)) 16

Defining New Procedures Want to add new procedures For example, a scheme program: (define

Defining New Procedures Want to add new procedures For example, a scheme program: (define twice (lambda (x) (+ x x))) (twice 4) (define (lambda? exp) (tag-check exp 'lambda)) (define (eval exp env) (cond … ((lambda? exp) (eval-lambda exp env)) … (else (error "unknown expression " exp)))) 17

Lambda expression (define (eval-lambda exp env) (make-procedure (lambda-parameters exp) (lambda-body exp) env)) (define (lambda-parameters

Lambda expression (define (eval-lambda exp env) (make-procedure (lambda-parameters exp) (lambda-body exp) env)) (define (lambda-parameters exp) (cadr exp)) (define (lambda-body exp) (cddr exp)) (define (make-procedure parameters body env) (list 'procedure parameters body env)) 18

Implementation of lambda (eval '(define twice (lambda (x) (+ x x))) GE) (eval '(lambda

Implementation of lambda (eval '(define twice (lambda (x) (+ x x))) GE) (eval '(lambda (x) (+ x x)) GE) (eval-lambda '(lambda (x) (+ x x)) GE) (make-procedure '(x) ’((+ x x)) GE) (list ’procedure '(x) ’((+ x x)) GE symbol procedure symbol + This data symbol structure is x a procedure! 19

Naming the procedure (eval '(define twice (lambda (x) (+ x x))) GE) z 9

Naming the procedure (eval '(define twice (lambda (x) (+ x x))) GE) z 9 true #t + symbol primitive scheme procedure + twice symbol procedure symbol + symbol x 20

Selctors for procedure type (define (compound-procedure? exp) (tag-check exp ‘procedure)) (define (procedure-parameters compound) (cadr

Selctors for procedure type (define (compound-procedure? exp) (tag-check exp ‘procedure)) (define (procedure-parameters compound) (cadr compound)) (define (procedure-body compound) (caddr compound)) (define (procedure-env compound) (cadddr compound)) 21

Eval sequence (define (eval-sequence exps env) (cond ((last-exp? exps) (eval (first-exp exps) env)) (else

Eval sequence (define (eval-sequence exps env) (cond ((last-exp? exps) (eval (first-exp exps) env)) (else (eval (first-exp exps) env) (eval-sequence (rest-exps) env)))) (define (last-exp? seq) (null? (cdr seq))) (define (first-exp seq) (car seq)) (define (rest-exps seq) (cdr seq)) 22

The Meta-Circular Evaluator: Apply (define (apply procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure arguments)) ((compound-procedure?

The Meta-Circular Evaluator: Apply (define (apply procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure arguments)) ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) arguments (procedure-environment procedure)))) (else (error "Unknown procedure type" procedure)))) 23

Implementation of apply (1) (eval '(twice 4) GE) (apply (eval 'twice GE) (map (lambda

Implementation of apply (1) (eval '(twice 4) GE) (apply (eval 'twice GE) (map (lambda (e) (eval e GE)) '(4))) (apply (list 'procedure '(x) ’((+ x x)) GE) '(4)) (eval-seq ’((+ x x)) (extend-environment '(x) '(4) GE)) (eval '(+ x x) E 1) A E 1 GE names x values 4 24

Implementation of apply (2) (eval '(+ x x) E 1) (apply (eval + E

Implementation of apply (2) (eval '(+ x x) E 1) (apply (eval + E 1) (map (lambda (e) (eval e E 1)) '(x x))) (apply '(primitive #[add]) (list (eval 'x E 1))) (apply '(primitive #[add]) '(4 4)) (scheme-apply #[add] '(4 4)) 8 A E 1 GE names x values 4 25

We Implemented Lexical Scoping Free variables in an application of procedure f get their

We Implemented Lexical Scoping Free variables in an application of procedure f get their values from the procedure where f was defined (by the appropriate lambda special form). Also called static binding (define (foo x y) (lambda (z) (+ x y z))) (define bar (foo 1 2)) bar : (lambda (z) (+ 1 2 z)) (bar 3) 26

Lexical Scoping Environment Diagram (define (foo x y) (lambda (z) (+ x y z)))

Lexical Scoping Environment Diagram (define (foo x y) (lambda (z) (+ x y z))) (define bar (foo 1 2)) (bar 3) GE foo: p: x y body: (l (z) (+ x y z)) Will always evaluate (+ x y z) in a new environment inside the surrounding lexical environment. bar: E 1 x: 1 y: 2 p: z body: (+ x y z) E 2 z: 3 (+ x y z) | E 2 => 6 27

Alternative Model: Dynamic Scoping • Dynamic scope: – Look up free variables in the

Alternative Model: Dynamic Scoping • Dynamic scope: – Look up free variables in the caller's environment rather than the surrounding lexical environment • Example: (define x 11) (define (pooh x) (bear 20)) (define bear (lambda (y) (+ x y))) (pooh 9) => 29 28

Dynamic Scoping Environment Diagram (define x 11) (define (pooh x) (bear 20)) Will evaluate

Dynamic Scoping Environment Diagram (define x 11) (define (pooh x) (bear 20)) Will evaluate (+ x y) in an environment that extends the caller's environment. (define (bear y) (+ x y)) (pooh 9) GE x: 11 pooh: bear: E 1 p: x body: (bear 20) E 2 x: 9 y: 20 (+ x y) | E 2 => 29 p: y body: (+ x y) 29

A "Dynamic" Version of Scheme (define (d-eval exp env) (cond ((self-evaluating? exp) ((variable? exp)

A "Dynamic" Version of Scheme (define (d-eval exp env) (cond ((self-evaluating? exp) ((variable? exp) (lookup-variable-value exp env)). . . ((lambda? exp) (make-procedure (lambda-parameters exp) (lambda-body exp) '*no-environment*)) ; CHANGE: no env. . . ((application? exp) (d-apply (d-eval (operator exp) env) (list-of-values (operands exp) env)) ; CHANGE: add env (else (error "Unknown expression -- M-EVAL" exp)))) 30

A "Dynamic" Scheme – d-apply (define (d-apply procedure arguments calling-env) (cond ((primitive-procedure? procedure) (apply-primitive-procedure

A "Dynamic" Scheme – d-apply (define (d-apply procedure arguments calling-env) (cond ((primitive-procedure? procedure) (apply-primitive-procedure arguments)) ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) arguments calling-env))) ; CHANGE: use calling env (else (error "Unknown procedure" procedure)))) 31