种类推断不能推断和约束种类什么都不做

时间:2014-07-29 00:25:02

标签: haskell ghc type-constraints type-kinds

tl; dr如何让Class拥有(* -> *) -> Constraint种类?

我有以下代码:

{-# LANGUAGE ConstraintKinds, TypeFamilies, RankNTypes, MultiParamTypeClasses #-}

class MonadTrans t where
    type Class t
    lift :: (Monad m) => m b -> t m b
    generalize :: (Monad m) => t m a -> (forall m'. (Class t m') => m a)

class (Monad m) => MaybeC m where
    fart :: m a

data MaybeT m a = NothingT | JustT (m a)

instance (Monad m) => Monad (MaybeT m) where
    return = JustT . return
    (JustT a) >>= f = a >>= f
    NothingT >>= f = NothingT

instance (Monad m) => MaybeC (MaybeT m) where
    fart = NothingT

instance MonadTrans MaybeT where
    type Class Maybe = MaybeC --Error Is HERE
    lift = JustT
    generalize NothingT = fart
    generalize JustT a  = return a

编译器说:&#34; Expected kind '* -> Constraint', but MaybeC&#39;善良(* -> *) -> Constraint'</code>&#34;但是Class(*->*)->Constraint

然后我尝试了:

{-# LANGUAGE ConstraintKinds, TypeFamilies, RankNTypes, MultiParamTypeClasses #-}

class MonadTrans t where
    type Class t :: (*->*)->Constraint
    lift :: (Monad m) => m b -> t m b
    generalize :: (Monad m) => t m a -> (forall m'. (Class t m') => m a)

class (Monad m) => MaybeC m where
    fart :: m a

data MaybeT m a = NothingT | JustT (m a)

instance (Monad m) => Monad (MaybeT m) where
    return = JustT . return
    (JustT a) >>= f = a >>= f
    NothingT >>= f = NothingT

instance (Monad m) => MaybeC (MaybeT m) where
    fart = NothingT

instance MonadTrans MaybeT where
    type Class Maybe = MaybeC
    lift = JustT
    generalize NothingT = fart
    generalize JustT a  = return a

但是编译器说&#34; pad1.hs:4:37: Not in scope: type constructor or class 'Constraint'&#34;,所以ConstraintKinds对我没有任何作用。

0 个答案:

没有答案