[Haskell-cafe] Category theory monad <----> Haskell monad
Cale Gibbard
cgibbard at gmail.com
Thu Aug 18 20:39:37 EDT 2005
On 14/08/05, Carl Marks <id2359 at yahoo.com> wrote:
> Is there any text/article which makes precise/rigorous/explicit the connection
> between the category theoretic definition of monad with the haskell
> implementation?
Well, a monad over a category C is an endofunctor T on C, together
with a pair of natural transformations eta: 1 -> T, and mu: T^2 -> T
such that
1) mu . (mu . T) = mu . (T . mu)
2) mu . (T . eta) = mu . (eta . T) = id_C
In Haskell, a monad is an endofunctor on the category of all Haskell
types and Haskell functions between them. Application of the
endofunctor to an object is given by applying a type constructor (the
one which is made an instance of the Monad class). Application of the
endofunctor to a function is carried out by fmap or liftM. The natural
transformation eta is called return, and mu is called join (found in
the Monad library).
Haskell uses a somewhat different (but equivalent) basis for a monad,
in that it is not map, return, and join which need defining to make a
type an instance of the Monad class, but return and (>>=), called
"bind" or "extend".
One can define bind in terms of fmap, and join as
x >>= f = join (fmap f x)
and one can get back join and fmap from return and bind:
join x = x >>= id
fmap f x = x >>= (return . f)
hope this helps,
- Cale
More information about the Haskell-Cafe
mailing list