我正在编写一个类型类来向 Haskell 数据类型添加类型反射。它的一部分看起来像这样:
type VarName a = Text
class Reflective a where
-- | A reflective type may have a fixed name, or it may be a union
-- where each variant has a distinct name.
reflectiveName :: a -> VarName a
-- | One default value of the type for each reflective name.
reflectiveDefaults :: [a]
这个想法是,如果我写
data ExampleType = Foo Int | Bar String
然后在Reflective
实例reflectiveName
中将酌情返回“Foo”或“Bar”,reflectiveDefaults
并将返回[Foo 0, Bar ""]
所以现在我可以编写一个函数来给我所有变体的名称:
reflectiveNames :: (Reflective a) => [VarName a]
reflectiveNames = map reflectiveName reflectiveDefaults
我可以这样称呼它:
exampleNames = reflectiveNames :: [VarName ExampleType]
当我编译这个时,我在类型声明中得到以下错误reflectiveNames
:
• Could not deduce (Reflective a0)
from the context: Reflective a
bound by the type signature for:
reflectiveNames :: Reflective a => [VarName a]
The type variable ‘a0’ is ambiguous
但是,如果我用新类型替换 VarName:
newtype VarName a = VarName Text
然后它工作。
这是 Haskell 类型系统的一个特性,还是 GHC 中的一个错误?如果是前者,为什么要发明一个新的类型变量a0?