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

CS61A Lecture 25

  • Upload
    neith

  • View
    41

  • Download
    3

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: CS61A Lecture 25

CS61A Lecture 25

2011-08-02Colleen Lewis

1

Page 2: CS61A Lecture 25

Clicker poll Have you started project 4 part 1?A)Yes – we’re done!B)Yes – we’re close to done!C)Yes – we started D)Yes – we started reading the project/codeE)No – we haven’t started

2

Page 3: CS61A Lecture 25

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

3

Page 4: CS61A Lecture 25

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

4

Page 5: CS61A Lecture 25

REVIEW What is a procedure?

STk> (mc-eval '(lambda (x) (* x x)) '(((a) 3)))

(procedure (x) ((* x x)) (((a) 3)))

5

car cdr

((a).(3))Params: xBody: (+ x x)

Globala: 3

procedure

car cdr

(x)

car cdr

((* x x))

car cdr car cdr

Page 6: CS61A Lecture 25

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

6

analyzeList

representing expression

STK Scheme expression(λ(env)

Page 7: CS61A Lecture 25

The body of a lambda gets analyzed!

STk> (mc-eval '(lambda (x) 3) '(((a) 3)))

(procedure (x) ((λ(env) 3)) (((a) 3)))

7

car cdr

((a).(3))

Globala: 3

procedure

car cdr

(x)

car cdr

((λ(env) 3))

car cdr car cdr

Page 8: CS61A Lecture 25

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

( approc env)))))

8

(λ(e) #t)

(λ(e) 3)

(λ(e) 4)

Page 9: CS61A Lecture 25

The body of a lambda gets analyzed!

STk>(mc-eval '(lambda (x) '(if #t 3 4))'(((a) 3)))

(procedure (x) ( ) (((a) 3)))

9

car cdr

((a).(3))

procedure

car cdr

(x)

car cdr

((if (true? ((λ (e) #t) env) ((λ (e) 3) env) ((λ (e) 4) env)))

car cdr car cdr

(if (true? ((λ (e) #t) env) ((λ (e) 3) env) ((λ (e) 4) env))

Page 10: CS61A Lecture 25

The body of a lambda gets analyzed!

STk>(mc-eval '(define f

(lambda (x)

'(if #t 3 4))

'(((a) 3)))

((a).(3))

10

((f a) . (__3))

car cdr

procedure

car cdr

(x)

car cdr

((if (true? ((λ (e) #t) env) ((λ (e) 3) env) ((λ (e) 4) env)))

car cdr car cdrf

Page 11: CS61A Lecture 25

The body of a lambda gets analyzed!

STk>(mce)

;;; M-Eval input:

(f 1)

((a).(3))

11

((f a) . (__3))

car cdr

procedure

car cdr

(x)

car cdr

((if (true? ((λ (e) #t) env) ((λ (e) 3) env) ((λ (e) 4) env)))

car cdr car cdrf

If we call f many times – we save

time!

(define (mc-eval exp env) ((analyze exp) env))

Page 12: CS61A Lecture 25

Lazy Evaluator(played by Lazy Smurf)

12

I’ll get the “actual value” of arguments once

they’re really needed

Page 13: CS61A Lecture 25

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

13

Page 14: CS61A Lecture 25

Thunk ADT (not a STk REAL thunk!)(define (delay-it exp env)

(list 'thunk exp env))

(define (thunk? obj)

(tagged-list? obj 'thunk))

(define (thunk-exp thunk) (cadr thunk))

(define (thunk-env thunk) (caddr thunk))

exp is(A) list representing an expression or (B) REAL Scheme?

14

thunk

car cdr car cdr car cdr

exp env

Thunk list

Page 15: CS61A Lecture 25

What would lazy [eval/smurf] do?STk> (define (square x) (* x x))

STk> (square (+ 2 3))

A. Applicative Order B. Normal Order

Page 16: CS61A Lecture 25

STk> (define x 3)STk> (define (square x) (* x x))

STk> (square (+ 2 x))

Applicative Order Normal Order

BEFORE we call square: figure

out arguments!

This x should come from global!

Page 17: CS61A Lecture 25

If we’re going to delay-it we need to keep track of the environment!

(define (delay-it exp env)

(list 'thunk exp env))

17

thunk

car cdr car cdr car cdr

env(+ 2 x)

When I get forced: evaluate the exp in

this environment

Page 18: CS61A Lecture 25

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

18

Page 19: CS61A Lecture 25

How lazy [smurf] mc-eval evaluates args?!?

(define (mc-eval exp env)

(cond ...

((application? exp)

(mc-apply (mc-eval (operator exp) env)

(list-of-values (operands exp) env)))

19

We’re going to change the range of mc-eval so we’ll have to change

this too.

Page 20: CS61A Lecture 25

Regular mc-apply(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))))

20

procedure

car cdr

(x)

car cdr

((show 2)(show 3))

car cdr car cdr

env

Page 21: CS61A Lecture 25

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

21

Page 22: CS61A Lecture 25

The lazy mc-apply

mc-apply

Compound (User defined)

Procedure

Primitive procedure

22

Don’t evaluate the arguments

DO evaluate the arguments

Page 23: CS61A Lecture 25

In the lazy versionWhere do arguments get delayed?A. In mc-evalB. In mc-applyC. In bothD. In neitherE. ???

23

Page 24: CS61A Lecture 25

In the REGULAR versionWhere do arguments get evaluated?A. In mc-evalB. In mc-applyC. In bothD. In neitherE. ???

24

Page 25: CS61A Lecture 25

In the lazy versionWhere do arguments get evaluated?A. In mc-evalB. In mc-applyC. In bothD. In neitherE. ???

25

Page 26: CS61A Lecture 25

How many of these are different in Normal vs. Applicative order?

(invent one example that isn’t and one that is)STk> 3

3

STk> (define x 3)

x

STk> x

3

STk> 'x

x

26

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

Page 27: CS61A Lecture 25

(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?"))))

27

How many times might we be lazy (and delay

stuff)?A. 0

B. 1-2 C. 3-5 D. 6-8 E.??

Page 28: CS61A Lecture 25

A problem with delaying stuff

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

28

env

User-defined procedure:

Don’t evaluate the arguments

Page 29: CS61A Lecture 25

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

29

Page 30: CS61A Lecture 25

What can be returned by the lazy mc-eval function

What can be retA.ValuesB.ListsC.Thunk ADTsD.All of the aboveE.None of the above

30

Page 31: CS61A Lecture 25

What happens here?

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

31

env

env

Page 32: CS61A Lecture 25

SOLUTION(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))

32

mc-eval might return a delayed argument from a

compound procedure

This was:mc-eval

If the driver-loop needs to print it – make sure you haven’t been TOO lazy.

Page 33: CS61A Lecture 25

actual-value(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))

33

A. (mc-evalB. (actual-value

??

thunk

car cdr car cdr car cdr

exp env

Page 34: CS61A Lecture 25

Example of why we call actual-valueSTk> (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)) )

34

env

Page 35: CS61A Lecture 25

(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?"))))

35

What happens if we pass a

Thunk ADT as exp?

A. errorsB. application

C. lambdaD. self-eval.

E.??

Page 36: CS61A Lecture 25

if’s need actual values!

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

36

mc-eval sometimes returns Thunk ADTs

Page 37: CS61A Lecture 25

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

37

Page 38: CS61A Lecture 25

Compilers

• Analyze syntax • Make something that can be run on a

computer• Provide optimization• Provide useful feedback to the programmer

when there are errors

38

Page 39: CS61A Lecture 25

Frames in MCE(below the line)

((x y) . (2 4))

or

((x y) 2 4 )

39

Globalx: 2y: 4

E1a: 5b: 7c: 3

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

Page 40: CS61A Lecture 25

Environments(below the line)

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

40

Error checking omitted

Page 41: CS61A Lecture 25

Environments(below the line)

List of frames! (define the-empty-environment '())

(extend-environment

'(x y) ;; vars

'(2 4) ;; vals

the-empty-environment) ;; base-env

41

car cdr

((x y).(1 2))

Globalx: 2y: 4

Frame

Environment

Page 42: CS61A Lecture 25

Environments (Below the line)

42

Globalx: 2y: 4

E1a: 5b: 7c: 3

car cdr

((x y).(1 2))

car cdr

((a b c).(5 7 3))