Haskell/Arrows

Introduction
Arrows are a generalization of monads: every monad gives rise to an arrow, but not all arrows give rise to monads. They serve much the same purpose as monads -- providing a common structure for libraries -- but are more general. In particular they allow notions of computation that may be partially static (independent of the input) or may take multiple inputs. If your application works fine with monads, you might as well stick with them. But if you're using a structure that's very like a monad, but isn't one, maybe it's an arrow.

and the arrow tail
Let's begin by getting to grips with the arrows notation. We'll work with the simplest possible arrow there is (the function) and build some toy programs strictly in the aims of getting acquainted with the syntax.

Fire up your text editor and create a Haskell file, say toyArrows.hs:

These are our first two arrows. The first is the identity function in arrow form, and second, slightly more exciting, is an arrow that adds one to its input. Load this up in GHCi, using the -XArrows extension and see what happens.

% ghci -XArrows toyArrows.hs     ___         ___ _ / _ \ /\ /\/ __(_) / /_\// /_/ / /  | |      GHC Interactive, version 6.4.1, for Haskell 98. / /_\\/ __ / /___| |      http://www.haskell.org/ghc/ \____/\/ /_/\____/|_|     Type :? for help.

Loading package base-1.0 ... linking ... done. Compiling Main            ( toyArrows.hs, interpreted ) Ok, modules loaded: Main. 3 "foo" 4 101
 * Main> idA 3
 * Main> idA "foo"
 * Main> plusOne 3
 * Main> plusOne 100

Thrilling indeed. Up to now, we have seen three new constructs in the arrow notation:
 * the keyword
 * the imported function
 * the imported function

Now that we know how to add one to a value, let's try something twice as difficult: adding TWO:

One simple approach is to feed (a+1) as input into the  arrow. Note the similarity between  and. You should notice that there is a basic pattern here which goes a little something like this: proc FOO -> SOME_ARROW -< (SOMETHING_WITH_FOO)

notation
Our current implementation of  is rather disappointing actually... shouldn't it just be   twice? We can do better, but to do so, we need to introduce the do notation:

Now try this out in GHCi:

Prelude> :r Compiling Main            ( toyArrows.hs, interpreted ) Ok, modules loaded: Main. 7
 * Main> plusTwoBis 5

You can use this do notation to build up sequences as long as you would like:

Monads and arrows

 * FIXME: I'm no longer sure, but I believe the intention here was to show what the difference is having this proc notation instead to just a regular chain of dos

/Not in book