No kind signatures

From HaskellWiki
Revision as of 18:21, 25 March 2013 by Monoidal (talk | contribs) (parens)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Question

I have a datatype that needs other kinds than the compiler infers but my compiler does not support KindSignatures. E.g. I want to define

newtype MonadTransformer t m a = MonadTransformer (t m a)

with a :: *, m :: * -> *, t :: (* -> *) -> (* -> *), but the compiler infers a :: *, m :: *, t :: * -> * -> *!

Answer

You can achieve this using the phantom type of the Const functor.

import Control.Applicative (Const(Const))

newtype MonadTransformer t m a = MonadTransformer (Const (t m a) (m a))

monadTransformer :: t m a -> MonadTransformer t m a
monadTransformer = MonadTransformer . Const

runMonadTransformer :: MonadTransformer t m a -> t m a
runMonadTransformer (MonadTransformer (Const m)) = m

For more than one kind specification you still need only one Const, since you can use a tuple type like so Const (t m a) (t m a, m a, a). So to speak, the second type argument of Const allows you to specify examples of how to use the type parameters of MonadTransformer.