Search code examples
scalahaskellfunctional-programminglanguage-agnosticcategory-theory

A type constructor IS a monad or HAS a monad?


People usually say a type IS a monad.

In some functional languages and libraries (like Scala/Scalaz), you have a type constructor like List or Option, and you can define a Monad implementation that is separated from the original type. So basically there's nothing that forbids you in the type system from creating distinct instances of Monad for the same type constructor.


  • is it possible for a type constructor to have multiple monads?
  • if yes, can you provide any meaningful example of that? any "artificial" one?
  • what about monoids, applicatives... ?

Solution

  • You can commonly find this all around in mathematics.

    • A monad is a triple (T, return, bind) such that (...). When bind and return can be inferred from the context, we just refer to the monad as T.

    • A monoid is a triple (M, e, •) such that (...). (...) we just refer to the monoid as M.

    • A topological space is a pair (S, T) such that (...). We just refer to the topological space as S.

    • A ring is a tuple (V, 0, +, 1, ×)...

    So indeed, for a given type constructor T there may be multiple different definitions of return and bind that make a monad. To avoid having to refer to the triple every time, we can give T different names to disambiguate, in a way which corresponds to the newtype construct in Haskell. For example: [] vs ZipList, State s vs ReaderT s (Writer s).


    P.S. There is something artificial in saying that a monad or a monoid is a triple, especially given that there are different presentations: we could also say that a monad is a triple (T, fmap, join), or that a monoid is a pair (M, •), with the identity element hidden in the extra condition (because it is uniquely determined by anyway). The ontology of mathematical structures is a more philosophical question that is outside the scope of SO (as well as outside my expertise). But a more prudent way to reformulate such definitions may be to say that "a monad is (defined|characterized) by a triple (T, return, bind)".