[commit: ghc] master: Restore invariant in `Data (Ratio a)` instance (3df429e)

git at git.haskell.org git at git.haskell.org
Thu Jan 22 21:35:45 UTC 2015


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/3df429e29b6fabda12af71091ba4ad1360f49b44/ghc

>---------------------------------------------------------------

commit 3df429e29b6fabda12af71091ba4ad1360f49b44
Author: Herbert Valerio Riedel <hvr at gnu.org>
Date:   Wed Jan 21 08:21:36 2015 +0100

    Restore invariant in `Data (Ratio a)` instance
    
    (2nd attempt, this time leaving the `Constr` using `":%"`)
    
    The Data instance for `Ratio` just uses the raw `:%` constructor and
    doesn't check that the result is reduced to normal form.
    
    The fix is to add back the `Integral` constraint on the Data
    instance (which was dropped in c409b6f30373535) and to use `%` rather
    than `:%` in the `gfoldl` and `gunfold` implementation.
    
    This restores the invariant and matches the behavior of "virtual
    constructors" we've used to patch up such problems elsewhere.
    
    This addresses #10011
    
    Reviewed By: ekmett, austin
    
    Differential Revision: https://phabricator.haskell.org/D625


>---------------------------------------------------------------

3df429e29b6fabda12af71091ba4ad1360f49b44
 libraries/base/Data/Data.hs | 6 +++---
 libraries/base/changelog.md | 2 ++
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/libraries/base/Data/Data.hs b/libraries/base/Data/Data.hs
index dce610b..6961b25 100644
--- a/libraries/base/Data/Data.hs
+++ b/libraries/base/Data/Data.hs
@@ -1064,10 +1064,10 @@ ratioConstr = mkConstr ratioDataType ":%" [] Infix
 ratioDataType :: DataType
 ratioDataType = mkDataType "GHC.Real.Ratio" [ratioConstr]
 
-instance Data a => Data (Ratio a) where
-  gfoldl k z (a :% b) = z (:%) `k` a `k` b
+instance (Data a, Integral a) => Data (Ratio a) where
+  gfoldl k z (a :% b) = z (%) `k` a `k` b
   toConstr _ = ratioConstr
-  gunfold k z c | constrIndex c == 1 = k (k (z (:%)))
+  gunfold k z c | constrIndex c == 1 = k (k (z (%)))
   gunfold _ _ _ = error "Data.Data.gunfold(Ratio)"
   dataTypeOf _  = ratioDataType
 
diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md
index 83ae5e4..0d7ebcf 100644
--- a/libraries/base/changelog.md
+++ b/libraries/base/changelog.md
@@ -140,6 +140,8 @@
 
   * Add `callocArray` and `callocArray0` to `Foreign.Marshal.Array`. (#9859)
 
+  * Restore invariant in `Data (Ratio a)` instance (#10011)
+
 ## 4.7.0.2  *Dec 2014*
 
   * Bundled with GHC 7.8.4



More information about the ghc-commits mailing list