[Haskell-cafe] Correct parsers for bounded integral values
Jeff Clites
jclites at mac.com
Fri Aug 8 00:28:43 UTC 2025
> On Aug 3, 2025, at 12:17 PM, Stefan Klinger <haskell at stefan-klinger.de> wrote:
>
> Jeff Clites via Haskell-Cafe (2025-Jul-21, excerpt):
>> I think you will find, though, that `read @Word8` is intended to
>> match the behavior of `(fromInteger @Word8) . (read @Integer)`. At
>> least, the `Int` case is specified in the Haskell Report (in the
>> Prelude implementation).
>
> Sorry, I cannot find this. Would you have a URL and a line number for
> me? I'm really sorry my google-foo seems to be insufficient.
This part is what I was thinking of:
https://www.haskell.org/onlinereport/haskell2010/haskellch9.html#verbatim-307
That's the `Read` instance for `Int`, which defines `readsPrec` (which is used by `reads`, which is used by `read`, also defined there). It uses `fromInteger`.
This is the section of the spec with the Standard Prelude. (It says it's serving as a specification, though not requiring the actual implementation to be what's given here.)
That's only specifically about `Int`. The limited-width types are in these two sections:
https://www.haskell.org/onlinereport/haskell2010/haskellch18.html
https://www.haskell.org/onlinereport/haskell2010/haskellch23.html
This is the part of the spec about the standard libraries, though it doesn't show the full implementations. I don't know if they are supplied elsewhere (I've looked and can't find them), though I don't think that that means that their behavior is unspecified. It does mention, "All arithmetic is performed modulo 2^n, where n is the number of bits in the type." So it doesn't supply the `Read` implementations for these other types and it doesn't directly say that `Read` has to wrap even though arithmetic operations do. But I would expect that the intention is that they act consistently with `Int`.
When I said, "I think you will find..." in my original message, what I really meant was that if you file a bug report I think the reply will be that the behavior is as intended (specifically, to match `Int`). But who knows.
Enjoy!
Jeff
More information about the Haskell-Cafe
mailing list