9 REL :: "(string \<times> string) \<Rightarrow> bool" |
9 REL :: "(string \<times> string) \<Rightarrow> bool" |
10 UPLUS :: "'a set \<Rightarrow> 'a set \<Rightarrow> (nat \<times> 'a) set" |
10 UPLUS :: "'a set \<Rightarrow> 'a set \<Rightarrow> (nat \<times> 'a) set" |
11 |
11 |
12 abbreviation |
12 abbreviation |
13 "EClass x R \<equiv> R `` {x}" |
13 "EClass x R \<equiv> R `` {x}" |
|
14 |
|
15 abbreviation |
|
16 "append_rexp2 r_itm r \<equiv> append_rexp r r_itm" |
|
17 |
14 |
18 |
15 notation (latex output) |
19 notation (latex output) |
16 str_eq_rel ("\<approx>\<^bsub>_\<^esub>") and |
20 str_eq_rel ("\<approx>\<^bsub>_\<^esub>") and |
17 str_eq ("_ \<approx>\<^bsub>_\<^esub> _") and |
21 str_eq ("_ \<approx>\<^bsub>_\<^esub> _") and |
18 Seq (infixr "\<cdot>" 100) and |
22 Seq (infixr "\<cdot>" 100) and |
25 L ("\<^raw:\ensuremath{\cal{L}}>'(_')" [0] 101) and |
29 L ("\<^raw:\ensuremath{\cal{L}}>'(_')" [0] 101) and |
26 Lam ("\<lambda>'(_')" [100] 100) and |
30 Lam ("\<lambda>'(_')" [100] 100) and |
27 Trn ("'(_, _')" [100, 100] 100) and |
31 Trn ("'(_, _')" [100, 100] 100) and |
28 EClass ("\<lbrakk>_\<rbrakk>\<^bsub>_\<^esub>" [100, 100] 100) and |
32 EClass ("\<lbrakk>_\<rbrakk>\<^bsub>_\<^esub>" [100, 100] 100) and |
29 transition ("_ \<^raw:\ensuremath{\stackrel{\text{>_\<^raw:}}{\Longmapsto}}> _" [100, 100, 100] 100) and |
33 transition ("_ \<^raw:\ensuremath{\stackrel{\text{>_\<^raw:}}{\Longmapsto}}> _" [100, 100, 100] 100) and |
30 Setalt ("\<^raw:\ensuremath{\bigplus}>_" [1000] 999) |
34 Setalt ("\<^raw:\ensuremath{\bigplus}>_" [1000] 999) and |
|
35 append_rexp2 ("_ \<^raw:\ensuremath{\triangleleft}> _" [100, 100] 100) and |
|
36 append_rhs_rexp ("_ \<^raw:\ensuremath{\triangleleft}> _" [100, 100] 100) |
|
37 |
31 (*>*) |
38 (*>*) |
32 |
39 |
33 |
40 |
34 section {* Introduction *} |
41 section {* Introduction *} |
35 |
42 |
125 \end{equation} |
132 \end{equation} |
126 |
133 |
127 \noindent |
134 \noindent |
128 changes the type---the disjoint union is not a set, but a set of pairs. |
135 changes the type---the disjoint union is not a set, but a set of pairs. |
129 Using this definition for disjoint unions means we do not have a single type for automata |
136 Using this definition for disjoint unions means we do not have a single type for automata |
130 and hence will not be able to state properties about \emph{all} |
137 and hence will not be able to state certain properties about \emph{all} |
131 automata, since there is no type quantification available in HOL. An |
138 automata, since there is no type quantification available in HOL. An |
132 alternative, which provides us with a single type for automata, is to give every |
139 alternative, which provides us with a single type for automata, is to give every |
133 state node an identity, for example a natural |
140 state node an identity, for example a natural |
134 number, and then be careful to rename these identities apart whenever |
141 number, and then be careful to rename these identities apart whenever |
135 connecting two automata. This results in clunky proofs |
142 connecting two automata. This results in clunky proofs |
139 |
146 |
140 Functions are much better supported in Isabelle/HOL, but they still lead to similar |
147 Functions are much better supported in Isabelle/HOL, but they still lead to similar |
141 problems as with graphs. Composing, for example, two non-deterministic automata in parallel |
148 problems as with graphs. Composing, for example, two non-deterministic automata in parallel |
142 poses again the problem of how to implement disjoint unions. Nipkow \cite{Nipkow98} |
149 poses again the problem of how to implement disjoint unions. Nipkow \cite{Nipkow98} |
143 dismisses the option of using identities, because it leads to ``messy proofs''. He |
150 dismisses the option of using identities, because it leads to ``messy proofs''. He |
144 opts for a variant of \eqref{disjointunion}, but writes |
151 opts for a variant of \eqref{disjointunion} using bitlists, but writes |
145 |
152 |
146 \begin{quote} |
153 \begin{quote} |
147 \it ``If the reader finds the above treatment in terms of bit lists revoltingly |
154 \it ``If the reader finds the above treatment in terms of bit lists revoltingly |
148 concrete, I cannot disagree.'' |
155 concrete, \phantom{``}I cannot disagree.'' |
149 \end{quote} |
156 \end{quote} |
150 |
157 |
151 \noindent |
158 \noindent |
152 Moreover, it is not so clear how to conveniently impose a finiteness condition |
159 Moreover, it is not so clear how to conveniently impose a finiteness condition |
153 upon functions in order to represent \emph{finite} automata. The best is |
160 upon functions in order to represent \emph{finite} automata. The best is |
154 probably to resort to more advanced reasoning frameworks, such as \emph{locales}. |
161 probably to resort to more advanced reasoning frameworks, such as \emph{locales} |
|
162 or \emph{type classes}, |
|
163 which are not avaiable in all HOL-based theorem provers. |
155 |
164 |
156 Because of these problems to do with representing automata, there seems |
165 Because of these problems to do with representing automata, there seems |
157 to be no substantial formalisation of automata theory and regular languages |
166 to be no substantial formalisation of automata theory and regular languages |
158 carried out in a HOL-based theorem prover. Nipkow establishes in |
167 carried out in a HOL-based theorem prover. Nipkow establishes in |
159 \cite{Nipkow98} the link between regular expressions and automata in |
168 \cite{Nipkow98} the link between regular expressions and automata in |
160 the context of lexing. The only larger formalisations of automata theory |
169 the restricted context of lexing. The only larger formalisations of automata theory |
161 are carried out in Nuprl \cite{Constable00} and in Coq (for example |
170 are carried out in Nuprl \cite{Constable00} and in Coq (for example |
162 \cite{Filliatre97}). |
171 \cite{Filliatre97}). |
163 |
172 |
164 In this paper, we will not attempt to formalise automata theory in |
173 In this paper, we will not attempt to formalise automata theory in |
165 Isabelle/HOL, but take a completely different approach to regular |
174 Isabelle/HOL, but take a completely different approach to regular |
195 |
204 |
196 section {* Preliminaries *} |
205 section {* Preliminaries *} |
197 |
206 |
198 text {* |
207 text {* |
199 Strings in Isabelle/HOL are lists of characters with the \emph{empty string} |
208 Strings in Isabelle/HOL are lists of characters with the \emph{empty string} |
200 being represented by the empty list, written @{term "[]"}. \emph{Languages} |
209 being represented by the empty list, written @{term "[]"}. \emph{Languages} |
201 are sets of strings. The language containing all strings is written in |
210 are sets of strings. The language containing all strings is written in |
202 Isabelle/HOL as @{term "UNIV::string set"}. The concatenation of two languages |
211 Isabelle/HOL as @{term "UNIV::string set"}. The concatenation of two languages |
203 is written @{term "A ;; B"} and a language raised to the power @{text n} is written |
212 is written @{term "A ;; B"} and a language raised to the power @{text n} is written |
204 @{term "A \<up> n"}. Their definitions are |
213 @{term "A \<up> n"}. Their definitions are |
205 |
214 |
215 where @{text "@"} is the usual list-append operation. The Kleene-star of a language @{text A} |
224 where @{text "@"} is the usual list-append operation. The Kleene-star of a language @{text A} |
216 is defined as the union over all powers, namely @{thm Star_def}. In the paper |
225 is defined as the union over all powers, namely @{thm Star_def}. In the paper |
217 we will make use of the following properties of these constructions. |
226 we will make use of the following properties of these constructions. |
218 |
227 |
219 \begin{proposition}\label{langprops}\mbox{}\\ |
228 \begin{proposition}\label{langprops}\mbox{}\\ |
220 \begin{tabular}{@ {}ll@ {\hspace{10mm}}ll} |
229 \begin{tabular}{@ {}ll} |
221 (i) & @{thm star_cases} & (ii) & @{thm[mode=IfThen] pow_length}\\ |
230 (i) & @{thm star_cases} \\ |
222 (iii) & @{thm seq_Union_left} & |
231 (ii) & @{thm[mode=IfThen] pow_length}\\ |
|
232 (iii) & @{thm seq_Union_left} \\ |
223 \end{tabular} |
233 \end{tabular} |
224 \end{proposition} |
234 \end{proposition} |
225 |
235 |
226 \noindent |
236 \noindent |
227 We omit the proofs, but invite the reader to consult |
237 In @{text "(ii)"} we use the notation @{term "length s"} for the length of a string. |
|
238 We omit the proofs for these properties, but invite the reader to consult |
228 our formalisation.\footnote{Available at ???} |
239 our formalisation.\footnote{Available at ???} |
229 |
240 |
230 |
241 |
231 The notation in Isabelle/HOL for the quotient of a language @{text A} according to an |
242 The notation in Isabelle/HOL for the quotient of a language @{text A} according to an |
232 equivalence relation @{term REL} is @{term "A // REL"}. We will write |
243 equivalence relation @{term REL} is @{term "A // REL"}. We will write |
309 @{thm (rhs) L_rexp.simps(6)[where r="r"]}\\ |
320 @{thm (rhs) L_rexp.simps(6)[where r="r"]}\\ |
310 \end{tabular} |
321 \end{tabular} |
311 \end{tabular} |
322 \end{tabular} |
312 \end{center} |
323 \end{center} |
313 |
324 |
314 Given a set of regular expressions @{text rs}, we will need the operation of generating |
325 Given a set of regular expressions @{text rs}, we will make use of the operation of generating |
315 a corresponding regular expressions that matches all languages of @{text rs}. We only need the existence |
326 a regular expression that matches all languages of @{text rs}. We only need to know the existence |
316 of such a regular expressions and therefore we use Isabelle's @{const "fold_graph"} and Hilbert's |
327 of such a regular expression and therefore we use Isabelle/HOL's @{const "fold_graph"} and Hilbert's |
317 @{text "\<epsilon>"} to define @{term "\<Uplus>rs"}. This function, roughly speaking, folds @{const ALT} over the |
328 @{text "\<epsilon>"} to define @{term "\<Uplus>rs"}. This function, roughly speaking, folds @{const ALT} over the |
318 set @{text rs} with @{const NULL} for the empty set. We can prove that for finite sets @{text rs} |
329 set @{text rs} with @{const NULL} for the empty set. We can prove that for finite sets @{text rs} |
319 |
330 |
320 \begin{center} |
331 \begin{center} |
321 @{thm (lhs) folds_alt_simp}@{text "= \<Union> (\<calL> ` rs)"} |
332 @{thm (lhs) folds_alt_simp}@{text "= \<Union> (\<calL> ` rs)"} |
342 |
353 |
343 \noindent |
354 \noindent |
344 It is easy to see that @{term "\<approx>A"} is an equivalence relation, which |
355 It is easy to see that @{term "\<approx>A"} is an equivalence relation, which |
345 partitions the set of all strings, @{text "UNIV"}, into a set of disjoint |
356 partitions the set of all strings, @{text "UNIV"}, into a set of disjoint |
346 equivalence classes. An example is the regular language containing just |
357 equivalence classes. An example is the regular language containing just |
347 the string @{text "[c]"}, then @{term "\<approx>({[c]})"} partitions @{text UNIV} |
358 the string @{text "[c]"}. The relation @{term "\<approx>({[c]})"} partitions @{text UNIV} |
348 into the three equivalence classes @{text "X\<^isub>1"}, @{text "X\<^isub>2"} and @{text "X\<^isub>3"} |
359 into the three equivalence classes @{text "X\<^isub>1"}, @{text "X\<^isub>2"} and @{text "X\<^isub>3"} |
349 as follows |
360 as follows |
350 |
361 |
351 \begin{center} |
362 \begin{center} |
352 @{text "X\<^isub>1 = {[]}"}\hspace{5mm} |
363 @{text "X\<^isub>1 = {[]}"}\hspace{5mm} |
369 \begin{equation} |
380 \begin{equation} |
370 @{thm finals_def} |
381 @{thm finals_def} |
371 \end{equation} |
382 \end{equation} |
372 |
383 |
373 \noindent |
384 \noindent |
374 In our running example, @{text "X\<^isub>1"} is the only equivalence class in @{term "finals {[c]}"}. |
385 In our running example, @{text "X\<^isub>2"} is the only equivalence class in @{term "finals {[c]}"}. |
375 It is straightforward to show that in general @{thm lang_is_union_of_finals} and |
386 It is straightforward to show that in general @{thm lang_is_union_of_finals} and |
376 @{thm finals_in_partitions} hold. |
387 @{thm finals_in_partitions} hold. |
377 Therefore if we know that there exists a regular expression for every |
388 Therefore if we know that there exists a regular expression for every |
378 equivalence class in @{term "finals A"} (which by assumption must be |
389 equivalence class in @{term "finals A"} (which by assumption must be |
379 a finite set), then we can obtain a regular expression using @{text "\<bigplus>"} |
390 a finite set), then we can using @{text "\<bigplus>"} obtain a regular expression |
380 that matches every string in @{text A}. |
391 using that matches every string in @{text A}. |
381 |
392 |
382 |
393 |
383 Our proof of Thm.~\ref{myhillnerodeone} relies on a method that can calculate a |
394 Our proof of Thm.~\ref{myhillnerodeone} relies on a method that can calculate a |
384 regular expression for \emph{every} equivalence class, not just the ones |
395 regular expression for \emph{every} equivalence class, not just the ones |
385 in @{term "finals A"}. We |
396 in @{term "finals A"}. We |
386 first define the notion of \emph{transition} between equivalence classes |
397 first define the notion of \emph{one-character-transition} between equivalence classes |
387 % |
398 % |
388 \begin{equation} |
399 \begin{equation} |
389 @{thm transition_def} |
400 @{thm transition_def} |
390 \end{equation} |
401 \end{equation} |
391 |
402 |
392 \noindent |
403 \noindent |
393 which means that if we concatenate all strings matching the regular expression @{text r} |
404 which means that if we concatenate the character @{text c} to the end of all |
394 to the end of all strings in the equivalence class @{text Y}, we obtain a subset of |
405 strings in the equivalence class @{text Y}, we obtain a subset of |
395 @{text X}. Note that we do not define an automaton here, we merely relate two sets |
406 @{text X}. Note that we do not define an automaton here, we merely relate two sets |
396 (with the help of a regular expression). In our concrete example we have |
407 (with the help of a regular expression). In our concrete example we have |
397 @{term "X\<^isub>1 \<Turnstile>(CHAR c)\<Rightarrow> X\<^isub>2"} and @{term "X\<^isub>1 \<Turnstile>r\<Rightarrow> X\<^isub>3"} with @{text r} being any |
408 @{term "X\<^isub>1 \<Turnstile>c\<Rightarrow> X\<^isub>2"}, @{term "X\<^isub>1 \<Turnstile>d\<Rightarrow> X\<^isub>3"} with @{text d} being any |
398 other regular expression than @{const EMPTY} and @{term "CHAR c"}. |
409 other character than @{text c}, and @{term "X\<^isub>3 \<Turnstile>d\<Rightarrow> X\<^isub>3"} for any @{text c}. |
399 |
410 |
400 Next we build an equational system that |
411 Next we build an equational system that |
401 contains an equation for each equivalence class. Suppose we have |
412 contains an equation for each equivalence class. Suppose we have |
402 the equivalence classes @{text "X\<^isub>1,\<dots>,X\<^isub>n"}, there must be one and only one that |
413 the equivalence classes @{text "X\<^isub>1,\<dots>,X\<^isub>n"}, there must be one and only one that |
403 contains the empty string @{text "[]"} (since equivalence classes are disjoint). |
414 contains the empty string @{text "[]"} (since equivalence classes are disjoint). |
412 \end{tabular} |
423 \end{tabular} |
413 \end{center} |
424 \end{center} |
414 |
425 |
415 \noindent |
426 \noindent |
416 where the pairs @{text "(Y\<^isub>i\<^isub>j, CHAR c\<^isub>i\<^isub>j)"} stand for all transitions |
427 where the pairs @{text "(Y\<^isub>i\<^isub>j, CHAR c\<^isub>i\<^isub>j)"} stand for all transitions |
417 @{term "Y\<^isub>i\<^isub>j \<Turnstile>(CHAR c\<^isub>i\<^isub>j)\<Rightarrow> X\<^isub>i"}. The term @{text "\<lambda>(EMPTY)"} acts as a marker for the equivalence |
428 @{term "Y\<^isub>i\<^isub>j \<Turnstile>c\<^isub>i\<^isub>j\<Rightarrow> X\<^isub>i"}. There can only be finitely many such |
418 class containing @{text "[]"}. (Note that we mark, roughly speaking, the |
429 transitions since there are only finitely many equivalence classes |
|
430 and finitely many characters. |
|
431 The term @{text "\<lambda>(EMPTY)"} in the first equation acts as a marker for the equivalence |
|
432 class containing @{text "[]"}. (As an aside note that we mark, roughly speaking, the |
419 single ``initial'' state in the equational system, which is different from |
433 single ``initial'' state in the equational system, which is different from |
420 the method by Brzozowski \cite{Brzozowski64}, since he marks the ``terminal'' |
434 the method by Brzozowski \cite{Brzozowski64}: he marks the ``terminal'' |
421 states. We are forced to set up the equational system in this way, because |
435 states. We are forced to set up the equational system in this way, because |
422 the Myhill-Nerode relation determines the ``direction'' of the transitions. |
436 the Myhill-Nerode relation determines the ``direction'' of the transitions. |
423 The successor ``state'' of an equivalence class @{text Y} can be reached by adding |
437 The successor ``state'' of an equivalence class @{text Y} can be reached by adding |
424 characters to the end of @{text Y}. This is also the reason why we have to use |
438 characters to the end of @{text Y}. This is also the reason why we have to use |
425 our reverse version of Arden's lemma.) |
439 our reverse version of Arden's lemma.) |
426 Overloading the function @{text L} for the two kinds of terms in the |
440 Overloading the function @{text L} for the two kinds of terms in the |
427 equational system as follows |
441 equational system, we have |
428 |
442 |
429 \begin{center} |
443 \begin{center} |
430 @{thm L_rhs_item.simps(2)[where X="Y" and r="r", THEN eq_reflection]}\hspace{10mm} |
444 @{text "\<calL>(Y, r) \<equiv>"} % |
|
445 @{thm (rhs) L_rhs_item.simps(2)[where X="Y" and r="r", THEN eq_reflection]}\hspace{10mm} |
431 @{thm L_rhs_item.simps(1)[where r="r", THEN eq_reflection]} |
446 @{thm L_rhs_item.simps(1)[where r="r", THEN eq_reflection]} |
432 \end{center} |
447 \end{center} |
433 |
448 |
434 \noindent |
449 \noindent |
435 we can prove for @{text "X\<^isub>2\<^isub>.\<^isub>.\<^isub>n"} that the following equations |
450 we can prove for @{text "X\<^isub>2\<^isub>.\<^isub>.\<^isub>n"} that the following equations |
445 @{text "X\<^isub>1 = \<calL>(Y\<^isub>i\<^isub>1, CHAR c\<^isub>i\<^isub>1) \<union> \<dots> \<union> \<calL>(Y\<^isub>i\<^isub>p, CHAR c\<^isub>i\<^isub>p) \<union> \<calL>(\<lambda>(EMPTY))"}. |
460 @{text "X\<^isub>1 = \<calL>(Y\<^isub>i\<^isub>1, CHAR c\<^isub>i\<^isub>1) \<union> \<dots> \<union> \<calL>(Y\<^isub>i\<^isub>p, CHAR c\<^isub>i\<^isub>p) \<union> \<calL>(\<lambda>(EMPTY))"}. |
446 \end{equation} |
461 \end{equation} |
447 |
462 |
448 \noindent |
463 \noindent |
449 The reason for adding the @{text \<lambda>}-marker to our equational system is |
464 The reason for adding the @{text \<lambda>}-marker to our equational system is |
450 to obtain this equation, which only holds in this form since none of |
465 to obtain this equation: it only holds in this form since none of |
451 the other terms contain the empty string. |
466 the other terms contain the empty string. |
452 |
467 |
453 |
468 |
454 Our proof of Thm.~\ref{myhillnerodeone} will be by transforming the |
469 Our proof of Thm.~\ref{myhillnerodeone} will proceed by transforming the |
455 equational system into a \emph{solved form} maintaining the invariants |
470 equational system into a \emph{solved form} maintaining the invariants |
456 \eqref{inv1} and \eqref{inv2}. From the solved form we will be able to read |
471 \eqref{inv1} and \eqref{inv2}. From the solved form we will be able to read |
457 off the regular expressions. |
472 off the regular expressions. |
458 |
473 |
459 In order to transform an equational system into solved form, we have two main |
474 In order to transform an equational system into solved form, we have two main |
460 operations: one that takes an equation of the form @{text "X = rhs"} and removes |
475 operations: one that takes an equation of the form @{text "X = rhs"} and removes |
461 the recursive occurences of @{text X} in @{text rhs} using our variant of Arden's |
476 the recursive occurences of @{text X} in @{text rhs} using our variant of Arden's |
462 Lemma (Lem.~\ref{arden}). The other operation takes an equation @{text "X = rhs"} |
477 Lemma. The other operation takes an equation @{text "X = rhs"} |
463 and substitutes @{text X} throughout the rest of the equational system |
478 and substitutes @{text X} throughout the rest of the equational system |
464 adjusting the regular expressions approriately. To define them we need the |
479 adjusting the remaining regular expressions approriately. To define this adjustment |
465 operation |
480 we define the \emph{append-operation} |
466 |
481 |
467 \begin{center} |
482 \begin{center} |
468 @{thm attach_rexp.simps(2)[where X="Y" and r="r\<^isub>1" and rexp="r\<^isub>2", THEN eq_reflection]}\hspace{10mm} |
483 @{thm append_rexp.simps(2)[where X="Y" and r="r\<^isub>1" and rexp="r\<^isub>2", THEN eq_reflection]}\hspace{10mm} |
469 @{thm attach_rexp.simps(1)[where r="r\<^isub>1" and rexp="r\<^isub>2", THEN eq_reflection]} |
484 @{thm append_rexp.simps(1)[where r="r\<^isub>1" and rexp="r\<^isub>2", THEN eq_reflection]} |
470 \end{center} |
485 \end{center} |
471 |
486 |
472 |
487 \noindent |
|
488 which we also lift to entire right-hand sides of equations, written as |
|
489 @{thm (lhs) append_rhs_rexp_def[where rexp="r"]}. |
|
490 |
|
491 |
|
492 \begin{center} |
|
493 @{thm arden_op_def} |
|
494 \end{center} |
473 *} |
495 *} |
474 |
496 |
475 section {* Regular Expressions Generate Finitely Many Partitions *} |
497 section {* Regular Expressions Generate Finitely Many Partitions *} |
476 |
498 |
477 text {* |
499 text {* |