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

git at git.haskell.org git at git.haskell.org
Wed Jan 21 18:30:03 UTC 2015


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

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/79b0d0e633af8302d2dd907663a4a231cd889b67/ghc

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

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

    Restore invariant in `Data (Ratio a)` instance
    
    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


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

79b0d0e633af8302d2dd907663a4a231cd889b67
 libraries/base/Data/Data.hs | 8 ++++----
 libraries/base/changelog.md | 2 ++
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/libraries/base/Data/Data.hs b/libraries/base/Data/Data.hs
index dce610b..4a6c8be 100644
--- a/libraries/base/Data/Data.hs
+++ b/libraries/base/Data/Data.hs
@@ -1059,15 +1059,15 @@ instance Data Word64 where
 ------------------------------------------------------------------------------
 
 ratioConstr :: Constr
-ratioConstr = mkConstr ratioDataType ":%" [] Infix
+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