מבוא מורחב למדעי המחשב בשפת Scheme תרגול 21. Overview 1. Dynamic Binding 2. Lazy Evaluation 3. More MC-Eval 2.

Slides:



Advertisements
Similar presentations
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Scheme in Scheme?!? …the metacircular evaluator….
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
1 Programming Languages (CS 550) Operational Semantics of Scheme using Substitution and the Lambda Calculus Jeremy R. Johnson TexPoint fonts used in EMF.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 10. Environment Model 3.2, pages
1 Programming Languages (CS 550) Lecture 7 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts used in EMF. Read.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Environment Model 3.2, pages
מבוא מורחב למדעי המחשב תרגול 13 Lazy Evaluation. Lazy Evaluation Implementation Two types of values – Delayed Values – Actual Values Introduce two functions.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Metacircular Evaluator 4.1, pages definitions file on web 2.
(define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list.
1 Lecture 18 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
6.001 SICP 1 Normal (Lazy) Order Evaluation Memoization Streams.
Extended Introduction to CS Preparation for Final Exam.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 11. Dotted tail notation (define (proc m1 m2. opt) ) Mandatory Arguments: m1, m2 Mandatory Arguments: m1, m2.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 26: In Praise of Idleness.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Extended Introduction to CS Preparation for Final Exam.
Lexical vs. Dynamic Scope …where do I point to?. Intro… Remember in Scheme whenever we call a procedure we pop a frame and point it to where the procedure.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Scheme More MCE examples. Q1 new special form which defines global variables (static ) search the global environment – Variable exists: does nothing,
SICP Variations on a Scheme (2) Beyond Scheme – designing language variants: Lazy evaluation Complete conversion – normal order evaluator Upward.
1 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure + symbol.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is aa good way to learn more about programming languages  Interpreters are.
1 The metacircular evaluator (Cont.) Defining new procedures (define (lambda? e) (tag-check e 'lambda)) (define (eval exp env) (cond ((number? exp)
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 15: Meta-Circular Evaluator 한 태숙.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
1 Subt. model interpreter: Structure of the interpreter ASP Derived expressions Core Test Data structures Utils A racket lib Proc / Primitive-Proc. Global.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
The environment-based operational semantics Chapter
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
1 Lecture 20 Lazy Evaluation Continued (4.2.1, 4.2.2) MC-eval examples from exams (Time permitting)
1 Read-Eval-Print Loop (define (driver-loop) (prompt-for-input input-prompt) (let ((input (read))) (let ((output (eval input the-global-env))) (announce-output.
1 Subt. model interpreter: Introduction Abstract Syntax Parser Abstract Syntax Parser Derived Expressions Core Test Data Structures Utils (a racket lib)
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Lecture 19 Dynamic Scoping Lazy Evaluation (4.2.1, 4.2.2)
1/ SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Making the environment model concrete Defining eval defines the language –Provides.
CS 598 Scripting Languages Design and Implementation 10. Interpreters Part I: Lisp.
1 Lecture 19 Review last lecture on evaluator, Dynamic Scoping Lazy Evaluation (4.2.1, 4.2.2)
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Evaluators for Functional Programming 1. How to describe (specify) a programming language? 1.Syntax: atoms, primitives, combination and abstraction means.
6.037 Lecture 7B Scheme Variants Normal Order Lazy Evaluation Streams Edited by Mike Phillips & Ben Vandiver Original Material by Eric Grimson & Duane.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Operational Semantics of Scheme
6.001 SICP Variations on a Scheme
The interpreter.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
Env. Model Implementation
Original material by Eric Grimson
The Metacircular Evaluator
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
Lecture 13 - Assignment and the environments model Chapter 3
6.001 SICP Variations on a Scheme
Lecture 14: The environment model (cont
6.001 SICP Interpretation Parts of an interpreter
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

מבוא מורחב למדעי המחשב בשפת Scheme תרגול 21

Overview 1. Dynamic Binding 2. Lazy Evaluation 3. More MC-Eval 2

3 Dynamic vs. Static Binding (define (factorial n) (if (= n 1) 1 (* n (factorial (- n 1))))) Draw environments in static and dynamic binding for (factorial 5) What is the maximal number of environments that the ‘ * ’ operator is looked for (in one search)? (Appeared on a final exam)

4 Variations on a Scheme: dynamic MCE Lexical (static) binding Free variables are searched in the surrounding lexical environment Dynamic binding Free variables are searched in calling procedures.

5 Static binding GE factorial: p: n b: (if (= n 1) 1…) n:5n:4n:3n:2n:1

6 GE factorial: p: n b: (if (= n 1) 1…) n:5n:4n:3 n:2n:1 Dynamic binding

7 Example: if>=< Format: (if>= if= if<) exp1 and exp2 evaluate to numerical values if exp1>exp2 evaluate and return if> if exp1=exp2 evaluate and return if= otherwise, evaluate and return if<

8 Predicate and Selectors (define (if>=<? exp) (tagged-list? exp ‘if>=<)) (define (if>=<exp1 exp) (cadr exp)) (define (if>=<exp2 exp) (caddr exp)) (define (if> exp) (cadddr exp)) (define (if= exp) (list-ref exp 4)) (define (if< exp) (list-ref exp 5))

9 mc-eval implementation (define (eval-if>=< exp env) (let ((exp1 (mc-eval (if>=<exp1 exp) env)) (exp2 (mc-eval (if>=<exp2 exp) env)) (cond ((> exp1 exp2) (mc-eval (if> exp) env)) ((= exp1 exp2) (mc-eval (if= exp) env)) (else (mc-eval (if< exp) env)))))

10 Lazy Evaluation Normal order - delay operands (only) Special forms and compound procedures may return delayed objects Primitives must receive the actual value Print-on-screen is actual value (l-eval exp env) may return delayed value (actual-value exp env) returns real value We use actual-value only if we have to, otherwise we use l-eval

11 delayed if>=< (define (eval-if>=< exp env) (let ((exp1 (actual-value (if>=<exp1 exp) env)) (exp2 (actual-value (if>=<exp2 exp) env)) (cond ((> exp1 exp2) (l-eval (if> exp) env)) ((= exp1 exp2) (l-eval (if= exp) env)) (else (l-eval (if< exp) env)))))

12 Example In mc-eval (define count 0) (define (id x) (set! count (+ count 1)) x) (define w (id (id 10))) count  w  count 

13 In l-eval (define count 0) (define (id x) (set! count (+ count 1)) x) (define w (id (id 10))) count  w  count  Example (cont.)

14 delay and memoization (define (square x) (* x x)) (define count 0) (define (id x) (set! count (+ count 1)) x) (square (id 10))  ? count  ?

15 Practice Question When a procedure with no parameters is applied, an empty frame is opened Change the MCE code such that parameter-less procedures will be applied in the parent environment Is the modified evaluator equivalent to the original one? Explain or give a contradicting example

16 Modification to 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 ….)))) (if (null? (procedure-parameters procedure)) (eval-sequence (procedure-body procedure) (procedure-environment procedure)) )

17 Practice Question Suppose we can manipulate environments as if they were data types, using the following imaginary special forms: (this-env) (get-env proc) (set-env! proc env)

18 Example (define x 2) (define (f y) (+ x y)) (define (make-env x) (this-env)) GE x:2 f: make-env p: y b: (+ x y) p: x b: (this-env)

19 (define env1 (make-env 3)) GE x:2 f: env1 make-env p: y b: (+ x y) p: x b: (this-env) E1 x:3 Example (cont.)

20 (set-env! f env1) GE x:2 f: env1 make-env p: y b: (+ x y) p: x b: (this-env) E1 x:3 Example (cont.)

21 Section A What will evaluating the following code return? Apply changes to the env. diagram. (f 5) (define (mystery g) (define (aux x y) (set-env! g (this-env))) (aux 1 4)) (mystery f) (f 5)

22 (f 5) GE x:2 f: env1 make-env p: y b: (+ x y) p: x b: (this-env) E1 x:3 E2 y:5 Solution

23 (mystery f) GE x:2 f: env1 make-env mystery: E1 x:3 E2 y:5 E3 g: aux: E4 x:1 y:4 Solution (cont.)

24 (f 5) GE x:2 f: env1 make-env mystery: E1 x:3 E2 y:5 E3 g: aux: E4 x:1 y:4 E5 y:5 Solution (cont.)

25 Section B Complete (call-from-env proc vars env) which applies proc on vars from environment env, instead of proc ’s original environment When returning, proc ’s environment is restored to its original environment

26 Code (define (call-from-env proc vars env (let ((original-env )) ? (let ((result )) ? ? ))) (get-env proc) (set-env! proc env) (apply proc vars) (set-env! proc original-env) result

27 Section C,D,E Add support for this-env, get-env and set- env! in the mc-eval The representation of an environment is the interior data structure inside the evaluator Relaxation: In set-env! you may assume that the proc operand is evaluated to a compound (not primitive) procedure

28 mc-eval additions Add in mc-eval: ((this-env? exp) (eval-this-env exp env)) ((get-env? exp) (eval-get-env exp env)) ((set-env? exp) (eval-set-env exp env))

29 Predicates (define (this-env? exp) ) (define (get-env? exp) ) (define (set-env? exp) ) (tagged-list? exp ‘this-env) (tagged-list? exp ‘get-env) (tagged-list? exp ‘set-env!)

30 Selectors (this-env) no selectors (get-env proc) (define (get-env-proc exp) (cadr exp)) (set-env! proc env) (define (set-env-proc exp) (cadr exp)) (define (set-env-env exp) (caddr exp))

31 eval-this-env eval-this-env is simply: (define (eval-this-env exp env) env)

32 eval-get-env (define (eval-get-env exp env) (let ((proc _______________________________)) (if (primitive-procedure? proc) _______________________________ _______________________________ ))) (mc-eval (get-env-proc exp) env) the-global-environment (procedure-environment proc)

33 eval-set-env (define (eval-set-env exp env) (let ((proc ________________________________ ) (new-env ________________________________ )) __________________________________ )) Reminder: a compound procedure is represented by: (list ‘procedure parameters body env) (mc-eval (set-env-proc exp) env) (mc-eval (set-env-env exp) env) (set-car! (cdddr proc) new-env)