Haskell data types with type variables

tan*_*nvi 1 haskell ghci

I have been going through tutorial examples for data types and I have defined the following:

ghci> data Animal a b = Cat a | Dog b | Rat
ghci> data BreedOfCat = Siamese | Persian | Moggie
ghci> :t Cat Siamese
Cat Siamese :: Animal BreedOfCat b
ghci> :t Cat Persian
Cat Persian :: Animal BreedOfCat b
ghci> :t Dog 12
Dog 12 :: Num b => Animal a b
ghci> :t Dog "Fifo"
Dog "Fifo" :: Animal a String
ghci> :t Rat
Rat :: Animal a b
Run Code Online (Sandbox Code Playgroud)

如果Dog "Fifo"给出Animal a String,为什么不Dog 12给出Animal a Integer(这是教程所说的应该给出的)

Wil*_*sem 5

基本上是这样。它只是不为 选取特定类型12,这可以是属于类型类成员的任何Num类型,因此. 如果你强制选择一个for ,它确实会:Num b => Animal a bInteger12

ghci> :t Dog (12 :: Integer)
Animal a Integer
Run Code Online (Sandbox Code Playgroud)