[commit: ghc] master: Override default `clearBit` method impl for `Natural` (5984a69)

git at git.haskell.org git at git.haskell.org
Mon Oct 16 20:19:39 UTC 2017


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

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/5984a698fc2974b719365a9647a7cae1bed51eec/ghc

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

commit 5984a698fc2974b719365a9647a7cae1bed51eec
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


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

5984a698fc2974b719365a9647a7cae1bed51eec
 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 99cfb8f..edffb10 100644
--- a/libraries/base/GHC/Natural.hs
+++ b/libraries/base/GHC/Natural.hs
@@ -345,6 +345,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