Lazy Programming Lazy evaluation:

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
C. Varela; Adapted from S. Haridi and P. Van Roy1 Declarative Programming Techniques Lazy Execution (VRH 4.5) Carlos Varela RPI Adapted with permission.
0 PROGRAMMING IN HASKELL Chapter 12 – Lazy evaluation and infinite lists Slides not from Hutton.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Laziness and Infinite Datastructures Koen Lindström Claessen.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
Functional Programming Lecture 4 - More Lists Muffy Calder.
Functional Programming in Haskell Motivation through Concrete Examples Adapted from Lectures by Simon Thompson.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
© William James Calhoun, : Multiplying Monomials To start the chapter, a couple of terms need to be defined. monomial - a number, a variable,
Lesson 8.4 Multiplication Properties of Exponents
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
S. Haridi and P. Van Roy1 Lazy Execution Seif Haridi KTH Peter Van Roy UCL.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
Overview of the Haskell 98 Programming Language
CS5205Haskell1 CS5205: Foundations in Programming Languages FP with Haskell A pure lazy functional language that embodies many innovative ideas in language.
Function Definition by Cases and Recursion Lecture 2, Programmeringsteknik del A.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
CSED101 INTRODUCTION TO COMPUTING FUNCTION ( 함수 ) 유환조 Hwanjo Yu.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
CS321 Functional Programming 2 © JAS Programming with Streams A stream is never finite and could be defined as special polymorphic type data stream.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Functional Programming Language: Haskell (cont’d) Lecture 20 – Functional Programming, Spring 2008.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Advanced Functional Programming 2010
Lecture 14: Advanced Topic: Functional Programming
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Polymorphic Functions
Functional Programming
String is a synonym for the type [Char].
Laziness and Infinite Datastructures
Conditional Expressions
dr Robert Kowalczyk WMiI UŁ
Theory of Computation Lecture 4: Programs and Computable Functions II
Functional Programming Lecture 12 - more higher order functions
Functions and patterns
A lightening tour in 45 minutes
Haskell Chapter 4.
Carlos Varela RPI September 22, 2017 Adapted with permission from:
PROGRAMMING IN HASKELL
Haskell.
PROGRAMMING IN HASKELL
Higher-Order Functions
PROGRAMMING IN HASKELL
Functional Programming Lecture 2 - Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Types and Classes in Haskell
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CS 457/557: Functional Languages Folds
CSE 3302 Programming Languages
Functions and patterns
CSCE 314: Programming Languages Dr. Dylan Shell
Testing vs Proving Testing uses a set of “typical” examples,
PROGRAMMING IN HASKELL
Language semantics Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Functional Programming Lecture 2 - Functions
PROGRAMMING IN HASKELL
Presentation transcript:

Lazy Programming Lazy evaluation: an expression is evaluated if and only if its value is needed to compute the overall result Consequences: elements are generated on demand so that complex intermediate structures are not necessarily expensive, e.g., an intermediate list , expression may describe infinite data structures, e.g, the infinite list of all prime numbers, implications to program design.

Example + + h x y = x+x h (9-3) (h 12 15) (9-3) (9-3) (9-3) + (9-3) 6+6 12 (9-3) (9-3) + (9-3)

Calculation rules Pattern matching f :: [Int] -> [Int] -> Int f [] ys = 0 f xs [] = 0 f (x:xs) (y:ys) = x+y f [1..3] [1..3] f (1:[2..3]) [1..3] f (1:[2..3]) (1:[2..3]) 1+1 2

Calculation rules (cont’d) Guards f :: Int -> Int -> Int -> Int f m n p | m>=n && m>=p = m | n>=m && n>=p = n | otherwise = p f (2+3) (4-1) (3+9) ?? (2+3)>=(4-1) && (2+3)>=(3+9) ??  False && 3>=12 ??  5>=3 && 5>=(3+9) ??  False ??  True && 5>=(3+9) ?? otherwise ??  5>=(3+9) ??  True ??  5>=12  12 ??  False ?? 3>=5 && 3>=12

Calculation rules (cont’d) Local definitions f :: Int -> Int -> Int front (x:y:zs) = x+y f m n front [x] = x | notNil xs = front xs | otherwise = n notNil [] = False where xs = [m..n] notNil (_:_) = True f 3 5 ?? notNil xs  front xs ?? where where ?? xs = [3..5] xs = 3:[4..5] ??  3:[4..5]  3:4:[5] ??  notNil (3:[4..5])  3+4 ??  True  7

Calculation rules (cont’d) Evaluation order = order in which applications are evaluated when there is a choice. Evaluation is from the outside in: f1 e1 (f2 e2 17) Evaluation is from left to right: f1 e1 + f2 e2

List comprehension revisited List comprehension does not add any new programs to the Haskell language. It is a short-hand for a combination of multiple applications of map (concatMap) and filter. pyTriple n = [ (x,y,z) | x <- [2..n], y <- [x+1..n], z <- [y+1..n], x*x + y*y == z*z ] pyTriple’ n = concatMap (\x -> concatMap (\y -> concatMap (\z -> if x*x + y*y == z*z then [(x,y,z)] else []) [y+1..n]) [x+1..n]) [2..n] Evaluation rules: [ e | False , q2, …, qk]  [] [ e | v <- [], q2, …, qk]  [] [ e | v <- a:xs, q2, …, qk]  [ e{a/v} | q2{a/v}, …, qk{a/v} ] ++ [ e | v <- xs, q2, …, qk ]

Example [ x+y | x <- [1,2], isEven x, y <- [x..2*x] ] [ 1+y | isEven 1, y <- [1..2*1] ] ++ [ x+y | x <- [2], isEven x, y <- [2..2*2] ]  [ 1+y | False, y <- [1..2*1] ] ++  [ 2+y | isEven 2, y <- [2..2*2] ]  [ 2+y | True, y <- [2..2*2] ]  [ 2+y | y <- [2..2*2] ] [ 2+y | y <- 2:[3..2*2] ] [ 2+2 | ] ++ [ 2+y | y <- [3..2*2] ] [ 4 ] ++ [ 2+y | y <- [3..2*2] ] [ 4 ] ++ [ 2+y | y <- 3:[4..2*2] ] [ 4 ] ++ [ 2+3 | ] ++ [ 2+y | y <- [4..2*2] ] [ 4 ] ++ [ 5 ] ++ [ 2+y | y <- [4..2*2] ] [ 4,5 ] ++ [ 2+y | y <- [4..2*2] ] [ 4,5 ] ++ [ 2+y | y <- 4:[] ] [ 4,5 ] ++ [ 2+4 | ] ++ [ 2+y | y <- [] ] [ 4,5 ] ++ [ 6 ] ++ [ 2+y | y <- [] ] [ 4,5,6 ] ++ [ 2+y | y <- [] ] [ 4,5,6 ] ++ [] [ 4,5,6 ]

Infinite lists ones :: [Int] ones = 1 : ones [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,………………^C{Interrupted!}] addFirstTwo :: [Int] -> Int addFirstTwo (x:y:zs) = x+y addFirstTwo ones addFirstTwo (1:ones) addFirstTwo (1:1:ones) 1+1 2

Example: prime numbers 2 3 4 5 6 7 8 9 10 11 12 13 2 3 5 7 9 11 13 2 3 5 7 11 13 The sieve of Eratosthenes primes :: [Integer] primes = sieve [2..] sieve :: [Integer] -> [Integer] sieve (x:xs) = x:sieve [ y | y <- xs, y `mod` x > 0]

Example (cont’d) primes is an infinite list  be careful by using it 7 `elem` primes  True, but 6 `elem` primes does not terminate elemOrd :: Ord a => a -> [a] -> Bool elemOrd _ [] = False elemOrd y (x:xs) | x<y = elemOrd y xs | x==y = True | otherwise = False

Example: Generating random numbers nextRand :: Int -> Int nextRand n = (multiplier * n + increment) `mod` modulus randomSequence :: Int -> [Int] randomSequence = iterate nextRand seed, multiplier, increment, modulus :: Int seed = 17489 multiplier = 25173 increment = 13849 modulus = 65536 randonSequence seed  [17489,59134,9327,52468,43805,8378,18395,…

Another example pythagTriples = [ (x,y,z) | x <- [2..], y <- [x+1..], z <- [y+1..], x*x + y*y == z*z ] What is the result of pythagTriples? No output!!! pythagTriples' = [ (x,y,z) | z <- [4..], y <- [3..z-1], x <- [2..y-1], or use a function infiniteProduct :: [a] -> [b] –> [(a,b)]

Infinite elements in algebraic types data Tree a = Empty | Node a (Tree a) (Tree a) deriving Show replaceRightLeaf :: Tree a -> Tree a -> Tree a replaceRightLeaf t1 Empty = t1 replaceRightLeaf t1 (Node x t2 t3) = Node x t2 (replaceRightLeaf t1 t3) loopRightTree :: Tree a -> Tree a loopRightTree t = replaceRightLeaf (loopRightTree t) t tree1 = Node 1 (Node 2 Empty Empty) (Node 3 Empty Empty) loopRightTree tree1  Node 1 (Node 2 Empty Empty) (Node 3 Empty (Node 1 (Node 2 Empty Empty) (Node 3 Empty (Node 1 (Node 2 Empty Empty) (Node 3 Empty …