我有以下代码:
{-# LANGUAGE NoImplicitPrelude, OverloadedStrings, TypeFamilies #-}
module AI.Analysis.Rules where
import ClassyPrelude
-- Our set of rules
data RuleSet a = RuleSet [Rule a] [Rule a]
deriving (Eq)
mkRuleSet :: (Ord a) => [Rule a] -> RuleSet a
mkRuleSet rules = uncurry RuleSet (partition isStandard uniques)
where uniques = ordNub rules
isStandard x = case x of
Standard _ _ -> True
LastResort _ -> False
instance (Show a) => Show (RuleSet a) where
show (RuleSet s l) = unlines [toLines s, "----", toLines l]
where toLines = unlines . fmap show
instance (Ord a) => Monoid (RuleSet a) where
mempty = RuleSet [] []
mappend (RuleSet s1 l1) (RuleSet s2 l2) = RuleSet (ordNub (s1 ++ s2)) (ordNub (l1 ++ l2))
instance (Ord a) => Semigroup (RuleSet a) where
(<>) = mappend
type instance Element (RuleSet a) = (Rule a)
instance MonoFoldable (RuleSet a) --this is unhappy
-- A rule in our system
-- For now, we assume rules *individually* are always internally-consistent
data Rule a = Standard [a] a | LastResort a
deriving (Eq)
mkRule :: (Eq a, Ord a) => [a] -> a -> Rule a
mkRule as c = case as of
[] -> LastResort c
_ -> Standard ((sort . ordNub) as) c
-- Last-resort rules and standard rules cannot be compared for consistency
mutuallyConsistent :: (Eq a) => Rule a -> Rule a -> Maybe Bool
mutuallyConsistent (LastResort c1) (LastResort c2) = Just (c1 == c2)
mutuallyConsistent (Standard as1 c1) (Standard as2 c2) = Just ((as1 /= as2) || (c1 == c2))
mutuallyConsistent _ _ = Nothing
instance (Show a) => Show (Rule a) where
show x = case x of
Standard as c -> formatAnd as ++ " -> " ++ show c
LastResort c -> "-> " ++ show c
where formatAnd = unwords . intersperse "^" . map show . otoList
-- LastResort rules are always ordered smaller than standard ones
instance (Ord a) => Ord (Rule a) where
(<=) (LastResort _) (Standard _ _) = True
(<=) (Standard _ _) (LastResort _) = False
(<=) (LastResort c1) (LastResort c2) = c1 <= c2
(<=) (Standard as1 c1) (Standard as2 c2) = (as1 <= as2) || (c1 <= c2)
但是,我从编译器中得到以下错误,其含义我无法理解:
/home/koz/documents/uni/research/summer-research-2015/clinical/rules-analysis/src/AI/Analysis/Rules.hs:47:10:
Couldn't match type ‘a’ with ‘Rule a’
‘a’ is a rigid type variable bound by
the instance declaration
at /home/koz/documents/uni/research/summer-research-2015/clinical/rules-analysis/src/AI/Analysis/Rules.hs:47:10
Expected type: Element (RuleSet a)
Actual type: a
Relevant bindings include
ofoldMap :: (Element (RuleSet a) -> m) -> RuleSet a -> m
(bound at /home/koz/documents/uni/research/summer-research-2015/clinical/rules-analysis/src/AI/Analysis/Rules.hs:47:10)
In the expression:
mono-traversable-0.10.0.1:Data.MonoTraversable.$gdmofoldMap
In an equation for ‘ofoldMap’:
ofoldMap
= mono-traversable-0.10.0.1:Data.MonoTraversable.$gdmofoldMap
In the instance declaration for ‘MonoFoldable (RuleSet a)’
我可以说,我的想法似乎有意义 - 毕竟,RuleSet
只是Rule
的容器,应该允许可折叠性,但错误信息不是对我没有任何意义。有人可以澄清我在这里没有掌握的内容吗?
答案 0 :(得分:3)
你有没有试过实际上实现这个类?看起来默认定义和类型系列有些奇怪。如果至少定义以下内容,则文件类型将检查:
instance MonoFoldable (RuleSet a) where --this is unhappy
ofoldl1Ex' = undefined
ofoldr1Ex = undefined
ofoldl' = undefined
ofoldr = undefined
ofoldMap = undefined
编辑:优雅的前奏,我现在知道我永远不会使用,具有默认实现和包含约束t a ~ mono, a ~ Element (t a)
的类型签名。因为我必须在这里三思而后行。 t a ~ RuleSet a0
所以t == RuleSet
和a == a0
。然后a ~ Element (RuleSet a)
,这是您在邮件中的确切错误,会建议a ~ Rule a
,这是不对的。
答案 1 :(得分:3)
澄清默认实现:由于存在大量具有正确多态性的类型 - 因此Functor
- MonoFunctor
的实例提供了一种简单的方法来实现这些实例MonoFunctor
,default method signatures。如果您有Functor
,只需声明instance MonoFunctor
即可。
在您的情况下,您收到一条令人困惑的错误消息,因为您的类型实际上是Functor
,但与您所需的MonoFunctor
实例的类型不同。具体来说,根据其形状,RuleSet a
是Functor
的{{1}},而您希望它是a
。这没有任何问题,它只是与默认实现冲突,因此您需要提供单独的实现。
请注意,这不是特定于您的类型:任何不是从Rule a
到Functor
的简单翻译都需要这项工作。这适用于某些内置实例,例如MonoFunctor
和Text
。