具有关联数据族C
的类型类X
需要以下coerceX
函数:
class C t where
data X t :: * -> *
coerceX :: Coercion a b -> Coercion (X t a) (X t b)
如果我实现如下所示的类型类,如何编写coerceX
?
instance C (T r) where
newtype X (T r) a = X (Reader r a)
coerceX c = ...
您可以与构造函数Coercion
进行模式匹配,并对新值使用相同的构造函数。
此编译:
{-# LANGUAGE TypeFamilies #-}
import Data.Type.Coercion
import Control.Monad.Trans.Reader
class C t where
data X t :: * -> *
coerceX :: Coercion a b -> Coercion (X t a) (X t b)
data T r = Unused
instance C (T r) where
newtype X (T r) a = X (Reader r a)
coerceX Coercion = Coercion