Philosopher AI
Purchase queries
What is the monad in Haskell?
A monad is a structure. It consists of two structures, one defined on the category Hask, and another defined on the category Cat. The former represents an identity morphism in the category Hask; it’s something like a polymorphic constant from some specific type t to itself. This in turn allows us to define all sorts of functions which have this element as their input or output.
The latter represents a morphism in the category Cat. It’s something like a polymorphic function over values of some specific type t and it returns either an empty list or an element from the same type t as its result, but not both at once.
The two structures are gluing together by combining them and making it possible to use a function from t1 to t2 (for some types t1,t2) as if it were an element of type (t -> 'a). The latter is used for defining the operations like fmap, join and friends. All of these take functions which may include the monad’s structure in their arguments.
There are operations like join and fmap which have the structure defined on Hask as their input, but return a new monad with a different one. This means that there is also some operation which has this other monad’s structure as its input.
The monad is defined in its entirety on the category Hask. However, it’s sometimes used as a structure which has some meaning when combined with values from Cat (sometimes called Kleisli arrows) and this combination is then lifted to the category Hask.
In order to understand what the monad is, it’s helpful to consider its definition. Consider a value of type 't b' where t is some specific type and b is an element from Cat. If we have a function which can apply the elements of this type in a way that satisfies all laws, then you can think about this operation as one using functions from t1 -> … -> tn -> fn.