gpt4 book ai didi

haskell - 为什么 ContT 不能成为 MonadError 的实例?

转载 作者:行者123 更新时间:2023-12-02 17:10:56 24 4
gpt4 key购买 nike

我有一个包含 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 pragma(我的印象是它并不太令人担忧,例如参见 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}

ErrorT String (ContT Bool Identity) () 在包 mtl 中可以是:

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

ContT r (ErrorT e Identity) a 在包 mtl 中是不行的。但你可以写它。

你想要组合单子(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com