我想定义一个monad转换器,除其他外,它赋予一个具有错误功能的基本monad。如果基本monad是变换的monad应该是MonadPlus的一个实例,但我无法弄清楚如何定义MonadPlus实例,以便ErrorT转换器将返回多个答案(如果有多个答案)。我的两次尝试都在下面的代码中。在代码的末尾,是一个语句,显示monad转换器在基本monad为[]
时应该如何表现。感谢。
import Control.Monad.Error
import Control.Monad.Trans.Class
data MyTrans m a = MyTrans {runMyTrans :: ErrorT String m a}
instance Monad m => Monad (MyTrans m) where
return = MyTrans . lift . return
m >>= f = MyTrans $ do x <- runMyTrans m
runMyTrans (f x)
instance MonadTrans MyTrans where
lift m = MyTrans $ lift m
instance MonadPlus m => MonadPlus (MyTrans m) where
mzero = MyTrans $ lift mzero
-- Attempt #1 (Only reveals the first element)
m `mplus` n = MyTrans $ (runMyTrans m) `mplus` (runMyTrans n)
-- Attempt #2 (Incomplete: see undefined statements)
-- m `mplus` n = MyTrans $
-- lift $ do a <- runErrorT $ runMyTrans m
-- b <- runErrorT $ runMyTrans n
-- case a of
-- Right r 1
-- Left _ -> undefined
-- -> case b of
-- Left _ -> undefined
-- Right t -> return r `mplus` return t
type MyMonad = MyTrans []
x = return 1 :: MyMonad Int
y = MyTrans $ throwError "Error" :: MyMonad Int
z = x `mplus` y
main = do
print $ (runErrorT $ runMyTrans z) -- should be [Right 1, Left "Error"]
答案 0 :(得分:4)
instance MonadPlus m => MonadPlus (MyTrans m a) where
mzero = MyTrans (ErrorT mzero)
MyTrans (ErrorT m) `mplus` MyTrans (ErrorT n) = MyTrans (ErrorT (m `mplus` n))
小心:我只是确认这有您要求的行为,而不是检查它是否符合MonadPlus
法律。有可能违反其中的部分或全部内容!使用风险自负。