1

I've written two monads m1 and m2 whose semantics are such that it's possible to define this operation:

bindM1AndM2: (a -> m1 (m2 b)) -> m1 (m2 a) -> m1 (m2 b)

This is like >>=, but for binding double monads with an a inside an m2 inside an m1, and using that to convert the inner type while preserving the two outer monad layers.

Is there a general name for this operation in Haskell? Are there certain general type class(es) for which it makes sense, and can it be built from more fundamental building blocks? I'm familiar with the one particular example I'm working on, but not sure if there is something deeper here.

jacobsa
  • 5,719
  • 1
  • 28
  • 60
  • 1
    tl;dr of dupe: There's no way to write such a function that's general over all monads. For the subset of them where that is possible, the phrase you're looking for is "monad transformers". – Joseph Sible-Reinstate Monica Nov 04 '22 at 05:41
  • Thanks! I've had some trouble with monad transfomers because they seem backwards to my brain. Like for the case of `MaybeT`, in my example which of `m1` and `m2` is `Maybe`? Is it the inner one or the outer one? – jacobsa Nov 04 '22 at 05:44
  • 2
    It depends on the transformer. For example, `MaybeT IO Int` is isomorphic to `IO (Maybe Int)`, but `ReaderT String IO Int` is isomorphic to `Reader String (IO Int)`. – Joseph Sible-Reinstate Monica Nov 04 '22 at 05:48
  • I see, thanks. Are you able to answer the question I left in a comment on the [answer](https://stackoverflow.com/a/33193136/1505451) in the dupe? – jacobsa Nov 04 '22 at 07:12

0 Answers0