自 GHC 7.8 起,Typeable
是多类的。查看内置 Typeable
实例列表 in the documentation ,我注意到一些有趣的事情:
Typeable ((* -> *) -> Constraint) Alternative
Typeable ((* -> *) -> Constraint) Applicative
Typeable (* -> Constraint) Monoid
显然,它可以查看(某些)类型的类型表示约束
:
Prelude Data.Monoid Data.Typeable> typeRep $ (Proxy :: Proxy (Monoid Int))
Monoid Int
此功能有任何用途还是只是偶然提供的?
最佳答案
嗯,现在允许使用ConstraintKind
。因此,这意味着您可以定义通过约束参数化的数据类型。
一个(人为的)示例:
data CPair (c :: * -> Constraint) where
MkCPair :: (c a, c b) => a -> b -> CPair c
这是一对可能不同类型的两个组件,它们共享一个公共(public)类:
aPair :: CPair Show
aPair = MkCPair 'x' True
现在,我们是否希望aPair
能够可输入
?这需要 CPair
和 Show
来
也可以可输入
。
deriving instance Typeable CPair
deriving instance Typeable Show
现在:
GHCi> typeOf aPair
CPair Show
因此,如果类现在可以显示为类型,则只能为类派生 Typeable
。
有趣的是,Typeable Show
不是预定义的,但 Typeable Applicative
是预定义的。这是因为有一个新的 GHC 扩展 AutoDeriveTypeable
,它似乎在某些模块中启用,但在其他模块中未启用。使用 AutoDeriveTypeable
,可以为模块中的所有内容(包括类)派生出 Typeable
实例。
关于haskell - `Typeable (* -> Constraint) Monoid`实例的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23277838/