0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

50
1 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers

Transcript of 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

Page 1: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

1

PROGRAMMING IN HASKELL

Chapter 9 - Higher-Order Functions,Functional Parsers

Page 2: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

Introduction

2

A function is called higher-order if it takes a function as an argument or returns a function as a result.

twice :: (a a) a atwice f x = f (f x)

twice is higher-order because ittakes a function as its first

argument.

Page 3: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

Why Are They Useful?

3

Common programming idioms can be encoded as functions within the language itself.

Domain specific languages can be defined as collections of higher-order functions.

Algebraic properties of higher-order functions can be used to reason about programs.

Page 4: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

The Map Function

4

The higher-order library function called map applies a function to every element of a list.

map :: (a b) [a] [b]

For example:

> map (+1) [1,3,5,7]

[2,4,6,8]

Page 5: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

5

Alternatively, for the purposes of proofs, the map function can also be defined using recursion:

The map function can be defined in a particularly simple manner using a list comprehension:

map f xs = [f x | x xs]

map f [] = []

map f (x:xs) = f x : map f xs

Page 6: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

The Filter Function

6

The higher-order library function filter selects every element from a list that satisfies a predicate.

filter :: (a Bool) [a] [a]

For example:

> filter even [1..10]

[2,4,6,8,10]

Page 7: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

7

Alternatively, it can be defined using recursion:

Filter can be defined using a list comprehension:

filter p xs = [x | x xs, p x]

filter p [] = []

filter p (x:xs)

| p x = x : filter p xs

| otherwise = filter p xs

Page 8: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

The Foldr Function

8

A number of functions on lists can be defined using the following simple pattern of recursion:

f [] = vf (x:xs) = x f xs

f maps the empty list to some value v, and any non-empty list to some function applied to its head

and f of its tail.

Page 9: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

9

For example:

sum [] = 0sum (x:xs) = x + sum xs

and [] = Trueand (x:xs) = x && and xs

product [] = 1product (x:xs) = x * product xs

v = 0 = +

v = 1 = *

v = True = &&

Page 10: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

10

The higher-order library function foldr (fold right) encapsulates this simple pattern of recursion, with the function and the value v as arguments.

For example:

sum = foldr (+) 0

product = foldr (*) 1

or = foldr (||) False

and = foldr (&&) True

Page 11: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

11

Foldr itself can be defined using recursion:

foldr :: (a b b) b [a] b

foldr f v [] = v

foldr f v (x:xs) = f x (foldr f v xs)

However, it is best to think of foldr non-recursively, as simultaneously replacing each (:) in a list by a given function, and [] by a given value.

Page 12: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

12

sum [1,2,3]

foldr (+) 0 [1,2,3]=

foldr (+) 0 (1:(2:(3:[])))=

1+(2+(3+0))=

6=

For example:

Replace each (:)by (+) and [] by

0.

Page 13: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

13

product [1,2,3]

foldr (*) 1 [1,2,3]=

foldr (*) 1 (1:(2:(3:[])))=

1*(2*(3*1))=

6=

For example:

Replace each (:)by (*) and [] by

1.

Page 14: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

Other Foldr Examples

14

Even though foldr encapsulates a simple pattern of recursion, it can be used to define many more functions than might first be expected.

Recall the length function:

length :: [a] Int

length [] = 0

length (_:xs) = 1 + length xs

Page 15: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

15

length [1,2,3]

length (1:(2:(3:[])))=

1+(1+(1+0))=

3=

Hence, we have:

length = foldr (_ n 1+n) 0

Replace each (:) by _ n

1+n and [] by 0.

For example:

Page 16: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

16

Now recall the reverse function:

reverse [] = []reverse (x:xs) = reverse xs ++ [x]

reverse [1,2,3]

reverse (1:(2:(3:[])))=

(([] ++ [3]) ++ [2]) ++ [1]=

[3,2,1]=

For example:Replace each (:) by x xs xs ++ [x] and [] by [].

Page 17: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

17

Hence, we have:

reverse = foldr (x xs xs ++ [x]) []

Finally, we note that the append function (++) has a particularly compact definition using foldr:

(++ ys) = foldr (:) ys

Replace each (:) by (:) and []

by ys.

Page 18: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

Why Is Foldr Useful?

18

Some recursive functions on lists, such as sum, are simpler to define using foldr.

Properties of functions defined using foldr can be proved using algebraic properties of foldr, such as fusion and the banana split rule.

Advanced program optimisations can be simpler if foldr is used in place of explicit recursion.

Page 19: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

Other Library Functions

19

The library function (.) returns the composition of two functions as a single function.

(.) :: (b c) (a b) (a c)f . g = x f (g x)

For example:

odd :: Int Boolodd = not . even

Page 20: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

20

The library function all decides if every element of a list satisfies a given predicate.

all :: (a Bool) [a] Boolall p xs = and [p x | x xs]

For example:

> all even [2,4,6,8,10]

True

Page 21: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

21

Dually, the library function any decides if at leastone element of a list satisfies a predicate.

any :: (a Bool) [a] Boolany p xs = or [p x | x xs]

For example:

> any isSpace "abc def"

True

Page 22: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

22

The library function takeWhile selects elements from a list while a predicate holds of all the elements.

takeWhile :: (a Bool) [a] [a]takeWhile p [] = []takeWhile p (x:xs) | p x = x : takeWhile p xs | otherwise = []

For example:

> takeWhile isAlpha "abc def"

"abc"

Page 23: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

23

Dually, the function dropWhile removes elements while a predicate holds of all the elements.

dropWhile :: (a Bool) [a] [a]dropWhile p [] = []dropWhile p (x:xs) | p x = dropWhile p xs | otherwise = x:xs

For example:

> dropWhile isSpace " abc"

"abc"

Page 24: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

Exercises

24

(3) Redefine map f and filter p using foldr.

(2) Express the comprehension [f x | x xs, p x] using the functions map and filter.

(1)What are higher-order functions that return functions as results better known as?

Page 25: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

What is a Parser?

25

A parser is a program that analyses a piece of text to determine its syntactic structure.

23+4 means 4

+

2

32

Page 26: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

Where Are They Used?

26

Almost every real life program uses some form of parser to pre-process its input.

Haskell programs

Shell scripts

HTML documents

Hugs

Unix

Explorer

parses

Page 27: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

The Parser Type

27

In a functional language such as Haskell, parsers can naturally be viewed as functions.

type Parser = String Tree

A parser is a function that takes a string and returns some form

of tree.

Page 28: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

28

However, a parser might not require all of its input string, so we also return any unused input:

type Parser = String (Tree,String)

A string might be parsable in many ways, including none, so we generalize to a list of results:

type Parser = String [(Tree,String)]

Page 29: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

29

Finally, a parser might not always produce a tree, so we generalize to a value of any type:

type Parser a = String [(a,String)]

Note:

For simplicity, we will only consider parsers that either fail and return the empty list of results, or succeed and return a singleton list.

Page 30: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

Basic Parsers

30

The parser item fails if the input is empty, and consumes the first character otherwise:

item :: Parser Char

item = inp case inp of

[] []

(x:xs) [(x,xs)]

Page 31: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

31

The parser failure always fails:

failure :: Parser a

failure = inp []

The parser return v always succeeds, returning the value v without consuming any input:

return :: a Parser a

return v = inp [(v,inp)]

Page 32: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

32

The parser p +++ q behaves as the parser p if it succeeds, and as the parser q otherwise:

(+++) :: Parser a Parser a Parser a

p +++ q = inp case p inp of

[] parse q inp

[(v,out)] [(v,out)]

The function parse applies a parser to a string:

parse :: Parser a String [(a,String)]

parse p inp = p inp

Page 33: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

Examples

33

% hugs Parsing

> parse item ""[]

> parse item "abc"[('a',"bc")]

The behavior of the five parsing primitives can be illustrated with some simple examples:

Page 34: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

34

> parse failure "abc"

[]

> parse (return 1) "abc"

[(1,"abc")]

> parse (item +++ return 'd') "abc"

[('a',"bc")]

> parse (failure +++ return 'd') "abc"

[('d',"abc")]

Page 35: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

35

Note:

The library file Parsing is available on the web from the Programming in Haskell home page.

For technical reasons, the first failure example actually gives an error concerning types, but this does not occur in non-trivial examples.

The Parser type is a monad, a mathematical structure that has proved useful for modeling many different kinds of computations.

Page 36: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

Sequencing

36

A sequence of parsers can be combined as a single composite parser using the keyword do.

For example:

p :: Parser (Char,Char)

p = do x item

item

y item

return (x,y)

Page 37: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

37

Note:

Each parser must begin in precisely the same column. That is, the layout rule applies.

The values returned by intermediate parsers are discarded by default, but if required can be named using the operator.

The value returned by the last parser is the value returned by the sequence as a whole.

Page 38: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

38

If any parser in a sequence of parsers fails, then the sequence as a whole fails. For example:

> parse p "abcdef"[((’a’,’c’),"def")]

> parse p "ab"[]

The do notation is not specific to the Parser type, but can be used with any monadic type.

Page 39: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

Derived Primitives

39

sat :: (Char Bool) Parser Charsat p = do x item if p x then return x else failure

Parsing a character that satisfies a predicate:

Page 40: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

40

digit :: Parser Char

digit = sat isDigit

char :: Char Parser Char

char x = sat (x ==)

Parsing a digit and specific characters:

Applying a parser zero or more times:

many :: Parser a Parser [a]

many p = many1 p +++ return []

Page 41: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

41

many1 :: Parser a -> Parser [a]many1 p = do v p vs many p return (v:vs)

Applying a parser one or more times:

Parsing a specific string of characters:

string :: String Parser Stringstring [] = return []string (x:xs) = do char x string xs return (x:xs)

Page 42: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

Example

42

We can now define a parser that consumes a list of one or more digits from a string:

p :: Parser Stringp = do char '[' d digit ds many (do char ',' digit) char ']' return (d:ds)

Page 43: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

43

For example:

> parse p "[1,2,3,4]"

[("1234","")]

> parse p "[1,2,3,4"

[]

Note:

More sophisticated parsing libraries can indicate and/or recover from errors in the input string.

Page 44: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

Arithmetic Expressions

44

Consider a simple form of expressions built up from single digits using the operations of addition + and multiplication *, together with parentheses.

We also assume that:

* and + associate to the right;

* has higher priority than +.

Page 45: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

45

Formally, the syntax of such expressions is defined by the following context free grammar:

expr term '+' expr term

term factor '*' term factor

factor digit '(' expr ')‘

digit '0' '1' '9'

Page 46: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

46

However, for reasons of efficiency, it is important to factorise the rules for expr and term:

expr term ('+' expr )

term factor ('*' term )

Note:

The symbol denotes the empty string.

Page 47: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

47

It is now easy to translate the grammar into a parser that evaluates expressions, by simply rewriting the grammar rules using the parsing primitives.

That is, we have:

expr :: Parser Intexpr = do t term do char '+' e expr return (t + e) +++ return t

Page 48: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

48

factor :: Parser Intfactor = do d digit return (digitToInt d) +++ do char '(' e expr char ')' return e

term :: Parser Intterm = do f factor do char '*' t term return (f * t) +++ return f

Page 49: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

49

Finally, if we define

eval :: String Int

eval xs = fst (head (parse expr xs))

then we try out some examples:

> eval "2*3+4"10

> eval "2*(3+4)"14

Page 50: 0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.

Exercises

50

(2) Extend the expression parser to allow the use of subtraction and division, based upon the following extensions to the grammar:

expr term ('+' expr '-' expr )

term factor ('*' term '/' term )

(1) Why does factorising the expression grammar make the resulting parser more efficient?