[Haskell-cafe] I think I discovered my first Monoid instance
Ben Franksen
ben.franksen at online.de
Tue Sep 29 09:33:58 UTC 2020
Am 29.09.20 um 10:27 schrieb Mario Lang:
> instance Monoid QuadBitboard where
> mempty = QBB 0 0 0 0
>
> -- | bitwise XOR
> instance Semigroup QuadBitboard where
> QBB b0 b1 b2 b3 <> QBB b0' b1' b2' b3' =
> QBB (b0 `xor` b0') (b1 `xor` b1') (b2 `xor` b2') (b3 `xor` b3')
>
> But maybe I am violating some laws
The Semigroup is okay, since 'xor' is indeed associative, and your
instance basically lifts it to 4-tuples.
The Monoid instance is wrong, though. There is no unit for 'xor'!
Cheers
Ben
More information about the Haskell-Cafe
mailing list