monads in Erlang?

Richard Carlsson richardc@REDACTED
Thu Feb 20 11:02:58 CET 2003


On Thu, 20 Feb 2003, Ulf Wiger wrote:

> On Thu, 20 Feb 2003, Peter-Henry Mander wrote:
> >
> >I've stumbled across monads[1] and their use in Haskell[2],
> >and they seem very useful. Not being a computer scientist
> >as such (I studied cybernetics and comp.sci. jointly), only
> >an engineer, I didn't know of them until now. I tried to
> >find an equivalent example in Erlang, but a quick search
> >found nothing. Can anyone here enlighten me further?
> >
[...]
> I think monads could probably be added to Erlang, but the gains
> would not be as obvious as in Haskell. I'm also not quite sure if
> the lack of currying will prevent an elegant implementation of
> monads.

I had a new look at monads recently, to refresh my memory and to see
whether there would be any point in supporting monads in Erlang, but my
conclusion was "no". What monads do, is that they let you syntactically
hide the "plumbing" necessary when you thread a state variable through
the program (or a particular part of the program). This is very good for
a Haskell programmer, who has no other choice (no side effects at all).

But the Haskell guy has to decide, for every part of his program, what
type of monad it might need. If he wants to do I/O at any point, he must
use the I/O monad all the way from the start of the program to each
point where I/O is used. If he needs exceptions, he must do the same
with the exception monad (and combine it with the I/O monad into a
single monad when both things may be needed). Et cetera.

In Erlang, it's like we already *have* a monad type for all expressions;
it handles message passing, exceptions, the process dictionary, and some
extra side effects. "All the goals, none of the fuss." Of course, it
makes it hard sometimes to understand what's going on. That's why Erlang
tries to keep the amount of side effects down (compare e.g. to Lisp),
and mostly restricts it to messages.

And yes, the lack of currying and strong typing makes it hard to get an
efficient implementation. Otherwise, some sort of monad support could
occasionally be nice for those cases where you today typically use
'mapfoldl'.

	/Richard


Richard Carlsson (richardc@REDACTED)   (This space intentionally left blank.)
E-mail: Richard.Carlsson@REDACTED	WWW: http://user.it.uu.se/~richardc/




More information about the erlang-questions mailing list