CS 3015 Lecture 24 - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

CS 3015 Lecture 24

Description:

The main changes are in the handling of procedure application. The application? ... thunks when procedures are applied to arguments and to force these thunks later ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 16
Provided by: JVB4
Category:
Tags: force | in | lecture | procedure

less

Transcript and Presenter's Notes

Title: CS 3015 Lecture 24


1
CS 3015 Lecture 24
  • Normal and Applicative order evaluation

2
Recall
  • Scheme is an applicative-order language
  • all the arguments to Scheme procedures are
    evaluated when the procedure is applied
  • Normal-order languages delay evaluation of
    procedure arguments until the actual argument
    values are needed
  • Delaying evaluation is called lazy evaluation

3
An advantage of Lazy Evaluation
  • An advantage of lazy evaluation is that some
    procedures can do useful computation even if
    evaluation of some of their arguments would
    produce errors or would not terminate

4
An Interpreter with Lazy Evaluation
  • We will modify the interpreter so that
  • Compound procedures arguments will be handled
    lazily
  • Primitive procedures will remain as before
  • Basic idea of the modification is that, when
    applying a procedure, the interpreter must
    determine which arguments are to be evaluated and
    which are to be delayed
  • The delayed arguments are not evaluated
  • they are transformed into objects called thunks

5
Basic idea of Thunks
  • Similar to delay except must contain the
    information required to produce the value of the
    argument when it is needed
  • i.e., must contain the argument expression and
    the environment in which the procedure
    application is being evaluated
  • Just as with delay, the process of evaluating the
    expression in a thunk is called forcing

6
Forcing Thunks
  • A thunk is forced when
  • It is passed to a primitive procedure that will
    use the value of the thunk
  • It is the value of a predicate of a conditional
  • It is the value of an operator that is about to
    be applied as a procedure
  • Just like with delay/force, we can memoize the
    value of a thunk the first time it is forced

7
Modifying the Evaluator
  • The main changes are in the handling of procedure
    application
  • The application? clause of eval becomes
  • ((application? exp) (apply 
  • (actual-value (operator exp) env)   (operands e
    xp)   env))
  • Notice we pass the actual operands and the
    environment

8
Actual-value
  • (define (actual-value exp env)  (force-it (eval e
    xp env)))
  • Apply must be changed because eval has passed in
    unevaluated operand expressions
  • For primitive procedures, evaluate all the
    arguments before applying the primitive
  • For compound procedures, delay all the arguments
    before applying the procedure

9
New definition of apply
  • (define (apply procedure arguments env)(cond ((pr
    imitive-procedure? procedure)       (apply-primit
    ive-procedure         procedure        
    (list-of-arg-values 
  • arguments env)))  

10
  • ((compound-procedure? procedure)     
     (eval-sequence       (procedure-body procedure)
           (extend-environment        (procedure-para
    meters procedure)        (list-of-delayed-args 
  • arguments env)         (procedure-environment
     procedure))))   (else    (error     " proced
    ure))))

11
Handling argument lists
  • (define (list-of-arg-values exps env)(if (no-oper
    ands? exps)    '()    (cons
  • (actual-value (first-operand exps) env)   
      (list-of-arg-values (rest-operands exps)       
                      env))))
  • (define (list-of-delayed-args exps env)(if (no-op
    erands? exps)    '()    (cons (delay-it (first-o
    perand exps) env)          (list-of-delayed-args 
  • (rest-operands exps)               env))))

12
Eval-if
  • The only other change required is in eval-if
  • The predicate must be obtained with actual-value
    instead of eval
  • (define (eval-if exp env)  (if (true? 
  • (actual-value (if-predicate exp) 
  • env))      (eval (if-consequent exp) env) 
         (eval (if-alternative exp) env)))

13
Representing Thunks
  • The evaluator must arrange to create thunks when
    procedures are applied to arguments and to force
    these thunks later
  • A thunk must package an expression together with
    the environment, so that the argument can be
    produced later
  • To force the thunk, we simply extract the
    expression and environment from the thunk and
    evaluate the expression in the environment

14
Definition of force-it
  • (define (force-it obj)(if (thunk? obj)    (actua
    l-value 
  • (thunk-exp obj) 
  • (thunk-env obj))    obj))

15
Representing Thunks
  • (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))
Write a Comment
User Comments (0)
About PowerShow.com