新型阅读器monad的强制,为其值类型指定了强制

问题描述 投票:2回答:1

具有关联数据族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 = ...
haskell coercion type-level-computation
1个回答
0
投票

您可以与构造函数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
© www.soinside.com 2019 - 2024. All rights reserved.