Functors¶
AUTHORS:
 David Kohel and William Stein
 David Joyner (20051217): examples
 Robert Bradshaw (20070623): Pyrexify
 Simon King (20100430): more examples, several bug fixes, reimplementation of the default call method, making functors applicable to morphisms (not only to objects)
 Simon King (201012): Pickling of functors without loosing domain and codomain

sage.categories.functor.
ForgetfulFunctor
(domain, codomain)¶ Construct the forgetful function from one category to another.
INPUT:
C
,D
 two categoriesOUTPUT:
A functor that returns the corresponding object of
D
for any element ofC
, by forgetting the extra structure.ASSUMPTION:
The category
C
must be a subcategory ofD
.EXAMPLES:
sage: rings = Rings() sage: abgrps = CommutativeAdditiveGroups() sage: F = ForgetfulFunctor(rings, abgrps) sage: F The forgetful functor from Category of rings to Category of commutative additive groups
It would be a mistake to call it in opposite order:
sage: F = ForgetfulFunctor(abgrps, rings) Traceback (most recent call last): ... ValueError: Forgetful functor not supported for domain Category of commutative additive groups
If both categories are equal, the forgetful functor is the same as the identity functor:
sage: ForgetfulFunctor(abgrps, abgrps) == IdentityFunctor(abgrps) True

class
sage.categories.functor.
ForgetfulFunctor_generic
¶ Bases:
sage.categories.functor.Functor
The forgetful functor, i.e., embedding of a subcategory.
NOTE:
Forgetful functors should be created using
ForgetfulFunctor()
, since the init method of this class does not check whether the domain is a subcategory of the codomain.EXAMPLES:
sage: F = ForgetfulFunctor(FiniteFields(),Fields()) #indirect doctest sage: F The forgetful functor from Category of finite enumerated fields to Category of fields sage: F(GF(3)) Finite Field of size 3

class
sage.categories.functor.
Functor
¶ Bases:
sage.structure.sage_object.SageObject
A class for functors between two categories
NOTE:
 In the first place, a functor is given by its domain and codomain, which are both categories.
 When defining a subclass, the user should not implement a call method.
Instead, one should implement three methods, which are composed in the
default call method:
_coerce_into_domain(self, x)
: Return an object ofself
’s domain, corresponding tox
, or raise aTypeError
. Default: Raise
TypeError
ifx
is not inself
’s domain.
 Default: Raise
_apply_functor(self, x)
: Applyself
to an objectx
ofself
’s domain. Default: Conversion into
self
’s codomain.
 Default: Conversion into
_apply_functor_to_morphism(self, f)
: Applyself
to a morphismf
inself
’s domain.  Default: Returnself(f.domain()).hom(f,self(f.codomain()))
.
EXAMPLES:
sage: rings = Rings() sage: abgrps = CommutativeAdditiveGroups() sage: F = ForgetfulFunctor(rings, abgrps) sage: F.domain() Category of rings sage: F.codomain() Category of commutative additive groups sage: from sage.categories.functor import is_Functor sage: is_Functor(F) True sage: I = IdentityFunctor(abgrps) sage: I The identity functor on Category of commutative additive groups sage: I.domain() Category of commutative additive groups sage: is_Functor(I) True
Note that by default, an instance of the class Functor is coercion from the domain into the codomain. The above subclasses overloaded this behaviour. Here we illustrate the default:
sage: from sage.categories.functor import Functor sage: F = Functor(Rings(),Fields()) sage: F Functor from Category of rings to Category of fields sage: F(ZZ) Rational Field sage: F(GF(2)) Finite Field of size 2
Functors are not only about the objects of a category, but also about their morphisms. We illustrate it, again, with the coercion functor from rings to fields.
sage: R1.<x> = ZZ[] sage: R2.<a,b> = QQ[] sage: f = R1.hom([a+b],R2) sage: f Ring morphism: From: Univariate Polynomial Ring in x over Integer Ring To: Multivariate Polynomial Ring in a, b over Rational Field Defn: x > a + b sage: F(f) Ring morphism: From: Fraction Field of Univariate Polynomial Ring in x over Integer Ring To: Fraction Field of Multivariate Polynomial Ring in a, b over Rational Field Defn: x > a + b sage: F(f)(1/x) 1/(a + b)
We can also apply a polynomial ring construction functor to our homomorphism. The result is a homomorphism that is defined on the base ring:
sage: F = QQ['t'].construction()[0] sage: F Poly[t] sage: F(f) Ring morphism: From: Univariate Polynomial Ring in t over Univariate Polynomial Ring in x over Integer Ring To: Univariate Polynomial Ring in t over Multivariate Polynomial Ring in a, b over Rational Field Defn: Induced from base ring by Ring morphism: From: Univariate Polynomial Ring in x over Integer Ring To: Multivariate Polynomial Ring in a, b over Rational Field Defn: x > a + b sage: p = R1['t']('(x^2 + x)*t^2 + (x^2  x)*t  4*x^2  x + 1') sage: F(f)(p) (a^2  2*a*b  b^2 + a + b)*t^2 + (a^2 + 2*a*b + b^2  a  b)*t  4*a^2  8*a*b  4*b^2  a  b + 1

codomain
()¶ The codomain of self
EXAMPLES:
sage: F = ForgetfulFunctor(FiniteFields(),Fields()) sage: F.codomain() Category of fields

domain
()¶ The domain of self
EXAMPLES:
sage: F = ForgetfulFunctor(FiniteFields(),Fields()) sage: F.domain() Category of finite enumerated fields

sage.categories.functor.
IdentityFunctor
(C)¶ Construct the identity functor of the given category.
INPUT:
A category,
C
.OUTPUT:
The identity functor in
C
.EXAMPLES:
sage: rings = Rings() sage: F = IdentityFunctor(rings) sage: F(ZZ['x','y']) is ZZ['x','y'] True

class
sage.categories.functor.
IdentityFunctor_generic
(C)¶ Bases:
sage.categories.functor.ForgetfulFunctor_generic
Generic identity functor on any category
NOTE:
This usually is created using
IdentityFunctor()
.EXAMPLES:
sage: F = IdentityFunctor(Fields()) #indirect doctest sage: F The identity functor on Category of fields sage: F(RR) is RR True sage: F(ZZ) Traceback (most recent call last): ... TypeError: x (=Integer Ring) is not in Category of fields

sage.categories.functor.
is_Functor
(x)¶ Test whether the argument is a functor
NOTE:
There is a deprecation warning when using it from top level. Therefore we import it in our doc test.
EXAMPLES:
sage: from sage.categories.functor import is_Functor sage: F1 = QQ.construction()[0] sage: F1 FractionField sage: is_Functor(F1) True sage: is_Functor(FractionField) False sage: F2 = ForgetfulFunctor(Fields(), Rings()) sage: F2 The forgetful functor from Category of fields to Category of rings sage: is_Functor(F2) True