Type class constraint on type family instances

前端 未结 1 970
既然无缘
既然无缘 2021-02-07 10:48

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 ens

1条回答
  •  迷失自我
    2021-02-07 11:33

    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
    

    0 讨论(0)
提交回复
热议问题