Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Metacircular Evaluator

Similar presentations


Presentation on theme: "The Metacircular Evaluator"— Presentation transcript:

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

2 High level to Low-level reduction
The Programmer The Computer Transformation Command Processing Unit T Program in High Level Language Program in Low Level Machine Language

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

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

5 The Metacircular Evaluator
Inputs Scheme Program ME CPU 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. Outputs The Metacircular Evaluator is an Interpreter for Scheme on a machine whose machine language is Scheme.

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

7 The Eval/Apply Cycle

8 The Meta-Circular Evaluator: Eval
(define (eval exp env) (cond ((self-evaluating? exp) 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)))) Special forms This is a top down, modular piece of software, built correctly with many layers of abstraction. However, since you havn’t built an interpreter before, we will not start directly a top down review of the process but rather first give a simplified example. Note that the use of a high level description in the main eval loop allows easy modification For possible changes to the language, but also makes it harder to add new constructs since they must Be added by changing this main eveal loop.

9 The Meta-Circular Evaluator: Apply
(define (apply procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure 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))))

10 How the Environment Works
manipulation 3. How the Environment Works E2 x: plus: (procedure ...) E1 Abstractly – in our environment diagrams: Concretely – our implementation (as in SICP) list of values enclosing- environment list of variables frame x plus 10 E2 procedure

11 Extending the Environment
plus: (procedure ...) E3 x: 4 y: 5 Abstractly (extend-environment '(x y) (list 4 5) E2) Concretely E2 list of values list of variables frame x y 4 E3 5 E1

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

13 "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

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

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

16 Eval-definition (define (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))

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

18 Lambda expression (define (make-procedure parameters body env)
   (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))

19 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) GE This data structure is a procedure! symbol procedure symbol + symbol x

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

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

22 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 exps) env)))) (define (last-exp? seq) (null? (cdr seq))) (define (first-exp seq) (car seq)) (define (rest-exps seq) (cdr seq))

23 The Meta-Circular Evaluator: Apply
(define (apply procedure arguments) (cond ((primitive-procedure? procedure) (apply-primitive-procedure 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))))

24 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) E1) GE E1 A names values 4 x

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

26 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 3) bar : (lambda (z) (+ 1 2 z))

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

28 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

29 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 p: x body: (bear 20) pooh: bear: p: y body: (+ x y) x: 9 E1 y: 20 E2 (+ x y) | E2 => 29

30 A "Dynamic" Version of Scheme
(define (d-eval exp env) (cond ((self-evaluating? exp) 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) env)) ;CHANGE: add env (else (error "Unknown expression -- M-EVAL" exp))))

31 A "Dynamic" Scheme – d-apply
(define (d-apply procedure arguments calling-env) (cond ((primitive-procedure? procedure) (apply-primitive-procedure 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))))


Download ppt "The Metacircular Evaluator"

Similar presentations


Ads by Google