Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

r""" 

Examples of monoids 

""" 

from __future__ import absolute_import 

#***************************************************************************** 

# Copyright (C) 2008-2009 Nicolas M. Thiery <nthiery at users.sf.net> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# http://www.gnu.org/licenses/ 

#****************************************************************************** 

 

from sage.misc.cachefunc import cached_method 

from sage.structure.parent import Parent 

from sage.structure.element_wrapper import ElementWrapper 

from sage.categories.all import Monoids 

from .semigroups import FreeSemigroup 

from sage.sets.family import Family 

 

class FreeMonoid(FreeSemigroup): 

r""" 

An example of a monoid: the free monoid 

 

This class illustrates a minimal implementation of a monoid. For a 

full featured implementation of free monoids, see :func:`FreeMonoid`. 

 

EXAMPLES:: 

 

sage: S = Monoids().example(); S 

An example of a monoid: the free monoid generated by ('a', 'b', 'c', 'd') 

 

sage: S.category() 

Category of monoids 

 

This is the free semigroup generated by:: 

 

sage: S.semigroup_generators() 

Family ('a', 'b', 'c', 'd') 

 

with product rule given by concatenation of words:: 

 

sage: S('dab') * S('acb') 

'dabacb' 

 

and unit given by the empty word:: 

 

sage: S.one() 

'' 

 

We conclude by running systematic tests on this monoid:: 

 

sage: TestSuite(S).run(verbose = True) 

running ._test_an_element() . . . pass 

running ._test_associativity() . . . pass 

running ._test_cardinality() . . . pass 

running ._test_category() . . . pass 

running ._test_elements() . . . 

Running the test suite of self.an_element() 

running ._test_category() . . . pass 

running ._test_eq() . . . pass 

running ._test_new() . . . pass 

running ._test_not_implemented_methods() . . . pass 

running ._test_pickling() . . . pass 

pass 

running ._test_elements_eq_reflexive() . . . pass 

running ._test_elements_eq_symmetric() . . . pass 

running ._test_elements_eq_transitive() . . . pass 

running ._test_elements_neq() . . . pass 

running ._test_eq() . . . pass 

running ._test_new() . . . pass 

running ._test_not_implemented_methods() . . . pass 

running ._test_one() . . . pass 

running ._test_pickling() . . . pass 

running ._test_prod() . . . pass 

running ._test_some_elements() . . . pass 

""" 

 

def __init__(self, alphabet=('a','b','c','d')): 

r""" 

The free monoid 

 

INPUT: 

 

- ``alphabet`` -- a tuple of strings: the generators of the monoid 

 

EXAMPLES:: 

 

sage: M = Monoids().example(alphabet=('a','b','c')); M 

An example of a monoid: the free monoid generated by ('a', 'b', 'c') 

 

TESTS:: 

 

sage: TestSuite(M).run() 

 

""" 

self.alphabet = alphabet 

Parent.__init__(self, category = Monoids()) 

 

def _repr_(self): 

r""" 

TESTS:: 

 

sage: M = Monoids().example(alphabet=('a','b','c')) 

sage: M._repr_() 

"An example of a monoid: the free monoid generated by ('a', 'b', 'c')" 

 

""" 

return "An example of a monoid: the free monoid generated by %s"%(self.alphabet,) 

 

@cached_method 

def one(self): 

r""" 

Returns the one of the monoid, as per :meth:`Monoids.ParentMethods.one`. 

 

EXAMPLES:: 

 

sage: M = Monoids().example(); M 

An example of a monoid: the free monoid generated by ('a', 'b', 'c', 'd') 

sage: M.one() 

'' 

 

""" 

return self("") 

 

@cached_method 

def monoid_generators(self): 

r""" 

Return the generators of this monoid. 

 

EXAMPLES:: 

 

sage: M = Monoids().example(); M 

An example of a monoid: the free monoid generated by ('a', 'b', 'c', 'd') 

sage: M.monoid_generators() 

Finite family {'a': 'a', 'c': 'c', 'b': 'b', 'd': 'd'} 

sage: a,b,c,d = M.monoid_generators() 

sage: a*d*c*b 

'adcb' 

""" 

return Family(self.alphabet, self) 

 

class Element (ElementWrapper): 

wrapped_class = str 

 

Example = FreeMonoid