我为xor函数做了一个运算符,看起来是这样的:
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
发布于 2017-07-06 10:01:39
我在证明中犯了一个愚蠢的错误,因为阿梅洛说这个身份是假的。这是完整的证明,我希望它现在是正确的。
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
https://stackoverflow.com/questions/44945116
复制相似问题