[Haskell-cafe] Why is Bool no instance of Num and Bits?

Stephan Friedrichs deduktionstheorem at web.de
Fri May 8 10:36:41 EDT 2009


Hi!

When looking for an xor function, I found one in Data.Bits but couldn't
use it for Bool, because Bool is no instance of Bits and of Num (which
would be necessary, because it's "class (Num b) => Bits b"). My question
is: Why not?

We could declare

instance Num Bool where
    (+) False = id
    (+) True  = not

    (*) True  True = True
    (*) _     _    = False

    (-) = (+)

    negate      = id
    abs         = id
    signum      = const True
    fromInteger = not . even

which basically implements the field with 2 elements and

instance Bits Bool where
    bitSize  = const 1
    isSigned = const False

    (.&.) = (&&)
    (.|.) = (||)
    xor   = (+)

    complement = not

    shift  = const
    shiftL = const
    shiftR = const

    rotate  = const
    rotateL = const
    rotateR = const

    bit = (==0)

    setBit _ 0 = True
    setBit b _ = b

    clearBit _ 0 = False
    clearBit b _ = b

    complementBit b 0 = not b
    complementBit b _ = b

    testBit b 0 = b
    testBit _ _ = False

quite trivial... Why is this not part of base? Or am I missing something?

//Stephan


-- 

Früher hieß es ja: Ich denke, also bin ich.
Heute weiß man: Es geht auch so.

 - Dieter Nuhr


More information about the Haskell-Cafe mailing list