4 ms·
Imagine a concept called "Additive". Any type that defines a '+' operator is "Additive". In most industrial languages, integers, floats and strings are Additiv
by barrkel 12y ago
Imagine a concept called "Additive". Any type that defines a '+' operator is "Additive".
In most industrial languages, integers, floats and strings are Additive, because these things support the '+' operator.
Monads are at the same level of "concept" as Additive. Something is a monad if it has a set of operators that map to the Monad concept. The level of abstraction you need to be at, mentally, is "supports a specific set of operations with specific rules". A lot like an interface, but at the type level.
It is definitely a single concept, but there are variations on it, like Functor and Applicative, which are related. The core concept is adding a layer of indirection between a value and application of a function to the value. What the indirection does is the chief distinguishing feature of the monad. A Maybe monad only applies the function if the value is not None, for example. A List monad applies the function to each value in the list. The IO monad, on the other hand, logically stores the function and keeps it for later - the result of computing a pure functional Haskell program returning IO is an imperative program, which is then executed. That's how Haskell gets its side-effects in a pure fashion.
- eru 12y agoThe Haskellic name for Additive is Monoid.
- wz1000 12y agoThat depends on how you define Additive, whether it includes the concept of commutativity, associativity and so on.
- kqr 12y agoSemigroup is a closer match. Monoid includes an identity element. (All monoids are semigroups, and a lot of semigroups are monoids, but not all of them.)
- barrkel 12y agoI made up "Additive" to introduce the idea of different types all having a single operator in common, rather than as a concrete Haskell-ish thing. Its purpose here is to focus on the right level of abstraction when thinking about what a monad is, particularly when coming from languages like Java, C++, C#, etc.