Is it possible to derive Eq instance for the B a
, if given additional help, e.g. Eq a
somewhere?
{-# LANGUAGE TypeFamilies #-}
class A a where
type B a
somef :: a -> B a -> B a -> Bool
Question deriving instances with type families and instance definitions for type families are close.
The following don't work nor similar modifications on type B a
-line (or just tried the wrong ones).
{-# LANGUAGE StandaloneDeriving #-}
-- deriving instance Eq (B a) -- illegal application
-- deriving instance Eq a => Eq (B a) -- illegal application
Constraining Eq a => A a
didn't help. Adding constraint to somef
compiles (somef :: Eq a => ...
) and works for this method. However, in this case it would be nice to be able to tell that the type B a
is equatable in general (such that non-equatable B a's would not be allowed) and not method-by-method.
I think this does the trick...
{-# LANGUAGE TypeFamilies, FlexibleContexts #-}
class Eq (B a) => A a where
type B a
somef :: a -> B a -> B a -> Bool
As verification that it works, the following instance is accepted
data HasEqInstance = HasEqInstance deriving Eq
instance A () where
type B () = HasEqInstance
somef = undefined
But this one is rejected with No instance for (Eq NoEqInstance)
data NoEqInstance = NoEqInstance
instance A () where
type B () = NoEqInstance
somef = undefined