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

Authored by puffnfresh on Dec 1 2014, 9:56 PM.

Details

Summary

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)
Test Plan

QuickCheck from GHCi

Diff Detail

Repository
rGHC Glasgow Haskell Compiler
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
puffnfresh updated this revision to Diff 1820.Dec 1 2014, 9:56 PM
puffnfresh retitled this revision from to Make `read . show = id` for Data.Fixed (fixes #9240).
puffnfresh updated this object.
puffnfresh edited the test plan for this revision. (Show Details)
puffnfresh updated the Trac tickets for this revision.
hvr accepted this revision.Dec 2 2014, 2:15 AM
hvr edited edge metadata.

LGTM

This revision is now accepted and ready to land.Dec 2 2014, 2:15 AM
ekmett accepted this revision.Dec 2 2014, 2:15 AM
ekmett added a reviewer: ekmett.
ekmett added a subscriber: ekmett.

Makes sense to me.

This revision was automatically updated to reflect the committed changes.