author | urbanc |
Thu, 10 Feb 2011 05:57:56 +0000 | |
changeset 92 | a9ebc410a5c8 |
parent 90 | 97b783438316 |
child 93 | 2aa3756dcc9f |
permissions | -rw-r--r-- |
24 | 1 |
(*<*) |
2 |
theory Paper |
|
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
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 |
149 |
poses again the problem of how to implement disjoint unions. Nipkow \cite{Nipkow98} |
|
150 |
dismisses the option of using identities, because it leads to ``messy proofs''. He |
|
92 | 151 |
opts for a variant of \eqref{disjointunion} using bitlists, but writes |
82 | 152 |
|
153 |
\begin{quote} |
|
154 |
\it ``If the reader finds the above treatment in terms of bit lists revoltingly |
|
92 | 155 |
concrete, \phantom{``}I cannot disagree.'' |
82 | 156 |
\end{quote} |
157 |
||
158 |
\noindent |
|
159 |
Moreover, it is not so clear how to conveniently impose a finiteness condition |
|
160 |
upon functions in order to represent \emph{finite} automata. The best is |
|
92 | 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. |
|
82 | 164 |
|
66 | 165 |
Because of these problems to do with representing automata, there seems |
166 |
to be no substantial formalisation of automata theory and regular languages |
|
82 | 167 |
carried out in a HOL-based theorem prover. Nipkow establishes in |
168 |
\cite{Nipkow98} the link between regular expressions and automata in |
|
92 | 169 |
the restricted context of lexing. The only larger formalisations of automata theory |
82 | 170 |
are carried out in Nuprl \cite{Constable00} and in Coq (for example |
171 |
\cite{Filliatre97}). |
|
58 | 172 |
|
82 | 173 |
In this paper, we will not attempt to formalise automata theory in |
174 |
Isabelle/HOL, but take a completely different approach to regular |
|
175 |
languages. Instead of defining a regular language as one where there exists |
|
176 |
an automaton that recognises all strings of the language, we define a |
|
177 |
regular language as: |
|
54 | 178 |
|
82 | 179 |
\begin{definition} |
77 | 180 |
A language @{text A} is \emph{regular}, provided there is a regular expression that matches all |
54 | 181 |
strings of @{text "A"}. |
182 |
\end{definition} |
|
183 |
||
184 |
\noindent |
|
88 | 185 |
The reason is that regular expressions, unlike graphs, matrices and functons, can |
71 | 186 |
be easily defined as inductive datatype. Consequently a corresponding reasoning |
187 |
infrastructure comes for free. This has recently been exploited in HOL4 with a formalisation |
|
188 |
of regular expression matching based on derivatives \cite{OwensSlind08}. The purpose of this paper is to |
|
189 |
show that a central result about regular languages---the Myhill-Nerode theorem---can |
|
190 |
be recreated by only using regular expressions. This theorem gives necessary |
|
191 |
and sufficient conditions for when a language is regular. As a corollary of this |
|
67 | 192 |
theorem we can easily establish the usual closure properties, including |
193 |
complementation, for regular languages.\smallskip |
|
61 | 194 |
|
195 |
\noindent |
|
88 | 196 |
{\bf Contributions:} |
197 |
There is an extensive literature on regular languages. |
|
198 |
To our knowledge, our proof of the Myhill-Nerode theorem is the |
|
67 | 199 |
first that is based on regular expressions, only. We prove the part of this theorem |
200 |
stating that a regular expression has only finitely many partitions using certain |
|
201 |
tagging-functions. Again to our best knowledge, these tagging functions have |
|
202 |
not been used before to establish the Myhill-Nerode theorem. |
|
24 | 203 |
*} |
204 |
||
50 | 205 |
section {* Preliminaries *} |
206 |
||
207 |
text {* |
|
67 | 208 |
Strings in Isabelle/HOL are lists of characters with the \emph{empty string} |
92 | 209 |
being represented by the empty list, written @{term "[]"}. \emph{Languages} |
67 | 210 |
are sets of strings. The language containing all strings is written in |
71 | 211 |
Isabelle/HOL as @{term "UNIV::string set"}. The concatenation of two languages |
90 | 212 |
is written @{term "A ;; B"} and a language raised to the power @{text n} is written |
71 | 213 |
@{term "A \<up> n"}. Their definitions are |
54 | 214 |
|
215 |
\begin{center} |
|
58 | 216 |
@{thm Seq_def[THEN eq_reflection, where A1="A" and B1="B"]} |
217 |
\hspace{7mm} |
|
218 |
@{thm pow.simps(1)[THEN eq_reflection, where A1="A"]} |
|
219 |
\hspace{7mm} |
|
220 |
@{thm pow.simps(2)[THEN eq_reflection, where A1="A" and n1="n"]} |
|
54 | 221 |
\end{center} |
222 |
||
223 |
\noindent |
|
58 | 224 |
where @{text "@"} is the usual list-append operation. The Kleene-star of a language @{text A} |
71 | 225 |
is defined as the union over all powers, namely @{thm Star_def}. In the paper |
88 | 226 |
we will make use of the following properties of these constructions. |
58 | 227 |
|
71 | 228 |
\begin{proposition}\label{langprops}\mbox{}\\ |
92 | 229 |
\begin{tabular}{@ {}ll} |
230 |
(i) & @{thm star_cases} \\ |
|
231 |
(ii) & @{thm[mode=IfThen] pow_length}\\ |
|
232 |
(iii) & @{thm seq_Union_left} \\ |
|
71 | 233 |
\end{tabular} |
234 |
\end{proposition} |
|
235 |
||
236 |
\noindent |
|
92 | 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 |
|
71 | 239 |
our formalisation.\footnote{Available at ???} |
240 |
||
241 |
||
90 | 242 |
The notation in Isabelle/HOL for the quotient of a language @{text A} according to an |
243 |
equivalence relation @{term REL} is @{term "A // REL"}. We will write |
|
71 | 244 |
@{text "\<lbrakk>x\<rbrakk>\<^isub>\<approx>"} for the equivalence class defined |
245 |
as @{text "{y | y \<approx> x}"}. |
|
246 |
||
247 |
||
51 | 248 |
Central to our proof will be the solution of equational systems |
77 | 249 |
involving sets of languages. For this we will use Arden's lemma \cite{Brzozowski64} |
90 | 250 |
which solves equations of the form @{term "X = A ;; X \<union> B"} in case |
71 | 251 |
@{term "[] \<notin> A"}. However we will need the following ``reverse'' |
50 | 252 |
version of Arden's lemma. |
253 |
||
75 | 254 |
\begin{lemma}[Reverse Arden's Lemma]\label{arden}\mbox{}\\ |
86 | 255 |
If @{thm (prem 1) arden} then |
256 |
@{thm (lhs) arden} has the unique solution |
|
257 |
@{thm (rhs) arden}. |
|
50 | 258 |
\end{lemma} |
259 |
||
260 |
\begin{proof} |
|
86 | 261 |
For the right-to-left direction we assume @{thm (rhs) arden} and show |
262 |
that @{thm (lhs) arden} holds. From Prop.~\ref{langprops}@{text "(i)"} |
|
71 | 263 |
we have @{term "A\<star> = {[]} \<union> A ;; A\<star>"}, |
50 | 264 |
which is equal to @{term "A\<star> = {[]} \<union> A\<star> ;; A"}. Adding @{text B} to both |
265 |
sides gives @{term "B ;; A\<star> = B ;; ({[]} \<union> A\<star> ;; A)"}, whose right-hand side |
|
51 | 266 |
is equal to @{term "(B ;; A\<star>) ;; A \<union> B"}. This completes this direction. |
50 | 267 |
|
86 | 268 |
For the other direction we assume @{thm (lhs) arden}. By a simple induction |
51 | 269 |
on @{text n}, we can establish the property |
50 | 270 |
|
271 |
\begin{center} |
|
86 | 272 |
@{text "(*)"}\hspace{5mm} @{thm (concl) arden_helper} |
50 | 273 |
\end{center} |
274 |
||
275 |
\noindent |
|
276 |
Using this property we can show that @{term "B ;; (A \<up> n) \<subseteq> X"} holds for |
|
71 | 277 |
all @{text n}. From this we can infer @{term "B ;; A\<star> \<subseteq> X"} using the definition |
278 |
of @{text "\<star>"}. |
|
51 | 279 |
For the inclusion in the other direction we assume a string @{text s} |
86 | 280 |
with length @{text k} is element in @{text X}. Since @{thm (prem 1) arden} |
75 | 281 |
we know by Prop.~\ref{langprops}@{text "(ii)"} that |
71 | 282 |
@{term "s \<notin> X ;; (A \<up> Suc k)"} since its length is only @{text k} |
51 | 283 |
(the strings in @{term "X ;; (A \<up> Suc k)"} are all longer). |
53 | 284 |
From @{text "(*)"} it follows then that |
50 | 285 |
@{term s} must be element in @{term "(\<Union>m\<in>{0..k}. B ;; (A \<up> m))"}. This in turn |
75 | 286 |
implies that @{term s} is in @{term "(\<Union>n. B ;; (A \<up> n))"}. Using Prop.~\ref{langprops}@{text "(iii)"} |
71 | 287 |
this is equal to @{term "B ;; A\<star>"}, as we needed to show.\qed |
50 | 288 |
\end{proof} |
67 | 289 |
|
290 |
\noindent |
|
88 | 291 |
Regular expressions are defined as the inductive datatype |
67 | 292 |
|
293 |
\begin{center} |
|
294 |
@{text r} @{text "::="} |
|
295 |
@{term NULL}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
296 |
@{term EMPTY}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
297 |
@{term "CHAR c"}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
298 |
@{term "SEQ r r"}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
299 |
@{term "ALT r r"}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
300 |
@{term "STAR r"} |
|
301 |
\end{center} |
|
302 |
||
303 |
\noindent |
|
88 | 304 |
and the language matched by a regular expression is defined as |
67 | 305 |
|
306 |
\begin{center} |
|
307 |
\begin{tabular}{c@ {\hspace{10mm}}c} |
|
308 |
\begin{tabular}{rcl} |
|
309 |
@{thm (lhs) L_rexp.simps(1)} & @{text "\<equiv>"} & @{thm (rhs) L_rexp.simps(1)}\\ |
|
310 |
@{thm (lhs) L_rexp.simps(2)} & @{text "\<equiv>"} & @{thm (rhs) L_rexp.simps(2)}\\ |
|
311 |
@{thm (lhs) L_rexp.simps(3)[where c="c"]} & @{text "\<equiv>"} & @{thm (rhs) L_rexp.simps(3)[where c="c"]}\\ |
|
312 |
\end{tabular} |
|
313 |
& |
|
314 |
\begin{tabular}{rcl} |
|
315 |
@{thm (lhs) L_rexp.simps(4)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]} & @{text "\<equiv>"} & |
|
316 |
@{thm (rhs) L_rexp.simps(4)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]}\\ |
|
317 |
@{thm (lhs) L_rexp.simps(5)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]} & @{text "\<equiv>"} & |
|
318 |
@{thm (rhs) L_rexp.simps(5)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]}\\ |
|
319 |
@{thm (lhs) L_rexp.simps(6)[where r="r"]} & @{text "\<equiv>"} & |
|
320 |
@{thm (rhs) L_rexp.simps(6)[where r="r"]}\\ |
|
321 |
\end{tabular} |
|
322 |
\end{tabular} |
|
323 |
\end{center} |
|
70 | 324 |
|
92 | 325 |
Given a set of regular expressions @{text rs}, we will make use of the operation of generating |
326 |
a regular expression that matches all languages of @{text rs}. We only need to know the existence |
|
327 |
of such a regular expression and therefore we use Isabelle/HOL's @{const "fold_graph"} and Hilbert's |
|
90 | 328 |
@{text "\<epsilon>"} to define @{term "\<Uplus>rs"}. This function, roughly speaking, folds @{const ALT} over the |
88 | 329 |
set @{text rs} with @{const NULL} for the empty set. We can prove that for finite sets @{text rs} |
82 | 330 |
|
88 | 331 |
\begin{center} |
332 |
@{thm (lhs) folds_alt_simp}@{text "= \<Union> (\<calL> ` rs)"} |
|
333 |
\end{center} |
|
334 |
||
335 |
\noindent |
|
90 | 336 |
holds, whereby @{text "\<calL> ` rs"} stands for the |
337 |
image of the set @{text rs} under function @{text "\<calL>"}. |
|
88 | 338 |
|
82 | 339 |
|
50 | 340 |
*} |
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
341 |
|
54 | 342 |
section {* Finite Partitions Imply Regularity of a Language *} |
343 |
||
344 |
text {* |
|
77 | 345 |
The key definition in the Myhill-Nerode theorem is the |
75 | 346 |
\emph{Myhill-Nerode relation}, which states that w.r.t.~a language two |
347 |
strings are related, provided there is no distinguishing extension in this |
|
348 |
language. This can be defined as: |
|
349 |
||
70 | 350 |
\begin{definition}[Myhill-Nerode Relation]\mbox{}\\ |
75 | 351 |
@{thm str_eq_def[simplified str_eq_rel_def Pair_Collect]} |
70 | 352 |
\end{definition} |
353 |
||
71 | 354 |
\noindent |
75 | 355 |
It is easy to see that @{term "\<approx>A"} is an equivalence relation, which |
356 |
partitions the set of all strings, @{text "UNIV"}, into a set of disjoint |
|
90 | 357 |
equivalence classes. An example is the regular language containing just |
92 | 358 |
the string @{text "[c]"}. The relation @{term "\<approx>({[c]})"} partitions @{text UNIV} |
90 | 359 |
into the three equivalence classes @{text "X\<^isub>1"}, @{text "X\<^isub>2"} and @{text "X\<^isub>3"} |
360 |
as follows |
|
361 |
||
362 |
\begin{center} |
|
363 |
@{text "X\<^isub>1 = {[]}"}\hspace{5mm} |
|
364 |
@{text "X\<^isub>2 = {[c]}"}\hspace{5mm} |
|
365 |
@{text "X\<^isub>3 = UNIV - {[], [c]}"} |
|
366 |
\end{center} |
|
367 |
||
368 |
One direction of the Myhill-Nerode theorem establishes |
|
75 | 369 |
that if there are finitely many equivalence classes, then the language is |
77 | 370 |
regular. In our setting we therefore have to show: |
75 | 371 |
|
372 |
\begin{theorem}\label{myhillnerodeone} |
|
373 |
@{thm[mode=IfThen] hard_direction} |
|
374 |
\end{theorem} |
|
71 | 375 |
|
75 | 376 |
\noindent |
90 | 377 |
To prove this theorem, we first define the set @{term "finals A"} as those equivalence |
75 | 378 |
classes that contain strings of @{text A}, namely |
379 |
% |
|
71 | 380 |
\begin{equation} |
70 | 381 |
@{thm finals_def} |
71 | 382 |
\end{equation} |
383 |
||
384 |
\noindent |
|
92 | 385 |
In our running example, @{text "X\<^isub>2"} is the only equivalence class in @{term "finals {[c]}"}. |
90 | 386 |
It is straightforward to show that in general @{thm lang_is_union_of_finals} and |
79 | 387 |
@{thm finals_in_partitions} hold. |
75 | 388 |
Therefore if we know that there exists a regular expression for every |
389 |
equivalence class in @{term "finals A"} (which by assumption must be |
|
92 | 390 |
a finite set), then we can using @{text "\<bigplus>"} obtain a regular expression |
391 |
using that matches every string in @{text A}. |
|
70 | 392 |
|
75 | 393 |
|
90 | 394 |
Our proof of Thm.~\ref{myhillnerodeone} relies on a method that can calculate a |
79 | 395 |
regular expression for \emph{every} equivalence class, not just the ones |
77 | 396 |
in @{term "finals A"}. We |
92 | 397 |
first define the notion of \emph{one-character-transition} between equivalence classes |
75 | 398 |
% |
71 | 399 |
\begin{equation} |
400 |
@{thm transition_def} |
|
401 |
\end{equation} |
|
70 | 402 |
|
71 | 403 |
\noindent |
92 | 404 |
which means that if we concatenate the character @{text c} to the end of all |
405 |
strings in the equivalence class @{text Y}, we obtain a subset of |
|
77 | 406 |
@{text X}. Note that we do not define an automaton here, we merely relate two sets |
90 | 407 |
(with the help of a regular expression). In our concrete example we have |
92 | 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 |
409 |
other character than @{text c}, and @{term "X\<^isub>3 \<Turnstile>d\<Rightarrow> X\<^isub>3"} for any @{text c}. |
|
75 | 410 |
|
411 |
Next we build an equational system that |
|
412 |
contains an equation for each equivalence class. Suppose we have |
|
413 |
the equivalence classes @{text "X\<^isub>1,\<dots>,X\<^isub>n"}, there must be one and only one that |
|
414 |
contains the empty string @{text "[]"} (since equivalence classes are disjoint). |
|
77 | 415 |
Let us assume @{text "[] \<in> X\<^isub>1"}. We build the following equational system |
75 | 416 |
|
417 |
\begin{center} |
|
418 |
\begin{tabular}{rcl} |
|
419 |
@{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)"} \\ |
|
420 |
@{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)"} \\ |
|
421 |
& $\vdots$ \\ |
|
422 |
@{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)"}\\ |
|
423 |
\end{tabular} |
|
424 |
\end{center} |
|
70 | 425 |
|
75 | 426 |
\noindent |
82 | 427 |
where the pairs @{text "(Y\<^isub>i\<^isub>j, CHAR c\<^isub>i\<^isub>j)"} stand for all transitions |
92 | 428 |
@{term "Y\<^isub>i\<^isub>j \<Turnstile>c\<^isub>i\<^isub>j\<Rightarrow> X\<^isub>i"}. There can only be finitely many such |
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 |
|
75 | 433 |
single ``initial'' state in the equational system, which is different from |
92 | 434 |
the method by Brzozowski \cite{Brzozowski64}: he marks the ``terminal'' |
90 | 435 |
states. We are forced to set up the equational system in this way, because |
436 |
the Myhill-Nerode relation determines the ``direction'' of the transitions. |
|
437 |
The successor ``state'' of an equivalence class @{text Y} can be reached by adding |
|
438 |
characters to the end of @{text Y}. This is also the reason why we have to use |
|
439 |
our reverse version of Arden's lemma.) |
|
440 |
Overloading the function @{text L} for the two kinds of terms in the |
|
92 | 441 |
equational system, we have |
75 | 442 |
|
443 |
\begin{center} |
|
92 | 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} |
|
86 | 446 |
@{thm L_rhs_item.simps(1)[where r="r", THEN eq_reflection]} |
75 | 447 |
\end{center} |
448 |
||
449 |
\noindent |
|
450 |
we can prove for @{text "X\<^isub>2\<^isub>.\<^isub>.\<^isub>n"} that the following equations |
|
451 |
% |
|
452 |
\begin{equation}\label{inv1} |
|
83 | 453 |
@{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 | 454 |
\end{equation} |
455 |
||
456 |
\noindent |
|
457 |
hold. Similarly for @{text "X\<^isub>1"} we can show the following equation |
|
458 |
% |
|
459 |
\begin{equation}\label{inv2} |
|
83 | 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))"}. |
75 | 461 |
\end{equation} |
462 |
||
463 |
\noindent |
|
77 | 464 |
The reason for adding the @{text \<lambda>}-marker to our equational system is |
92 | 465 |
to obtain this equation: it only holds in this form since none of |
77 | 466 |
the other terms contain the empty string. |
467 |
||
468 |
||
92 | 469 |
Our proof of Thm.~\ref{myhillnerodeone} will proceed by transforming the |
89 | 470 |
equational system into a \emph{solved form} maintaining the invariants |
471 |
\eqref{inv1} and \eqref{inv2}. From the solved form we will be able to read |
|
472 |
off the regular expressions. |
|
473 |
||
474 |
In order to transform an equational system into solved form, we have two main |
|
475 |
operations: one that takes an equation of the form @{text "X = rhs"} and removes |
|
476 |
the recursive occurences of @{text X} in @{text rhs} using our variant of Arden's |
|
92 | 477 |
Lemma. The other operation takes an equation @{text "X = rhs"} |
89 | 478 |
and substitutes @{text X} throughout the rest of the equational system |
92 | 479 |
adjusting the remaining regular expressions approriately. To define this adjustment |
480 |
we define the \emph{append-operation} |
|
89 | 481 |
|
482 |
\begin{center} |
|
92 | 483 |
@{thm append_rexp.simps(2)[where X="Y" and r="r\<^isub>1" and rexp="r\<^isub>2", THEN eq_reflection]}\hspace{10mm} |
484 |
@{thm append_rexp.simps(1)[where r="r\<^isub>1" and rexp="r\<^isub>2", THEN eq_reflection]} |
|
89 | 485 |
\end{center} |
486 |
||
92 | 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"]}. |
|
75 | 490 |
|
92 | 491 |
|
492 |
\begin{center} |
|
493 |
@{thm arden_op_def} |
|
494 |
\end{center} |
|
54 | 495 |
*} |
496 |
||
497 |
section {* Regular Expressions Generate Finitely Many Partitions *} |
|
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
498 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
499 |
text {* |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
500 |
|
54 | 501 |
\begin{theorem} |
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
502 |
Given @{text "r"} is a regular expressions, then @{thm rexp_imp_finite}. |
54 | 503 |
\end{theorem} |
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
504 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
505 |
\begin{proof} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
506 |
By induction on the structure of @{text r}. The cases for @{const NULL}, @{const EMPTY} |
50 | 507 |
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
|
508 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
509 |
\begin{center} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
510 |
\begin{tabular}{l} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
511 |
@{thm quot_null_eq}\\ |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
512 |
@{thm quot_empty_subset}\\ |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
513 |
@{thm quot_char_subset} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
514 |
\end{tabular} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
515 |
\end{center} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
516 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
517 |
\end{proof} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
518 |
*} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
519 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
520 |
|
54 | 521 |
section {* Conclusion and Related Work *} |
522 |
||
92 | 523 |
text {* |
524 |
In this paper we took the view that a regular language as one where there exists |
|
525 |
a regular expression that matches all its strings. For us it was important to find |
|
526 |
out how far we can push this point of view. Having formalised the Myhill-Nerode |
|
527 |
theorem means pushed very far. Having the Myhill-Nerode theorem means we can |
|
528 |
formalise much of the textbook results in this subject. |
|
529 |
||
530 |
||
531 |
*} |
|
532 |
||
533 |
||
24 | 534 |
(*<*) |
535 |
end |
|
536 |
(*>*) |