Presentation is loading. Please wait.

Presentation is loading. Please wait.

6.001 SICP 1 6.001 SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.

Similar presentations


Presentation on theme: "6.001 SICP 1 6.001 SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements."— Presentation transcript:

1 6.001 SICP 1 6.001 SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements

2 6.001 SICP 2 Stages of an interpreter "(average 4 (+ 5 5))" (average4( +55 ) ) 4 symbol average 5 symbol + 5 7 Lexical analyzer Parser Evaluator Environment Printer "7" input to each stage

3 6.001 SICP 3 Arithmetic calculator (define (tag-check e sym) (and (pair? e) (eq? (car e) sym))) (define (sum? e) (tag-check e 'plus*)) (define (eval exp) (cond ((number? exp) exp) ((sum? exp) (eval-sum exp)) (else (error "unknown expression " exp)))) (define (eval-sum exp) (+ (eval (cadr exp)) (eval (caddr exp)))) (eval '(plus* 24 (plus* 5 6)))

4 6.001 SICP 4 Arithmetic calculator Want to evaluate arithmetic expressions of two arguments, like: (plus* 24 (plus* 5 6))

5 6.001 SICP 5 We are just walking through a tree … 24plus* 65 (eval ) 24plus* 65 sum? checks the tag

6 6.001 SICP 6 We are just walking through a tree … (eval-sum ) 24plus* 65 65 (+ (eval 24) (eval )) (+ (eval 5) (eval 6))

7 6.001 SICP 7 The Evaluators 1.arithmetic calculator 2.add names: quote, define, environment is a table 3.add conditionals: eval-greater, eval-if, first apply 4.operators in environment: make-primitive, scheme-apply 5.environment as parameter to eval eval-special and lookup 6.compound procedures lambda evaluates to double bubble (list of params, body, encl.env.) apply on double bubble drops frame from encl.env., binds names to values, evals body in new env. environment is now a list of tables

8 6.001 SICP 8 Eval 6 review - eval (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)))) (else (error "unknown expression " exp)))) (define (define? exp) (tag-check exp ’define*)) (define (if? exp) (tag-check exp ’if*)) (define (application? e) (pair? e)) (define (lambda? e) (tag-check e 'lambda*))

9 6.001 SICP 9 Eval 6 review - env (define (extend-env-with-new-frame names values env) (let ((new-frame (make-table))) (make-bindings! names values new-frame) (cons new-frame env))) (define (make-bindings! names values table) (for-each (lambda (name value) (table-put! table name value)) names values)) ; the initial global environment (define GE (extend-env-with-new-frame (list 'plus* 'greater*) (list (make-primitive +) (make-primitive >)) nil))

10 6.001 SICP 10 Eval 6 review - lookup / define ; lookup searches the list of frames for the first match (define (lookup name env) (if (null? env) (error "unbound variable: " name) (let ((binding (table-get (car env) name))) (if (null? binding) (lookup name (cdr env)) (binding-value binding))))) ; define changes the first frame in the environment (define (eval-define exp env) (let ((name (cadr exp)) (defined-to-be (caddr exp))) (table-put! (car env) name (eval defined-to-be env)) 'undefined))

11 6.001 SICP 11 Eval 6 review - if (define (eval-if exp env) (let ((predicate (cadr exp)) (consequent (caddr exp)) (alternative (cadddr exp))) (let ((test (eval predicate env))) (cond ((eq? test #t) (eval consequent env)) ((eq? test #f) (eval alternative env)) (else (error "val not boolean: " predicate))))))

12 6.001 SICP 12 Eval 6 review - lambda (define (eval-lambda exp env) (let ((args (cadr exp)) (body (caddr exp))) (make-compound args body env))) ;; ADT that implements the “double bubble” (define compound-tag 'compound) (define (make-compound parameters body env) (list compound-tag parameters body env)) (define (compound? exp) (tag-check exp compound-tag)) (define (parameters compound) (cadr compound)) (define (body compound) (caddr compound)) (define (env compound) (cadddr compound))

13 6.001 SICP 13 Eval 6 review - apply (define scheme-apply apply) (define (apply operator operands) (cond ((primitive? operator) (scheme-apply (get-scheme-procedure operator) operands)) ((compound? operator) (eval (body operator) (extend-env-with-new-frame (parameters operator) operands (env operator)))) (else (error "operator not a procedure: " operator))))

14 6.001 SICP 14 Eval examples ; assume (table-put! (car GE) ‘times* (make- primitive *)) (eval '(define* z* (plus* 1 3)) GE) (eval '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (eval '(mpy* 3 z*) GE)

15 6.001 SICP 15 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define* z* (plus* 1 3)) | GE

16 6.001 SICP 16 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)))) (else (error "unknown expression " exp)))) (define (eval-define exp env) (let ((name (cadr exp)) (defined-to-be (caddr exp))) (table-put! (car env) name (eval defined-to-be env)) 'undefined))

17 6.001 SICP 17 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define* z* (plus* 1 3)) | GE (plus* 1 3) | GE

18 6.001 SICP 18 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)) )) (else (error "unknown expression " exp))))

19 6.001 SICP 19 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define* z* (plus* 1 3)) | GE (plus* 1 3) | GE 1 | GE 3 | GE

20 6.001 SICP 20 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)))) (else (error "unknown expression " exp))))

21 6.001 SICP 21 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define* z* (plus* 1 3)) | GE (plus* 1 3) | GE 1 | GE ==> 1 3 | GE ==> 3

22 6.001 SICP 22 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)) )) (else (error "unknown expression " exp))))

23 6.001 SICP 23 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)) )) (else (error "unknown expression " exp))))

24 6.001 SICP 24 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define* z* (plus* 1 3)) | GE (plus* 1 3) | GE 1 | GE ==> 1 3 | GE ==> 3 plus* | GE

25 6.001 SICP 25 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)))) (else (error "unknown expression " exp))))

26 6.001 SICP 26 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define* z* (plus* 1 3)) | GE (plus* 1 3) | GE 1 | GE ==> 1 3 | GE ==> 3 plus* | GE ==> (primitive [prim +])

27 6.001 SICP 27 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)) )) (else (error "unknown expression " exp))))

28 6.001 SICP 28 (define scheme-apply apply) (define (apply operator operands) (cond ((primitive? operator) (scheme-apply (get-scheme-procedure operator) operands)) ((compound? operator) (eval (body operator) (extend-env-with-new-frame (parameters operator) operands (env operator)))) (else (error "operator not a procedure: " operator))))

29 6.001 SICP 29 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define* z* (plus* 1 3)) | GE (plus* 1 3) | GE 1 | GE ==> 1 3 | GE ==> 3 plus* | GE ==> (primitive [prim +]) (scheme-apply [prim +] 1 3) ==> 4

30 6.001 SICP 30 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)))) (else (error "unknown expression " exp)))) (define (eval-define exp env) (let ((name (cadr exp)) (defined-to-be (caddr exp))) (table-put! (car env) name (eval defined-to-be env)) 'undefined))

31 6.001 SICP 31 evaluation example (eval '(define* z* (plus* 1 3)) GE) (define z* (plus* 1 3)) | GE (plus* 1 3) | GE 1 | GE ==> 1 3 | GE ==> 3 plus* | GE ==> (primitive [prim +]) (scheme-apply [prim +] 1 3) ==> 4 (table-put! (car GE) ‘z* 4) ;modified environment!

32 6.001 SICP 32 evaluation example ;assume (table-put! (car GE) ‘times* (make- primitive *)) (eval '(define* z* (plus* 1 3)) GE) (eval '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (eval '(mpy* 3 z*) GE)

33 6.001 SICP 33 evaluation example (eval '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (define* mpy* (lambda* (x* y*) (times* x* y*))) | GE

34 6.001 SICP 34 evaluation example (eval '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (define* mpy* (lambda* (x* y*) (times* x* y*))) | GE (lambda* (x* y*) (times* x* y*)) | GE

35 6.001 SICP 35 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)) )) (else (error "unknown expression " exp)))) (define (eval-lambda exp env) (let ((args (cadr exp)) (body (caddr exp))) (make-compound args body env))) (define compound-tag 'compound) (define (make-compound parameters body env) (list compound-tag parameters body env))

36 6.001 SICP 36 evaluation example (eval* '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (define* mpy* (lambda* (x* y*) (times* x* y*))) | GE (lambda* (x* y*) (times* x* y*)) | GE (make-compound ‘(x* y*) ‘(times* x* y*) GE)

37 6.001 SICP 37 evaluation example (eval '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (define* mpy* (lambda* (x* y*) (times* x* y*))) | GE (lambda* (x* y*) (times* x* y*)) | GE (make-compound ‘(x* y*) ‘(times* x* y*) GE) ==> (list ‘compound ‘(x* y*) ‘(times* x* y*) GE)

38 6.001 SICP 38 evaluation example (eval '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (define* mpy* (lambda* (x* y*) (times* x* y*))) | GE (lambda* (x* y*) (times* x* y*)) | GE (make-compound ‘(x* y*) ‘(times* x* y*) GE) ==> (list ‘compound ‘(x* y*) ‘(times* x* y*) GE) (table-put! (car GE) ‘mpy* (list ‘compound ‘(x* y*) ‘(times* x* y*) GE)) ;modified environment!

39 6.001 SICP 39 evaluation example ;assume (table-put! (car GE) ‘times* (make- primitive *)) (eval '(define* z* (plus* 1 3)) GE) (eval '(define* mpy* (lambda* (x* y*) (times* x* y*))) GE) (eval '(mpy* 3 z*) GE)

40 6.001 SICP 40 evaluation example (eval '(mpy* 3 z*) GE) (mpy* 3 z*) | GE

41 6.001 SICP 41 (define (eval exp env) (cond ((number? exp) exp) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)) )) (else (error "unknown expression " exp))))

42 6.001 SICP 42 evaluation example (eval '(mpy* 3 z*) GE) (mpy* 3 z*)| GE mpy* | GE ==> ‘(compound …) 3 | GE ==> 3 z* | GE ==> 4 (apply (list ‘compound ‘(x* y*) ‘(times* x* y*) GE) ‘( 3 4))

43 6.001 SICP 43 (define scheme-apply apply) (define (apply operator operands) (cond ((primitive? operator) (scheme-apply (get-scheme-procedure operator) operands)) ((compound? operator) (eval (body operator) (extend-env-with-new-frame (parameters operator) operands (env operator)))) (else (error "operator not a procedure: " operator))))

44 6.001 SICP 44 evaluation example (eval '(mpy* 3 z*) GE) (mpy* 3 z*) | GE mpy* | GE ==> ‘(compound …) 3 | GE ==> 3 z* | GE ==> 4 (apply (list ‘compound ‘(x* y*) ‘(times* x* y*) GE) ‘(3 4)) (times* x* y*) | (extend-env-with-new-frame ‘(x* y*) ‘(3 4) GE)) [E1--> (x*:3, y*:4) --> GE ] (times* x* y*) | E1 (scheme-apply [prim *] 3 4) ==> 12!

45 6.001 SICP 45 evaluation example (eval '(define* class (quote* is-over))) (define* class (quote* is-over)) | GE (quote* is-over) | GE

46 6.001 SICP 46 evaluation example (eval '(define* class (quote* is-over))) (define* class (quote* is-over)) | GE (quote* is-over) | GE unbound variable: quote*

47 6.001 SICP 47 (define (eval exp env) (cond ((number? exp) exp) ((quote? exp) (eval-quote exp)) ((symbol? exp) (lookup exp env)) ((define? exp) (eval-define exp env)) ((if? exp) (eval-if exp env)) ((lambda? exp) (eval-lambda exp env)) ((application? exp) (apply (eval (car exp) env) (map (lambda (e) (eval e env)) (cdr exp)) )) (else (error "unknown expression " exp)))) (define quote? (tag-check exp ‘quote*)) (define (eval-quote exp) (cadr exp))

48 6.001 SICP 48 evaluation example (eval '(define class (quote* is-over))) (define class (quote* is-over)) | GE (quote* is-over) | GE ==> is-over (table-put! (car GE) ‘class ‘is-over)

49 6.001 SICP 49 Summary Eval / Apply cycle is core of eval. eval calls apply with operator and args apply calls eval with expression and env no pending operations on either call – if expression is iterative so is evaluation

50 6.001 SICP 50 Exercise Extend the define* evaluator so define* also returns a value. e.g. (define* a 16) ==> 16

51 6.001 SICP 51 Eval 6 review - lookup / define ; lookup searches the list of frames for the first match (define (lookup name env) (if (null? env) (error "unbound variable: " name) (let ((binding (table-get (car env) name))) (if (null? binding) (lookup name (cdr env)) (binding-value binding))))) ; define changes the first frame in the environment (define (eval-define exp env) (let ((name (cadr exp)) (defined-to-be (caddr exp))) (table-put! (car env) name (eval defined-to-be env)) 'undefined))

52 6.001 SICP 52 Exercise Extend our language with the and operator. (and.. )

53 6.001 SICP 53 Exercise -- add AND (define (tag-check e sym) (and (pair? e) (eq? (car e) sym))) (define (and? e) (tag-check e 'and*)) (define (eval exp) (cond ((number? exp) exp) ((boolean? exp) exp) ((and? exp) (eval-and exp)) (else (error "unknown expression " exp)))) (define (eval-and exp) (define (try-next terms) (if (null? terms) #t (let ((first-term (eval (car terms)))) (if first-term (try-next (cdr terms)) #f)))) (try-next (cdr exp))) (eval '(and* #t #t))

54 6.001 SICP 54 Exercise - 4 Modify the interpreter so we can write infix assignments instead. (define MyVar 10)  (myVar := 10)

55 6.001 SICP 55 Eval 6 review - lookup / define ; lookup searches the list of frames for the first match (define (lookup name env) (if (null? env) (error "unbound variable: " name) (let ((binding (table-get (car env) name))) (if (null? binding) (lookup name (cdr env)) (binding-value binding))))) ; define changes the first frame in the environment (define (eval-define exp env) (let ((name (cadr exp)) (defined-to-be (caddr exp))) (table-put! (car env) name (eval defined-to-be env)) 'undefined))


Download ppt "6.001 SICP 1 6.001 SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements."

Similar presentations


Ads by Google