Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 26: The Metacircular Evaluator Eval Apply

Similar presentations


Presentation on theme: "Lecture 26: The Metacircular Evaluator Eval Apply"— Presentation transcript:

1 David Evans http://www.cs.virginia.edu/~evans
Lecture 26: The Metacircular Evaluator Eval Apply CS200: Computer Science University of Virginia Computer Science David Evans

2 Menu Metacircular Evaluator Core Review PS6 Implementing Environments
Adventure Game Nominations Implementing Environments 27 March 2002 CS 200 Spring 2002

3 Environmental Model of Evaluation
To evaluate a combination, evaluate all the subexpressions and apply the value of the first subexpression to the values of the other subexpressions. To apply a compound procedure to a set of arguments, evaluate the body of the procedure in a new environment. The parent of the new environment is the procedure’s environment; the frame is a new frame that contains places with the formal parameters bound to the arguments. 27 March 2002 CS 200 Spring 2002

4 meval (define (meval expr env) (cond ((self-evaluating? expr) expr)
((variable? expr) (environment-lookup-name expr env)) ((lambda? expr) (make-procedure (lambda-parameters expr) (lambda-body expr) env)) ((application? expr) (mapply (meval (application-operator expr) env) (map (lambda (subexpr) (meval subexpr env)) (application-operands expr)))) (else (error "Unknown expression: " exp)))) 27 March 2002 CS 200 Spring 2002

5 mapply (define (mapply procedure operands) (cond
((primitive-procedure? procedure) (apply-primitive procedure operands)) ((compound-procedure? procedure) (meval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) operands (procedure-environment procedure)))) (else (error “Can’t apply: " procedure)))) 27 March 2002 CS 200 Spring 2002

6 PS6 27 March 2002 CS 200 Spring 2002

7 Question 3 What is (make-object ‘book)? book (define make-object
global environment (define make-object (lambda (name) (lambda (message) (if (eq? message ‘object?) ))))))) name : book parameters: message body: (if (eq? message …) 27 March 2002 CS 200 Spring 2002

8 What is ((make-object ‘book) ‘name)?
global environment (define make-object (lambda (name) (lambda (message) (if (eq? message ‘object?) (if (eq? message 'name) (lambda (self) name) ))))))) name : book message : name parameters: self body: name 27 March 2002 CS 200 Spring 2002

9 What is (((make-object ‘book) ‘name) 3)?
global environment (define make-object (lambda (name) (lambda (message) (if (eq? message ‘object?) (if (eq? message 'name) (lambda (self) name) ))))))) name : book message : name 3 self : parameters: self body: name name 27 March 2002 CS 200 Spring 2002

10 Adventure Game Nominees
27 March 2002 CS 200 Spring 2002

11 The Nominees Are… “Governor” “Honor System” “Hungry” “Making the Band”
27 March 2002 CS 200 Spring 2002

12 Environments 27 March 2002 CS 200 Spring 2002

13 (nest 3) (define nest (lambda (x) (+ x x)))) > ((nest 3) 4)
From Lecture 19: global environment (define nest (lambda (x) (+ x x)))) > ((nest 3) 4) + : #<primitive:+> nest: x : 3 parameters: x body: (lambda (x) (+ x x)) x : 3 (nest 3) ((lambda (x) (+ x x)) 4) x : 4 27 March 2002 CS 200 Spring 2002 (+ x x)

14 Representing Environments
An environment is a frame and a parent. x : 4 (define (make-new-environment frame env) (cons frame env)) 27 March 2002 CS 200 Spring 2002

15 Environment Procedures
(define (first-frame env) (car env)) (define (enclosing-environment env) (cdr env)) 27 March 2002 CS 200 Spring 2002

16 Representing Frames (define (make-empty-frame) (list))
A frame is a list of name-value pairs. y : 3 x : 4 (define (make-empty-frame) (list)) 27 March 2002 CS 200 Spring 2002

17 Environmental Model of Evaluation
To evaluate a combination, evaluate all the subexpressions and apply the value of the first subexpression to the values of the other subexpressions. To apply a compound procedure to a set of arguments, evaluate the body of the procedure in a new environment. The parent of the new environment is the procedure’s environment; the frame is a new frame that contains places with the formal parameters bound to the arguments. 27 March 2002 CS 200 Spring 2002

18 mapply (define (mapply procedure operands) (cond
((primitive-procedure? procedure) (apply-primitive procedure operands)) ((compound-procedure? procedure) (meval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) operands (procedure-environment procedure)))) (else (error “Can’t apply: " procedure)))) 27 March 2002 CS 200 Spring 2002

19 extend-environment (define (extend-environment names values env)
(make-new-environment (map (lambda (name value) (cons name value)) names values) env)) 27 March 2002 CS 200 Spring 2002

20 meval (define (meval expr env) (cond ((self-evaluating? expr) expr)
((variable? expr) (environment-lookup-name expr env)) ((lambda? expr) (make-procedure (lambda-parameters expr) (lambda-body expr) env)) ((application? expr) (mapply (meval (application-operator expr) env) (map (lambda (subexpr) (meval subexpr env)) (application-operands expr)))) (else (error "Unknown expression: " exp)))) 27 March 2002 CS 200 Spring 2002

21 environment-lookup-name
(define (environment-lookup-name name env) (if (null? env) (error "No binding " name) (if (frame-contains? name (first-frame env)) (frame-lookup-name name (environment-lookup-name name (enclosing-environment env))))) 27 March 2002 CS 200 Spring 2002

22 frame procedures (define (frame-contains? name frame)
(insertlg (lambda (var result) (if (eq? (car var) name) #t result)) frame #f)) (define (frame-lookup-name name frame) (if (null? frame) (error "Name not found:" name) (if (eq? (car (car frame)) name) (cdr (car frame)) (frame-lookup-name name (cdr frame))))) 27 March 2002 CS 200 Spring 2002

23 meval (define (meval expr env) (cond ((self-evaluating? expr) expr)
((variable? expr) (environment-lookup-name expr env)) ((lambda? expr) (make-procedure (lambda-parameters expr) (lambda-body expr) env)) ((application? expr) (mapply (meval (application-operator expr) env) (map (lambda (subexpr) (meval subexpr env)) (application-operands expr)))) (else (error "Unknown expression: " exp)))) 27 March 2002 CS 200 Spring 2002

24 mapply (define (mapply procedure operands) (cond
((primitive-procedure? procedure) (apply-primitive procedure operands)) ((compound-procedure? procedure) (meval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) operands (procedure-environment procedure)))) (else (error “Can’t apply: " procedure)))) 27 March 2002 CS 200 Spring 2002

25 procedure procedures (define (make-procedure parameters body environment) (list 'procedure parameters body environment)) (define (compound-procedure? expr) (tagged-list? expr 'procedure)) (define (procedure-parameters procedure) (cadr procedure)) (define (procedure-body procedure) (caddr procedure)) (define (procedure-environment procedure) (cadddr procedure)) (define (tagged-list? expr tag) (if (pair? expr) (eq? (car expr) tag) #f)) 27 March 2002 CS 200 Spring 2002

26 meval (define (meval expr env) (cond ((self-evaluating? expr) expr)
((variable? expr) (environment-lookup-name expr env)) ((lambda? expr) (make-procedure (lambda-parameters expr) (lambda-body expr) env)) ((application? expr) (mapply (meval (application-operator expr) env) (map (lambda (subexpr) (meval subexpr env)) (application-operands expr)))) (else (error "Unknown expression: " exp)))) 27 March 2002 CS 200 Spring 2002

27 self-evaluating? (define (self-evaluating? expr)
(or (number? expr) (string? expr) (primitive-procedure? expr))) 27 March 2002 CS 200 Spring 2002

28 Primitive Procedures (define (primitive-procedure? expr)
(tagged-list? expr 'primitive-procedure)) (define (make-primitive-procedure expr) (list 'primitive-procedure expr)) (define (primitive-procedure-procedure procedure) (cadr procedure)) 27 March 2002 CS 200 Spring 2002

29 the-global-environment
(define the-empty-environment '()) (define the-global-environment (make-new-environment (list (cons '+ (make-primitive-procedure +)) (cons '* (make-primitive-procedure *)) (cons '- (make-primitive-procedure -)) ) the-empty-environment)) 27 March 2002 CS 200 Spring 2002

30 Charge This is powerful: once we have an metacircular evaluator, we can easily make changes to the language! Friday: quantum computing, variations on Scheme PS7: extend our Mini-Scheme evaluator to model quantum computing (loosely) 27 March 2002 CS 200 Spring 2002


Download ppt "Lecture 26: The Metacircular Evaluator Eval Apply"

Similar presentations


Ads by Google