Haskell - specifying kind in data declaration
In this declaration
data Const a = Const Integer
Haskell infers that Const is * -> *. Is it possible to make Const take a type constructor instead, so it will be (* -> *) -> *? Idea开发者_运维知识库lly, it should be a -> *, but there are no polymorphic kinds. This thread shows one solution - adding unused constructor. Has the situation changed since 2002?
You're right, since GHC doesn't support polymorphic kinds, it will do kind defaulting to *.
However, using the KindSignatures extension, you can explicitly annotate a data declaration with a specific kind:
{-# LANGUAGE KindSignatures #-}
data Const (a :: * -> *) = Const Integer
Or, if your a fan of GADTs:
{-# LANGUAGE GADTs #-}
data Const :: (* -> *) -> * where
Const :: Integer -> Const a
You can now write:
x :: Const Maybe
x = Const 2
I just compiled the original program with the experimental Utrecht Haskell Compiler (UHC).
The UHC doesn't really compile any real world programs, but it does support kind polymorphism! Its interesseting to see it infer:
-- kind of data type:
Const :: forall a . a -> *
-- type of constructor:
Const :: Integer -> forall* a . forall b(a) . Const b
加载中,请稍侯......
精彩评论