在类型族实例上键入类约束

Mic*_*mas 14 haskell types typeclass type-families

是否可以指定必须由类型系列的所有实例满足的类型类约束?

例如,给定以下声明,我如何确保所有实例也是以下实例Eq:

data family Channel c :: *

非常感谢,

迈克尔

chi*_*chi 13

这是你想要的?

{-# 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
Run Code Online (Sandbox Code Playgroud)