150PLD-Fall2019 / Reading

3 stars 0 forks source link

<||> question #269

Open jakebennert opened 5 years ago

jakebennert commented 5 years ago

Is choice a comparable option instead of <||>? https://hackage.haskell.org/package/parsec-3.1.14.0/docs/Text-Parsec-Combinator.html

ChrisEPhifer commented 5 years ago

I don't believe so; our implementation of <||> uses try on the first parser, which says "if you fail, restore the input state to what it was before you consumed any input". From what I can tell, choice does not restore the input state after failure.

Here's the implementation of choice:

choice :: (Stream s m t) => [ParsecT s u m a] -> ParsecT s u m a
choice ps           = foldr (<|>) mzero ps

You could certainly roll your own version of choice that uses <||> instead of <|>, though.

ChrisEPhifer commented 5 years ago

Thinking about this a bit more, this will actually only effect you if any of the strings you're attempting to parse with the parsers given to choice consume inputs that share a prefix... I think if all the options are common-prefix-free, you should be totally fine to use choice instead of chained uses of <||>.