haskell - Is there a better way than writing `lift`s before each `IO` in a nested `MonadTrans`? -
sometimes need use several nested monadtrans. example, put maybet inside of exceptt, mimick continue , break in imperative programming:
runexceptt . form [1..] $ \ _ -> runmaybet $ ... mzero -- mimics continue lift $ throwe "..." -- mimics break lift . lift $ putstrln "hello!" ... however, above code shows, every time need io inside of "artificial loop", need put ugly lift . lift before it. imagine if have more complicated nestings, , lot of io operations, becomes anonyance. how make code cleaner , more concise?
for particular case of io, can use liftio :: monadio m => io -> m a lift io action through arbitrarily deep monad transformer stack:
liftio $ putstrln "hello" this expression has type monadio m => m (). transformer stack contains instance of monadio somewhere inside of should have monadio instance itself, why works stack of depth.
Comments
Post a Comment