DataKinds扩展将“值”(即构造函数)提升为类型。比如True
和False
变成不同种类的那种Bool
。
我想做的是相反的,即将类型降级为值。具有此签名的函数会很好:
demote :: Proxy (a :: t) -> t
我实际上可以这样做,例如Bool
:
class DemoteBool (a :: Bool) where
demoteBool :: Proxy (a :: Bool) -> Bool
instance DemoteBool True where
demoteBool _ = True
instance DemoteBool False where
demoteBool _ = False
但是,我必须为我想降级为它的值的任何类型编写实例。有没有更好的方法来做这不涉及这么多样板?