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

问题描述:

我有一个数据类型,我通过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' 
+0

你究竟能把什么放到'''区域? 'TInt32'不是一个有人居住的类型,所以它不能在价值位置使用'a'。我猜像'crazyThing :: TypeTraits a => SomeTypeConstructor a'?我很难想象这样的事情可能是有用的,但如果我真的伸展,我可以想象它有用*不知何故*。也许。 – 2012-04-24 04:33:21

+0

这个想法是有每个实例的特征。所以我没有在寻找值,而只是一个占位符来指定类型类型,因此我想在那里选择函数。 例如:class TypeTraits(a :: Type)其中type HType a sizeOf :: Proxy a - > Int' – Chetan 2012-04-24 15:51:48

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

顺便说一句,如果你打开PolyKinds扩展,那么你应该能够完全省略这种注释。 (其实,你可能必须这样做,来定义你自己的Proxy类型,因为我认为Data.Proxy中的一个可能是* -> *,而你需要Type -> *。如果你定义了data Proxy p = Proxy并且PolyKinds开启,那么它会被推断为AnyK -> *。)

+1

[paper]中的Proxy数据类型(http://research.microsoft.com/en-us/ people/dimitris/fc-kind-poly.pdf)终于有道理,谢谢! – Chetan 2012-04-23 20:28:10