Is it possible to specify a type class constraint that must be satisfied by all instances of a type family?
For example, given the following declaration, how would I ensure that all instances are also instances of Eq
:
data family Channel c :: *
Many thanks,
Michael
Is this what you are looking for?
{-# LANGUAGE FlexibleContexts, TypeFamilies, FlexibleInstances #-}
-- Data family inside a class so that we can add an extra Eq constraint
class Eq (Channel c) => MyClass c where
data Channel c :: *
-- A simple toy instance
instance MyClass Int where
data Channel Int = CI Int deriving Eq
-- A more complex instance with separate Eq instance
instance MyClass Char where
data Channel Char = CC Char
instance Eq (Channel Char) where
(CC x) == (CC y) = x == y
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With