我有一个包含ErrorT的monad转换器堆栈,我想将ContT r转换器包装在整个东西上。当我尝试执行此操作时,我对throwError的调用会产生类型错误-显然ContT r并非自动成为MonadError的实例。很好,我想-我将它变成一个:

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

使用一些合适的liftCatch定义。但是现在编译时出现错误:
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)'

我很高兴使用UndecidableInstances编译指示(我觉得它不太令人担忧,例如,请参阅this question),但我想知道将延续转换器转换为MonadError实例是否有困难-我猜是否很好,Control.Monad.Trans软件包的作者已经做过了...对吗?

最佳答案

ContT和ErrorT都允许非标准控制流。有一种方法可以在Mtl中将ErrorT类型包装在ContT周围:

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

但是,这两个monad变压器不会上下班。记住:
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}

软件包mtl中可以的ErrorT String (ContT Bool Identity) ()可以是:
ErrorT (ContT ( \ (k :: Either String () -> Identity Bool) -> k (Right ()) ) )

软件包mtl中的ContT r (ErrorT e Identity) a不合适。但是你可以写它。

您想要在组合的monad中使用(>> =)的语义是什么?您如何期望您的嵌套错误处理程序堆栈与非本地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"

-}

关于haskell - 为什么不能将ContT设为MonadError的实例?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10742151/

10-14 04:43