Reputation: 7825
I've been coding my own bit-vectors (represented as strict tuples over Word64
values) as an exercise in time and space optimizations and wanted to define instances of the Bits
typeclass for them, but then I noticed that the class declaration for Bits
is defined as follows:
class Num a => Bits a
To workaround this, I'm defining a fake Num
instance as well, made up mostly of error
-valued functions as a hack, but this doesn't feel right...
What was the rationale of depending on the Num
type-class for bit-wise operations? Wouldn't it make more sense to be able to have Bits
instances independen from having to declare a Num
instance as well?
Upvotes: 6
Views: 230
Reputation: 137947
Bits
depends on Num
, because Num
provides numeric literals and negation, which are used in the default methods for Bits
, like so:
bit :: Int -> a
bit i = 1 `shiftL` i
testBit :: a -> Int -> Bool
x `testBit` i = (x .&. bit i) /= 0
If there were no default methods, you could imagine getting away without the Num
constraint.
Upvotes: 4