下面给出的代码编译,好的。
data Car p q r = Car {company :: p
, model :: q
, year ::r
} deriving (Show)
tellCar :: (Show a) => Car String String a -> String
有哪些基本原则/约定/逻辑可以提醒我只需要在“tellCar”中使用“Show a”,而不是任何其他选项?我在哪里可以找到资源来学习这些原则/约定/逻辑?
如果我在 tellCar 中错误地使用了“Show Car”,编译时会收到以下错误消息:
*Main> :load "/home/optimight/baby.hs"
[1 of 1] Compiling Main ( /home/optimight/baby.hs, interpreted )
/home/optimight/baby.hs:96:18:
Expecting three more arguments to `Car'
In the type signature for `tellCar':
tellCar :: Show Car => Car String String a -> String
Failed, modules loaded: none.
如果我在 tellCar 中错误地使用了“Show z”,编译时会收到以下错误消息:
*Main> :load "/home/optimight/baby.hs"
[1 of 1] Compiling Main ( /home/optimight/baby.hs, interpreted )
/home/optimight/baby.hs:96:1:
Ambiguous constraint `Show z'
At least one of the forall'd type variables mentioned by the constraint
must be reachable from the type after the '=>'
In the type signature for `tellCar':
tellCar :: Show z => Car String String a -> String
Failed, modules loaded: none.
如果我在 tellCar 中错误地使用了“显示字符串”,编译时会收到以下错误消息:
Prelude> :load "/home/optimight/baby.hs"
[1 of 1] Compiling Main ( /home/optimight/baby.hs, interpreted )
/home/optimight/baby.hs:96:1:
Non type-variable argument in the constraint: Show String
(Use -XFlexibleContexts to permit this)
In the type signature for `tellCar':
tellCar :: Show String => Car String String a -> String
Failed, modules loaded: none.