Cse536 Functional Programming Lecture #14, Nov. 10, 2004 Programming with Streams –Infinite lists v.s. Streams –Normal order evaluation –Recursive streams.

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Advertisements

22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
0 PROGRAMMING IN HASKELL Chapter 12 – Lazy evaluation and infinite lists Slides not from Hutton.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Advanced Programming Handout 4. Introductions  Me: Benjamin C. Pierce (known as Benjamin, or, if you prefer, Dr. Pierce, but not Ben or Professor) 
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
Cse536 Functional Programming 1 6/23/2015 Lecture #17, Dec. 1, 2004 Todays Topics – Higher Order types »Type constructors that take types as arguments.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Recursion.
Cse502 Functional Programming 1 6/26/2015 Lecture #11, Nov. 4, 2002 Todays Topics –Using Calculation to prove 2 functions are equal »An example from class.
Chapter 14 Programming With Streams. Streams  A stream is an infinite sequence of values.  We could define a special data type for them: data Stream.
Advanced Programming Handout 11 Programming With Streams (SOE Chapter 14)
Chapter 5 Polymorphic and Higher-Order Functions.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Functional Programming Notes for CSCE 190 Based on Sebesta,
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Chapter 1: Number Patterns 1.2: Mathematical Patterns Essential Question: How can recognizing number patterns help in making a decision?
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Cs7120 (Prasad)L9-RECUR-IND1 Recursion and Induction.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
COMP313A Functional Programming (1)
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
CATCH 1 : Case and Termination Checking for Haskell Neil Mitchell (supervised by Colin Runciman) 1 Name courtesy of Mike Dodds.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
Kyung-Goo Doh Hanyang University - ERICAComputer Science & Engineering Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
CS5205Haskell1 CS5205: Foundation in Programming Languages Basics of Functional Programming.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Recursion Higher Order Functions CSCE 314 Spring 2016.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Functional Programming Lecture 3 - Lists Muffy Calder.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
CS321 Functional Programming 2 © JAS Programming with Streams A stream is never finite and could be defined as special polymorphic type data stream.
8.1 – Sequences and Series. Sequences Infinite sequence = a function whose domain is the set of positive integers a 1, a 2, …, a n are the terms of the.
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Lecture 12.
Conditional Expressions
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
Theory of Computation Lecture 4: Programs and Computable Functions II
A lightening tour in 45 minutes
Haskell Chapter 4.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Winter 2013.
CSE 311: Foundations of Computing
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
Proving Properties of Recursive List Functions
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2013.
Streams Sections 3.5.1,3.5.2 Pages
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2016.
PROGRAMMING IN HASKELL
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.
PROGRAMMING IN HASKELL
Streams, Delayed Evaluation and a Normal Order Interpreter
PROGRAMMING IN HASKELL
CSE-321 Programming Languages Introduction to Functional Programming
?? Programming with Streams Reading assignment
PROGRAMMING IN HASKELL
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2019.
Presentation transcript:

Cse536 Functional Programming Lecture #14, Nov. 10, 2004 Programming with Streams –Infinite lists v.s. Streams –Normal order evaluation –Recursive streams –Stream Diagrams –Lazy patterns –memoization –Inductive properties of infinite lists Reading assignment –Chapter 14. Programming with Streams –Chapter 15. A module of reactive animations

Cse536 Functional Programming Infinite lists v.s. Streams data Stream a = a :^ Stream a A stream is an infinite list. It is never empty We could define a stream in Haskell as written above. But we prefer to use lists. This way we get to reuse all the polymorphic functions on lists.

Cse536 Functional Programming Infinite lists and bottom twos = 2 : twos twos = 2 : (2 : twos) twos = 2 : (2 : (2 : twos)) twos = 2 : (2 : (2 : (2 : twos))) bot :: a bot = bot What is the difference between twos and bot ? Sometimes we write z for bot

Cse536 Functional Programming Normal Order evaluation Why does head(twos) work? –Head (2 : twos) –Head(2 : (2 : twos)) –Head (2: (2 : (2 : twos))) The outermost – leftmost rule. Outermost –Use the body of the function before its arguments Leftmost –Use leftmost terms: (K 4) (5 + 2) –Be careful with Infix: (m + 2) `get` (x:xs)

Cse536 Functional Programming Normal order continued Let let x = y + 2 z = x / 0 in if x=0 then z else w Where f w = if x=0 then z else w where x = y + 2 z = x / 0 Case exp’s –case f x of [] -> a ; (y:ys) -> b

Cse536 Functional Programming Recursive streams fibA 0 = 1 fibA 1 = 1 fibA n = fibA(n-1) + fibA(n-2) Unfold this a few times fibA 8 = fibA 7 + fibA 6 = (fibA 6 + fibA 5) + (fibA 5 + fibA 4) = ((fibA 5 + fibA 4) + (fibA 4 + fibA 3)) +((fibA 4 + fibA 3) + (fibA 3 + fibA 2))

Cse536 Functional Programming Fibonacci Stream fibs :: [ Integer ] fibs = 1 : 1 : (zipWith (+) fibs (tail fibs)) This is much faster! And uses less resources. Why? … fibonacci sequence … tail of fibonacci sequence … tail of tail of fibonacci sequence

Cse536 Functional Programming Abstract on tail of fibs fibs = 1 : 1 : (add fibs (tail fibs)) = 1 : tf where tf = 1 : add fibs (tail fibs) = 1 : tf where tf = 1 : add fibs tf Abstract on tail of tf = 1 : tf where tf = 1 : tf2 tf2 = add fibs tf Unfold add = 1 : tf where tf = 1 : tf2 tf2 = 2 : add tf tf2 Add x y = zipWith (+) x y

Cse536 Functional Programming Abstract and unfold again fibs = 1 : tf where tf = 1 : tf2 tf2 = 2 : add tf tf2 = 1 : tf where tf = 1 : tf2 tf2 = 2 : tf3 tf3 = add tf tf2 = 1 : tf where tf = 1 : tf2 tf2 = 2 : tf3 tf3 = 3 : add tf2 tf3 tf is used only once, so eliminate = 1 : 1 : tf2 where tf2 = 2 : tf3 tf3 = 3 : add tf2 tf3

Cse536 Functional Programming Again This can go on forever. But note how the sharing makes the inefficiencies of fibA go away. fibs = 1 : 1 : 2 : tf3 where tf3 = 3 : tf4 tf4 = 5 : add tf3 tf4 fibs = 1 : 1 : 2 : 3 : tf4 where tf4 = 5 : tf5 tf5 = 8 : add tf4 tf5

Cse536 Functional Programming Stream Diagrams (:) add fibs = [1,1,2,3,5,8,…] [1,2,3,5,8, …] 1 1 [2,3,5,8,…] Streams are “wires” along which values flow. Boxes and circles take wires as input and produce values for new wires as output. Forks in a wire send their values to both destinations A stream diagram corresponds to a Haskell function (usually recursive)

Cse536 Functional Programming Example Stream Diagram counter :: [ Bool ] -> [ Int ] counter inp = out where out = if* inp then* 0 else* next next = [0] followedBy map (+ 1) out [0] if* 0 inp out +1 next

Cse536 Functional Programming Example counter :: [ Bool ] -> [ Int ] counter inp = out where out = if* inp then* 0 else* next next = [0] followedBy map (+ 1) out [0] if* 0 F out next

Cse536 Functional Programming Example counter :: [ Bool ] -> [ Int ] counter inp = out where out = if* inp then* 0 else* next next = [0] followedBy map (+ 1) out [0] if* 0 F:F.. 0: :1.. 1:2.. out next

Cse536 Functional Programming Example counter :: [ Bool ] -> [ Int ] counter inp = out where out = if* inp then* 0 else* next next = [0] followedBy map (+ 1) out [0] if* 0 F:F:T.. 0:1: :1:2 1:2:1.. out next

Cse536 Functional Programming Client, Server Example type Response = Integer type Request = Integer client :: [Response] -> [Request] client ys = 1 : ys server :: [Request] -> [Response] server xs = map (+1) xs reqs = client resps resps = server reqs Typical. A set of mutually recursive equations

Cse536 Functional Programming reqs = client resps = 1 : resps = 1 : server reqs Abstract on (tail reqs) = 1 : tr where tr = server reqs Use definition of server = 1 : tr where tr = 2 : server reqs abstract = 1 : tr where tr = 2 : tr2 tr2 = server reqs Since tr is used only once = 1 : 2 : tr2 where tr2 = server reqs Repeat as required client ys = 1 : ys server xs = map (+1) xs reqs = client resps resps = server reqs

Cse536 Functional Programming Lazy Patterns Suppose client wants to test servers responses. clientB (y : ys) = if ok y then 1 :(y:ys) else error "faulty server" where ok x = True server xs = map (+1) xs Now what happens... Reqs = client resps = client(server reqs) = client(server(client resps)) = client(server(client(server reqs)) We can’t unfold

Cse536 Functional Programming Solution 1 Rewrite client client ys = 1 : (if ok (head ys) then ys else error "faulty server") where ok x = True Pulling the (:) out of the if makes client immediately exhibit a cons cell Using (head ys) rather than the pattern (y:ys) makes the evaluation of ys lazy

Cse536 Functional Programming Solution 2 – lazy patterns client ~(y:ys) = 1 : (if ok y then y:ys else err) where ok x = True err = error "faulty server” Calculate using where clauses Reqs = client resps = 1 : (if ok y then y:ys else err) where (y:ys) = resps = 1 : y : ys where (y:ys) = resps = 1 : resps In Haskell the ~ before a pattern makes it lazy

Cse536 Functional Programming Memoization fibsFn :: () -> [ Integer ] fibsFn () = 1 : 1 : (zipWith (+) (fibsFn ()) (tail (fibsFn ()))) Unfolding we get: fibsFn () = 1:1: add (fibsFn()) (tail (fibsFn ())) = 1 : tf where tf = 1:add(fibsFn())(tail(fibsFn())) But we can’t proceed since we can’t identify tf with tail(fibsFn()). Further unfolding becomes exponential.

Cse536 Functional Programming memo1 We need a function that builds a table of previous calls. Memo : (a -> b) -> (a -> b) Memo fib x = if x in_Table_at i then Table[i] else set_table(x,fib x); return fib x Memo1 builds a table with exactly 1 entry.

Cse536 Functional Programming Using memo1 mfibsFn x = let mfibs = memo1 mfibsFn in 1:1:zipWith(+)(mfibs())(tail(mfibs())) Main> take 20 (mfibsFn()) [1,1,2,3,5,8,13,21,34,55,89,144,233,377, 610,987,1597,2584,4181,6765]

Cse536 Functional Programming Inductive properties of infinite lists Which properties are true of infinite lists –take n xs ++ drop n xs = xs –reverse(reverse xs) = xs Recall that z is the error or non-terminating computation. Think of z as an approximation to an answer. We can get more precise approximations by: ones = 1 : ones z 1 : z 1 : 1 : z 1 : 1 : 1 : z

Cse536 Functional Programming Proof by induction To do a proof about infinite lists, do a proof by induction where the base case is z, rather than [] since an infinite list does not have a [] case (because its infinite). –1) Prove P{ z } –2) Assume P{xs} is true then prove P{x:xs} Auxiliary rule: –Pattern match against z returns z. I.e. c ase z of { [] -> e; y:ys -> f }

Cse536 Functional Programming Example Prove: P{ x } == (x ++ y) ++ w = x ++ (y++w) 1)Prove P{ z } ( z ++ y) ++ w = z ++ (y++w) 2)Assume P{xs} (xs ++ y) ++ w = xs ++ (y++w) Prove P{x:xs} (x:xs ++ y) ++ w = x:xs ++ (y++w)

Cse536 Functional Programming Base Case ( z ++ y) ++ w = z ++ (y++w) ( z ++ y) ++ w pattern match in def of ++ z ++ w pattern match in def of ++ z pattern match in def of ++ z ++ (y++w)

Cse536 Functional Programming Induction step 1)Assume P{xs} (xs ++ y) ++ w = xs ++ (y++w) Prove P{x:xs} (x:xs ++ y) ++ w = x:xs ++ (y++w) (x:xs ++ y) ++ w Def of (++) (x:(xs ++ y)) ++ w Def of (++) x :((xs ++ y) ++ w) Induction hypothesis x : (xs ++ (y ++ w)) Def of (++) (x:xs) ++ (y ++ w)