I have a monad for a computation that may fail and does some logging:
f1 :: WriterT [String] (Either String) a
I have a function that will not fail but does some logging:
f2 :: Writer [String] b
What’s the best way to update the writer monad in f1 using the log from f2, and capture the output of the f2 computation? At the moment I’m doing this:
f2result <- (\(r,l) -> do {tell l; return r}) (runWriter f2)
I am using lift to update the inner monad with a different computation, so switching around the Writer and Either monads will not solve the problem.
If you defined
f2, the easiest possible approach may be to refactorf2so it’s defined thusly:Which shouldn’t be too hard, since
Writer w bis defined asWriterT w Identity b, and theIdentitymonad doesn’t give you anything.Then you’d be able to chain them just by doing
f1 >> f2.If you can’t redefine
f2, you could always define your own with the appropriate signature:And if you’ve a bunch of
f2to wrap, you could always define a function to wrap them for youSo you can do
f1 >> wrap f2a >> wrap f2b >> wrap f2c ...