Sat Jul 23 14:13:30 CEST 2011

Monad is a type class

Some important things to realize about monads, from my earlier
misconceptions which missed the (awesome) generality of this concept:

  * `Monad' is a type class and is used to say something _about_ a
    parameteric type.  For a parametric type (M t), the expression
    (Monad M) declares that the parametric type M implements the

      bind   :: M a -> (a -> M b) -> M b
      return :: a -> M a

    an `instance' declaration makes this explicit.

  * The abstract type Monad M => (M t) can carry a lot of hidden
    information next to "something of type t".  I.e. a typical
    declaration of a monadic type is:

       instance Monad (M a b c ...)

    where the types a b c ... are type parameters that do not take
    part in the monad interface.  The full type of M would be:

       M a b c ... t

    where the parameter `t' is the one that takes part in the monadic
    interface.  The type predicate `Monad' expects a parametric type
    with one parameter.  The "type" of `Monad' is called a "kind", and
    is * -> *.  [1]

  * The occurence of type paremeter t in a monadic type (M t) does not
    have to reflect to a naked data item in the implementation.  It
    can just as well be the input or output type of a function or a
    parameter in any parametric type.

    I.e. values of the (Cont v) monad do not contain concrete values
    v; they are functions that take a v type as input.

In some sense it is not monads that are the difficult concept, it is
type classes in general.  The ladder of abstraction is the list:

  1. basic types,      not parameterized
  2. parametric type,  parameterized by basic types
  3. type classes,     parameterized by parametric types

[1] http://www.haskell.org/haskellwiki/Kind