1 COMP313A Programming Languages Functional Programming (7)

20
1 COMP313A Programming Languages Functional Programming (7)

Transcript of 1 COMP313A Programming Languages Functional Programming (7)

Page 1: 1 COMP313A Programming Languages Functional Programming (7)

1

COMP313A Programming Languages

Functional Programming (7)

Page 2: 1 COMP313A Programming Languages Functional Programming (7)

2

Lecture Outline

• Type checking– type classes

• Some Haskell tricks

Page 3: 1 COMP313A Programming Languages Functional Programming (7)

3

Polymorphism versus Operator Overloading

• Use the same function name with different types

• Polymorphism – the same function definition

• Operator overloading – different function for each different type

Page 4: 1 COMP313A Programming Languages Functional Programming (7)

4

Polymorphism

first (x, y) = x

list x = [x]

Page 5: 1 COMP313A Programming Languages Functional Programming (7)

5

Operator Overloading

• ==• Defined for Int and ….

• One for pairs

(n,m) == (p,q) = (n==p) && (m==q)

Page 6: 1 COMP313A Programming Languages Functional Programming (7)

6

Type Classes in Haskell• Underpinned by the notion of overloading• Mechanism by which overloaded functions can be given

types – why? • Elem does not make sense for every possible a• How can we still have the flexibility of polymorphic types

and ensure type safety• Use type classes to do this

elem :: a -> [a] -> Boolelem x [] = Falseelem x (y:ys) = (x == y) || elem x ys

Page 7: 1 COMP313A Programming Languages Functional Programming (7)

7

elemBool :: Bool -> [Bool] -> Boolelem x [] = Falseelem x (y:ys) = (x == Booly) || elemBool x ys

elemInt :: Int -> [Int] -> Boolelem x [] = Falseelem x (y:ys) = (x == Inty) || elemInt x ys

We could have:

YUK!

Page 8: 1 COMP313A Programming Languages Functional Programming (7)

8

Type Classes in HaskellOperator Overloading

• Could instead make the equality function a parameter:

• But this is too general

• Instead

• Where type a is restricted to those types which can be used with equality

elem :: (a ->a->Bool) ->a -> [a] -> Bool

elem :: a -> [a] -> Bool

Page 9: 1 COMP313A Programming Languages Functional Programming (7)

9

elemBool :: Bool -> [Bool] -> Boolelem x [] = Falseelem x (y:ys) = (x == y) || elemBool x ys

elemInt :: Int -> [Int] -> Boolelem x [] = Falseelem x (y:ys) = (x == y) || elemInt x ys

elem :: Eq a a -> [a] -> Boolelem x [] = Falseelem x (y:ys) = (x == y) || elem x ys

context

Page 10: 1 COMP313A Programming Languages Functional Programming (7)

10

Operator Overloading

• Think of as:– the equality operator has a set of types

defined for it. These are the types which can be used with the equality operator

• Thus we say: – The definition of elem can be used over all

types with equality• Type classes are used to give a type to

functions like elem.

Page 11: 1 COMP313A Programming Languages Functional Programming (7)

11

Type Checking and Classesmember [] y = Falsemember (x:xs) y = x == y || member xs y

Apply the function member to an expression e, whose type is Ord b => [[b]] Eg. A list of lists of integers. Integers

have an ordering

partial application member e

Unify the type expressions giving (without contexts)

member :: [[b]] -> [b] -> Bool e::[[b]]

The type will be?

And member e the type [b] -> Bool

Page 12: 1 COMP313A Programming Languages Functional Programming (7)

12

Type Checking and Classes

• But also have to unify the contexts(Eq [b], Ord b) i.e are the set of defined for Eq the same as the set

of types defined for Ord

• Now check and simplify the context– the requirements in a context, ([b], b) can only

apply to type variables– need to simplify requirements like Eq [b]

[3, 5, 6] == [3, 5, 6] 3==3 & 5==5 & 6 == 6

instance Eq a => Eq [a] where….

– (Eq b, Ord b)

Page 13: 1 COMP313A Programming Languages Functional Programming (7)

13

• (Eq b, Ord b)

Class Eq a => Ord a where …. tells us how the class is derived but not the

instances

Any instance of Ord is an instance of Eq but …so….

What should the unified context be…

Page 14: 1 COMP313A Programming Languages Functional Programming (7)

14

Ord b

member e :: Ord b => [b] -> Bool

Page 15: 1 COMP313A Programming Languages Functional Programming (7)

15

Assignment hints

• Some Haskell tricks

Page 16: 1 COMP313A Programming Languages Functional Programming (7)

16

the cons operator

x:restx:y:restx:y:z:rest

head x head y head zhead (drop 1 x)

[x,y,z]:rest

To get the last ement in a list use “last”To get a list with all but the first element use “tail”

tail rest

head last rest

[["auckland", "pokeno", "sh1"],["pokeno", "tauranga", "sh2"], ["tauranga", "opotiki", "sh2"], ["opotiki", "gisborne", "sh2"], ["gisborne", "napier", "sh2"], [ "napier", "woodville", "sh2"]]

Page 17: 1 COMP313A Programming Languages Functional Programming (7)

17

How would you get the second last element in a list?

Page 18: 1 COMP313A Programming Languages Functional Programming (7)

18

To make a list

x:rest

[x] [x,y,z] etc

If x y and z are characters then the type of [x,y,z]is String

[x,y,z]:rest

[x,y,z] == “fred”

A function to make a list out of some argument

list x = [x]

Page 19: 1 COMP313A Programming Languages Functional Programming (7)

19

Sometimes it is easier to just create an auxiliary function which accumulates the result as an argument

Sometimes it is easier to build a list in reverse order

Take the list “the quick brown fox”

process_list text = process_list_aux text “”

process_list_aux [] result = ------------process_list_aux x:text result = process_list_aux text -----------

: versus ++

Page 20: 1 COMP313A Programming Languages Functional Programming (7)

20