Modelling & Datatypes Koen Lindstrm Claessen Software Software - - PowerPoint PPT Presentation

modelling datatypes
SMART_READER_LITE
LIVE PREVIEW

Modelling & Datatypes Koen Lindstrm Claessen Software Software - - PowerPoint PPT Presentation

Modelling & Datatypes Koen Lindstrm Claessen Software Software = Programs + Data Modelling Data A big part of designing software is modelling the data in an appropriate way Numbers are not good for this! We model the data by


slide-1
SLIDE 1

Modelling & Datatypes

Koen Lindström Claessen

slide-2
SLIDE 2

Software

Software = Programs + Data

slide-3
SLIDE 3

Modelling Data

  • A big part of designing software is

modelling the data in an appropriate way

  • Numbers are not good for this!
  • We model the data by defining new types
slide-4
SLIDE 4

Modelling a Card Game

  • Every card has a suit
  • Model by a new type:

data Suit = Spades | Hearts | Diamonds | Clubs The new type The values

  • f this type

Hearts, Whist, Plump, Bridge, ...

slide-5
SLIDE 5

Investigating the new type

Main> :i Suit

  • - type constructor

data Suit

  • - constructors:

Spades :: Suit Hearts :: Suit Diamonds :: Suit Clubs :: Suit Main> :i Spades Spades :: Suit -- data constructor The new type The new values

  • - constructors

Types and constructors start with a capital letter

slide-6
SLIDE 6

Printing Values

  • Fix

Main> Spades ERROR - Cannot find "show" function for: *** Expression : Spades *** Of type : Suit Main> :i show show :: Show a => a -> String -- class member Needed to print values data Suit = Spades | Hearts | Diamonds | Clubs deriving Show Main> Spades Spades

slide-7
SLIDE 7

The Colours of Cards

  • Each suit has a colour – red or black
  • Model colours by a type
  • Define functions by pattern matching

data Colour = Black | Red deriving Show colour :: Suit -> Colour colour Spades = Black colour Hearts = Red colour Diamonds = Red colour Clubs = Black One equation per value Main> colour Hearts Red

slide-8
SLIDE 8

The Colours of Cards

colour :: Suit -> Colour colour Spades = Black colour Hearts = Red colour Diamonds = Red colour Clubs = Black colour :: Suit -> Colour colour Spades = Black colour Clubs = Black colour _ = Red Matches anything

slide-9
SLIDE 9

The Ranks of Cards

  • Cards have ranks: 2..10, J, Q, K, A
  • Model by a new type

Numeric ranks data Rank = Numeric Integer | Jack | Queen | King | Ace deriving Show Main> :i Numeric Numeric :: Integer -> Rank -- data constructor Main> Numeric 3 Numeric 3 Numeric ranks contain an Integer

slide-10
SLIDE 10

Rank Beats Rank

  • When does one rank beat another?

A K Q J m n J Q K A m>n

slide-11
SLIDE 11

Rank Beats Rank

rankBeats :: Rank -> Rank -> Bool

slide-12
SLIDE 12

Rank Beats Rank

  • When does one rank beat another?

A K Q J m n J Q K A m>n

slide-13
SLIDE 13

Rank Beats Rank

rankBeats :: Rank -> Rank -> Bool

slide-14
SLIDE 14

Rank Beats Rank

  • When does one rank beat another?

A K Q J m n J Q K A m>n

slide-15
SLIDE 15

Rank Beats Rank

rankBeats :: Rank -> Rank -> Bool

slide-16
SLIDE 16

Rank Beats Rank

  • When does one rank beat another?

A K Q J m n J Q K A m>n

slide-17
SLIDE 17

Rank Beats Rank

rankBeats :: Rank -> Rank -> Bool rankBeats _ Ace = False Matches anything at all Nothing beats an Ace

slide-18
SLIDE 18

Rank Beats Rank

  • When does one rank beat another?

A K Q J m n J Q K A m>n

slide-19
SLIDE 19

Rank Beats Rank

rankBeats :: Rank -> Rank -> Bool rankBeats _ Ace = False rankBeats Ace _ = True Used only if the first equation does not match. An Ace beats anything else

slide-20
SLIDE 20

Rank Beats Rank

  • When does one rank beat another?

A K Q J m n J Q K A m>n

slide-21
SLIDE 21

Rank Beats Rank

rankBeats :: Rank -> Rank -> Bool rankBeats _ Ace = False rankBeats Ace _ = True rankBeats _ King = False rankBeats King _ = True

slide-22
SLIDE 22

Rank Beats Rank

  • When does one rank beat another?

A K Q J m n J Q K A m>n

slide-23
SLIDE 23

Rank Beats Rank

rankBeats :: Rank -> Rank -> Bool rankBeats _ Ace = False rankBeats Ace _ = True rankBeats _ King = False rankBeats King _ = True rankBeats _ Queen = False rankBeats Queen _ = True rankBeats _ Jack = False rankBeats Jack _ = True

slide-24
SLIDE 24

Rank Beats Rank

  • When does one rank beat another?

A K Q J m n J Q K A m>n

slide-25
SLIDE 25

Rank Beats Rank

rankBeats :: Rank -> Rank -> Bool rankBeats _ Ace = False rankBeats Ace _ = True rankBeats _ King = False rankBeats King _ = True rankBeats _ Queen = False rankBeats Queen _ = True rankBeats _ Jack = False rankBeats Jack _ = True rankBeats (Numeric m) (Numeric n) = m > n Match Numeric 7, for example Names the number in the rank

slide-26
SLIDE 26

Examples

Main> rankBeats Jack (Numeric 7) True Main> rankBeats (Numeric 10) Queen False

slide-27
SLIDE 27

Modelling a Card

  • A Card has both a Rank and a Suit
  • Define functions to inspect both

data Card = Card Rank Suit deriving Show rank :: Card -> Rank rank (Card r s) = r suit :: Card -> Suit suit (Card r s) = s

slide-28
SLIDE 28

When does one card beat another?

  • When both cards have the same suit, and

the rank is higher

cardBeats :: Card -> Card -> Bool cardBeats c c' | suit c == suit c' = rankBeats (rank c) (rank c') | otherwise = False data Suit = Spades | Hearts | Diamonds | Clubs deriving (Show, Eq)

can be written down simpler...

slide-29
SLIDE 29

When does one card beat another?

  • When both cards have the same suit, and

the rank is higher

cardBeats :: Card -> Card -> Bool cardBeats c c' = suit c == suit c’ && rankBeats (rank c) (rank c')

slide-30
SLIDE 30

Modelling a Hand of Cards

  • A hand may contain any number of cards

from zero up!

  • The solution is… recursion!

data Hand = Cards Card … Card deriving Show We can’t use …!!!

slide-31
SLIDE 31

Modelling a Hand of Cards

  • A hand may contain any number of cards

from zero up!

– A hand may be empty – It may consist of a first card and the rest

  • The rest is another hand of cards!

data Hand = Empty | Some Card Hand deriving Show A recursive type! Solve the problem of modelling a hand with

  • ne fewer cards!

very much like a list...

slide-32
SLIDE 32

When can a hand beat a card?

  • An empty hand beats nothing
  • A non-empty hand can beat a card if the

first card can, or the rest of the hand can!

  • A recursive function!

handBeats :: Hand -> Card -> Bool handBeats Empty card = False handBeats (Some c h) card = cardBeats c card || handBeats h card

slide-33
SLIDE 33

Discarding the first card

  • Discarding the first card:

discard :: Hand -> Hand ... discard :: Hand -> Hand discard (Some c h) = h discard :: Hand -> Hand discard (Some c h) = h discard Empty = error “no card to discard”

slide-34
SLIDE 34

What Did We Learn?

  • Modelling the problem using datatypes

with components

  • Using recursive datatypes to model things
  • f varying size
  • Using recursive functions to manipulate

recursive datatypes

slide-35
SLIDE 35

Lists

  • - how they work
slide-36
SLIDE 36

Lists

  • A list is either:

– An empty list – With at least one (first) element, and the rest

  • What to put on the place of the ??

data List = Empty | Some ?? List

slide-37
SLIDE 37

Lists

  • A type parameter
  • Some 12 (Some 3 Empty) :: List Integer
  • Some ”apa” (Some ”bepa” Empty) :: List String

data List a = Empty | Some a (List a)

slide-38
SLIDE 38

Lists

  • Empty :: List Integer
  • Empty :: List Bool
  • Empty :: List String
  • ...

data List a = Empty | Some a (List a)

slide-39
SLIDE 39

Programming Examples

  • tom :: List a -> Bool
  • forst :: List a -> a
  • sist :: List a -> a
  • size :: List a -> Integer
  • summan :: List Integer -> Integer
slide-40
SLIDE 40

Summary

  • Datatypes model data

– Constructor functions with 0, 1 or more arguments

  • Recursive datatypes

– When the size and shap can change – Recursive functions

  • Type parameter

– For generalizing over the content