我想定义一个 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"]