@@ -82,85 +82,69 @@ extension Automaton {
82
82
}
83
83
}
84
84
85
- /// Constructs an automaton to recognize the complement of this regular set:
86
- ///
87
- /// .*(x1|x2|...).*
88
- ///
89
- /// where 'words' is [x1, x2, ...].
90
- ///
91
- /// This is Lemma 2.1.3 in:
92
- ///
93
- /// String Rewriting Systems, R.V. Book, F. Otto 1993. Springer New York.
94
- func buildAutomaton( _ words: [ Word ] , _ alphabet: Int ) -> Automaton {
95
- // Proper prefixes of each word.
96
- var prefixes = Set < Word > ( )
97
-
98
- var result = Automaton ( )
99
-
100
- func isIrreducible( _ word: Word ) -> Bool {
101
- for i in 0 ..< word. count {
102
- for other in words {
103
- if i + other. count <= word. count {
104
- if Word ( word [ i ..< ( i + other. count) ] ) == other {
105
- return false
106
- }
107
- }
108
- }
109
- }
85
+ extension RewritingSystem {
86
+ /// Constructs an automaton to recognize the complement of this regular set:
87
+ ///
88
+ /// .*(x1|x2|...).*
89
+ ///
90
+ /// where 'words' is [x1, x2, ...].
91
+ ///
92
+ /// This is Lemma 2.1.3 in:
93
+ ///
94
+ /// String Rewriting Systems, R.V. Book, F. Otto 1993. Springer New York.
95
+ func buildAutomaton( ) -> Automaton {
96
+ // Proper prefixes of each word.
97
+ var prefixes = Set < Word > ( )
110
98
111
- return true
112
- }
99
+ var result = Automaton ( )
113
100
114
- prefixes. insert ( [ ] )
115
- for word in words {
116
- for i in 0 ..< word. count {
117
- let prefix = Word ( word [ 0 ..< i] )
118
- prefixes. insert ( prefix)
101
+ func isIrreducible( _ word: Word ) -> Bool {
102
+ return reduceOne ( word) == nil
119
103
}
120
- }
121
104
122
- result. states = prefixes. sorted { compare ( $0, $1, order: . shortlex) == . lessThan }
105
+ prefixes. insert ( [ ] )
106
+ for rule in presentation. rules {
107
+ let word = rule. lhs
108
+ for i in 0 ..< word. count {
109
+ let prefix = Word ( word [ 0 ..< i] )
110
+ prefixes. insert ( prefix)
111
+ }
112
+ }
123
113
124
- for prefix in prefixes {
125
- for x in 0 ..< UInt8 ( alphabet) {
126
- let word = prefix + [ x]
114
+ result. states = prefixes. sorted { compare ( $0, $1, order: . shortlex) == . lessThan }
127
115
128
- if prefixes. contains ( word) {
129
- result. transitions. append ( ( prefix, x, word) )
130
- continue
131
- }
116
+ for prefix in prefixes {
117
+ for x in 0 ..< UInt8 ( alphabet) {
118
+ let word = prefix + [ x]
132
119
133
- if !isIrreducible( word) {
134
- continue
135
- }
120
+ if prefixes. contains ( word) {
121
+ result. transitions. append ( ( prefix, x, word) )
122
+ continue
123
+ }
136
124
137
- for i in 1 ... word. count {
138
- let suffix = Word ( word [ i... ] )
125
+ if !isIrreducible( word) {
126
+ continue
127
+ }
128
+
129
+ for i in 1 ... word. count {
130
+ let suffix = Word ( word [ i... ] )
139
131
140
- if prefixes. contains ( suffix) {
141
- result. transitions. append ( ( prefix, x, suffix) )
142
- break
132
+ if prefixes. contains ( suffix) {
133
+ result. transitions. append ( ( prefix, x, suffix) )
134
+ break
135
+ }
143
136
}
144
137
}
145
138
}
146
- }
147
-
148
- return result
149
- }
150
139
151
- extension Presentation {
152
- /// The Irr(R) automaton.
153
- func automaton( alphabet: Int ) -> Automaton {
154
- return buildAutomaton ( rules. map { $0. lhs } , alphabet)
140
+ return result
155
141
}
156
142
157
143
/// Returns the number of irreducible words in this monoid presentation, or
158
144
/// nil if this set is infinite.
159
- ///
160
- /// If the presentation is complete, this is the cardinality of the
161
- /// presented monoid. Otherwise, it is an upper bound.
162
- func cardinality( alphabet: Int ) -> Int ? {
163
- let automaton = automaton ( alphabet: alphabet)
145
+ var cardinality : Int ? {
146
+ precondition ( state == . complete)
147
+ let automaton = buildAutomaton ( )
164
148
if automaton. hasStar {
165
149
return nil
166
150
}
0 commit comments