Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/categories/examples/monoids.py
4057 views
1
r"""
2
Examples of monoids
3
"""
4
#*****************************************************************************
5
# Copyright (C) 2008-2009 Nicolas M. Thiery <nthiery at users.sf.net>
6
#
7
# Distributed under the terms of the GNU General Public License (GPL)
8
# http://www.gnu.org/licenses/
9
#******************************************************************************
10
11
from sage.misc.cachefunc import cached_method
12
from sage.structure.parent import Parent
13
from sage.structure.element_wrapper import ElementWrapper
14
from sage.categories.all import Monoids
15
from semigroups import FreeSemigroup
16
17
class FreeMonoid(FreeSemigroup):
18
r"""
19
An example of a monoid: the free monoid
20
21
This class illustrates a minimal implementation of a monoid.
22
23
EXAMPLES::
24
25
sage: S = Monoids().example(); S
26
An example of a monoid: the free monoid generated by ('a', 'b', 'c', 'd')
27
28
sage: S.category()
29
Category of monoids
30
31
This is the free semigroup generated by::
32
33
sage: S.semigroup_generators()
34
Family ('a', 'b', 'c', 'd')
35
36
with product rule given by $a \times b = a$ for all $a, b$::
37
38
sage: S('dab') * S('acb')
39
'dabacb'
40
41
We conclude by running systematic tests on this monoid::
42
43
sage: TestSuite(S).run(verbose = True)
44
running ._test_an_element() . . . pass
45
running ._test_associativity() . . . pass
46
running ._test_category() . . . pass
47
running ._test_elements() . . .
48
Running the test suite of self.an_element()
49
running ._test_category() . . . pass
50
running ._test_eq() . . . pass
51
running ._test_not_implemented_methods() . . . pass
52
running ._test_pickling() . . . pass
53
pass
54
running ._test_elements_eq() . . . pass
55
running ._test_eq() . . . pass
56
running ._test_not_implemented_methods() . . . pass
57
running ._test_one() . . . pass
58
running ._test_pickling() . . . pass
59
running ._test_prod() . . . pass
60
running ._test_some_elements() . . . pass
61
"""
62
63
def __init__(self, alphabet=('a','b','c','d')):
64
r"""
65
The free monoid
66
67
INPUT::
68
69
- ``alphabet`` -- a tuple of strings: the generators of the monoid
70
71
EXAMPLES::
72
73
sage: M = Monoids().example(alphabet=('a','b','c')); M
74
An example of a monoid: the free monoid generated by ('a', 'b', 'c')
75
76
TESTS::
77
78
sage: TestSuite(M).run()
79
80
"""
81
self.alphabet = alphabet
82
Parent.__init__(self, category = Monoids())
83
84
def _repr_(self):
85
r"""
86
TESTS::
87
88
sage: M = Monoids().example(alphabet=('a','b','c'))
89
sage: M._repr_()
90
"An example of a monoid: the free monoid generated by ('a', 'b', 'c')"
91
92
"""
93
return "An example of a monoid: the free monoid generated by %s"%(self.alphabet,)
94
95
@cached_method
96
def one(self):
97
r"""
98
Returns the one of the monoid, as per :meth:`Monoids.ParentMethods.one`.
99
100
EXAMPLES::
101
102
sage: M = Monoids().example(); M
103
An example of a monoid: the free monoid generated by ('a', 'b', 'c', 'd')
104
sage: M.one()
105
''
106
107
"""
108
return self("")
109
110
class Element (ElementWrapper):
111
wrapped_class = str
112
113
Example = FreeMonoid
114
115