author | urbanc |
Sun, 06 Feb 2011 11:21:12 +0000 | |
changeset 70 | 8ab3a06577cf |
parent 67 | 7478be786f87 |
child 71 | 426070e68b21 |
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 |
|
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
15 |
notation (latex output) |
50 | 16 |
str_eq_rel ("\<approx>\<^bsub>_\<^esub>") and |
17 |
Seq (infixr "\<cdot>" 100) and |
|
18 |
Star ("_\<^bsup>\<star>\<^esup>") and |
|
19 |
pow ("_\<^bsup>_\<^esup>" [100, 100] 100) and |
|
58 | 20 |
Suc ("_+1" [100] 100) and |
54 | 21 |
quotient ("_ \<^raw:\ensuremath{\!\sslash\!}> _" [90, 90] 90) and |
66 | 22 |
REL ("\<approx>") and |
67 | 23 |
UPLUS ("_ \<^raw:\ensuremath{\uplus}> _" [90, 90] 90) and |
70 | 24 |
L ("L '(_')" [0] 101) and |
25 |
EClass ("\<lbrakk>_\<rbrakk>\<^bsub>_\<^esub>" [100, 100] 100) |
|
24 | 26 |
(*>*) |
27 |
||
70 | 28 |
|
24 | 29 |
section {* Introduction *} |
30 |
||
31 |
text {* |
|
58 | 32 |
Regular languages are an important and well-understood subject in Computer |
60 | 33 |
Science, with many beautiful theorems and many useful algorithms. There is a |
66 | 34 |
wide range of textbooks on this subject, many of which are aimed at students |
35 |
and contain very detailed ``pencil-and-paper'' proofs |
|
60 | 36 |
(e.g.~\cite{Kozen97}). It seems natural to exercise theorem provers by |
37 |
formalising these theorems and by verifying formally the algorithms. |
|
59 | 38 |
|
66 | 39 |
There is however a problem: the typical approach to regular languages is to |
40 |
introduce finite automata and then define everything in terms of them. For |
|
41 |
example, a regular language is normally defined as one whose strings are |
|
42 |
recognised by a finite deterministic automaton. This approach has many |
|
43 |
benefits. Among them is that it is easy to convince oneself from the fact that |
|
44 |
regular languages are closed under complementation: one just has to exchange |
|
45 |
the accepting and non-accepting states in the corresponding automaton to |
|
46 |
obtain an automaton for the complement language. The problem, however, lies with |
|
67 | 47 |
formalising such reasoning in a HOL-based theorem prover, in our case |
70 | 48 |
Isabelle/HOL. Automata are build up from states and transitions that |
49 |
need to be represented as graphs or matrices, neither |
|
66 | 50 |
of which can be defined as inductive datatype.\footnote{In some works |
51 |
functions are used to represent state transitions, but also they are not |
|
52 |
inductive datatypes.} This means we have to build our own reasoning |
|
53 |
infrastructure for them, as neither Isabelle/HOL nor HOL4 nor HOLlight support |
|
54 |
them with libraries. |
|
55 |
||
56 |
Even worse, reasoning about graphs and matrices can be a real hassle in HOL-based |
|
57 |
theorem provers. Consider for example the operation of sequencing |
|
58 |
two automata, say $A_1$ and $A_2$, by connecting the |
|
67 | 59 |
accepting states of $A_1$ to the initial state of $A_2$: |
61 | 60 |
|
60 | 61 |
|
62 |
\begin{center} |
|
66 | 63 |
\begin{tabular}{ccc} |
64 |
\begin{tikzpicture}[scale=0.8] |
|
65 |
%\draw[step=2mm] (-1,-1) grid (1,1); |
|
66 |
||
67 |
\draw[rounded corners=1mm, very thick] (-1.0,-0.3) rectangle (-0.2,0.3); |
|
68 |
\draw[rounded corners=1mm, very thick] ( 0.2,-0.3) rectangle ( 1.0,0.3); |
|
69 |
||
70 |
\node (A) at (-1.0,0.0) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
71 |
\node (B) at ( 0.2,0.0) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
72 |
||
73 |
\node (C) at (-0.2, 0.13) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
74 |
\node (D) at (-0.2,-0.13) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
75 |
||
76 |
\node (E) at (1.0, 0.2) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
77 |
\node (F) at (1.0,-0.0) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
78 |
\node (G) at (1.0,-0.2) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
79 |
||
80 |
\draw (-0.6,0.0) node {\footnotesize$A_1$}; |
|
81 |
\draw ( 0.6,0.0) node {\footnotesize$A_2$}; |
|
82 |
\end{tikzpicture} |
|
83 |
||
84 |
& |
|
85 |
||
86 |
\raisebox{1.1mm}{\bf\Large$\;\;\;\Rightarrow\,\;\;$} |
|
87 |
||
88 |
& |
|
89 |
||
90 |
\begin{tikzpicture}[scale=0.8] |
|
91 |
%\draw[step=2mm] (-1,-1) grid (1,1); |
|
92 |
||
93 |
\draw[rounded corners=1mm, very thick] (-1.0,-0.3) rectangle (-0.2,0.3); |
|
94 |
\draw[rounded corners=1mm, very thick] ( 0.2,-0.3) rectangle ( 1.0,0.3); |
|
95 |
||
96 |
\node (A) at (-1.0,0.0) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
97 |
\node (B) at ( 0.2,0.0) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
98 |
||
99 |
\node (C) at (-0.2, 0.13) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
100 |
\node (D) at (-0.2,-0.13) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
101 |
||
102 |
\node (E) at (1.0, 0.2) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
103 |
\node (F) at (1.0,-0.0) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
104 |
\node (G) at (1.0,-0.2) [circle, very thick, draw, fill=white, inner sep=0.4mm] {}; |
|
105 |
||
106 |
\draw (C) to [very thick, bend left=45] (B); |
|
107 |
\draw (D) to [very thick, bend right=45] (B); |
|
108 |
||
109 |
\draw (-0.6,0.0) node {\footnotesize$A_1$}; |
|
110 |
\draw ( 0.6,0.0) node {\footnotesize$A_2$}; |
|
111 |
\end{tikzpicture} |
|
112 |
||
113 |
\end{tabular} |
|
60 | 114 |
\end{center} |
115 |
||
116 |
\noindent |
|
67 | 117 |
On ``paper'' we can define the corresponding graph in terms of the disjoint |
118 |
union of the state nodes. Unfortunately in HOL, the definition for disjoint |
|
66 | 119 |
union, namely |
60 | 120 |
|
61 | 121 |
\begin{center} |
66 | 122 |
@{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}"} |
61 | 123 |
\end{center} |
60 | 124 |
|
61 | 125 |
\noindent |
66 | 126 |
changes the type---the disjoint union is not a set, but a set of pairs. |
127 |
Using this definition for disjoint unions means we do not have a single type for automata |
|
128 |
and hence will not be able to state properties about \emph{all} |
|
67 | 129 |
automata, since there is no type quantification available in HOL. An |
130 |
alternative, which provides us with a single type for automata, is to give every |
|
131 |
state node an identity, for example a natural |
|
70 | 132 |
number, and then be careful to rename these identities apart whenever |
67 | 133 |
connecting two automata. This results in clunky proofs |
66 | 134 |
establishing that properties are invariant under renaming. Similarly, |
67 | 135 |
connecting two automata represented as matrices results in very adhoc |
66 | 136 |
constructions, which are not pleasant to reason about. |
137 |
||
138 |
Because of these problems to do with representing automata, there seems |
|
139 |
to be no substantial formalisation of automata theory and regular languages |
|
140 |
carried out in a HOL-based theorem prover. We are only aware of the |
|
70 | 141 |
large formalisation of automata theory in Nuprl \cite{Constable00} and |
67 | 142 |
some smaller formalisations in Coq, for example \cite{Filliatre97}. |
58 | 143 |
|
66 | 144 |
In this paper, we will not attempt to formalise automata theory, but take a completely |
145 |
different approach to regular languages. Instead of defining a regular language as one |
|
146 |
where there exists an automaton that recognises all strings of the language, we define |
|
67 | 147 |
a regular language as: |
54 | 148 |
|
149 |
\begin{definition}[A Regular Language] |
|
66 | 150 |
A language @{text A} is regular, provided there is a regular expression that matches all |
54 | 151 |
strings of @{text "A"}. |
152 |
\end{definition} |
|
153 |
||
154 |
\noindent |
|
66 | 155 |
The reason is that regular expressions, unlike graphs and matrices, can |
156 |
be easily defined as inductive datatype. Therefore a corresponding reasoning |
|
70 | 157 |
infrastructure comes for free. This has recently been used in HOL4 for formalising regular |
158 |
expression matching based on derivatives \cite{OwensSlind08}. The purpose of this paper is to |
|
66 | 159 |
show that a central result about regular languages, the Myhill-Nerode theorem, |
70 | 160 |
can be recreated by only using regular expressions. This theorem gives a necessary |
67 | 161 |
and sufficient condition for when a language is regular. As a corollary of this |
162 |
theorem we can easily establish the usual closure properties, including |
|
163 |
complementation, for regular languages.\smallskip |
|
61 | 164 |
|
165 |
\noindent |
|
67 | 166 |
{\bf Contributions:} To our knowledge, our proof of the Myhill-Nerode theorem is the |
167 |
first that is based on regular expressions, only. We prove the part of this theorem |
|
168 |
stating that a regular expression has only finitely many partitions using certain |
|
169 |
tagging-functions. Again to our best knowledge, these tagging functions have |
|
170 |
not been used before to establish the Myhill-Nerode theorem. |
|
24 | 171 |
*} |
172 |
||
50 | 173 |
section {* Preliminaries *} |
174 |
||
175 |
text {* |
|
67 | 176 |
Strings in Isabelle/HOL are lists of characters with the \emph{empty string} |
177 |
being represented by the empty list, written @{term "[]"}. \emph{Languages} |
|
178 |
are sets of strings. The language containing all strings is written in |
|
179 |
Isabelle/HOL as @{term "UNIV::string set"}. The notation for the quotient |
|
180 |
of a language @{text A} according to a relation @{term REL} is @{term "A // |
|
181 |
REL"}. The concatenation of two languages is written @{term "A ;; B"}; a |
|
182 |
language raised to the power $n$ is written @{term "A \<up> n"}. Both concepts |
|
183 |
are defined as |
|
54 | 184 |
|
185 |
\begin{center} |
|
58 | 186 |
@{thm Seq_def[THEN eq_reflection, where A1="A" and B1="B"]} |
187 |
\hspace{7mm} |
|
188 |
@{thm pow.simps(1)[THEN eq_reflection, where A1="A"]} |
|
189 |
\hspace{7mm} |
|
190 |
@{thm pow.simps(2)[THEN eq_reflection, where A1="A" and n1="n"]} |
|
54 | 191 |
\end{center} |
192 |
||
193 |
\noindent |
|
58 | 194 |
where @{text "@"} is the usual list-append operation. The Kleene-star of a language @{text A} |
195 |
is defined as the union over all powers, namely @{thm Star_def}. |
|
196 |
||
51 | 197 |
Central to our proof will be the solution of equational systems |
50 | 198 |
involving regular expressions. For this we will use the following ``reverse'' |
199 |
version of Arden's lemma. |
|
200 |
||
201 |
\begin{lemma}[Reverse Arden's Lemma]\mbox{}\\ |
|
202 |
If @{thm (prem 1) ardens_revised} then |
|
203 |
@{thm (lhs) ardens_revised} has the unique solution |
|
204 |
@{thm (rhs) ardens_revised}. |
|
205 |
\end{lemma} |
|
206 |
||
207 |
\begin{proof} |
|
51 | 208 |
For the right-to-left direction we assume @{thm (rhs) ardens_revised} and show |
209 |
that @{thm (lhs) ardens_revised} holds. From Lemma ??? we have @{term "A\<star> = {[]} \<union> A ;; A\<star>"}, |
|
50 | 210 |
which is equal to @{term "A\<star> = {[]} \<union> A\<star> ;; A"}. Adding @{text B} to both |
211 |
sides gives @{term "B ;; A\<star> = B ;; ({[]} \<union> A\<star> ;; A)"}, whose right-hand side |
|
51 | 212 |
is equal to @{term "(B ;; A\<star>) ;; A \<union> B"}. This completes this direction. |
50 | 213 |
|
214 |
For the other direction we assume @{thm (lhs) ardens_revised}. By a simple induction |
|
51 | 215 |
on @{text n}, we can establish the property |
50 | 216 |
|
217 |
\begin{center} |
|
218 |
@{text "(*)"}\hspace{5mm} @{thm (concl) ardens_helper} |
|
219 |
\end{center} |
|
220 |
||
221 |
\noindent |
|
222 |
Using this property we can show that @{term "B ;; (A \<up> n) \<subseteq> X"} holds for |
|
223 |
all @{text n}. From this we can infer @{term "B ;; A\<star> \<subseteq> X"} using Lemma ???. |
|
51 | 224 |
For the inclusion in the other direction we assume a string @{text s} |
50 | 225 |
with length @{text k} is element in @{text X}. Since @{thm (prem 1) ardens_revised} |
51 | 226 |
we know that @{term "s \<notin> X ;; (A \<up> Suc k)"} since its length is only @{text k} |
227 |
(the strings in @{term "X ;; (A \<up> Suc k)"} are all longer). |
|
53 | 228 |
From @{text "(*)"} it follows then that |
50 | 229 |
@{term s} must be element in @{term "(\<Union>m\<in>{0..k}. B ;; (A \<up> m))"}. This in turn |
230 |
implies that @{term s} is in @{term "(\<Union>n. B ;; (A \<up> n))"}. Using Lemma ??? this |
|
231 |
is equal to @{term "B ;; A\<star>"}, as we needed to show.\qed |
|
232 |
\end{proof} |
|
67 | 233 |
|
234 |
\noindent |
|
235 |
Regular expressions are defined as the following inductive datatype |
|
236 |
||
237 |
\begin{center} |
|
238 |
@{text r} @{text "::="} |
|
239 |
@{term NULL}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
240 |
@{term EMPTY}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
241 |
@{term "CHAR c"}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
242 |
@{term "SEQ r r"}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
243 |
@{term "ALT r r"}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
244 |
@{term "STAR r"} |
|
245 |
\end{center} |
|
246 |
||
247 |
\noindent |
|
248 |
The language matched by a regular expression is defined as usual: |
|
249 |
||
250 |
\begin{center} |
|
251 |
\begin{tabular}{c@ {\hspace{10mm}}c} |
|
252 |
\begin{tabular}{rcl} |
|
253 |
@{thm (lhs) L_rexp.simps(1)} & @{text "\<equiv>"} & @{thm (rhs) L_rexp.simps(1)}\\ |
|
254 |
@{thm (lhs) L_rexp.simps(2)} & @{text "\<equiv>"} & @{thm (rhs) L_rexp.simps(2)}\\ |
|
255 |
@{thm (lhs) L_rexp.simps(3)[where c="c"]} & @{text "\<equiv>"} & @{thm (rhs) L_rexp.simps(3)[where c="c"]}\\ |
|
256 |
\end{tabular} |
|
257 |
& |
|
258 |
\begin{tabular}{rcl} |
|
259 |
@{thm (lhs) L_rexp.simps(4)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]} & @{text "\<equiv>"} & |
|
260 |
@{thm (rhs) L_rexp.simps(4)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]}\\ |
|
261 |
@{thm (lhs) L_rexp.simps(5)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]} & @{text "\<equiv>"} & |
|
262 |
@{thm (rhs) L_rexp.simps(5)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]}\\ |
|
263 |
@{thm (lhs) L_rexp.simps(6)[where r="r"]} & @{text "\<equiv>"} & |
|
264 |
@{thm (rhs) L_rexp.simps(6)[where r="r"]}\\ |
|
265 |
\end{tabular} |
|
266 |
\end{tabular} |
|
267 |
\end{center} |
|
70 | 268 |
|
50 | 269 |
*} |
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
270 |
|
54 | 271 |
section {* Finite Partitions Imply Regularity of a Language *} |
272 |
||
273 |
text {* |
|
70 | 274 |
\begin{definition}[Myhill-Nerode Relation]\mbox{}\\ |
275 |
@{thm str_eq_rel_def[simplified]} |
|
276 |
\end{definition} |
|
277 |
||
278 |
\begin{definition} @{text "finals A"} are the equivalence classes that contain |
|
279 |
strings from @{text A}\\ |
|
280 |
@{thm finals_def} |
|
281 |
\end{definition} |
|
282 |
||
283 |
@{thm lang_is_union_of_finals} |
|
284 |
||
285 |
||
54 | 286 |
\begin{theorem} |
287 |
Given a language @{text A}. |
|
70 | 288 |
@{thm[mode=IfThen] hard_direction} |
54 | 289 |
\end{theorem} |
290 |
*} |
|
291 |
||
292 |
section {* Regular Expressions Generate Finitely Many Partitions *} |
|
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
293 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
294 |
text {* |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
295 |
|
54 | 296 |
\begin{theorem} |
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
297 |
Given @{text "r"} is a regular expressions, then @{thm rexp_imp_finite}. |
54 | 298 |
\end{theorem} |
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
299 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
300 |
\begin{proof} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
301 |
By induction on the structure of @{text r}. The cases for @{const NULL}, @{const EMPTY} |
50 | 302 |
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
|
303 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
304 |
\begin{center} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
305 |
\begin{tabular}{l} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
306 |
@{thm quot_null_eq}\\ |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
307 |
@{thm quot_empty_subset}\\ |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
308 |
@{thm quot_char_subset} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
309 |
\end{tabular} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
310 |
\end{center} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
311 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
312 |
\end{proof} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
313 |
*} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
314 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
315 |
|
54 | 316 |
section {* Conclusion and Related Work *} |
317 |
||
24 | 318 |
(*<*) |
319 |
end |
|
320 |
(*>*) |