A monad is a mathematical structure which is heavily used in (pure) functional programming, basically Haskell. However, there are many other mathematical structures available, like for example applicative functors, strong monads, or monoids. Some have more specific, some are more generic. Yet, monads are much more popular. Why is that?
One explanation I came up with, is that they are a sweet spot between genericity and specificity. This means monads capture enough assumptions about the data to apply the algorithms we typically use and the data we usually have fulfills the monadic laws.
Another explanation could be that Haskell provides syntax for monads (do-notation), but not for other structures, which means Haskell programmers (and thus functional programming researchers) are intuitively drawn towards monads, where a more generic or specific (efficient) function would work as well.
monads are used to address the more general problem of computations (involving state, input/output, backtracking, ...) returning values: they do not solve any input/output-problems directly but rather provide an elegant and flexible abstraction of many solutions to related problems.
A monad is an algebraic structure in category theory, and in Haskell it is used to describe computations as sequences of steps, and to handle side effects such as state and IO. Monads are abstract, and they have many useful concrete instances. Monads provide a way to structure a program.
So in simple words, a monad is a rule to pass from any type X to another type T(X) , and a rule to pass from two functions f:X->T(Y) and g:Y->T(Z) (that you would like to compose but can't) to a new function h:X->T(Z) . Which, however, is not the composition in strict mathematical sense.
The term monad (from Ancient Greek μονάς (monas) 'unity', and μόνος (monos) 'alone') is used in some cosmic philosophy and cosmogony to refer to a most basic or original substance. As originally conceived by the Pythagoreans, the Monad is the Supreme Being, divinity or the totality of all things.
I suspect that the disproportionately large attention given to this one particular type class (Monad
) over the many others is mainly a historical fluke. People often associate IO
with Monad
, although the two are independently useful ideas (as are list reversal and bananas). Because IO
is magical (having an implementation but no denotation) and Monad
is often associated with IO
, it's easy to fall into magical thinking about Monad
.
(Aside: it's questionable whether IO
even is a monad. Do the monad laws hold? What do the laws even mean for IO
, i.e., what does equality mean? Note the problematic association with the state monad.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With