author | urbanc |
Thu, 17 Feb 2011 13:25:29 +0000 | |
changeset 111 | d65d071798ff |
parent 110 | e500cab16be4 |
child 112 | 62fdb4bf7239 |
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 |
|
95 | 36 |
append_rhs_rexp ("_ \<^raw:\ensuremath{\triangleleft}> _" [100, 100] 50) |
92 | 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 |
101 | 49 |
formalising the 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} |
101 | 150 |
dismisses for this the option of using identities, because it leads according to |
151 |
him to ``messy proofs''. He |
|
103 | 152 |
opts for a variant of \eqref{disjointunion} using bit lists, but writes |
82 | 153 |
|
154 |
\begin{quote} |
|
93 | 155 |
\it% |
156 |
\begin{tabular}{@ {}l@ {}p{0.88\textwidth}@ {}} |
|
101 | 157 |
`` & All lemmas appear obvious given a picture of the composition of automata\ldots |
158 |
Yet their proofs require a painful amount of detail.'' |
|
159 |
\end{tabular} |
|
160 |
\end{quote} |
|
161 |
||
162 |
\noindent |
|
163 |
and |
|
164 |
||
165 |
\begin{quote} |
|
166 |
\it% |
|
167 |
\begin{tabular}{@ {}l@ {}p{0.88\textwidth}@ {}} |
|
93 | 168 |
`` & If the reader finds the above treatment in terms of bit lists revoltingly |
101 | 169 |
concrete, I cannot disagree. A more abstract approach is clearly desirable.'' |
93 | 170 |
\end{tabular} |
82 | 171 |
\end{quote} |
101 | 172 |
|
173 |
||
82 | 174 |
\noindent |
175 |
Moreover, it is not so clear how to conveniently impose a finiteness condition |
|
176 |
upon functions in order to represent \emph{finite} automata. The best is |
|
92 | 177 |
probably to resort to more advanced reasoning frameworks, such as \emph{locales} |
178 |
or \emph{type classes}, |
|
110 | 179 |
which are \emph{not} avaiable in all HOL-based theorem provers. |
82 | 180 |
|
66 | 181 |
Because of these problems to do with representing automata, there seems |
182 |
to be no substantial formalisation of automata theory and regular languages |
|
93 | 183 |
carried out in HOL-based theorem provers. Nipkow establishes in |
82 | 184 |
\cite{Nipkow98} the link between regular expressions and automata in |
100 | 185 |
the context of lexing. The only larger formalisations of automata theory |
82 | 186 |
are carried out in Nuprl \cite{Constable00} and in Coq (for example |
187 |
\cite{Filliatre97}). |
|
58 | 188 |
|
82 | 189 |
In this paper, we will not attempt to formalise automata theory in |
190 |
Isabelle/HOL, but take a completely different approach to regular |
|
191 |
languages. Instead of defining a regular language as one where there exists |
|
192 |
an automaton that recognises all strings of the language, we define a |
|
193 |
regular language as: |
|
54 | 194 |
|
82 | 195 |
\begin{definition} |
77 | 196 |
A language @{text A} is \emph{regular}, provided there is a regular expression that matches all |
54 | 197 |
strings of @{text "A"}. |
198 |
\end{definition} |
|
199 |
||
200 |
\noindent |
|
110 | 201 |
The reason is that regular expressions, unlike graphs, matrices and functions, can |
71 | 202 |
be easily defined as inductive datatype. Consequently a corresponding reasoning |
203 |
infrastructure comes for free. This has recently been exploited in HOL4 with a formalisation |
|
101 | 204 |
of regular expression matching based on derivatives \cite{OwensSlind08} and |
205 |
with an equivalence checker for regular expressions in Isabelle/HOL \cite{KraussNipkow11}. |
|
206 |
The purpose of this paper is to |
|
71 | 207 |
show that a central result about regular languages---the Myhill-Nerode theorem---can |
208 |
be recreated by only using regular expressions. This theorem gives necessary |
|
209 |
and sufficient conditions for when a language is regular. As a corollary of this |
|
67 | 210 |
theorem we can easily establish the usual closure properties, including |
211 |
complementation, for regular languages.\smallskip |
|
61 | 212 |
|
213 |
\noindent |
|
88 | 214 |
{\bf Contributions:} |
215 |
There is an extensive literature on regular languages. |
|
216 |
To our knowledge, our proof of the Myhill-Nerode theorem is the |
|
67 | 217 |
first that is based on regular expressions, only. We prove the part of this theorem |
218 |
stating that a regular expression has only finitely many partitions using certain |
|
219 |
tagging-functions. Again to our best knowledge, these tagging functions have |
|
220 |
not been used before to establish the Myhill-Nerode theorem. |
|
24 | 221 |
*} |
222 |
||
50 | 223 |
section {* Preliminaries *} |
224 |
||
225 |
text {* |
|
67 | 226 |
Strings in Isabelle/HOL are lists of characters with the \emph{empty string} |
92 | 227 |
being represented by the empty list, written @{term "[]"}. \emph{Languages} |
67 | 228 |
are sets of strings. The language containing all strings is written in |
71 | 229 |
Isabelle/HOL as @{term "UNIV::string set"}. The concatenation of two languages |
90 | 230 |
is written @{term "A ;; B"} and a language raised to the power @{text n} is written |
93 | 231 |
@{term "A \<up> n"}. They are defined as usual |
54 | 232 |
|
233 |
\begin{center} |
|
58 | 234 |
@{thm Seq_def[THEN eq_reflection, where A1="A" and B1="B"]} |
235 |
\hspace{7mm} |
|
236 |
@{thm pow.simps(1)[THEN eq_reflection, where A1="A"]} |
|
237 |
\hspace{7mm} |
|
238 |
@{thm pow.simps(2)[THEN eq_reflection, where A1="A" and n1="n"]} |
|
54 | 239 |
\end{center} |
240 |
||
241 |
\noindent |
|
58 | 242 |
where @{text "@"} is the usual list-append operation. The Kleene-star of a language @{text A} |
71 | 243 |
is defined as the union over all powers, namely @{thm Star_def}. In the paper |
88 | 244 |
we will make use of the following properties of these constructions. |
58 | 245 |
|
71 | 246 |
\begin{proposition}\label{langprops}\mbox{}\\ |
92 | 247 |
\begin{tabular}{@ {}ll} |
248 |
(i) & @{thm star_cases} \\ |
|
249 |
(ii) & @{thm[mode=IfThen] pow_length}\\ |
|
250 |
(iii) & @{thm seq_Union_left} \\ |
|
71 | 251 |
\end{tabular} |
252 |
\end{proposition} |
|
253 |
||
254 |
\noindent |
|
100 | 255 |
In @{text "(ii)"} we use the notation @{term "length s"} for the length of a |
256 |
string. This property states that if @{term "[] \<notin> A"} then the lengths of |
|
257 |
the strings in @{term "A \<up> (Suc n)"} must be longer than @{text n}. We omit |
|
258 |
the proofs for these properties, but invite the reader to consult our |
|
259 |
formalisation.\footnote{Available at ???} |
|
71 | 260 |
|
90 | 261 |
The notation in Isabelle/HOL for the quotient of a language @{text A} according to an |
262 |
equivalence relation @{term REL} is @{term "A // REL"}. We will write |
|
71 | 263 |
@{text "\<lbrakk>x\<rbrakk>\<^isub>\<approx>"} for the equivalence class defined |
264 |
as @{text "{y | y \<approx> x}"}. |
|
265 |
||
266 |
||
51 | 267 |
Central to our proof will be the solution of equational systems |
101 | 268 |
involving equivalence classes of languages. For this we will use Arden's lemma \cite{Brzozowski64} |
93 | 269 |
which solves equations of the form @{term "X = A ;; X \<union> B"} provided |
71 | 270 |
@{term "[] \<notin> A"}. However we will need the following ``reverse'' |
50 | 271 |
version of Arden's lemma. |
272 |
||
75 | 273 |
\begin{lemma}[Reverse Arden's Lemma]\label{arden}\mbox{}\\ |
86 | 274 |
If @{thm (prem 1) arden} then |
275 |
@{thm (lhs) arden} has the unique solution |
|
276 |
@{thm (rhs) arden}. |
|
50 | 277 |
\end{lemma} |
278 |
||
279 |
\begin{proof} |
|
86 | 280 |
For the right-to-left direction we assume @{thm (rhs) arden} and show |
281 |
that @{thm (lhs) arden} holds. From Prop.~\ref{langprops}@{text "(i)"} |
|
71 | 282 |
we have @{term "A\<star> = {[]} \<union> A ;; A\<star>"}, |
50 | 283 |
which is equal to @{term "A\<star> = {[]} \<union> A\<star> ;; A"}. Adding @{text B} to both |
284 |
sides gives @{term "B ;; A\<star> = B ;; ({[]} \<union> A\<star> ;; A)"}, whose right-hand side |
|
51 | 285 |
is equal to @{term "(B ;; A\<star>) ;; A \<union> B"}. This completes this direction. |
50 | 286 |
|
86 | 287 |
For the other direction we assume @{thm (lhs) arden}. By a simple induction |
51 | 288 |
on @{text n}, we can establish the property |
50 | 289 |
|
290 |
\begin{center} |
|
86 | 291 |
@{text "(*)"}\hspace{5mm} @{thm (concl) arden_helper} |
50 | 292 |
\end{center} |
293 |
||
294 |
\noindent |
|
295 |
Using this property we can show that @{term "B ;; (A \<up> n) \<subseteq> X"} holds for |
|
71 | 296 |
all @{text n}. From this we can infer @{term "B ;; A\<star> \<subseteq> X"} using the definition |
297 |
of @{text "\<star>"}. |
|
51 | 298 |
For the inclusion in the other direction we assume a string @{text s} |
86 | 299 |
with length @{text k} is element in @{text X}. Since @{thm (prem 1) arden} |
75 | 300 |
we know by Prop.~\ref{langprops}@{text "(ii)"} that |
71 | 301 |
@{term "s \<notin> X ;; (A \<up> Suc k)"} since its length is only @{text k} |
51 | 302 |
(the strings in @{term "X ;; (A \<up> Suc k)"} are all longer). |
53 | 303 |
From @{text "(*)"} it follows then that |
50 | 304 |
@{term s} must be element in @{term "(\<Union>m\<in>{0..k}. B ;; (A \<up> m))"}. This in turn |
75 | 305 |
implies that @{term s} is in @{term "(\<Union>n. B ;; (A \<up> n))"}. Using Prop.~\ref{langprops}@{text "(iii)"} |
71 | 306 |
this is equal to @{term "B ;; A\<star>"}, as we needed to show.\qed |
50 | 307 |
\end{proof} |
67 | 308 |
|
309 |
\noindent |
|
88 | 310 |
Regular expressions are defined as the inductive datatype |
67 | 311 |
|
312 |
\begin{center} |
|
313 |
@{text r} @{text "::="} |
|
314 |
@{term NULL}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
315 |
@{term EMPTY}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
316 |
@{term "CHAR c"}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
317 |
@{term "SEQ r r"}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
318 |
@{term "ALT r r"}\hspace{1.5mm}@{text"|"}\hspace{1.5mm} |
|
319 |
@{term "STAR r"} |
|
320 |
\end{center} |
|
321 |
||
322 |
\noindent |
|
88 | 323 |
and the language matched by a regular expression is defined as |
67 | 324 |
|
325 |
\begin{center} |
|
326 |
\begin{tabular}{c@ {\hspace{10mm}}c} |
|
327 |
\begin{tabular}{rcl} |
|
328 |
@{thm (lhs) L_rexp.simps(1)} & @{text "\<equiv>"} & @{thm (rhs) L_rexp.simps(1)}\\ |
|
329 |
@{thm (lhs) L_rexp.simps(2)} & @{text "\<equiv>"} & @{thm (rhs) L_rexp.simps(2)}\\ |
|
330 |
@{thm (lhs) L_rexp.simps(3)[where c="c"]} & @{text "\<equiv>"} & @{thm (rhs) L_rexp.simps(3)[where c="c"]}\\ |
|
331 |
\end{tabular} |
|
332 |
& |
|
333 |
\begin{tabular}{rcl} |
|
334 |
@{thm (lhs) L_rexp.simps(4)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]} & @{text "\<equiv>"} & |
|
335 |
@{thm (rhs) L_rexp.simps(4)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]}\\ |
|
336 |
@{thm (lhs) L_rexp.simps(5)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]} & @{text "\<equiv>"} & |
|
337 |
@{thm (rhs) L_rexp.simps(5)[where ?r1.0="r\<^isub>1" and ?r2.0="r\<^isub>2"]}\\ |
|
338 |
@{thm (lhs) L_rexp.simps(6)[where r="r"]} & @{text "\<equiv>"} & |
|
339 |
@{thm (rhs) L_rexp.simps(6)[where r="r"]}\\ |
|
340 |
\end{tabular} |
|
341 |
\end{tabular} |
|
342 |
\end{center} |
|
70 | 343 |
|
100 | 344 |
Given a finite set of regular expressions @{text rs}, we will make use of the operation of generating |
92 | 345 |
a regular expression that matches all languages of @{text rs}. We only need to know the existence |
346 |
of such a regular expression and therefore we use Isabelle/HOL's @{const "fold_graph"} and Hilbert's |
|
93 | 347 |
@{text "\<epsilon>"} to define @{term "\<Uplus>rs"}. This operation, roughly speaking, folds @{const ALT} over the |
100 | 348 |
set @{text rs} with @{const NULL} for the empty set. We can prove that for a finite set @{text rs} |
110 | 349 |
% |
350 |
\begin{equation}\label{uplus} |
|
351 |
\mbox{@{thm (lhs) folds_alt_simp} @{text "= \<Union> (\<calL> ` rs)"}} |
|
352 |
\end{equation} |
|
88 | 353 |
|
354 |
\noindent |
|
90 | 355 |
holds, whereby @{text "\<calL> ` rs"} stands for the |
356 |
image of the set @{text rs} under function @{text "\<calL>"}. |
|
50 | 357 |
*} |
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
358 |
|
100 | 359 |
section {* The Myhill-Nerode Theorem, First Part *} |
54 | 360 |
|
361 |
text {* |
|
77 | 362 |
The key definition in the Myhill-Nerode theorem is the |
75 | 363 |
\emph{Myhill-Nerode relation}, which states that w.r.t.~a language two |
364 |
strings are related, provided there is no distinguishing extension in this |
|
365 |
language. This can be defined as: |
|
366 |
||
70 | 367 |
\begin{definition}[Myhill-Nerode Relation]\mbox{}\\ |
75 | 368 |
@{thm str_eq_def[simplified str_eq_rel_def Pair_Collect]} |
70 | 369 |
\end{definition} |
370 |
||
71 | 371 |
\noindent |
75 | 372 |
It is easy to see that @{term "\<approx>A"} is an equivalence relation, which |
373 |
partitions the set of all strings, @{text "UNIV"}, into a set of disjoint |
|
108 | 374 |
equivalence classes. To illustrate this quotient construction, let us give a simple |
101 | 375 |
example: consider the regular language containing just |
92 | 376 |
the string @{text "[c]"}. The relation @{term "\<approx>({[c]})"} partitions @{text UNIV} |
101 | 377 |
into three equivalence classes @{text "X\<^isub>1"}, @{text "X\<^isub>2"} and @{text "X\<^isub>3"} |
90 | 378 |
as follows |
379 |
||
380 |
\begin{center} |
|
381 |
@{text "X\<^isub>1 = {[]}"}\hspace{5mm} |
|
382 |
@{text "X\<^isub>2 = {[c]}"}\hspace{5mm} |
|
383 |
@{text "X\<^isub>3 = UNIV - {[], [c]}"} |
|
384 |
\end{center} |
|
385 |
||
386 |
One direction of the Myhill-Nerode theorem establishes |
|
93 | 387 |
that if there are finitely many equivalence classes, like in the example above, then |
388 |
the language is regular. In our setting we therefore have to show: |
|
75 | 389 |
|
390 |
\begin{theorem}\label{myhillnerodeone} |
|
96 | 391 |
@{thm[mode=IfThen] Myhill_Nerode1} |
75 | 392 |
\end{theorem} |
71 | 393 |
|
75 | 394 |
\noindent |
90 | 395 |
To prove this theorem, we first define the set @{term "finals A"} as those equivalence |
100 | 396 |
classes from @{term "UNIV // \<approx>A"} that contain strings of @{text A}, namely |
75 | 397 |
% |
71 | 398 |
\begin{equation} |
70 | 399 |
@{thm finals_def} |
71 | 400 |
\end{equation} |
401 |
||
402 |
\noindent |
|
92 | 403 |
In our running example, @{text "X\<^isub>2"} is the only equivalence class in @{term "finals {[c]}"}. |
90 | 404 |
It is straightforward to show that in general @{thm lang_is_union_of_finals} and |
79 | 405 |
@{thm finals_in_partitions} hold. |
75 | 406 |
Therefore if we know that there exists a regular expression for every |
100 | 407 |
equivalence class in \mbox{@{term "finals A"}} (which by assumption must be |
93 | 408 |
a finite set), then we can use @{text "\<bigplus>"} to obtain a regular expression |
98 | 409 |
that matches every string in @{text A}. |
70 | 410 |
|
75 | 411 |
|
90 | 412 |
Our proof of Thm.~\ref{myhillnerodeone} relies on a method that can calculate a |
79 | 413 |
regular expression for \emph{every} equivalence class, not just the ones |
77 | 414 |
in @{term "finals A"}. We |
93 | 415 |
first define the notion of \emph{one-character-transition} between |
416 |
two equivalence classes |
|
75 | 417 |
% |
71 | 418 |
\begin{equation} |
419 |
@{thm transition_def} |
|
420 |
\end{equation} |
|
70 | 421 |
|
71 | 422 |
\noindent |
92 | 423 |
which means that if we concatenate the character @{text c} to the end of all |
424 |
strings in the equivalence class @{text Y}, we obtain a subset of |
|
77 | 425 |
@{text X}. Note that we do not define an automaton here, we merely relate two sets |
110 | 426 |
(with the help of a character). In our concrete example we have |
92 | 427 |
@{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 | 428 |
other character than @{text c}, and @{term "X\<^isub>3 \<Turnstile>d\<Rightarrow> X\<^isub>3"} for any @{text d}. |
75 | 429 |
|
110 | 430 |
Next we build an \emph{initial equational system} that |
75 | 431 |
contains an equation for each equivalence class. Suppose we have |
432 |
the equivalence classes @{text "X\<^isub>1,\<dots>,X\<^isub>n"}, there must be one and only one that |
|
433 |
contains the empty string @{text "[]"} (since equivalence classes are disjoint). |
|
77 | 434 |
Let us assume @{text "[] \<in> X\<^isub>1"}. We build the following equational system |
75 | 435 |
|
436 |
\begin{center} |
|
437 |
\begin{tabular}{rcl} |
|
438 |
@{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)"} \\ |
|
439 |
@{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)"} \\ |
|
440 |
& $\vdots$ \\ |
|
441 |
@{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)"}\\ |
|
442 |
\end{tabular} |
|
443 |
\end{center} |
|
70 | 444 |
|
75 | 445 |
\noindent |
100 | 446 |
where the terms @{text "(Y\<^isub>i\<^isub>j, CHAR c\<^isub>i\<^isub>j)"} |
447 |
stand for all transitions @{term "Y\<^isub>i\<^isub>j \<Turnstile>c\<^isub>i\<^isub>j\<Rightarrow> |
|
448 |
X\<^isub>i"}. There can only be |
|
110 | 449 |
finitely many such terms in a right-hand side since by assumption there are only finitely many |
100 | 450 |
equivalence classes and only finitely many characters. The term @{text |
451 |
"\<lambda>(EMPTY)"} in the first equation acts as a marker for the equivalence class |
|
452 |
containing @{text "[]"}.\footnote{Note that we mark, roughly speaking, the |
|
75 | 453 |
single ``initial'' state in the equational system, which is different from |
100 | 454 |
the method by Brzozowski \cite{Brzozowski64}, where he marks the |
455 |
``terminal'' states. We are forced to set up the equational system in our |
|
456 |
way, because the Myhill-Nerode relation determines the ``direction'' of the |
|
457 |
transitions. The successor ``state'' of an equivalence class @{text Y} can |
|
458 |
be reached by adding characters to the end of @{text Y}. This is also the |
|
459 |
reason why we have to use our reverse version of Arden's lemma.} |
|
460 |
Overloading the function @{text \<calL>} for the two kinds of terms in the |
|
92 | 461 |
equational system, we have |
75 | 462 |
|
463 |
\begin{center} |
|
92 | 464 |
@{text "\<calL>(Y, r) \<equiv>"} % |
465 |
@{thm (rhs) L_rhs_item.simps(2)[where X="Y" and r="r", THEN eq_reflection]}\hspace{10mm} |
|
86 | 466 |
@{thm L_rhs_item.simps(1)[where r="r", THEN eq_reflection]} |
75 | 467 |
\end{center} |
468 |
||
469 |
\noindent |
|
100 | 470 |
and we can prove for @{text "X\<^isub>2\<^isub>.\<^isub>.\<^isub>n"} that the following equations |
75 | 471 |
% |
472 |
\begin{equation}\label{inv1} |
|
83 | 473 |
@{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 | 474 |
\end{equation} |
475 |
||
476 |
\noindent |
|
477 |
hold. Similarly for @{text "X\<^isub>1"} we can show the following equation |
|
478 |
% |
|
479 |
\begin{equation}\label{inv2} |
|
83 | 480 |
@{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 | 481 |
\end{equation} |
482 |
||
483 |
\noindent |
|
101 | 484 |
The reason for adding the @{text \<lambda>}-marker to our initial equational system is |
103 | 485 |
to obtain this equation: it only holds with the marker, since none of |
108 | 486 |
the other terms contain the empty string. The point of the initial equational system is |
487 |
that solving it means we will be able to extract a regular expression for every equivalence class. |
|
100 | 488 |
|
101 | 489 |
Our representation for the equations in Isabelle/HOL are pairs, |
108 | 490 |
where the first component is an equivalence class (a set of strings) |
491 |
and the second component |
|
101 | 492 |
is a set of terms. Given a set of equivalence |
100 | 493 |
classes @{text CS}, our initial equational system @{term "Init CS"} is thus |
101 | 494 |
formally defined as |
104 | 495 |
% |
496 |
\begin{equation}\label{initcs} |
|
497 |
\mbox{\begin{tabular}{rcl} |
|
100 | 498 |
@{thm (lhs) Init_rhs_def} & @{text "\<equiv>"} & |
499 |
@{text "if"}~@{term "[] \<in> X"}\\ |
|
500 |
& & @{text "then"}~@{term "{Trn Y (CHAR c) | Y c. Y \<in> CS \<and> Y \<Turnstile>c\<Rightarrow> X} \<union> {Lam EMPTY}"}\\ |
|
501 |
& & @{text "else"}~@{term "{Trn Y (CHAR c)| Y c. Y \<in> CS \<and> Y \<Turnstile>c\<Rightarrow> X}"}\\ |
|
502 |
@{thm (lhs) Init_def} & @{text "\<equiv>"} & @{thm (rhs) Init_def} |
|
104 | 503 |
\end{tabular}} |
504 |
\end{equation} |
|
100 | 505 |
|
506 |
||
507 |
||
508 |
\noindent |
|
509 |
Because we use sets of terms |
|
101 | 510 |
for representing the right-hand sides of equations, we can |
100 | 511 |
prove \eqref{inv1} and \eqref{inv2} more concisely as |
93 | 512 |
% |
100 | 513 |
\begin{lemma}\label{inv} |
514 |
If @{thm (prem 1) test} then @{text "X = \<Union> \<calL> ` rhs"}. |
|
515 |
\end{lemma} |
|
77 | 516 |
|
93 | 517 |
\noindent |
92 | 518 |
Our proof of Thm.~\ref{myhillnerodeone} will proceed by transforming the |
100 | 519 |
initial equational system into one in \emph{solved form} maintaining the invariant |
108 | 520 |
in Lem.~\ref{inv}. From the solved form we will be able to read |
89 | 521 |
off the regular expressions. |
522 |
||
100 | 523 |
In order to transform an equational system into solved form, we have two |
89 | 524 |
operations: one that takes an equation of the form @{text "X = rhs"} and removes |
110 | 525 |
any recursive occurrences of @{text X} in the @{text rhs} using our variant of Arden's |
92 | 526 |
Lemma. The other operation takes an equation @{text "X = rhs"} |
89 | 527 |
and substitutes @{text X} throughout the rest of the equational system |
110 | 528 |
adjusting the remaining regular expressions appropriately. To define this adjustment |
108 | 529 |
we define the \emph{append-operation} taking a term and a regular expression as argument |
89 | 530 |
|
531 |
\begin{center} |
|
92 | 532 |
@{thm append_rexp.simps(2)[where X="Y" and r="r\<^isub>1" and rexp="r\<^isub>2", THEN eq_reflection]}\hspace{10mm} |
533 |
@{thm append_rexp.simps(1)[where r="r\<^isub>1" and rexp="r\<^isub>2", THEN eq_reflection]} |
|
89 | 534 |
\end{center} |
535 |
||
92 | 536 |
\noindent |
108 | 537 |
We lift this operation to entire right-hand sides of equations, written as |
93 | 538 |
@{thm (lhs) append_rhs_rexp_def[where rexp="r"]}. With this we can define |
101 | 539 |
the \emph{arden-operation} for an equation of the form @{text "X = rhs"} as: |
110 | 540 |
% |
541 |
\begin{equation}\label{arden_def} |
|
542 |
\mbox{\begin{tabular}{rc@ {\hspace{2mm}}r@ {\hspace{1mm}}l} |
|
94 | 543 |
@{thm (lhs) Arden_def} & @{text "\<equiv>"}~~\mbox{} & \multicolumn{2}{@ {\hspace{-2mm}}l}{@{text "let"}}\\ |
544 |
& & @{text "rhs' ="} & @{term "rhs - {Trn X r | r. Trn X r \<in> rhs}"} \\ |
|
545 |
& & @{text "r' ="} & @{term "STAR (\<Uplus> {r. Trn X r \<in> rhs})"}\\ |
|
546 |
& & \multicolumn{2}{@ {\hspace{-2mm}}l}{@{text "in"}~~@{term "append_rhs_rexp rhs' r'"}}\\ |
|
110 | 547 |
\end{tabular}} |
548 |
\end{equation} |
|
93 | 549 |
|
550 |
\noindent |
|
101 | 551 |
In this definition, we first delete all terms of the form @{text "(X, r)"} from @{text rhs}; |
110 | 552 |
then we calculate the combined regular expressions for all @{text r} coming |
94 | 553 |
from the deleted @{text "(X, r)"}, and take the @{const STAR} of it; |
554 |
finally we append this regular expression to @{text rhs'}. It can be easily seen |
|
110 | 555 |
that this operation mimics Arden's lemma on the level of equations. To ensure |
556 |
the non-emptiness condition of Arden's lemma we say that a right-hand side is |
|
557 |
\emph{ardenable} provided |
|
558 |
||
559 |
\begin{center} |
|
560 |
@{thm ardenable_def} |
|
561 |
\end{center} |
|
562 |
||
563 |
\noindent |
|
564 |
This allows us to prove |
|
565 |
||
566 |
\begin{lemma}\label{ardenable} |
|
567 |
If @{text "X = \<Union>\<calL> ` rhs"}, |
|
568 |
@{thm (prem 2) Arden_keeps_eq} and |
|
569 |
@{thm (prem 3) Arden_keeps_eq}, then |
|
570 |
@{text "X = \<Union>\<calL> ` (Arden X rhs)"} |
|
571 |
\end{lemma} |
|
572 |
||
573 |
\noindent |
|
95 | 574 |
The \emph{substituion-operation} takes an equation |
575 |
of the form @{text "X = xrhs"} and substitutes it into the right-hand side @{text rhs}. |
|
94 | 576 |
|
577 |
\begin{center} |
|
95 | 578 |
\begin{tabular}{rc@ {\hspace{2mm}}r@ {\hspace{1mm}}l} |
579 |
@{thm (lhs) Subst_def} & @{text "\<equiv>"}~~\mbox{} & \multicolumn{2}{@ {\hspace{-2mm}}l}{@{text "let"}}\\ |
|
580 |
& & @{text "rhs' ="} & @{term "rhs - {Trn X r | r. Trn X r \<in> rhs}"} \\ |
|
581 |
& & @{text "r' ="} & @{term "\<Uplus> {r. Trn X r \<in> rhs}"}\\ |
|
582 |
& & \multicolumn{2}{@ {\hspace{-2mm}}l}{@{text "in"}~~@{term "rhs' \<union> append_rhs_rexp xrhs r'"}}\\ |
|
583 |
\end{tabular} |
|
94 | 584 |
\end{center} |
95 | 585 |
|
586 |
\noindent |
|
110 | 587 |
We again delete first all occurrence of @{text "(X, r)"} in @{text rhs}; we then calculate |
95 | 588 |
the regular expression corresponding to the deleted terms; finally we append this |
589 |
regular expression to @{text "xrhs"} and union it up with @{text rhs'}. When we use |
|
590 |
the substitution operation we will arrange it so that @{text "xrhs"} does not contain |
|
110 | 591 |
any occurrence of @{text X}. |
96 | 592 |
|
100 | 593 |
With these two operation in place, we can define the operation that removes one equation |
594 |
from an equational systems @{text ES}. The operation @{const Subst_all} |
|
96 | 595 |
substitutes an equation @{text "X = xrhs"} throughout an equational system @{text ES}; |
100 | 596 |
@{const Remove} then completely removes such an equation from @{text ES} by substituting |
110 | 597 |
it to the rest of the equational system, but first eliminating all recursive occurrences |
96 | 598 |
of @{text X} by applying @{const Arden} to @{text "xrhs"}. |
599 |
||
600 |
\begin{center} |
|
601 |
\begin{tabular}{rcl} |
|
602 |
@{thm (lhs) Subst_all_def} & @{text "\<equiv>"} & @{thm (rhs) Subst_all_def}\\ |
|
603 |
@{thm (lhs) Remove_def} & @{text "\<equiv>"} & @{thm (rhs) Remove_def} |
|
604 |
\end{tabular} |
|
605 |
\end{center} |
|
100 | 606 |
|
607 |
\noindent |
|
110 | 608 |
Finally, we can define how an equational system should be solved. For this |
107 | 609 |
we will need to iterate the process of eliminating equations until only one equation |
100 | 610 |
will be left in the system. However, we not just want to have any equation |
107 | 611 |
as being the last one, but the one involving the equivalence class for |
612 |
which we want to calculate the regular |
|
108 | 613 |
expression. Let us suppose this equivalence class is @{text X}. |
107 | 614 |
Since @{text X} is the one to be solved, in every iteration step we have to pick an |
108 | 615 |
equation to be eliminated that is different from @{text X}. In this way |
616 |
@{text X} is kept to the final step. The choice is implemented using Hilbert's choice |
|
107 | 617 |
operator, written @{text SOME} in the definition below. |
100 | 618 |
|
619 |
\begin{center} |
|
620 |
\begin{tabular}{rc@ {\hspace{4mm}}r@ {\hspace{1mm}}l} |
|
621 |
@{thm (lhs) Iter_def} & @{text "\<equiv>"}~~\mbox{} & \multicolumn{2}{@ {\hspace{-4mm}}l}{@{text "let"}}\\ |
|
622 |
& & @{text "(Y, yrhs) ="} & @{term "SOME (Y, yrhs). (Y, yrhs) \<in> ES \<and> X \<noteq> Y"} \\ |
|
623 |
& & \multicolumn{2}{@ {\hspace{-4mm}}l}{@{text "in"}~~@{term "Remove ES Y yrhs"}}\\ |
|
624 |
\end{tabular} |
|
625 |
\end{center} |
|
626 |
||
627 |
\noindent |
|
110 | 628 |
The last definition we need applies @{term Iter} over and over until a condition |
629 |
@{text Cond} is \emph{not} satisfied anymore. The condition states that there |
|
630 |
are more than one equation left in the equational system @{text ES}. To solve |
|
631 |
an equational system we use Isabelle/HOL's @{text while}-operator as follows: |
|
101 | 632 |
|
100 | 633 |
\begin{center} |
634 |
@{thm Solve_def} |
|
635 |
\end{center} |
|
636 |
||
101 | 637 |
\noindent |
103 | 638 |
We are not concerned here with the definition of this operator |
639 |
(see \cite{BerghoferNipkow00}), but note that we eliminate |
|
640 |
in each @{const Iter}-step a single equation, and therefore |
|
641 |
have a well-founded termination order by taking the cardinality |
|
642 |
of the equational system @{text ES}. This enables us to prove |
|
104 | 643 |
properties about our definition of @{const Solve} when we ``call'' it with |
644 |
the equivalence class @{text X} and the initial equational system |
|
645 |
@{term "Init (UNIV // \<approx>A)"} from |
|
108 | 646 |
\eqref{initcs} using the principle: |
110 | 647 |
% |
648 |
\begin{equation}\label{whileprinciple} |
|
649 |
\mbox{\begin{tabular}{l} |
|
103 | 650 |
@{term "invariant (Init (UNIV // \<approx>A))"} \\ |
651 |
@{term "\<forall>ES. invariant ES \<and> Cond ES \<longrightarrow> invariant (Iter X ES)"}\\ |
|
652 |
@{term "\<forall>ES. invariant ES \<and> Cond ES \<longrightarrow> card (Iter X ES) < card ES"}\\ |
|
653 |
@{term "\<forall>ES. invariant ES \<and> \<not> Cond ES \<longrightarrow> P ES"}\\ |
|
654 |
\hline |
|
655 |
\multicolumn{1}{c}{@{term "P (Solve X (Init (UNIV // \<approx>A)))"}} |
|
110 | 656 |
\end{tabular}} |
657 |
\end{equation} |
|
103 | 658 |
|
659 |
\noindent |
|
104 | 660 |
This principle states that given an invariant (which we will specify below) |
661 |
we can prove a property |
|
662 |
@{text "P"} involving @{const Solve}. For this we have to discharge the following |
|
663 |
proof obligations: first the |
|
103 | 664 |
initial equational system satisfies the invariant; second that the iteration |
104 | 665 |
step @{text "Iter"} preserves the the invariant as long as the condition @{term Cond} holds; |
103 | 666 |
third that @{text "Iter"} decreases the termination order, and fourth that |
104 | 667 |
once the condition does not hold anymore then the property @{text P} must hold. |
103 | 668 |
|
104 | 669 |
The property @{term P} in our proof will state that @{term "Solve X (Init (UNIV // \<approx>A))"} |
108 | 670 |
returns with a single equation @{text "X = xrhs"} for some @{text "xrhs"}, and |
104 | 671 |
that this equational system still satisfies the invariant. In order to get |
672 |
the proof through, the invariant is composed of the following six properties: |
|
103 | 673 |
|
674 |
\begin{center} |
|
104 | 675 |
\begin{tabular}{@ {}rcl@ {\hspace{-13mm}}l @ {}} |
676 |
@{text "invariant ES"} & @{text "\<equiv>"} & |
|
103 | 677 |
@{term "finite ES"} & @{text "(finiteness)"}\\ |
678 |
& @{text "\<and>"} & @{thm (rhs) finite_rhs_def} & @{text "(finiteness rhs)"}\\ |
|
104 | 679 |
& @{text "\<and>"} & @{text "\<forall>(X, rhs)\<in>ES. X = \<Union>\<calL> ` rhs"} & @{text "(soundness)"}\\ |
680 |
& @{text "\<and>"} & @{thm (rhs) distinct_equas_def}\\ |
|
681 |
& & & @{text "(distinctness)"}\\ |
|
110 | 682 |
& @{text "\<and>"} & @{thm (rhs) ardenable_all_def} & @{text "(ardenable)"}\\ |
104 | 683 |
& @{text "\<and>"} & @{thm (rhs) valid_eqs_def} & @{text "(validity)"}\\ |
103 | 684 |
\end{tabular} |
685 |
\end{center} |
|
686 |
||
104 | 687 |
\noindent |
688 |
The first two ensure that the equational system is always finite (number of equations |
|
108 | 689 |
and number of terms in each equation); the second makes sure the ``meaning'' of the |
690 |
equations is preserved under our transformations. The other properties are a bit more |
|
691 |
technical, but are needed to get our proof through. Distinctness states that every |
|
110 | 692 |
equation in the system is distinct. Ardenable ensures that we can always |
693 |
apply the arden operation. |
|
108 | 694 |
The last property states that every @{text rhs} can only contain equivalence classes |
695 |
for which there is an equation. Therefore @{text lhss} is just the set containing |
|
696 |
the first components of an equational system, |
|
697 |
while @{text "rhss"} collects all equivalence classes @{text X} in the terms of the |
|
110 | 698 |
form @{term "Trn X r"}. That means @{thm (lhs) lhss_def}~@{text "\<equiv> {X | (X, rhs) \<in> ES}"} |
699 |
and @{thm (lhs) rhss_def}~@{text "\<equiv> {X | (X, r) \<in> rhs}"}. |
|
108 | 700 |
|
104 | 701 |
|
110 | 702 |
It is straightforward to prove that the initial equational system satisfies the |
105 | 703 |
invariant. |
704 |
||
110 | 705 |
\begin{lemma}\label{invzero} |
104 | 706 |
@{thm[mode=IfThen] Init_ES_satisfies_invariant} |
707 |
\end{lemma} |
|
708 |
||
105 | 709 |
\begin{proof} |
710 |
Finiteness is given by the assumption and the way how we set up the |
|
711 |
initial equational system. Soundness is proved in Lem.~\ref{inv}. Distinctness |
|
712 |
follows from the fact that the equivalence classes are disjoint. The ardenable |
|
110 | 713 |
property also follows from the setup of the equational system, as does |
105 | 714 |
validity.\qed |
715 |
\end{proof} |
|
716 |
||
110 | 717 |
\begin{lemma}\label{iterone} |
104 | 718 |
@{thm[mode=IfThen] iteration_step_invariant[where xrhs="rhs"]} |
719 |
\end{lemma} |
|
720 |
||
107 | 721 |
\begin{proof} |
110 | 722 |
This boils down to choosing an equation @{text "Y = yrhs"} to be eliminated |
723 |
and to show that @{term "Subst_all (ES - {(Y, yrhs)}) Y (Arden Y yrhs)"} |
|
724 |
preserves the invariant. |
|
725 |
We prove this as follows: |
|
726 |
||
727 |
\begin{center} |
|
728 |
@{text "\<forall> ES."} @{thm (prem 1) Subst_all_satisfies_invariant} implies |
|
729 |
@{thm (concl) Subst_all_satisfies_invariant} |
|
730 |
\end{center} |
|
731 |
||
732 |
\noindent |
|
733 |
Finiteness is straightforward, as @{const Subst} and @{const Arden} operations |
|
734 |
keep the equational system finite. These operation also preserve soundness |
|
735 |
distinctness (we proved soundness for @{const Arden} in Lem.~\ref{ardenable}). |
|
736 |
The property Ardenable is clearly preserved because the append-operation |
|
737 |
cannot make a regular expression to match the empty string. Validity is |
|
738 |
given because @{const Arden} removes an equivalence class from @{text yrhs} |
|
739 |
and then @{const Subst_all} removes @{text Y} from the equational system. |
|
740 |
Having proved the implication above, we can replace @{text "ES"} with @{text "ES - {(Y, yrhs)}"} |
|
741 |
which matches with our proof-obligation of @{const "Subst_all"}. Since |
|
742 |
\mbox{@{term "ES = ES - {(Y, yrhs)} \<union> {(Y, yrhs)}"}}, we can use our assumption |
|
743 |
to complete the proof.\qed |
|
107 | 744 |
\end{proof} |
745 |
||
110 | 746 |
\begin{lemma}\label{itertwo} |
104 | 747 |
@{thm[mode=IfThen] iteration_step_measure[simplified (no_asm), where xrhs="rhs"]} |
748 |
\end{lemma} |
|
749 |
||
105 | 750 |
\begin{proof} |
751 |
By assumption we know that @{text "ES"} is finite and has more than one element. |
|
752 |
Therefore there must be an element @{term "(Y, yrhs) \<in> ES"} with |
|
110 | 753 |
@{term "(Y, yrhs) \<noteq> (X, rhs)"}. Using the distinctness property we can infer |
105 | 754 |
that @{term "Y \<noteq> X"}. We further know that @{text "Remove ES Y yrhs"} |
755 |
removes the equation @{text "Y = yrhs"} from the system, and therefore |
|
756 |
the cardinality of @{const Iter} strictly decreases.\qed |
|
757 |
\end{proof} |
|
758 |
||
104 | 759 |
\begin{lemma} |
760 |
If @{thm (prem 1) Solve} and @{thm (prem 2) Solve} then there exists |
|
761 |
a @{text rhs} such that @{term "Solve X (Init (UNIV // \<approx>A)) = {(X, rhs)}"} |
|
762 |
and @{term "invariant {(X, rhs)}"}. |
|
763 |
\end{lemma} |
|
764 |
||
107 | 765 |
\begin{proof} |
110 | 766 |
In order to prove this lemma using \eqref{whileprinciple}, we have to use a slightly |
767 |
stronger invariant since Lem.~\ref{iterone} and \ref{itertwo} have the precondition |
|
768 |
that @{term "(X, rhs) \<in> ES"} for some @{text rhs}. This precondition is needed |
|
769 |
in order to choose in the @{const Iter}-step an equation that is not \mbox{@{term "X = rhs"}}. |
|
770 |
Therefore our invariant is cannot be just @{term "invariant ES"}, but must be |
|
771 |
@{term "invariant ES \<and> (\<exists>rhs. (X, rhs) \<in> ES)"}. By assumption |
|
772 |
@{thm (prem 2) Solve} and Lem.~\ref{invzero}, the more general invariant holds for |
|
773 |
the initial equational system. This is premise 1 of~\eqref{whileprinciple}. |
|
774 |
Premise 2 is given by Lem.~\ref{iterone} and the fact that @{const Iter} might |
|
775 |
modify the @{text rhs} in the equation @{term "X = rhs"}, but does not remove it. |
|
776 |
Premise 3 of~\eqref{whileprinciple} is by Lem.~\ref{itertwo}. Now in premise 4 |
|
777 |
we like to show that there exists a @{text rhs} such that @{term "ES = {(X, rhs)}"} |
|
778 |
and that @{text "invariant {(X, rhs)}"} holds, provided the condition @{text "Cond"} |
|
779 |
does not hols. By the stronger invariant we know there exists such a @{text "rhs"} |
|
780 |
with @{term "(X, rhs) \<in> ES"}. Because @{text Cond} is not true, we know the cardinality |
|
781 |
of @{text ES} is @{text 1}. This means @{text "ES"} must actually be the equation @{text "X = rhs"}, |
|
782 |
for which the invariant holds. This allows us to conclude that |
|
783 |
@{term "Solve X (Init (UNIV // \<approx>A)) = {(X, rhs)}"} and @{term "invariant {(X, rhs)}"} hold.\qed |
|
107 | 784 |
\end{proof} |
785 |
||
106 | 786 |
\noindent |
787 |
With this lemma in place we can show that for every equivalence class in @{term "UNIV // \<approx>A"} |
|
788 |
there exists a regular expression. |
|
789 |
||
105 | 790 |
\begin{lemma}\label{every_eqcl_has_reg} |
791 |
@{thm[mode=IfThen] every_eqcl_has_reg} |
|
792 |
\end{lemma} |
|
793 |
||
794 |
\begin{proof} |
|
795 |
By the preceeding Lemma, we know that there exists a @{text "rhs"} such |
|
796 |
that @{term "Solve X (Init (UNIV // \<approx>A))"} returns the equation @{text "X = rhs"}, |
|
797 |
and that the invariant holds for this equation. That means we |
|
798 |
know @{text "X = \<Union>\<calL> ` rhs"}. We further know that |
|
109 | 799 |
this is equal to \mbox{@{text "\<Union>\<calL> ` (Arden X rhs)"}} using the properties of the |
110 | 800 |
invariant and Lem.\ref{ardenable}. Using the validity property for the equation @{text "X = rhs"}, |
106 | 801 |
we can infer that @{term "rhss rhs \<subseteq> {X}"} and because the arden operation |
802 |
removes that @{text X} from @{text rhs}, that @{term "rhss (Arden X rhs) = {}"}. |
|
803 |
That means @{term "Arden X rhs"} can only consist of terms of the form @{term "Lam r"}. |
|
804 |
So we can collect those (finitely many) regular expressions and have @{term "X = L (\<Uplus>rs)"}. |
|
805 |
With this we can conclude the proof.\qed |
|
105 | 806 |
\end{proof} |
807 |
||
106 | 808 |
\noindent |
809 |
Lem.~\ref{every_eqcl_has_reg} allows us to finally give a proof for the first direction |
|
810 |
of the Myhill-Nerode theorem. |
|
105 | 811 |
|
106 | 812 |
\begin{proof}[of Thm.~\ref{myhillnerodeone}] |
105 | 813 |
By Lem.~\ref{every_eqcl_has_reg} we know that there exists a regular language for |
814 |
every equivalence class in @{term "UNIV // \<approx>A"}. Since @{text "finals A"} is |
|
110 | 815 |
a subset of @{term "UNIV // \<approx>A"}, we also know that for every equivalence class |
105 | 816 |
in @{term "finals A"} there exists a regular language. Moreover by assumption |
106 | 817 |
we know that @{term "finals A"} must be finite, and therefore there must be a finite |
105 | 818 |
set of regular expressions @{text "rs"} such that |
819 |
||
820 |
\begin{center} |
|
821 |
@{term "\<Union>(finals A) = L (\<Uplus>rs)"} |
|
822 |
\end{center} |
|
823 |
||
824 |
\noindent |
|
825 |
Since the left-hand side is equal to @{text A}, we can use @{term "\<Uplus>rs"} |
|
107 | 826 |
as the regular expression that is needed in the theorem.\qed |
105 | 827 |
\end{proof} |
54 | 828 |
*} |
829 |
||
100 | 830 |
|
831 |
||
832 |
||
833 |
section {* Myhill-Nerode, Second Part *} |
|
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
834 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
835 |
text {* |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
836 |
|
54 | 837 |
\begin{theorem} |
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
838 |
Given @{text "r"} is a regular expressions, then @{thm rexp_imp_finite}. |
54 | 839 |
\end{theorem} |
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
840 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
841 |
\begin{proof} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
842 |
By induction on the structure of @{text r}. The cases for @{const NULL}, @{const EMPTY} |
50 | 843 |
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
|
844 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
845 |
\begin{center} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
846 |
\begin{tabular}{l} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
847 |
@{thm quot_null_eq}\\ |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
848 |
@{thm quot_empty_subset}\\ |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
849 |
@{thm quot_char_subset} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
850 |
\end{tabular} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
851 |
\end{center} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
852 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
853 |
\end{proof} |
109 | 854 |
|
855 |
||
856 |
@{thm tag_str_ALT_def[where ?L1.0="A" and ?L2.0="B"]} |
|
857 |
||
858 |
@{thm tag_str_SEQ_def[where ?L1.0="A" and ?L2.0="B"]} |
|
859 |
||
860 |
@{thm tag_str_STAR_def[where ?L1.0="A"]} |
|
39
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
861 |
*} |
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
862 |
|
a59473f0229d
tuned a little bit the section about finite partitions
urbanc
parents:
37
diff
changeset
|
863 |
|
54 | 864 |
section {* Conclusion and Related Work *} |
865 |
||
92 | 866 |
text {* |
109 | 867 |
In this paper we took the view that a regular language is one where there exists |
111 | 868 |
a regular expression that matches all its strings. For us it was ineteresting to find |
92 | 869 |
out how far we can push this point of view. Having formalised the Myhill-Nerode |
109 | 870 |
theorem means pushed quite far. Having the Myhill-Nerode theorem means we can |
92 | 871 |
formalise much of the textbook results in this subject. |
872 |
||
111 | 873 |
Our proof of the first direction is very much inspired by \emph{Brz |
874 |
algebraic mehod} used to convert a finite atomaton to a regular |
|
875 |
expression. The close connection can be seen by considering the equivalence |
|
876 |
classes as the states of the minimal automaton for the regular language. |
|
877 |
However there are some subtle differences. If we identify equivalence |
|
878 |
classes with the states of the automaton, then the most natural choice is to |
|
879 |
characterise each state with the set of strings starting from the initial |
|
880 |
state leading up to that state. Usually the states are characterised as the |
|
881 |
ones starting from that state leading to the terminal states. The first |
|
882 |
choice has consequences how the initial equational system is set up. We have |
|
883 |
the $\lambda$-term on our ``initial state'', while Brz has it on the |
|
884 |
terminal states. This means we also need to reverse the direction of Arden's |
|
885 |
lemma. |
|
92 | 886 |
|
111 | 887 |
We briefly considered using the method Brz presented in the Appendix of ??? |
888 |
in order to prove the second direction of the Myhill-Nerode thereom. There |
|
889 |
he calculates the derivatives for regular expressions and shows that there |
|
890 |
can be only finitely many of them. We could use as the tag of a string |
|
891 |
@{text s} the derivative of a regular expression generated with respect to |
|
892 |
@{text s}. Using the fact that two strings are Myhill-Nerode related |
|
893 |
whenever their derivative is the same together with the fact that there are |
|
894 |
only finitely many derivatives for a regular expression would give us the |
|
895 |
same argument. However it seems not so easy to calculate the derivatives |
|
896 |
and then to count them. Therefore we preferred our direct method of |
|
897 |
using tagging-functions involving equivalence classes. This is also where |
|
898 |
our method shines, because we can completely side-step the standard |
|
899 |
argument \cite{Kozen97} where automata need to be composed, which is not so |
|
900 |
convenient to formalise in a HOL-based theorem prover. |
|
901 |
||
902 |
||
903 |
Lines of code / nuprl |
|
904 |
||
905 |
closure properties |
|
906 |
||
92 | 907 |
*} |
908 |
||
909 |
||
24 | 910 |
(*<*) |
911 |
end |
|
912 |
(*>*) |