Why does `Bits` depend on` Num`?

I encoded my own bit vectors (being strict tuples over Word64 values) as an exercise in optimizing time and space and wanted to define instances of the Bits class for them, but then I noticed that the class declaration for Bits is defined as follows:

 class Num a => Bits a 

To get around this, I also define a fake Num instance, consisting mainly of error valued functions as a hack, but that doesn't seem right ...

What was the rationale for the Num type class dependency for bit operations? Wouldn't it make sense to be able to have Bits instances regardless of the need to declare a Num instance?

+6
source share
1 answer

Bits dependent on Num because Num provides the numeric literals and negation that are used in the default methods for Bits , for example:

 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 being able to leave without the Num limit.

+4
source

Source: https://habr.com/ru/post/891907/


All Articles