Hello,

Here is a quick description of what is below: Subclasses of Element
complain that no sorting algorithm is defined even when all the rich
comparison methods have been implemented. Bug?

In the code sample below, C is a class that inherits from Element and
implements all the rich comparison methods.

{{{
sage: class C(sage.structure.element.Element):
...       def __init__(self, a):
...           self.a = a
...       def __repr__(self):
...           return str(self.a)
...       def __eq__(self, other):
...           return self.a == other.a
...       def __ne__(self, other):
...           return self.a != other.a
...       def __lt__(self, other):
...           return self.a < other.a
...       def __le__(self, other):
...           return self.a <= other.a
...       def __gt__(self, other):
...           return self.a > other.a
...       def __ge__(self, other):
...           return self.a >= other.a
}}}

In theory, the cmp function should use the rich comparison methods for
comparisions. However, since __cmp__ has also been implemented (it is
implemented by Element), cmp bypasses all the rich comparison methods and
calls __cmp__ directly. This leads to an error since Element requires
subclasses to define a sorting algorithm:

{{{
sage: a = C('a')
sage: b = C('b')

sage: a < b
True

sage: cmp(a,b)
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
  File "element.pyx", line 648, in
sage.structure.element.Element.__cmp__ (sage/structure/element.c:6064)
  File "element.pyx", line 561, in sage.structure.element.Element._cmp
(sage/structure/element.c:5135)
  File "element.pyx", line 663, in
sage.structure.element.Element._cmp_c_impl
(sage/structure/element.c:6239)
NotImplementedError: BUG: sort algorithm for elements of 'None' not implemented
}}}

And this leads to problems with sorting via the cmp function:

{{{
sage: sorted([b,a])
[a, b]

sage: sorted([b,a], cmp=cmp)
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
  File "element.pyx", line 648, in
sage.structure.element.Element.__cmp__ (sage/structure/element.c:6064)
  File "element.pyx", line 561, in sage.structure.element.Element._cmp
(sage/structure/element.c:5135)
  File "element.pyx", line 663, in
sage.structure.element.Element._cmp_c_impl
(sage/structure/element.c:6239)
NotImplementedError: BUG: sort algorithm for elements of 'None' not implemented
}}}

One can get around this by implementing __cmp__ as well as the rich
comparison methods, but I'm wondering whether it might be better for
Element to check for and use the rich comparison methods before freaking
out.

Is there a good reason (speed? efficiency?) for this behaviour? Should I
open a ticket?

(For the record, Nicolas tells me that the new category theory code does
not fix this issue.)

Franco

--

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to 
sage-devel-unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to