author | urbanc |
Thu, 10 Feb 2011 12:32:45 +0000 | |
changeset 94 | 5b12cd0a3b3c |
parent 93 | 2aa3756dcc9f |
child 95 | 9540c2f2ea77 |
permissions | -rw-r--r-- |
24 | 1 |
(*<*) |
2 |
theory Paper |
|
94 | 3 |
imports "../Myhill" "LaTeXsugar" |
24 | 4 |
begin |
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
5 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
6 |
declare [[show_question_marks = false]] |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
7 |
|
54 | 8 |
consts |
9 |
REL :: "(string \<times> string) \<Rightarrow> bool" |
|
66 | 10 |
UPLUS :: "'a set \<Rightarrow> 'a set \<Rightarrow> (nat \<times> 'a) set" |
54 | 11 |
|
70 | 12 |
abbreviation |
13 |
"EClass x R \<equiv> R `` {x}" |
|
54 | 14 |
|
92 | 15 |
abbreviation |
16 |
"append_rexp2 r_itm r \<equiv> append_rexp r r_itm" |
|
17 |
||
18 |
||
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
19 |
notation (latex output) |
50 | 20 |
str_eq_rel ("\<approx>\<^bsub>_\<^esub>") and |
75 | 21 |
str_eq ("_ \<approx>\<^bsub>_\<^esub> _") and |
50 | 22 |
Seq (infixr "\<cdot>" 100) and |
23 |
Star ("_\<^bsup>\<star>\<^esup>") and |
|
24 |
pow ("_\<^bsup>_\<^esup>" [100, 100] 100) and |
|
58 | 25 |
Suc ("_+1" [100] 100) and |
54 | 26 |
quotient ("_ \<^raw:\ensuremath{\!\sslash\!}> _" [90, 90] 90) and |
66 | 27 |
REL ("\<approx>") and |
67 | 28 |
UPLUS ("_ \<^raw:\ensuremath{\uplus}> _" [90, 90] 90) and |
82 | 29 |
L ("\<^raw:\ensuremath{\cal{L}}>'(_')" [0] 101) and |
75 | 30 |
Lam ("\<lambda>'(_')" [100] 100) and |
89 | 31 |
Trn ("'(_, _')" [100, 100] 100) and |
71 | 32 |
EClass ("\<lbrakk>_\<rbrakk>\<^bsub>_\<^esub>" [100, 100] 100) and |
88 | 33 |
transition ("_ \<^raw:\ensuremath{\stackrel{\text{>_\<^raw:}}{\Longmapsto}}> _" [100, 100, 100] 100) and |
92 | 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 |
||
24 | 38 |
(*>*) |
39 |
||
70 | 40 |
|
24 | 41 |
section {* Introduction *} |
42 |
||
43 |
text {* |
|
58 | 44 |
Regular languages are an important and well-understood subject in Computer |
60 | 45 |
Science, with many beautiful theorems and many useful algorithms. There is a |
66 | 46 |
wide range of textbooks on this subject, many of which are aimed at students |
47 |
and contain very detailed ``pencil-and-paper'' proofs |
|
60 | 48 |
(e.g.~\cite{Kozen97}). It seems natural to exercise theorem provers by |
49 |
formalising these theorems and by verifying formally the algorithms. |
|
59 | 50 |
|
66 | 51 |
There is however a problem: the typical approach to regular languages is to |
52 |
introduce finite automata and then define everything in terms of them. For |
|
53 |
example, a regular language is normally defined as one whose strings are |
|
54 |
recognised by a finite deterministic automaton. This approach has many |
|
71 | 55 |
benefits. Among them is the fact that it is easy to convince oneself that |
66 | 56 |
regular languages are closed under complementation: one just has to exchange |
57 |
the accepting and non-accepting states in the corresponding automaton to |
|
58 |
obtain an automaton for the complement language. The problem, however, lies with |
|
67 | 59 |
formalising such reasoning in a HOL-based theorem prover, in our case |
70 | 60 |
Isabelle/HOL. Automata are build up from states and transitions that |
82 | 61 |
need to be represented as graphs, matrices or functions, none |
62 |
of which can be defined as inductive datatype. |
|
66 | 63 |
|
82 | 64 |
In case of graphs and matrices, this means we have to build our own |
65 |
reasoning infrastructure for them, as neither Isabelle/HOL nor HOL4 nor |
|
66 |
HOLlight support them with libraries. Even worse, reasoning about graphs and |
|
67 |
matrices can be a real hassle in HOL-based theorem provers. Consider for |
|
68 |
example the operation of sequencing two automata, say $A_1$ and $A_2$, by |
|
69 |
connecting the accepting states of $A_1$ to the initial state of $A_2$: |
|
60 | 70 |
|
71 |
\begin{center} |
|
66 | 72 |
\begin{tabular}{ccc} |
73 |
\begin{tikzpicture}[scale=0.8] |
|
74 |
%\draw[step=2mm] (-1,-1) grid (1,1); |
|
75 |
||
76 |
\draw[rounded corners=1mm, very thick] (-1.0,-0.3) rectangle (-0.2,0.3); |
|
77 |
\draw[rounded corners=1mm, very thick] ( 0.2,-0.3) rectangle ( 1.0,0.3); |
|
78 |
||
79 |
\node (A) at (-1.0,0.0) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
80 |
\node (B) at ( 0.2,0.0) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
81 |
||
82 |
\node (C) at (-0.2, 0.13) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
83 |
\node (D) at (-0.2,-0.13) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
84 |
||
85 |
\node (E) at (1.0, 0.2) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
86 |
\node (F) at (1.0,-0.0) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
87 |
\node (G) at (1.0,-0.2) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
88 |
||
89 |
\draw (-0.6,0.0) node {\footnotesize$A_1$}; |
|
90 |
\draw ( 0.6,0.0) node {\footnotesize$A_2$}; |
|
91 |
\end{tikzpicture} |
|
92 |
||
93 |
& |
|
94 |
||
95 |
\raisebox{1.1mm}{\bf\Large$\;\;\;\Rightarrow\,\;\;$} |
|
96 |
||
97 |
& |
|
98 |
||
99 |
\begin{tikzpicture}[scale=0.8] |
|
100 |
%\draw[step=2mm] (-1,-1) grid (1,1); |
|
101 |
||
102 |
\draw[rounded corners=1mm, very thick] (-1.0,-0.3) rectangle (-0.2,0.3); |
|
103 |
\draw[rounded corners=1mm, very thick] ( 0.2,-0.3) rectangle ( 1.0,0.3); |
|
104 |
||
105 |
\node (A) at (-1.0,0.0) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
106 |
\node (B) at ( 0.2,0.0) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
107 |
||
108 |
\node (C) at (-0.2, 0.13) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
109 |
\node (D) at (-0.2,-0.13) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
110 |
||
111 |
\node (E) at (1.0, 0.2) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
112 |
\node (F) at (1.0,-0.0) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
113 |
\node (G) at (1.0,-0.2) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
114 |
||
115 |
\draw (C) to [very thick, bend left=45] (B); |
|
116 |
\draw (D) to [very thick, bend right=45] (B); |
|
117 |
||
118 |
\draw (-0.6,0.0) node {\footnotesize$A_1$}; |
|
119 |
\draw ( 0.6,0.0) node {\footnotesize$A_2$}; |
|
120 |
\end{tikzpicture} |
|
121 |
||
122 |
\end{tabular} |
|
60 | 123 |
\end{center} |
124 |
||
125 |
\noindent |
|
67 | 126 |
On ``paper'' we can define the corresponding graph in terms of the disjoint |
88 | 127 |
union of the state nodes. Unfortunately in HOL, the standard definition for disjoint |
66 | 128 |
union, namely |
82 | 129 |
% |
130 |
\begin{equation}\label{disjointunion} |
|
66 | 131 |
@{term "UPLUS A\<^isub>1 A\<^isub>2 \<equiv> {(1, x) | x. x \<in> A\<^isub>1} \<union> {(2, y) | y. y \<in> A\<^isub>2}"} |
82 | 132 |
\end{equation} |
60 | 133 |
|
61 | 134 |
\noindent |
66 | 135 |
changes the type---the disjoint union is not a set, but a set of pairs. |
136 |
Using this definition for disjoint unions means we do not have a single type for automata |
|
92 | 137 |
and hence will not be able to state certain properties about \emph{all} |
67 | 138 |
automata, since there is no type quantification available in HOL. An |
139 |
alternative, which provides us with a single type for automata, is to give every |
|
140 |
state node an identity, for example a natural |
|
70 | 141 |
number, and then be careful to rename these identities apart whenever |
67 | 142 |
connecting two automata. This results in clunky proofs |
66 | 143 |
establishing that properties are invariant under renaming. Similarly, |
67 | 144 |
connecting two automata represented as matrices results in very adhoc |
66 | 145 |
constructions, which are not pleasant to reason about. |
146 |
||
82 | 147 |
Functions are much better supported in Isabelle/HOL, but they still lead to similar |
88 | 148 |
problems as with graphs. Composing, for example, two non-deterministic automata in parallel |
93 | 149 |
requires also the formalisation of disjoint unions. Nipkow \cite{Nipkow98} |
150 |
dismisses the option of using identities, because it leads according to him to ``messy proofs''. He |
|
92 | 151 |
opts for a variant of \eqref{disjointunion} using bitlists, but writes |
82 | 152 |
|
153 |
\begin{quote} |
|
93 | 154 |
\it% |
155 |
\begin{tabular}{@ {}l@ {}p{0.88\textwidth}@ {}} |
|
156 |
`` & If the reader finds the above treatment in terms of bit lists revoltingly |
|
157 |
concrete, I cannot disagree.''\\ |
|
158 |
`` & All lemmas appear obvious given a picture of the composition of automata\ldots |
|
159 |
Yet their proofs require a painful amount of detail.'' |
|
160 |
\end{tabular} |
|
82 | 161 |
\end{quote} |
162 |
||
163 |
\noindent |
|
164 |
Moreover, it is not so clear how to conveniently impose a finiteness condition |
|
165 |
upon functions in order to represent \emph{finite} automata. The best is |
|
92 | 166 |
probably to resort to more advanced reasoning frameworks, such as \emph{locales} |
167 |
or \emph{type classes}, |
|
93 | 168 |
which are not avaiable in \emph{all} HOL-based theorem provers. |
82 | 169 |
|
66 | 170 |
Because of these problems to do with representing automata, there seems |
171 |
to be no substantial formalisation of automata theory and regular languages |
|
93 | 172 |
carried out in HOL-based theorem provers. Nipkow establishes in |
82 | 173 |
\cite{Nipkow98} the link between regular expressions and automata in |
92 | 174 |
the restricted context of lexing. The only larger formalisations of automata theory |
82 | 175 |
are carried out in Nuprl \cite{Constable00} and in Coq (for example |
176 |
\cite{Filliatre97}). |
|
58 | 177 |
|
82 | 178 |
In this paper, we will not attempt to formalise automata theory in |
179 |
Isabelle/HOL, but take a completely different approach to regular |
|
180 |
languages. Instead of defining a regular language as one where there exists |
|
181 |
an automaton that recognises all strings of the language, we define a |
|
182 |
regular language as: |
|
54 | 183 |
|
82 | 184 |
\begin{definition} |
77 | 185 |
A language @{text A} is \emph{regular}, provided there is a regular expression that matches all |
54 | 186 |
strings of @{text "A"}. |
187 |
\end{definition} |
|
188 |
||
189 |
\noindent |
|
88 | 190 |
The reason is that regular expressions, unlike graphs, matrices and functons, can |
71 | 191 |
be easily defined as inductive datatype. Consequently a corresponding reasoning |
192 |
infrastructure comes for free. This has recently been exploited in HOL4 with a formalisation |
|
193 |
of regular expression matching based on derivatives \cite{OwensSlind08}. The purpose of this paper is to |
|
194 |
show that a central result about regular languages---the Myhill-Nerode theorem---can |
|
195 |
be recreated by only using regular expressions. This theorem gives necessary |
|
196 |
and sufficient conditions for when a language is regular. As a corollary of this |
|
67 | 197 |
theorem we can easily establish the usual closure properties, including |
198 |
complementation, for regular languages.\smallskip |
|
61 | 199 |
|
200 |
\noindent |
|
88 | 201 |
{\bf Contributions:} |
202 |
There is an extensive literature on regular languages. |
|
203 |
To our knowledge, our proof of the Myhill-Nerode theorem is the |
|
67 | 204 |
first that is based on regular expressions, only. We prove the part of this theorem |
205 |
stating that a regular expression has only finitely many partitions using certain |
|
206 |
tagging-functions. Again to our best knowledge, these tagging functions have |
|
207 |
not been used before to establish the Myhill-Nerode theorem. |
|
24 | 208 |
*} |
209 |
||
50 | 210 |
section {* Preliminaries *} |
211 |
||
212 |
text {* |
|
67 | 213 |
Strings in Isabelle/HOL are lists of characters with the \emph{empty string} |
92 | 214 |
being represented by the empty list, written @{term "[]"}. \emph{Languages} |
67 | 215 |
are sets of strings. The language containing all strings is written in |
71 | 216 |
Isabelle/HOL as @{term "UNIV::string set"}. The concatenation of two languages |
90 | 217 |
is written @{term "A ;; B"} and a language raised to the power @{text n} is written |
93 | 218 |
@{term "A \<up> n"}. They are defined as usual |
54 | 219 |
|
220 |
\begin{center} |
|
58 | 221 |
@{thm Seq_def[THEN eq_reflection, where A1="A" and B1="B"]} |
222 |
\hspace{7mm} |
|
223 |
@{thm pow.simps(1)[THEN eq_reflection, where A1="A"]} |
|
224 |
\hspace{7mm} |
|
225 |
@{thm pow.simps(2)[THEN eq_reflection, where A1="A" and n1="n"]} |
|
54 | 226 |
\end{center} |
227 |
||
228 |
\noindent |
|
58 | 229 |
where @{text "@"} is the usual list-append operation. The Kleene-star of a language @{text A} |
71 | 230 |
is defined as the union over all powers, namely @{thm Star_def}. In the paper |
88 | 231 |
we will make use of the following properties of these constructions. |
58 | 232 |
|
71 | 233 |
\begin{proposition}\label{langprops}\mbox{}\\ |
92 | 234 |
\begin{tabular}{@ {}ll} |
235 |
(i) & @{thm star_cases} \\ |
|
236 |
(ii) & @{thm[mode=IfThen] pow_length}\\ |
|
237 |
(iii) & @{thm seq_Union_left} \\ |
|
71 | 238 |
\end{tabular} |
239 |
\end{proposition} |
|
240 |
||
241 |
\noindent |
|
92 | 242 |
In @{text "(ii)"} we use the notation @{term "length s"} for the length of a string. |
243 |
We omit the proofs for these properties, but invite the reader to consult |
|
71 | 244 |
our formalisation.\footnote{Available at ???} |
245 |
||
246 |
||
90 | 247 |
The notation in Isabelle/HOL for the quotient of a language @{text A} according to an |
248 |
equivalence relation @{term REL} is @{term "A // REL"}. We will write |
|
71 | 249 |
@{text "\<lbrakk>x\<rbrakk>\<^isub>\<approx>"} for the equivalence class defined |
250 |
as @{text "{y | y \<approx> x}"}. |
|
251 |
||
252 |
||
51 | 253 |
Central to our proof will be the solution of equational systems |
77 | 254 |
involving sets of languages. For this we will use Arden's lemma \cite{Brzozowski64} |
93 | 255 |
which solves equations of the form @{term "X = A ;; X \<union> B"} provided |
71 | 256 |
@{term "[] \<notin> A"}. However we will need the following ``reverse'' |
50 | 257 |
version of Arden's lemma. |
258 |
||
75 | 259 |
\begin{lemma}[Reverse Arden's Lemma]\label{arden}\mbox{}\\ |
86 | 260 |
If @{thm (prem 1) arden} then |
261 |
@{thm (lhs) arden} has the unique solution |
|
262 |
@{thm (rhs) arden}. |
|
50 | 263 |
\end{lemma} |
264 |
||
265 |
\begin{proof} |
|
86 | 266 |
For the right-to-left direction we assume @{thm (rhs) arden} and show |
267 |
that @{thm (lhs) arden} holds. From Prop.~\ref{langprops}@{text "(i)"} |
|
71 | 268 |
we have @{term "A\<star> = {[]} \<union> A ;; A\<star>"}, |
50 | 269 |
which is equal to @{term "A\<star> = {[]} \<union> A\<star> ;; A"}. Adding @{text B} to both |
270 |
sides gives @{term "B ;; A\<star> = B ;; ({[]} \<union> A\<star> ;; A)"}, whose right-hand side |
|
51 | 271 |
is equal to @{term "(B ;; A\<star>) ;; A \<union> B"}. This completes this direction. |
50 | 272 |
|
86 | 273 |
For the other direction we assume @{thm (lhs) arden}. By a simple induction |
51 | 274 |
on @{text n}, we can establish the property |
50 | 275 |
|
276 |
\begin{center} |
|
86 | 277 |
@{text "(*)"}\hspace{5mm} @{thm (concl) arden_helper} |
50 | 278 |
\end{center} |
279 |
||
280 |
\noindent |
|
281 |
Using this property we can show that @{term "B ;; (A \<up> n) \<subseteq> X"} holds for |
|
71 | 282 |
all @{text n}. From this we can infer @{term "B ;; A\<star> \<subseteq> X"} using the definition |
283 |
of @{text "\<star>"}. |
|
51 | 284 |
For the inclusion in the other direction we assume a string @{text s} |
86 | 285 |
with length @{text k} is element in @{text X}. Since @{thm (prem 1) arden} |
75 | 286 |
we know by Prop.~\ref{langprops}@{text "(ii)"} that |
71 | 287 |
@{term "s \<notin> X ;; (A \<up> Suc k)"} since its length is only @{text k} |
51 | 288 |
(the strings in @{term "X ;; (A \<up> Suc k)"} are all longer). |
53 | 289 |
From @{text "(*)"} it follows then that |
50 | 290 |
@{term s} must be element in @{term "(\<Union>m\<in>{0..k}. B ;; (A \<up> m))"}. This in turn |
75 | 291 |
implies that @{term s} is in @{term "(\<Union>n. B ;; (A \<up> n))"}. Using Prop.~\ref{langprops}@{text "(iii)"} |
71 | 292 |
this is equal to @{term "B ;; A\<star>"}, as we needed to show.\qed |
50 | 293 |
\end{proof} |
67 | 294 |
|
295 |
\noindent |
|
88 | 296 |
Regular expressions are defined as the inductive datatype |
67 | 297 |
|
298 |
\begin{center} |
|
299 |
@{text r} @{text "::="} |
|
300 |
@{term NULL}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
301 |
@{term EMPTY}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
302 |
@{term "CHAR c"}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
303 |
@{term "SEQ r r"}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
304 |
@{term "ALT r r"}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
305 |
@{term "STAR r"} |
|
306 |
\end{center} |
|
307 |
||
308 |
\noindent |
|
88 | 309 |
and the language matched by a regular expression is defined as |
67 | 310 |
|
311 |
\begin{center} |
|
312 |
\begin{tabular}{c@ {\hspace{10mm}}c} |
|
313 |
\begin{tabular}{rcl} |
|
314 |
@{thm (lhs) L_rexp.simps(1)} & @{text "\<equiv>"} & @{thm (rhs) L_rexp.simps(1)}\\ |
|
315 |
@{thm (lhs) L_rexp.simps(2)} & @{text "\<equiv>"} & @{thm (rhs) L_rexp.simps(2)}\\ |
|
316 |
@{thm (lhs) L_rexp.simps(3)[where c="c"]} & @{text "\<equiv>"} & @{thm (rhs) L_rexp.simps(3)[where c="c"]}\\ |
|
317 |
\end{tabular} |
|
318 |
& |
|
319 |
\begin{tabular}{rcl} |
|
320 |
@{thm (lhs) L_rexp.simps(4)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]} & @{text "\<equiv>"} & |
|
321 |
@{thm (rhs) L_rexp.simps(4)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]}\\ |
|
322 |
@{thm (lhs) L_rexp.simps(5)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]} & @{text "\<equiv>"} & |
|
323 |
@{thm (rhs) L_rexp.simps(5)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]}\\ |
|
324 |
@{thm (lhs) L_rexp.simps(6)[where r="r"]} & @{text "\<equiv>"} & |
|
325 |
@{thm (rhs) L_rexp.simps(6)[where r="r"]}\\ |
|
326 |
\end{tabular} |
|
327 |
\end{tabular} |
|
328 |
\end{center} |
|
70 | 329 |
|
92 | 330 |
Given a set of regular expressions @{text rs}, we will make use of the operation of generating |
331 |
a regular expression that matches all languages of @{text rs}. We only need to know the existence |
|
332 |
of such a regular expression and therefore we use Isabelle/HOL's @{const "fold_graph"} and Hilbert's |
|
93 | 333 |
@{text "\<epsilon>"} to define @{term "\<Uplus>rs"}. This operation, roughly speaking, folds @{const ALT} over the |
88 | 334 |
set @{text rs} with @{const NULL} for the empty set. We can prove that for finite sets @{text rs} |
82 | 335 |
|
88 | 336 |
\begin{center} |
93 | 337 |
@{thm (lhs) folds_alt_simp} @{text "= \<Union> (\<calL> ` rs)"} |
88 | 338 |
\end{center} |
339 |
||
340 |
\noindent |
|
90 | 341 |
holds, whereby @{text "\<calL> ` rs"} stands for the |
342 |
image of the set @{text rs} under function @{text "\<calL>"}. |
|
50 | 343 |
*} |
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
344 |
|
54 | 345 |
section {* Finite Partitions Imply Regularity of a Language *} |
346 |
||
347 |
text {* |
|
77 | 348 |
The key definition in the Myhill-Nerode theorem is the |
75 | 349 |
\emph{Myhill-Nerode relation}, which states that w.r.t.~a language two |
350 |
strings are related, provided there is no distinguishing extension in this |
|
351 |
language. This can be defined as: |
|
352 |
||
70 | 353 |
\begin{definition}[Myhill-Nerode Relation]\mbox{}\\ |
75 | 354 |
@{thm str_eq_def[simplified str_eq_rel_def Pair_Collect]} |
70 | 355 |
\end{definition} |
356 |
||
71 | 357 |
\noindent |
75 | 358 |
It is easy to see that @{term "\<approx>A"} is an equivalence relation, which |
359 |
partitions the set of all strings, @{text "UNIV"}, into a set of disjoint |
|
93 | 360 |
equivalence classes. Let us give an example: consider the regular language containing just |
92 | 361 |
the string @{text "[c]"}. The relation @{term "\<approx>({[c]})"} partitions @{text UNIV} |
90 | 362 |
into the three equivalence classes @{text "X\<^isub>1"}, @{text "X\<^isub>2"} and @{text "X\<^isub>3"} |
363 |
as follows |
|
364 |
||
365 |
\begin{center} |
|
366 |
@{text "X\<^isub>1 = {[]}"}\hspace{5mm} |
|
367 |
@{text "X\<^isub>2 = {[c]}"}\hspace{5mm} |
|
368 |
@{text "X\<^isub>3 = UNIV - {[], [c]}"} |
|
369 |
\end{center} |
|
370 |
||
371 |
One direction of the Myhill-Nerode theorem establishes |
|
93 | 372 |
that if there are finitely many equivalence classes, like in the example above, then |
373 |
the language is regular. In our setting we therefore have to show: |
|
75 | 374 |
|
375 |
\begin{theorem}\label{myhillnerodeone} |
|
376 |
@{thm[mode=IfThen] hard_direction} |
|
377 |
\end{theorem} |
|
71 | 378 |
|
75 | 379 |
\noindent |
90 | 380 |
To prove this theorem, we first define the set @{term "finals A"} as those equivalence |
75 | 381 |
classes that contain strings of @{text A}, namely |
382 |
% |
|
71 | 383 |
\begin{equation} |
70 | 384 |
@{thm finals_def} |
71 | 385 |
\end{equation} |
386 |
||
387 |
\noindent |
|
92 | 388 |
In our running example, @{text "X\<^isub>2"} is the only equivalence class in @{term "finals {[c]}"}. |
90 | 389 |
It is straightforward to show that in general @{thm lang_is_union_of_finals} and |
79 | 390 |
@{thm finals_in_partitions} hold. |
75 | 391 |
Therefore if we know that there exists a regular expression for every |
392 |
equivalence class in @{term "finals A"} (which by assumption must be |
|
93 | 393 |
a finite set), then we can use @{text "\<bigplus>"} to obtain a regular expression |
92 | 394 |
using that matches every string in @{text A}. |
70 | 395 |
|
75 | 396 |
|
90 | 397 |
Our proof of Thm.~\ref{myhillnerodeone} relies on a method that can calculate a |
79 | 398 |
regular expression for \emph{every} equivalence class, not just the ones |
77 | 399 |
in @{term "finals A"}. We |
93 | 400 |
first define the notion of \emph{one-character-transition} between |
401 |
two equivalence classes |
|
75 | 402 |
% |
71 | 403 |
\begin{equation} |
404 |
@{thm transition_def} |
|
405 |
\end{equation} |
|
70 | 406 |
|
71 | 407 |
\noindent |
92 | 408 |
which means that if we concatenate the character @{text c} to the end of all |
409 |
strings in the equivalence class @{text Y}, we obtain a subset of |
|
77 | 410 |
@{text X}. Note that we do not define an automaton here, we merely relate two sets |
90 | 411 |
(with the help of a regular expression). In our concrete example we have |
92 | 412 |
@{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 |
93 | 413 |
other character than @{text c}, and @{term "X\<^isub>3 \<Turnstile>d\<Rightarrow> X\<^isub>3"} for any @{text d}. |
75 | 414 |
|
415 |
Next we build an equational system that |
|
416 |
contains an equation for each equivalence class. Suppose we have |
|
417 |
the equivalence classes @{text "X\<^isub>1,\<dots>,X\<^isub>n"}, there must be one and only one that |
|
418 |
contains the empty string @{text "[]"} (since equivalence classes are disjoint). |
|
77 | 419 |
Let us assume @{text "[] \<in> X\<^isub>1"}. We build the following equational system |
75 | 420 |
|
421 |
\begin{center} |
|
422 |
\begin{tabular}{rcl} |
|
423 |
@{text "X\<^isub>1"} & @{text "="} & @{text "(Y\<^isub>1\<^isub>1, CHAR c\<^isub>1\<^isub>1) + \<dots> + (Y\<^isub>1\<^isub>p, CHAR c\<^isub>1\<^isub>p) + \<lambda>(EMPTY)"} \\ |
|
424 |
@{text "X\<^isub>2"} & @{text "="} & @{text "(Y\<^isub>2\<^isub>1, CHAR c\<^isub>2\<^isub>1) + \<dots> + (Y\<^isub>2\<^isub>o, CHAR c\<^isub>2\<^isub>o)"} \\ |
|
425 |
& $\vdots$ \\ |
|
426 |
@{text "X\<^isub>n"} & @{text "="} & @{text "(Y\<^isub>n\<^isub>1, CHAR c\<^isub>n\<^isub>1) + \<dots> + (Y\<^isub>n\<^isub>q, CHAR c\<^isub>n\<^isub>q)"}\\ |
|
427 |
\end{tabular} |
|
428 |
\end{center} |
|
70 | 429 |
|
75 | 430 |
\noindent |
82 | 431 |
where the pairs @{text "(Y\<^isub>i\<^isub>j, CHAR c\<^isub>i\<^isub>j)"} stand for all transitions |
93 | 432 |
@{term "Y\<^isub>i\<^isub>j \<Turnstile>c\<^isub>i\<^isub>j\<Rightarrow> X\<^isub>i"}. Our internal represeantation for the right-hand |
433 |
sides are sets of terms. |
|
434 |
There can only be finitely many such |
|
435 |
terms since there are only finitely many equivalence classes |
|
436 |
and only finitely many characters. |
|
92 | 437 |
The term @{text "\<lambda>(EMPTY)"} in the first equation acts as a marker for the equivalence |
93 | 438 |
class containing @{text "[]"}.\footnote{Note that we mark, roughly speaking, the |
75 | 439 |
single ``initial'' state in the equational system, which is different from |
93 | 440 |
the method by Brzozowski \cite{Brzozowski64}, where he marks the ``terminal'' |
441 |
states. We are forced to set up the equational system in our way, because |
|
90 | 442 |
the Myhill-Nerode relation determines the ``direction'' of the transitions. |
443 |
The successor ``state'' of an equivalence class @{text Y} can be reached by adding |
|
444 |
characters to the end of @{text Y}. This is also the reason why we have to use |
|
93 | 445 |
our reverse version of Arden's lemma.} |
446 |
Overloading the function @{text \<calL>} for the two kinds of terms in the |
|
92 | 447 |
equational system, we have |
75 | 448 |
|
449 |
\begin{center} |
|
92 | 450 |
@{text "\<calL>(Y, r) \<equiv>"} % |
451 |
@{thm (rhs) L_rhs_item.simps(2)[where X="Y" and r="r", THEN eq_reflection]}\hspace{10mm} |
|
86 | 452 |
@{thm L_rhs_item.simps(1)[where r="r", THEN eq_reflection]} |
75 | 453 |
\end{center} |
454 |
||
455 |
\noindent |
|
456 |
we can prove for @{text "X\<^isub>2\<^isub>.\<^isub>.\<^isub>n"} that the following equations |
|
457 |
% |
|
458 |
\begin{equation}\label{inv1} |
|
83 | 459 |
@{text "X\<^isub>i = \<calL>(Y\<^isub>i\<^isub>1, CHAR c\<^isub>i\<^isub>1) \<union> \<dots> \<union> \<calL>(Y\<^isub>i\<^isub>q, CHAR c\<^isub>i\<^isub>q)"}. |
75 | 460 |
\end{equation} |
461 |
||
462 |
\noindent |
|
463 |
hold. Similarly for @{text "X\<^isub>1"} we can show the following equation |
|
464 |
% |
|
465 |
\begin{equation}\label{inv2} |
|
83 | 466 |
@{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))"}. |
75 | 467 |
\end{equation} |
468 |
||
469 |
\noindent |
|
77 | 470 |
The reason for adding the @{text \<lambda>}-marker to our equational system is |
92 | 471 |
to obtain this equation: it only holds in this form since none of |
93 | 472 |
the other terms contain the empty string. Since we use sets for representing |
473 |
the right-hans side we can write \eqref{inv1} and \eqref{inv2} more |
|
474 |
concisely for an equation of the form @{text "X = rhs"} as |
|
475 |
% |
|
476 |
\begin{equation}\label{inv} |
|
477 |
\mbox{@{text "X = \<Union> (\<calL> ` rhs)"}} |
|
478 |
\end{equation} |
|
77 | 479 |
|
93 | 480 |
\noindent |
92 | 481 |
Our proof of Thm.~\ref{myhillnerodeone} will proceed by transforming the |
93 | 482 |
equational system into a \emph{solved form} maintaining the invariant |
483 |
\eqref{inv}. From the solved form we will be able to read |
|
89 | 484 |
off the regular expressions. |
485 |
||
486 |
In order to transform an equational system into solved form, we have two main |
|
487 |
operations: one that takes an equation of the form @{text "X = rhs"} and removes |
|
93 | 488 |
the recursive occurences of @{text X} in the @{text rhs} using our variant of Arden's |
92 | 489 |
Lemma. The other operation takes an equation @{text "X = rhs"} |
89 | 490 |
and substitutes @{text X} throughout the rest of the equational system |
92 | 491 |
adjusting the remaining regular expressions approriately. To define this adjustment |
492 |
we define the \emph{append-operation} |
|
89 | 493 |
|
494 |
\begin{center} |
|
92 | 495 |
@{thm append_rexp.simps(2)[where X="Y" and r="r\<^isub>1" and rexp="r\<^isub>2", THEN eq_reflection]}\hspace{10mm} |
496 |
@{thm append_rexp.simps(1)[where r="r\<^isub>1" and rexp="r\<^isub>2", THEN eq_reflection]} |
|
89 | 497 |
\end{center} |
498 |
||
92 | 499 |
\noindent |
500 |
which we also lift to entire right-hand sides of equations, written as |
|
93 | 501 |
@{thm (lhs) append_rhs_rexp_def[where rexp="r"]}. With this we can define |
94 | 502 |
the \emph{arden-operation} for an equation of the form @{text "X = rhs"}: |
503 |
||
92 | 504 |
\begin{center} |
94 | 505 |
\begin{tabular}{rc@ {\hspace{2mm}}r@ {\hspace{1mm}}l} |
506 |
@{thm (lhs) Arden_def} & @{text "\<equiv>"}~~\mbox{} & \multicolumn{2}{@ {\hspace{-2mm}}l}{@{text "let"}}\\ |
|
507 |
& & @{text "rhs' ="} & @{term "rhs - {Trn X r | r. Trn X r \<in> rhs}"} \\ |
|
508 |
& & @{text "r' ="} & @{term "STAR (\<Uplus> {r. Trn X r \<in> rhs})"}\\ |
|
509 |
& & \multicolumn{2}{@ {\hspace{-2mm}}l}{@{text "in"}~~@{term "append_rhs_rexp rhs' r'"}}\\ |
|
510 |
\end{tabular} |
|
92 | 511 |
\end{center} |
93 | 512 |
|
513 |
\noindent |
|
94 | 514 |
We first delete all terms of the form @{text "(X, r)"} from @{text rhs}; |
515 |
then we calculate the combinded regular expressions for all @{text r} coming |
|
516 |
from the deleted @{text "(X, r)"}, and take the @{const STAR} of it; |
|
517 |
finally we append this regular expression to @{text rhs'}. It can be easily seen |
|
518 |
that this operation mimics Arden's lemma on the level of equations. |
|
93 | 519 |
|
94 | 520 |
|
521 |
\begin{center} |
|
522 |
@{thm Subst_def} |
|
523 |
\end{center} |
|
54 | 524 |
*} |
525 |
||
526 |
section {* Regular Expressions Generate Finitely Many Partitions *} |
|
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
527 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
528 |
text {* |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
529 |
|
54 | 530 |
\begin{theorem} |
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
531 |
Given @{text "r"} is a regular expressions, then @{thm rexp_imp_finite}. |
54 | 532 |
\end{theorem} |
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
533 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
534 |
\begin{proof} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
535 |
By induction on the structure of @{text r}. The cases for @{const NULL}, @{const EMPTY} |
50 | 536 |
and @{const CHAR} are straightforward, because we can easily establish |
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
537 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
538 |
\begin{center} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
539 |
\begin{tabular}{l} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
540 |
@{thm quot_null_eq}\\ |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
541 |
@{thm quot_empty_subset}\\ |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
542 |
@{thm quot_char_subset} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
543 |
\end{tabular} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
544 |
\end{center} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
545 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
546 |
\end{proof} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
547 |
*} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
548 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
549 |
|
54 | 550 |
section {* Conclusion and Related Work *} |
551 |
||
92 | 552 |
text {* |
553 |
In this paper we took the view that a regular language as one where there exists |
|
554 |
a regular expression that matches all its strings. For us it was important to find |
|
555 |
out how far we can push this point of view. Having formalised the Myhill-Nerode |
|
556 |
theorem means pushed very far. Having the Myhill-Nerode theorem means we can |
|
557 |
formalise much of the textbook results in this subject. |
|
558 |
||
559 |
||
560 |
*} |
|
561 |
||
562 |
||
24 | 563 |
(*<*) |
564 |
end |
|
565 |
(*>*) |