[Haskell-cafe] Explaining monads

Ronald Guida ronguida at mindspring.com
Sun Aug 12 02:22:11 EDT 2007


Stefan O'Rear wrote:
 > On Sat, Aug 11, 2007 at 03:00:04PM -0400, Ronald Guida wrote:
 >> The question remains: "What is special about Monad or ArrowApply,
 >> compared to Arrow?" or "What is more general about Arrow, compared
 >> to Monad or ArrowApply?"
 >
 > If all you have is an Arrow, then you must make up your mind what
 > you're going to do ahead of time.
 >
 > ArrowChoice gives you the ability to make basic "yes or no"
 > decisions at run time.
 >
 > ArrowApply gives you arbitrary computed jumps.

OK, so I thought this through.

To summarize the classes of arrows:

1. Arrow is a device for sequencing side-effects in a fixed order.

 > If all you have is an Arrow, then you must make up your mind what
 > you're going to do ahead of time.

2. ArrowChoice is a device for sequencing side-effects in a fixed
   order with options.  Some effects can be selected at run-time, but
   (1) the available choices are fixed at compile-time and (2) options
   have to be selected before running the arrow computation.

 > ArrowChoice gives you the ability to make basic "yes or no"
 > decisions at run time.
BUT, you have to make these decisions before you run the arrow
computation.

3. ArrowApply is a device for sequencing side-effects, such that
   functions can dynamically choose side-effects at run-time based on
   intermediate results of arrow computations.

 > ArrowApply gives you arbitrary computed jumps.

---

We know that ArrowApply is equivalent to Monad.

 > Imagine trying to write an interpreter for a toy language with I/O,
 > and IO is a plain Arrow and not a Monad.  You can read input and
 > parse it, but you can't actually do IO because the IO you need to
 > do, depends on the input you read - precisely what Arrow forbids!

Here's a toy language, described by a regular expression:
  0(10)*110

I want to read characters, one at a time, and eventually decide to
"Accept" or "Reject" a string.

Let me try to understand my options.

 * With a simple Arrow, I can create a fixed sequence of "read"
   operations, and I can't act on the results (i.e. by choosing
   whether or not to keep reading) at run-time.

 * With ArrowChoice, I can create a set of alternative paths for
   "read" operations, and I can choose a path at run-time, but I have
   to choose a fixed path /before/ I get to see any results.

 * With ArrowApply, I can "read" one character and act on that
   character to choose what actions to perform next.  I can read
   characters until I can render an "Accept" or "Reject" decision.

Clearly, I need ArrowApply (or Monad) to get the job done.

In conclusion:

"What is special about Monad or ArrowApply, compared to Arrow?"
Arrow lets me sequence side-effects in a fixed order.  Monad lets me
dynamically choose side effects at run-time based on intermediate
results of previous side-effects.

-- Ron



More information about the Haskell-Cafe mailing list