Bring back the XOR

174 views
Skip to first unread message

vgermrk

unread,
Mar 10, 2008, 6:30:10 AM3/10/08
to sage-devel
Since the preparser replaces "^" with "**" (which is good!),
i want a way to access the python-buildin-XOR again.

I suggested in IRC that the preparser should also replace "xor" with
"^",
so that one can do "5 xor 3".
But since i did not convince everybody (on IRC), let's discuss here
about it.

So what do you think?

-vgermrk-

Stephen Forrest

unread,
Mar 10, 2008, 7:24:45 AM3/10/08
to sage-...@googlegroups.com

What are the main counterarguments?  I expect the main issue is with avoiding excessive divergence from Python syntax, though I think this is a special case as Sage has already overridden Python's native xor binary operator.  Are there operator precedence issues here?

Having "xor" as a binary operator does fit with the native "and" and "or", and Sage programmers are probably more likely than random programmers to want to make use of "xor" and "implies" in their conditional logic.

For the record, Maple has all of "and", "or", "xor", and "implies" as binary operators.

Steve

David Joyner

unread,
Mar 10, 2008, 7:34:26 AM3/10/08
to sage-...@googlegroups.com
Another solution (avoiding the preparer) is to simply define

sage: def xor(a,b): return eval("%s^%s"%(a,b))
....:
sage: xor(1,0)
1
sage: xor(1,1)
0

which mimics the native Python behavior:

Python 2.5.1 (r251:54863, Oct 5 2007, 13:50:07)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1^0
1
>>> 1^1
0

David Roe

unread,
Mar 10, 2008, 7:37:02 AM3/10/08
to sage-...@googlegroups.com
Note that the function that is called by "^" is __xor__.  So you can already do a.__xor__(b).  If you want to write the function that David describes, calling a.__xor__(b) is better than using eval.
David

John Cremona

unread,
Mar 10, 2008, 1:41:13 PM3/10/08
to sage-...@googlegroups.com
I don't suppose ^^ (for xor) will have much support....

John


--
John Cremona

Robert Bradshaw

unread,
Mar 10, 2008, 2:09:37 PM3/10/08
to sage-...@googlegroups.com

"and" and "or" in python are not binary operators, they are control
flow keywords. (Specifically, they short circuit).

I think we should have something. 10r is a "raw" python integer, 2.3r
a python float, but ^r looks like "raising to the r." ^^ seems like
the most reasonable syntax.

- Robert

Carl Witty

unread,
Mar 10, 2008, 2:54:26 PM3/10/08
to sage-devel
On Mar 10, 4:24 am, "Stephen Forrest" <stephen.forr...@gmail.com>
wrote:
I kind of liked " xor " until you said this...

"and" and "or" are control flow operators, and work on booleans. We
want an xor that is a bitwise operation on integers; I now think that
using the word "xor" would be confusing.

(Note that Maple's xor operator works on booleans, and gives errors on
integers.)

Note that "^^" does have the same problem: in C/C++/etc., |,&,^ are
bitwise operators and ||,&& are boolean operators; somebody looking at
^^ could think that it's supposed to be a boolean operator. Even so,
I prefer "^^" to "xor".

Carl

William Stein

unread,
Mar 10, 2008, 2:59:42 PM3/10/08
to sage-...@googlegroups.com

+1
Unless somebody thinks of something better, I like ^^ as well.

William

boo...@u.washington.edu

unread,
Mar 10, 2008, 3:29:11 PM3/10/08
to sage-...@googlegroups.com


I don't like ^^. Since ~ is a unary operator, we could safely use it as a binary operator, as well. I don't really like a~b to denote xor... but I like it better than ^^.


John Cremona

unread,
Mar 10, 2008, 3:40:13 PM3/10/08
to sage-...@googlegroups.com
I was only half serious when I suggested ^^, and was surprised that
others quote liked it. But I *really* don't like ~. The only similar
symbol ever used in logic means "not" (negation) so this is completely
unintuitive.

John


--
John Cremona

Robert Bradshaw

unread,
Mar 10, 2008, 4:50:10 PM3/10/08
to sage-...@googlegroups.com
On Mar 10, 2008, at 12:40 PM, John Cremona wrote:

> I was only half serious when I suggested ^^, and was surprised that
> others quote liked it.

I don't think it's that pretty, but I think it's the best of many
other bad alternatives, and it at least somewhat ties in to the
python choice of xor. Also, I don't see xor being used very often,
and so is an allowable wart to let 2^5 = 32 (which is used all over).

> But I *really* don't like ~. The only similar
> symbol ever used in logic means "not" (negation) so this is completely
> unintuitive.

I would echo this. On another note, a~b is much a harder to preparse
as it must detect whether ~ is being used as a binary or unary
operation.

vgermrk

unread,
Mar 17, 2008, 5:33:12 AM3/17/08
to sage-devel
Reply all
Reply to author
Forward
0 new messages