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