[Haskell-cafe] Parsec lookahead and <|>

Martijn van Steenbergen martijn at van.steenbergen.nl
Thu Aug 20 07:44:15 EDT 2009

Goedemiddag café,

Consider the following function, using parsec-3.0.0:

> la :: Parsec String () (Maybe Char)
> la = lookAhead (optionMaybe anyChar)

*Lookahead> parseTest (char 'a' <|> char 'b') "a"
*Lookahead> parseTest (char 'a' <|> char 'b') "b"
*Lookahead> parseTest (la *> char 'a' <|> char 'b') "a"
*Lookahead> parseTest (la *> char 'a' <|> char 'b') "b"
parse error at (line 1, column 2):
unexpected "b"
expecting "a"

The first three work fine and as expected, but the fourth example fails 
where I would expect success. I know <|> won't try the rhs if the lhs 
consumed input, but lookAhead's documentation promises not to consume 
any input. Is this a bug in Parsec or am I missing something?



More information about the Haskell-Cafe mailing list