3

Is it possible to resolve y as a function of f and x, if the monads N and M are one-way?

x :: M (N X)

f :: X -> M (N Y)

-- pattern matching not allowed, one-way monads implied
y :: M (N Y)
y = _

Full definitions:

data X = X
data Y = Y

-- pattern matching not allowed, one-way monads implied
data M a = M a
data N a = N a

instance Functor M where
  fmap = undefined

instance Applicative M where
  pure = undefined
  (<*>) = undefined

instance Monad M where
  (>>=) = undefined

instance Functor N where
  fmap = undefined

instance Applicative N where
  pure = undefined
  (<*>) = undefined

instance Monad N where
  (>>=) = undefined


x :: M (N X)
x = undefined

f :: X -> M (N Y)
f = undefined

-- pattern matching not allowed, one-way monads implied
y :: M (N Y)
y = _

My intuition is that this is not generally possible, and that it is related to the problem that monad transformers solve, but I am not sure.

Matthew Piziak
  • 3,430
  • 4
  • 35
  • 49
  • 1
    Monads are not generally closed under composition. That is, given monads `m` and `n`, the composition of `m` and `n` is not necessarily a monad. In this specific case, however, where both are the identity monad, they do compose. – Rein Henrichs Aug 15 '17 at 18:24
  • Ah, yes, the definitions are placeholders, and not intended to be `Identity`. So, okay, this is not generally possible. – Matthew Piziak Aug 15 '17 at 18:26
  • I've voted to close this as a duplicate of a question with a high quality answer on why monads do not compose (while applicatives do). I hope it's helpful. – Rein Henrichs Aug 15 '17 at 18:28
  • It is, thank you Rein. I'll vote to close as well. – Matthew Piziak Aug 16 '17 at 02:51

0 Answers0