Parsing Ratio with zero denominator

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Parsing Ratio with zero denominator

Dannyu NDos
Shouldn't the parser be pfail rather than throwing an error?

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Parsing Ratio with zero denominator

Dannyu NDos
I mean, shouldn't there be a guard?

instance (Integral a, Read a) => ReadPrec (Ratio a) where
readPrec = parens . prec 7 $ do
n <- step readPrec
lift (expect (Symbol "%"))
d <- step readPrec
guard (0 /= d)
return (n % d)

2020년 5월 31일 (일) 오후 12:13, Dannyu NDos <[hidden email]>님이 작성:
Shouldn't the parser be pfail rather than throwing an error?

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Parsing Ratio with zero denominator

Sven Panne-2
Am So., 31. Mai 2020 um 10:06 Uhr schrieb Dannyu NDos <[hidden email]>:
I mean, shouldn't there be a guard? [...]

In general, I'm heavily opposed against any change which can introduce subtle changes in the semantics without any warning/compilation error. Consider e.g. length (readsPrec 1 "2 % 0" :: [(Rational, String)]). Currently, this returns 1, which will silently turn into 0 with your proposed change, at least if I see this correctly. Therefore: -100 for this proposal. :-}

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Parsing Ratio with zero denominator

Henning Thielemann
In reply to this post by Dannyu NDos

On Sun, 31 May 2020, Dannyu NDos wrote:

> I mean, shouldn't there be a guard?
>
> instance (Integral a, Read a) => ReadPrec (Ratio a) where
> readPrec = parens . prec 7 $ do
> n <- step readPrec
> lift (expect (Symbol "%"))
> d <- step readPrec
> guard (0 /= d)
> return (n % d)


I think you are right. A readPrec parser should only generate parser
errors and should be total. I do not know if anyone relies on the current
behavior. I suspect that most users are not aware of the problem and their
programs will be aborted in case of zero denominator where they shouldn't.
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Parsing Ratio with zero denominator

David Feuer
In reply to this post by Dannyu NDos
Yes, I think you're right.

On Sat, May 30, 2020, 11:13 PM Dannyu NDos <[hidden email]> wrote:
Shouldn't the parser be pfail rather than throwing an error?
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries