diff options
author | Koz Ross <koz.ross@retro-freedom.nz> | 2021-01-22 17:19:32 +1300 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2021-01-23 15:30:37 -0500 |
commit | 97208613414106e493a586d295ca05393e136ba4 (patch) | |
tree | 30d508cb7f1eb699d70e6cf7c52effb19f630126 | |
parent | 1a3f3247ce050b1fb66a88bcd4d77ea74cacf507 (diff) | |
download | haskell-97208613414106e493a586d295ca05393e136ba4.tar.gz |
FiniteBits for some newtype instances, notes on why
-rw-r--r-- | libraries/base/Data/Bits.hs | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/libraries/base/Data/Bits.hs b/libraries/base/Data/Bits.hs index 67772df563..95105a4b3f 100644 --- a/libraries/base/Data/Bits.hs +++ b/libraries/base/Data/Bits.hs @@ -71,8 +71,14 @@ import GHC.Show -- >>> complement oneBits :: Word == zeroBits :: Word -- True -- +-- = Note +-- +-- The constraint on 'oneBits' is arguably too strong. However, as some types +-- (such as 'Natural') have undefined 'complement', this is the only safe +-- choice. +-- -- @since 4.16 -oneBits :: (Bits a) => a +oneBits :: (FiniteBits a) => a oneBits = complement zeroBits {-# INLINE oneBits #-} @@ -99,8 +105,12 @@ newtype And a = And { getAnd :: a } instance (Bits a) => Semigroup (And a) where And x <> And y = And (x .&. y) --- | @since 4.16 -instance (Bits a) => Monoid (And a) where +-- | This constraint is arguably too strong. However, +-- as some types (such as 'Natural') have undefined 'complement', this is the +-- only safe choice. +-- +-- @since 4.16 +instance (FiniteBits a) => Monoid (And a) where mempty = And oneBits -- | Monoid under bitwise inclusive OR. @@ -177,10 +187,18 @@ newtype Iff a = Iff { getIff :: a } Read -- ^ @since 4.16 ) --- | @since 4.16 -instance (Bits a) => Semigroup (Iff a) where +-- | This constraint is arguably +-- too strong. However, as some types (such as 'Natural') have undefined +-- 'complement', this is the only safe choice. +-- +-- @since 4.16 +instance (FiniteBits a) => Semigroup (Iff a) where Iff x <> Iff y = Iff . complement $ (x `xor` y) --- | @since 4.16 -instance (Bits a) => Monoid (Iff a) where +-- | This constraint is arguably +-- too strong. However, as some types (such as 'Natural') have undefined +-- 'complement', this is the only safe choice. +-- +-- @since 4.16 +instance (FiniteBits a) => Monoid (Iff a) where mempty = Iff oneBits |