1 / 35

CS61A Lecture 24

CS61A Lecture 24. 2011-08-01 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. Goals.

magee
Download Presentation

CS61A Lecture 24

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 24 2011-08-01 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. Goals Increase comfort with the meta-circular evaluator (MCE) Identify inefficiency See efficiency improvement using analyze Connect the ideas in analyze to compiling

  4. mce review • You could define new functions in mce? • A. True • B. False

  5. How many calls to mc-eval? ;;; M-Eval input: (define (fact n) (if (= n 0) 1 (* n (fact (- n 1))))) 1 B) 2 C) 3 D) 4 E) 5 Does this make any calls to mc-apply? A) Yes B) NO!!!

  6. How many calls to mc-eval? ;;; M-Eval input: (define (simple x) x) A)1 B) 2 C) 3 D) 4 E) 5 (define (mc-eval exp env) (display (list 'mc-eval 'exp: exp)) (newline) (cond ((self-evaluating? exp) exp) (mc-eval exp: (define (simple x) x)) (mc-eval exp: (lambda (x) x))

  7. How many calls to mc-eval? ;;; M-Eval input: (simple 5) A)1 B) 2 C) 3 D) 4 E) 5 (mc-eval exp: (simple 5)) (mc-eval exp: simple) (mc-eval exp: 5) (mc-eval exp: x)

  8. 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)))

  9. How many calls to mc-eval?DEMO 3 2 1 5 4 6 7 8 9 (fact 0) (procedure (n) ((if (= n 0) 1 (* n (fact (- n 1))))) env)

  10. calls to mc-eval (mc-eval exp: (fact 0)) (mc-eval exp: fact) (mc-eval exp: 0) (mc-eval exp: (if (= n 0) 1 (* n (fact (- n 1))))) (mc-eval exp: (= n 0)) (mc-eval exp: =) (mc-eval exp: 0) (mc-eval exp: n) (mc-eval exp: 1)

  11. (fact 1) (mc-eval exp: (if (= n 0) 1 (* n (fact (- n 1))))) (mc-eval exp: (= n 0)) (mc-eval exp: =) (mc-eval exp: 0) (mc-eval exp: n) (mc-eval exp: 1) (mc-eval exp: n) (fact 1) (mc-eval exp: (fact 1)) (mc-eval exp: fact) (mc-eval exp: 1) (mc-eval exp: (if (= n 0) 1 (* n (fact (- n 1))))) (mc-eval exp: (= n 0)) (mc-eval exp: =) (mc-eval exp: 0) (mc-eval exp: n) (mc-eval exp: (* n (fact (- n 1)))) (mc-eval exp: *) (mc-eval exp: (fact (- n 1))) (mc-eval exp: fact) (mc-eval exp: (- n 1)) (mc-eval exp: -) (mc-eval exp: 1) (mc-eval exp: n)

  12. (fact 1) (mc-eval exp: (if (= n 0) 1 (* n (fact (- n 1))))) (mc-eval exp: (= n 0)) (mc-eval exp: =) (mc-eval exp: 0) (mc-eval exp: n) (mc-eval exp: 1) (mc-eval exp: n) (fact 1) (mc-eval exp: (fact 1)) (mc-eval exp: fact) (mc-eval exp: 1) (mc-eval exp: (if (= n 0) 1 (* n (fact (- n 1))))) (mc-eval exp: (= n 0)) (mc-eval exp: =) (mc-eval exp: 0) (mc-eval exp: n) (mc-eval exp: (* n (fact (- n 1)))) (mc-eval exp: *) (mc-eval exp: (fact (- n 1))) (mc-eval exp: fact) (mc-eval exp: (- n 1)) (mc-eval exp: -) (mc-eval exp: 1) (mc-eval exp: n)

  13. (mc-eval exp: (fact 2)) (mc-eval exp: fact) (mc-eval exp: 2) (fact 2) (mc-eval exp: (if (= n 0) 1 (* n (fact (- n 1))))) (mc-eval exp: (= n 0)) (mc-eval exp: =) (mc-eval exp: 0) (mc-eval exp: n) (mc-eval exp: (* n (fact (- n 1)))) (mc-eval exp: *) (mc-eval exp: (fact (- n 1))) (mc-eval exp: fact) (mc-eval exp: (- n 1)) (mc-eval exp: -) (mc-eval exp: 1) (mc-eval exp: n) (mc-eval exp: (if (= n 0) 1 (* n (fact (- n 1))))) (mc-eval exp: (= n 0)) (mc-eval exp: =) (mc-eval exp: 0) (mc-eval exp: n) (mc-eval exp: (* n (fact (- n 1)))) (mc-eval exp: *) (mc-eval exp: (fact (- n 1))) (mc-eval exp: fact) (mc-eval exp: (- n 1)) (mc-eval exp: -) (mc-eval exp: 1) (mc-eval exp: n) (mc-eval exp: (if (= n 0) 1 (* n (fact (- n 1))))) (mc-eval exp: (= n 0)) (mc-eval exp: =) (mc-eval exp: 0) (mc-eval exp: n) (mc-eval exp: 1) (mc-eval exp: n) (mc-eval exp: n)

  14. (mc-eval exp: (fact 2)) (mc-eval exp: fact) (mc-eval exp: 2) (mc-eval exp: (if (= n 0) 1 (* n (fact (- n 1))))) (mc-eval exp: (= n 0)) (mc-eval exp: =) (mc-eval exp: 0) (mc-eval exp: n) (mc-eval exp: (* n (fact (- n 1)))) (mc-eval exp: *) (mc-eval exp: (fact (- n 1))) (mc-eval exp: fact) (mc-eval exp: (- n 1)) (mc-eval exp: -) (mc-eval exp: 1) (mc-eval exp: n) (fact 3) (mc-eval exp: (if (= n 0) 1 (* n (fact (- n 1))))) (mc-eval exp: (= n 0)) (mc-eval exp: =) (mc-eval exp: 0) (mc-eval exp: n) (mc-eval exp: (* n (fact (- n 1)))) (mc-eval exp: *) (mc-eval exp: (fact (- n 1))) (mc-eval exp: fact) (mc-eval exp: (- n 1)) (mc-eval exp: -) (mc-eval exp: 1) (mc-eval exp: n) (mc-eval exp: (if (= n 0) 1 (* n (fact (- n 1))))) (mc-eval exp: (= n 0)) (mc-eval exp: =) (mc-eval exp: 0) (mc-eval exp: n) (mc-eval exp: (* n (fact (- n 1)))) (mc-eval exp: *) (mc-eval exp: (fact (- n 1))) (mc-eval exp: fact) (mc-eval exp: (- n 1)) (mc-eval exp: -) (mc-eval exp: 1) (mc-eval exp: n) (mc-eval exp: (if (= n 0) 1 (* n (fact (- n 1))))) (mc-eval exp: (= n 0)) (mc-eval exp: =) (mc-eval exp: 0) (mc-eval exp: n) (mc-eval exp: 1) (mc-eval exp: n) (mc-eval exp: n)

  15. Each call to mc-eval could have a lot of sub-calls! Most didn’t depend upon the environment so I could do in advance (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?"))))

  16. analzye (analyze exp) (define (mc-evalexpenv) ( env)) What is the domain and range of analyze? Domain: function Range: function Domain: expression Range: function Domain: function Range: expression Domain: expression Range: expression Other

  17. analyze List representing expression analyze STK Scheme expression (λ(env) (define (mc-eval exp env) ((analyze exp) env))

  18. analyze (define (mc-eval exp env) ((analyze exp) env)) (define (analyze exp) (cond ((self-evaluating? exp) ((quoted? exp) … ((variable? exp) … ((assignment? exp) … ((definition? exp) … ((if? exp) … ((lambda? exp) … ((begin? exp) … ((cond? exp) … ((application? exp) … (else (error "Unknown" exp))))

  19. (define (mc-eval exp env) (cond ((self-evaluating? exp) exp) ((quoted? exp) (text-of-quotation exp)) ((variable? exp)(lookup-variable-value exp env)) … (define (analyze exp) (cond ((self-evaluating? exp) (analyze-self-evaluating exp)) ((quoted? exp) (analyze-quoted exp)) ((variable? exp) (analyze-variable exp)) …

  20. List representing expression analyze STK Scheme expression (λ(env) (define (mc-evalexpenv) (cond ((self-evaluating? exp) exp) ... (define (analyze exp) (cond ((self-evaluating? exp) (analyze-self-evaluating exp))... (define (analyze-self-evaluating exp) (lambda (env) exp)) Is the domain and range correct? A) Yes B) No

  21. (define (mc-evalexpenv) (cond ... ((variable? exp)(lookup-variable-value expenv)) ... (define (analyze exp) (cond ... ((variable? exp) (analyze-variable exp)) ... (define (analyze-variable exp) ;; write this! (lambda (env) (lookup-variable-value expenv))) Is the thing you returned entirely scheme (it only needs to be interpreted by STk)? A) Yes B) No C) ???

  22. (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))

  23. analyze-quoted (define (mc-eval exp env) (cond ... ((quoted? exp) (text-of-quotation exp)) ... (define (text-of-quotation exp) (cadr exp)) (define (analyze exp) (cond ... ((quoted? exp) (analyze-quoted exp)) ...

  24. Two versions of analyze-quoted (define (analyze-quoted_v1 exp) (lambda (env) (text-of-quotation exp))) (define (analyze-quoted_v2 exp) (let ((qval (text-of-quotation exp))) (lambda (env) qval))) Only v1 works Only v2 works v1 is better v2 is better They are the same

  25. Write analyze-if Is the analyzed code faster if it is run multiple times? A)Y B)N (define (mc-eval exp env) (cond ... ((if? exp) (eval-if exp env)) ... (define (analyze exp) (cond ... ((if? exp) (analyze-if exp)) ... ... (define (eval-if exp env) (if (true? (mc-eval (if-predicate exp) env)) (mc-eval (if-consequent exp) env) (mc-eval (if-alternative exp) env)))

  26. analyze-if solution (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) (aproc env))))) Is the thing you returned entirely scheme (it only needs to be interpreted by STk)? A) Yes B) No C) ???

  27. (analyze '(if #t 3 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) (aproc env)))))

  28. (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)))))

  29. Do we save time using the analyzing mce? (sent-sum '(1 2 3 4 5 6 7 8)) A) Yes B) No C)?? (sent-sum '()) A) Yes B) No C)?? (list (+ 2 3) (+ 4 5) (+ 2 3)) A) Yes B) No C)?? (list (sq 2) (sq 3) (sq 4)) A) Yes B) No C)??

  30. Compiling Java Compile Run Makes an executable file cory [344] ~/javaexample # emacs HelloWorld.java & cory [346] ~/javaexample # javac HelloWorld.java cory [347] ~/javaexample # java HelloWorld hello world cory [348] ~/javaexample # ls HelloWorld.java HelloWorld.class

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

  32. 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 )

  33. 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))

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

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