[Haskell-cafe] Why is Bool no instance of Num and Bits?
Deniz Dogan
deniz.a.m.dogan at gmail.com
Fri May 8 10:40:58 EDT 2009
2009/5/8 Stephan Friedrichs <deduktionstheorem at web.de>:
> 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
Isn't "XOR" for booleans (/=)?
Deniz Dogan
More information about the Haskell-Cafe
mailing list