Exclusive-Or 是否形成 Bools 的幺半群?
Does the Exclusive-Or form a monoid for Bools?
我为异或函数创建了一个运算符,它看起来像这样:
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
我为异或函数创建了一个运算符,它看起来像这样:
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