1 / 42

CS61A Lecture 25

CS61A Lecture 25. 2011-08-02 Colleen Lewis. Clicker poll . Have you started project 4 part 1? Yes – we’re done! Yes – we’re close to done! Yes – we started Yes – we started reading the project/code No – we haven’t started. Review Analyzing Evaluator. What procedures look like

neith
Download Presentation

CS61A Lecture 25

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS61A Lecture 25 2011-08-02 Colleen Lewis

  2. Clicker poll  Have you started project 4 part 1? Yes – we’re done! Yes – we’re close to done! Yes – we started Yes – we started reading the project/code No – we haven’t started

  3. Review Analyzing Evaluator What procedures look like What the output of analyze was Fact: The body of a lambda gets analyzed! We can give names to analyzed lambdas

  4. What gets returned by mc-eval?(not analyzing eval) STk> (mc-eval '(lambda (x) (* x x)) '(((a) 3))) A.(procedure (x) ((* x x)) (((a) 3))) B.(procedure (x) (* x x) (((a) 3))) C.(lambda (x) ((* x x)) (((a) 3))) D.(lambda (x) (* x x) (((a) 3))) E. Other (or no clue!)

  5. REVIEW What is a procedure? car car car car car cdr cdr cdr cdr cdr procedure ((* x x)) (x) Global a: 3 ((a).(3)) Params: x Body: (+ x x) STk> (mc-eval '(lambda (x) (* x x)) '(((a) 3))) (procedure (x) ((* x x)) (((a) 3)))

  6. List representing expression analyze STK Scheme expression (λ(env) (define (mc-eval exp env) (cond ((self-evaluating? exp) exp) ... (define (analyze exp) (cond ((self-evaluating? exp) (analyze-self-evaluating exp))... (define (analyze-self-evaluating exp) (lambda (env) exp))

  7. The body of a lambda gets analyzed! car car car car car cdr cdr cdr cdr cdr ((λ(env) 3)) procedure (x) Global a: 3 ((a).(3)) STk> (mc-eval '(lambda (x) 3) '(((a) 3))) (procedure (x) ((λ(env) 3)) (((a) 3)))

  8. (analyze '(if #t 3 4)) (λ(e) #t) (λ(e) 3) (λ(e) 4) (define (analyze exp) (cond ((self-evaluating? exp) (analyze-self-evaluating exp)) ((if? exp) (analyze-if exp))... (define (analyze-if exp) (let ((pproc (analyze (if-predicate exp))) (cproc (analyze (if-consequent exp))) (aproc (analyze (if-alternative exp)))) (lambda (env) (if (true? ( pproc env)) ( cproc env) ( approc env)))))

  9. The body of a lambda gets analyzed! (if (true? ((λ (e) #t) env) ((λ (e) 3) env) ((λ (e) 4) env)) car car car car car cdr cdr cdr cdr cdr ((if (true? ((λ (e) #t) env) ((λ (e) 3) env) ((λ (e) 4) env))) procedure (x) ((a).(3)) STk>(mc-eval '(lambda (x) '(if #t 3 4))'(((a) 3))) (procedure (x) () (((a) 3)))

  10. The body of a lambda gets analyzed! car car car car car cdr cdr cdr cdr cdr ((if (true? ((λ (e) #t) env) ((λ (e) 3) env) ((λ (e) 4) env))) procedure (x) f ((f a) . (__3)) ((a).(3)) STk>(mc-eval '(define f (lambda (x) '(if #t 3 4)) '(((a) 3)))

  11. The body of a lambda gets analyzed! (define (mc-eval exp env) ((analyze exp) env)) If we call f many times – we save time! car car car car car cdr cdr cdr cdr cdr ((if (true? ((λ (e) #t) env) ((λ (e) 3) env) ((λ (e) 4) env))) procedure (x) f ((f a) . (__3)) ((a).(3)) STk>(mce) ;;; M-Eval input: (f 1)

  12. I’ll get the “actual value” of arguments once they’re really needed Lazy Evaluator (played by Lazy Smurf)

  13. Understanding Lazy [eval/smurf] • Thunk ADT • A Thunk List – not a real scheme thunk • Storing the environment • If we’re going to delay the evaluation of arguments we need to keep track of the environment where they should be evaluated. • Delay arguments to user-defined procedures! • Not to primitive procedures

  14. Thunk ADT (not a STkREAL thunk!) Thunk list car car car cdr cdr cdr exp env thunk (define (delay-it expenv) (list 'thunkexpenv)) (define (thunk? obj) (tagged-list? obj 'thunk)) (define (thunk-expthunk) (cadrthunk)) (define (thunk-envthunk) (caddrthunk)) exp is(A) list representing an expression or (B) REAL Scheme?

  15. What would lazy [eval/smurf] do? A. Applicative Order B. Normal Order STk> (define (square x) (* x x)) STk> (square (+ 2 3))

  16. BEFORE we call square: figure out arguments! This x should come from global! Applicative Order Normal Order STk> (define x 3) STk> (define (square x) (* x x)) STk> (square (+ 2 x))

  17. If we’re going to delay-it we need to keep track of the environment! car car car cdr cdr cdr thunk env (+ 2 x) When I get forced: evaluate the exp in this environment (define (delay-it exp env) (list 'thunk exp env))

  18. How (regular) mc-eval evaluated args?!? (define (mc-eval exp env) (cond ... ((application? exp) (mc-apply (mc-eval (operator exp) env) (list-of-values (operands exp) env))) (define (list-of-values exps env) (if (no-operands? exps) '() (cons (mc-eval (first-operand exps) env) (list-of-values (rest-operands exps)env))))

  19. How lazy [smurf] mc-eval evaluates args?!? We’re going to change the range of mc-eval so we’ll have to change this too. (define (mc-eval exp env) (cond ... ((application? exp) (mc-apply (mc-eval (operator exp) env) (list-of-values (operands exp) env)))

  20. Regular mc-apply car car car car cdr cdr cdr cdr ((show 2)(show 3)) procedure (x) env (define (mc-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))))

  21. This is the lazy version – how many changes? A. 1 B. 2 C. 3 D. 4 (define (mc-apply procedure arguments env) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure (list-of-arg-values arguments env))) ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) (list-of-delayed-args arguments env) (procedure-environment procedure)))) (else (error "Unknown" procedure))))

  22. The lazy mc-apply mc-apply DO evaluate the arguments Don’t evaluate the arguments Primitive procedure Compound (User defined) Procedure

  23. In the lazy versionWhere do arguments get delayed? A. In mc-eval B. In mc-apply C. In both D. In neither E. ???

  24. In the REGULAR versionWhere do arguments get evaluated? A. In mc-eval B. In mc-apply C. In both D. In neither E. ???

  25. In the lazy versionWhere do arguments get evaluated? A. In mc-eval B. In mc-apply C. In both D. In neither E. ???

  26. How many of these are different in Normal vs. Applicative order?(invent one example that isn’t and one that is) STk> (set! x 4) okay STk> (if #t 3 4) 3 STk> (lambda (x) x) #[closure arglist=(x) 7ff27c98] STk> (begin 2 3) 3 A. 0 B. 1-2 C. 3-5 D. 6-8 E.?? STk> 3 3 STk> (define x 3) x STk> x 3 STk> 'x x

  27. How many times might we be lazy (and delay stuff)? A. 0 B. 1-2 C. 3-5 D. 6-8 E.?? (define (mc-eval exp env) (cond ((self-evaluating? exp)... ((variable? exp)... ((quoted? exp) ... ((assignment? exp) ... ((definition? exp) ... ((if? exp) ... ((lambda? exp) ... ((begin? exp) ... ((cond? exp) ... ((application? exp) ... (else (error “what?"))))

  28. A problem with delaying stuff env User-defined procedure: Don’t evaluate the arguments STk> (load "lazy.scm") okay STk> (define g-env (setup-environment)) g-env STk> (mc-eval '((lambda (x) x) (+ 2 3)) g-env) (thunk (+ 2 3) )

  29. Remember – we delayed args to compound procedures (user-defined) (define (mc-apply procedure arguments env) (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure (list-of-arg-values arguments env))) ((compound-procedure? procedure) (eval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) (list-of-delayed-args arguments env) (procedure-environment procedure)))) (else (error "Unknown" procedure))))

  30. What can be returned by the lazy mc-eval function What can be ret Values Lists Thunk ADTs All of the above None of the above

  31. What happens here? env env STk> (load "lazy.scm") okay STk> (define g-env (setup-environment)) g-env STk> (mc-eval '((lambda(x)(+ 1 x))(+ 2 3)) g-env) What is returned? a. (thunk (+ 1 5) ) b. (thunk (+ 1 (+ 2 3)) ) c. 6 d. Something else e. ???

  32. If the driver-loop needs to print it – make sure you haven’t been TOO lazy. SOLUTION mc-eval might return a delayed argument from a compound procedure This was: mc-eval (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (actual-value input the-global-environment))) (announce-output output-prompt) (user-print output))) (driver-loop))

  33. actual-value A. (mc-eval B. (actual-value car car car cdr cdr cdr ?? thunk exp env (define (actual-value exp env) (force-it (mc-eval exp env))) (define (force-it obj) (if (thunk? obj) (actual-value (thunk-exp obj) (thunk-env obj)) obj))

  34. Example of why we call actual-value env STk> (load "lazy.scm") okay STk> (define g-env (setup-environment)) g-env STk> (mc-eval '((lambda (x) x) ((lambda (y) y) (+ 2 3))) g-env) (thunk ((lambda (y) y) (+ 2 3)) )

  35. What happens if we pass a Thunk ADT as exp? A. errors B. application C. lambda D. self-eval. E.?? (define (mc-eval exp env) (cond ((self-evaluating? exp)... ((variable? exp)... ((quoted? exp) ... ((assignment? exp) ... ((definition? exp) ... ((if? exp) ... ((lambda? exp) ... ((begin? exp) ... ((cond? exp) ... ((application? exp) ... (else (error “what?"))))

  36. if’s need actual values! mc-eval sometimes returns Thunk ADTs (define (eval-if exp env) (if (true? (actual-value (if-predicate exp) env)) (mc-eval (if-consequent exp) env) (mc-eval (if-alternative exp) env)))

  37. Summary & Additional Notes • Thunk ADTs could also be memoized • We delayed arguments to compound procedures • Compound procedures are defined by the user • We didn’t delay arguments to primitive procedures • We made sure we had the actual value to print it • Ifs needed REAL values

  38. Compilers Analyze syntax Make something that can be run on a computer Provide optimization Provide useful feedback to the programmer when there are errors

  39. E1 a: 5 b: 7 c: 3 Frames in MCE(below the line) Global x: 2 y: 4 ((a b c) . (5 7 3)) or ((a b c) 5 7 3 ) (define (frame-variables frame) (car frame)) (define (frame-values frame) (cdr frame)) ((x y) . (2 4)) or ((x y) 2 4 )

  40. Environments(below the line) Error checking omitted List of frames! (define the-empty-environment '()) (extend-environment '(x y) ;; vars '(2 4) ;; vals the-empty-environment) ;; base-env (define (extend-environment vars vals base-env) (cons (make-frame vars vals) base-env))

  41. Environments(below the line) Global x: 2 y: 4 car cdr Environment ((x y).(1 2)) Frame List of frames! (define the-empty-environment '()) (extend-environment '(x y) ;; vars '(2 4) ;; vals the-empty-environment) ;; base-env

  42. Environments (Below the line) E1 a: 5 b: 7 c: 3 Global x: 2 y: 4 car car cdr cdr ((a b c).(5 7 3)) ((x y).(1 2))

More Related