Ticket #3738: 3738-5-coerce_compat_layer.patch

File 3738-5-coerce_compat_layer.patch, 15.9 KB (added by robertwb, 11 years ago)
  • sage/categories/homset.py

    # HG changeset patch
    # User Robert Bradshaw <robertwb@math.washington.edu>
    # Date 1217319877 25200
    # Node ID d8c9400d07d71d25dea7cb8f604aee2dedb7c644
    # Parent  42946824190e651af18848bcf3cf0f7c92cfe479
    Compatability layer between old parents and new coercion model.
    
    Sage starts up and seems to work fine.
    
    diff -r 42946824190e -r d8c9400d07d7 sage/categories/homset.py
    a b class Homset(Set_generic): 
    246246        return "Set of Morphisms from %s to %s in %s"%(
    247247            self._domain, self._codomain, self.__category)
    248248
     249    def __nonzero__(self):
     250        return True
     251
    249252    def homset_category(self):
    250253        """
    251254        Return the category that this is a Hom in, i.e., this is
  • sage/structure/all.py

    diff -r 42946824190e -r d8c9400d07d7 sage/structure/all.py
    a b from element import (\ 
    2222    is_PrincipalIdealDomainElement,
    2323    is_RingElement,
    2424    is_Vector,
     25   
     26    get_coercion_model,
     27    coercion_traceback
    2528    )
    2629
    2730from parent      import Parent, is_Parent
  • sage/structure/coerce_maps.pyx

    diff -r 42946824190e -r d8c9400d07d7 sage/structure/coerce_maps.pyx
    a b cdef bint print_warnings = 0 
    1616
    1717cdef class DefaultConvertMorphism(Morphism):
    1818    """
    19     This morphism simply differs action to the codomain's element_class method,
     19    This morphism simply differs action to the codomain's element_constructor method,
    2020    passing in the codomain as the first argument.
    2121    """
    2222    def __init__(self, domain, codomain, force_use=False):
    cdef class DefaultConvertMorphism(Morphi 
    2525        Morphism.__init__(self, homset.Hom(domain, codomain))
    2626        self._coerce_cost = 100
    2727        self._force_use = force_use
    28         if self._codomain._element_class is None:
    29             raise RuntimeError, "BUG in coercion model, no element class for %s" % type(self._codomain)
     28        if self._codomain._element_constructor is None:
     29            raise RuntimeError, "BUG in coercion model, no element constructor for %s" % type(self._codomain)
    3030
    3131    cpdef Element _call_(self, x):
    3232        try:
    33             return self._codomain._element_class(self._codomain, x)
     33            return self._codomain._element_constructor(self._codomain, x)
    3434        except:
    3535            if print_warnings:
    3636                print type(self._codomain), self._codomain
    37                 print type(self._codomain._element_class), self._codomain._element_class
     37                print type(self._codomain._element_constructor), self._codomain._element_constructor
    3838            raise
    3939
    4040    cpdef Element _call_with_args(self, x, args=(), kwds={}):
    4141        try:
    4242            if len(args) == 0:
    4343                if len(kwds) == 0:
    44                     return self._codomain._element_class(self._codomain, x)
     44                    return self._codomain.__element_constructor(self._codomain, x)
    4545                else:
    46                     return self._codomain._element_class(self._codomain, x, **kwds)
     46                    return self._codomain._element_constructor(self._codomain, x, **kwds)
    4747            else:
    4848                if len(kwds) == 0:
    49                     return self._codomain._element_class(self._codomain, x, *args)
     49                    return self._codomain._element_constructor(self._codomain, x, *args)
    5050                else:
    51                     return self._codomain._element_class(self._codomain, x, *args, **kwds)
     51                    return self._codomain._element_constructor(self._codomain, x, *args, **kwds)
    5252        except:
    5353            if print_warnings:
    5454                print type(self._codomain), self._codomain
    55                 print type(self._codomain._element_class), self._codomain._element_class
     55                print type(self._codomain._element_constructor), self._codomain._element_constructor
    5656            raise
    5757
    5858    def _repr_type(self):
    cdef class DefaultConvertMorphism(Morphi 
    6060
    6161cdef class DefaultConvertMorphism_unique(DefaultConvertMorphism):
    6262    """
    63     This morphism simply differs action to the codomain's element_class method,
     63    This morphism simply differs action to the codomain's element_constructor method,
    6464    WITHOUT passing in the codomain as the first argument.
    6565
    6666    This is used for creating elements that don't take a parent as the first argument
    6767    to their __init__ method, for example, Integers, Rationals, Algebraic Reals... all
    68     have a unique parent. It is also used when the element_class is a bound method
     68    have a unique parent. It is also used when the element_constructor is a bound method
    6969    (whose self argument is assumed to be bound to the codomain).
    7070    """
    7171    cpdef Element _call_(self, x):
    7272        try:
    73             return self._codomain._element_class(x)
     73            return self._codomain._element_constructor(x)
    7474        except:
    7575            if print_warnings:
    7676                print type(self._codomain), self._codomain
    77                 print type(self._codomain._element_class), self._codomain._element_class
     77                print type(self._codomain._element_constructor), self._codomain._element_constructor
    7878            raise
    7979
    8080    cpdef Element _call_with_args(self, x, args=(), kwds={}):
    8181        try:
    8282            if len(args) == 0:
    8383                if len(kwds) == 0:
    84                     return self._codomain._element_class(x)
     84                    return self._codomain._element_constructor(x)
    8585                else:
    86                     return self._codomain._element_class(x, **kwds)
     86                    return self._codomain._element_constructor(x, **kwds)
    8787            else:
    8888                if len(kwds) == 0:
    89                     return self._codomain._element_class(x, *args)
     89                    return self._codomain._element_constructor(x, *args)
    9090                else:
    91                     return self._codomain._element_class(x, *args, **kwds)
     91                    return self._codomain._element_constructor(x, *args, **kwds)
    9292        except:
    9393            if print_warnings:
    9494                print type(self._codomain), self._codomain
    95                 print type(self._codomain._element_class), self._codomain._element_class
     95                print type(self._codomain._element_constructor), self._codomain._element_constructor
    9696            raise
    9797
    9898    def _repr_type(self):
  • sage/structure/element.pyx

    diff -r 42946824190e -r d8c9400d07d7 sage/structure/element.pyx
    a b def set_coercion_model(cm): 
    27732773    global coercion_model
    27742774    coercion_model = cm
    27752775
     2776def coercion_traceback(dump=True):
     2777    if dump:
     2778        for e in coercion_model.exception_stack():
     2779            print e
     2780    else:
     2781        return coercion_model.exception_stack()
    27762782
    27772783###############################################################################
    27782784
  • sage/structure/parent_old.pyx

    diff -r 42946824190e -r d8c9400d07d7 sage/structure/parent_old.pyx
    a b This came up in some subtle bug once. 
    2525
    2626cimport sage_object
    2727import operator
     28from parent import Set_PythonType, Set_PythonType_class
    2829
    2930include '../ext/python_object.pxi'
    3031include '../ext/python_bool.pxi'
    cdef class Parent(parent.Parent): 
    7374        self._action_list = list(actions)
    7475        self._action_hash = {}
    7576       
    76         cdef Parent other
     77        cdef parent.Parent other
    7778        for mor in embeddings:
    7879            other = mor.domain()
    7980            print "embedding", self, " --> ", other
    cdef class Parent(parent.Parent): 
    130131    # New Coercion support functionality
    131132    #################################################################################
    132133   
    133     def coerce_map_from(self, S):
    134         return self.coerce_map_from_c(S)
     134#    def coerce_map_from(self, S):
     135#        return self.coerce_map_from_c(S)
    135136   
    136137    cpdef coerce_map_from_c(self, S):
    137138        if S is self:
    cdef class Parent(parent.Parent): 
    174175        import sage.categories.morphism
    175176        from sage.categories.morphism import Morphism
    176177        from sage.categories.homset import Hom
    177         cdef Parent R
     178        cdef parent.Parent R
    178179        for mor in self._coerce_from_list:
    179180            if PY_TYPE_CHECK(mor, Morphism):
    180181                R = mor.domain()
    cdef class Parent(parent.Parent): 
    186187            if R is S:
    187188                return mor
    188189            else:
    189                 connecting = R.coerce_map_from_c(S)
     190                connecting = R.coerce_map_from(S)
    190191                if connecting is not None:
    191192                    return mor * connecting
    192193               
    cdef class Parent(parent.Parent): 
    195196        if self.has_coerce_map_from_c(S):
    196197            if isinstance(S, type):
    197198                S = Set_PythonType(S)
    198             return sage.categories.morphism.FormalCoercionMorphism(Hom(S, self))
     199            return sage.categories.morphism.CallMorphism(Hom(S, self))
    199200        else:
    200201            return None
    201202   
    202     def get_action(self, S, op=operator.mul, self_on_left=True):
    203         return self.get_action_c(S, op, self_on_left)
     203#    def get_action(self, S, op=operator.mul, self_on_left=True):
     204#        return self.get_action_c(S, op, self_on_left)
    204205       
    205206    cpdef get_action_c(self, S, op, bint self_on_left):
    206207        try:
    cdef class Parent(parent.Parent): 
    230231        from sage.categories.morphism import Morphism
    231232        from sage.categories.homset import Hom
    232233        from coerce_actions import LeftModuleAction, RightModuleAction
    233         cdef Parent R
     234        cdef parent.Parent R
    234235        for action in self._action_list:
    235236            if PY_TYPE_CHECK(action, Action):
    236237                if self_on_left:
    cdef class Parent(parent.Parent): 
    265266                        return PrecomposedAction(action, connecting, None)
    266267               
    267268
    268         if op is operator.mul and PY_TYPE_CHECK(S, Parent):
     269        if op is operator.mul and PY_TYPE_CHECK(S, parent.Parent):
    269270            from coerce_actions import LeftModuleAction, RightModuleAction, LAction, RAction
    270271            # Actors define _l_action_ and _r_action_
    271272            # Acted-on elements define _lmul_ and _rmul_
    cdef class Parent(parent.Parent): 
    276277            # then _an_element_c() == 0 could be very bad.
    277278            #
    278279            #
    279             x = self._an_element_c()
    280             y = (<Parent>S)._an_element_c()
     280            x = self.an_element()
     281            y = (<parent.Parent>S).an_element()
    281282#            print "looking for action ", self, "<--->", S
    282283#            print "looking for action ", x, "<--->", y
    283284           
    cdef class Parent(parent.Parent): 
    418419            pass
    419420        raise TypeError, "no canonical coercion to self defined"
    420421
    421     def has_coerce_map_from(self, S):
    422         return self.has_coerce_map_from_c(S)
     422#    def has_coerce_map_from(self, S):
     423#        return self.has_coerce_map_from_c(S)
    423424
    424425    cpdef has_coerce_map_from_c(self, S):
    425426        """
    cdef class Parent(parent.Parent): 
    446447        return self.has_coerce_map_from_c_impl(S)
    447448
    448449    cdef has_coerce_map_from_c_impl(self, S):
    449         if not PY_TYPE_CHECK(S, Parent):
     450        if not PY_TYPE_CHECK(S, parent.Parent):
    450451            return False
    451452        try:
    452             self._coerce_c((<Parent>S)._an_element_c())
     453            self._coerce_c((<parent.Parent>S).an_element())
    453454        except TypeError:
    454455            return False
    455456        except NotImplementedError, msg:
    456457            raise NotImplementedError, "%s\nAlso, please make sure you have implemented has_coerce_map_from_impl or has_coerce_map_from_c_impl (or better _an_element_c_impl or _an_element_impl if possible) for %s"%(msg,self)
    457458        return True
    458459
    459     def _an_element_impl(self):     # override this in Python
     460    cpdef _an_element_impl(self):     # override this in Python
    460461        r"""
    461462        Implementation of a function that returns an element (often non-trivial)
    462463        of a parent object.  Every parent object should implement it,
    cdef class Parent(parent.Parent): 
    514515        """
    515516        cdef int r
    516517
    517         if not PY_TYPE_CHECK(right, Parent) or not PY_TYPE_CHECK(left, Parent):
     518        if not PY_TYPE_CHECK(right, parent.Parent) or not PY_TYPE_CHECK(left, parent.Parent):
    518519            # One is not a parent -- use arbitrary ordering
    519520            if (<PyObject*>left) < (<PyObject*>right):
    520521                r = -1
    cdef class Parent(parent.Parent): 
    615616        from sage.categories.all import Hom
    616617        return Hom(self, codomain, cat)
    617618
    618 
    619619    ############################################################################
    620     # Set baseclass --
     620    # Coercion Compatibility Layer
    621621    ############################################################################
    622622
     623    cpdef _coerce_map_from_(self, S):
     624        return self.coerce_map_from_c(S)
     625   
     626    cpdef _get_action_(self, other, op, bint self_on_left):
     627        return self.get_action_c(other, op, self_on_left)
     628   
     629    cpdef _an_element_(self):
     630        return self._an_element_c()
     631       
     632    cpdef _generic_convert_map(self, S):
     633        if self._element_constructor is None:
     634            from sage.categories.morphism import CallMorphism
     635            from sage.categories.homset import Hom
     636            return CallMorphism(Hom(S, self))
     637        else:
     638            return parent.Parent._generic_convert_map(self, S)
     639       
    623640
    624 class Set_generic(Parent): # Cannot use Parent because Element._parent is ParentWithBase
    625     """
    626     Abstract base class for sets.
    627     """
    628     def category(self):
    629         """
    630         The category that this set belongs to, which is the category
    631         of all sets.
    632641
    633         EXAMPLES:
    634             sage: Set(QQ).category()
    635             Category of sets
    636         """
    637         import sage.categories.all
    638         return sage.categories.all.Sets()
    639 
    640     def object(self):
    641         return self
    642 
    643 
    644 cdef _set_cache = {}
    645 def Set_PythonType(t):
    646     """
    647     The set of all object of a given type. This is primarily used to fit
    648     types (such as int, float, list) into the coercion model (as domains
    649     and codomains must be Sage Parents).
    650    
    651     EXAMPLES:
    652         sage: from sage.structure.parent import Set_PythonType
    653         sage: Set_PythonType(int) is Set_PythonType(int)
    654         True
    655     """
    656     try:
    657         return _set_cache[t]
    658     except KeyError:
    659         _set_cache[t] = s = Set_PythonType_class(t)
    660         return s
    661 
    662 class Set_PythonType_class(Set_generic):
    663 
    664     def __init__(self, theType):
    665         self._type = theType
    666 
    667     def __call__(self, x):
    668         return self._type(x)
    669 
    670     def __hash__(self):
    671         return hash(self._type)
    672        
    673     def __cmp__(self, other):
    674         if self is other:
    675             return 0
    676         if isinstance(other, Set_PythonType_class):
    677             return cmp(self._type, other._type)
    678         else:
    679             return cmp(self._type, other)
    680 
    681     def __contains__(self, x):
    682         return isinstance(x, self._type)
    683 
    684     def _latex_(self):
    685         return self._repr_()
    686 
    687     def _repr_(self):
    688         return "Set of Python objects of %s"%(str(self._type)[1:-1])
    689 
    690     def object(self):
    691         return self._type
    692 
    693     def cardinality(self):
    694         if self._type is bool:
    695             return 2
    696         else:
    697             import sage.rings.infinity
    698             return sage.rings.infinity.infinity
    699 
     642############################################################################
     643# Set baseclass --
     644############################################################################
    700645
    701646# These functions are to guerentee that user defined _lmul_, _rmul_, _l_action_, _r_action_ do
    702647# not in turn call __mul__ on their arguments, leading to an infinite loop.