Type class constraint on type family instances

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


这是你想要的?

{-# 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
链接地址: http://www.djcxy.com/p/43138.html

上一篇: 具有类型约束的Haskell类型系列实例

下一篇: 在类型族实例上输入类约束