在haskell中,s k v ~ s1 k1 v1
,其中s :: * -> * -> *
是否意味着k ~ k1
,v ~ v1
或s ~ s1
?如果没有,为什么不呢?
我在编写一些实验代码时遇到了这个问题,其中一小部分是:
newtype Article = Article String
newtype ArticleId = ArticleId Int
newtype Comment = Comment String
newtype CommentId = CommentId Int
data TableName k v where
Articles :: TableName ArticleId Article
Comments :: TableName CommentId Comment
data CRUD k v r where
Create :: v -> CRUD k v k
Read :: k -> CRUD k v (Maybe v)
data Operation t r where
Operation :: s k v -> CRUD k v r -> Operation (s k v) r
operatesOn :: (Eq (s k v)) => s k v -> Operation (s k v) r -> Bool
operatesOn tableName1 (Operation tableName2 _) = tableName1 == tableName2
由于以下错误而无法编译:
Could not deduce (v1 ~ v)
from the context (Eq (s k v))
bound by the type signature for
operatesOn :: Eq (s k v) => s k v -> Operation (s k v) r -> Bool
or from (s k v ~ s1 k1 v1)
bound by a pattern with constructor
Operation :: forall r (s :: * -> * -> *) k v.
s k v -> CRUD k v r -> Operation (s k v) r,
in an equation for `operatesOn'
`v1' is a rigid type variable bound by
a pattern with constructor
Operation :: forall r (s :: * -> * -> *) k v.
s k v -> CRUD k v r -> Operation (s k v) r,
in an equation for `operatesOn'
at src\Example\Error.hs:44:24
`v' is a rigid type variable bound by
the type signature for
operatesOn :: Eq (s k v) => s k v -> Operation (s k v) r -> Bool
at src\Example\Error.hs:43:15
Expected type: s k v
Actual type: s1 k1 v1
In the second argument of `(==)', namely `tableName2'
In the expression: tableName1 == tableName2
In an equation for `operatesOn':
operatesOn tableName1 (Operation tableName2 _)
= tableName1 == tableName2
答案 0 :(得分:1)
如果我将操作的定义更改为
data Operation t r where
Operation :: (t ~ s k v) => t -> CRUD k v r -> Operation t r
operateOn函数将编译,类似于它对Maybe
的处理方式 (t ~ s k v) =>
约束(?)仍将拒绝错误的程序,如:
doesntCompile = Operation Article $ Create $ Comment "Yo"