1

次のコードがあります。

{-# 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)’

私が言うことができる限りでは、私の考えは理にかなっているようです.結局のところ、 aRuleSetは s の単なるコンテナでRuleあり、折りたたみ可能であるべきですが、問題のエラーメッセージは私には意味がありません. 誰かが私がここで把握できなかったものを明確にしてもらえますか?

4

2 に答える 2

3

デフォルトの実装について明確にするために: 適切にポリモーフィックな型が多数あり、そのため のインスタンスが存在するため、デフォルトのメソッド シグネチャを使用して、それらを のインスタンスにする簡単な方法を提供しFunctorます。がある場合は、宣言するだけで十分です。MonoFunctorMonoFunctorFunctorinstance MonoFunctor

あなたの場合、あなたのタイプは実際には aですが、目的のインスタンスFunctorとは異なるタイプであるため、紛らわしいエラーメッセージが表示されます。具体的にはMonoFunctor、その形状により、になりたいのですが、 です。デフォルトの実装と競合するだけなので、別の実装を提供する必要があります。RuleSet aFunctoraRule a

これはあなたのタイプに固有のものではないことに注意してください: からFunctorへの単純な変換でないものはすべて、MonoFunctorこの作業を必要とします。Textこれは、やなどの組み込みインスタンスの一部に適用されますByteString

于 2016-01-13T09:04:12.420 に答える