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

The Ord class

Page 10

The Enum class

Page 11

The enumeration syntax

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

Page 12

The Bounded class

Page 13

The Show and Read classes

Page 14

Derived instances

Page 15

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 16

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 17

Defining your own Show instance 3

Page 18

Defining your own Show instance 4

Page 19

Before and after

Page 20

Defining your own class 1

Page 21

Defining your own class 2

Exhaustive Testing

Page 22

Ambiguity

Page 23

Numeric literals, monomorphism and defaulting

Page 24

A few notes on type classes

Page 25

Overloading in Standard ML

Page 26

Type classes in Haskell vs OO languages