Exclusive-Or 是否形成布尔值的幺半群?



我为异或函数做了一个运算符,它看起来像这样:

op :: Integer -> Integer -> Maybe Integer
op x y
| x == 0 && y == 0 = Just 0
| x == 0 && y == 1 = Just 1
| x == 1 && y == 0 = Just 1
| x == 1 && y == 1 = Just 0
| otherwise = Nothing

我用 0 和 1 代替了 True 和 False,但这应该不会对结果产生影响。我读到它形成了一个幺半群,但我不明白为什么。结合性是显而易见的,不需要证明(我已经自己做了证明(,但是单位元素是什么,为什么?

编辑:这是一个没有数字的:

xor :: Bool -> Bool -> Bool
xor x y | x == True && y == False = True
| x == False && y == True = True
| otherwise = False

我在证明中犯了一个愚蠢的错误,因为amalloy说身份是错误的。这是完整的证据,我希望它现在是正确的。

mempty <> p = p
xor False p = p
-- For p = True:
xor False True = True
True = True
-- For p = False:
xor False False = False
False = False
-- Q.E.D

最新更新