Is the composition of an arbitrary monad with a traversable always a monad?

后端 未结 2 979
我在风中等你
我在风中等你 2020-12-09 09:58

If I have two monads m and n, and n is traversable, do I necessarily have a composite m-over-n monad?

<
2条回答
  •  长情又很酷
    2020-12-09 10:39

    No, it's not always a monad. You need extra compatibility conditions relating the monad operations of the two monads and the distributive law sequence :: n (m a) -> m (n a), as described for example on Wikipedia.

    Your previous question gives an example in which the compatibility conditions are not met, namely

    S = m = [], with unit X -> SX sending x to [x];

    T = n = (->) Bool, or equivalently TX = X × X, with unit X -> TX sending x to (x,x).

    The bottom right diagram on the Wikipedia page does not commute, since the composition S -> TS -> ST sends xs :: [a] to (xs,xs) and then to the Cartesian product of all pairs drawn from xs; while the right-hand map S -> ST sends xs to the "diagonal" consisting of only the pairs (x,x) for x in xs. It is the same problem that caused your proposed monad to not satisfy one of the unit laws.

提交回复
热议问题