User:Duplode/Arrow parser rewrite

Can it be done better?
Swierstra and Duponcheel (1996) noticed that, in some use cases, a smarter parser could immediately fail upon seeing the very first character. For example, in the  parser above, the choice of first letter parsers was limited to either the letter 'o' for "one" or the letter 't' for both "two" and "three". This smarter parser would also be able to garbage collect input sooner because it could look ahead to see if any other parsers might be able to consume the input, and drop input that could not be consumed. This new parser is a lot like the monadic parsers with the major difference that it exports static information. It's like a monad, but it also tells you what it can parse.

There's one major problem. This doesn't fit into the  interface. Monadic composition works with  functions, and functions alone. There's no way to attach static information. You have only one choice, throw in some input, and see if it passes or fails.

Back when this issue first arose, the monadic interface was being touted as a completely general purpose tool in the functional programming community, so finding that there was some particularly useful code that just couldn't fit into that interface was something of a setback. This is where arrows come in. John Hughes's Generalising monads to arrows proposed the arrows abstraction as a new, more flexible tool.

Static and dynamic parsers
Let us examine Swierstra and Duponcheel's parser in greater detail, from the perspective of arrows as presented by Hughes. The parser has two components: a fast, static parser which tells us if the input is worth trying to parse; and a slow, dynamic parser which does the actual parsing work.

The static parser consists of a flag, which tells us if the parser can accept the empty input, and a list of possible starting characters. For example, the static parser for a single character would be as follows:

It does not accept the empty string and the list of possible starting characters consists only of.

The dynamic parser needs a little more dissecting. What we see is a function that goes from  to. It is useful to think in terms of sequencing two parsers: each parser consumes the result of the previous parser, along with the remaining bits of input stream , it does something with  to produce its own result  , consumes a bit of string and returns that. So, as an example of this in action, consider a dynamic parser, where the   represents a count of the characters parsed so far. The table below shows what would happen if we sequence a few of them together and set them loose on the string "cake" :

So the point here is that a dynamic parser has two jobs : it does something to the output of the previous parser (informally, ), and it consumes a bit of the input string, (informally,  ), hence the type. Now, in the case of a dynamic parser for a single character (type ), the first job is trivial. We ignore the output of the previous parser, return the character we have parsed and consume one character off the stream:

This might lead you to ask a few questions. For instance, what's the point of accepting the output of the previous parser if we're just going to ignore it? And shouldn't the dynamic parser be making sure that the current character off the stream matches the character to be parsed by testing ? The answer to the second question is no − and in fact, this is part of the point: the work is not necessary because the check would already have been performed by the static parser. Naturally, things are only so simple because we are testing just one character. If we were writing a parser for several characters in sequence we would need dynamic parsers that actually tested the second and further characters; and if we wanted to build an output string by chaining several parsers of characters then we would need the output of previous parsers.

Time to put both parsers together. Here is our S+D style parser for a single character:

Bringing the arrow combinators in
With the preliminary bit of exposition done, we are now going to implement the Arrow class for, and by doing so, give you a glimpse of what makes arrows useful. So let's get started:

should convert an arbitrary function into a parsing arrow. In this case, we have to use "parse" in a very loose sense: the resulting arrow accepts the empty string, and only the empty string (its set of first characters is ). Its sole job is to take the output of the previous parsing arrow and do something with it. That being so, it does not consume any input.

Likewise, the  combinator is relatively straightforward. Given a parser, we want to produce a new parser that accepts a pair of inputs. The first component of the input, is what we actually want to parse. The second part passes through untouched:

We also have to supply the  instance. is entirely obvious, as  must hold:

On the other hand, the implementation of  requires a little more thought. We want to take two parsers, and return a combined parser incorporating the static and dynamic parsers of both arguments:

Combining the dynamic parsers is easy enough; we just do function composition. Putting the static parsers together requires a little bit of thought. First of all, the combined parser can only accept the empty string if both parsers do. Fair enough, now how about the starting symbols? Well, the parsers are supposed to be in a sequence, so the starting symbols of the second parser shouldn't really matter. If life were simple, the starting symbols of the combined parser would only be. Alas, life is not simple, because parsers could very well accept the empty input. If the first parser accepts the empty input, then we have to account for this possibility by accepting the starting symbols from both the first and the second parsers

So what do arrows buy us?
If you look back at our  type and blank out the static parser section, you might notice that this looks a lot like the arrow instances for functions.

There's the odd  variable out for the ride, which makes the definitions look a little strange, but the outline of e.g. the simple   functions is there. Actually, what you see here is roughly the arrow instance for the  monad/Kleisli morphism (let ,   and   actually be  ).

That's fine, but we could have easily done that with bind in classic monadic style, with  becoming just an odd helper function that could be easily written with a bit of pattern matching. But remember, our Parser type is not just the dynamic parser − it also contains the static parser.

This is not at all a function, it's just pushing around some data types, and it cannot be expressed in a monadic way. But the  interface can deal with just as well. And when we combine the two types, we get a two-for-one deal: the static parser data structure goes along for the ride along with the dynamic parser. The Arrow interface lets us transparently compose and manipulate the two parsers, static and dynamic, as a unit, which we can then run as a traditional, unified function.