6

我有一个通过 ghc 7.4.1 中的 DataKinds 推广的数据类型和一个我想用来执行类型特定操作的给定类型类。

data Type = TInt32 | TInt64 | TInt16
class TypeTraits a where
  ...

然后我尝试创建提升类型的类型类实例,如下所示:

instance TypeTraits TInt32 where
  ...

我收到以下类型的错误:

Kind mis-match
The first argument of `TypeTraits' should have kind `*',
but `TInt32' has kind `Type'
In the instance declaration for `TypeTraits TInt32'

试图通过指定“a”的类型来解决这个问题:

class TypeTraits (a :: Type) where
  ...

Kind mis-match
Expected kind `ArgKind', but `a' has kind `Type'
In the type `a -> String'
In the class declaration for `TypeTraits'
4

1 回答 1

7

问题出在班级的主体上;具有提升类型的类型没有任何值,因此您不能拥有将一个作为参数的函数。你必须使用Proxy a -> String或类似的。

顺便说一句,如果您打开PolyKinds扩展程序,那么您应该能够完全省略 kind 注释。(实际上,您可能必须这样做,才能定义自己的Proxy类型,因为我认为 Data.Proxy 中的那个可能是* -> *,而您需要Type -> *。如果您data Proxy p = Proxy使用PolyKindson 定义,那么它将被推断为AnyK -> *。)

于 2012-04-23T19:12:06.937 回答