I'm learning haskell, and trying to use applicative functors as much as possible instead of monad. It is very neat and easy to compose. However, occasionally some types like IO [IO [a]]
or IO Maybe IO Maybe a
will rise in the code, which cause me big trouble. Apparently monad become inevitable in these scenarios.
I know there is a flat operation like join:: m (m a) -> m a
for single level monads. Is there anything similar for multi-level monads? Anything in monad transformers?
Thanks a lot!
If you note that
m (n _)
is a monad transformer then you can define this operation. This is certainly the case when we notice thatIO (Maybe a)
is the same asMaybeT IO a
: we then just useMaybeT
'sjoin
. We can do this in no small part due to the fact that Maybe and IO "layer" especially nicely.On the other hand, not all "composed" monads are monad transformers. In particular,
IO [a]
isn't really one. The true ListT transformer we'd like to have and tojoin
looks likeIt is the case that we can turn
IO [a]
intoListT IO a
and visa-versa, but it is not the case that these operations invert one another. Truly,IO [a]
is not a monad in its own right and cannot bejoin
ed.