Personal tools

No kind signatures

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
(How to live without KindSignatures)
 
m (parens)
 
Line 5: Line 5:
 
E.g. I want to define
 
E.g. I want to define
 
<haskell>
 
<haskell>
newtype MonadTransformer t m a = MonadTransformer t m a
+
newtype MonadTransformer t m a = MonadTransformer (t m a)
 
</haskell>
 
</haskell>
 
with <hask>a :: *</hask>, <hask>m :: * -> *</hask>, <hask>t :: (* -> *) -> (* -> *)</hask>,
 
with <hask>a :: *</hask>, <hask>m :: * -> *</hask>, <hask>t :: (* -> *) -> (* -> *)</hask>,

Latest revision as of 18:21, 25 March 2013

[edit] 1 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 :: * -> * -> *
!

[edit] 2 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
.