<p dir="ltr">So should we consider it a bug in attoparsec? And should somebody fix it?</p>
<div class="gmail_quote">On 11 Jun 2016 10:04, "Erik Hesselink" <<a href="mailto:hesselink@gmail.com">hesselink@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">OK, so you're using attoparsec (through aeson). That's valuable<br>
information would have been useful to include in your initial email.<br>
<br>
After a bit of experimenting and reading the docs, I think the problem<br>
is that attoparsec's Alternative instance isn't completely correct.<br>
>From the docs, Alternative is supposed to be "a monoid on applicative<br>
functors". The Monoid laws say, among other things, that `mappend x<br>
mempty = x`. For Applicative, this would mean `x <|> empty = x`.<br>
However, in attoparsec:<br>
<br>
λ parse (fail "boom" <|> empty) "hello"<br>
Fail "hello" [] "Failed reading: empty"<br>
<br>
In parsec, on the other hand:<br>
<br>
λ parse (fail "boom" <|> empty) "" "hello"<br>
Left (line 1, column 1):<br>
boom<br>
<br>
So I don't think changing Applicative or asum is the way to go here.<br>
<br>
Erik<br>
<br>
On 11 June 2016 at 07:15, Юрий Сыровецкий (Yuriy Syrovetskiy)<br>
<<a href="mailto:cblp@cblp.su">cblp@cblp.su</a>> wrote:<br>
> λ> :show imports<br>
> import Prelude -- implicit<br>
> import Data.Aeson.Types<br>
> import Data.Foldable<br>
> import Control.Applicative<br>
> λ> parse (\v -> withObject "Object" (\_ -> pure "OK") v <|> fail<br>
> "Expected object") Null<br>
> Error "Expected object"<br>
> λ> parse (\v -> asum [withObject "Object" (\_ -> pure "OK") v, fail<br>
> "Expected object"]) Null<br>
> Error "empty"<br>
><br>
> Maybe instance Alternative Parser should be changed to ignore "empty"?<br>
><br>
> 2016-06-10 20:01 GMT+03:00 Oleg Grenrus <<a href="mailto:oleg.grenrus@iki.fi">oleg.grenrus@iki.fi</a>>:<br>
>> If you aren’t scared of the dependencies: semigroupoids has asum1 (yet it’s easy to write yourself)<br>
>><br>
>> <a href="https://s3.amazonaws.com/haddock.stackage.org/lts-6.2/semigroupoids-5.0.1/Data-Semigroup-Foldable.html#v:asum1" rel="noreferrer" target="_blank">https://s3.amazonaws.com/haddock.stackage.org/lts-6.2/semigroupoids-5.0.1/Data-Semigroup-Foldable.html#v:asum1</a><br>
>><br>
>> λ Prelude Data.Semigroup.Foldable Data.List.NonEmpty > asum1 ([Right 1, Right 2, Left "error"] :: NonEmpty (Either String Int))<br>
>> Right 1<br>
>> λ Prelude Data.Semigroup.Foldable Data.List.NonEmpty > asum1 ([Left "error"] :: NonEmpty (Either String Int))<br>
>> Left "error"<br>
>><br>
>> - Oleg<br>
>><br>
>>> On 10 Jun 2016, at 17:37, Юрий Сыровецкий (Yuriy Syrovetskiy) <<a href="mailto:cblp@cblp.su">cblp@cblp.su</a>> wrote:<br>
>>><br>
>>> Hello<br>
>>><br>
>>> I want to define some parser such way:<br>
>>><br>
>>> myParser = tryA <|> tryB <|> fail "input must be either A or B"<br>
>>><br>
>>> It works. But then I want to rewrite it with asum:<br>
>>><br>
>>> myParser = asum [tryA, tryB, fail "must be A or B"]<br>
>>><br>
>>> It works, but the wrong way. Instead of my error it writes "empty".<br>
>>> Just "empty".<br>
>>><br>
>>> It is so because in base library<br>
>>><br>
>>> asum = foldr (<|>) empty<br>
>>><br>
>>> What if it was defined<br>
>>><br>
>>> asum [] = empty<br>
>>> asum [x:xs] = x <|> asum xs<br>
>>><br>
>>> It would help me with my parser. But what can this break? Why isn't<br>
>>> this done yet?<br>
>>><br>
>>> --<br>
>>> Yuriy Syrovetskiy, <a href="http://cblp.su" rel="noreferrer" target="_blank">http://cblp.su</a><br>
>>> _______________________________________________<br>
>>> Haskell-Cafe mailing list<br>
>>> <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
>>> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
>>><br>
>><br>
><br>
><br>
><br>
> --<br>
> Yuriy Syrovetskiy, <a href="http://cblp.su" rel="noreferrer" target="_blank">http://cblp.su</a><br>
> _______________________________________________<br>
> Haskell-Cafe mailing list<br>
> <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div>