Page 1

Overloading and type classes in Haskell

How to make ad-hoc polymorphism less ad hoc (*)

Page 2

Overloading

Page 3

Overloading in Haskell

Page 4

Type classes and instances

Page 5

The Eq class

Page 6

Defining your own instances

Page 7

Equality instance for pairs

Page 8

Equality instance for lists

Page 9

Infinitely many instances

Page 10

The Ord class

Page 11

The Enum class

Page 12

The enumeration syntax

Enumerations can be used for any type in the
Enum
class, e.g.:

Page 13

The Bounded class

Page 14

The Show and Read classes

Page 15

Derived instances

Page 16

Defining your own Show instance 1

data Suit = Spades | Hearts | Diamonds | Clubs
            deriving (Eq,Ord,Enum,Bounded)

instance Show Suit where
  show Spades   = "♠"
  show Hearts   = "♥"
  show Diamonds = "♦"
  show Clubs    = "♣"
Page 17

Defining your own Show instance 2

data Rank = Numeric Int | Jack | Queen | King | Ace
            deriving (Eq,Ord)

instance Show Rank where
  show (Numeric n) = show n
  show Jack        = "J"
  show Queen       = "Q"
  show King        = "K"
  show Ace         = "A"
Page 18

Defining your own Show instance 3

Page 19

Defining your own Show instance 4

Page 20

Before and after

Page 21

More type classes 1

Page 22

More type classes 2

Page 23

Defining your own class 1

Page 24

SmallCheck

Page 25

Defining your own class 2

Exhaustive Testing

class SmallCheck prop where
  smallCheck :: prop -> Bool

instance SmallCheck Bool where
  smallCheck b = b

instance (Small a,SmallCheck prop) => SmallCheck (a->prop) where
  smallCheck f = and [smallCheck (f x)|x<-values]
Page 26

Ambiguity

Page 27

Numeric literals, monomorphism and defaulting

Page 28

A few notes on type classes

Page 29

Overloading in Standard ML

Page 30

Type classes in Haskell vs OO languages