【发布时间】:2013-05-04 08:00:54
【问题描述】:
我在 Haskell 中有一个类型,可以让 Map 有多个与键关联的值。
如果我编译以下代码:
type Mapa k v = Map k [v]
instance Monoid (Mapa k v) where
--mempty :: Mapa k v
mempty = DM.empty
--mappend :: Mapa k v -> Mapa k v -> Mapa k v
mappend a b = DM.unionWith (++) a b
GHCi 会抛出:
Illegal instance declaration for `Monoid (Map k [v])'
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use -XFlexibleInstances if you want to disable this.)
In the instance declaration for `Monoid (Map k [v])'
Mapa 应该是newtype 还是data;
或者有什么问题?
【问题讨论】: