New to Haskell - Trying to create associative xor chart

196 views Asked by At

I'm trying to create a associate xor chart on Haskell, which to my understanding, should come out true in every instance. Though, when i call my function I get: [True, True, True, False, True, False, True, True]. Can anyone see the mistake I made?

bools = [True, False]

xor_assoc = [   ((r || (p || q)) && not (r || (p && q)))
             == ((p || (r || q)) && not (p || (r && q)))
            | r <- bools,
              p <- bools,
              q <- bools]
3

There are 3 answers

0
Satvik On

I dont know what you are doing exactly. But the way you are doing list comprehension, it will generate all possible 2^3 cases. And your predicate returns False in two of them.

When (p,q,r) = (False,False,True) then (r || (p && q)) becomes True hence lhs is False, whereas (p || (r && q)) is False and hence rhs is True.

Similar is the case with (True,False,False) in which lhs is True and rhs is False.

0
J. Abrahamson On

Your principle is wrong: (r || (p && q)) /= (p || (r && q)). Counter-example is (p, q, r) = (T, F, F) where we have

(F || (T && F)) /= (T || (F && F))
(F || F       ) /= (T || F       )
F               /= T

Notably, both (||) and (&&) are individually associative, but do not associate around one another.

0
Sassa NF On

Are you trying to implement r xor p xor q? That should have 4 True and 4 False.

[r /= (p /= q) | let bools = [True, False], r <- bools, p <- bools, q <- bools]

Or if you are proving xor is associative, then:

[(r /= (p /= q)) == ((r /= p) /= q) |
 let bools = [True, False], r <- bools, p <- bools, q <- bools]

This one does have all 8 True