Ticket #11943: trac11943_mro_for_all_super_categories_combined.patch

File trac11943_mro_for_all_super_categories_combined.patch, 81.5 KB (added by SimonKing, 7 years ago)

Replaces the two other patches: Order the super categories of a category according to Python's mro. Internally replace (all_)super_categories by lazy attributes, to prevent a regression. Use _subcategory_hook_

  • doc/en/reference/misc.rst

    # HG changeset patch
    # User Simon King <simon.king@uni-jena.de>
    # Date 1319403311 -7200
    # Node ID 3a46a777c4f1cba138408b2131cb7db8f313e7f8
    # Parent  2608fdf58341170b43a95ca19236d0ce2e81057a
    #11943: Order the super categories of a category according to Python's method resolution order
    Introduce lazy-attribute versions of super_categories and all_super_categories, for
    getting a speed-up. Allow to use a shortpath in is_subcategory, by using _subcategory_hook_.
    Use subclass check on parent classes to determine subcategories.
    
    diff --git a/doc/en/reference/misc.rst b/doc/en/reference/misc.rst
    a b  
    88
    99   sage/misc/abstract_method
    1010   sage/misc/cachefunc
     11   sage/misc/c3
    1112   sage/misc/decorators
    1213   sage/misc/classgraph
    1314   sage/misc/dev_tools
  • module_list.py

    diff --git a/module_list.py b/module_list.py
    a b  
    10581058    Extension('sage.misc.cython_c',
    10591059              sources = ['sage/misc/cython_c.pyx']),
    10601060
     1061    Extension('sage.misc.c3',
     1062              sources = ['sage/misc/c3.pyx']),
     1063
    10611064    Extension('sage.misc.derivative',
    10621065              sources = ['sage/misc/derivative.pyx']),
    10631066
  • sage/categories/additive_magmas.py

    diff --git a/sage/categories/additive_magmas.py b/sage/categories/additive_magmas.py
    a b  
    3535
    3636    """
    3737
    38     @cached_method
    3938    def super_categories(self):
    4039        """
    4140        EXAMPLES::
  • sage/categories/affine_weyl_groups.py

    diff --git a/sage/categories/affine_weyl_groups.py b/sage/categories/affine_weyl_groups.py
    a b  
    4444        sage: TestSuite(C).run()
    4545    """
    4646
    47     @cached_method
    4847    def super_categories(self):
    4948        r"""
    5049        EXAMPLES::
  • sage/categories/algebra_ideals.py

    diff --git a/sage/categories/algebra_ideals.py b/sage/categories/algebra_ideals.py
    a b  
    1212
    1313from sage.misc.cachefunc import cached_method
    1414from category_types import Category_ideal
     15from algebra_modules import AlgebraModules
     16from commutative_algebras import CommutativeAlgebras
    1517
    1618class AlgebraIdeals(Category_ideal):
    1719    """
     
    5860        """
    5961        return self.ambient()
    6062
    61     @cached_method
    6263    def super_categories(self):
    6364        """
     65        The category of algebra modules should be a super category of this category.
     66
     67        However, since algebra modules are currently only available over commutative rings,
     68        we have to omit it if our ring is non-commutative.
     69
    6470        EXAMPLES::
    6571
    6672            sage: AlgebraIdeals(QQ[x]).super_categories()
    6773            [Category of algebra modules over Univariate Polynomial Ring in x over Rational Field]
     74            sage: C = AlgebraIdeals(FreeAlgebra(QQ,2,'a,b'))
     75            sage: C.super_categories()
     76            []
     77
    6878        """
    69         from algebra_modules import AlgebraModules
    7079        R = self.algebra()
    71         return [AlgebraModules(R)]
     80        try:
     81            if R.is_commutative():
     82                return [AlgebraModules(R)]
     83        except (AttributeError,NotImplementedError):
     84            pass
     85        return []
  • sage/categories/algebra_modules.py

    diff --git a/sage/categories/algebra_modules.py b/sage/categories/algebra_modules.py
    a b  
    1212
    1313from sage.misc.cachefunc import cached_method
    1414from category_types import Category_module
     15from modules import Modules
    1516
    1617class AlgebraModules(Category_module):
    1718    """
     
    7778        """
    7879        return self.base_ring()
    7980
    80     @cached_method
    8181    def super_categories(self):
    8282        """
    8383        EXAMPLES::
     
    8686            [Category of modules over Univariate Polynomial Ring in x over Rational Field]
    8787        """
    8888        R = self.algebra()
    89         from modules import Modules
    9089        return [Modules(R)]
  • sage/categories/algebras.py

    diff --git a/sage/categories/algebras.py b/sage/categories/algebras.py
    a b  
    2222from sage.categories.dual import DualObjectsCategory
    2323from sage.categories.tensor import TensorProductsCategory, tensor
    2424from sage.categories.morphism import SetMorphism
     25from sage.categories.modules import Modules
    2526from sage.categories.rings import Rings
    2627from sage.misc.cachefunc import cached_method
    2728from sage.structure.sage_object import have_same_parent
     
    6667        from sage.rings.ring import Algebra
    6768        return isinstance(x, Algebra) and x.base_ring() == self.base_ring()
    6869
    69     @cached_method
    7070    def super_categories(self):
    7171        """
    7272        EXAMPLES::
     
    7575            [Category of rings, Category of modules over Integer Ring]
    7676        """
    7777        R = self.base_ring()
    78         from sage.categories.rings import Rings
    79         from sage.categories.modules import Modules
    8078        return [Rings(), Modules(R)]
    8179
    8280    class ParentMethods: # (Algebra):  # Eventually, the content of Algebra should be moved here
  • sage/categories/bialgebras.py

    diff --git a/sage/categories/bialgebras.py b/sage/categories/bialgebras.py
    a b  
    2929        sage: TestSuite(Bialgebras(ZZ)).run()
    3030    """
    3131
    32     @cached_method
    3332    def super_categories(self):
    3433        """
    3534        EXAMPLES::
  • sage/categories/bimodules.py

    diff --git a/sage/categories/bimodules.py b/sage/categories/bimodules.py
    a b  
    1313
    1414from sage.categories.category import Category
    1515from sage.misc.cachefunc import cached_method
     16from sage.categories.left_modules import LeftModules
     17from sage.categories.right_modules import RightModules
     18
    1619from sage.categories.rings import Rings
    1720_Rings = Rings()
    1821
     
    103106        from sage.misc.latex import latex
    104107        return "{%s}_{%s}_{%s}"%(Category._latex_(self), latex(self._left_base_ring), latex(self._right_base_ring))
    105108
    106     @cached_method
    107109    def super_categories(self):
    108110        """
    109111        EXAMPLES::
     
    113115        """
    114116        R = self.left_base_ring()
    115117        S = self.right_base_ring()
    116         from sage.categories.left_modules import LeftModules
    117         from sage.categories.right_modules import RightModules
    118118        return [LeftModules(R), RightModules(S)]
    119119
    120120    class ParentMethods:
  • sage/categories/cartesian_product.py

    diff --git a/sage/categories/cartesian_product.py b/sage/categories/cartesian_product.py
    a b  
    145145        """
    146146        return self
    147147
    148     def base(self):
     148    def base_ring(self):
    149149        """
    150         The base of a cartesian product is the base (usually a ring) of the underlying category.
     150        The base ring of a cartesian product is the base ring of the underlying category.
    151151
    152152        EXAMPLES::
    153153
    154             sage: Algebras(ZZ).CartesianProducts().base()
     154            sage: Algebras(ZZ).CartesianProducts().base_ring()
    155155            Integer Ring
    156156        """
    157         return self.base_category().base()
     157        return self.base_category().base_ring()
    158158
    159159# This is Category.CartesianProducts
    160160def CartesianProducts(self):
  • sage/categories/category.py

    diff --git a/sage/categories/category.py b/sage/categories/category.py
    a b  
    9898from sage.misc.abstract_method import abstract_method, abstract_methods_of_class
    9999from sage.misc.lazy_attribute import lazy_attribute
    100100from sage.misc.cachefunc import cached_method, cached_function
     101from sage.misc.c3 import C3_algorithm
     102from sage.misc.unknown import Unknown
    101103#from sage.misc.misc import attrcall
    102104
    103105from sage.structure.sage_object import SageObject
     
    135137
    136138    # Ensure associativity by flattening JoinCategory's
    137139    # Invariant: the super categories of a JoinCategory are not JoinCategories themselves
    138     categories = sum( (tuple(category.super_categories()) if isinstance(category, JoinCategory) else (category,)
     140    categories = sum( (tuple(category._super_categories) if isinstance(category, JoinCategory) else (category,)
    139141                       for category in categories), ())
    140142
    141143    # remove redundant categories which are super categories of others
     
    228230        sage: from sage.categories.all import Category
    229231        sage: from sage.misc.lazy_attribute import lazy_attribute
    230232        sage: class As (Category):
    231         ...       @cached_method
    232233        ...       def super_categories(self):
    233234        ...           return []
    234235        ...
     
    238239        ...           f = fA
    239240        ...
    240241        sage: class Bs (Category):
    241         ...       @cached_method
    242242        ...       def super_categories(self):
    243243        ...           return [As()]
    244244        ...
     
    247247        ...               return "B"
    248248        ...
    249249        sage: class Cs (Category):
    250         ...       @cached_method
    251250        ...       def super_categories(self):
    252251        ...           return [As()]
    253252        ...
     
    257256        ...           f = fC
    258257        ...
    259258        sage: class Ds (Category):
    260         ...       @cached_method
    261259        ...       def super_categories(self):
    262260        ...           return [Bs(),Cs()]
    263261        ...
     
    275273        True
    276274
    277275    We construct a parent in the category Ds() (that is an instance of
    278     Ds().parent_class, and check that it has access to all the
     276    Ds().parent_class), and check that it has access to all the
    279277    methods provided by all the categories, with the appropriate
    280278    inheritance order.
    281279    ::
     
    426424        EXAMPLES::
    427425
    428426            sage: class SemiprimitiveRings(Category):
    429             ...       @cached_method
    430427            ...       def super_categories(self):
    431428            ...           return [Rings()]
    432429            ...
     
    610607#         """
    611608#         return hash(self.__category) # Any reason not to use id?
    612609
     610    def _subcategory_hook_(self, category):
     611        """
     612        Quick subcategory check.
     613
     614        INPUT:
     615
     616        - ``category`` -- a category
     617
     618        OUTPUT:
     619
     620        - ``True``, if ``category`` is a subcategory of ``self``.
     621        - ``False``, if ``category`` is not a subcategory of ``self``.
     622        - ``Unknown``, if a quick check was not enough to determine
     623          whether ``category`` is a subcategory of ``self`` or not.
     624
     625        The aim of this method is to offer a framework to add cheap
     626        tests for subcategories. When doing
     627        ``category.is_subcategory(self)`` (note the reverse order of
     628        ``self`` and ``category``), this method is usually called
     629        first.  Only if it returns ``Unknown``, :meth:`is_subcategory`
     630        will build the list of super categories of ``category``.
     631
     632        This method need not to handle the case where ``category`` is
     633        ``self``; this is the first test that is done in
     634        :meth:`is_subcategory`.
     635
     636        This default implementation tests whether the parent class
     637        of ``category`` is a subclass of the parent class of ``self``.
     638        Currently (as of trac ticket #11900) this is a complete
     639        subcategory test. But this will change with trac ticket #11935.
     640
     641        EXAMPLE::
     642
     643            sage: Rings()._subcategory_hook_(Rings())
     644            True
     645        """
     646        return issubclass(category.parent_class, self.parent_class)
     647
    613648    def __contains__(self, x):
    614649        """
    615650        Membership testing
     
    721756    @abstract_method
    722757    def super_categories(self):
    723758        """
    724         Returns the immediate super categories of ``self``
     759        Returns the *immediate* super categories of ``self``
     760
     761        Every category should implement this method.
    725762
    726763        EXAMPLES::
    727764
     
    729766            [Category of monoids]
    730767            sage: Objects().super_categories()
    731768            []
     769
     770        .. note::
     771
     772            Mathematically speaking, the order of the super categories
     773            should be irrelevant. However, in practice, this order
     774            influences the result of :meth:`all_super_categories`, and
     775            accordingly of the method resolution order for parent and
     776            element classes. Namely, since ticket 11943, Sage uses the
     777            same `C3` algorithm for determining the order on the list
     778            of *all* super categories as Python is using for the
     779            method resolution order of new style classes.
     780
     781        .. note::
     782
     783            Whenever speed matters, developers are advised to use the
     784            lazy attribute :meth:`_super_categories` instead of
     785            calling this method.
    732786        """
    733787
    734     @cached_method
    735     def _all_super_categories_raw(self):
    736         """
    737         Return all super categories of this category, without removing duplicates.
     788    @lazy_attribute
     789    def _all_super_categories(self):
     790        r"""
     791        All the super categories of this category, including this category.
    738792
    739         TEST::
     793        Since :trac:`11943`, the order of super categories is
     794        determined by Python's method resolution order C3 algorithm.
    740795
    741             sage: Rngs()._all_super_categories_raw()
    742             [Category of commutative additive groups,
     796        .. seealso:: :meth:`all_super_categories`
     797
     798        .. note:: this attribute is likely to eventually become a tuple.
     799
     800        EXAMPLES::
     801
     802            sage: C = Rings(); C
     803            Category of rings
     804            sage: C._all_super_categories
     805            [Category of rings,
     806             Category of rngs,
     807             Category of commutative additive groups,
     808             Category of semirings,
    743809             Category of commutative additive monoids,
    744810             Category of commutative additive semigroups,
    745              Category of additive magmas, Category of sets,
    746              Category of sets with partial maps,
    747              Category of objects,
     811             Category of additive magmas,
     812             Category of monoids,
    748813             Category of semigroups,
    749814             Category of magmas,
    750815             Category of sets,
    751816             Category of sets with partial maps,
    752817             Category of objects]
     818        """
     819        return C3_algorithm(self,'_super_categories','_all_super_categories',False)
    753820
     821    @lazy_attribute
     822    def _all_super_categories_proper(self):
     823        r"""
     824        All the proper super categories of this category.
     825
     826        Since :trac:`11943`, the order of super categories is
     827        determined by Python's method resolution order C3 algorithm.
     828
     829        .. seealso:: :meth:`all_super_categories`
     830
     831        .. note:: this attribute is likely to eventually become a tuple.
     832
     833        EXAMPLES::
     834
     835            sage: C = Rings(); C
     836            Category of rings
     837            sage: C._all_super_categories_proper
     838            [Category of rngs,
     839             Category of commutative additive groups,
     840             Category of semirings,
     841             Category of commutative additive monoids,
     842             Category of commutative additive semigroups,
     843             Category of additive magmas,
     844             Category of monoids,
     845             Category of semigroups,
     846             Category of magmas,
     847             Category of sets,
     848             Category of sets with partial maps,
     849             Category of objects]
    754850        """
    755         all_categories = []
    756         for cat in self.super_categories():
    757             all_categories.append(cat)
    758             all_categories.extend(cat._all_super_categories_raw())
    759         return all_categories
     851        return self._all_super_categories[1:]
    760852
    761     @cached_method
    762     def all_super_categories(self, proper = False):
    763         r"""
    764         Returns a linear extension (topological sort) of all the
    765         (proper) super categories of this category, and cache the
    766         result.
     853    @lazy_attribute
     854    def _set_of_super_categories(self):
     855        """
     856        The frozen set of all proper super categories of this category.
     857
     858        .. note:: this is used for speeding up category containment tests.
     859
     860        .. seealso:: :meth:`all_super_categories`
     861
     862        EXAMPLES::
     863
     864            sage: Groups()._set_of_super_categories
     865            frozenset([...])
     866            sage: sorted(Groups()._set_of_super_categories, key=repr)
     867            [Category of magmas, Category of monoids, Category of objects, Category of semigroups,
     868             Category of sets, Category of sets with partial maps]
     869
     870        TESTS::
     871
     872            sage: C = HopfAlgebrasWithBasis(GF(7))
     873            sage: C._set_of_super_categories == frozenset(C._all_super_categories_proper)
     874            True
     875        """
     876        return frozenset(self._all_super_categories_proper)
     877
     878    def all_super_categories(self, proper=False):
     879        """
     880        Returns the list of all super categories of this category.
    767881
    768882        INPUT:
    769883
    770          - ``proper``: a boolean; defaults to False.  Whether to exclude this category.
     884         - ``proper`` -- a boolean (default: ``False``); whether to exclude this category.
    771885
    772         FIXME:
     886        Since :trac:`11943`, the order of super categories is
     887        determined by Python's method resolution order C3 algorithm.
    773888
    774         - make sure that this is compatible with the python algorithm
    775           for method resolution and make it O(n+m)
     889        .. note::
     890
     891            Whenever speed matters, the developers are advised to use
     892            instead the lazy attributes :meth:`_all_super_categories`,
     893            :meth:`_all_super_categories_proper`, or
     894            :meth:`_set_of_all_super_categories`, as
     895            appropriate. Simply because lazy attributes are much
     896            faster than any method.
    776897
    777898        EXAMPLES::
    778899
     
    806927             Category of sets,
    807928             Category of sets with partial maps,
    808929             Category of objects]
     930
     931            sage: Sets().all_super_categories()
     932            [Category of sets, Category of sets with partial maps, Category of objects]
     933            sage: Sets().all_super_categories(proper=True)
     934            [Category of sets with partial maps, Category of objects]
     935            sage: Sets().all_super_categories() is Sets()._all_super_categories
     936            True
     937            sage: Sets().all_super_categories(proper=True) is Sets()._all_super_categories_proper
     938            True
     939
    809940        """
    810         done = set()
    811         linear_extension = []
    812         for cat in reversed(self._all_super_categories_raw()):
    813             if not cat in done:
    814                 done.add(cat)
    815                 linear_extension.append(cat)
    816         linear_extension.reverse()
    817941        if proper:
    818             return linear_extension
    819         else:
    820             return [self] + linear_extension
     942            return self._all_super_categories_proper
     943        return self._all_super_categories
     944
     945    @lazy_attribute
     946    def _super_categories(self):
     947        """
     948        The immediate super categories of this category.
     949
     950        This lazy attributes caches the result of the mandatory method
     951        :meth:`super_categories` for speed.
     952
     953        Whenever speed matters, developers are advised to use this
     954        lazy attribute rather than calling :meth:`super_categories`.
     955
     956        .. note:: this attribute is likely to eventually become a tuple.
     957
     958        EXAMPLES::
     959
     960            sage: Rings()._super_categories
     961            [Category of rngs, Category of semirings]
     962        """
     963        return self.super_categories()
     964
     965    def _test_category_graph(self, **options):
     966        """
     967        Check that the category graph matches with Python's method resolution order
     968
     969        .. note::
     970
     971            By :trac:`11943`, the list of categories returned by
     972            :meth:`all_super_categories` is supposed to match with the
     973            method resolution order of the parent and element
     974            classes. This method checks this.
     975
     976        .. todo:: currently, this won't work for hom categories.
     977
     978        EXAMPLES::
     979
     980            sage: C = HopfAlgebrasWithBasis(QQ)
     981            sage: C.parent_class.mro() == [X.parent_class for X in C._all_super_categories] + [object]
     982            True
     983            sage: C.element_class.mro() == [X.element_class for X in C._all_super_categories] + [object]
     984            True
     985            sage: TestSuite(C).run()    # indirect doctest
     986
     987        """
     988        tester = self._tester(**options)
     989        tester.assert_(self.parent_class.mro() == [C.parent_class for C in self._all_super_categories] + [object])
     990        tester.assert_(self.element_class.mro() == [C.element_class for C in self._all_super_categories] + [object])
    821991
    822992#    def construction(self):
    823993#        return (self.__class__,)
     
    8611031        # super categories, but not on the base (except when the super categories depend
    8621032        # on the base). When that is done, calling the cached function will be needed again.
    8631033        #return dynamic_class("%s.parent_class"%self.__class__.__name__,
    864         #                     tuple(cat.parent_class for cat in self.super_categories()),
     1034        #                     tuple(cat.parent_class for cat in self.super_categories),
    8651035        #                     self.ParentMethods,
    8661036        #                     reduction = (getattr, (self, "parent_class")))
    8671037        return dynamic_class_internal.f("%s.parent_class"%self.__class__.__name__,
    868                              tuple(cat.parent_class for cat in self.super_categories()),
     1038                             tuple(cat.parent_class for cat in self._super_categories),
    8691039                             self.ParentMethods,
    8701040                             reduction = (getattr, (self, "parent_class")))
    8711041
     
    8921062        # super categories, but not on the base (except when the super categories depend
    8931063        # on the base). When that is done, calling the cached function will be needed again.
    8941064        #return dynamic_class("%s.element_class"%self.__class__.__name__,
    895         #                     (cat.element_class for cat in self.super_categories()),
     1065        #                     (cat.element_class for cat in self.super_categories),
    8961066        #                     self.ElementMethods,
    8971067        #                     reduction = (getattr, (self, "element_class"))
    8981068        #                     )
    8991069        return dynamic_class_internal.f("%s.element_class"%self.__class__.__name__,
    900                              tuple(cat.element_class for cat in self.super_categories()),
     1070                             tuple(cat.element_class for cat in self._super_categories),
    9011071                             self.ElementMethods,
    9021072                             reduction = (getattr, (self, "element_class")))
    9031073
     
    9171087
    9181088
    9191089    # Operations on the lattice of categories
    920     @cached_method
    9211090    def is_subcategory(self, c):
    9221091        """
    9231092        Returns True if self is naturally embedded as a subcategory of c.
     
    9621131        """
    9631132        if c is self:
    9641133            return True
    965         assert(isinstance(c, Category))
    966         if isinstance(c, JoinCategory):
    967             return all(self.is_subcategory(x) for x in c.super_categories())
    968         if isinstance(self, JoinCategory):
    969             for cat in self.super_categories():
    970                 if cat.is_subcategory(c):
    971                     return True
    972             return False
    973         try:
    974             cbase = c.base()
    975         except (AttributeError, TypeError, NotImplementedError):
    976             cbase = None
    977         if cbase is not None:
    978             try:
    979                 selfbase = self.base()
    980             except (AttributeError, TypeError, NotImplementedError):
    981                 selfbase = None
    982             if selfbase is not cbase:
    983                 return False
    984         return c in self.all_super_categories()
     1134        subcat_hook = c._subcategory_hook_(self)
     1135        if subcat_hook is Unknown:
     1136            return c in self._set_of_super_categories
     1137        return subcat_hook
    9851138
    9861139    def or_subcategory(self, category = None):
    9871140        """
     
    10411194        if isinstance(c, Category):
    10421195            return self.is_subcategory(c)
    10431196        else:
    1044             return any(isinstance(cat, c) for cat in self.all_super_categories())
     1197            return any(isinstance(cat, c) for cat in self._all_super_categories)
    10451198
    10461199    @cached_method
    10471200    def _meet_(self, other):
     
    10981251            # %time L = EllipticCurve('960d1').prove_BSD()
    10991252            return self
    11001253        else:
    1101             return Category.join(self._meet_(sup) for sup in other.super_categories())
     1254            return Category.join(self._meet_(sup) for sup in other._super_categories)
    11021255
    11031256    @staticmethod
    11041257    def meet(categories):
     
    11461299            Join of Category of groups and Category of commutative additive monoids
    11471300            sage: J.super_categories()
    11481301            [Category of groups, Category of commutative additive monoids]
    1149             sage: J.all_super_categories(proper = True)
     1302            sage: J.all_super_categories(proper=True)
    11501303            [Category of groups,
    11511304             Category of monoids,
    11521305             Category of semigroups,
     
    12351388        try: #if hasattr(self, "HomCategory"):
    12361389            return self.HomCategory(self)
    12371390        except AttributeError:
    1238             return Category.join((category.hom_category() for category in self.super_categories()))
     1391            return Category.join((category.hom_category() for category in self._super_categories))
    12391392
    12401393    def example(self, *args, **keywords):
    12411394        """
     
    13651518    """
    13661519    An abstract base class for all categories of homsets
    13671520
    1368     The hierarchy of homset categories is built in parallel to that of
    1369     their base categories (which is plain wrong!!!)
     1521    .. todo::
     1522
     1523        Get a consistent hierarchy of homset categories. Currently, it
     1524        is built in parallel to that of their base categories (which
     1525        is plain wrong!!!)
    13701526
    13711527    """
    13721528    def __init__(self, category, name=None):
     
    13771533         - ``category`` -- the category whose Homsets are the objects of this category.
    13781534         - ``name`` -- An optional name for this category.
    13791535
    1380         EXAMPLES::
     1536        EXAMPLES:
     1537
     1538        We need to skip one test, since the hierarchy of hom categories isn't
     1539        consistent yet::
    13811540
    13821541            sage: C = sage.categories.category.HomCategory(Rings()); C
    13831542            Category of hom sets in Category of rings
    1384             sage: TestSuite(C).run()
     1543            sage: TestSuite(C).run(skip=['_test_category_graph'])
    13851544        """
    13861545        Category.__init__(self, name)
    13871546        self.base_category = category
     
    14091568
    14101569        """
    14111570        from sage.categories.category_types import Category_over_base
    1412         for C in self.all_super_categories():
     1571        for C in self._all_super_categories_proper:
    14131572            if isinstance(C,Category_over_base):
    14141573                return C.base()
    14151574        raise AttributeError, "This hom category has no base"
    14161575
    1417     @cached_method
    14181576    def super_categories(self):
    14191577        """
    14201578        Returns the immediate super categories, as per :meth:`Category.super_categories`.
     
    14261584        """
    14271585        return Category.join(self.extra_super_categories() +
    14281586                             [category.hom_category()
    1429                               for category in self.base_category.super_categories()],
     1587                              for category in self.base_category._super_categories],
    14301588                             as_list=True)
    14311589    @cached_method
    14321590    def extra_super_categories(self):
     
    14581616        Join of Category of groups and Category of commutative additive monoids
    14591617        sage: J.super_categories()
    14601618        [Category of groups, Category of commutative additive monoids]
    1461         sage: J.all_super_categories(proper = True)
     1619        sage: J.all_super_categories(proper=True)
    14621620        [Category of groups, Category of monoids, Category of semigroups, Category of magmas, Category of commutative additive monoids, Category of commutative additive semigroups, Category of additive magmas, Category of sets, Category of sets with partial maps, Category of objects]
    14631621    """
    14641622
     
    14801638            sage: C = JoinCategory((Groups(), CommutativeAdditiveMonoids())); C
    14811639            Join of Category of groups and Category of commutative additive monoids
    14821640            sage: TestSuite(C).run()
     1641
    14831642        """
    14841643        assert(len(super_categories) >= 2)
    14851644        assert(all(not isinstance(category, JoinCategory) for category in super_categories))
     
    15011660        """
    15021661        return self._super_categories
    15031662
     1663    def _subcategory_hook_(self, category):
     1664        """
     1665        Returns whether ``category`` is a subcategory of this join category
     1666
     1667        INPUT:
     1668
     1669        - ``category`` -- a category.
     1670
     1671        .. note::
     1672
     1673            ``category`` is a sub-category of this join category if
     1674            and only if it is a sub-category of all super categories
     1675            of this join category.
     1676
     1677        EXAMPLE::
     1678
     1679            sage: QQ['x'].category().is_subcategory(Category.join([Rings(), VectorSpaces(QQ)]))  # indirect doctest
     1680            True
     1681        """
     1682        return all(category.is_subcategory(X) for X in self._super_categories)
     1683
    15041684    def _repr_(self):
    15051685        """
    15061686        Print representation.
     
    15171697            sage: Category.join((Sets().Facades(), Groups()))
    15181698            Category of facade groups
    15191699        """
    1520         categories = self.super_categories()
     1700        categories = self._super_categories
    15211701        from sets_cat import Sets
    15221702        if len(categories) == 2 and Sets().Facades() in categories:
    15231703            categories = list(categories)
    15241704            categories.remove(Sets().Facades())
    15251705            return "Category of facade %s"%(categories[0]._repr_object_names())
    1526         return "Join of %s"%(" and ".join(str(cat) for cat in self.super_categories()))
     1706        return "Join of %s"%(" and ".join(str(cat) for cat in categories))
  • sage/categories/category_types.py

    diff --git a/sage/categories/category_types.py b/sage/categories/category_types.py
    a b  
    1414#                  http://www.gnu.org/licenses/
    1515#*****************************************************************************
    1616
    17 from sage.misc.cachefunc import cached_method
    1817from sage.misc.latex import latex
    1918from category import Category
    20 from sage.categories.rings import Rings
    21 _Rings = Rings()
     19from objects import Objects
    2220
    2321####################################################################
    2422#   Different types of categories
     
    7674        """
    7775        return self.__object(x)
    7876
    79     @cached_method
    8077    def super_categories(self):
    8178        """
    8279        EXAMPLES::
     
    8885
    8986        check that this is what we want.
    9087        """
    91         from objects import Objects
    9288        return [Objects()]
    9389
    9490    def object(self):
     
    153149        from sage.rings.rational_field import QQ
    154150        return cls(QQ)
    155151
    156     @cached_method
    157152    def super_categories(self):
    158153        """
    159154        EXAMPLES::
     
    161156            sage: Sequences(ZZ).super_categories()
    162157            [Category of objects]
    163158        """
    164         from objects import Objects
    165159        return [Objects()]  # Almost FiniteEnumeratedSets() except for possible repetitions
    166160
    167161    def _call_(self, x):
     
    208202        Category.__init__(self, name)
    209203        self.__base = base
    210204
    211 
    212205    @classmethod
    213206    def an_instance(cls):
    214207        """
     
    260253#############################################################
    261254# Category of objects over some base ring
    262255#############################################################
     256class AbelianCategory(Category):
     257    def is_abelian(self):
     258        return True
     259
    263260class Category_over_base_ring(Category_over_base):
    264261    def __init__(self, base, name=None):
    265         assert base in _Rings, "base must be a ring"
     262        from sage.categories.rings import Rings
     263        assert base in Rings(), "base must be a ring"
    266264        Category_over_base.__init__(self, base, name)
    267265        self.__base = base
    268266
     
    273271        """
    274272        return self.base()
    275273
    276 class AbelianCategory(Category):
    277     def is_abelian(self):
    278         return True
    279 
    280274#############################################################
    281275# Category of objects in some ambient object
    282276#############################################################
     
    358352        sage: TestSuite(SimplicialComplexes()).run()
    359353    """
    360354
    361     @cached_method
    362355    def super_categories(self):
    363356        """
    364357        EXAMPLES::
     
    366359            sage: SimplicialComplexes().super_categories()
    367360            [Category of objects]
    368361        """
    369         from objects import Objects
    370362        return [Objects()] # anything better?
    371363
    372364#############################################################
     
    389381        sage: TestSuite(ChainComplexes(RationalField())).run()
    390382    """
    391383
    392     @cached_method
    393384    def super_categories(self):
    394385        """
    395386        EXAMPLES::
     
    397388            sage: ChainComplexes(Integers(9)).super_categories()
    398389            [Category of objects]
    399390        """
    400         from objects import Objects
    401391        return [Objects()] # anything better?
  • sage/categories/classical_crystals.py

    diff --git a/sage/categories/classical_crystals.py b/sage/categories/classical_crystals.py
    a b  
    5555        running ._test_stembridge_local_axioms() . . . pass
    5656    """
    5757
    58     @cached_method
    5958    def super_categories(self):
    6059        r"""
    6160        EXAMPLES::
  • sage/categories/coalgebras.py

    diff --git a/sage/categories/coalgebras.py b/sage/categories/coalgebras.py
    a b  
    4545
    4646        sage: TestSuite(Coalgebras(ZZ)).run()
    4747    """
    48     @cached_method
    4948    def super_categories(self):
    5049        """
    5150        EXAMPLES::
  • sage/categories/commutative_additive_groups.py

    diff --git a/sage/categories/commutative_additive_groups.py b/sage/categories/commutative_additive_groups.py
    a b  
    3232        sage: TestSuite(CommutativeAdditiveGroups()).run()
    3333    """
    3434
    35     @cached_method
    3635    def super_categories(self):
    3736        """
    3837        EXAMPLES::
  • sage/categories/commutative_additive_monoids.py

    diff --git a/sage/categories/commutative_additive_monoids.py b/sage/categories/commutative_additive_monoids.py
    a b  
    3434        sage: TestSuite(CommutativeAdditiveMonoids()).run()
    3535    """
    3636
    37     @cached_method
    3837    def super_categories(self):
    3938        """
    4039        EXAMPLES::
  • sage/categories/commutative_additive_semigroups.py

    diff --git a/sage/categories/commutative_additive_semigroups.py b/sage/categories/commutative_additive_semigroups.py
    a b  
    3636
    3737    """
    3838
    39     @cached_method
    4039    def super_categories(self):
    4140        """
    4241        EXAMPLES::
  • sage/categories/commutative_algebra_ideals.py

    diff --git a/sage/categories/commutative_algebra_ideals.py b/sage/categories/commutative_algebra_ideals.py
    a b  
    1212
    1313from sage.misc.cachefunc import cached_method
    1414from category_types import Category_ideal, Category_in_ambient
     15from algebra_ideals import AlgebraIdeals
    1516
    1617class CommutativeAlgebraIdeals(Category_ideal):
    1718    """
     
    6667        """
    6768        return self.ambient()
    6869
    69     @cached_method
    7070    def super_categories(self):
    7171        """
    7272        EXAMPLES::
     
    7474            sage: CommutativeAlgebraIdeals(QQ[x]).super_categories()
    7575            [Category of algebra ideals in Univariate Polynomial Ring in x over Rational Field]
    7676        """
    77         from algebra_ideals import AlgebraIdeals
    7877        R = self.algebra()
    7978        return [AlgebraIdeals(R)]
  • sage/categories/commutative_ring_ideals.py

    diff --git a/sage/categories/commutative_ring_ideals.py b/sage/categories/commutative_ring_ideals.py
    a b  
    1313from sage.misc.cachefunc import cached_method
    1414from category_types import Category_ideal
    1515from sage.categories.commutative_rings import CommutativeRings
     16from ring_ideals import RingIdeals
    1617
    1718class CommutativeRingIdeals(Category_ideal):
    1819    """
     
    4546            raise TypeError, "R (=%s) must be a commutative ring"%R
    4647        Category_ideal.__init__(self, R)
    4748
    48     @cached_method
    4949    def super_categories(self):
    5050        """
    5151        EXAMPLES::
     
    5353            sage: CommutativeRingIdeals(ZZ).super_categories()
    5454            [Category of ring ideals in Integer Ring]
    5555        """
    56         from ring_ideals import RingIdeals
    5756        R = self.ring()
    5857        return [RingIdeals(R)]
  • sage/categories/covariant_functorial_construction.py

    diff --git a/sage/categories/covariant_functorial_construction.py b/sage/categories/covariant_functorial_construction.py
    a b  
    354354            sage: sage.categories.algebra_functor.AlgebrasCategory.default_super_categories(FiniteMonoids(), QQ)
    355355            Category of monoid algebras over Rational Field
    356356        """
    357         return Category.join([getattr(cat, cls._functor_category)(*args) for cat in category.super_categories()])
     357        return Category.join([getattr(cat, cls._functor_category)(*args) for cat in category._super_categories])
    358358
     359    def is_subcategory(self, C):
     360        """
     361        .. todo:: doctests + explain why this method is needed
     362        """
     363        if C is self:
     364            return True
     365        return any(X.is_subcategory(C) for X in self._super_categories)
    359366
    360367    def __init__(self, category, *args):
    361368        """
     
    363370
    364371            sage: from sage.categories.covariant_functorial_construction import CovariantConstructionCategory
    365372            sage: class FooBars(CovariantConstructionCategory):
    366             ...       pass
    367             sage: C = FooBars(ModulesWithBasis(QQ))
     373            ...       _functor_category = "FooBars"
     374            sage: Category.FooBars = lambda self: FooBars.category_of(self)
     375            sage: C = FooBars(ModulesWithBasis(ZZ))
    368376            sage: C
    369             Category of foo bars of modules with basis over Rational Field
     377            Category of foo bars of modules with basis over Integer Ring
    370378            sage: C.base_category()
    371             Category of modules with basis over Rational Field
     379            Category of modules with basis over Integer Ring
    372380            sage: latex(C)
    373             \mathbf{FooBars}(\mathbf{ModulesWithBasis}_{\Bold{Q}})
     381            \mathbf{FooBars}(\mathbf{ModulesWithBasis}_{\Bold{Z}})
    374382            sage: import __main__; __main__.FooBars = FooBars # Fake FooBars being defined in a python module
    375383            sage: TestSuite(C).run()
    376384        """
     
    409417        """
    410418        return []
    411419
    412     @cached_method
    413420    def super_categories(self):
    414421        """
    415422        Returns the super categories of a construction category
  • sage/categories/coxeter_groups.py

    diff --git a/sage/categories/coxeter_groups.py b/sage/categories/coxeter_groups.py
    a b  
    9696        running ._test_some_elements() . . . pass
    9797    """
    9898
    99     @cached_method
    10099    def super_categories(self):
    101100        """
    102101        EXAMPLES::
  • sage/categories/crystals.py

    diff --git a/sage/categories/crystals.py b/sage/categories/crystals.py
    a b  
    8282        running ._test_stembridge_local_axioms() . . . pass
    8383    """
    8484
    85     @cached_method
    8685    def super_categories(self):
    8786        r"""
    8887        EXAMPLES::
  • sage/categories/division_rings.py

    diff --git a/sage/categories/division_rings.py b/sage/categories/division_rings.py
    a b  
    3131        sage: TestSuite(DivisionRings()).run()
    3232    """
    3333
    34     @cached_method
    3534    def super_categories(self):
    3635        """
    3736        EXAMPLES::
  • sage/categories/domains.py

    diff --git a/sage/categories/domains.py b/sage/categories/domains.py
    a b  
    3232        sage: TestSuite(Domains()).run()
    3333    """
    3434
    35     @cached_method
    3635    def super_categories(self):
    3736        """
    3837        EXAMPLES::
  • sage/categories/enumerated_sets.py

    diff --git a/sage/categories/enumerated_sets.py b/sage/categories/enumerated_sets.py
    a b  
    8585        sage: TestSuite(C).run()
    8686    """
    8787
    88     @cached_method
    8988    def super_categories(self):
    9089        """
    9190        EXAMPLES::
  • sage/categories/euclidean_domains.py

    diff --git a/sage/categories/euclidean_domains.py b/sage/categories/euclidean_domains.py
    a b  
    3131        sage: TestSuite(EuclideanDomains()).run()
    3232    """
    3333
    34     @cached_method
    3534    def super_categories(self):
    3635        """
    3736        EXAMPLES::
  • sage/categories/fields.py

    diff --git a/sage/categories/fields.py b/sage/categories/fields.py
    a b  
    1313
    1414from sage.categories.category import Category
    1515from sage.categories.category_singleton import Category_singleton, Category_contains_method_by_parent_class
     16from sage.categories.euclidean_domains import EuclideanDomains
     17from sage.categories.unique_factorization_domains import UniqueFactorizationDomains
     18from sage.categories.division_rings import DivisionRings
     19
    1620from sage.misc.cachefunc import cached_method
    1721from sage.misc.lazy_attribute import lazy_class_attribute
    1822from sage.rings.field import is_Field
     
    4347        sage: TestSuite(Fields()).run()
    4448    """
    4549
    46     @cached_method
    4750    def super_categories(self):
    4851        """
    4952        EXAMPLES::
     
    5255            [Category of euclidean domains, Category of unique factorization domains, Category of division rings]
    5356
    5457        """
    55         from sage.categories.basic import EuclideanDomains, UniqueFactorizationDomains, DivisionRings
    5658        return [EuclideanDomains(), UniqueFactorizationDomains(), DivisionRings()]
    5759
    5860    def __contains__(self, x):
  • sage/categories/finite_groups.py

    diff --git a/sage/categories/finite_groups.py b/sage/categories/finite_groups.py
    a b  
    3131        sage: C = FiniteGroups()
    3232        sage: TestSuite(C).run(verbose = True)
    3333        running ._test_category() . . . pass
     34        running ._test_category_graph() . . . pass
    3435        running ._test_not_implemented_methods() . . . pass
    3536        running ._test_pickling() . . . pass
    3637
  • sage/categories/finite_permutation_groups.py

    diff --git a/sage/categories/finite_permutation_groups.py b/sage/categories/finite_permutation_groups.py
    a b  
    3333        sage: C = FinitePermutationGroups()
    3434        sage: TestSuite(C).run(verbose = True)
    3535        running ._test_category() . . . pass
     36        running ._test_category_graph() . . . pass
    3637        running ._test_not_implemented_methods() . . . pass
    3738        running ._test_pickling() . . . pass
    3839
  • sage/categories/finite_semigroups.py

    diff --git a/sage/categories/finite_semigroups.py b/sage/categories/finite_semigroups.py
    a b  
    4444        sage: C = FiniteSemigroups()
    4545        sage: TestSuite(C).run(verbose = True)
    4646        running ._test_category() . . . pass
     47        running ._test_category_graph() . . . pass
    4748        running ._test_not_implemented_methods() . . . pass
    4849        running ._test_pickling() . . . pass
    4950
  • sage/categories/g_sets.py

    diff --git a/sage/categories/g_sets.py b/sage/categories/g_sets.py
    a b  
    1212
    1313from sage.categories.category import Category
    1414from sage.misc.cachefunc import cached_method
     15from sets_cat import Sets
    1516
    1617#############################################################
    1718# GSets
     
    5152    #def construction(self):
    5253    #    return (self.__class__, self.__G)
    5354
    54     @cached_method
    5555    def super_categories(self):
    5656        """
    5757        EXAMPLES::
     
    5959            sage: GSets(SymmetricGroup(8)).super_categories()
    6060            [Category of sets]
    6161        """
    62         from sets_cat import Sets
    6362        return [Sets()]
    6463
    6564    @classmethod
  • sage/categories/gcd_domains.py

    diff --git a/sage/categories/gcd_domains.py b/sage/categories/gcd_domains.py
    a b  
    1111from sage.categories.category import Category
    1212from sage.categories.category_singleton import Category_singleton
    1313from sage.misc.cachefunc import cached_method
     14from sage.categories.integral_domains import IntegralDomains
    1415
    1516class GcdDomains(Category_singleton):
    1617    """
     
    3031        sage: TestSuite(GcdDomains()).run()
    3132    """
    3233
    33     @cached_method
    3434    def super_categories(self):
    3535        """
    3636        EXAMPLES::
     
    3838            sage: GcdDomains().super_categories()
    3939            [Category of integral domains]
    4040        """
    41         from sage.categories.integral_domains import IntegralDomains
    4241        return [IntegralDomains()]
    4342
    4443    class ParentMethods:
  • sage/categories/groupoid.py

    diff --git a/sage/categories/groupoid.py b/sage/categories/groupoid.py
    a b  
    1212
    1313from sage.categories.category import Category
    1414from sage.misc.cachefunc import cached_method
     15from sets_cat import Sets
    1516
    1617class Groupoid(Category):
    1718    """
     
    5758    #def construction(self):
    5859    #    return (self.__class__, self.__G)
    5960
    60     @cached_method
    6161    def super_categories(self):
    6262        """
    6363        EXAMPLES::
     
    6565            sage: Groupoid(DihedralGroup(3)).super_categories()
    6666            [Category of sets]
    6767        """
    68         from sets_cat import Sets
    6968        return [Sets()] # ???
    7069
    7170    @classmethod
  • sage/categories/groups.py

    diff --git a/sage/categories/groups.py b/sage/categories/groups.py
    a b  
    3434        sage: TestSuite(Groups()).run()
    3535    """
    3636
    37     @cached_method
    3837    def super_categories(self):
    3938        """
    4039        EXAMPLES::
     
    344343                [Category of hopf algebras with basis over Rational Field, Category of monoid algebras over Rational Field]
    345344
    346345                sage: Groups().example().algebra(ZZ).categories()
    347                 [Category of group algebras over Integer Ring, Category of hopf algebras with basis over Integer Ring, Category of bialgebras with basis over Integer Ring, Category of coalgebras with basis over Integer Ring, Category of hopf algebras over Integer Ring, Category of bialgebras over Integer Ring, Category of coalgebras over Integer Ring, Category of monoid algebras over Integer Ring, Category of semigroup algebras over Integer Ring, Category of algebras with basis over Integer Ring, Category of algebras over Integer Ring, Category of rings, Category of rngs, Category of semirings, Category of monoids, Category of semigroups, Category of magmas, Category of set algebras over Integer Ring, Category of modules with basis over Integer Ring, Category of modules over Integer Ring, Category of bimodules over Integer Ring on the left and Integer Ring on the right, Category of left modules over Integer Ring, Category of right modules over Integer Ring, Category of commutative additive groups, Category of commutative additive monoids, Category of commutative additive semigroups, Category of additive magmas, Category of sets, Category of sets with partial maps, Category of objects]
     346                [Category of group algebras over Integer Ring,
     347                 Category of hopf algebras with basis over Integer Ring,
     348                 Category of bialgebras with basis over Integer Ring,
     349                 Category of monoid algebras over Integer Ring,
     350                 Category of semigroup algebras over Integer Ring,
     351                 Category of algebras with basis over Integer Ring,
     352                 Category of coalgebras with basis over Integer Ring,
     353                 Category of set algebras over Integer Ring,
     354                 Category of modules with basis over Integer Ring,
     355                 Category of hopf algebras over Integer Ring,
     356                 Category of bialgebras over Integer Ring,
     357                 Category of algebras over Integer Ring,
     358                 Category of rings,
     359                 Category of rngs,
     360                 Category of coalgebras over Integer Ring,
     361                 Category of modules over Integer Ring,
     362                 Category of bimodules over Integer Ring on the left and Integer Ring on the right,
     363                 Category of left modules over Integer Ring,
     364                 Category of right modules over Integer Ring,
     365                 Category of commutative additive groups,
     366                 Category of semirings,
     367                 Category of commutative additive monoids,
     368                 Category of commutative additive semigroups,
     369                 Category of additive magmas,
     370                 Category of monoids,
     371                 Category of semigroups,
     372                 Category of magmas,
     373                 Category of sets,
     374                 Category of sets with partial maps,
     375                 Category of objects]
    348376            """
    349377            from sage.categories.hopf_algebras_with_basis import HopfAlgebrasWithBasis
    350378            return [HopfAlgebrasWithBasis(self.base_ring())]
  • sage/categories/hecke_modules.py

    diff --git a/sage/categories/hecke_modules.py b/sage/categories/hecke_modules.py
    a b  
    1313from sage.categories.category_types import Category_module
    1414from sage.misc.cachefunc import cached_method
    1515from sage.categories.category import HomCategory
     16from sage.categories.all import ModulesWithBasis
    1617
    1718class HeckeModules(Category_module):
    1819    r"""
     
    7374            raise TypeError, "R (=%s) must be a commutative ring"%R
    7475        Category_module.__init__(self, R, "Hecke modules")
    7576
    76     @cached_method
    7777    def super_categories(self):
    7878        """
    7979        EXAMPLES::
     
    8181            sage: HeckeModules(QQ).super_categories()
    8282            [Category of modules with basis over Rational Field]
    8383        """
    84         from sage.categories.all import ModulesWithBasis
    8584        R = self.base_ring()
    8685        return [ModulesWithBasis(R)]
    8786
  • sage/categories/highest_weight_crystals.py

    diff --git a/sage/categories/highest_weight_crystals.py b/sage/categories/highest_weight_crystals.py
    a b  
    5858        running ._test_stembridge_local_axioms() . . . pass
    5959    """
    6060
    61     @cached_method
    6261    def super_categories(self):
    6362        r"""
    6463        EXAMPLES::
  • sage/categories/hopf_algebras.py

    diff --git a/sage/categories/hopf_algebras.py b/sage/categories/hopf_algebras.py
    a b  
    3232        sage: TestSuite(HopfAlgebras(ZZ)).run()
    3333    """
    3434
    35     @cached_method
    3635    def super_categories(self):
    3736        """
    3837        EXAMPLES::
  • sage/categories/integral_domains.py

    diff --git a/sage/categories/integral_domains.py b/sage/categories/integral_domains.py
    a b  
    1111from sage.categories.category import Category
    1212from sage.categories.category_singleton import Category_singleton
    1313from sage.misc.cachefunc import cached_method
     14from sage.categories.commutative_rings import CommutativeRings
     15from sage.categories.domains import Domains
    1416
    1517class IntegralDomains(Category_singleton):
    1618    """
     
    2931        sage: TestSuite(IntegralDomains()).run()
    3032    """
    3133
    32     @cached_method
    3334    def super_categories(self):
    3435        """
    3536        EXAMPLES::
     
    3738            sage: IntegralDomains().super_categories()
    3839            [Category of commutative rings, Category of domains]
    3940        """
    40         from sage.categories.basic import CommutativeRings, Domains
    4141        return [CommutativeRings(), Domains()]
    4242
    4343    class ParentMethods:
  • sage/categories/left_modules.py

    diff --git a/sage/categories/left_modules.py b/sage/categories/left_modules.py
    a b  
    1010
    1111from category_types import Category_over_base_ring
    1212from sage.misc.cachefunc import cached_method
     13from sage.categories.commutative_additive_groups import CommutativeAdditiveGroups
    1314
    1415#?class LeftModules(Category_over_base_rng):
    1516class LeftModules(Category_over_base_ring):
     
    3031        sage: TestSuite(LeftModules(ZZ)).run()
    3132    """
    3233
    33     @cached_method
    3434    def super_categories(self):
    3535        """
    3636        EXAMPLES::
     
    3838            sage: LeftModules(QQ).super_categories()
    3939            [Category of commutative additive groups]
    4040        """
    41         from sage.categories.commutative_additive_groups import CommutativeAdditiveGroups
    4241        return [CommutativeAdditiveGroups()]
    4342
    4443    class ParentMethods:
  • sage/categories/magmas.py

    diff --git a/sage/categories/magmas.py b/sage/categories/magmas.py
    a b  
    3434    TESTS::
    3535
    3636        sage: C = Magmas()
    37         sage: TestSuite(C).run(verbose=True)
    38         running ._test_category() . . . pass
    39         running ._test_not_implemented_methods() . . . pass
    40         running ._test_pickling() . . . pass
    41 
     37        sage: TestSuite(C).run()
    4238    """
    43     @cached_method
    4439    def super_categories(self):
    4540        """
    4641        EXAMPLES::
  • sage/categories/matrix_algebras.py

    diff --git a/sage/categories/matrix_algebras.py b/sage/categories/matrix_algebras.py
    a b  
    1212
    1313from category_types import Category_over_base_ring
    1414from sage.misc.cachefunc import cached_method
     15from algebras import Algebras
    1516
    1617class MatrixAlgebras(Category_over_base_ring):
    1718    """
     
    2728        sage: TestSuite(MatrixAlgebras(ZZ)).run()
    2829    """
    2930
    30     @cached_method
    3131    def super_categories(self):
    3232        """
    3333        EXAMPLES::
     
    3535            sage: MatrixAlgebras(QQ).super_categories()
    3636            [Category of algebras over Rational Field]
    3737        """
    38         from algebras import Algebras
    3938        R = self.base_ring()
    4039        return [Algebras(R)]
  • sage/categories/modular_abelian_varieties.py

    diff --git a/sage/categories/modular_abelian_varieties.py b/sage/categories/modular_abelian_varieties.py
    a b  
    1212
    1313from category_types import Category_over_base
    1414from sage.misc.cachefunc import cached_method
     15from sets_cat import Sets
    1516
    1617class ModularAbelianVarieties(Category_over_base):
    1718    """
     
    4950        """
    5051        return self.base()
    5152
    52     @cached_method
    5353    def super_categories(self):
    5454        """
    5555        EXAMPLES::
     
    5757            sage: ModularAbelianVarieties(QQ).super_categories()
    5858            [Category of sets]
    5959        """
    60         from sets_cat import Sets
    6160        return [Sets()] # FIXME
  • sage/categories/modules.py

    diff --git a/sage/categories/modules.py b/sage/categories/modules.py
    a b  
    1414from sage.categories.category import HomCategory
    1515from category_types import Category_module
    1616from sage.misc.cachefunc import cached_method
     17from sage.categories.bimodules import Bimodules
    1718from sage.categories.fields import Fields
    1819_Fields = Fields()
    1920from vector_spaces import VectorSpaces
     
    120121        result._reduction[2]['dispatch'] = False
    121122        return result
    122123
    123     @cached_method
    124124    def super_categories(self):
    125125        """
    126126        EXAMPLES::
     
    136136            [Category of modules over Rational Field]
    137137        """
    138138        R = self.base_ring()
    139         from sage.categories.bimodules import Bimodules
    140139        return [Bimodules(R,R)]
    141140
    142141    class ParentMethods:
  • sage/categories/monoids.py

    diff --git a/sage/categories/monoids.py b/sage/categories/monoids.py
    a b  
    4646        sage: TestSuite(C).run()
    4747
    4848    """
    49     @cached_method
    5049    def super_categories(self):
    5150        """
    5251        Returns a list of the immediate super categories of ``self``.
  • sage/categories/number_fields.py

    diff --git a/sage/categories/number_fields.py b/sage/categories/number_fields.py
    a b  
    5555        sage: TestSuite(NumberFields()).run()
    5656    """
    5757
    58     @cached_method
    5958    def super_categories(self):
    6059        """
    6160        EXAMPLES::
  • sage/categories/objects.py

    diff --git a/sage/categories/objects.py b/sage/categories/objects.py
    a b  
    3838        sage: TestSuite(Objects()).run()
    3939    """
    4040
    41     @cached_method
    4241    def super_categories(self):
    4342        """
    4443        EXAMPLES::
  • sage/categories/partially_ordered_monoids.py

    diff --git a/sage/categories/partially_ordered_monoids.py b/sage/categories/partially_ordered_monoids.py
    a b  
    3333        sage: TestSuite(PartiallyOrderedMonoids()).run()
    3434    """
    3535
    36     @cached_method
    3736    def super_categories(self):
    3837        """
    3938        EXAMPLES::
  • sage/categories/pointed_sets.py

    diff --git a/sage/categories/pointed_sets.py b/sage/categories/pointed_sets.py
    a b  
    1313from sage.categories.category import Category
    1414from sage.categories.category_singleton import Category_singleton
    1515from sage.misc.cachefunc import cached_method
     16from sets_cat import Sets
    1617
    1718class PointedSets(Category_singleton):
    1819    """
     
    3132    #    import sage.sets.all
    3233    #    return sage.sets.all.Set(X, pt)
    3334
    34     @cached_method
    3535    def super_categories(self):
    3636        """
    3737        EXAMPLES::
     
    3939            sage: PointedSets().super_categories()
    4040            [Category of sets]
    4141        """
    42         from sets_cat import Sets
    4342        return [Sets()] # ???
  • sage/categories/primer.py

    diff --git a/sage/categories/primer.py b/sage/categories/primer.py
    a b  
    546546     Category of algebras over Rational Field,
    547547     Category of rings,
    548548     Category of rngs,
    549      Category of semirings,
    550549     Category of vector spaces over Rational Field,
    551550     Category of modules over Rational Field,
    552551     Category of bimodules over Rational Field on the left and Rational Field on the right,
    553552     Category of left modules over Rational Field,
    554553     Category of right modules over Rational Field,
    555554     Category of commutative additive groups,
     555     Category of semirings,
    556556     Category of commutative additive monoids,
    557557     Category of commutative additive semigroups,
    558558     Category of additive magmas,
     
    613613Writing a new category
    614614----------------------
    615615
    616 Each category should come with a good example, in sage.categories.examples.
     616Each category `C` **must** be provided with a method ``C.super_categories()``
     617and *can* be provided with a method ``C._subcategory_hook_(D)``. Also, it
     618may be needed to insert `C` into the output of the ``super_categories()`` method
     619of some other category. This determines the position of `C` in the category graph.
    617620
    618 The order between super categories should not be mathematically
    619 relevant (otherwise this usually means the category hierarchy is
    620 wrong). On the other hand, it should be consistent, to help Python
    621 build the method resolution order for the generated classes (it always
    622 respects inheritance, and tries to respect the order of the bases).
     621A category *may* provide methods that can be used by all its objects,
     622respectively by all elements of its objects.
    623623
    624 The current convention is to order them lexicographically w.r.t. the
    625 following criterions:
     624Each category *should* come with a good example, in :mod:`sage.categories.examples`.
     625
     626Inserting the new category into the category graph
     627..................................................
     628
     629``C.super_categories()``  must return a list of categories, namely
     630the *immediate* super categories of `C`. The generic method
     631``C.all_super_categories()`` will recursively determine the list
     632of *all* super categories of `C`, by using the so-called C3 algorithm,
     633that is also used for the method resolution order of new-style classes
     634in Python (see trac ticket #11943).
     635
     636Of course, if you know that your new category `C` is an immediate
     637super category of some existing category `D`, then you should modify
     638`D`'s ``super_categories`` method so that `C` is included.
     639
     640The order between the super categories does influence the inheritance
     641of methods for parents and elements. Namely, if `P` is an object in
     642the category `C` and if `C_1` and `C_2` are both super categories of
     643`C` defining some method ``foo``, then `P` will use `C_1`'s version of
     644``foo`` only if `C_1` appears in ``C.all_super_categories()`` before
     645`C_2`. This is an *implementation detail*: the order between super
     646categories should not be mathematically relevant, and code should not
     647rely on any specific order, as it it subject to later change.
     648
     649Also, the C3 algorithm will only be able to determine a consistent order
     650on the list of all super categories if the orders on the different lists
     651of *immediate* super categories is sane. See :func:`sage.misc.c3.C3_algorithm`
     652and :meth:`sage.categories.category.Category.all_super_categories` for examples.
     653
     654It is thus useful to follow certain conventions when ordering the
     655immediate super categories. The current convention is to order them
     656lexicographically w.r.t. the following criteria:
    626657
    627658 - Graded... or Finite dimensional... first
    628659 - ...WithBasis first
     
    652683     Category of algebras over Rational Field,
    653684     Category of rings,
    654685     Category of rngs,
    655      Category of semirings,
    656      Category of monoids,
    657      Category of semigroups,
    658      Category of magmas,
    659686     Category of coalgebras over Rational Field,
    660687     Category of vector spaces over Rational Field,
    661688     Category of modules over Rational Field,
     
    663690     Category of left modules over Rational Field,
    664691     Category of right modules over Rational Field,
    665692     Category of commutative additive groups,
     693     Category of semirings,
    666694     Category of commutative additive monoids,
    667695     Category of commutative additive semigroups,
    668696     Category of additive magmas,
     697     Category of monoids,
     698     Category of semigroups,
     699     Category of magmas,
    669700     Category of sets,
    670701     Category of sets with partial maps,
    671702     Category of objects]
    672703
    673 Todo: any better convention? Maybe we should further specify that subcategories of Modules() go first?
     704Subcategory hook (advanced optimization feature)
     705................................................
     706
     707The default implementation of the method ``C.is_subcategory(D)`` is to
     708look up whether `D` appears in ``C.all_super_categories()``. However,
     709building the list of all the super categories of `C` is an expensive
     710operation that is sometimes best avoided. For example, if both `C` and
     711`D` are categories defined over a base, but the bases differ, then one
     712knows right away that they can not be subcategories of each other.
     713
     714When such a short-path is known, one can implement a method
     715``_subcategory_hook_``. ``C.is_subcategory(D)`` first calls
     716``D._subcategory_hook_(C)``. If this returns ``Unknown``, then
     717``C.is_subcategory(D)`` tries to find ``D`` in
     718``C.all_super_categories()``. Otherwise, ``C.is_subcategory(D)``
     719returns the result of ``D._subcategory_hook_(C)``.
     720
     721By default, ``D._subcategory_hook_(C)`` tests
     722``issubclass(C.parent_class,D.parent_class)``, which is very often
     723giving the right answer::
     724
     725    sage: Rings()._subcategory_hook_(Algebras(QQ))
     726    True
     727    sage: HopfAlgebras(QQ)._subcategory_hook_(Algebras(QQ))
     728    False
     729    sage: Algebras(QQ)._subcategory_hook_(HopfAlgebras(QQ))
     730    True
     731
     732Methods for objects and elements
     733................................
     734
     735Different objects of the same category share some algebraic features, and
     736very often these features can be encoded in a method, in a generic way.
     737For example, for every commutative additive monoid, it makes sense to ask
     738for the sum of a list of elements. Sage's category framework allows to
     739provide a generic implementation for all objects of a category.
     740
     741If you want to provide your new category with generic methods for objects
     742(or elements of objects), then you simply add an attribute called
     743``ParentMethods`` (or ``ElementMethods``) carrying a class. The methods
     744of that class will automatically become methods of the objects (or the
     745elements). For instance::
     746
     747    sage: P.<x,y> = ZZ[]
     748    sage: P.sum([x,y,1])
     749    x + y + 1
     750    sage: P.sum.__module__
     751    'sage.categories.commutative_additive_monoids'
     752    sage: P.sum.__func__ is CommutativeAdditiveMonoids().ParentMethods.sum.__func__
     753    True
     754
     755We recommend to study the code of one example::
     756
     757    sage: C = CommutativeAdditiveMonoids()
     758    sage: C??                               # not tested
    674759
    675760Caveats
    676761-------
  • sage/categories/principal_ideal_domains.py

    diff --git a/sage/categories/principal_ideal_domains.py b/sage/categories/principal_ideal_domains.py
    a b  
    1111from sage.categories.category import Category
    1212from sage.categories.category_singleton import Category_singleton
    1313from sage.misc.cachefunc import cached_method
     14from sage.categories.unique_factorization_domains import UniqueFactorizationDomains
    1415
    1516class PrincipalIdealDomains(Category_singleton):
    1617    """
     
    3637        sage: TestSuite(PrincipalIdealDomains()).run()
    3738    """
    3839
    39     @cached_method
    4040    def super_categories(self):
    4141        """
    4242        EXAMPLES::
     
    4444            sage: PrincipalIdealDomains().super_categories()
    4545            [Category of unique factorization domains]
    4646        """
    47         from sage.categories.basic import UniqueFactorizationDomains
    4847        return [UniqueFactorizationDomains()]
    4948
    5049    class ParentMethods:
  • sage/categories/quotient_fields.py

    diff --git a/sage/categories/quotient_fields.py b/sage/categories/quotient_fields.py
    a b  
    1212from sage.categories.category_singleton import Category_singleton
    1313from sage.misc.cachefunc import cached_method
    1414from sage.misc.abstract_method import abstract_method
     15from sage.categories.fields import Fields
    1516
    1617class QuotientFields(Category_singleton):
    1718    """
     
    2930        sage: TestSuite(QuotientFields()).run()
    3031    """
    3132
    32     @cached_method
    3333    def super_categories(self):
    3434        """
    3535        EXAMPLES::
     
    3737            sage: QuotientFields().super_categories()
    3838            [Category of fields]
    3939        """
    40         from sage.categories.fields import Fields
    4140        return [Fields()]
    4241
    4342    class ParentMethods:
  • sage/categories/right_modules.py

    diff --git a/sage/categories/right_modules.py b/sage/categories/right_modules.py
    a b  
    1010
    1111from category_types import Category_over_base_ring
    1212from sage.misc.cachefunc import cached_method
     13from sage.categories.commutative_additive_groups import CommutativeAdditiveGroups
    1314
    1415##?class RightModules(Category_over_base_rng):
    1516class RightModules(Category_over_base_ring):
     
    3031        sage: TestSuite(RightModules(ZZ)).run()
    3132    """
    3233
    33     @cached_method
    3434    def super_categories(self):
    3535        """
    3636        EXAMPLES::
     
    3838            sage: RightModules(QQ).super_categories()
    3939            [Category of commutative additive groups]
    4040        """
    41         from sage.categories.commutative_additive_groups import CommutativeAdditiveGroups
    4241        return [CommutativeAdditiveGroups()]
    4342
    4443    class ParentMethods:
  • sage/categories/ring_ideals.py

    diff --git a/sage/categories/ring_ideals.py b/sage/categories/ring_ideals.py
    a b  
    1212
    1313from category_types import Category_ideal
    1414from sage.misc.cachefunc import cached_method
     15from modules import Modules
    1516from sage.categories.rings import Rings
    1617_Rings = Rings()
    1718
     
    5758            raise TypeError, "R (=%s) must be a ring"%R
    5859        Category_ideal.__init__(self, R)
    5960
    60     @cached_method
    6161    def super_categories(self):
    6262        """
    6363        EXAMPLES::
     
    6767            sage: RingIdeals(QQ).super_categories()
    6868            [Category of vector spaces over Rational Field]
    6969        """
    70         from modules import Modules
    7170        R = self.ring()
    7271        return [Modules(R)]
  • sage/categories/rings.py

    diff --git a/sage/categories/rings.py b/sage/categories/rings.py
    a b  
    1111#                  http://www.gnu.org/licenses/
    1212#******************************************************************************
    1313
     14from sage.categories.rngs import Rngs
     15from sage.categories.semirings import Semirings
    1416from sage.categories.category import Category
    1517from sage.categories.category_singleton import Category_singleton
    1618from category import HomCategory
     
    4244       in the category ``Algebras(P)``.
    4345    """
    4446
    45     @cached_method
    4647    def super_categories(self):
    4748        """
    4849        EXAMPLES::
     
    5051            sage: Rings().super_categories()
    5152            [Category of rngs, Category of semirings]
    5253        """
    53         from sage.categories.rngs import Rngs
    54         from sage.categories.semirings import Semirings
    5554        return [Rngs(), Semirings()]
    5655
    5756    class ParentMethods:
  • sage/categories/rngs.py

    diff --git a/sage/categories/rngs.py b/sage/categories/rngs.py
    a b  
    1111from sage.categories.category import Category
    1212from sage.categories.category_singleton import Category_singleton
    1313from sage.misc.cachefunc import cached_method
     14from commutative_additive_groups import CommutativeAdditiveGroups
     15from semigroups import Semigroups
    1416
    1517class Rngs(Category_singleton):
    1618    """
     
    3133        sage: TestSuite(Rngs()).run()
    3234    """
    3335
    34     @cached_method
    3536    def super_categories(self):
    3637        """
    3738        EXAMPLES::
     
    3940            sage: Rngs().super_categories()
    4041            [Category of commutative additive groups, Category of semigroups]
    4142        """
    42         from commutative_additive_groups import CommutativeAdditiveGroups
    43         from semigroups import Semigroups
    4443        return [CommutativeAdditiveGroups(), Semigroups()]
    4544
    4645    class ParentMethods:
  • sage/categories/schemes.py

    diff --git a/sage/categories/schemes.py b/sage/categories/schemes.py
    a b  
    1313from sage.categories.category import Category, HomCategory
    1414from sage.categories.category_types import Category_over_base
    1515from sage.misc.cachefunc import cached_method
     16from sets_cat import Sets
    1617
    1718def Schemes(X=None):
    1819    """
     
    5758        """
    5859        Category.__init__(self, "Schemes")
    5960
    60     @cached_method
    6161    def super_categories(self):
    6262        """
    6363        EXAMPLES::
     
    6565            sage: Schemes().super_categories()
    6666            [Category of sets]
    6767        """
    68         from sets_cat import Sets
    6968        return [Sets()]
    7069
    7170    def _call_(self, x):
     
    203202        """
    204203        return self.base()
    205204
    206     @cached_method
    207205    def super_categories(self):
    208206        """
    209207        EXAMPLES::
  • sage/categories/semigroups.py

    diff --git a/sage/categories/semigroups.py b/sage/categories/semigroups.py
    a b  
    4242        sage: C = Semigroups()
    4343        sage: TestSuite(C).run(verbose=True)
    4444        running ._test_category() . . . pass
     45        running ._test_category_graph() . . . pass
    4546        running ._test_not_implemented_methods() . . . pass
    4647        running ._test_pickling() . . . pass
    4748
    4849    """
    49     @cached_method
    5050    def super_categories(self):
    5151        """
    5252        EXAMPLES::
  • sage/categories/semirings.py

    diff --git a/sage/categories/semirings.py b/sage/categories/semirings.py
    a b  
    1010
    1111from sage.categories.category import Category
    1212from sage.categories.category_singleton import Category_singleton
     13from sage.categories.commutative_additive_monoids import CommutativeAdditiveMonoids
     14from sage.categories.monoids import Monoids
    1315from sage.misc.cachefunc import cached_method
    1416
    1517class Semirings(Category_singleton):
     
    3638        sage: TestSuite(Semirings()).run()
    3739    """
    3840
    39     @cached_method
    4041    def super_categories(self):
    4142        """
    4243        EXAMPLES::
     
    4445            sage: Semirings().super_categories()
    4546            [Category of commutative additive monoids, Category of monoids]
    4647        """
    47         from sage.categories.commutative_additive_monoids import CommutativeAdditiveMonoids
    48         from sage.categories.monoids import Monoids
    4948        return [CommutativeAdditiveMonoids(), Monoids()]
    5049
    5150    class ParentMethods:
  • sage/categories/sets_cat.py

    diff --git a/sage/categories/sets_cat.py b/sage/categories/sets_cat.py
    a b  
    157157
    158158    """
    159159
    160     @cached_method
    161160    def super_categories(self):
    162161        r"""
    163162        We include SetsWithPartialMaps between Sets and Objects so that we
  • sage/categories/sets_with_partial_maps.py

    diff --git a/sage/categories/sets_with_partial_maps.py b/sage/categories/sets_with_partial_maps.py
    a b  
    1313from sage.categories.category import Category, HomCategory
    1414from sage.categories.category_singleton import Category_singleton
    1515from sage.misc.cachefunc import cached_method
     16from objects import Objects
    1617
    1718class SetsWithPartialMaps(Category_singleton):
    1819    """
     
    4142    #    import sage.sets.all
    4243    #    return sage.sets.all.Set(X, pt)
    4344
    44     @cached_method
    4545    def super_categories(self):
    4646        """
    4747        EXAMPLES::
     
    4949            sage: SetsWithPartialMaps().super_categories()
    5050            [Category of objects]
    5151        """
    52         from objects import Objects
    5352        return [Objects()]
    5453
    5554    class HomCategory(HomCategory):
  • sage/categories/unique_factorization_domains.py

    diff --git a/sage/categories/unique_factorization_domains.py b/sage/categories/unique_factorization_domains.py
    a b  
    1111from sage.categories.category import Category
    1212from sage.categories.category_singleton import Category_singleton
    1313from sage.misc.cachefunc import cached_method
     14from sage.categories.gcd_domains import GcdDomains
    1415
    1516class UniqueFactorizationDomains(Category_singleton):
    1617    """
     
    3031        sage: TestSuite(UniqueFactorizationDomains()).run()
    3132    """
    3233
    33     @cached_method
    3434    def super_categories(self):
    3535        """
    3636        EXAMPLES::
     
    3838            sage: UniqueFactorizationDomains().super_categories()
    3939            [Category of gcd domains]
    4040        """
    41         from sage.categories.gcd_domains import GcdDomains
    4241        return [GcdDomains()]
    4342
    4443    class ParentMethods:
  • sage/categories/vector_spaces.py

    diff --git a/sage/categories/vector_spaces.py b/sage/categories/vector_spaces.py
    a b  
    110110        """
    111111        return self.base_ring()
    112112
    113     @cached_method
    114113    def super_categories(self):
    115114        """
    116115        EXAMPLES::
  • sage/categories/weyl_groups.py

    diff --git a/sage/categories/weyl_groups.py b/sage/categories/weyl_groups.py
    a b  
    4949        sage: TestSuite(C).run()
    5050    """
    5151
    52     @cached_method
    5352    def super_categories(self):
    5453        r"""
    5554        EXAMPLES::
  • new file sage/misc/c3.pyx

    diff --git a/sage/misc/c3.pyx b/sage/misc/c3.pyx
    new file mode 100644
    - +  
     1"""
     2The C3 algorithm
     3
     4The C3 algorithm is used as method resolution order for new style
     5classes in Python. The implementation here is used to order the list
     6of super categories of a category.
     7
     8AUTHOR:
     9
     10- Simon King (2011-11): initial version.
     11"""
     12#*****************************************************************************
     13#  Copyright (C) 2011    Simon King <simon.king@uni-jena.de>
     14#
     15#  Distributed under the terms of the GNU General Public License (GPL)
     16#                  http://www.gnu.org/licenses/
     17#******************************************************************************
     18
     19include "../ext/python.pxi"
     20
     21cpdef list C3_algorithm(object start, str bases, str attribute, bint proper):
     22    """
     23    An implementation of the C3 algorithm.
     24
     25    C3 is the algorithm used by Python to construct the method
     26    resolution order for new style classes involving multiple
     27    inheritance.
     28
     29    This implementation is used to order the list of super categories
     30    of a category; see
     31    :meth:`~sage.categories.category.Category.all_super_categories`.
     32    The purpose is to ensure that list of super categories matches
     33    with the method resolution order of the parent or element classes
     34    of a category.
     35
     36    INPUT:
     37
     38    - ``start`` -- an object; the returned list is built upon data
     39      provided by certain attributes of ``start``.
     40    - ``bases`` -- a string; the name of an attribute of ``start``
     41      providing a list of objects.
     42    - ``attribute`` -- a string; the name of an attribute of the
     43      objects provided in ``getattr(start,bases)``. That attribute is
     44      supposed to provide a list.
     45
     46    ASSUMPTIONS:
     47
     48    Our implementation of the algorithm only works on lists of
     49    objects that compare equal if and only if they are identical.
     50
     51    OUTPUT:
     52
     53    A list, the result of the C3 algorithm applied to the list
     54    ``[getattr(X,attribute) for X in getattr(start,bases)]``.
     55
     56    EXAMPLES:
     57
     58    We start with some categories having an inconsistent inheritance
     59    order::
     60
     61        sage: class X(Category):
     62        ...    def super_categories(self):
     63        ...        return [Objects()]
     64        sage: class Y(Category):
     65        ...    def super_categories(self):
     66        ...        return [Objects()]
     67        sage: class A(Category):
     68        ...    def super_categories(self):
     69        ...        return [X(), Y()]
     70        sage: class B(Category):
     71        ...    def super_categories(self):
     72        ...        return [Y(), X()]
     73        sage: class Foo(Category):
     74        ...    def super_categories(self):
     75        ...       return [A(), B()]
     76        sage: F = Foo()
     77
     78    Python is not able to create a consistent method resolution order
     79    for the parent class::
     80
     81        sage: F.parent_class
     82        Traceback (most recent call last):
     83        ...
     84        TypeError: Cannot create a consistent method resolution
     85        order (MRO) for bases ....parent_class, ....parent_class
     86
     87    Since the C3 algorithm is used for determining the list of
     88    all super categories (by trac ticket #11943), a similar error
     89    arises here::
     90
     91        sage: F.all_super_categories()
     92        Traceback (most recent call last):
     93        ...
     94        ValueError: Can not merge the items Category of x, Category of y.
     95
     96    Next, we demonstrate how our implementation of the C3 algorithm
     97    is used to compute the list of all super categories::
     98
     99        sage: C = Category.join([HopfAlgebrasWithBasis(QQ), FiniteEnumeratedSets()])
     100        sage: from sage.misc.c3 import C3_algorithm
     101        sage: C3_algorithm(C,'_super_categories','_all_super_categories',True) == C._all_super_categories_proper
     102        True
     103        sage: C3_algorithm(C,'_super_categories','_all_super_categories',False) == C._all_super_categories
     104        True
     105
     106    By trac ticket #11943, the following consistency tests are part
     107    of the test suites of categories (except for hom categories)::
     108
     109        sage: C.parent_class.mro() == [x.parent_class for x in C.all_super_categories()]+[object]
     110        True
     111        sage: C.element_class.mro() == [x.element_class for x in C.all_super_categories()]+[object]
     112        True
     113
     114    """
     115    # The lists in the arguments are reverted,
     116    # so that we can do pop() in lieue of pop(0).
     117    # In addition, containedness in the tail is tested using lists.
     118    cdef list out
     119    if proper:
     120        out = []
     121    else:
     122        out = [start]
     123    cdef list args = getattr(start,bases)
     124    if not args:
     125        return out
     126    cdef list curr_tail, tmp_tail
     127    cdef set curr_set, tmp_set
     128    cdef object curr_obj
     129    cdef bint next_item_found
     130    cdef list heads = []
     131    cdef list tails = []
     132    cdef list tailsets = []
     133    for curr_obj in args:
     134        curr_tail = getattr(curr_obj, attribute)
     135        heads.append(curr_tail[0])
     136        tmp_tail = PyList_GetSlice(curr_tail,1,PyList_GET_SIZE(curr_tail))
     137        PyList_Reverse(tmp_tail)
     138        tails.append(tmp_tail)
     139        tailsets.append(set(tmp_tail))
     140    cdef int i,j, lenargs
     141    lenargs = len(heads)
     142    for i from 0<=i<lenargs:
     143        curr_tail = <list>PyList_GET_ITEM(tails,i)
     144        if curr_tail is None:
     145            continue
     146        curr_set = <set>PyList_GET_ITEM(tailsets,i)
     147        O = <object>PyList_GET_ITEM(heads,i)
     148        next_item_found=True
     149        for j from 0<=j<i:
     150            tmp_tail = <list>PyList_GET_ITEM(tails,j)
     151            if tmp_tail is None:
     152                continue
     153            tmp_set = <set>PyList_GET_ITEM(tailsets,j)
     154            X = <object>PyList_GET_ITEM(heads,j)
     155            if X is O:
     156                try:
     157                    X = tmp_tail.pop()
     158                    heads[j] = X
     159                    tmp_set.remove(X)
     160                except IndexError:
     161                    tails[j] = None
     162            elif O in tmp_set:
     163                next_item_found=False
     164                break
     165        if next_item_found:
     166            for j from i<j<lenargs:
     167                tmp_tail = <list>PyList_GET_ITEM(tails,j)
     168                if tmp_tail is None:
     169                    continue
     170                tmp_set = <set>PyList_GET_ITEM(tailsets,j)
     171                X = <object>PyList_GET_ITEM(heads,j)
     172                if X is O:
     173                    try:
     174                        X = tmp_tail.pop()
     175                        heads[j] = X
     176                        tmp_set.remove(X)
     177                    except IndexError:
     178                        tails[j] = None
     179                elif O in tmp_set:
     180                    next_item_found=False
     181                    break
     182        if next_item_found:
     183            out.append(O)
     184            try:
     185                O = curr_tail.pop()
     186                heads[i] = O
     187                curr_set.remove(O)
     188            except IndexError:
     189                tails[i] = None
     190
     191            i = -1
     192    # Either we need to raise an error, or the list is done.
     193    if tails.count(None)<lenargs:
     194        raise ValueError, "Can not merge the items %s."%', '.join([repr(heads[i]) for i,t in enumerate(tails) if t is not None])
     195    return out