[Haskell-cafe] ANNOUNCE: Parsed 0.0.1
Mike Izbicki
mike at izbicki.me
Wed Apr 1 15:01:37 UTC 2015
For the Parsec parser
(many1 (string "a") <|> many1 (string "b")) >> string "c"
we have an equivalent Parsed parser given by:
(choice "many 'match a'" "many 'match b'" | match c)
Verifying it accepts your test case:
$ (choice "many 'match a'" "many 'match b'" | match c) <<< 'bc'
bc
$ echo $?
Indeed, you are correct about the slightly different semantics of
choice. It essentially automatically wraps its arguments in "try".
For those wanting the more traditional Alternative interface, I've
just uploaded a combinator called (<|>). We can use this combinator
exactly as you would in haskell (except that bash requires lots of
escaping):
$ (\(\<\|\>\) 'match ab' 'match a') <<< "a"
$ echo $?
2
$ (\(\<\|\>\) 'match ab' 'match a') <<< "ab"
ab
$ echo $?
0
Thanks for the bug report!
On Wed, Apr 1, 2015 at 4:48 AM, Tillmann Rendel
<rendel at informatik.uni-tuebingen.de> wrote:
> Hi again,
>
> I wrote:
>>
>> In parsec,
>>
>> (many1 (string "a") <|> many1 (string "b")) >> string "c"
>>
>> accepts "bc", but I don't see how the corresponding grammar can be
>> par-séd.
>
>
> Sorry, I think I was confused because your implementation looked like an
> attempt at unlimited backtracking to me. To compare with Parsec, it is
> better to treat your implementation as an attempt to implement Parsec's
> semantics. In that case, we should note that in Parsec,
>
> (string "ab" <|> string "a")
>
> rejects "a", but maybe your implementation would accept it?
>
>
> Tillmann
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
More information about the Haskell-Cafe
mailing list