Opened 11 months ago

Closed 6 months ago

#9240 closed bug (fixed)

"read . show ≡ id" not satisfied by Data.Fixed

Reported by: hvr Owned by: ekmett
Priority: normal Milestone: 7.10.1
Component: Core Libraries Version: 7.2.1
Keywords: Cc: hvr, ekmett, core-libraries-committee@…
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: #9231 Differential Revisions: Phab:D547

Description (last modified by hvr)

As pointed out in #9231, the desired property "read . show ≡ id" is not satisfied for all resolutions. For instance, consider the following example:

> data B7
> instance HasResolution B7 where resolution _ = 128

> 1.070 :: Fixed B7
1.062
> 1.062 :: Fixed B7
1.054

> read "1.070" :: Fixed B7
1.062
> read "1.062" :: Fixed B7
1.054

This behaviour can be reproduced all the way back to GHC 7.2.1

Change History (5)

comment:1 Changed 11 months ago by hvr

  • Description modified (diff)

comment:2 Changed 8 months ago by thoughtpolice

  • Component changed from libraries/base to Core Libraries
  • Owner set to ekmett

Moving over to new owning component 'Core Libraries'.

comment:3 Changed 6 months ago by thomie

  • Cc core-libraries-committee@… added
  • Differential Revisions set to Phab:D547
  • Status changed from new to patch

comment:4 Changed 6 months ago by Herbert Valerio Riedel <hvr@…>

In 7c38e985aa211ca44039c6d1db9fa13690749c59/ghc:

Make `read . show = id` for Data.Fixed (fix #9240)

The QuickCheck property now succeeds:

    prop :: Fixed B7 -> Bool
    prop a = read (show a) == a

This changes the Show instance for Fixed to round up, rather than down
when calculating a digit. This needs to happen because Read also
rounds down:

    data B7

    instance HasResolution B7 where
      resolution _ = 128

    1 / 128 = 0.0078125

    read "0.007" = (0.000 :: Fixed B7)

Here is an example of the change to Show:

    showFixed False (0.009 :: Fixed B7)

    -- Broken: "0.007"
    -- Fixed:  "0.008"

And now Read can continue to round down:

    read "0.008" = (0.0078125 :: Fixed B7)

Reviewed By: hvr, ekmett

Differential Revision: https://phabricator.haskell.org/D547

comment:5 Changed 6 months ago by hvr

  • Resolution set to fixed
  • Status changed from patch to closed
Note: See TracTickets for help on using tickets.