[Haskell-cafe] Functions with side-effects?

Wolfgang Jeltsch wolfgang at jeltsch.net
Wed Dec 21 12:31:35 EST 2005


Am Mittwoch, 21. Dezember 2005 16:28 schrieb David Barton:
> Wolfgang Jeltsch writes:
> ----- Original Message -----
>
> > Am Mittwoch, 21. Dezember 2005 13:15 schrieb Creighton Hogg:
> >> [...]
> >>
> >> Monads, I believe, can be just thought of as containers for state.
> >
> > I would say that you are talking especially about the I/O monad here.  A
> > monad as such is a rather general concept like a group is in algebra.
>
> While this is correct, I'm afraid that for most of us it is a flavorless
> answer.  I wish I had the mathematical mind that made the word "group" in
> this context instantly intuitively recognizable, but I don't.

The point is that the concept of a monad is a rather abstract one.  Maybe we 
should take vector spaces instead of groups.  A vector space is just a set 
together with two operations, namely vector addition and scalar 
multiplication whereby the operations have to fulfill a couple of laws.  One 
law, for example, would be that vector addition is commutative.

The point is that this vector space concept is very general.  There are a lot 
of concrete vector spaces which all fit that general idea.  For example, R^2 
together with the commonly defined vector addition and scalar multiplication 
is a vector space.  But there are many others, lots of which seem to have 
nothing to do with R^2 and its operations from the first sight.  So we have a 
general concept "vector space" and a lot of concrete vector spaces.

Now, "monad" is a general concept like "vector space".  In Haskell terms a 
monad is a type of kind * -> * (i.e., a type which needs to be applied to one 
type parameter in order to be able to be used as a type of expressions) 
together with operations (>>=) and return whereby (>>=) and return have to 
have specific types and have to fulfill certain laws.

IO together with its (>>=) and return is an example of a concrete monad.  [] 
together with concatMap and \x -> [x] is another one.  This may seem very 
surprising at first ("What do lists have to do with I/O computations?") but 
it's really so.

> I think Phil Wadler said it best when he said that a monad is a
> *computation*.

No, a value of type IO <something> is a computation.  The type IO plus (>>=) 
plus return is a monad.  So is [] plus concatMap plus \x -> [x].

> [...]

> Dave Barton

Best wishes,
Wolfgang


More information about the Haskell-Cafe mailing list