@@ -82,85 +82,69 @@ extension Automaton {
8282 }
8383}
8484
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 > ( )
11098
111- return true
112- }
99+ var result = Automaton ( )
113100
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
119103 }
120- }
121104
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+ }
123113
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 }
127115
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]
132119
133- if !isIrreducible( word) {
134- continue
135- }
120+ if prefixes. contains ( word) {
121+ result. transitions. append ( ( prefix, x, word) )
122+ continue
123+ }
136124
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... ] )
139131
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+ }
143136 }
144137 }
145138 }
146- }
147-
148- return result
149- }
150139
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
155141 }
156142
157143 /// Returns the number of irreducible words in this monoid presentation, or
158144 /// 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 ( )
164148 if automaton. hasStar {
165149 return nil
166150 }
0 commit comments