modelling & datatypes
DESCRIPTION
Koen Lindström Claessen. Modelling & Datatypes. 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 defining new types. Modelling a Card Game. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/1.jpg)
Modelling & Datatypes
Koen Lindström Claessen
![Page 2: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/2.jpg)
Software
Software = Programs + Data
![Page 3: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/3.jpg)
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
![Page 4: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/4.jpg)
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 of this type
Hearts, Whist, Plump,
Bridge, ...
![Page 5: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/5.jpg)
Investigating the new type
Main> :i Suit-- type constructordata Suit
-- constructors:Spades :: SuitHearts :: SuitDiamonds :: SuitClubs :: Suit
Main> :i SpadesSpades :: Suit -- data constructor
The new type
The new values-- constructors
Types and constructors start with a capital letter
![Page 6: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/6.jpg)
Printing Values
• Fix
Main> SpadesERROR - Cannot find "show" function for:*** Expression : Spades*** Of type : Suit
Main> :i showshow :: Show a => a -> String -- class member
Needed to print values
data Suit = Spades | Hearts | Diamonds | Clubs deriving Show
Main> SpadesSpades
![Page 7: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/7.jpg)
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 -> Colourcolour Spades = Blackcolour Hearts = Redcolour Diamonds = Redcolour Clubs = Black
One equation per value
Main> colour HeartsRed
![Page 8: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/8.jpg)
The Ranks of Cards
• Cards have ranks: 2..10, J, Q, K, A
• Model by a new typeNumeric ranks
data Rank = Numeric Integer | Jack | Queen | King | Ace deriving Show
Main> :i NumericNumeric :: Integer -> Rank -- data constructor Main> Numeric 3Numeric 3
Numeric ranks contain an Integer
![Page 9: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/9.jpg)
Rank Beats Rank
• When does one rank beat another?
A
K
Q
J
m
n J Q K A
m>n
![Page 10: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/10.jpg)
Rank Beats Rank
rankBeats :: Rank -> Rank -> Bool
![Page 11: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/11.jpg)
Rank Beats Rank
• When does one rank beat another?
A
K
Q
J
m
n J Q K A
m>n
![Page 12: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/12.jpg)
Rank Beats Rank
• When does one rank beat another?
A
K
Q
J
m
n J Q K A
m>n
![Page 13: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/13.jpg)
Rank Beats Rank
rankBeats :: Rank -> Rank -> BoolrankBeats _ Ace = False
Matches anything at all
Nothing beats an Ace
![Page 14: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/14.jpg)
Rank Beats Rank
• When does one rank beat another?
A
K
Q
J
m
n J Q K A
m>n
![Page 15: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/15.jpg)
Rank Beats Rank
rankBeats :: Rank -> Rank -> BoolrankBeats _ Ace = FalserankBeats Ace _ = True
Used only if the first equation does not
match.
An Ace beats anything else
![Page 16: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/16.jpg)
Rank Beats Rank
• When does one rank beat another?
A
K
Q
J
m
n J Q K A
m>n
![Page 17: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/17.jpg)
Rank Beats Rank
rankBeats :: Rank -> Rank -> BoolrankBeats _ Ace = FalserankBeats Ace _ = TruerankBeats _ King = FalserankBeats King _ = True
![Page 18: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/18.jpg)
Rank Beats Rank
• When does one rank beat another?
A
K
Q
J
m
n J Q K A
m>n
![Page 19: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/19.jpg)
Rank Beats Rank
rankBeats :: Rank -> Rank -> BoolrankBeats _ Ace = FalserankBeats Ace _ = TruerankBeats _ King = FalserankBeats King _ = TruerankBeats _ Queen = FalserankBeats Queen _ = TruerankBeats _ Jack = FalserankBeats Jack _ = True
![Page 20: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/20.jpg)
Rank Beats Rank
• When does one rank beat another?
A
K
Q
J
m
n J Q K A
m>n
![Page 21: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/21.jpg)
Rank Beats Rank
rankBeats :: Rank -> Rank -> BoolrankBeats _ Ace = FalserankBeats Ace _ = TruerankBeats _ King = FalserankBeats King _ = TruerankBeats _ Queen = FalserankBeats Queen _ = TruerankBeats _ Jack = FalserankBeats Jack _ = TruerankBeats (Numeric m) (Numeric n) = m > n
Match Numeric 7, for example
Names the number in the rank
![Page 22: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/22.jpg)
Examples
Main> rankBeats Jack (Numeric 7)TrueMain> rankBeats (Numeric 10) QueenFalse
![Page 23: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/23.jpg)
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 -> Rankrank (Card r s) = r
suit :: Card -> Suitsuit (Card r s) = s
![Page 24: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/24.jpg)
A Useful Abbreviation
• Define type and inspection functions together, as follows
data Card = Card {rank :: Rank, suit :: Suit} deriving Show
![Page 25: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/25.jpg)
When does one card beat another?
• When both cards have the same suit, and the rank is higher
cardBeats :: Card -> Card -> BoolcardBeats 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...
![Page 26: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/26.jpg)
When does one card beat another?
• When both cards have the same suit, and the rank is higher
cardBeats :: Card -> Card -> BoolcardBeats c c' = suit c == suit c’ && rankBeats (rank c) (rank c')
![Page 27: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/27.jpg)
Intermezzo: Figures
• Modelling geometrical figures– triangle– rectangle– circle
data Figure = Triangle ... | Rectangle ... | Circle ...
circumference :: Figure -> Doublecircumference = ...
![Page 28: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/28.jpg)
Intermezzo: Figures
data Figure = Triangle Double Double Double | Rectangle Double Double | Circle Double
circumference :: Figure -> Doublecircumference (Triangle a b c) = a + b + ccircumference (Rectangle x y) = 2* (x + y)circumference (Circle r) = 2 * pi * r
![Page 29: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/29.jpg)
Intermezzo: Figures
data Figure = Triangle Double Double Double | Rectangle Double Double | Circle Double
-- typesTriangle :: Double -> Double -> Double -> FigureRectangle :: Double -> Double -> FigureCircle :: Double -> Figure
square :: Double -> Figuresquare s = Rectangle s s
![Page 30: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/30.jpg)
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 …!!!
![Page 31: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/31.jpg)
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 | Add Card Hand deriving Show
A recursive type!Solve the problem of modelling a hand with
one fewer cards!
very much like a list...
![Page 32: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/32.jpg)
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 -> BoolhandBeats Empty card = FalsehandBeats (Add c h) card = cardBeats c card || handBeats h card
![Page 33: Modelling & Datatypes](https://reader035.vdocuments.net/reader035/viewer/2022062517/56813e81550346895da8ad4f/html5/thumbnails/33.jpg)
What Did We Learn?
• Modelling the problem using datatypes with components
• Using recursive datatypes to model things of varying size
• Using recursive functions to manipulate recursive datatypes
• Writing properties of more complex algorithms