מבוא מורחב - שיעור 15 1 Lecture 15 Streams. מבוא מורחב - שיעור 15 2 Streams: Motivation (define (sum-primes a b) (define (iter count accum) (cond ((>

Slides:



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

מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
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.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully.
6.001 SICP 1 Normal (Lazy) Order Evaluation Memoization Streams.
Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator.
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 13. Streams 3.5, pages definitions file on web 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.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
SICP Infinite streams – using lazy evaluation Beyond Scheme – designing language variants: Streams – an alternative programming style!
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
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.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 13: Streams 한 태숙.
Streams and Lazy Evaluation in Lisp and Scheme. Overview Examples of using closures Delay and force Macros Different models of expression evaluation –
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 12. Outline Streams Infinite streams Stream implementation Questions from exams 2.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
Practice session #6: 1. Sequence operations 2. Partial evaluation with currying 3. Lazy-lists.
מבוא מורחב 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 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
PPL Lazy Lists. Midterm 2012 (define sum-vals (λ (ts) (if (ts-simple? ts) (ts-val ts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))
1 Lecture 19 Dynamic Scoping Lazy Evaluation (4.2.1, 4.2.2)
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
1 מבוא מורחב למדעי המחשב בשפת Scheme תרגול Outline Mutable list structure RPN calculator Vectors and sorting.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 6. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Streams Review A Review of Streams Mark Boady. What Are Steams? Delayed lists We pretend that the stream is a list In reality we only know the next value,
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
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
CS314 – Section 5 Recitation 10
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
6.001 SICP Variations on a Scheme
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
PPL Lazy Lists.
(defmacro (delay x) (list 'lambda () x))
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
Closures and Streams cs784(Prasad) L11Clos
Env. Model Implementation
Streams and Lazy Evaluation in Lisp and Scheme
Nondeterministic Evaluation
Streams Sections 3.5.1,3.5.2 Pages
Lecture 18 Infinite Streams and
Lecture 18.
Dynamic Scoping Lazy Evaluation
Lecture #8 מבוא מורחב.
6.001 SICP Streams – the lazy way
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Streams and Lazy Evaluation in Lisp and Scheme
Streams and Lazy Evaluation in Lisp and Scheme
Lecture 13 - Assignment and the environments model Chapter 3
6.001 SICP Data abstractions
List and list operations (continue).
Closures and Streams cs7100(Prasad) L11Clos
Lecture # , , , , מבוא מורחב.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Streams and Lazy Evaluation in Lisp and Scheme
Streams Contract is the same as pairs...
Presentation transcript:

מבוא מורחב - שיעור 15 1 Lecture 15 Streams

מבוא מורחב - שיעור 15 2 Streams: Motivation (define (sum-primes a b) (define (iter count accum) (cond ((> count b) accum) ((prime? count) (iter (+ count 1) (+ count accum))) (else (iter (+ count 1) accum)))) (iter a 0)) (define (sum-primes a b) (accumulate + 0 (filter prime? (enumerate-interval a b)))) Second implementation consumes a lot of storage

מבוא מורחב - שיעור 15 3 Streams: Motivation (Cont.) (car (cdr (filter prime? (enumerate-interval )))) Requires a lot of time and space How can we gain the efficiency of iteration, and retain the elegance of sequence-operations? Streams!

מבוא מורחב - שיעור 17 4 Remember normal order evaluation? Normal (Lazy) Order Evaluation: go ahead and apply operator with unevaluated argument subexpressions evaluate a subexpression only when value is needed –to print –by primitive procedure (that is, primitive procedures are "strict" in their arguments) Compromise approach: give programmer control between normal and applicative order. Streams: lists with delays

מבוא מורחב - שיעור 15 5 Streams interface Constructors: the-empty-stream (cons-stream x y) Predicate: ( stream-null? x) cons-stream treats its second argument as a delayed object. Selectors: (stream-car (cons-stream x y)) = x (stream-cdr (cons-stream x y)) = y

מבוא מורחב - שיעור 15 6 Streams via delay and force (cons-stream ) is a special form equivalent to (cons (delay )) (define (stream-car stream) (car stream)) (define (stream-cdr stream) (force (cdr stream)))

מבוא מורחב - שיעור 15 7 delay and force (delay ) ==> a promise to evaluate exp (force ) ==> evaluate the delayed object and return the result (define x (delay (+ 1 1))) x  # (force x)  2 (delay ) is a special form. force is not a special form.

מבוא מורחב - שיעור 15 8 What are these mysterious delay and force ? delay is a special form such that (delay ) is equivalent to (lambda () ) force is a procedure that calls a procedure produced by delay: (define (force delayed-object) (delayed-object))

מבוא מורחב - שיעור 15 9 Let us recall lists (define (enumerate-interval low high) (if (> low high) ‘() (cons low (enumerate-interval (+ low 1) high)))) (define int123 (enumerate-interval 1 3)) (enumerate-interval 1 3) (cons 1 (enumerate-interval 2 3)) (cons 1 (cons 2 (enumerate-interval 3 3))) (cons 1 (cons 2 (cons 3 (enumerate-interval 4 3))) (cons 1 (cons 2 (cons 3 ‘())))

מבוא מורחב - שיעור Enumerating with streams (define (stream-enumerate-interval low high) (if (> low high) the-empty-stream (cons-stream low (stream-enumerate-interval (+ low 1) high)))) (define s (stream-enumerate-interval 1 3)) (stream-enumerate-interval 1 3) (cons-stream 1 (stream-enumerate-interval 2 3)) (cons 1 (delay (stream-enumerate-interval 2 3))) (cons 1 (lambda () (stream-enumerate-interval 2 3)))

מבוא מורחב - שיעור (define s (stream-enumerate-interval 1 3)) | GE GE p: low high b : (if (> low high) the-empty-stream (cons-stream... ) stream-enumerate-interval: (cons-stream 1 (str-enu-int (+ low 1) high))| E1 (cons 1 (lambda () (str-enu-int (+ low 1) high)) | E1 p: b: (str-enu_int (+ low 1) high) s: 1 low 1 high 3 E1

מבוא מורחב - שיעור (define s1 (stream-cdr s)) (stream-cdr s) (stream-cdr (cons 1 (lambda () (str-enu-int 2 3)))) (force (cdr (cons 1 (lambda () (str-enu-int 2 3))))) (force (lambda () (str-enu-int 2 3))) ((lambda () (str-enu-int 2 3))) (str-enu-int 2 3) (cons-stream 2 (str-enu-int 3 3)) (cons 2 (delay (str-enu-int 3 3))) (cons 2 (lambda () (str-enu-int 3 3))) Calling stream-cdr

מבוא מורחב - שיעור (define s1 (stream-cdr s)) | GE (force (cdr s)) | GE GE p: low high b : (if (> low high) the-empty-stream (cons-stream... ) str-enu-int: s: E1 low 1 1 high 3 p: b:(str-enu-int (+ low 1) high) 2 low 2 high 3 s1:

מבוא מורחב - שיעור stream-ref, stream-map (define (stream-ref s n) (if (= n 0) (stream-car s) (stream-ref (stream-cdr s) (- n 1)))) (define (stream-map proc s) (if (stream-null? s) the-empty-stream (cons-stream (proc (stream-car s)) (stream-map proc (stream-cdr s)))) Also a version with multiple stream arguments

מבוא מורחב - שיעור Stream-filter (define (stream-filter pred stream) (cond ((stream-null? stream) the-empty-stream) ((pred (stream-car stream)) (cons-stream (stream-car stream) (stream-filter pred (stream-cdr stream)))) (else (stream-filter pred (stream-cdr stream)))))

מבוא מורחב - שיעור Applicative vs. Normal order evaluation. (stream-car (stream-cdr (stream-filter prime?(str-enu-int )))) (car (cdr (filter prime? (enu-int )))) Both return the second prime larger or equal to 10 (which is 13) With lists it takes about operations With streams about three.

מבוא מורחב - שיעור How does it work? - I (stream-car (stream-cdr (stream-filter prime? (str-enu-int )))) (stream-car (stream-cdr (stream-filter prime? (cons 10 (delay (str-enu-int )))))) (stream-car (stream-cdr (stream-filter prime? (force (lambda () (str-enu-int )))))) (stream-car (stream-cdr (stream-filter prime? (cons 11 (delay (str-enu-int ))))))

מבוא מורחב - שיעור How does it work? - II (stream-car (stream-filter prime? (stream-cdr (cons 11 (delay (str-enu-int )))))) (stream-car (stream-cdr (cons 11 (delay (stream-filter prime? (stream-cdr (cons 11 (delay (str-enu-int ))))))))) (stream-car (stream-filter prime? (str-enu-int )))

מבוא מורחב - שיעור How does it work? - III (stream-car (stream-filter prime? (cons 12 (delay (str-enu-int ))))) (stream-car (stream-filter prime? (str-enu-int ))) (stream-car (stream-filter prime? (cons 13 (delay (str-enu-int ))))) (stream-car (cons 13 (delay (stream-filter prime? (stream-cdr (cons 13 (delay (str-enu-int )))))))) 13

Memoization Suppose we have the following scenario: (define x (delay (very-hard-function a))) (force x) We need to call the hard function twice. Scheme will automatically detect that this is the second time we try to evaluate the function and use the value we have evaluated before.

How is it done? delay is actually defined as follows: (delay ) translates to (memo-proc (lambda () )) (define (memo-proc proc) (let ((already-run? #f) (result #f)) (lambda () (if (not already-run?) (begin (set! result (proc)) (set! already-run? true) result) result))))

מבוא מורחב - שיעור Infinite streams Since streams are delayed, we are not limited to finite streams!! Some objects are more naturally infinite

מבוא מורחב - שיעור The integers (define (integers-from n) (cons-stream n (integers-from (+ n 1)))) (define integers (integers-from 1))

מבוא מורחב - שיעור Integers not divisible by 7 (define (divisible? x y) (= (remainder x y) 0)) (define no-sevens (stream-filter (lambda (x) (not (divisible? x 7))) integers)) (stream-ref no-sevens 100)  117

מבוא מורחב - שיעור Fibonacci sequence (define (fib-seq-from a b) (cons-stream a (fib-seq-from b (+ a b)))) (define fib-seq (fib-seq-from 0 1))

מבוא מורחב - שיעור Fibonacci sequence  (define (fib-seq-from a b) (cons-stream a (fib-seq-from b (+ a b))))  (define fib-seq (fib-seq-from 0 1))  fib-seq (0. # [fib-seq-from 1 1])  (stream-ref fib-seq 3) (stream-ref (stream-cdr fib-seq) 2) (stream-ref (fib-seq-from 1 1) 2) (stream-ref (cons 1 (delay (fib-seq-from 1 2))) 2) (stream-ref (stream-cdr (cons 1 (delay (fib-seq-from 1 2)) 1) (stream-ref (fib-seq from 1 2) 1) (stream-ref (cons 1 (delay (fib-seq from 2 3))) 1) (stream-ref (stream-cdr (cons 1 (delay (fib-seq-from 2 3)))) 0) (stream-ref (fib-seq-from 2 3) 0) (stream-ref (cons 2 (delay (fib-seq-from 3 5))) 0) 2

מבוא מורחב - שיעור Finding all the primes using the sieve of Eratosthenes

מבוא מורחב - שיעור The sieve of Eratosthenes using streams (define (sieve str) (cons-stream (stream-car str) (sieve (stream-filter (lambda (x) (not (divisible? x (stream-car str)))) (stream-cdr str))))) (define primes (sieve (stream-cdr integers)))

מבוא מורחב - שיעור The integers, again (implicit version) We saw the definition: (define (integers-from n) (cons-stream n (integers-from (+ n 1)))) (define integers (integers-from 1)) An alternative definition: (define integers (cons-stream 1 (stream-map (lambda (x) (+ x 1)) integers)) (integers) integers input to stream-map result: element