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

Similar documents
Functional Programming in Haskell Part I : Basics

Programming Language Concepts: Lecture 14

Lecture 19: Functions, Types and Data Structures in Haskell

Advanced features of Functional Programming (Haskell)

CITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists

Introduction to Programming, Aug-Dec 2006

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

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

An introduction to functional programming. July 23, 2010

Haskell Overview III (3A) Young Won Lim 10/4/16

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

Introduction to Programming, Aug-Dec 2008

Functional Programming

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Logic - CM0845 Introduction to Haskell

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

Lecture 5: Lazy Evaluation and Infinite Data Structures

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Haskell Types COMP360

CS 320: Concepts of Programming Languages

Algebraic Types. Chapter 14 of Thompson

Topic 8: Lazy Evaluation

User-Defined Algebraic Data Types

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

Introduction to Programming: Lecture 3

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

CSC324 Principles of Programming Languages

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

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

CS 320: Concepts of Programming Languages

CSc 372 Comparative Programming Languages

Haskell Types, Classes, and Functions, Currying, and Polymorphism

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

Inductive Data Types

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

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

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Fall 2018 Lecture N

CS 457/557: Functional Languages

Programming Languages Fall 2013

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

Tree Equality: The Problem

Standard prelude. Appendix A. A.1 Classes

Functional Programming and Haskell

Lecture 2: List algorithms using recursion and list comprehensions

CSCE 314 Programming Languages

Typed Racket: Racket with Static Types

Sometimes it s good to be lazy

Principles of Programming Languages

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

THE CONCEPTION AND APPLICATION OF PFL : A PROCESS FUNCTIONAL PROGRAMMING LANGUAGE

Topic 6: Partial Application, Function Composition and Type Classes

Topic 6: Partial Application, Function Composition and Type Classes

Haskell An Introduction

Introduction to Functional Programming in Haskell 1 / 56

Programming Language Concepts: Lecture 19

SML A F unctional Functional Language Language Lecture 19

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes. Haskell Overloading (3) Haskell Overloading (2)

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

CS 440: Programming Languages and Translators, Spring 2019 Mon

Haskell Types COMP360

Homework 1: Functional Programming, Haskell

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

Haskell & functional programming, some slightly more advanced stuff. Matteo Pradella

CSE 3302 Programming Languages Lecture 8: Functional Programming

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

INTRODUCTION TO HASKELL

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

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

CS 360: Programming Languages Lecture 12: More Haskell

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

CSc 520. Principles of Programming Languages 11: Haskell Basics

Course year Typeclasses and their instances

A general introduction to Functional Programming using Haskell

Programming in Haskell Aug-Nov 2015

Implementing an abstract datatype. Linked lists and queues

Programming Language Concepts, CS2104 Lecture 7

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

Parallel Haskell on MultiCores and Clusters

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

Functional Logic Programming Language Curry

Programming Paradigms

Haskell Overview II (2A) Young Won Lim 8/9/16

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Structural polymorphism in Generic Haskell

Overloading, Type Classes, and Algebraic Datatypes

The Typed Racket Guide

Principles of Programming Languages

Lecture 1 August 9, 2017

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

Short Notes of CS201

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Exercise 1 ( = 22 points)

Lecture 10 September 11, 2017

Introduction to Programming: Lecture 10

This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator

Shell CSCE 314 TAMU. Functions continued

Transcription:

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures Madhavan Mukund Chennai Mathematical Institute 92 G N Chetty Rd, Chennai 600 017, India madhavan@cmi.ac.in http://www.cmi.ac.in/ madhavan Madras Christian College, 10 December 2003 p.1

Haskell review Program Collection of function definitions Madras Christian College, 10 December 2003 p.2

Haskell review Program Collection of function definitions Computation Rewriting using definitions Madras Christian College, 10 December 2003 p.2

Haskell review Program Collection of function definitions Computation Rewriting using definitions Functions are associated with input and output types Madras Christian College, 10 December 2003 p.2

Haskell review Program Collection of function definitions Computation Rewriting using definitions Functions are associated with input and output types isdigit :: Char -> Bool Madras Christian College, 10 December 2003 p.2

Haskell review Program Collection of function definitions Computation Rewriting using definitions Functions are associated with input and output types isdigit :: Char -> Bool isdigit 0 = True isdigit 1 = True... isdigit 9 = True isdigit c = False Madras Christian College, 10 December 2003 p.2

Haskell review Program Collection of function definitions Computation Rewriting using definitions Functions are associated with input and output types isdigit :: Char -> Bool isdigit 0 = True isdigit 1 = True... isdigit 9 = True isdigit c = False isdigit c (c >= 0 && c <= 9 ) = True otherwise = False Madras Christian College, 10 December 2003 p.2

Haskell review... Basic collective type is a list Madras Christian College, 10 December 2003 p.3

Haskell review... Basic collective type is a list Define list functions by induction on structure Madras Christian College, 10 December 2003 p.3

Haskell review... Basic collective type is a list Define list functions by induction on structure Example Adding up a list of integers Madras Christian College, 10 December 2003 p.3

Haskell review... Basic collective type is a list Define list functions by induction on structure Example Adding up a list of integers sum :: [Int] -> Int sum [] = 0 sum (x:l) = x + (sum l) Madras Christian College, 10 December 2003 p.3

Haskell review... Basic collective type is a list Define list functions by induction on structure Example Adding up a list of integers sum :: [Int] -> Int sum [] = 0 sum (x:l) = x + (sum l) (Conditional) polymorphism Madras Christian College, 10 December 2003 p.3

Haskell review... Basic collective type is a list Define list functions by induction on structure Example Adding up a list of integers sum :: [Int] -> Int sum [] = 0 sum (x:l) = x + (sum l) (Conditional) polymorphism Most general type of sum is sum :: (Num a) => [a] -> a where Num a is true for any type a that supports basic arithmetic operations +, -,... Madras Christian College, 10 December 2003 p.3

Today s agenda Adding new types Madras Christian College, 10 December 2003 p.4

Today s agenda Adding new types Defining abstract datatypes Provide an interface that hides the implementation Madras Christian College, 10 December 2003 p.4

Today s agenda Adding new types Defining abstract datatypes Provide an interface that hides the implementation Using infinite data structures Madras Christian College, 10 December 2003 p.4

User defined datatypes The data declaration adds new datatypes Madras Christian College, 10 December 2003 p.5

User defined datatypes The data declaration adds new datatypes Enumerated types data Signal = Red Yellow Green Madras Christian College, 10 December 2003 p.5

User defined datatypes The data declaration adds new datatypes Enumerated types data Signal = Red Yellow Green Can use this type in a function such as stopwhen :: Signal -> Bool stopwhen Red = True stopwhen c = False Madras Christian College, 10 December 2003 p.5

User defined datatypes The data declaration adds new datatypes Enumerated types data Signal = Red Yellow Green Can use this type in a function such as stopwhen :: Signal -> Bool stopwhen Red = True stopwhen c = False What if we write instead stopwhen2 :: Signal -> Bool stopwhen2 c (c == Red) = True otherwise = False Madras Christian College, 10 December 2003 p.5

User defined types and type classes stopwhen2 requires Eq Signal Madras Christian College, 10 December 2003 p.6

User defined types and type classes stopwhen2 requires Eq Signal How about nextlight :: Signal -> Signal nextlight Green = Yellow nextlight Yellow = Red nextlight Red = Green Madras Christian College, 10 December 2003 p.6

User defined types and type classes stopwhen2 requires Eq Signal How about nextlight :: Signal -> Signal nextlight Green = Yellow nextlight Yellow = Red nextlight Red = Green Displaying result of nextlight requires Show Signal Madras Christian College, 10 December 2003 p.6

User defined types and type classes stopwhen2 requires Eq Signal How about nextlight :: Signal -> Signal nextlight Green = Yellow nextlight Yellow = Red nextlight Red = Green Displaying result of nextlight requires Show Signal Show a is true of type a if there is a function show :: a -> String that allows values of a to be displayed Madras Christian College, 10 December 2003 p.6

Adding user defined types to type classes Simplest solution is data Signal = Red Yellow Green deriving (Eq, Show, Ord) Madras Christian College, 10 December 2003 p.7

Adding user defined types to type classes Simplest solution is data Signal = Red Yellow Green deriving (Eq, Show, Ord) Fixes default values Madras Christian College, 10 December 2003 p.7

Adding user defined types to type classes Simplest solution is data Signal = Red Yellow Green deriving (Eq, Show, Ord) Fixes default values Red == Red, Red /= Yellow,... Madras Christian College, 10 December 2003 p.7

Adding user defined types to type classes Simplest solution is data Signal = Red Yellow Green deriving (Eq, Show, Ord) Fixes default values Red == Red, Red /= Yellow,... show Red = "Red", show Yellow = "Yellow",... Madras Christian College, 10 December 2003 p.7

Adding user defined types to type classes Simplest solution is data Signal = Red Yellow Green deriving (Eq, Show, Ord) Fixes default values Red == Red, Red /= Yellow,... show Red = "Red", show Yellow = "Yellow",... Red < Yellow < Green Madras Christian College, 10 December 2003 p.7

Adding user defined types to type classes... Or, provide your own functions Madras Christian College, 10 December 2003 p.8

Adding user defined types to type classes... Or, provide your own functions data Signal = Red Yellow Green deriving (Eq) Madras Christian College, 10 December 2003 p.8

Adding user defined types to type classes... Or, provide your own functions data Signal = Red Yellow Green deriving (Eq) instance Show Signal where show Yellow = "Yellow" show c = "Black" Madras Christian College, 10 December 2003 p.8

Adding user defined types to type classes... Or, provide your own functions data Signal = Red Yellow Green deriving (Eq) instance Show Signal where show Yellow = "Yellow" show c = "Black" instance Ord Signal where Green <= Yellow = True Yellow <= Red = True Red <= Green = True x <= y = False Madras Christian College, 10 December 2003 p.8

Adding user defined types to type classes... Or, provide your own functions data Signal = Red Yellow Green deriving (Eq) instance Show Signal where show Yellow = "Yellow" show c = "Black" instance Ord Signal where Green <= Yellow = True Yellow <= Red = True Red <= Green = True x <= y = False In the class Ord, >, >=,... are defined in terms of <= Madras Christian College, 10 December 2003 p.8

Adding user defined types to type classes... Or, provide your own functions data Signal = Red Yellow Green deriving (Eq) instance Show Signal where show Yellow = "Yellow" show c = "Black" instance Ord Signal where Green <= Yellow = True Yellow <= Red = True Red <= Green = True x <= y = False In the class Ord, >, >=,... are defined in terms of <= Note: <= need not even be a consistent ordering! Madras Christian College, 10 December 2003 p.8

Recursive datatypes A binary tree to store integers at each node data Btreeint = Nil Node Int Btreeint Btreeint Madras Christian College, 10 December 2003 p.9

Recursive datatypes A binary tree to store integers at each node data Btreeint = Nil Node Int Btreeint Btreeint Nil and Node are constructors Madras Christian College, 10 December 2003 p.9

Recursive datatypes A binary tree to store integers at each node data Btreeint = Nil Node Int Btreeint Btreeint Nil and Node are constructors The constructor Nil takes zero arguments Madras Christian College, 10 December 2003 p.9

Recursive datatypes A binary tree to store integers at each node data Btreeint = Nil Node Int Btreeint Btreeint Nil and Node are constructors The constructor Nil takes zero arguments A constant, like Red, Green,... Madras Christian College, 10 December 2003 p.9

Recursive datatypes A binary tree to store integers at each node data Btreeint = Nil Node Int Btreeint Btreeint Nil and Node are constructors The constructor Nil takes zero arguments A constant, like Red, Green,... The constructor Node has three arguments Madras Christian College, 10 December 2003 p.9

Recursive datatypes A binary tree to store integers at each node data Btreeint = Nil Node Int Btreeint Btreeint Nil and Node are constructors The constructor Nil takes zero arguments A constant, like Red, Green,... The constructor Node has three arguments First component Int: value stored at the node Madras Christian College, 10 December 2003 p.9

Recursive datatypes A binary tree to store integers at each node data Btreeint = Nil Node Int Btreeint Btreeint Nil and Node are constructors The constructor Nil takes zero arguments A constant, like Red, Green,... The constructor Node has three arguments First component Int: value stored at the node Other two components Btreeint: left and right subtrees Madras Christian College, 10 December 2003 p.9

Recursive datatypes... Example The tree 6 would be written as 4 8 Node 6 (Node 4 Nil Nil) (Node 8 (Node 7 Nil Nil) Nil) 7 Madras Christian College, 10 December 2003 p.10

Functions on recursive datatypes Define by induction on the structure of datatype Madras Christian College, 10 December 2003 p.11

Functions on recursive datatypes Define by induction on the structure of datatype How many values are there in the tree? Madras Christian College, 10 December 2003 p.11

Functions on recursive datatypes Define by induction on the structure of datatype How many values are there in the tree? size :: Btreeint -> Int size Nil = 0 size (Node n t1 t2) = 1 + (size t1) + (size t2) Madras Christian College, 10 December 2003 p.11

Functions on recursive datatypes Define by induction on the structure of datatype How many values are there in the tree? size :: Btreeint -> Int size Nil = 0 size (Node n t1 t2) = 1 + (size t1) + (size t2) List out all values in the tree Madras Christian College, 10 December 2003 p.11

Functions on recursive datatypes Define by induction on the structure of datatype How many values are there in the tree? size :: Btreeint -> Int size Nil = 0 size (Node n t1 t2) = 1 + (size t1) + (size t2) List out all values in the tree listout :: Btreeint -> [Int] listout Nil = [] listout (Node n t1 t2) = [n] ++ listout t1 ++ listout t2 Madras Christian College, 10 December 2003 p.11

Polymorphic recursive datatypes A binary tree to store arbitrary values at each node? data Btree a = Nil Node a (Btree a) (Btree a) Madras Christian College, 10 December 2003 p.12

Polymorphic recursive datatypes A binary tree to store arbitrary values at each node? data Btree a = Nil Node a (Btree a) (Btree a) What if we want to use Btree a as a search tree Madras Christian College, 10 December 2003 p.12

Polymorphic recursive datatypes A binary tree to store arbitrary values at each node? data Btree a = Nil Node a (Btree a) (Btree a) What if we want to use Btree a as a search tree Values in the tree must have a natural ordering Madras Christian College, 10 December 2003 p.12

Polymorphic recursive datatypes A binary tree to store arbitrary values at each node? data Btree a = Nil Node a (Btree a) (Btree a) What if we want to use Btree a as a search tree Values in the tree must have a natural ordering Conditional polymorphism! (Ord a) => data Btree a = Nil Node a (Btree a) (Btree a) Madras Christian College, 10 December 2003 p.12

Polymorphic recursive datatypes... Built in list type is a polymorphic recursive datatype Madras Christian College, 10 December 2003 p.13

Polymorphic recursive datatypes... Built in list type is a polymorphic recursive datatype data Mylist a = Emptylist Append a (Mylist a) Madras Christian College, 10 December 2003 p.13

Polymorphic recursive datatypes... Built in list type is a polymorphic recursive datatype data Mylist a = Emptylist Append a (Mylist a) Since lists are built in, they can use special symbols [] and : for constructors Emptylist and Append Madras Christian College, 10 December 2003 p.13

Adding recursive datatypes to type classes Can inherit type classes from underlying type Madras Christian College, 10 December 2003 p.14

Adding recursive datatypes to type classes Can inherit type classes from underlying type data Btree a = Nil Node a (Btree a) (Btree a) deriving (Eq, Show) Madras Christian College, 10 December 2003 p.14

Adding recursive datatypes to type classes Can inherit type classes from underlying type data Btree a = Nil Node a (Btree a) (Btree a) deriving (Eq, Show) Note: Not Eq (Btree a) but Eq a => Eq (Btree a) Madras Christian College, 10 December 2003 p.14

Adding recursive datatypes to type classes Can inherit type classes from underlying type data Btree a = Nil Node a (Btree a) (Btree a) deriving (Eq, Show) Note: Not Eq (Btree a) but Eq a => Eq (Btree a) Derived == checks that trees have same structure Madras Christian College, 10 December 2003 p.14

Adding recursive datatypes to type classes Can inherit type classes from underlying type data Btree a = Nil Node a (Btree a) (Btree a) deriving (Eq, Show) Note: Not Eq (Btree a) but Eq a => Eq (Btree a) Derived == checks that trees have same structure 6 8 /= 8 6 Madras Christian College, 10 December 2003 p.14

Adding recursive datatypes to type classes... Or we can define our own functions Madras Christian College, 10 December 2003 p.15

Adding recursive datatypes to type classes... Or we can define our own functions instance (Eq a) => Eq (Btree a) where t1 == t2 = (listout t1 == listout t2) Madras Christian College, 10 December 2003 p.15

Adding recursive datatypes to type classes... Or we can define our own functions instance (Eq a) => Eq (Btree a) where t1 == t2 = (listout t1 == listout t2) 6 8 == 8 6 because [6,8] == [6,8] Madras Christian College, 10 December 2003 p.15

Adding recursive datatypes to type classes... Or we can define our own functions instance (Eq a) => Eq (Btree a) where t1 == t2 = (listout t1 == listout t2) 6 8 == 8 6 because [6,8] == [6,8] Madras Christian College, 10 December 2003 p.15

Adding recursive datatypes to type classes... Or we can define our own functions instance (Eq a) => Eq (Btree a) where t1 == t2 = (listout t1 == listout t2) 6 8 == 8 6 because [6,8] == [6,8] Madras Christian College, 10 December 2003 p.15

Declarative programming with abstract datatypes Rotate right transformation used to balance trees x y y x t 3 t 1 t 1 t 2 t 2 t 3 Madras Christian College, 10 December 2003 p.16

Declarative programming with abstract datatypes Rotate right transformation used to balance trees x y y x t 3 t 1 t 1 t 2 t 2 t 3 rotateright (Node x (Node y t1 t2) t3) = Node y t1 (Node x t2 t3) Madras Christian College, 10 December 2003 p.16

Abstract datatypes Example Queues Madras Christian College, 10 December 2003 p.17

Abstract datatypes Example Queues Stores sequence of values in FIFO fashion Madras Christian College, 10 December 2003 p.17

Abstract datatypes Example Queues Stores sequence of values in FIFO fashion Append items at the tail of queue Madras Christian College, 10 December 2003 p.17

Abstract datatypes Example Queues Stores sequence of values in FIFO fashion Append items at the tail of queue Want a datatype Queue a with functions addq :: (Queue a) -> a -> (Queue a) removeq :: (Queue a) -> (a,queue a) isemptyq :: (Queue a) -> Bool emptyqueue :: (Queue a) Madras Christian College, 10 December 2003 p.17

Abstract datatypes... Implement a queue as a list Madras Christian College, 10 December 2003 p.18

Abstract datatypes... Implement a queue as a list data Queue a = Qu [a] Madras Christian College, 10 December 2003 p.18

Abstract datatypes... Implement a queue as a list data Queue a = Qu [a] addq :: (Queue a) -> a -> (Queue a) addq (Qu l) d = Qu (d:l) removeq :: (Queue a) -> (a,queue a) removeq (Qu l)) = (last l,qu (init l)) isemptyq :: (Queue a) -> Bool isemptyq (Qu []) = True isemptyq q = False emptyqueue :: (Queue a) emptyqueue = Qu [] Madras Christian College, 10 December 2003 p.18

Modules Group together the definitions for Queue a in a separate reusable module Madras Christian College, 10 December 2003 p.19

Modules Group together the definitions for Queue a in a separate reusable module module Queue where data Queue a = Qu [a] addq :: (Queue a) -> a -> (Queue a)... emptyqueue :: (Queue a)... Madras Christian College, 10 December 2003 p.19

Modules Group together the definitions for Queue a in a separate reusable module module Queue where data Queue a = Qu [a] addq :: (Queue a) -> a -> (Queue a)... emptyqueue :: (Queue a)... Use these definitions in another file import Queue Madras Christian College, 10 December 2003 p.19

Modules Group together the definitions for Queue a in a separate reusable module module Queue where data Queue a = Qu [a] addq :: (Queue a) -> a -> (Queue a)... emptyqueue :: (Queue a)... Use these definitions in another file import Queue How do we prevent unauthorized access to a queue? remsec :: (Queue a) -> (a,queue a) remsec (Qu (x:y:l)) = (y, Qu (x:l)) Madras Christian College, 10 December 2003 p.19

Modules... Restrict visibility outside module Madras Christian College, 10 December 2003 p.20

Modules... Restrict visibility outside module module Queue(addq,removeq,isemptyq,emptyqueue) where... Madras Christian College, 10 December 2003 p.20

Modules... Restrict visibility outside module module Queue(addq,removeq,isemptyq,emptyqueue) where... Constructor Qu is not visible if you do import Queue Madras Christian College, 10 December 2003 p.20

Modules... Restrict visibility outside module module Queue(addq,removeq,isemptyq,emptyqueue) where... Constructor Qu is not visible if you do import Queue Can override imported function with local definition Madras Christian College, 10 December 2003 p.20

Modules... Restrict visibility outside module module Queue(addq,removeq,isemptyq,emptyqueue) where... Constructor Qu is not visible if you do import Queue Can override imported function with local definition All Haskell programs implicitly import Prelude Madras Christian College, 10 December 2003 p.20

Modules... Restrict visibility outside module module Queue(addq,removeq,isemptyq,emptyqueue) where... Constructor Qu is not visible if you do import Queue Can override imported function with local definition All Haskell programs implicitly import Prelude Redefine builtin functions using import Prelude hiding (max) Madras Christian College, 10 December 2003 p.20

Modules... Restrict visibility outside module module Queue(addq,removeq,isemptyq,emptyqueue) where... Constructor Qu is not visible if you do import Queue Can override imported function with local definition All Haskell programs implicitly import Prelude Redefine builtin functions using import Prelude hiding (max) Madras Christian College, 10 December 2003 p.20

Rewriting revisted More than one expression may qualify for rewriting Madras Christian College, 10 December 2003 p.21

Rewriting revisted More than one expression may qualify for rewriting sqr x = x*x Madras Christian College, 10 December 2003 p.21

Rewriting revisted More than one expression may qualify for rewriting sqr x = x*x sqr (4+3) Madras Christian College, 10 December 2003 p.21

Rewriting revisted More than one expression may qualify for rewriting sqr x = x*x sqr (4+3) sqr 7 7*7 49 Madras Christian College, 10 December 2003 p.21

Rewriting revisted More than one expression may qualify for rewriting sqr x = x*x sqr (4+3) sqr 7 7*7 49 (4+3)*(4+3) (4+3)*7 7*7 49 Madras Christian College, 10 December 2003 p.21

Rewriting revisted More than one expression may qualify for rewriting sqr x = x*x sqr (4+3) sqr 7 7*7 49 (4+3)*(4+3) (4+3)*7 7*7 49 If there are multiple expressions to rewrite, Haskell chooses outermost expression Madras Christian College, 10 December 2003 p.21

Rewriting revisted More than one expression may qualify for rewriting sqr x = x*x sqr (4+3) sqr 7 7*7 49 (4+3)*(4+3) (4+3)*7 7*7 49 If there are multiple expressions to rewrite, Haskell chooses outermost expression Outermost reduction Lazy rewriting Evaluate argument to a function only when needed. Madras Christian College, 10 December 2003 p.21

Rewriting revisted More than one expression may qualify for rewriting sqr x = x*x sqr (4+3) sqr 7 7*7 49 (4+3)*(4+3) (4+3)*7 7*7 49 If there are multiple expressions to rewrite, Haskell chooses outermost expression Outermost reduction Lazy rewriting Evaluate argument to a function only when needed. Eager rewriting evaluate arguments before evaluating function Madras Christian College, 10 December 2003 p.21

Lazy rewriting Haskell evaluates arguments only when needed Madras Christian College, 10 December 2003 p.22

Lazy rewriting Haskell evaluates arguments only when needed power :: Float -> Int -> Float power x n = if (n == 0) then 1.0 else x * (power x (n-1)) Madras Christian College, 10 December 2003 p.22

Lazy rewriting Haskell evaluates arguments only when needed power :: Float -> Int -> Float power x n = if (n == 0) then 1.0 else x * (power x (n-1)) power (8.0/0.0) 0 1.0 Madras Christian College, 10 December 2003 p.22

Infinite lists! The following definition makes sense in Haskell from n = n : from (n+1) Madras Christian College, 10 December 2003 p.23

Infinite lists! The following definition makes sense in Haskell from n = n : from (n+1) from 2 Madras Christian College, 10 December 2003 p.23

Infinite lists! The following definition makes sense in Haskell from n = n : from (n+1) from 2 2:(from 3) 2:(3:(from 4)) 2:(3:(4:(from 5)))... Madras Christian College, 10 December 2003 p.23

Infinite lists! The following definition makes sense in Haskell from n = n : from (n+1) from 2 2:(from 3) 2:(3:(from 4)) 2:(3:(4:(from 5)))... Limit is the infinite list [2,3,4,5,...] Madras Christian College, 10 December 2003 p.23

Infinite lists! The following definition makes sense in Haskell from n = n : from (n+1) from 2 2:(from 3) 2:(3:(from 4)) 2:(3:(4:(from 5)))... Limit is the infinite list [2,3,4,5,...] Haskell can (and will) generate it incrementally, till you stop it, or it runs out of memory Madras Christian College, 10 December 2003 p.23

Infinite lists! The following definition makes sense in Haskell from n = n : from (n+1) from 2 2:(from 3) 2:(3:(from 4)) 2:(3:(4:(from 5)))... Limit is the infinite list [2,3,4,5,...] Haskell can (and will) generate it incrementally, till you stop it, or it runs out of memory Can write [2..] to denote [2,3,4,...] Madras Christian College, 10 December 2003 p.23

Why infinite lists? Can sometimes simplify a problem Madras Christian College, 10 December 2003 p.24

Why infinite lists? Can sometimes simplify a problem Consider the problem of computing the n th prime number Madras Christian College, 10 December 2003 p.24

Why infinite lists? Can sometimes simplify a problem Consider the problem of computing the n th prime number Idea: generate all prime numbers and wait for the n th entry Madras Christian College, 10 December 2003 p.24

Why infinite lists? Can sometimes simplify a problem Consider the problem of computing the n th prime number Idea: generate all prime numbers and wait for the n th entry The Sieve of Eratosthenes Madras Christian College, 10 December 2003 p.24

Why infinite lists? Can sometimes simplify a problem Consider the problem of computing the n th prime number Idea: generate all prime numbers and wait for the n th entry The Sieve of Eratosthenes Start with [2,3,4,...] Madras Christian College, 10 December 2003 p.24

Why infinite lists? Can sometimes simplify a problem Consider the problem of computing the n th prime number Idea: generate all prime numbers and wait for the n th entry The Sieve of Eratosthenes Start with [2,3,4,...] Transfer smallest number into list of primes and delete all its multiples Madras Christian College, 10 December 2003 p.24

Why infinite lists? Can sometimes simplify a problem Consider the problem of computing the n th prime number Idea: generate all prime numbers and wait for the n th entry The Sieve of Eratosthenes Start with [2,3,4,...] Transfer smallest number into list of primes and delete all its multiples Repeat second step Madras Christian College, 10 December 2003 p.24

Why infinite lists? Can sometimes simplify a problem Consider the problem of computing the n th prime number Idea: generate all prime numbers and wait for the n th entry The Sieve of Eratosthenes Start with [2,3,4,...] Transfer smallest number into list of primes and delete all its multiples Repeat second step forever! Madras Christian College, 10 December 2003 p.24

The Sieve of Eratosthenes primes = sieve [2..] where sieve (x:l) = x : sieve [y y <- l, mod y x > 0] Madras Christian College, 10 December 2003 p.25

The Sieve of Eratosthenes primes = sieve [2..] where sieve (x:l) = x : sieve [y y <- l, mod y x > 0] How does this work? Madras Christian College, 10 December 2003 p.25

The Sieve of Eratosthenes primes = sieve [2..] where sieve (x:l) = x : sieve [y y <- l, mod y x > 0] How does this work? sieve [2..] Madras Christian College, 10 December 2003 p.25

The Sieve of Eratosthenes primes = sieve [2..] where sieve (x:l) = x : sieve [y y <- l, mod y x > 0] How does this work? sieve [2..] 2:sieve [y y <- [3..], mod y 2 > 0] Madras Christian College, 10 December 2003 p.25

The Sieve of Eratosthenes primes = sieve [2..] where sieve (x:l) = x : sieve [y y <- l, mod y x > 0] How does this work? sieve [2..] 2:sieve [y y <- [3..], mod y 2 > 0] 2:sieve (3:[y y <- [4..], mod y 2 > 0]) Madras Christian College, 10 December 2003 p.25

The Sieve of Eratosthenes primes = sieve [2..] where sieve (x:l) = x : sieve [y y <- l, mod y x > 0] How does this work? sieve [2..] 2:sieve [y y <- [3..], mod y 2 > 0] 2:sieve (3:[y y <- [4..], mod y 2 > 0]) 2:3:sieve [z z <- [y <- [4..], mod y 2 > 0], mod z 3 > 0]... Madras Christian College, 10 December 2003 p.25

The Sieve of Eratosthenes primes = sieve [2..] where sieve (x:l) = x : sieve [y y <- l, mod y x > 0] How does this work? sieve [2..] 2:sieve [y y <- [3..], mod y 2 > 0] 2:sieve (3:[y y <- [4..], mod y 2 > 0]) 2:3:sieve [z z <- [y <- [4..], mod y 2 > 0], mod z 3 > 0]... 2:3:sieve [z z <- [5,7,9...], mod z 3 > 0]... Madras Christian College, 10 December 2003 p.25

The Sieve of Eratosthenes primes = sieve [2..] where sieve (x:l) = x : sieve [y y <- l, mod y x > 0] How does this work? sieve [2..] 2:sieve [y y <- [3..], mod y 2 > 0] 2:sieve (3:[y y <- [4..], mod y 2 > 0]) 2:3:sieve [z z <- [y <- [4..], mod y 2 > 0], mod z 3 > 0]... 2:3:sieve [z z <- [5,7,9...], mod z 3 > 0]... 2:3:sieve [5,7,11...]... Madras Christian College, 10 December 2003 p.25

The n th prime We now have an infinite list primes of primes Madras Christian College, 10 December 2003 p.26

The n th prime We now have an infinite list primes of primes nthprime n = head (drop (n-1) primes) Madras Christian College, 10 December 2003 p.26

The n th prime We now have an infinite list primes of primes nthprime n = head (drop (n-1) primes) Drop the first n 1 numbers from primes Madras Christian College, 10 December 2003 p.26

The n th prime We now have an infinite list primes of primes nthprime n = head (drop (n-1) primes) Drop the first n 1 numbers from primes To take the head of the rest, only need to compute one more entry in the list Madras Christian College, 10 December 2003 p.26

The n th prime We now have an infinite list primes of primes nthprime n = head (drop (n-1) primes) Drop the first n 1 numbers from primes To take the head of the rest, only need to compute one more entry in the list Once enough has been computed, the rest of primes is ignored! Madras Christian College, 10 December 2003 p.26

Concluding remarks Functional programming provides a framework for declarative programming Madras Christian College, 10 December 2003 p.27

Concluding remarks Functional programming provides a framework for declarative programming Provably correct programs Madras Christian College, 10 December 2003 p.27

Concluding remarks Functional programming provides a framework for declarative programming Provably correct programs Rapid prototyping Madras Christian College, 10 December 2003 p.27

Concluding remarks Functional programming provides a framework for declarative programming Provably correct programs Rapid prototyping Haskell has a powerful typing mechanism Madras Christian College, 10 December 2003 p.27

Concluding remarks Functional programming provides a framework for declarative programming Provably correct programs Rapid prototyping Haskell has a powerful typing mechanism Conditional polymorphism Madras Christian College, 10 December 2003 p.27

Concluding remarks Functional programming provides a framework for declarative programming Provably correct programs Rapid prototyping Haskell has a powerful typing mechanism Conditional polymorphism Modules with hiding and overriding for abstract datatypes Madras Christian College, 10 December 2003 p.27

Concluding remarks Functional programming provides a framework for declarative programming Provably correct programs Rapid prototyping Haskell has a powerful typing mechanism Conditional polymorphism Modules with hiding and overriding for abstract datatypes Lazy evaluation permits infinite data structures Madras Christian College, 10 December 2003 p.27

For more information Software and other resources http://www.haskell.org Quick tutorial A Gentle Introduction to Haskell by Paul Hudak et al Textbooks The Craft of Functional Programming by Simon Thompson Introduction to Functional Programming in Haskell by Richard Bird Madras Christian College, 10 December 2003 p.28