On Tue, Apr 12, 2005 at 11:13:18PM -0400, John Macdonald wrote: > On Tuesday 12 April 2005 22:36, Patrick R. Michaud wrote: > > It's entirely possible that I have my mathematics messed up here, > > but C<xor> doesn't seem to me to be entirely associative, at least not > > as I commonly think of associativity. Consider > > > > $x = (((0 xor 2) xor 4) xor 6); # ((a xor b) xor c) == 6 > > > > $y = ((0 xor 2) xor (4 xor 6)); # (a xor (b xor c)) == 2 > > You're both messed up your math and you've mixed the boolean > xor function up with the extension of it to an array of bits that is used > for integer xor. When you xor integers, you are really xor'ing each > of the bit positions in the integers in parallel, using the boolean xor > function for each bit independently.
No, I'm afraid I haven't messed up my math. Integer-xor is &infix:<+^> . Array-of-bits-xor is &infix:<~^> . I'm specifically talking about &infix:<xor>, which "Perl 6 and Parrot Essentials" says (p. 36): The XOR relation ... returns the value of the true operand if any one operand is true, and a false value if both are true or neither is true. It does *not* say that XOR returns "true", it says it returns "the value of the true operand". (Note that this is different from p5, which at least on my system does return true/false for C<xor>.) > You can see the same boolean/integer difference in perl5 with: > > (4&2) is 0 > (4&&2) is 4 Yes, I know this difference. And in perl 6, the difference is (4 +& 2) is 0 (4 && 2) is 4 (4 +^ 2) is 6 (4 ^^ 2) is false > Meanwhile, with your example (using integer xor): > (((0 xor 2) xor 4) xor 6) > is ((2 xor 4) xor 6) > is (6 xor 6) > is 0 Integer xor is (((0 +^ 2) +^ 4) +^ 6) == 0. > The same example, using boolean xor: > (((0 xor 2) xor 4) xor 6) > --> (((false xor true) xor true) xor true) > --> ((true xor true) xor true) > --> (false xor true) > --> true Boolean xor is (((0 ?^ 2) ?^ 4) ?^ 6) == 1. > While boolean xor and integer xor give different results (just as boolean > and integer AND gave different results in my example above) they are > both perfectly associative. Yes, boolean xor and integer xor are associative. But that's not what I was talking about. In particular: (((0 xor 2) xor "four") xor "six") --> ((2 xor "four") xor "six") --> (false xor "six") --> "six" > The issue for perl, in the boolean xor case, is which particular true value > will be returned The &infix:<xor> operator in perl 6 is not a "boolean" operator, it's a low-level "logical" operator, same as &infix:<and> and &infix:<or>. And C<xor> returns one of its arguments (or "false"), again the same that C<and> and C<or> do. And within these constraints, I don't believe that C<xor> is mathematically associative according to any of the definitions offerred thus far. Pm