是否可以为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 families和instance 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)并且不是逐个方法,这将是很好的。
答案 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