0

The topic of what is a Monad has been extensively covered in several stackoverflow questions, as well as other forms. Nevertheless, I find it scarce to find examples of what is a false Monad (I don't know if there is an existing term for that, I kind of made-up the word). By false Monad, I mean that it fulfills one or more of the Monad properties, but does NOT fulfill all the properties.

To my understanding, the properties of Monads can be summarized as:

// 1. Monad(foo).flatMap(f) == f(foo)
// 2. Monad(bar).flatMap(f => f(bar)) == Monad(bar)
// 3. Monad(baz).flatMap(f).flatMap(g) == Monad(baz).flatMap(x => f(x).flatMap(g))

I am looking for examples of structures that are Not monads. Ideally, examples of Monads that do apply some but not all the laws for Monads. Further readings are also welcome.

Update: My objective is not to have an open discussion of what a Monad is not. It's rather have code examples in Scala for structures that apply some but not all Monad laws.

If it helps, I found an example mentioned on this article on Monads, that states that a non-Monad can be a list whose flatMap implementation returns the concatenation of the small lists in reverse. I hope that provides a better direction of the examples needed to help clear my confusion.

alt-f4
  • 2,112
  • 17
  • 49
  • 2
    This is a really odd request. A monad is NOT a banana, for example. If you flatten a banana, you get some banana paste at best. It's delicious but I'm not sure this is very useful thing in the context of Stack Overflow. – VLAZ Jul 13 '20 at 10:54
  • 1
    This question cannot have a single authoritative answer. It looks like an invitation to an open discussion. – n. m. could be an AI Jul 13 '20 at 10:55
  • @VLAZ Was not my intention to discuss what is not a Monad in general, rather structures that apply some properties of a Monad but not all. I am happy to make my question more specific – alt-f4 Jul 13 '20 at 10:57
  • 2
    There are infinitely many structures that don't obey all the monadic laws. [JavaScript Promises](https://stackoverflow.com/questions/45712106/why-are-promises-monads/) for example. But I don't think that 1. we can exhaustively categorise all of these structures 2. categorising them has much value. – VLAZ Jul 13 '20 at 10:58
  • @VLAZ I have updated my question with slightly more detail. I am not looking for an exhaustive category of all possible non-Monads. But rather, examples like: `a list whose flatMap implementation returns the concatenation of the small lists in reverse.` – alt-f4 Jul 13 '20 at 11:03
  • @n.'pronouns'm. I am not looking for a single authoritative answer, bur rather one or more examples for data structures that do not obey the Monad laws (similar to the example I tried to provide in my updated answer). Of course I understand it is challenging as Monads are an abstraction in itself. Happy to make my question more specific if you have any ideas. – alt-f4 Jul 13 '20 at 11:09
  • This site is built *for* authoritative answers. If your question does not admit one, it probably does not belong here. – n. m. could be an AI Jul 13 '20 at 11:27
  • 3
    A lawless or unprincipled monad is a monad-like structure that provides the necessary operations (`pure`, `flatmap`) but whose `flatmap` operation tempers with the value or isn't associative or whose `pure` operation actually performs an effect. A monad must only be concerned with the context, not the value inside this context. –  Jul 13 '20 at 12:04
  • 2
    Firstly, some structure will be not a monad if you don't know how to `flatten` it. For example binary tree with values in leaves is a monad because you can `flatten` binary tree of binary trees extending the outer tree by gluing inner trees in leaves of the outer tree. But binary tree with values in all nodes is not a monad because it's not clear how naturally to flatten a binary tree of binary trees. – Dmytro Mitin Jul 13 '20 at 12:17
  • 2
    Secondly, there can be some structure that we know how to `flatten` and `pure` but not satisfying monadic laws. Such things are called illegal instances. See https://github.com/non/alleycats – Dmytro Mitin Jul 13 '20 at 12:20
  • What is the objective of this question? Or just simply curiosity? – Luis Miguel Mejía Suárez Jul 13 '20 at 13:17
  • @LuisMiguelMejíaSuárez recently got introduced to Monads so trying to wrap my head around them. Curiosity. – alt-f4 Jul 13 '20 at 13:18
  • Related: [A monad is just a monoid in the category of endofunctors, what's the problem?](https://stackoverflow.com/questions/3870088/a-monad-is-just-a-monoid-in-the-category-of-endofunctors-whats-the-problem#:~:text=All%20told%2C%20a%20monad%20in,X%20here%20is%20a%20category.&text=This%20is%20a%20category%20in,the%20morphisms%20are%20natural%20transformations) – user Jul 13 '20 at 18:51

1 Answers1

1

Speaking in Java's terminology: a monad = a flatMap+unit interface + a monad-laws contract.

You can always get rid of laws/contract, but then you are left with just an interface without any guarantees - and in monad's case it is very important because the contract basically let you combine functions A => F[B] as if they were were numbers (the order of parenthesis doesn't matter, there is a 0 which doesn't affect the result). Take away any of the laws and suddenly the analogy falls apart. (Kind of like Cloneable interface in Java, where implementation throws NotImplementedException - it is an implementation of an interface, but it's rather useless). For that reason nobody would call them algebras, and an interface is the best name they deserve.

Try and Future as such implementations - they have maps and flatMaps but aren't considered lawful. Are they useful? Yes. Can you refactor them brainlessly like you could with every monad? No, because you can change the behavior. In Cats such implementations would be likely to land in alleycats library.

If you are looking for less powerful algebras (but still algebras!) than monad, there are functors or applicative functors.

Mateusz Kubuszok
  • 24,995
  • 4
  • 42
  • 64