类型族和派生实例(Eq)

时间:2016-07-28 19:47:05

标签: haskell type-families

是否可以为B a派生Eq实例,如果给予其他帮助,例如Eq a某个地方?

{-# LANGUAGE TypeFamilies #-}

class A a where
  type B a 
  somef :: a -> B a -> B a -> Bool

问题deriving instances with type familiesinstance definitions for type families已接近尾声。 以下内容不适用于type B a -line(或者只是尝试过错误)。

{-# LANGUAGE StandaloneDeriving #-}
-- deriving instance Eq (B a) -- illegal application
-- deriving instance Eq a => Eq (B a) -- illegal application

约束Eq a => A a没有帮助。将约束添加到somef编译(somef :: Eq a => ...)并适用于此方法。但是,在这种情况下,能够告诉type B a一般是等同的(这样不允许不等的B a)并且不是逐个方法,这将是很好的。

1 个答案:

答案 0 :(得分:7)

我认为这就是诀窍......

{-# LANGUAGE TypeFamilies, FlexibleContexts #-}

class Eq (B a) => A a where
  type B a 
  somef :: a -> B a -> B a -> Bool

验证它有效,接受以下实例

data HasEqInstance = HasEqInstance deriving Eq
instance A () where
  type B () = HasEqInstance
  somef = undefined

但是这个被No instance for (Eq NoEqInstance)

拒绝了
data NoEqInstance = NoEqInstance
instance A () where
  type B () = NoEqInstance
  somef = undefined
相关问题