[commit: ghc] ghc-8.2: Override default `clearBit` method impl for `Natural` (3de07dc)

git at git.haskell.org git at git.haskell.org
Mon Oct 16 21:31:28 UTC 2017


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

On branch  : ghc-8.2
Link       : http://ghc.haskell.org/trac/ghc/changeset/3de07dcf221548e73c3623a085cae99d0b519c8b/ghc

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

commit 3de07dcf221548e73c3623a085cae99d0b519c8b
Author: Herbert Valerio Riedel <hvr at gnu.org>
Date:   Mon Oct 16 22:12:03 2017 +0200

    Override default `clearBit` method impl for `Natural`
    
    The default implementation of `clearBit` is in terms of
    `complement`. However, `complement` is not well-defined
    for `Natural` and this consequently renders the default
    implementation of `clearBit` dysfunctional.
    
    This implements `clearBit` in terms of `testBit`
    and `setBit` which are both well-defined for `Natural`s.
    
    This addresses #13203
    
    (cherry picked from commit 5984a698fc2974b719365a9647a7cae1bed51eec)


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

3de07dcf221548e73c3623a085cae99d0b519c8b
 libraries/base/GHC/Natural.hs | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/libraries/base/GHC/Natural.hs b/libraries/base/GHC/Natural.hs
index 0e5abc7..39da862 100644
--- a/libraries/base/GHC/Natural.hs
+++ b/libraries/base/GHC/Natural.hs
@@ -346,6 +346,11 @@ instance Bits Natural where
 
     -- TODO: setBit, clearBit, complementBit (needs more primitives)
 
+    -- NB: We cannot use the default impl of 'clearBit' due to
+    -- 'complement' not being well-defined for 'Natural' (c.f. #13203)
+    clearBit x i | testBit x i = complementBit x i
+                 | otherwise   = x
+
     shiftL n           0 = n
     shiftL (NatS# 0##) _ = NatS# 0##
     shiftL (NatS# 1##) i = bit i



More information about the ghc-commits mailing list