[Haskell-cafe] Sequencing Parsers: a Simple Example

Shachaf Ben-Kiki shachaf at gmail.com
Sat Dec 1 11:30:50 EST 2007


> Hi
> (>>=) :: Parser a -> Parser b -> Parser b
> p >>= f = \inp ->
>    case p inp of
>      [] -> []
>      [(v, out)] -> parse (f v) out
> based on a lot of guesswork, after the mess created by the OCR, I
> managed to get the above example to work syntactically but is it
> semantically correct?
> Thanks, Paul

You probably want:

(>>=) :: Parser a -> (a -> Parser b) -> Parser b
p >>= f = \inp -> case parse p inp of
                    [] -> []
                    [(v,out)] -> parse (f v) out

Assuming that you're following Graham Hutton's book.

Note that this definition won't actually compile; you probably need a
Monad instance and a newtype to get this to work properly (see
http://www.cs.nott.ac.uk/~gmh/Parsing.lhs for a working version of the
same code).

    Shachaf


More information about the Haskell-Cafe mailing list