[Haskell-cafe] Parsec question

Tomasz Zielonka tomasz.zielonka at gmail.com
Thu Jun 21 02:28:04 EDT 2007

On Thu, Jun 21, 2007 at 03:34:54PM +0930, Levi Stephen wrote:
> Is there a way through combining types/parsers that the double do
> block in primary could be avoided?
> I understand it's necessary right now because the parsers identifier
> and stringLiteral return different types, so I can't just write:
> >i <- identifier <|> stringLiteral

You can use the fact that (GenParser tok st) is a monad and use liftM:

    i <- liftM PrimaryIdentifier identifier <|> liftM PrimaryLiteral stringLiteral

I often find it convenient to use "choice" instead of <|> for long more
complicated alternatives, for example like this:

primary =
        [ do
            i <- identifier
            return $ PrimaryIdentifier i
        , do
            l <- stringLiteral
            return $ PrimaryLiteral l

> So, I'm not sure whether my types need work,

I have a feeling that Identifier and Literal could just be type
synonyms, because newtype's don't seem to be neccesary or beneficial
here.  I could be wrong though - after all, I don't know your intentions
and the rest of the program.

Best regards

More information about the Haskell-Cafe mailing list