Loading in 2 Seconds...

CS321 Functional Programming 2 Dr John A Sharp 10 credits Tuesday 10am Robert Recorde Room Thursday 11am Robert Recorde

Loading in 2 Seconds...

- By
**emily** - Follow User

- 318 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about 'CS321 Functional Programming 2 Dr John A Sharp 10 credits Tuesday 10am Robert Recorde Room Thursday 11am Robert Recorde' - emily

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

CS321 Functional Programming 2

Dr John A Sharp

10 credits

Tuesday 10am Robert Recorde Room

Thursday 11am Robert Recorde Room

© JAS 2005

Assessment

80% written examination in May/June

20% coursework

probably two assignments – roughly weeks 4 and 7

© JAS 2005

(Provisional)

- Type Classes
- Programming with Streams
- Lazy Data Structures
- Memoization
- Lambda Calculus
- Type Checking and Inference
- Implementation Approaches

© JAS 2005

Course will not follow any specific text

S Thompson, Haskell: The Craft of Functional Programming, Second Edition, Addison-Wesley, 1999

P Hudak, The Haskell School of Expression – Learning Functional Programming through Multimedia, Cambridge University press, 2000

R Bird, Introduction to Functional Programming using Haskell, Second Edition, Prentice-Hall, 1998

A J T Davie, An Introduction to Functional Programming using Haskell, Cambridge University Press, 1992

www.haskell.org

© JAS 2005

Notes will be handed out in sections

mainly copies of PowerPoint slides

some Haskell programs

They will also be available on a course web page

www.cs.swan.ac.uk/~csjohn/cs321/main.html

© JAS 2005

Assumptions from CS221 Functional Programming 1

- Familiar with principles of Functional Programming
- Able to write and run simple Haskell programs/scripts
- Familiar with concepts of types and higher-order functions
- Able to define own structured types
- Familiar with basic λ calculus

© JAS 2005

Type Classes

Monomorphic functions

add1 :: Int -> Int

add1 x = x + 1

Parametric polymorphic functions

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

map f [] = []

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

© JAS 2005

Ad-hoc polymorphic functions

add :: Int -> Int -> Int

add x y = x + y

add :: Float -> Float -> Float

add :: Double -> Double -> Double

add :: Num a => a -> a -> a

add x y = x + y

© JAS 2005

This sort of type expression is sometimes referred to as a “Qualified Type”.

Num a is termed a predicate which limits the types for which a type expression is valid. It is also termed the context for the type.

Multiple predicates can be defined

contrived :: (Num a, Eq b)=>a->b->b->a

contrived x y z = if y == z

then x + x

else x + x + x

© JAS 2005

For completeness we could specify an empty context for any polymorphic function

map :: () => (a -> b) -> [a] -> [b]

map f [] = []

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

© JAS 2005

The predicate restricts the set of types for which the expression is valid.

Num a should be read as “for all types a that are members of the class Num.

Members of a class (which are types) are also called instances of a class.

© JAS 2005

Type classes allow us to group together types which have common properties (or more accurately common operations that can be applied to them).

An obvious example is the types for which equality can be defined. An appropriate Class is defined in the standard prelude:-

class Eq a where

(==), (/=) :: a -> a -> Bool

x /= y = not ( x == y )

© JAS 2005

class Eq a where

header introducing name of class and parameters (a)

(==), (/=) :: a -> a -> Bool

signature listing functions applicable to instances of

class and their type

(==) and (/=) are termed member functions

x /= y = not ( x == y )

default definitions of member functions that can be

overridden

© JAS 2005

Having defined the concept of an equality class the next step is to define various instances of the class.

The following examples are again taken from the standard prelude.

instance Eq Int where (==) = primEqInt

--primEqint is a primitive Haskell function

instance Eq Bool where

True == True = True

False == False = True

_ == _ = False

-- defined by pattern matching

© JAS 2005

c == d = ord c == ord d

-- note the second == is defined on integers

instance (Eq a,Eq b) => Eq(a,b) where

(x,y) == (u,v) = x==u && y==v

-- pairwise equality

instance Eq a => Eq [a] where

[] == [] = True

[] == (y:ys) = False

(x:xs)== [] = False

(x:xs)== (y:ys) = x==y && xs==ys

-- extend to equality on lists

© JAS 2005

These declarations allow us to obtain definitions for equality on an infinite family of types involving pairs, and lists of (pairs and lists of) Int, Bool, Char.

The general format of an instance declaration is

instancecontext=>predicatewhere

definitions of member functions

We can define instances of Eq for user defined types.

© JAS 2005

Consider a definition of Sets

data Set a = Set [a]

To make the type Set a a member of the class Eq we define an instance of Eq.

Note that we do not use the standard (==) on the type a as we do not require the elements of the Sets to be in the same order.

instance Eq a => Eq (Set a) where

Set xs == Set ys =

xs 'subset' ys && ys 'subset xs

where xs 'subset' ys =

all ('elem' ys) xs

© JAS 2005

Some types (such as functions) can not be defined to be members of the class Eq (for obvious reasons, I hope). The error message that results from a mistaken attempt to test for equality can be obscure.

It is possible to make a test for equality on functions type check ok and produce a run-time error message using the standard error function defined in the standard prelude.

instance Eq (a->b) where

(==) = error "== not defined on fns"

© JAS 2005

If the functions are defined to operate on a finite set of elements then a form of equality can be defined.

instance Eq a => Eq (Bool->a) where

f == g = f False == g False &&

f True == g True

It is possible to have instances for both Eq (a->b) and

Eq (Bool->a) as long as both are not required at the same time in type checking some expression.

An a

© JAS 2005

Inheritance, Derived Classes, Superclasses, and Subclasses

In general a class declaration has the form

classcontext=>Class a1 .. anwhere

type declarations for member functions

default declarations of member functions

where Class is the name of a new type class which takes n arguments (a1 .. an ).

As with instances the context must be satisfied in order to construct any instance of the Class.

The predicates in the context part of the declaration are called the superclasses of Class. Class is a subclass of the classes in the context.

© JAS 2005

Consider a definition of a Class Ord whose instances have both strict (<), (>) and non-strict (<=), (>=) versions of an ordering defined on their elements.

class Eq a => Ord a where

(<),(<=),(>),(>=) :: a->a->Bool

max,min :: a->a->a

x < y = x <= y && x /= y

x >= y = y <= x

x > y = y < x

max x y | x >= y = x

| y >= x = y

min x y | x <= y = x

| y <= x = y

© JAS 2005

Why define Eq as a superclass of Ord?

• the default definition of (<) relies on the use of (/=) taken from the class Eq. Therefore every instance of Ord must be an instance of Eq.

• given the definition of non-strict ordering (<=) it is always possible to define (==) and hence (/=) using

x==y = x<=y && y<= x

so there will be no loss of generality in requiring Eq to be a superclass of Ord.

© JAS 2005

It is possible for some types to require a type to be an instance of class Ord in order to define it to be an instance of the class Eq.

For example consider an alternative way of defining equality on Sets.

instance Ord (Set a) => Eq (Set a) where

x == y = x <= y && y <= x

instance Eq a => Ord (Set a) where

Set xs <= Set ys = all ('elem' ys) xs

© JAS 2005

Ord

Enum

Integral

Fractional

RealFloat

Num

Real

RealFrac

Floating

Monad

Read

Functor

Show

MonadPlus

All Prelude Types

All but IO, (->)

IO, [], Maybe

IO, [], Maybe

IO, [], Maybe

Numeric Class Hierarchy

(), Bool, Char,

Int, Integer, Float,

Double, Ordering

All but IO, (->),

IOError

Int, Integer

All but IO, (->)

Int, Integer,

Float, Double

Int, Integer,

Float, Double

Float, Double

Bounded

Int, Char, Bool, (),

Ordering, tuples

Float, Double

Float, Double

Float, Double

© JAS 2005

Operations defined in these classes (red means must be provided)

Eq == /=

Ord < <= > >= max min compare

Num + - * negate abs signum fromInteger

Real toRational

Enum succ pred toEnum fromEnum enumFrom enumFromThen enumFromTo

enumFromThenTo

Integral quot rem div mod quotRem divMod

toInteger

Fractional / recip fromRational

RealFrac properFraction truncate round

ceiling floor

© JAS 2005

Floating pi exp log sqrt ** logBase sin cos

tan asin acos atan sinh cosh tanh asinh acosh atanh

RealFloat floatRadix floatDigits floatRange

decodeFloat encodeFloat exponent

significand scaleFloat isNaN

isInfinite isDenormalized isIEEE

isNegativeZero atan2

Show showsPrec showList show

Read readsPrec readList

Bounded minBound maxBound

Monad >>= >> return fail

Functor fmap

© JAS 2005

By introducing type classes Haskell has enabled programmers to use numerical types in a manner similar to the way they are used to in traditional imperative languages.

But what else are type classes useful for?

They can provide a mechanism for adding your own numerical types eg complex numbers

They can provide a mechanism for a sort of object-oriented programming

© JAS 2005

Imperative O-O Classes = Data + Methods

Objects contain values which can be changed by methods

Classes inherit data fields and methods

FP Classes = Methods

Methods/Functions can be applied to variables whose type is an Instance of a Class

Classes inherit methods/functions

© JAS 2005

Implementing Type Classes – An Approach using Dictionaries

A function with a qualified type context=>type is implemented by a function which takes an extra argument for every predicate in the context.

When the function is used each of these parameters is filled by a 'dictionary' which gives the values of each of the member functions in the appropriate class.

For example, for the class Eq each dictionary has at least two elements containing the definitions of the functions for (==) and (/=).

© JAS 2005

(#n d) to select nth element of dictionary

{dict} to denote a specific dictionary

( contents not displayed)

dnnn for a dictionary variable representing an unknown

dictionary

d:: Class Inst

to denote that d is dictionary for the instance

Inst of a class Class

© JAS 2005

The member functions of the class Eq thus behave as if defined

(==) d1 = (#1 d1)

(/=) d1 = (#2 d1)

The dictionary for Eq Int contains two entries:

d1 :: Eq Int

primEqInt

defNeq d1

© JAS 2005

Evaluating 2 == 3

=> (==) d1 2 3 => (#1 d1) 2 3

=> primEqInt 2 3

=> False

Evaluating 2 /= 3

=> (/=) d1 2 3 => (#2 d1) 2 3

=> defNeq d1 23

=> not ((==) d1 2 3)

=> not ((#1 d1) 2 3)

=> not (primEqInt 2 3)

=> not False

=> True

© JAS 2005

Now consider a more complex example

We have seen definitions of (==) in the instances of the class Eq defined for pairs and lists.

eqPair d (x,y) (u,v) =

(==) (#3 d) x u && (==) (#4 d) y v

eqList d [] [] = True

eqList d [] (y:ys) = False

eqList d (x:xs) [] = False

eqList d (x:xs) (y:ys) =

(==) (#3 d) x y && (==) d xs ys

© JAS 2005

The dictionary structure for Eq (Int, [Int]) is

d3 :: Eq (Int, [Int])

eqPair d3

defNeq d3

d2 :: Eq [Int]

eqList d3

defNeq d3

d1 :: Eq Int

defNeq d3

primEqInt

© JAS 2005

(2,[1]) == (2,[1.3])

=>

eqPair d3

(==) d3

(#1 d3)

(2,[1]) (2,[1,3])

=>

(==)

primEqInt

(#1 d1)

True

(#3 d3)

d1

2 2

&&

(==)

eqList d2

(#1 d2)

(#4 d3)

d2

[1] [1,3]

=>

(==)

primEqInt

(#1 d1)

True

(#3 d2)

d1

1 1

&&

eqList d2

(==)

(#1 d2)

False

d2

[]

[3]

© JAS 2005

Dictionaries for superclasses can be defined in a similar way as instance dictionaries.

For example for the Ord class which has Eq as a context

class Eq => Ord a where

(<),(<=),(>),(>=) :: a->a->Bool

max,min :: a->a->Bool

the dictionary would contain the following

(<)

(<=)

(>)

Eq a

(>=)

max

min

defLessThan d x y = (<=) d x y && (/=) (#7 d) x y

© JAS 2005

Combining Classes

A dictionary consists of three (possibly empty) components:

- Superclass dictionaries
- Instance specific dictionaries
- Implementation of class members

instances of Eq have no superclass dictionaries

Eq Int has no instance specific dictionary

Classes with no member functions can be used as abbreviations for lists of predicates

class C a where cee :: a -> a

class D a where dee :: a -> a

class (C a, D a) => CandD a

© JAS 2005

Contexts and Derived Types

eg1 x = [x] == [x] || x == x

Since the(==)operation is applied to both lists and an argument xif x::athen we would seem to require instancesEq aandEq [a]giving a type signature

(Eq [a], Eq a) => a -> Bool

with translation

eg1 d1 d2 x = (==) d1 [x] [x] || (==) d2 x x

© JAS 2005

However, givend1::Eq[a]we can always findEq aby taking the third element ofd1((#3 d1)::Eq a).

Since it is more efficient to select an element from a dictionary than to complicate both type and translation with extra parameters the type ofeg1is (by default) derived as

Eq [a] => a -> Bool

with translation

eg1 d1 x = (==) d1 [x] [x] ||(==) (#3 d1) x x

© JAS 2005

If you definitely required the tuple context then this can be produced by explicitly defining the type and context ofeg1

eg2 = (\x y-> x ==x || y==y)

The type derived for this is

(Eq b, Eq a) => a -> b -> Bool

with translation

\d1 d2 x y -> (==) d2 x x || (==) d1 y y

© JAS 2005

If you wished to ensure that only one dictionary parameter is used you could explicitly type is using

Eq (a,b) => a -> b -> Bool

with translation

\d1 x y -> (==) (#3 d1) x x || (==) (#4 d1) y y

© JAS 2005

Download Presentation

Connecting to Server..