Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 20 Lazy Evaluation Continued (4.2.1, 4.2.2) MC-eval examples from exams (Time permitting)

Similar presentations


Presentation on theme: "1 Lecture 20 Lazy Evaluation Continued (4.2.1, 4.2.2) MC-eval examples from exams (Time permitting)"— Presentation transcript:

1 1 Lecture 20 Lazy Evaluation Continued (4.2.1, 4.2.2) MC-eval examples from exams (Time permitting)

2 2 lazy evaluation- reminder (define (l-apply procedure arguments env) ; changed (cond ((primitive-procedure? procedure) (apply-primitive-procedure procedure (list-of-arg-values arguments env))) ((compound-procedure? procedure) (l-eval-sequence (procedure-body procedure) (extend-environment (procedure-parameters procedure) (list-of-delayed-args arguments env) (procedure-environment procedure)))) (else (error "Unknown proc" procedure))))

3 3 Lazy Evaluation – l-eval Most of the work is in l-apply ; need to call it with: actual value for the operator just expressions for the operands the environment... (define (l-eval exp env) (cond ((self-evaluating? exp) exp)... ((application? exp (l-apply (actual-value (operator exp) env) (operands exp) env)) (else (error "Unknown expression" exp))))

4 4 Actual vs. Delayed Values (define (actual-value exp env) (force-it (l-eval exp env))) (define (list-of-delayed-args exps env) (if (no-operands? exps) '() (cons (delay-it (first-operand exps) env) (list-of-delayed-args (rest-operands exps) env)))) (define (list-of-arg-values exps env) (if (no-operands? exps) '() (cons (actual-value (first-operand exps) env) (list-of-arg-values (rest-operands exps) env))))

5 5 Representing Thunks Abstractly – a thunk is a "promise" to return a value when later needed ("forced") Concretely – our representation: thunk envexp

6 6 Thunks – delay-it and force-it (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)) (define (force-it obj) (cond ((thunk? obj) (actual-value (thunk-exp obj) (thunk-env obj))) (else obj))) (define (actual-value exp env) (force-it (l-eval exp env)))

7 7 Lazy Evaluation – other changes needed Example – need actual predicate value in conditional if... (define (l-eval-if exp env) (if (true? (actual-value (if-predicate exp) env)) (l-eval (if-consequent exp) env) (l-eval (if-alternative exp) env))) Example – don't need actual value in assignment... (define (l-eval-assignment exp env) (set-variable-value! (assignment-variable exp) (l-eval (assignment-value exp) env) env) 'ok)

8 8 (l-eval ‘((lambda (x) (+ x x) ) (+ 1 1)) GE) (l-apply (actual-value ‘(lambda (x) (+ x x)) GE) ‘((+1 1 )) GE) (force-it (l-eval ‘(lambda (x) (+ x x)) GE)) ‘(procedure ‘(x) ‘((+ x x)) GE) (l-apply ‘(procedure ‘(x) ‘(+ x x) GE) ‘((+1 1 )) GE) (l-eval ‘(+ x x) E1) x : (‘thunk ‘(+1 1) GE) GE E1 (l-apply (actual-value ‘+ E1) ‘(x x) E1) (l-apply ‘(primitive #[add]) ‘(x x) E1) (apply-primitive-procedure ‘(primitive #[add]) (actual-value x E1)(actual-value x E1) ) (force-it (l-eval x E1)) (force-it (‘thunk ‘(+ 1 1) GE)) (actual-value ‘(+ 1 1) GE) (force-it (l-eval ‘(+ 1 1) GE)) ==> 2

9 9 (l-eval ‘(define f (lambda (x y) x)) GE) (eval-definition ‘(define f (lambda (x y) x)) GE) (define-variable! ‘f (l-eval ‘(lambda (x y) x) GE) GE) f: ‘(procedure ‘(x y) ‘(x) GE) GE

10 10 (l-eval ‘(f 1 1) GE) (l-apply (actual-value ‘f GE) ‘(1 1) GE) (force-it (l-eval ‘f GE)) ==> ‘(procedure ‘(x y) ‘(x) GE) (l-apply ‘(procedure ‘(x y) ‘(x) GE) ‘(1 1) GE) (l-eval ‘x E1) x : (‘thunk 1 GE) y: (‘thunk 1 GE) GE E1 ==> (‘thunk 1 GE)

11 11 (l-eval ‘(f (f 1 1) 1) GE) (l-apply (actual-value ‘f GE) ‘((f 1 1) 1) GE) (force-it (l-eval ‘f GE)) ‘(procedure ‘(x y) ‘(x) GE) (l-apply ‘(procedure ‘(x y) ‘(x) GE) ‘((f 1 1) 1) GE) (l-eval ‘x E1) x : (‘thunk ‘(f 1 1) GE) y: (‘thunk 1 GE) GE E1 ==> (‘thunk ‘(f 1 1) GE) Lets force this thunk..

12 12 (force-it (‘thunk ‘(f 1 1) GE)) (actual-value ‘(f 1 1) GE) (force-it (l-eval ‘(f 1 1) GE)) (force-it (l-apply (actual-value ‘f GE) ‘(1 1) GE)) (force-it (‘thunk 1 GE)) (actual-value 1 GE) (force-it (l-eval 1 GE)) ==> 1

13 13 Memo-izing Thunks Idea: once thunk exp has been evaluated, remember it If value is needed again, just return it rather than recompute thunk envexp Concretely – mutate a thunk into an evaluated-thunk evaluated- thunk result

14 14 Thunks – Memoizing Implementation (define (evaluated-thunk? obj) (tagged-list? obj 'evaluated-thunk)) (define (thunk-value evaluated-thunk) (cadr evaluated-thunk)) (define (force-it obj) (cond ((thunk? obj) (let ((result (actual-value (thunk-exp obj) (thunk-env obj)))) (set-car! obj 'evaluated-thunk) (set-car! (cdr obj) result) (set-cdr! (cdr obj) '()) result)) ((evaluated-thunk? obj) (thunk-value obj)) (else obj)))

15 15 Laziness and Language Design We have a dilemma with lazy evaluation Advantage: only do work when value actually needed Disadvantages –not sure when expression will be evaluated; can be very big issue in a language with side effects –may evaluate same expression more than once Alternative approach: give programmer control! Memoization doesn't fully resolve our dilemma Advantage: Evaluate expression at most once Disadvantage: What if we want evaluation on each use?

16 16 Variable Declarations: lazy and lazy-memo We want to extend the language as follows: (lambda (a (b lazy) c (d lazy-memo))...) "a", "c" are strict variables (evaluated before procedure application "b" is lazy; it gets (re)-evaluated each time its value is actually needed "d" is lazy-memo; it gets evaluated the first time its value is needed, and then that value is returned again any other time it is needed again.

17 17 Controllably Memo-izing Thunks thunk – never gets memoized thunk-memo – first eval is remembered evaluated-thunk – memoized-thunk that has already been evaluated when forced Thunk Memo envexp evaluated- thunk result

18 18 A new version of delay-it Look at the variable declaration to do the right thing... (define (delay-it decl exp env) (cond ((not (declaration? decl)) (l-eval exp env)) ((lazy? decl) (list 'thunk exp env)) ((memo? decl) (list 'thunk-memo exp env)) (else (error "unknown declaration:" decl))))

19 19 Changes to force-it (define (force-it obj) (cond ((thunk? obj) ;eval, but don't remember it (actual-value (thunk-exp obj) (thunk-env obj))) ((memoized-thunk? obj) ;eval and remember (let ((result (actual-value (thunk-exp obj) (thunk-env obj)))) (set-car! obj 'evaluated-thunk) (set-car! (cdr obj) result) (set-cdr! (cdr obj) '()) result)) ((evaluated-thunk? obj) (thunk-value obj)) (else obj)))

20 20 Changes to l-apply Key: in l-apply, only delay "lazy" or "lazy-memo" params make thunks for "lazy" parameters make memoized-thunks for "lazy-memo" parameters (define (l-apply procedure arguments env) (cond ((primitive-procedure? procedure)...) ; as before; apply on list-of-arg-values ((compound-procedure? procedure) (l-eval-sequence (procedure-body procedure) (let ((params (procedure-parameters procedure))) (extend-environment (map parameter-name params) (list-of-delayed-args params arguments env) (procedure-environment procedure))))) (else (error "Unknown proc" procedure))))

21 21 Deciding when to evaluate an argument... Process each variable declaration together with application subexpressions – delay as necessary: (define (list-of-delayed-args var-decls exps env) (if (no-operands? exps) '() (cons (delay-it (first-variable var-decls) (first-operand exps) env) (list-of-delayed-args (rest-variables var-decls) (rest-operands exps) env))))

22 22 Syntax Extensions – Parameter Declarations (define (first-variable var-decls) (car var-decls)) (define (rest-variables var-decls) (cdr var-decls)) (define declaration? pair?) (define (parameter-name var-decl) (if (pair? var-decl) (car var-decl) var-decl)) (define (lazy? var-decl) (and (pair? var-decl) (eq? 'lazy (cadr var-decl)))) (define (memo? var-decl) (and (pair? var-decl) (eq? 'lazy-memo (cadr var-decl))))

23 23 Summary Lazy evaluation – control over evaluation models Convert entire language to normal order Upward compatible extension –lazy & lazy-memo parameter declarations

24 MC-eval Add a new special form – decrease (decrease proc num) If proc is compound, change its body so that it will return a number smaller by num than what it should have If proc is not compound return error Otherwise assume it returns a number

25 ;;; M-Eval input: (define (double x) (+ x x)) ;;; M-Eval value: ok ;;; M-Eval input: (double 3) ;;; M-Eval value: 6 ;;; M-Eval input: (decrease double 1) ;;; M-Eval value: ok ;;; M-Eval input: (double 3) ;;; M-Eval value: 5 ;;; M-Eval input (decrease + 1) Error: can apply decrase only to compound procedures

26 Section A (define (decrease? exp) ___________________________ ) (define (decrease-proc-name exp) ______________________ ) (define (decrease-number exp) _______________________ )

27 (define (decrease? exp) ___(tagged-list? exp 'decrease)___________________ ) (define (decrease-proc-name exp) ____(cadr exp)__________________________ ) (define (decrease-number exp) _____(caddr exp)_______________________ )

28 Section B Decrease returns the new procedure body (define (modify-body proc expressions-list) (set-car! (cddr proc) expressions-list)) (define (eval-decrease exp env) (let* ((proc-name ________________________________ ) (number ________________________________ ) (proc (_________________________ proc-name env))) (cond ( ____________________________________ (error “Can apply decrease only to compound procedures”)) (else (modify-body proc (decrease proc number)) ‘ok))))

29 (define (modify-body proc expressions-list) (set-car! (cddr proc) expressions-list)) (define (eval-decrease exp env) (let* ((proc-name ____(decrease-proc-name exp)________ ) (number ____(decrease-number exp)_____________ ) (proc (_____mc-eval _____________ proc-name env))) (cond ( _____(not (compound-procedure? proc))____________ (error “Can apply decrease only to compound procedures”)) (else (modify-body proc (decrease proc number)) ‘ok))))


Download ppt "1 Lecture 20 Lazy Evaluation Continued (4.2.1, 4.2.2) MC-eval examples from exams (Time permitting)"

Similar presentations


Ads by Google