CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists

Similar documents
Lecture 5: Lazy Evaluation and Infinite Data Structures

Programming Language Concepts: Lecture 14

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

Standard prelude. Appendix A. A.1 Classes

1 The smallest free number

Lecture 2: List algorithms using recursion and list comprehensions

Programming Languages 3. Definition and Proof by Induction

CITS3211 FUNCTIONAL PROGRAMMING

COMPSCI 230 Discrete Math Prime Numbers January 24, / 15

Fall 2018 Lecture N

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Lecture 10 September 11, 2017

(ii) Define a function ulh that takes a list xs, and pairs each element with all other elements in xs.

Haskell: From Basic to Advanced. Part 3 A Deeper Look into Laziness

CSCE 314 Programming Languages

Logical Methods in... using Haskell Getting Started

Streams and Lazy Evaluation in Lisp

Shell CSCE 314 TAMU. Higher Order Functions

Topic 8: Lazy Evaluation

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

UCT Algorithm Circle: Number Theory

COP 4516: Math for Programming Contest Notes

Contents. A Source Code 32 A.1 ASCII versions of mathematical symbols A.2 Definitions of used library functions... 32

CS 440: Programming Languages and Translators, Spring 2019 Mon

Functional Programming

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

CS 457/557: Functional Languages

CS 320: Concepts of Programming Languages

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

Shell CSCE 314 TAMU. Functions continued

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Introduction to Programming: Lecture 6

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

Lambda Calculus. Concepts in Programming Languages Recitation 6:

COMPUTER SCIENCE 123. Foundations of Computer Science. 5. Strings

Functional Programming. Overview. Topics. Recall λ-terms. Examples

A tour of the Haskell Prelude

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

CS 320: Concepts of Programming Languages

02157 Functional Programming. Michael R. Ha. Sequences and Sequence Expressions. Michael R. Hansen

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Haskell through HUGS THE BASICS

HIGHER-ORDER FUNCTIONS

ITERATORS AND STREAMS 9

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

SML A F unctional Functional Language Language Lecture 19

! " Haskell Expressions are treated as: !" Thunks. !" Closures. !" Delayed Computations. !" Suspensions. ! " Expressions are evaluated: !

CSc 520 Principles of Programming Languages. Currying Revisited... Currying Revisited. 16: Haskell Higher-Order Functions

CSc 372. Comparative Programming Languages. 11 : Haskell Higher-Order Functions. Department of Computer Science University of Arizona

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

Introduction to Programming: Lecture 3

CSc 520. Principles of Programming Languages 11: Haskell Basics

Haskell Scripts. Yan Huang

F21SC Industrial Programming: Functional Programming in Python

CSc 372. Comparative Programming Languages. 3 : Haskell Introduction. Department of Computer Science University of Arizona

CSE 3302 Programming Languages Lecture 8: Functional Programming

Problem Solving for Intro to Computer Science

Types, Expressions, and States

Functional Programming in Haskell for A level teachers

Sometimes it s good to be lazy

Module 06. Topics: Iterative structure in Python Readings: ThinkP 7. CS116 Spring : Iteration

Graphical Untyped Lambda Calculus Interactive Interpreter

Getting Started with Haskell (10 points)

CITS3211 FUNCTIONAL PROGRAMMING. 10. Programming in the pure λ calculus

Static Contract Checking for Haskell

CSc 372. Comparative Programming Languages. 13 : Haskell Lazy Evaluation. Department of Computer Science University of Arizona

Applicative, traversable, foldable

CSc 372 Comparative Programming Languages

CS 320: Concepts of Programming Languages

G Programming Languages - Fall 2012

Haskell Refresher Informatics 2D

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

Introduction to Programming, Aug-Dec 2008

Programming in Haskell Aug-Nov 2015

Course year Typeclasses and their instances

CSci 450: Org. of Programming Languages Overloading and Type Classes

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Applicative, traversable, foldable

EDAF40. 2nd June :00-19:00. WRITE ONLY ON ONE SIDE OF THE PAPER - the exams will be scanned in and only the front/ odd pages will be read.

Semantics of programming languages

Tentamen Functioneel Programmeren 2001 Informatica, Universiteit Utrecht Docent: Wishnu Prasetya

Declaring Numbers. Bernd Braßel, Frank Huch and Sebastian Fischer. Department of Computer Science, University of Kiel, Germany

Putting Laziness to Work

Functional Programming TDA 452, DIT 142

CITS3211 FUNCTIONAL PROGRAMMING. 14. Graph reduction

Lecture 4: Higher Order Functions

CIS 252 Introduction to Computer Science Section M013: Exam 2 (Green) March 29, Points Possible

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Names and Functions. Chapter 2

CS 11 Haskell track: lecture 1

Lecture 1 August 9, 2017

Functional Programming I *** Functional Programming and Interactive Theorem Proving. Ulrich Berger Michaelmas Term 2006

Lecture Notes on Ints

Introduction to Haskell

Functional Logic Programming Language Curry

Public-Service Announcements

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Public-Service Announcements

Transcription:

CITS3211 FUNCTIONAL PROGRAMMING 7. Lazy evaluation and infinite lists Summary: This lecture introduces lazy evaluation and infinite lists in functional languages. cs123 notes: Lecture 19 R.L. While, 1997 2002. Modified by R. Davies 2003 7.

Reduction order Consider the following expression fst (sqr 4, sqr 2) There are two ways in which this might be evaluated Arguments first or innermost reduction: also known as applicative order reduction or eager evaluation or call by value fst (sqr 4, sqr 2) fst (4 * 4, sqr 2) fst (16, sqr 2) fst (16, 2 * 2) fst (16, 4) 16 Function first or outermost reduction: also known as normal order reduction or lazy evaluation fst (sqr 4, sqr 2) sqr 4 4 * 4 16 Note that the two schemes will never give different answers although sometimes outermost gives an answer when innermost doesn t CITS3211 Functional Programming 2 7. Lazy evaluation

Lazy evaluation Most programming languages use eager evaluation Haskell uses lazy evaluation The principle of lazy evaluation is evaluate an expression only if you know you have to e.g. sqr 2 above never gets evaluated This has several consequences fewer reductions avoids non termination e.g. fst (sqr 4, [1..]) allows a more flexible style of programming although lazy evaluation is usually somewhat slower due to the bookkeeping required to keep track of which expressions have been evaluated side effects like printing and modifying variables don't fit well, because the sequence of operations depends on how an expression is used [eager functional languages usually do include such side effects, but they require laziness to be manually programmed] effects in Haskell are instead provided via monads (next topic) CITS3211 Functional Programming 3 7. Lazy evaluation

An example: Boolean functions From the Prelude: (&&) :: Bool -> Bool -> Bool -- z && x returns True iff -- both z and x are True False && x = False True && x = x Note that && checks its left argument first, then checks its right argument only if the left is True so if z = last [True x <- [1..]] z && False = <infinite loop> but False && z = False So && isn t commutative! This Boolean left laziness is common to many programming languages CITS3211 Functional Programming 4 7. Lazy evaluation

An example: Boolean list functions Lazy evaluation generalises this idea to every definition in a program for example and :: [Bool] -> Bool -- and xs returns True iff -- every element on xs is True and [] = True and (x : xs) = x && and xs and checks its list in order from the first element onwards, and stops as soon as any element is False so and [True x <- [1..]] = <infinite loop> but and (False : [True x <- [1..]]) False && and [True x <- [1..]] False So here again lazy evaluation returns a result where eager evaluation wouldn't CITS3211 Functional Programming 5 7. Lazy evaluation

Infinite lists The expression [m..] denotes an infinite list What can we do with this? We can t examine the whole list that would take an infinite amount of time! We can examine any finite part of the list for example head [m..] = m [m..]!! k = m + k take k [m..] = [m.. m + k - 1] zip [m..] "abc" = [(m, 'a'), (m+1, 'b'), (m+2, 'c')] The system knows how far it needs to evaluate the list remember the principle of lazy evaluation CITS3211 Functional Programming 6 7. Lazy evaluation

Infinite lists But care is needed for example 4 `elem` [0, 2..] = True 3 `elem` [0, 2..] = <infinite loop> filter (< 10) [x ^ 2 x <- [1..]] = [1, 4, 9 <infinite loop> The system doesn t know that all of the subsequent numbers will fail the tests We need to incorporate the information that the numbers on the infinite lists are always increasing one way is to use takewhile 3 `elem` takewhile (<= 3) [0, 2..] = False takewhile (< 10) [x ^ 2 x <- [1..]] = [1, 4, 9] CITS3211 Functional Programming 7 7. Lazy evaluation

Consider the following functions. An example: prime numbers factors :: Int -> [Int] -- factors n returns a list -- containing the factors of n factors n = [k k <- [1.. n], n `mod` k == 0] isprime :: Int -> Bool -- isprime n returns True iff n is prime isprime n = factors n == [1, n] primesupto :: Int -> [Int] -- primesupto n returns a list containing -- the primes up to n primesupto n = [k k <- [2.. n], isprime k] But suppose we want the first n primes? use an infinite list n'primes :: Int -> [Int] -- n'primes n returns a list containing -- the first n primes n'primes n = take n [k k <- [2..], isprime k] CITS3211 Functional Programming 8 7. Lazy evaluation

iterate The function iterate takes a function f and generates an infinite list of repeated applications of f to a value x iterate :: (a -> a) -> a -> [a] -- iterate f x returns an infinite list of -- repeated applications of f to x iterate f x = x : iterate f (f x) note: recursion with no base case! For example iterate (+ 1) 1 = [1, 2, 3, 4, 5,... iterate (* 2) 1 = [1, 2, 4, 8, 16,... iterate (`div` 10) 271 = [271, 27, 2, 0, 0, 0, 0,... iterate captures a common pattern used in infinite lists CITS3211 Functional Programming 9 7. Lazy evaluation

Generate and test algorithms iterate is often used in generate and test algorithms Consider the function remainder that takes two numbers x and y and returns the remainder of x divided by y remainder :: Int -> Int -> Int -- pre: x >= 0 && y > 0 -- remainder x y returns the remainder -- of x divided by y remainder x y = head (dropwhile (>= y) (iterate f x)) where f u = u - y Consider the function digits that takes a number x and returns a list containing the digits in x digits :: Int -> [Int] -- pre: x > 0 -- digits x returns the digits in x digits = reverse. map (`mod` 10). takewhile (> 0). iterate (`div` 10) These two functions work in similar ways iterate generates candidate elements for the result the other functions test the candidates to see which ones we actually want The use of an infinite list allows us to completely separate the generation of candidates from the testing In an eager language, we would have to explicitly interleave these operations generate a candidate, test it, generate another candidate, test it, a much less modular definition CITS3211 Functional Programming 10 7. Lazy evaluation

An example: the Sieve of Eratosthenes The Sieve was the first algorithm for generating prime numbers efficiently Eratosthenes was a Greek mathematician 1. Write down the numbers 2, 3, 4, 5,... 2. Mark the first element p as prime 3. Remove all multiples of p from the list 4. Return to Step 2 It operates like this: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17... so 2 is prime 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31... (no multiples of 2) so 3 is prime 5 7 11 13 17 19 23 25 29 31 35 37 41 43... (no multiples of 2 or 3) so 5 is prime 7 11 13 17 19 23 29 31 37 41 43 47 49 53... (no multiples of 2, 3 or 5) etc. CITS3211 Functional Programming 11 7. Lazy evaluation

The Sieve of Eratosthenes The Sieve is implemented by the function sieve sieve :: [Int] -> [Int] -- sieve xs returns p and sieves xs sieve (p : xs) = p : sieve [x x <- xs, x `mod` p /= 0] all'primes :: [Int] -- all'primes returns a list -- containing all primes all'primes = sieve [2..] all'primes can then be used in other functions -- primesupto n returns a list containing -- the primes up to n primesupto n = takewhile (<= n) all'primes -- n'primes n returns a list containing -- the first n primes n'primes n = take n all'primes the generation of primes and the selection of elements for the result list are again separate operations Compare the performance of these definitions with the previous definitions CITS3211 Functional Programming 12 7. Lazy evaluation