Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (* -> *) -> *? Ideally, it should be a -> *, but there are no polymorphic kinds. This thread shows one solution - adding unused constructor. Has the situation changed since 2002?

like image 944
sdcvvc Avatar asked Jan 07 '10 19:01

sdcvvc


1 Answers

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
like image 138
Tom Lokhorst Avatar answered Sep 28 '22 16:09

Tom Lokhorst