[E-Lang] analogy between "when" and monads

Jonathan A Rees jar1@mumble.net
Fri, 17 Aug 2001 13:59:54 -0400

The following idle thoughts on E and monads occurred to me recently.
If you're not a languagemonger you can skip this message.

Has anyone else noticed the analogy between the E argument that says
that message passing / event loop style is better than blocking / RPC
style, on the one hand, and the functional programming community's
basic position that monads are better than uncontrolled side effects?
I just searched for "monad" at eros-os.org and didn't turn up

In each case, a fundamental but impure feature of the system is banned
from use in ordinary contexts, such as among the steps of a sequence
of things to do or inside of a recursion or other pattern of nested
calls.  Instead, the feature is only allowed at the beginning or end
of little chunks of code.  In Haskell, the little chunks are the
closures that the monad's "bind" operates on; in E the chunk are the
blocks of code in when statements.  E is more permissive than Haskell
in that "when" can occur in arbitrary contexts, but the idea is the
same - scary thing happens only at known places.

The rationale is similar in both cases: Programs that make use of the
impure feature in uncontrolled places are seen to be more difficult to
reason about, make correct, and maintain.

A similar argument is made when people outlaw reified continuations
(Scheme call-with-current-continuation (CWCC)), as most programming
languages do.  What -- capture the current control stack?  Doesn't
that lead to unreadable and undebuggable programs?  This design
position isn't usually seen as a limitation because CWCC is seen as
terribly exotic and expensive, but structurally it's pretty much the

As Mark M mentioned a while back there is no loss of "power" (i.e. the
ability to write impure code) in any of these three situations, since
there is a straightforward translation, continuation-passing style
conversion, that isolates the impurities in any given program.  CPS
will take program that uses reified continuations and convert it into
one that doesn't, or a program that uses side effects into a
functional program (this is what denotational semantics does), or a
program that uses RPC into one using only event loops and message

Danvy and Filinski showed that having reified continuations in the
language allows the conversion of arbitrary monads from explicit
(pure) for to implicit (impure) form in a program.  In E, CWCC would
be equivalent to RPC.  This is a fine reason why E can't have CWCC.

No point to make here.  I like all these things - E, FP, CWCC.