6.001 SICP 1 Normal (Lazy) Order Evaluation Memoization Streams.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Advertisements

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.
1 Programming Languages (CS 550) Lecture 7 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts used in EMF. Read.
מבוא מורחב למדעי המחשב תרגול 13 Lazy Evaluation. Lazy Evaluation Implementation Two types of values – Delayed Values – Actual Values Introduce two functions.
Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully.
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.
(define applicative-eval (lambda (exp) (cond ((atomic? exp) (eval-atomic exp)) ((special-form? exp) (eval-special-form exp)) ((list-form? exp) (eval-list.
מבוא מורחב - שיעור 15 1 Lecture 15 Streams. מבוא מורחב - שיעור 15 2 Streams: Motivation (define (sum-primes a b) (define (iter count accum) (cond ((>
1 Lecture 18 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure.
Extended Introduction to CS Preparation for Final Exam.
Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator.
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.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 13. Streams 3.5, pages definitions file on web 2.
Picking and Choosing… …Amb Above the Line. Intro to Nondeterminism So before we programmed to compute an answer to a problem. What if we set up a system.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 21. Overview 1. Dynamic Binding 2. Lazy Evaluation 3. More MC-Eval 2.
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.
SICP Infinite streams – using lazy evaluation Beyond Scheme – designing language variants: Streams – an alternative programming style!
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,
Cs776 (Prasad)L15strm1 Reasoning with Functional Programs Optimization by source to source transformation Well-founded induction Streams : Infinite lists.
SICP Variations on a Scheme (2) Beyond Scheme – designing language variants: Lazy evaluation Complete conversion – normal order evaluator Upward.
Closures and Streams More on Evaluations CS784(pm)1.
1 Lecture OO, the notion of inheritance 3 Stacks in OO style (define (make-stack) (let ((top-ptr '())) (define (empty?) (null? top-ptr)) (define.
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 13: Streams 한 태숙.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 15: Meta-Circular Evaluator 한 태숙.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
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 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.
1 מבוא מורחב למדעי המחשב בשפת Scheme תרגול Outline Mutable list structure RPN calculator Vectors and sorting.
1 Lecture 19 Review last lecture on evaluator, Dynamic Scoping Lazy Evaluation (4.2.1, 4.2.2)
6.037 Lecture 7B Scheme Variants Normal Order Lazy Evaluation Streams Edited by Mike Phillips & Ben Vandiver Original Material by Eric Grimson & Duane.
CSE 341 Lecture 21 delayed evaluation; thunks; streams slides created by Marty Stepp
(Thunking about Thunks)
Operational Semantics of Scheme
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
6.001 SICP Compilation Context: special purpose vs. universal machines
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Closures and Streams cs784(Prasad) L11Clos
Env. Model Implementation
Original material by Eric Grimson
Nondeterministic Evaluation
Streams Sections 3.5.1,3.5.2 Pages
The Metacircular Evaluator
Lecture 18 Infinite Streams and
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
6.001 SICP Streams – the lazy way
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
6.001 SICP Variations on a Scheme
6.001 SICP Interpretation Parts of an interpreter
topics interpreters meta-linguistic abstraction eval and apply
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

6.001 SICP 1 Normal (Lazy) Order Evaluation Memoization Streams

6.001 SICP 2 Normal Order (Lazy) Evaluation Alternative models for computation: Applicative Order: evaluate all arguments, then apply operator Normal Order: pass unevaluated expression to function evaluate only when needed for primitive operation

6.001 SICP 3 Applicative Order vs. Normal (Lazy) Order (define (foo x) (write-line "inside foo") (+ x x)) (foo (begin (write-line "eval arg") 222)) We first evaluated argument, then substituted value into the body of the procedure eval arg => 444 inside foo eval arg => 444 As if we substituted the unevaluated expression in the body of the procedure Applicative Order: Normal (Lazy) Order:

6.001 SICP 4 Applicative vs. Normal? (define (try a b) (if (= a 0) 1 b)) (try 0 (/ 1 0)) (try 0 (pi-to-this-many-digits ))

6.001 SICP 5 Exercise – Problem with Applicative Order Why cant we use define to define any special form? E.g write a procedure safe/ that only divide if b <> 0 (define (unless pred usual exc) (if pred exc usual)) (define (safe/ a b) (unless (= b 0) (/ a b) 0) (define (safe/ a b) (cond ((= b 0) 0) (else (/ a b))))

6.001 SICP 6 How can we implement lazy evaluation? (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))))

6.001 SICP 7 Thunks – a delayed argument Abstractly – a thunk is a "promise" to return a value when later needed ("forced") Concretely – our representation: thunk envexp (delay-it exp env) – promise to eval exp later (force-it exp) – eval exp now, leave no thunks

6.001 SICP 8 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)))

6.001 SICP 9 Exercise – Applicative vers Normal Order Write a function normal-order? That returns #t if the language is normal order and #f if the language is applicative order. (define (normal-order?) (let ((x #t)) ((lambda (x) ’()) (begin (set! x #f) ’())) x))

6.001 SICP 10 Memo-izing evaluation In lazy evaluation an arg is reevaluate each time it is used In applicative order evaluation argument is evaluated once Can we keep track of values once we’ve obtained them, and avoid cost of reevaluation? thunk envexp evaluated- thunk result

6.001 SICP 11 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)))

6.001 SICP 12 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?

6.001 SICP 13 Variable Declarations: lazy and lazy-memo Handle lazy and lazy-memo extensions in an upward- compatible fashion.; (lambda (a (b lazy) c (d lazy-memo))...) "a", "c" are the usual kind of variables (evaluated before procedure application "b" is lazy, “Normal Order”: 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.

6.001 SICP 14 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))))

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

6.001 SICP 16 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))))

6.001 SICP 17 Change 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)))

6.001 SICP 18 Order Comparison (define (foo x) (write-line "inside foo") (+ x x)) (foo (begin (write-line "eval arg") 222)) eval arg => 444 inside foo eval arg => 444 Applicative Order: Normal (lazy) Order: Memoized Normal (Lazy-memo) Order: inside foo eval arg => 444

6.001 SICP 19 Exercise Given this definition of l-abs: (define (l-abs (i lazy)) (if (< i 0) (- i) i)) Trace the following use of l-abs: (define (down) (begin (set! x (- x 2)) x) (define x 3) (l-abs (down))

6.001 SICP 20 Stream Object A pair-like object, except the cdr part is lazy (not evaluated until needed): a thunk-memo a value stream-car cons-stream stream-cdr (define (cons-stream x (y lazy-memo)) (cons x y)) (define stream-car car) (define stream-cdr cdr)

6.001 SICP 21 What will these print? (ex1) (define s (cons 5 (begin (write-line 7) 9))) (car s) (cdr s) (define t (cons-stream 5 (begin (write-line 7) 9))) (stream-car t) (stream-cdr t)

6.001 SICP 22 Decoupling computation from description Can separate order of events in computer from apparent order of events in procedure description (list-ref (filter (lambda (x) (prime? x)) (enumerate-interval )) 100) (stream-ref (stream-filter (lambda (x) (prime? x)) (stream-interval )) 100)

6.001 SICP 23 Decoupling computation from description (define (stream-interval a b) (if (> a b) the-empty-stream (cons-stream a (stream-interval (+ a 1) b)))) (define (filter-stream pred str) (if (pred (stream-car str)) (cons-stream (stream-car str) (filter-stream pred (stream-cdr str))) (filter-stream pred (stream-cdr str))))

6.001 SICP 24 Decoupling computation from description (define seq (stream-interval 1 10)) (define y (stream-filter even? seq)) Now! Lets do some calculation... (stream-ref y 3) -> 8

6.001 SICP 25 Creating streams There are two basic ways to create streams: 1.Build them up from scratch 2.Modify or combine existing streams

6.001 SICP 26 Creating infinite streams (define ones (cons-streams 1 ones)) (define (add-streams s1 s2) (cons-stream (+ (stream-car s1) (stream-car s2)) (add-streams (stream-cdr s1) (stream-cdr s2))))) (define integers (cons-stream 1 (add-streams ones integers))

6.001 SICP 27 Creating fibonacci streams (ex4) Write a procedure, (fibs), that returns a stream of Fibonacci numbers. The Fibonacci series goes ( ) It starts with two 1's, then each successive value is the sum of the previous two.

6.001 SICP 28 Creating fibonacci streams Write a procedure, (fibs), that returns a stream of Fibonacci numbers. The Fibonacci series goes ( ) It starts with two 1's, then each successive value is the sum of the previous two. (define fibs (cons-stream 0 (cons-stream 1 (add-stream (stream-cdr fibs) fibs))))

6.001 SICP 29 Add-Streams == Map2 (add-streams integers integers) == (map2-stream + integers integers) (define (map2-stream proc str1 str2) (cons-stream (proc (stream-car str1) (stream-car str2)) (map-stream2 proc (stream-cdr str1) (stream-cdr str2))))