46 Suc ("_+1" [100] 100) and |
46 Suc ("_+1" [100] 100) and |
47 quotient ("_ \<^raw:\ensuremath{\!\sslash\!}> _" [90, 90] 90) and |
47 quotient ("_ \<^raw:\ensuremath{\!\sslash\!}> _" [90, 90] 90) and |
48 REL ("\<approx>") and |
48 REL ("\<approx>") and |
49 UPLUS ("_ \<^raw:\ensuremath{\uplus}> _" [90, 90] 90) and |
49 UPLUS ("_ \<^raw:\ensuremath{\uplus}> _" [90, 90] 90) and |
50 lang ("\<^raw:\ensuremath{\cal{L}}>'(_')" [0] 101) and |
50 lang ("\<^raw:\ensuremath{\cal{L}}>'(_')" [0] 101) and |
|
51 lang_trm ("\<^raw:\ensuremath{\cal{L}}>'(_')" [0] 101) and |
51 Lam ("\<lambda>'(_')" [100] 100) and |
52 Lam ("\<lambda>'(_')" [100] 100) and |
52 Trn ("'(_, _')" [100, 100] 100) and |
53 Trn ("'(_, _')" [100, 100] 100) and |
53 EClass ("\<lbrakk>_\<rbrakk>\<^bsub>_\<^esub>" [100, 100] 100) and |
54 EClass ("\<lbrakk>_\<rbrakk>\<^bsub>_\<^esub>" [100, 100] 100) and |
54 transition ("_ \<^raw:\ensuremath{\stackrel{\text{>_\<^raw:}}{\Longmapsto}}> _" [100, 100, 100] 100) and |
55 transition ("_ \<^raw:\ensuremath{\stackrel{\text{>_\<^raw:}}{\Longmapsto}}> _" [100, 100, 100] 100) and |
55 Setalt ("\<^raw:\ensuremath{\bigplus}>_" [1000] 999) and |
56 Setalt ("\<^raw:\ensuremath{\bigplus}>_" [1000] 999) and |
56 Append_rexp2 ("_ \<^raw:\ensuremath{\triangleleft}> _" [100, 100] 100) and |
57 Append_rexp2 ("_ \<^raw:\ensuremath{\triangleleft}> _" [100, 100] 100) and |
57 Append_rexp_rhs ("_ \<^raw:\ensuremath{\triangleleft}> _" [100, 100] 50) and |
58 Append_rexp_rhs ("_ \<^raw:\ensuremath{\triangleleft}> _" [100, 100] 50) and |
58 |
59 |
59 uminus ("\<^raw:\ensuremath{\overline{>_\<^raw:}}>" [100] 100) and |
60 uminus ("\<^raw:\ensuremath{\overline{>_\<^raw:}}>" [100] 100) and |
60 tag_str_Plus ("tag\<^isub>A\<^isub>L\<^isub>T _ _" [100, 100] 100) and |
61 tag_str_Plus ("tag\<^bsub>PLUS\<^esub> _ _" [100, 100] 100) and |
61 tag_str_Plus ("tag\<^isub>A\<^isub>L\<^isub>T _ _ _" [100, 100, 100] 100) and |
62 tag_str_Plus ("tag\<^bsub>PLUS\<^esub> _ _ _" [100, 100, 100] 100) and |
62 tag_str_Times ("tag\<^isub>S\<^isub>E\<^isub>Q _ _" [100, 100] 100) and |
63 tag_str_Times ("tag\<^isub>S\<^isub>E\<^isub>Q _ _" [100, 100] 100) and |
63 tag_str_Times ("tag\<^isub>S\<^isub>E\<^isub>Q _ _ _" [100, 100, 100] 100) and |
64 tag_str_Times ("tag\<^isub>S\<^isub>E\<^isub>Q _ _ _" [100, 100, 100] 100) and |
64 tag_str_Star ("tag\<^isub>S\<^isub>T\<^isub>A\<^isub>R _" [100] 100) and |
65 tag_str_Star ("tag\<^isub>S\<^isub>T\<^isub>A\<^isub>R _" [100] 100) and |
65 tag_str_Star ("tag\<^isub>S\<^isub>T\<^isub>A\<^isub>R _ _" [100, 100] 100) |
66 tag_str_Star ("tag\<^isub>S\<^isub>T\<^isub>A\<^isub>R _ _" [100, 100] 100) and |
|
67 tag_eq_rel ("\<^raw:$\threesim$>\<^bsub>_\<^esub>") and |
|
68 Delta ("\<Delta>'(_')") and |
|
69 nullable ("\<delta>'(_')") |
66 |
70 |
67 lemma meta_eq_app: |
71 lemma meta_eq_app: |
68 shows "f \<equiv> \<lambda>x. g x \<Longrightarrow> f x \<equiv> g x" |
72 shows "f \<equiv> \<lambda>x. g x \<Longrightarrow> f x \<equiv> g x" |
69 by auto |
73 by auto |
70 |
74 |
225 \noindent |
229 \noindent |
226 changes the type---the disjoint union is not a set, but a set of |
230 changes the type---the disjoint union is not a set, but a set of |
227 pairs. Using this definition for disjoint union means we do not have a |
231 pairs. Using this definition for disjoint union means we do not have a |
228 single type for automata. As a result we will not be able to define a regular |
232 single type for automata. As a result we will not be able to define a regular |
229 language as one for which there exists an automaton that recognises all its |
233 language as one for which there exists an automaton that recognises all its |
230 strings. Similarly, we cannot state properties about \emph{all} automata, |
234 strings, since there is no type quantification available in HOL (unlike in Coq, for |
231 since there is no type quantification available in HOL (unlike in Coq, for |
|
232 example). |
235 example). |
233 |
236 |
234 An alternative, which provides us with a single type for automata, is to give every |
237 An alternative, which provides us with a single type for automata, is to give every |
235 state node an identity, for example a natural |
238 state node an identity, for example a natural |
236 number, and then be careful to rename these identities apart whenever |
239 number, and then be careful to rename these identities apart whenever |
316 will be needed for regular expressions. Moreover, a reasoning infrastructure |
319 will be needed for regular expressions. Moreover, a reasoning infrastructure |
317 (like induction and recursion) comes for free in HOL-based theorem provers. |
320 (like induction and recursion) comes for free in HOL-based theorem provers. |
318 This has recently been exploited in HOL4 with a formalisation of |
321 This has recently been exploited in HOL4 with a formalisation of |
319 regular expression matching based on derivatives \cite{OwensSlind08} and |
322 regular expression matching based on derivatives \cite{OwensSlind08} and |
320 with an equivalence checker for regular expressions in Isabelle/HOL |
323 with an equivalence checker for regular expressions in Isabelle/HOL |
321 \cite{KraussNipkow11}. The purpose of this paper is to show that a central |
324 \cite{KraussNipkow11}. The main purpose of this paper is to show that a central |
322 result about regular languages---the Myhill-Nerode theorem---can be |
325 result about regular languages---the Myhill-Nerode theorem---can be |
323 recreated by only using regular expressions. This theorem gives necessary |
326 recreated by only using regular expressions. This theorem gives necessary |
324 and sufficient conditions for when a language is regular. As a corollary of |
327 and sufficient conditions for when a language is regular. As a corollary of |
325 this theorem we can easily establish the usual closure properties, including |
328 this theorem we can easily establish the usual closure properties, including |
326 complementation, for regular languages.\medskip |
329 complementation, for regular languages.\medskip |
327 |
330 |
328 \noindent |
331 \noindent |
329 {\bf Contributions:} There is an extensive literature on regular languages. |
332 {\bf Contributions:} There is an extensive literature on regular |
330 To our best knowledge, our proof of the Myhill-Nerode theorem is the first |
333 languages. To our best knowledge, our proof of the Myhill-Nerode theorem is |
331 that is based on regular expressions, only. The part of this theorem stating |
334 the first that is based on regular expressions, only. The part of this |
332 that finitely many partitions imply regularity of the language is proved by |
335 theorem stating that finitely many partitions imply regularity of the |
333 an argument about solving equational sytems. This argument seems to be folklore. |
336 language is proved by an argument about solving equational sytems. This |
334 For the other part, we give two proofs: a |
337 argument appears to be folklore. For the other part, we give two proofs: one |
335 direct proof using certain tagging-functions, and an indirect proof using |
338 direct proof using certain tagging-functions, and another indirect proof |
336 Antimirov's partial derivatives \cite{Antimirov95} (also earlier russion work). |
339 using Antimirov's partial derivatives \cite{Antimirov95}. Again to our best |
337 Again to our best knowledge, the tagging-functions have not been used before to |
340 knowledge, the tagging-functions have not been used before to establish the |
338 establish the Myhill-Nerode theorem. |
341 Myhill-Nerode theorem. Derivatives of regular expressions have been used |
339 |
342 extensively in the literature, unlike partial derivatives. However, partial |
|
343 derivatives are more suitable in the context of the Myhill-Nerode theorem, |
|
344 since it is easier to establish formally their finiteness result. |
340 *} |
345 *} |
341 |
346 |
342 section {* Preliminaries *} |
347 section {* Preliminaries *} |
343 |
348 |
344 text {* |
349 text {* |
974 in @{term "finals A"} there exists a regular expression. Moreover by assumption |
986 in @{term "finals A"} there exists a regular expression. Moreover by assumption |
975 we know that @{term "finals A"} must be finite, and therefore there must be a finite |
987 we know that @{term "finals A"} must be finite, and therefore there must be a finite |
976 set of regular expressions @{text "rs"} such that |
988 set of regular expressions @{text "rs"} such that |
977 @{term "\<Union>(finals A) = L (\<Uplus>rs)"}. |
989 @{term "\<Union>(finals A) = L (\<Uplus>rs)"}. |
978 Since the left-hand side is equal to @{text A}, we can use @{term "\<Uplus>rs"} |
990 Since the left-hand side is equal to @{text A}, we can use @{term "\<Uplus>rs"} |
979 as the regular expression that is needed in the theorem.\qed |
991 as the regular expression that is needed in the theorem. |
980 \end{proof} |
992 \end{proof} |
981 *} |
993 *} |
982 |
994 |
983 |
995 |
984 |
996 |
985 |
997 |
986 section {* Myhill-Nerode, Second Part *} |
998 section {* Myhill-Nerode, Second Part *} |
987 |
999 |
988 text {* |
1000 text {* |
989 \noindent |
1001 \noindent |
990 We will prove in this section the second part of the Myhill-Nerode |
1002 In this section and the next we will give two proofs for establishing the second |
991 theorem. It can be formulated in our setting as follows: |
1003 part of the Myhill-Nerode theorem. It can be formulated in our setting as follows: |
992 |
1004 |
993 \begin{thrm} |
1005 \begin{thrm} |
994 Given @{text "r"} is a regular expression, then @{thm Myhill_Nerode2}. |
1006 Given @{text "r"} is a regular expression, then @{thm Myhill_Nerode2}. |
995 \end{thrm} |
1007 \end{thrm} |
996 |
1008 |
997 \noindent |
1009 \noindent |
998 The proof will be by induction on the structure of @{text r}. It turns out |
1010 The first proof will be by induction on the structure of @{text r}. It turns out |
999 the base cases are straightforward. |
1011 the base cases are straightforward. |
1000 |
1012 |
1001 |
1013 |
1002 \begin{proof}[Base Cases] |
1014 \begin{proof}[Base Cases] |
1003 The cases for @{const ZERO}, @{const ONE} and @{const ATOM} are routine, because |
1015 The cases for @{const ZERO}, @{const ONE} and @{const ATOM} are routine, because |
1010 @{thm quot_atom_subset} |
1022 @{thm quot_atom_subset} |
1011 \end{tabular} |
1023 \end{tabular} |
1012 \end{center} |
1024 \end{center} |
1013 |
1025 |
1014 \noindent |
1026 \noindent |
1015 hold, which shows that @{term "UNIV // \<approx>(L r)"} must be finite.\qed |
1027 hold, which shows that @{term "UNIV // \<approx>(lang r)"} must be finite. |
1016 \end{proof} |
1028 \end{proof} |
1017 |
1029 |
1018 \noindent |
1030 \noindent |
1019 Much more interesting, however, are the inductive cases. They seem hard to solve |
1031 Much more interesting, however, are the inductive cases. They seem hard to solve |
1020 directly. The reader is invited to try. |
1032 directly. The reader is invited to try. |
1021 |
1033 |
1022 Our proof will rely on some |
1034 Our first proof will rely on some |
1023 \emph{tagging-functions} defined over strings. Given the inductive hypothesis, it will |
1035 \emph{tagging-functions} defined over strings. Given the inductive hypothesis, it will |
1024 be easy to prove that the \emph{range} of these tagging-functions is finite |
1036 be easy to prove that the \emph{range} of these tagging-functions is finite. |
1025 (the range of a function @{text f} is defined as @{text "range f \<equiv> f ` UNIV"}). |
1037 The range of a function @{text f} is defined as |
|
1038 |
|
1039 \begin{center} |
|
1040 @{text "range f \<equiv> f ` UNIV"} |
|
1041 \end{center} |
|
1042 |
|
1043 \noindent |
|
1044 that means we take the image of @{text f} w.r.t.~all elements in the domain. |
1026 With this we will be able to infer that the tagging-functions, seen as relations, |
1045 With this we will be able to infer that the tagging-functions, seen as relations, |
1027 give rise to finitely many equivalence classes of @{const UNIV}. Finally we |
1046 give rise to finitely many equivalence classes of @{const UNIV}. Finally we |
1028 will show that the tagging-relations are more refined than @{term "\<approx>(L r)"}, which |
1047 will show that the tagging-relations are more refined than @{term "\<approx>(lang r)"}, which |
1029 implies that @{term "UNIV // \<approx>(L r)"} must also be finite (a relation @{text "R\<^isub>1"} |
1048 implies that @{term "UNIV // \<approx>(lang r)"} must also be finite---a relation @{text "R\<^isub>1"} |
1030 is said to \emph{refine} @{text "R\<^isub>2"} provided @{text "R\<^isub>1 \<subseteq> R\<^isub>2"}). |
1049 is said to \emph{refine} @{text "R\<^isub>2"} provided @{text "R\<^isub>1 \<subseteq> R\<^isub>2"}. |
1031 We formally define the notion of a \emph{tagging-relation} as follows. |
1050 We formally define the notion of a \emph{tagging-relation} as follows. |
1032 |
1051 |
1033 \begin{dfntn}[Tagging-Relation] Given a tagging-function @{text tag}, then two strings @{text x} |
1052 \begin{dfntn}[Tagging-Relation] Given a tagging-function @{text tag}, then two strings @{text x} |
1034 and @{text y} are \emph{tag-related} provided |
1053 and @{text y} are \emph{tag-related} provided |
1035 \begin{center} |
1054 \begin{center} |
1036 @{text "x =tag= y \<equiv> tag x = tag y"}\;. |
1055 @{text "x \<^raw:$\threesim$>\<^bsub>tag\<^esub> y \<equiv> tag x = tag y"}\;. |
1037 \end{center} |
1056 \end{center} |
1038 \end{dfntn} |
1057 \end{dfntn} |
1039 |
1058 |
1040 |
1059 |
1041 In order to establish finiteness of a set @{text A}, we shall use the following powerful |
1060 In order to establish finiteness of a set @{text A}, we shall use the following powerful |
1075 |
1094 |
1076 \begin{proof} |
1095 \begin{proof} |
1077 We prove this lemma again using \eqref{finiteimageD}. This time we set @{text f} to |
1096 We prove this lemma again using \eqref{finiteimageD}. This time we set @{text f} to |
1078 be @{text "X \<mapsto>"}~@{term "{R\<^isub>1 `` {x} | x. x \<in> X}"}. It is easy to see that |
1097 be @{text "X \<mapsto>"}~@{term "{R\<^isub>1 `` {x} | x. x \<in> X}"}. It is easy to see that |
1079 @{term "finite (f ` (UNIV // R\<^isub>2))"} because it is a subset of @{term "Pow (UNIV // R\<^isub>1)"}, |
1098 @{term "finite (f ` (UNIV // R\<^isub>2))"} because it is a subset of @{term "Pow (UNIV // R\<^isub>1)"}, |
1080 which is finite by assumption. What remains to be shown is that @{text f} is injective |
1099 which must be finite by assumption. What remains to be shown is that @{text f} is injective |
1081 on @{term "UNIV // R\<^isub>2"}. This is equivalent to showing that two equivalence |
1100 on @{term "UNIV // R\<^isub>2"}. This is equivalent to showing that two equivalence |
1082 classes, say @{text "X"} and @{text Y}, in @{term "UNIV // R\<^isub>2"} are equal, provided |
1101 classes, say @{text "X"} and @{text Y}, in @{term "UNIV // R\<^isub>2"} are equal, provided |
1083 @{text "f X = f Y"}. For @{text "X = Y"} to be equal, we have to find two elements |
1102 @{text "f X = f Y"}. For @{text "X = Y"} to be equal, we have to find two elements |
1084 @{text "x \<in> X"} and @{text "y \<in> Y"} such that they are @{text R\<^isub>2} related. |
1103 @{text "x \<in> X"} and @{text "y \<in> Y"} such that they are @{text R\<^isub>2} related. |
1085 We know there exists a @{text "x \<in> X"} with \mbox{@{term "X = R\<^isub>2 `` {x}"}}. |
1104 We know there exists a @{text "x \<in> X"} with \mbox{@{term "X = R\<^isub>2 `` {x}"}}. |
1086 From the latter fact we can infer that @{term "R\<^isub>1 ``{x} \<in> f X"} |
1105 From the latter fact we can infer that @{term "R\<^isub>1 ``{x} \<in> f X"} |
1087 and further @{term "R\<^isub>1 ``{x} \<in> f Y"}. This means we can obtain a @{text y} |
1106 and further @{term "R\<^isub>1 ``{x} \<in> f Y"}. This means we can obtain a @{text y} |
1088 such that @{term "R\<^isub>1 `` {x} = R\<^isub>1 `` {y}"} holds. Consequently @{text x} and @{text y} |
1107 such that @{term "R\<^isub>1 `` {x} = R\<^isub>1 `` {y}"} holds. Consequently @{text x} and @{text y} |
1089 are @{text "R\<^isub>1"}-related. Since by assumption @{text "R\<^isub>1"} refines @{text "R\<^isub>2"}, |
1108 are @{text "R\<^isub>1"}-related. Since by assumption @{text "R\<^isub>1"} refines @{text "R\<^isub>2"}, |
1090 they must also be @{text "R\<^isub>2"}-related, as we need to show.\qed |
1109 they must also be @{text "R\<^isub>2"}-related, as we need to show. |
1091 \end{proof} |
1110 \end{proof} |
1092 |
1111 |
1093 \noindent |
1112 \noindent |
1094 Chaining Lem.~\ref{finone} and \ref{fintwo} together, means in order to show |
1113 Chaining Lem.~\ref{finone} and \ref{fintwo} together, means in order to show |
1095 that @{term "UNIV // \<approx>(L r)"} is finite, we have to find a tagging-function whose |
1114 that @{term "UNIV // \<approx>(lang r)"} is finite, we have to find a tagging-function whose |
1096 range can be shown to be finite and whose tagging-relation refines @{term "\<approx>(L r)"}. |
1115 range can be shown to be finite and whose tagging-relation refines @{term "\<approx>(lang r)"}. |
1097 Let us attempt the @{const PLUS}-case first. |
1116 Let us attempt the @{const PLUS}-case first. |
1098 |
1117 |
1099 \begin{proof}[@{const "PLUS"}-Case] |
1118 \begin{proof}[@{const "PLUS"}-Case] |
1100 We take as tagging-function |
1119 We take as tagging-function |
1101 % |
1120 % |
1381 \noindent |
1400 \noindent |
1382 and we know that we have a @{term "y' \<in> A\<star>"} and @{text "y' < y"} |
1401 and we know that we have a @{term "y' \<in> A\<star>"} and @{text "y' < y"} |
1383 and also know @{term "(x - x'\<^isub>m\<^isub>a\<^isub>x) \<approx>A (y - y')"}. Unfolding the Myhill-Nerode |
1402 and also know @{term "(x - x'\<^isub>m\<^isub>a\<^isub>x) \<approx>A (y - y')"}. Unfolding the Myhill-Nerode |
1384 relation we know @{term "(y - y') @ z\<^isub>a \<in> A"}. We also know that @{term "z\<^isub>b \<in> A\<star>"}. |
1403 relation we know @{term "(y - y') @ z\<^isub>a \<in> A"}. We also know that @{term "z\<^isub>b \<in> A\<star>"}. |
1385 Therefore @{term "y' @ ((y - y') @ z\<^isub>a) @ z\<^isub>b \<in> A\<star>"}, which means |
1404 Therefore @{term "y' @ ((y - y') @ z\<^isub>a) @ z\<^isub>b \<in> A\<star>"}, which means |
1386 @{term "y @ z \<in> A\<star>"}. As the last step we have to set @{text "A"} to @{term "L r"} and |
1405 @{term "y @ z \<in> A\<star>"}. As the last step we have to set @{text "A"} to @{term "lang r"} and |
1387 complete the proof.\qed |
1406 complete the proof. |
1388 \end{proof} |
1407 \end{proof} |
1389 *} |
1408 *} |
1390 |
1409 |
1391 section {* Second Part based on Partial Derivatives *} |
1410 section {* Second Part based on Partial Derivatives *} |
1392 |
1411 |
1393 text {* |
1412 text {* |
1394 \noindent |
1413 \noindent |
1395 As we have seen in the previous section, in order to establish |
1414 As we have seen in the previous section, in order to establish |
1396 the second direction of the Myhill-Nerode theorem, we need to find |
1415 the second direction of the Myhill-Nerode theorem, we need to find |
1397 a more refined relation (more refined than ??) for which we can |
1416 a more refined relation than @{term "\<approx>(lang r)"} for which we can |
1398 show that there are only finitely many equivalence classes. |
1417 show that there are only finitely many equivalence classes. So far we |
1399 Brzozowski presented in the Appendix of~\cite{Brzozowski64} |
1418 showed this by induction on @{text "r"}. However, there is also |
|
1419 an indirect method to come up with such a refined relation. Assume |
|
1420 the following two definitions for a left-quotient of a language, which |
|
1421 we write as @{term "Der c A"} and @{term "Ders s A"} where |
|
1422 @{text c} is a character and @{text s} a string: |
|
1423 |
|
1424 \begin{center} |
|
1425 \begin{tabular}{r@ {\hspace{1mm}}c@ {\hspace{2mm}}l} |
|
1426 @{thm (lhs) Der_def} & @{text "\<equiv>"} & @{thm (rhs) Der_def}\\ |
|
1427 @{thm (lhs) Ders_def} & @{text "\<equiv>"} & @{thm (rhs) Ders_def}\\ |
|
1428 \end{tabular} |
|
1429 \end{center} |
|
1430 |
|
1431 \noindent |
|
1432 Now clearly we have the following relation between the Myhill-Nerode relation |
|
1433 (Definition~\ref{myhillneroderel}) and left-quotients |
|
1434 |
|
1435 \begin{equation}\label{mhders} |
|
1436 @{term "x \<approx>A y"} \hspace{4mm}\text{if and only if}\hspace{4mm} @{term "Ders x A = Ders y A"} |
|
1437 \end{equation} |
|
1438 |
|
1439 \noindent |
|
1440 It is realtively easy to establish the following identidies for left-quotients: |
|
1441 |
|
1442 \begin{center} |
|
1443 \begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{2mm}}l} |
|
1444 @{thm (lhs) Der_zero} & $=$ & @{thm (rhs) Der_zero}\\ |
|
1445 @{thm (lhs) Der_one} & $=$ & @{thm (rhs) Der_one}\\ |
|
1446 @{thm (lhs) Der_atom} & $=$ & @{thm (rhs) Der_atom}\\ |
|
1447 @{thm (lhs) Der_union} & $=$ & @{thm (rhs) Der_union}\\ |
|
1448 @{thm (lhs) Der_conc} & $=$ & @{thm (rhs) Der_conc}\\ |
|
1449 @{thm (lhs) Der_star} & $=$ & @{thm (rhs) Der_star}\\ |
|
1450 \end{tabular} |
|
1451 \end{center} |
|
1452 |
|
1453 \noindent |
|
1454 where @{text "\<Delta>"} is a function that tests whether the empty string |
|
1455 is in the language and returns @{term "{[]}"} or @{term "{}"}, respectively. |
|
1456 The only interesting case above is the last one where we use Prop.~\ref{langprops} |
|
1457 in order to infer that @{term "Der c (A\<star>) = Der c (A \<cdot> A\<star>)"}. We can |
|
1458 then complete the proof by observing that @{term "Delta A \<cdot> Der c (A\<star>) \<subseteq> (Der c A) \<cdot> A\<star>"}. |
|
1459 |
|
1460 Brzozowski observed that the left-quotients for languages of regular |
|
1461 expressions can be calculated directly via the notion of \emph{derivatives |
|
1462 of a regular expressions} \cite{Brzozowski64} which we define in Isabelle/HOL as |
|
1463 follows: |
|
1464 |
|
1465 \begin{center} |
|
1466 \begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1.5mm}}l@ {}} |
|
1467 @{thm (lhs) der.simps(1)} & @{text "\<equiv>"} & @{thm (rhs) der.simps(1)}\\ |
|
1468 @{thm (lhs) der.simps(2)} & @{text "\<equiv>"} & @{thm (rhs) der.simps(2)}\\ |
|
1469 @{thm (lhs) der.simps(3)[where c'="d"]} & @{text "\<equiv>"} & @{thm (rhs) der.simps(3)[where c'="d"]}\\ |
|
1470 @{thm (lhs) der.simps(4)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]} |
|
1471 & @{text "\<equiv>"} & @{thm (rhs) der.simps(4)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]}\\ |
|
1472 @{thm (lhs) der.simps(5)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]} |
|
1473 & @{text "\<equiv>"}\\ |
|
1474 \multicolumn{3}{@ {\hspace{5mm}}l@ {}}{@{thm (rhs) der.simps(5)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]}}\\ |
|
1475 @{thm (lhs) der.simps(6)} & @{text "\<equiv>"} & @{thm (rhs) der.simps(6)}\smallskip\\ |
|
1476 @{thm (lhs) ders.simps(1)} & @{text "\<equiv>"} & @{thm (rhs) ders.simps(1)}\\ |
|
1477 @{thm (lhs) ders.simps(2)} & @{text "\<equiv>"} & @{thm (rhs) ders.simps(2)}\\ |
|
1478 \end{tabular} |
|
1479 \end{center} |
|
1480 |
|
1481 \noindent |
|
1482 The function @{term "nullable r"} tests whether the regular expression |
|
1483 can recognise the empty string: |
|
1484 |
|
1485 \begin{center} |
|
1486 \begin{tabular}{cc} |
|
1487 \begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1.5mm}}l@ {}} |
|
1488 @{thm (lhs) nullable.simps(1)} & @{text "\<equiv>"} & @{thm (rhs) nullable.simps(1)}\\ |
|
1489 @{thm (lhs) nullable.simps(2)} & @{text "\<equiv>"} & @{thm (rhs) nullable.simps(2)}\\ |
|
1490 @{thm (lhs) nullable.simps(3)} & @{text "\<equiv>"} & @{thm (rhs) nullable.simps(3)}\\ |
|
1491 \end{tabular} & |
|
1492 \begin{tabular}{@ {}l@ {\hspace{1mm}}c@ {\hspace{1.5mm}}l@ {}} |
|
1493 @{thm (lhs) nullable.simps(4)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]} |
|
1494 & @{text "\<equiv>"} & @{thm (rhs) nullable.simps(4)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]}\\ |
|
1495 @{thm (lhs) nullable.simps(5)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]} |
|
1496 & @{text "\<equiv>"} & @{thm (rhs) nullable.simps(5)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]}\\ |
|
1497 @{thm (lhs) nullable.simps(6)} & @{text "\<equiv>"} & @{thm (rhs) nullable.simps(6)}\\ |
|
1498 \end{tabular} |
|
1499 \end{tabular} |
|
1500 \end{center} |
|
1501 |
|
1502 \noindent |
|
1503 Brzozowski proved |
|
1504 |
|
1505 \begin{equation}\label{Dersders} |
|
1506 \mbox{\begin{tabular}{l} |
|
1507 @{thm Der_der}\\ |
|
1508 @{thm Ders_ders} |
|
1509 \end{tabular}} |
|
1510 \end{equation} |
|
1511 |
|
1512 \noindent |
|
1513 where the first is by induction on @{text r} and the second by a simple |
|
1514 calculation. |
|
1515 |
|
1516 The importance in the context of the Myhill-Nerode theorem is that |
|
1517 we can use \eqref{mhders} and \eqref{Dersders} in order to derive |
|
1518 |
|
1519 \begin{center} |
|
1520 @{term "x \<approx>(lang r) y"} \hspace{4mm}if and only if\hspace{4mm} |
|
1521 @{term "lang (ders x r) = lang (ders y r)"} |
|
1522 \end{center} |
|
1523 |
|
1524 \noindent |
|
1525 which means @{term "x \<approx>(lang r) y"} provided @{term "ders x r = ders y r"}. |
|
1526 Consequently, we can use as the tagging relation |
|
1527 @{text "\<^raw:$\threesim$>\<^bsub>(\<lambda>x. ders x r)\<^esub>"}, which we know refines @{term "\<approx>(lang r)"}. |
|
1528 This almost helps us because Brozowski also proved that there for every |
|
1529 language there are only |
|
1530 finitely `dissimilar' derivatives for a regular expression. Two regulare |
|
1531 expressions are similar if they can be identified using the using the |
|
1532 ACI-identities |
|
1533 |
|
1534 \begin{center} |
|
1535 \begin{tabular}{cl} |
|
1536 (A) & @{term "Plus (Plus r\<^isub>1 r\<^isub>2) r\<^isub>3"} $\equiv$ @{term "Plus r\<^isub>1 (Plus r\<^isub>2 r\<^isub>3)"}\\ |
|
1537 (C) & @{term "Plus r\<^isub>1 r\<^isub>2"} $\equiv$ @{term "Plus r\<^isub>2 r\<^isub>1"}\\ |
|
1538 (I) & @{term "Plus r r"} $\equiv$ @{term "r"}\\ |
|
1539 \end{tabular} |
|
1540 \end{center} |
|
1541 |
|
1542 \noindent |
|
1543 Without the indentification, we unfortunately obtain infinitely many |
|
1544 derivations (an example is given in \cite[Page~141]{Sakarovitch09}). |
|
1545 Reasoning modulo ACI can be done, but it is very painful in a theorem prover. |
|
1546 |
1400 |
1547 |
1401 in order to prove the second |
1548 in order to prove the second |
1402 direction of the Myhill-Nerode theorem. There he calculates the |
1549 direction of the Myhill-Nerode theorem. There he calculates the |
1403 derivatives for regular expressions and shows that for every |
1550 derivatives for regular expressions and shows that for every |
1404 language there can be only finitely many of them %derivations (if |
1551 language there can be only finitely many of them %derivations (if |