MaybeT monad中没有任何内容:更简洁的方式?

时间:2017-05-03 07:47:58

标签: haskell monads

我正在试验MaybeT monad,特别是MaybeT Identity String

import Control.Monad.Trans.Maybe
import Control.Monad.Identity
import Data.Maybe

main :: IO ()
main = putStrLn . show . runIdentity . runMaybeT $ maybeGetString

maybeGetString :: MaybeT Identity String
maybeGetString = return "first" >>
                 maybeTNothing >> 
                 return "second"

maybeTNothing :: MaybeT Identity String
maybeTNothing = MaybeT $ return Nothing

MaybeT相当于Nothing的预测似乎是MaybeT $ return Nothing,感觉有点冗长,我必须明确使用{{1}构造函数。

MaybeT monad中是否有更短/更好/更清晰的Nothing写作方式?

1 个答案:

答案 0 :(得分:2)

受到@luqui

评论的启发

要在普通Nothing monad中表现为MaybemaybeTNothing应满足等式

maybeTNothing >>= f  =  maybeTNothing
v >> maybeTNothing   =  maybeTNothing

这正是来自MonadPlus的{​​{3}}的要求,MaybeT是其中的一个实例。所以我们可以使用mzero

maybeTNothing = mzero

mzero