Welcome to the CIS Seminar Laziness is good for you!

Slides:



Advertisements
Similar presentations
Chapter 4 Computation Bjarne Stroustrup
Advertisements

C. Varela; Adapted from S. Haridi and P. Van Roy1 Declarative Programming Techniques Lazy Execution (VRH 4.5) Carlos Varela RPI Adapted with permission.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 39: Lambda Calculus.
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
1 Introduction to Computability Theory Lecture12: Reductions Prof. Amos Israeli.
Laziness and Infinite Datastructures Koen Lindström Claessen.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Cse536 Functional Programming Lecture #14, Nov. 10, 2004 Programming with Streams –Infinite lists v.s. Streams –Normal order evaluation –Recursive streams.
Advanced Programming Handout 4. Introductions  Me: Benjamin C. Pierce (known as Benjamin, or, if you prefer, Dr. Pierce, but not Ben or Professor) 
CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm.
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.
EECS 20 Chapter 9 Part 21 Convolution, Impulse Response, Filters In Chapter 5 we Had our first look at LTI systems Considered discrete-time systems, some.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
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.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
Functional Programming in Haskell Motivation through Concrete Examples Adapted from Lectures by Simon Thompson.
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
DISCRETE MATHEMATICS I CHAPTER 11 Dr. Adam Anthony Spring 2011 Some material adapted from lecture notes provided by Dr. Chungsim Han and Dr. Sam Lomonaco.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
2-1 Functions We’ll put the Fun back into Functions!!
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CS 280 Data Structures Professor John Peterson. How Does Parsing Work? You need to know where to start (“statement”) This grammar is constructed so that.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
Lecture 31: Laziness University of Virginia cs1120 Fall 2009 David Evans.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
CS101 Computer Programming I Chapter 4 Extra Examples.
REVIEW A relation is a set of ordered pairs. {(2,3), (-1,5), (4,-2), (9,9), (0,-6)} This is a relation The domain is the set of all x values.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
Word problems DON’T PANIC! Some students believe they can’t do word problem solving in math. Don’t panic. It helps to read the question more than once.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Activity 1-17: Infinity.
{ What is a Number? Philosophy of Mathematics.  In philosophy and maths we like our definitions to give necessary and sufficient conditions.  This means.
Chapter SevenModern Programming Languages1 A Second Look At ML.
CS April 2002 Lazy Evaluation, Thunks, and Streams.
Spring 2008Programming Development Techniques 1 Topic 5.5 Higher Order Procedures (This goes back and picks up section 1.3 and then sections in Chapter.
David Evans CS200: Computer Science University of Virginia Computer Science Class 32: The Meaning of Truth.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
CS321 Functional Programming 2 © JAS Programming with Streams A stream is never finite and could be defined as special polymorphic type data stream.
CompSci Today’s Topics Computer Science Noncomputability Upcoming Special Topic: Enabled by Computer -- Decoding the Human Genome Reading Great.
REVIEW A relation is a set of ordered pairs. {(2,3), (-1,5), (4,-2), (9,9), (0,-6)} This is a relation The domain is the set of all x values.
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.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Introduction to CSCI 1311 Dr. Mark C. Lewis
Advanced Functional Programming 2010
Laziness and Infinite Datastructures
Applied Discrete Mathematics Week 2: Functions and Sequences
Loops BIS1523 – Lecture 10.
What Are They? Who Needs ‘em? An Example: Scoring in Tennis
Enough Mathematical Appetizers!
Control Structures - Repetition
Intro to Nested Looping
Count Controlled Loops (Nested)
What Are They? Who Needs ‘em? An Example: Scoring in Tennis
6.001 SICP Streams – the lazy way
Discrete Math for CS CMPSC 360 LECTURE 43 Last time: Variance
Intro to Nested Looping
Presentation transcript:

Welcome to the CIS Seminar Laziness is good for you!

Scheduling Nov 17: Stephen Dec 1: Darek Dec 8: Tim

A Tale of Two Students Prof: Read Chapter 4 Darek: Ha! Chapter 4 is a waste of time. I'll wait until I see the test and read it real quick during the test if I have to. Daniel: Better read that right now! You never know what will be on the test.

A Tale of Two Students Prof: Read Chapter 4 Darek: Ha! Chapter 4 is a waste of time. I'll wait until I see the test and read it real quick during the test if I have to. Daniel: Better read that right now! You never know what will be on the test. Prof: Test Time! Daniel: Drat – he never asked a question from chapter 4! Darek: Score! Sure glad I got all that extra sleep!

What's Going On Here? Suppose that there are no "real time" constraints – the test isn't time limited. Darek doesn't bother to do work that's not part of his final grade – he's lazy Daniel does work that might not have an impact on his grade – he's eager. These guys are representative of two basic programming language styles. We know which one is "right"!

Back to Lambda Calculus If you remember Lambda Calculus (hah!) it was a way of expressing computation that didn't commit you to any specific sequence of operations. You had a big cloud of "work" to do and you could do any piece of work (reduction) you wanted to. No matter which strategy you choose you eventually get the same answer.

Lazy Lambda Calculus Here's the big idea: you take your program and don't run (reduce) anything unless it's part of the "answer". That way you know all of your effort "counts". This sounds sensible! So what's the alternative?

Other Programming Languages Most programming languages do work at times they don't have to: x = f(y, z) // Will we really NEED x later? a = f(g(b), h(b)) // Does f really use both?

Lazy Languages In a lazy language, we don't do any work that isn't part of the "answer". What does that mean? x = f(y) doesn't DO anything unless we really want to know the value of x.

Every Language is a Little Lazy Even in regular old languages like Java, there are constructs that do behave lazily: if (y > 10) z = f(x) else z = g(x); We don't go out and evaluate both branches of the if – we wait until we know which is needed. Note that && and || are lazy too!

A Lazy Function iterate :: (a -> a) -> a -> [a] iterate f x = x : iterate f (f x) What the heck? This defines an infinite sequence of value: [x, f(x), f(f(x)), f(f(f(x))), …] Now in languages like Java this is an infinite loop! But not in a lazy language … You have to be careful not to print the entire result: take 10 (iterate (*2) 1)

Lazy Example addPairs :: [Integer] -> [Integer] addPairs (x:y:rest) = x+y : addPairs (y:rest) addPairs _ = [ ] Check this: addPairs [0,1,2,1,0] = [1,3,3,1]

Pascal's Triangle pascal :: [[Integer]] pascal = iterate f [1] where f x = addPairs ([0] ++ x ++ [0]) combinations n m = pascal !! (n-1) !! (m-1) Let's fire up Haskell and see this in action!

Generalizing Pascal's triangle can be explained in terms of "convolution". This is a big deal in the signal processing world in which systems are: * linear (if you double the input, you double the output) * time-invariant (they do the same thing today and tomorrow) * We'll also assume causality: output can't precede input If you know the response to a single 1 (the impulse response) you know the response to any input. This is discrete convolution: signals are an infinite stream of discrete samples – integration is now summation

Discrete Convolution convolve, addSeries :: [Integer] -> [Integer] -> [Integer] convolve [] x = [] convolve (x:xs) y = addSeries (map (*x) y) (0 : convolve xs y) addSeries [] x = x addSeries x [] = x addSeries (x:xs) (y:ys) = x+y : addSeries xs ys

A General Pascal Triangle pascal2 = iterate (convolve [1,1]) [1] But wait! We can do more! dice = iterate (convolve [0,1,1,1,1,1,1]) [1] test6 = dice !! 3

Convolution You CS guys can't hide from convolution: it's the basis for TONS of signal processing software (speech recognition, filtering, sound and image synthesis) Know Math to know CS!

Beyond the Infinite … The program we just did is overly convoluted because the streams are finite. Let's deal with infinite streams instead.

Infinite Streams / Convolution convolveI, addSeriesI :: [Integer] -> [Integer] -> [Integer] convolveI (x:xs) y = addSeries (map (*x) y) (0 : convolveI xs y) addSeriesI (x:xs) (y:ys) = x+y : addSeriesI xs ys (or using magic Haskell juju:) addSeriesI = zipWith (+)

Contemplating Infinity Infinity is great but we can't see it all at once. Let's see how Pascal is doing: zeros = 0 : zeros impulse = 1 : zeros pascalSignal = 1 : 1 : zeros infinitePascal = iterate (convolveI pascalSignal) impulse test7 = take 6 (map (take 6) infinitePascal)

Back to Programming Langs Haskell is a language without control constructs: * for loop: NO * while loop: NO * break: NO * if / then / else: YES (why?)

Why Not? We don't need control constructs when we have lazy evaluation. Things that require control constructs can be accomplished using ordinary data operations.

Comparison Java: for (i = 0; i < 10; i++) if (prime(i)) System.out.print(i); Haskell: filter isPrime (takeWhile (< 10) (iterate (+ 1) 0)) isPrime n = head (dropWhile (<n) primes) == n primes = sieve [2..] where sieve (p:ns) = p : sieve [n | n <- ns, n `mod` p > 0]

What is "="? In Java, it is an "action": it makes something happen. In Haskell, it is a "definition" – it doesn't do anything except allow a computation to look up the value of a name if needed. Haskell = mathematics Java = confusion

Why Laziness? Laziness is NOT about avoiding work (sorry, Darek!) Laziness is about defining things in a natural way without having to worry about the underlying computational process. Lazy programs are far more "plastic" that others: we can abstract at ANY point.

Conclusion