FW: RE [Haskell-cafe] Monad Description For Imperative Programmer
Claus Reinke
claus.reinke at talk21.com
Wed Aug 1 18:17:50 EDT 2007
a Monad is a type constructor with two operations, implementing
a standard interface and following a few simple rules.
the Monad type class tells you the interface (what operations
you've got, and their types), the Monad laws tell you what all
types implementing that interface should have in common.
the monadic interface gives you two operations, one to throw
things into a monad thing (return), and one to chain two monad
things together (>>=). the chaining explicitly caters for information
flowing from the first to the second parameter of (>>=).
the monad laws tell you two useful facts about monad things
thrown together in that way: whatever it is the monad does,
anything just thrown into it will take no part in that action,
and whichever way you use that chaining operation, the
structure of chaining is irrelevant, only the ordering of chained
monad things matters.
there are usually other ways to create 'primitive' monadic things,
which can be combined into complex monadic structures using
the operations from the Monad interface.
there is usually a way to interpret monadic structures built in
this way (a 'run' operation of some kind).
that's it, i think?-)
claus
examples include:
- i/o: primitive monadic things are basic i/o operations,
the 'run' operation is outside the language, applied to
'Main.main', and interprets (abstract) IO monad structures
sequentially, starting with the leftmost innermost i/o
operation in the structure and applying the second
argument of (>>=) to the result of executing the first.
- []: primitive monadic things are lists, the 'run' operation
is the identity, ie, the lists are directly exposed as data
structures, return creates a singleton list, (>>=) applies
its second argument to each element of its first argument
and concatenates the results (concatMap).
- State: primitive monadic things are operations on a state
type, returning a result and a state; return returns its
parameter, passing its input state unchanged, (>>=) applies
its first parameter to the input state, applies its second
parameter to the result value and result state of the first.
'run' is runState and applies a (possibly) complex monadic
thing to an input state, returning a result and a (modified)
state.
More information about the Haskell-Cafe
mailing list