20 Suc ("_+1" [100] 100) and |
20 Suc ("_+1" [100] 100) and |
21 quotient ("_ \<^raw:\ensuremath{\!\sslash\!}> _" [90, 90] 90) and |
21 quotient ("_ \<^raw:\ensuremath{\!\sslash\!}> _" [90, 90] 90) and |
22 REL ("\<approx>") and |
22 REL ("\<approx>") and |
23 UPLUS ("_ \<^raw:\ensuremath{\uplus}> _" [90, 90] 90) and |
23 UPLUS ("_ \<^raw:\ensuremath{\uplus}> _" [90, 90] 90) and |
24 L ("L '(_')" [0] 101) and |
24 L ("L '(_')" [0] 101) and |
25 EClass ("\<lbrakk>_\<rbrakk>\<^bsub>_\<^esub>" [100, 100] 100) |
25 EClass ("\<lbrakk>_\<rbrakk>\<^bsub>_\<^esub>" [100, 100] 100) and |
|
26 transition ("_ \<^raw:\ensuremath{\stackrel{\text{>_\<^raw:}}{\Longrightarrow}}> _" [100, 100, 100] 100) |
26 (*>*) |
27 (*>*) |
27 |
28 |
28 |
29 |
29 section {* Introduction *} |
30 section {* Introduction *} |
30 |
31 |
38 |
39 |
39 There is however a problem: the typical approach to regular languages is to |
40 There is however a problem: the typical approach to regular languages is to |
40 introduce finite automata and then define everything in terms of them. For |
41 introduce finite automata and then define everything in terms of them. For |
41 example, a regular language is normally defined as one whose strings are |
42 example, a regular language is normally defined as one whose strings are |
42 recognised by a finite deterministic automaton. This approach has many |
43 recognised by a finite deterministic automaton. This approach has many |
43 benefits. Among them is that it is easy to convince oneself from the fact that |
44 benefits. Among them is the fact that it is easy to convince oneself that |
44 regular languages are closed under complementation: one just has to exchange |
45 regular languages are closed under complementation: one just has to exchange |
45 the accepting and non-accepting states in the corresponding automaton to |
46 the accepting and non-accepting states in the corresponding automaton to |
46 obtain an automaton for the complement language. The problem, however, lies with |
47 obtain an automaton for the complement language. The problem, however, lies with |
47 formalising such reasoning in a HOL-based theorem prover, in our case |
48 formalising such reasoning in a HOL-based theorem prover, in our case |
48 Isabelle/HOL. Automata are build up from states and transitions that |
49 Isabelle/HOL. Automata are build up from states and transitions that |
137 |
138 |
138 Because of these problems to do with representing automata, there seems |
139 Because of these problems to do with representing automata, there seems |
139 to be no substantial formalisation of automata theory and regular languages |
140 to be no substantial formalisation of automata theory and regular languages |
140 carried out in a HOL-based theorem prover. We are only aware of the |
141 carried out in a HOL-based theorem prover. We are only aware of the |
141 large formalisation of automata theory in Nuprl \cite{Constable00} and |
142 large formalisation of automata theory in Nuprl \cite{Constable00} and |
142 some smaller formalisations in Coq, for example \cite{Filliatre97}. |
143 some smaller formalisations in Coq (for example \cite{Filliatre97}). |
143 |
144 |
144 In this paper, we will not attempt to formalise automata theory, but take a completely |
145 In this paper, we will not attempt to formalise automata theory, but take a completely |
145 different approach to regular languages. Instead of defining a regular language as one |
146 different approach to regular languages. Instead of defining a regular language as one |
146 where there exists an automaton that recognises all strings of the language, we define |
147 where there exists an automaton that recognises all strings of the language, we define |
147 a regular language as: |
148 a regular language as: |
151 strings of @{text "A"}. |
152 strings of @{text "A"}. |
152 \end{definition} |
153 \end{definition} |
153 |
154 |
154 \noindent |
155 \noindent |
155 The reason is that regular expressions, unlike graphs and matrices, can |
156 The reason is that regular expressions, unlike graphs and matrices, can |
156 be easily defined as inductive datatype. Therefore a corresponding reasoning |
157 be easily defined as inductive datatype. Consequently a corresponding reasoning |
157 infrastructure comes for free. This has recently been used in HOL4 for formalising regular |
158 infrastructure comes for free. This has recently been exploited in HOL4 with a formalisation |
158 expression matching based on derivatives \cite{OwensSlind08}. The purpose of this paper is to |
159 of regular expression matching based on derivatives \cite{OwensSlind08}. The purpose of this paper is to |
159 show that a central result about regular languages, the Myhill-Nerode theorem, |
160 show that a central result about regular languages---the Myhill-Nerode theorem---can |
160 can be recreated by only using regular expressions. This theorem gives a necessary |
161 be recreated by only using regular expressions. This theorem gives necessary |
161 and sufficient condition for when a language is regular. As a corollary of this |
162 and sufficient conditions for when a language is regular. As a corollary of this |
162 theorem we can easily establish the usual closure properties, including |
163 theorem we can easily establish the usual closure properties, including |
163 complementation, for regular languages.\smallskip |
164 complementation, for regular languages.\smallskip |
164 |
165 |
165 \noindent |
166 \noindent |
166 {\bf Contributions:} To our knowledge, our proof of the Myhill-Nerode theorem is the |
167 {\bf Contributions:} To our knowledge, our proof of the Myhill-Nerode theorem is the |
174 |
175 |
175 text {* |
176 text {* |
176 Strings in Isabelle/HOL are lists of characters with the \emph{empty string} |
177 Strings in Isabelle/HOL are lists of characters with the \emph{empty string} |
177 being represented by the empty list, written @{term "[]"}. \emph{Languages} |
178 being represented by the empty list, written @{term "[]"}. \emph{Languages} |
178 are sets of strings. The language containing all strings is written in |
179 are sets of strings. The language containing all strings is written in |
179 Isabelle/HOL as @{term "UNIV::string set"}. The notation for the quotient |
180 Isabelle/HOL as @{term "UNIV::string set"}. The concatenation of two languages |
180 of a language @{text A} according to a relation @{term REL} is @{term "A // |
181 is written @{term "A ;; B"} and a language raised to the power $n$ is written |
181 REL"}. The concatenation of two languages is written @{term "A ;; B"}; a |
182 @{term "A \<up> n"}. Their definitions are |
182 language raised to the power $n$ is written @{term "A \<up> n"}. Both concepts |
|
183 are defined as |
|
184 |
183 |
185 \begin{center} |
184 \begin{center} |
186 @{thm Seq_def[THEN eq_reflection, where A1="A" and B1="B"]} |
185 @{thm Seq_def[THEN eq_reflection, where A1="A" and B1="B"]} |
187 \hspace{7mm} |
186 \hspace{7mm} |
188 @{thm pow.simps(1)[THEN eq_reflection, where A1="A"]} |
187 @{thm pow.simps(1)[THEN eq_reflection, where A1="A"]} |
190 @{thm pow.simps(2)[THEN eq_reflection, where A1="A" and n1="n"]} |
189 @{thm pow.simps(2)[THEN eq_reflection, where A1="A" and n1="n"]} |
191 \end{center} |
190 \end{center} |
192 |
191 |
193 \noindent |
192 \noindent |
194 where @{text "@"} is the usual list-append operation. The Kleene-star of a language @{text A} |
193 where @{text "@"} is the usual list-append operation. The Kleene-star of a language @{text A} |
195 is defined as the union over all powers, namely @{thm Star_def}. |
194 is defined as the union over all powers, namely @{thm Star_def}. In the paper |
196 |
195 we will often make use of the following properties. |
|
196 |
|
197 \begin{proposition}\label{langprops}\mbox{}\\ |
|
198 \begin{tabular}{@ {}ll@ {\hspace{10mm}}ll} |
|
199 (i) & @{thm star_cases} & (ii) & @{thm[mode=IfThen] pow_length}\\ |
|
200 (iii) & @{thm seq_Union_left} & |
|
201 \end{tabular} |
|
202 \end{proposition} |
|
203 |
|
204 \noindent |
|
205 We omit the proofs of these properties, but invite the reader to consult |
|
206 our formalisation.\footnote{Available at ???} |
|
207 |
|
208 |
|
209 The notation for the quotient of a language @{text A} according to an |
|
210 equivalence relation @{term REL} is @{term "A // REL"}. We will write |
|
211 @{text "\<lbrakk>x\<rbrakk>\<^isub>\<approx>"} for the equivalence class defined |
|
212 as @{text "{y | y \<approx> x}"}. |
|
213 |
|
214 |
197 Central to our proof will be the solution of equational systems |
215 Central to our proof will be the solution of equational systems |
198 involving regular expressions. For this we will use the following ``reverse'' |
216 involving regular expressions. For this we will use Arden's lemma \cite{} |
|
217 which solves equations of the form @{term "X = A ;; X \<union> B"} provided |
|
218 @{term "[] \<notin> A"}. However we will need the following ``reverse'' |
199 version of Arden's lemma. |
219 version of Arden's lemma. |
200 |
220 |
201 \begin{lemma}[Reverse Arden's Lemma]\mbox{}\\ |
221 \begin{lemma}[Reverse Arden's Lemma]\mbox{}\\ |
202 If @{thm (prem 1) ardens_revised} then |
222 If @{thm (prem 1) ardens_revised} then |
203 @{thm (lhs) ardens_revised} has the unique solution |
223 @{thm (lhs) ardens_revised} has the unique solution |
204 @{thm (rhs) ardens_revised}. |
224 @{thm (rhs) ardens_revised}. |
205 \end{lemma} |
225 \end{lemma} |
206 |
226 |
207 \begin{proof} |
227 \begin{proof} |
208 For the right-to-left direction we assume @{thm (rhs) ardens_revised} and show |
228 For the right-to-left direction we assume @{thm (rhs) ardens_revised} and show |
209 that @{thm (lhs) ardens_revised} holds. From Lemma ??? we have @{term "A\<star> = {[]} \<union> A ;; A\<star>"}, |
229 that @{thm (lhs) ardens_revised} holds. From Prop.~\ref{langprops}$(i)$ |
|
230 we have @{term "A\<star> = {[]} \<union> A ;; A\<star>"}, |
210 which is equal to @{term "A\<star> = {[]} \<union> A\<star> ;; A"}. Adding @{text B} to both |
231 which is equal to @{term "A\<star> = {[]} \<union> A\<star> ;; A"}. Adding @{text B} to both |
211 sides gives @{term "B ;; A\<star> = B ;; ({[]} \<union> A\<star> ;; A)"}, whose right-hand side |
232 sides gives @{term "B ;; A\<star> = B ;; ({[]} \<union> A\<star> ;; A)"}, whose right-hand side |
212 is equal to @{term "(B ;; A\<star>) ;; A \<union> B"}. This completes this direction. |
233 is equal to @{term "(B ;; A\<star>) ;; A \<union> B"}. This completes this direction. |
213 |
234 |
214 For the other direction we assume @{thm (lhs) ardens_revised}. By a simple induction |
235 For the other direction we assume @{thm (lhs) ardens_revised}. By a simple induction |
218 @{text "(*)"}\hspace{5mm} @{thm (concl) ardens_helper} |
239 @{text "(*)"}\hspace{5mm} @{thm (concl) ardens_helper} |
219 \end{center} |
240 \end{center} |
220 |
241 |
221 \noindent |
242 \noindent |
222 Using this property we can show that @{term "B ;; (A \<up> n) \<subseteq> X"} holds for |
243 Using this property we can show that @{term "B ;; (A \<up> n) \<subseteq> X"} holds for |
223 all @{text n}. From this we can infer @{term "B ;; A\<star> \<subseteq> X"} using Lemma ???. |
244 all @{text n}. From this we can infer @{term "B ;; A\<star> \<subseteq> X"} using the definition |
|
245 of @{text "\<star>"}. |
224 For the inclusion in the other direction we assume a string @{text s} |
246 For the inclusion in the other direction we assume a string @{text s} |
225 with length @{text k} is element in @{text X}. Since @{thm (prem 1) ardens_revised} |
247 with length @{text k} is element in @{text X}. Since @{thm (prem 1) ardens_revised} |
226 we know that @{term "s \<notin> X ;; (A \<up> Suc k)"} since its length is only @{text k} |
248 we know by Prop.~\ref{langprops}$(ii)$ that |
|
249 @{term "s \<notin> X ;; (A \<up> Suc k)"} since its length is only @{text k} |
227 (the strings in @{term "X ;; (A \<up> Suc k)"} are all longer). |
250 (the strings in @{term "X ;; (A \<up> Suc k)"} are all longer). |
228 From @{text "(*)"} it follows then that |
251 From @{text "(*)"} it follows then that |
229 @{term s} must be element in @{term "(\<Union>m\<in>{0..k}. B ;; (A \<up> m))"}. This in turn |
252 @{term s} must be element in @{term "(\<Union>m\<in>{0..k}. B ;; (A \<up> m))"}. This in turn |
230 implies that @{term s} is in @{term "(\<Union>n. B ;; (A \<up> n))"}. Using Lemma ??? this |
253 implies that @{term s} is in @{term "(\<Union>n. B ;; (A \<up> n))"}. Using Prop.~\ref{langprops}$(iii)$ |
231 is equal to @{term "B ;; A\<star>"}, as we needed to show.\qed |
254 this is equal to @{term "B ;; A\<star>"}, as we needed to show.\qed |
232 \end{proof} |
255 \end{proof} |
233 |
256 |
234 \noindent |
257 \noindent |
235 Regular expressions are defined as the following inductive datatype |
258 Regular expressions are defined as the following inductive datatype |
236 |
259 |
273 text {* |
296 text {* |
274 \begin{definition}[Myhill-Nerode Relation]\mbox{}\\ |
297 \begin{definition}[Myhill-Nerode Relation]\mbox{}\\ |
275 @{thm str_eq_rel_def[simplified]} |
298 @{thm str_eq_rel_def[simplified]} |
276 \end{definition} |
299 \end{definition} |
277 |
300 |
278 \begin{definition} @{text "finals A"} are the equivalence classes that contain |
301 \noindent |
279 strings from @{text A}\\ |
302 It is easy to see that @{term "\<approx>A"} is an equivalence relation, which partitions |
|
303 the set of all string, @{text "UNIV"}, into a set of equivalence classed. We define |
|
304 the set @{term "finals A"} as those equivalence classes that contain strings of |
|
305 @{text A}, namely |
|
306 |
|
307 \begin{equation} |
280 @{thm finals_def} |
308 @{thm finals_def} |
281 \end{definition} |
309 \end{equation} |
282 |
310 |
283 @{thm lang_is_union_of_finals} |
311 \noindent |
284 |
312 It is easy to show that @{thm lang_is_union_of_finals} holds. We can also define |
|
313 a notion of \emph{transition} between equivalence classes as |
|
314 |
|
315 \begin{equation} |
|
316 @{thm transition_def} |
|
317 \end{equation} |
|
318 |
|
319 \noindent |
|
320 which means if we add the character @{text c} to all strings in the equivalence |
|
321 class @{text Y} HERE |
285 |
322 |
286 \begin{theorem} |
323 \begin{theorem} |
287 Given a language @{text A}. |
324 Given a language @{text A}. |
288 @{thm[mode=IfThen] hard_direction} |
325 @{thm[mode=IfThen] hard_direction} |
289 \end{theorem} |
326 \end{theorem} |