Why can't ContT be an instance of MonadError?

I have a monad transformer stack, including ErrorT, and I want to wrap the transformer ContT raround everything. When I try to do this, my calls throwErrorgenerate type errors - obviously, ContT rit is not automatically an instance MonadError. Great, I thought - I will just do it alone:

instance MonadError e m => MonadError e (ContT r m) where
  throwError = lift . throwError
  catchError = liftCatch . catchError

using some suitable definition liftCatch. But now I get compilation errors:

src\Language\Types.hs:68:10:
    Illegal instance declaration for `MonadError e (ContT r m)'
      (the Coverage Condition fails for one of the functional dependencies;
       Use -XUndecidableInstances to permit this)
    In the instance declaration for `MonadError e (ContT r m)'

I am glad to use the pragma UndecidableInstances (I have the impression that this is not too alarming, for example, see this question ), but I wondered if there was a difficulty in creating a continuation transformer to an instance MonadError- I assume that if everything was fine, the authors the package would Control.Monad.Transalready have done it ... right?

+5
source share
1 answer

ContT and ErrorT provide custom control flow. There is a way to wrap the ErrorT type around ContT in mtl:

instance (Error e, MonadCont m) => MonadCont (ErrorT e m)

But these two monad transformers do not commute. Remember:

newtype Identity a = Identity {runIdentity :: a}
newtype ErrorT e m a = ErrorT {runErrorT :: m (Either e a)}
newtype ContT r m a = ContT {runContT :: (a -> m r) -> m r}

ErrorT String (ContT Bool Identity) (), which is quite normal in the mtl package might be:

ErrorT (ContT ( \ (k :: Either String () -> Identity Bool) -> k (Right ()) ) )

ContT r (ErrorT e Identity) anot suitable in mtl package. But you can write it.

( β†’ =), ? , callCC?

:

{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, UndecidableInstances   #-}
import Control.Monad
import Control.Monad.Cont
import Control.Monad.Error
import Data.Function
import Data.IORef

handleError :: MonadError e m => (e -> m a) -> m a -> m a
handleError = flip catchError

test2 :: ErrorT String (ContT () IO) ()
test2 = handleError (\e -> throwError (e ++ ":top")) $ do
  x <- liftIO $ newIORef 1
  label <- callCC (return . fix)
  v <- liftIO (readIORef x)
  liftIO (print v)
  handleError (\e -> throwError (e ++ ":middle")) $ do
    when (v==4) $ do
      throwError "ouch"
  when (v < 10) $ do
         liftIO (writeIORef x (succ v))
         handleError (\e -> throwError (e ++ ":" ++ show v)) label
  liftIO $ print "done"

go2 = runContT (runErrorT test2) (either error return)

{-

*Main> go2
1
2
3
4
*** Exception: ouch:middle:top

-}

, mtl, :

instance MonadError e m => MonadError e (ContT r m) where
  throwError = lift . throwError
  catchError op h = ContT $ \k -> catchError (runContT op k) (\e -> runContT (h e) k)

test3 :: ContT () (ErrorT String IO) ()
test3 = handleError (\e -> throwError (e ++ ":top")) $ do
  x <- liftIO $ newIORef 1
  label <- callCC (return . fix)
  v <- liftIO (readIORef x)
  liftIO (print v)
  handleError (\e -> throwError (e ++ ":middle")) $ do
    when (v==4) $ do
      throwError "ouch"
  when (v < 10) $ do
         liftIO (writeIORef x (succ v))
         handleError (\e -> throwError (e ++ ":" ++ show v)) label
  liftIO $ print "done"

go3 = runErrorT (runContT test3 return)

{-

*Main> go3
1
2
3
4
Left "ouch:middle:3:middle:2:middle:1:middle:top"

-}
+8

All Articles