Hi all,
let’s say we have a newtype for ExceptT goes like:
newtype ExceptT (e :: Type) (m :: Type -> Type) (a :: Type) = ExceptT
{runExceptT :: m (Either e a)}
and say we want to implement the Monad for it,
instance (Monad m) => Monad (ExceptT e m) where
return = pure
a >>= f = ExceptT $ do
a' <- runExceptT a
case a' of
Left err -> pure $ Left err
Right val -> runExceptT $ f val
The above version works fine. But the below one doesn’t:
instance (Monad m) => Monad (ExceptT e m) where
return = pure
a >>= f = do
a' <- runExceptT a
case a' of
Left err -> ExceptT $ pure $ Left err
Right val -> f val
compiler throw the error:
• Couldn't match type ‘m’ with ‘ExceptT e m’
Expected: ExceptT e (ExceptT e m) a
Actual: ExceptT e m a
‘m’ is a rigid type variable bound by
the instance declaration
at /home/longlong/hcat/src/BasicStateDemo.hs:36:10-41
• In the first argument of ‘runExceptT’, namely ‘a’
In a stmt of a 'do' block: a' <- runExceptT a
In the expression:
do a' <- runExceptT a
case a' of
Left err -> ExceptT $ pure $ Left err
Right val -> f val
• Relevant bindings include
f :: a -> ExceptT e m b
(bound at /home/longlong/hcat/src/BasicStateDemo.hs:39:9)
a :: ExceptT e m a
(bound at /home/longlong/hcat/src/BasicStateDemo.hs:39:3)
(>>=) :: ExceptT e m a -> (a -> ExceptT e m b) -> ExceptT e m b
(bound at /home/longlong/hcat/src/BasicStateDemo.hs:39:5)
This error made me confused, can anyone help explain the difference and the error to me?
Thanks!
BRs,
Longlong