[Haskell-cafe] I think I discovered my first Monoid instance
Henning Thielemann
lemming at henning-thielemann.de
Tue Sep 29 09:38:47 UTC 2020
On Tue, 29 Sep 2020, Ben Franksen wrote:
> 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'!
Why should 0 not be the identity element?
More information about the Haskell-Cafe
mailing list