[Haskell-cafe] I think I discovered my first Monoid instance

Ben Franksen ben.franksen at online.de
Tue Sep 29 09:49:24 UTC 2020


Am 29.09.20 um 11:38 schrieb Henning Thielemann:
> 
> 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?

Sorry, i was confused. Indeed 0 is a unit. The Monoid instance is lawful.

Cheers
Ben



More information about the Haskell-Cafe mailing list