The Parsec library
In practice, you will want to use an off-the-shelf parser combinator library. In this chapter, we’ll study Parsec, which is one of the older and more established libraries. On Hackage, you can find various new libraries with more bells and whistles, but Parsec will do nicely as a starting point.
Different types of parsers
To provide additional flexibility and expressive power, Parsec’s parser type features three additional type parameters beyond the a
parameter for the result type:
ParsecT s u m a
Let’s discuss the three additional parameters from right to left:
- The monad parameter,
m
, signals thatParsecT s u
is a monad transformer; it can be layered on top of a monad,m
. For basic uses, we don’t need any underlying monad and can default to using the trivialIdentity
monad. For that, Parsec provides a type synonym:type Parsec s u = ParsecT s u Identity
- The
u
parameter is for a user-defined state. Indeed, some parsers...