如何创建提升类型的类型类实例?

Che*_*tan 6 haskell typeclass

我有一个数据类型,我通过ghc 7.4.1中的DataKinds和我想用来进行类型特定操作的给定类型类提升.

data Type = TInt32 | TInt64 | TInt16
class TypeTraits a where
  ...
Run Code Online (Sandbox Code Playgroud)

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

instance TypeTraits TInt32 where
  ...
Run Code Online (Sandbox Code Playgroud)

我收到以下类型的错误:

Kind mis-match
The first argument of `TypeTraits' should have kind `*',
but `TInt32' has kind `Type'
In the instance declaration for `TypeTraits TInt32'
Run Code Online (Sandbox Code Playgroud)

尝试通过指定'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'
Run Code Online (Sandbox Code Playgroud)

ehi*_*ird 7

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

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