Streams Contract is the same as pairs...

Slides:



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

Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
0 PROGRAMMING IN HASKELL Chapter 12 – Lazy evaluation and infinite lists Slides not from Hutton.
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
מבוא מורחב - שיעור 15 1 Lecture 15 Streams. מבוא מורחב - שיעור 15 2 Streams: Motivation (define (sum-primes a b) (define (iter count accum) (cond ((>
6.001 SICP 1 Normal (Lazy) Order Evaluation Memoization Streams.
Comp 205: Comparative Programming Languages Lazy Evaluation and Infinite Lists Lecture notes, exercises, etc., can be found at:
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
Chapter 4 Number Theory. Terms Factors Divides, divisible, divisibility Multiple.
מבוא מורחב 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.
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 한 태숙.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
Streams and Lazy Evaluation in Lisp and Scheme. Overview Examples of using closures Delay and force Macros Different models of expression evaluation –
Clojure 4 Sequences 20-Oct-15. Clojure errors (NO_SOURCE_FILE:12) Useless--just means you’re running from the REPL shell java.lang.Exception: EOF while.
S. Haridi and P. Van Roy1 Lazy Execution Seif Haridi KTH Peter Van Roy UCL.
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.
Streams and Lazy Evaluation A signal processing view of computation CD player DA con- verter ampspeakerdigital signal audio signal amplified audio signal.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
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))))))
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
CS321 Functional Programming 2 © JAS Programming with Streams A stream is never finite and could be defined as special polymorphic type data stream.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
CSE 341 Lecture 21 delayed evaluation; thunks; streams slides created by Marty Stepp
Multiplying Integers Objective: To Multiply Integers.
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
CSC 533: Programming Languages Spring 2016
Chapter 5: Control Structures II
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
6.001 SICP Variations on a Scheme
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
(defmacro (delay x) (list 'lambda () x))
Chapter 5: Control Structures II
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Queues Queues Queues.
Closures and Streams cs784(Prasad) L11Clos
COP4020 Programming Languages
CS21b: Structure and Interpretation
Streams and Lazy Evaluation in Lisp and Scheme
Streams Sections 3.5.1,3.5.2 Pages
Clojure 4 Sequences 27-Nov-18.
Lecture 18 Infinite Streams and
Clojure 4 Sequences 5-Dec-18.
Lecture 18.
Queues: Implemented using Arrays
Lecture #8 מבוא מורחב.
6.001 SICP Streams – the lazy way
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Delayed Evaluation Special forms in Scheme (e.g., if and cond) do not use applicative order evaluation Only one of two or more expressions is actually.
Streams, Delayed Evaluation and a Normal Order Interpreter
Streams and Lazy Evaluation in Lisp and Scheme
Streams and Lazy Evaluation in Lisp and Scheme
Lazy Programming Lazy evaluation:
6.001 SICP Data abstractions
List and list operations (continue).
Warm Up Graph each number on a number line. 1. – – –5 –4 –3 –2 –
Closures and Streams cs7100(Prasad) L11Clos
Lecture # , , , , מבוא מורחב.
Clojure 3 1-Jun-19.
Streams and Lazy Evaluation in Lisp and Scheme
Presentation transcript:

Streams Contract is the same as pairs... (head (pair-stream x str)) = x (tail (pair-stream x str)) = str ...but order of evaluation is different (pair-stream x str) evaluates x immediately, delays evaluation of str (tail str) forces evaluation of the tail

Lazy Evaluation compute values only when needed implement with special form delay (delay expr) make a promise to evaluate expr when forced to (force delayed-expr) collect on the promise

An infinite stream Compare (1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... (define (ones <stream>) (pair-stream 1 ones)) Compare (define (ones <list>) (pair 1 ones)) ==> ???

An infinite stream Compare (1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... (define (ones <stream>) (pair-stream 1 ones)) Compare (define (ones <list>) (pair 1 ones)) ==> error: unbound identifier ones

(pair-stream 1 ones) =macro expansion=> (make <pair-stream> head-stream: 1 tail-stream: (delay ones)) tail-stream: (method () ones)) ==> (1 . {proc () ones})

(head ones) ==> (head-stream ones) ==> 1 (tail ones) ==> (force (tail-stream ones)) ==> (force {proc () ones}) ==> ({proc () ones}) ==> ones ==> (1 . {proc () ones}) so ones is its own tail

Natural numbers (define integers-from (method ((n <integer>)) (pair-stream n (integers-from (inc n))))) (integers-from 42) is the infinite stream (42 43 44 45 46 47 48 49 50 51 52 ... (define (naturals <stream>) (integers-from 0)) naturals (0 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

(add-method filter (method ((test <boolean>) (s <stream>)) (cond ((empty? s) empty-stream) ((test (head s)) (pair-stream (head s) (filter test (tail s)))) (else: (filter test (tail s)))))) (define (divisible? <function>) (method ((x <integer>) (n <integer>)) (zero? (modulo x n)))) (define (divisible-by-3? <function>) (method ((x <integer>)) (divisible? x 3))) (define (threes <stream>) (filter divisible-by-3? naturals)) this is the infinite stream (0 3 6 9 12 15 ...

(define (print-stream <function>) (method ((s <stream>)) (print (head s)) (print-stream (tail s)))) (print-stream naturals) What does (print-stream naturals) do? Print 0, force the tail (integers-from (inc 0)) evaluate (inc 0) ==> 1 evaluate integers-from ==> (1 . {proc () (integers-from (inc 1))}) Print 1, force the tail... (2 . {promise (integers-from (inc 2))}) Print 2, force the tail... (3 . {promise (integers-from (inc 3))}) Print 3, force the tail...

Sieve of Eratosthenes (300 BC) 2 is prime a number n > 2 is prime iff it is not divisible by any smaller prime

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 ...

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 ...

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 ...

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 ...

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 ...

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 ...

(define (sieve <function>) (method ((s <stream>)) (bind ((next-prime (head s))) ;head is prime (pair-stream next-prime (sieve ;recursively sieve tail after (filter ;removing all multiples of head (method ((x <integer>)) (not (divisible? x next-prime))) (tail s))))))) (define (primes <stream>) (sieve (integers-from 2))) (print-stream primes) 2 3 5 7 11 13 17 19 23 ...

Componentwise addition of streams (define (add-streams <function>) (method ((a <stream>) (b <stream>)) (cond ((empty-stream? a) b) ((empty-stream? b) a) (else: (pair-stream (+ (head a) (head b)) (add-streams (tail a) (tail b))))))) (add-streams (a0 a1 a2 a3 ...) (b0 b1 b2 b3...)) ==> (a0+b0 a1+b1 a2+b2 a3+b3 ...)

Natural numbers revisited (define (naturals <stream>) (pair-stream 0 (add-streams ones naturals))) naturals = (0 1 2 3 4 5 6 7 8 9 10 11 12 ... ones = (1 1 1 1 1 1 1 1 1 1 1 1 1 ... sum = (1 2 3 4 5 6 7 8 9 10 11 12 13 ... Pair 0 onto the front and we get naturals back: (0 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

Fibonacci numbers F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2) for n  2 (define (fibs <stream>) (pair-stream 0 (pair-stream 1 (add-streams fibs (tail fibs))))) fibs = (0 1 1 2 3 5 8 13 21 34 ... (tail fibs) = (1 1 2 3 5 8 13 21 34 55 ... sum = (1 2 3 5 8 13 21 34 55 89 ... Pair 0 and 1 onto the front and we get fibs back: fibs = (0 1 1 2 3 5 8 13 21 34 55 89 ...

A more efficient version (define (fibs-from <function>) (method ((a <integer>) (b <integer>)) (pair-stream a (fibs-from b (+ a b))))) (define (efficient-fibs <stream>) (fibs-from 0 1)) this version: O(n) to create the first n fibs last version: O(p^n) where p = (1+sqrt(5))/2

Divergent computations It’s possible to create streams with nothing in them that are not the empty stream— even worse, empty? can't detect them! (define (lose <stream>) (filter odd? (filter even? naturals)))

Merge — merge two streams into one Example: joint bank account Dexter stream: 1/1/98 23:59 deposit $500 1/15/98 23:59 deposit $500 2/1/98 23:59 deposit $500 2/15/98 23:59 deposit $500 ... Fran stream: 1/3/98 10:36 withdraw $200 1/7/98 14:15 withdraw $150 1/10/98 15:44 withdraw $500 1/19/98 9:05 withdraw $825

Merge by date & time so that all transactions on the output stream are in the right order merge account balances Dexter Fran

merge (1 2 3 5 6 9 10 12 17 18 19 20 ... (1 3 6 12 17 18 20 ... (2 5 9 10 19 22 31 ...

(define (merge <function>) (method ((s1 <stream>) (s2 <stream>)) (cond ((empty? s1) s2) ((empty? s2) s1) ((< (head s1) (head s2)) (pair-stream (head s1) (merge (tail s1) s2))) (else: (head s2) (merge s1 (tail s2)))))))