【发布时间】:2018-11-06 11:53:25
【问题描述】:
我有以下代码
{-# LANGUAGE PolyKinds, DefaultSignatures, FlexibleContexts, DeriveAnyClass, DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, UndecidableInstances #-}
module DeriveTest where
import GHC.Generics
class GenericClass a m where
instance GenericClass f m => GenericClass (M1 i c f) m
instance Condition a m => GenericClass (K1 i a) m
class Condition (a :: k) (m :: * -> *) where
instance (Condition a m, Condition b m) => Condition (a b) m
instance {-# OVERLAPPABLE #-} Condition (a :: k) m
class Class (e :: (* -> *) -> *) where
classF :: e m -> ()
default classF :: GenericClass (Rep (e m)) m => e m -> ()
classF = undefined
它定义了具有更高种类的类型的类Class作为参数。它还定义了派生该类实例的通用方法。现在,如果我像这样声明一个新的数据类型,并尝试派生一个 Class 的实例
data T a m = T
{ field :: a }
deriving (Generic, Class)
我收到以下错误:
* Overlapping instances for Condition a m
arising from the 'deriving' clause of a data type declaration
Matching instances:
instance [overlappable] forall k (a :: k) (m :: * -> *).
Condition a m
instance forall k1 k2 (a :: k1 -> k2) (m :: * -> *) (b :: k1).
(Condition a m, Condition b m) =>
Condition (a b) m
(The choice depends on the instantiation of `a, m'
To pick the first instance above, use IncoherentInstances
when compiling the other instance declarations)
* When deriving the instance for (Class (T a))
|
22 | deriving (Generic, Class)
| ^^^^^
这个错误有点道理,因为我猜。该实例确实依赖于a 的实例化。但是,如果我只是这样写一个空实例:
data T a m = T
{ field :: a }
deriving (Generic)
instance Class (T a) -- works
它有效。为什么?以及如何使它与派生语句的行为相同?
这是使用 GHC 8.2.2
【问题讨论】:
-
@dfeuer 好的,做到了。
标签: haskell