[Haskell-cafe] Parsec combinator like Prolog's cut operator?

Erik de Castro Lopo mle+hs at mega-nerd.com
Wed Jun 30 05:48:01 EDT 2010


Stephen Tetley wrote:

> Malcolm Wallace describes a commit combinator in the paper "Partial
> parsing: combining choice with commitment" which sounds like what you
> would want. It is implemented for Polyparse rather than Parsec though.

Yes, I can see how that might help on some kinds of data. Thanks.

> If your Parsec parser uses try because it is not especially
> left-factored, one extra combinator I have found useful for
> left-factoring on the cheap is optionalSuffix:
> 
> optionalSuffix :: (a -> c)  -> (a -> b -> c)  -> Parser a -> Parser b -> Parser c
> optionalSuffix f g pbody psuffix = do
>    a <- pbody
>    mb_b <- optionMaybe psuffix
>    return $ maybe (f a) (\b -> g a b) mb_b

Funnily enough, while this function didn't help me directly, it did
inspire me to take yet another look at the problem code (I've made
at least 4 or 5 attempts to fix it over the last year). Revisiting
the problem now, with your code in mind, I came up with a solution
in about 5 minutes.

Now that I've fixed it, it seems so obvious. That led me to wonder
if there was a Parsec best-practices document. Does such a thing
exist?

Cheers,
Erik
-- 
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/


More information about the Haskell-Cafe mailing list