CSE 341 Lecture 21 delayed evaluation; thunks; streams slides created by Marty Stepp

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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CSE341: Programming Languages Lecture 16 Macros Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 15 Mutation, Pairs, Thunks, Laziness, Streams, Memoization Dan Grossman Fall 2011.
6.001 SICP 1 Normal (Lazy) Order Evaluation Memoization Streams.
SICP Variations on a Scheme (2) Beyond Scheme – designing language variants: Lazy evaluation Complete conversion – normal order evaluator Upward.
CSE341: Programming Languages Lecture 15 Macros Dan Grossman Spring 2013.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Scheme & Functional Programming. ( ) >> 64 ( ) >> 666 (* ) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.
Streams and Lazy Evaluation in Lisp and Scheme. Overview Examples of using closures Delay and force Macros Different models of expression evaluation –
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.
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))))))
CS April 2002 Lazy Evaluation, Thunks, and Streams.
1 Lecture 19 Dynamic Scoping Lazy Evaluation (4.2.1, 4.2.2)
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Forms Writing your own procedures CS 480/680 – Comparative Languages.
6.037 Lecture 7B Scheme Variants Normal Order Lazy Evaluation Streams Edited by Mike Phillips & Ben Vandiver Original Material by Eric Grimson & Duane.
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Functional Programming Languages
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
CS 326 Programming Languages, Concepts and Implementation
6.001 SICP Variations on a Scheme
CSE341: Programming Languages Lecture 15 Macros
PPL Lazy Lists.
Principles of programming languages 4: Parameter passing, Scope rules
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
CSE 341 Lecture 20 Mutation; memoization slides created by Marty Stepp
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Winter 2013.
Closures and Streams cs784(Prasad) L11Clos
Streams and Lazy Evaluation in Lisp and Scheme
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 15 Macros
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2013.
Streams Sections 3.5.1,3.5.2 Pages
Lecture 18 Infinite Streams and
Functional Programming
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2016.
Lecture 18.
Dynamic Scoping Lazy Evaluation
CSE341: Programming Languages Lecture 15 Macros
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2018.
6.001 SICP Streams – the lazy way
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Streams and Lazy Evaluation in Lisp and Scheme
6.001 SICP Variations on a Scheme
CSE341: Programming Languages Lecture 15 Macros
Dan Grossman / Eric Mullen Autumn 2017
Announcements Quiz 5 HW6 due October 23
CSE341: Programming Languages Lecture 15 Macros
6.001 SICP Interpretation Parts of an interpreter
CSE 341 Lecture 22 Macros; extending Scheme's syntax
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
More Scheme CS 331.
Streams Contract is the same as pairs...
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 15 Macros
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 15 Macros
Presentation transcript:

CSE 341 Lecture 21 delayed evaluation; thunks; streams slides created by Marty Stepp

2 Lazy evaluation lazy evaluation: delaying a computation until it is needed (or skipping it entirely, if its result is never used) (or avoiding re-computing a previously computed value) Where are some places Java uses lazy evaluation?  short-circuiting booleans with && and ||  skip evaluation of the un-taken branch of an if/else  (advanced) interning of strings  (advanced) classes are not loaded until they are referenced

3 Lazy evaluation in Scheme Scheme mostly uses eager evaluation, but... unused branches of if / cond aren't evaluated (if test expr1 ; true case expr2) ; false case  How could we verify that this is so?

4 Scheme argument evaluation suppose we have the following procedure: (define (foo b e1 e2) (if b (+ e1 e1 e1) ; true case (* e2 e2))) ; false case will the following code evaluate both the expressions? (foo #t (+ 2 3) (* 4 5))  why or why not?

5 Procedures with side effects suppose we create a procedure with a side effect: (define (square x) (display "squaring ") (display x) (newline) (* x x)) what output will the following code produce? (if (> 2 3) (square 4) (square 7))

6 Procedure calls as arguments with the previously defined square plus the code below: (define (foo b e1 e2) (if b (+ e1 e1 e1) ; true case (* e2 e2))) ; false case what output will the following code produce? (foo (> 2 3) (square 4) (square 7))  How can we modify it to evaluate only one of the two?

7 Thunks thunk: A piece of code or wrapper function used to perform a delayed computation.  a value that has already been "thought of"...think → thunk  first used in the influential ALGOL-60 language's compiler  also used as compatibility wrappers; in DLLs, inheritance... thunks are implemented as zero-argument procedures  instead of passing expression e (costly to compute?), pass a 0-arg procedure that, when called, computes/returns e

8 Scheme thunks we can modify our foo procedure to accept thunks: (define (foo b th1 th2) (if b (+ (th1) (th1) (th1)) ; true case (* (th2) (th2)))) ; false case we'll also modify our call to pass two thunks: (foo (> 2 3) (lambda () (square 4)) (lambda () (square 7)))  now what output does the call produce?

9 Problem: re-evaluating thunks our foo procedure evaluates each thunk multiple times: > (foo (= 2 2) (lambda () (square 4)) (lambda () (square 7))) squaring 4 16  how can we stop it from re-computing the same value?

10 Language support for delays (delay (procedure call)) some langs. include syntax to ease delayed computation delay accepts a call and, rather than executing it, wraps it in a structure called a promise that can execute it later: > (define x (delay (square 4))) > x #

11 Forcing a delayed execution (force delay) force accepts a promise, executes it (if necessary), and returns the result > (define x (delay (square 4))) > x # > (force x) 16 > x #

12 Use the force, Luke... we can modify our foo procedure to accept promise s: (define (foo b p1 p2) (if b (+ (force p1) (force p1) (force p1)) (* (force p2) (force p2)))) we'll also modify our call to pass two promises: (foo (> 2 3) (delay (square 4)) (delay (square 7)))  now what output does the call produce?

13 Streams stream: An "infinite" list.  example: the list of all natural numbers: 1, 2, 3, 4,... Whuck?  can't actually be infinite, for obvious reasons  but appears to be infinite, to the code using the list  idea: delay evaluation of each list pair's tail until needed –uses a procedure to describe the element that comes next like Unix pipes: cmd1 | cmd2 ; 2 "pulls" input from 1 1 L

14 Streams in Scheme a stream is a thunk that, when called, returns a pair: (next-answer. next-thunk)  first element: (car (stream))  second element: (car ((cdr (stream))))  third element: (car ((cdr ((cdr (stream)))))) nice division of labor:  stream's creator knows how to generate values  client knows how many are needed, what to do with each valuethunk! call valuethunk! call... car cdr

15 Examples of streams ; an endless list of 1s. (define ones (lambda () (cons 1 ones))) ; a list of all natural numbers: 1, 2, 3, 4,... (define (nat-nums2) (define (helper x) (cons x (lambda () (helper (+ x 1))))) (helper 1)) ; a list of all powers of two: 1, 2, 4, 8, 16,... (define (nat-nums2) (define (helper x) (cons x (lambda () (helper (* x 2))))) (helper 1))

16 Using streams (define ones (lambda () (cons 1 ones))) accessing the elements of a stream:  first element: (car (ones))  second: (car ((cdr (ones))))  third: (car ((cdr ((cdr (ones)))))) fourth: (car ((cdr ((cdr ((cdr (ones) ) ) ))) ))...  Remember, parentheses matter! (e) calls the thunk e.

17 Stream exercises Define a stream called harmonic that holds the elements of the harmonic series: 1 + 1/2 + 1/3 + 1/ Define a stream called fibs that represents the Fibonacci numbers. ALL OF THEM! > (car (fibs)) 1 > (car ((cdr (fibs)))) 1 > (car ((cdr ((cdr (fibs)))))) 2 > (car ((cdr ((cdr ((cdr (fibs)))))))) 3 > (car ((cdr ((cdr ((cdr ((cdr (fibs)))))))))) 5...

18 Useful stream procedures ; convenience procedures to create and examine a stream (define-syntax cons-stream (syntax-rules () ((cons-stream x y) (cons x (delay y))))) (define car-stream car) (define (cdr-stream stream) (force (cdr stream))) (define null-stream? null?) (define null-stream '()) ; returns the first n elements of the given stream (define (stream-section n stream) (cond ((= n 0) '()) (else (cons (head stream) (stream-section (- n 1) (tail stream)))))) ; merges two streams together (define (add-streams s1 s2) (let ((h1 (head s1)) (h2 (head s2))) (cons-stream (+ h1 h2) (add-streams (tail s1) (tail s2)))))

19 Using the stream procedures > (define ones (cons-stream 1 ones)) > (stream-section 7 ones) ( ) > (define (integers-starting-from n) (cons-stream n (integers-starting-from (+ n 1)))) > (define nat-nums (integers-starting-from 1)) > (stream-section 10 nat-nums) ( ) > (define fibs (cons-stream 1 (cons-stream 1 (add-streams (tail fibs) fibs)))) > (stream-section 14 fibs) ( )