Hierarchical data types

I would like to define data types hierarchically, for example:

data Cat = BigCat | SmallCat
data Animal = Cat | Dog

And then write a function that will take Animals as arguments, and write it using a template like this:

bigger::Animal -> Animal -> Bool
bigger SmallCat BigCat = False
bigger BigCat SmallCat = True
bigger Dog Cat = True
bigger Cat Dog = False

The compiler complains. He does not want to match the type Animalexplicitly written in the function signature, by type Catin the first and second lines of pattern matching. Why doesn't the hackell recognize that a big cat or a small cat is an animal?

+5
source share
2 answers

. - , . - , . data Animal = Cat | Dog Animal Cat Dog. Cat. , .

Cat, Animal ( Cat), :

data Animal = Cat Cat | Dog

: "Animal - , , Cat Dog. Cat Cat Dog ". Cat, Cat:

myCat = Cat BigCat

Animal, . :

data Cat = BigCat | SmallCat
data Animal = Cat Cat | Dog

bigger :: Animal -> Animal -> Bool
bigger (Cat SmallCat) (Cat BigCat)   = False
bigger (Cat BigCat)   (Cat SmallCat) = True
bigger Dog            (Cat _)        = True
bigger (Cat _)         Dog           = False

_ , - , .

+18

, Animal , Cat: Cat Animal, BigCat Cat.

, Cat :

data Cat = BigCat | SmallCat
data Animal = Cat Cat | Dog

- :

bigger (Cat SmallCat) (Cat BigCat) = False
bigger (Cat BigCat) (Cat SmallCat) = True
bigger Dog (Cat _) = True
bigger (Cat _) Dog = False

, , Cat , Cat . , - :

data Size = Big | Small
data Species = Cat | Dog
data Animal = Animal Species Size

, , .

, , , , , , , . , , deriving Ord, Enum, & c. . , , .

+12

All Articles