Вы находитесь на странице: 1из 22

Programare declarativa

Tipuri de date algebrice

, a
Traian Florin S, erbanut
FMI, UNIBUC
Departamentul de Informatica,
traian.serbanuta@fmi.unibuc.ro

30 octombrie 2015

,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

1 / 22

Ce este un tip de date algebric?

Orice!
data
data
data
data
data
data
data
data
data
data

Bool = False | True


Season = W i n t e r | S p r i n g | Summer | F a l l
Shape = C i r c l e F l o a t | Rectangle F l o a t F l o a t
L i s t a = N i l | Cons a ( L i s t a )
Nat = Zero | Succ Nat
Exp = L i t I n t | Add Exp Exp | Mul Exp Exp
Tree a = Empty | Leaf a | Branch ( Tree a ) ( Tree a )
Maybe a = Nothing | Just a
Pair a b = Pair a b
E i t h e r a b = L e f t a | Right b

,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

2 / 22

Tipul de date Boolean

Tipul de date Boolean

data Bool = False | True


not : : Bool > Bool
not False = True
not True
= False
(&&) : : Bool > Bool > Bool
False && q
= False
True && q
= q
( | | ) : : Bool > Bool > Bool
False | | q
= q
True | | q
= True

,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

3 / 22

Tipul de date Boolean

, ii s, i a reprezentarii

Definirea egalitat
Eq s, i Show

Eq
eqBool
eqBool
eqBool
eqBool

: : Bool > Bool > Bool


False False = True
True True = True
_
_
= False

Show
showBool : : Bool > S t r i n g
showBool False = " False "
showBool True = " True "

,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

4 / 22

Anotimpuri

Anotimpuri

data Season = S p r i n g | Summer | Autumn | W i n t e r


next
next
next
next
next

: : Season > Season


S p r i n g = Summer
Summer = Autumn
Autumn = W i n t e r
Winter = Spring

,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

5 / 22

Anotimpuri

, ii s, i a reprezentarii

Definirea egalitat
Eq s, i Show

eqSeason
eqSeason
eqSeason
eqSeason
eqSeason
eqSeason

: : Season > Season > Bool


S p r i n g S p r i n g = True
Summer Summer = True
Autumn Autumn = True
W i n t e r W i n t e r = True
_
_
= False

showSeason
showSeason
showSeason
showSeason
showSeason

: : Season > S t r i n g
Spring = " Spring "
Summer = "Summer"
Autumn = " Autumn "
Winter = " Winter "

,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

6 / 22

Anotimpuri

s, i indici
Enumerari
data Season = W i n t e r | S p r i n g | Summer | F a l l
t o I n t : : Season > I n t
t o I n t Winter = 0
t o I n t Spring = 1
t o I n t Summer = 2
toInt Fall
= 3
fromInt
fromInt
fromInt
fromInt
fromInt

: : I n t > Season
0 = Winter
1 = Spring
2 = Summer
3 = Fall

next : : Season > Season


next x = fromInt ( ( t o I n t x + 1 ) mod 4 )
eqSeason : : Season > Season > Bool
eqSeason x y = ( t o I n t x == t o I n t y )
,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

7 / 22

Forme geometrice

Cercuri s, i dreptunghiuri

type
type
type

Radius
Width
Height

=
=
=

Float
Float
Float

data

Shape

=
|

C i r c l e Radius
Rect Width H e i g h t

area : : Shape > F l o a t


area ( C i r c l e r ) = p i * r ^2
area ( Rect w h ) = w * h

,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

8 / 22

Forme geometrice

, ii s, i a reprezentarii

Definirea egalitat
Eq s, i Show

eqShape
eqShape
eqShape
eqShape

: : Shape > Shape > Bool


( C i r c l e r ) ( C i r c l e r ) = ( r == r )
( Rect w h ) ( Rect w h ) = (w == w ) && ( h == h )
_
_
= False

showShape : : Shape > S t r i n g


showShape ( C i r c l e r ) = " C i r c l e " ++ showF r
showShape ( Rect w h ) = " Rect " ++ showF w
++ " " ++ showF h
showF : : F l o a t > S t r i n g
showF x | x >= 0
= show x
| otherwise = " ( " ++ show x ++ " ) "

,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

9 / 22

Forme geometrice

Teste s, i operatori de proiect, ie


i s C i r c l e : : Shape > Bool
i s C i r c l e ( C i r c l e r ) = True
isCircle _
= False
i s R e c t : : Shape > Bool
i s R e c t ( Rect w h ) = True
isRect _
= False
r a d i u s : : Shape > F l o a t
radius ( Circle r ) = r
w i d t h : : Shape > F l o a t
w i d t h ( Rect w h ) = w
h e i g h t : : Shape > F l o a t
h e i g h t ( Rect w h ) = h
,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

10 / 22

Forme geometrice

Pattern-matching
area : : Shape > F l o a t
area ( C i r c l e r ) = p i * r ^2
area ( Rect w h ) = w * h
area : : Shape > F l o a t
area s =
i f i s C i r c l e s then
let
r = radius s
in
p i * r ^2
else i f i s R e c t s then
let
w = width s
h = height s
in
w * h
else e r r o r " i m p o s s i b l e "
,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

11 / 22

Liste

Pattern-matching

Declarat, ie ca tip de date algebric


data

List a

= Nil
| Cons a ( L i s t a )

append : : L i s t a > L i s t a > L i s t a


append N i l ys
= ys
append ( Cons x xs ) ys = Cons x ( append xs ys )

,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

12 / 22

Liste

Constructori simboluri
Declarat, ie ca tip de date algebric cu simboluri
= Nil
| a : : : List a
d e r i v i n g (Show)
infixr 5 : : :

data

List a

(+++) : : L i s t a > L i s t a > L i s t a


i n f i x r 5 +++
N i l +++ ys
= ys
( x : : : xs ) +++ ys = x : : : ( xs +++ ys )

Comparat, i cu versiunea folosind notat, ia predefinita


( + + ) : : [ a ] > [ a ] > [ a ]
[ ] ++ ys
= ys
( x : xs ) ++ ys = x : ( xs ++ ys )
,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

13 / 22

Liste

, ii s, i a reprezentarii

Definirea egalitat
Eq s, i Show

eqList
eqList
eqList
eqList

: : Eq a => L i s t a > L i s t a > Bool


N i l N i l = True
( x : : : xs ) ( y : : : ys ) = x == y && e q L i s t xs ys
_
_
= False

showList : : Show a => L i s t a > S t r i n g


showList N i l = " N i l "
showList ( x : : : xs ) = show x ++ " : : : " ++ showList xs

,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

14 / 22

Numere naturale

Numerele Naturale (Peano)


Declarat, ie ca tip de date algebric
data

Nat

=
|

Zero
Succ Nat

( ^ ^ ^ ) : : F l o a t > Nat > F l o a t


x ^^^ Zero
= 1.0
x ^^^ ( Succ n ) = x * x ^^^ n

Comparat, i cu versiunea folosind notat, ia predefinita


( ^ ^ ) : : F l o a t > I n t > F l o a t
x ^^ 0 = 1 . 0
x ^^ n = x * ( x ^^ ( n 1) )

,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

15 / 22

Numere naturale

Adunare s, i nmult, ire


Definit, ie pe tipul de date algebric
(+++) : : Nat > Nat > Nat
m +++ Zero
= m
m +++ ( Succ n ) = Succ (m +++ n )
( * * * ) : : Nat > Nat > Nat
m * * * Zero
= Zero
m * * * ( Succ n ) = (m * * * n ) +++ m

Comparat, i cu versiunea folosind notat, ia predefinita


( + ) : : I n t > I n t > I n t
m + 0 = m
m + n = (m + ( n 1) ) + 1
( * ) : : I n t > I n t > I n t
m * 0 = 0
m * n = (m * ( n 1) ) + m
,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

16 / 22

Numere naturale

Date personale

type
type
type
type
type
type

FirstName = S t r i n g
LastName
= String
Age
= Int
Height
= Float
PhoneNumber = S t r i n g
Flavor
= String

data Person = Person FirstName LastName Age H e i g h t


PhoneNumber F l a v o r

,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

17 / 22

Numere naturale

Proiect, ii
f i r s t N a m e : : Person > S t r i n g
f i r s t N a m e ( Person f i r s t n a m e _ _ _ _ _ ) = f i r s t n a m e
lastName : : Person > S t r i n g
lastName ( Person _ lastname _ _ _ _ ) = lastname
age : : Person > I n t
age ( Person _ _ age _ _ _ ) = age
h e i g h t : : Person > F l o a t
h e i g h t ( Person _ _ _ h e i g h t _ _ ) = h e i g h t
phoneNumber : : Person > S t r i n g
phoneNumber ( Person _ _ _ _ number _ ) = number
f l a v o r : : Person > S t r i n g
f l a v o r ( Person _ _ _ _ _ f l a v o r ) = f l a v o r
,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

18 / 22

Numere naturale

Utilizare

Main * > l e t i o n e l = Person " I o n " " Ionescu " 20 175.2


" 0712334567 " " Caramel "
Main * > f i r s t N a m e i o n e l
" Ion "
Main * > h e i g h t i o n e l
175.2
Main * > f l a v o r i o n e l
" Caramel "

,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

19 / 22


nregistrari

Date personale ca nregistrari

data Person = Person {


,
,
,
,
,
}

,a (UNIBUC)
Traian Florin S, erbanut

firstName : : String
lastName : : S t r i n g
age : : I n t
height : : Float
phoneNumber : : S t r i n g
f l a v o r : : String

PDTipuri date algebrice

30 octombrie 2015

20 / 22


nregistrari

Utilizare
Putem folosi att forma algebrica ct s, i cea de nregistrare
i o n e l = Person " I o n " " Ionescu " 20 175.2
" 0712334567 " " Caramel "
g i g e l = Person {
,
,
,
,

f i r s t N a m e = " Gheorghe "


lastName= " Georgescu "
age = 30 , h e i g h t = 192.3
phoneNumber = " 0798765432 "
flavor = " Vanilie " }

Putem folosi s, i pattern-matching

Proiect, iile sunt definite automat; sintaxa specializata pentru actualizari


nextYear : : Person > Person
nextYear person = person { age = age person + 1 }

,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

21 / 22


nregistrari

De ce algebric?

,a (UNIBUC)
Traian Florin S, erbanut

PDTipuri date algebrice

30 octombrie 2015

22 / 22

Вам также может понравиться