Are there any interesting commutative monads in Haskell?

Until I edited it, the Haskell Wiki claimed to be Maybea commutative monad (in fact, I think it still claims to be somewhere). This is clearly wrong because

do {a <- Nothing; b <- undefined; return (a,b)} === Nothing

and

do {b <- undefined; a <- Nothing; return (a,b)} === undefined

This failure of commutativity is actually quite important in real code: programmers rely on the computation to stop as soon as it reaches Nothing.

This leaves (among the monads described as commutative on the Haskell Wiki) only the monad Reader, which does not seem to do anything terrible. This raised my question about whether there are any commutative monads in Haskell that are significantly different from Reader, with the exception of limitations Reader.

Edit

I just realized that it is also possible to make a limited monad Writercommutative - it should accumulate values ​​in some commutative monoid. Still not interesting.

+4
source share
2 answers

Such statements sometimes have an implied requirement to “ignore the lower ones,” and this is probably one of these cases. This is commutative if we ignore the bottoms. It is better to express this requirement explicitly, because it can become important.

If we ignore the lower ones, then Maybeit is also a commutative Monad. Of course, as you mentioned in this matter, it is important to consider the bottom practically in reality, therefore it is good to know about non-commutativity in their presence.

+5
source

set monad , IMO, .

+3

Source: https://habr.com/ru/post/1545595/


All Articles