如果我具有通过类型约束实现的数据类型G
,如下所示:
data Ord v => G v = G [v] (M.Map v v)
我还具有以下签名的映射功能,并且具有类型约束(省略了实现)
mapG :: (Ord v, Ord v') => (v -> v') -> G v -> G v'
然后我想将G
用作函子,所以我要执行以下操作
instance Functor G where
fmap = mapG
它不会因为抱怨而编译
• No instance for (Ord a) arising from a use of ‘mapG’ Possible fix: add (Ord a) to the context of the type signature for: fmap :: (a -> b) -> G a -> G b
所以我尝试将类型约束绑定到实例,如下所示:
instance Ord v => Functor (G v) where
fmap = mapG
但更糟糕的是,编译器抱怨:
• Expecting one fewer argument to ‘G v’ Expected kind ‘* -> *’, but ‘G v’ has kind ‘*’ • In the first argument of ‘Functor’, namely ‘G v’ In the instance declaration for ‘Functor (G v)’
我现在不知道如何进行这项工作。有人可以解释并建议我该如何解决?
备注:
Ord
的类型约束是必需的(出于与问题无关的原因),因此我无法删除它。