如果您打算Reifies
在包装器中复制方法,那么您可以编写如下内容:
class R s a | s -> a where
r :: s -> a
-- etc.
class R s a => RW s a where
type RT s
r' :: a ~ RT s => s -> RT s
r' = r
-- etc.
instance R Bool String where
r = show
-- etc.
instance RW Bool String where
type RT Bool = String
-- no need to write r' = r, etc.
-- > :t r
-- r :: R s a => s -> a
-- > :t r'
-- r' :: RW s (RT s) => s -> RT s
-- > :t r True
-- r True :: [Char]
-- > :t r' True
-- r' True :: RT Bool
-- > r True
-- "True"
-- > r' True
-- "True"
a
因此和之间的关联RT s
可以表示为等式约束。
更新:对于具有类型族的 1 参数包装器,超过具有 Fundep 的 2 参数类:
class R s a | s -> a where
r :: s -> a
class R' s where
type RT s a
r' :: (R s a, a ~ RT s a) => s -> RT s a
r' = r
instance R Bool String where
r = show
instance R' Bool where
type RT Bool a = a
以便:
> :t r
r :: R s a => s -> a
> :t r'
r' :: (R' s, R s a, RT s a ~ a) => s -> RT s a
> :t r True
r True :: [Char]
> :t r' True
r' True :: RT Bool [Char]
> r True
"True"
> r' True
"True"
例如:
import qualified Data.Reflection as Reflection ( reflect )
import Data.Reflection hiding ( reflect )
class Reified s where
type ReifiedType s a
reflect :: (Reifies s a, a ~ ReifiedType s a) => proxy s -> ReifiedType s a
reflect = Reflection.reflect