198 text {* |
198 text {* |
199 Strings in Isabelle/HOL are lists of characters with the \emph{empty string} |
199 Strings in Isabelle/HOL are lists of characters with the \emph{empty string} |
200 being represented by the empty list, written @{term "[]"}. \emph{Languages} |
200 being represented by the empty list, written @{term "[]"}. \emph{Languages} |
201 are sets of strings. The language containing all strings is written in |
201 are sets of strings. The language containing all strings is written in |
202 Isabelle/HOL as @{term "UNIV::string set"}. The concatenation of two languages |
202 Isabelle/HOL as @{term "UNIV::string set"}. The concatenation of two languages |
203 is written @{term "A ;; B"} and a language raised to the power $n$ is written |
203 is written @{term "A ;; B"} and a language raised to the power @{text n} is written |
204 @{term "A \<up> n"}. Their definitions are |
204 @{term "A \<up> n"}. Their definitions are |
205 |
205 |
206 \begin{center} |
206 \begin{center} |
207 @{thm Seq_def[THEN eq_reflection, where A1="A" and B1="B"]} |
207 @{thm Seq_def[THEN eq_reflection, where A1="A" and B1="B"]} |
208 \hspace{7mm} |
208 \hspace{7mm} |
226 \noindent |
226 \noindent |
227 We omit the proofs, but invite the reader to consult |
227 We omit the proofs, but invite the reader to consult |
228 our formalisation.\footnote{Available at ???} |
228 our formalisation.\footnote{Available at ???} |
229 |
229 |
230 |
230 |
231 The notation for the quotient of a language @{text A} according to an |
231 The notation in Isabelle/HOL for the quotient of a language @{text A} according to an |
232 equivalence relation @{term REL} is in Isabelle/HOL @{term "A // REL"}. We will write |
232 equivalence relation @{term REL} is @{term "A // REL"}. We will write |
233 @{text "\<lbrakk>x\<rbrakk>\<^isub>\<approx>"} for the equivalence class defined |
233 @{text "\<lbrakk>x\<rbrakk>\<^isub>\<approx>"} for the equivalence class defined |
234 as @{text "{y | y \<approx> x}"}. |
234 as @{text "{y | y \<approx> x}"}. |
235 |
235 |
236 |
236 |
237 Central to our proof will be the solution of equational systems |
237 Central to our proof will be the solution of equational systems |
238 involving sets of languages. For this we will use Arden's lemma \cite{Brzozowski64} |
238 involving sets of languages. For this we will use Arden's lemma \cite{Brzozowski64} |
239 which solves equations of the form @{term "X = A ;; X \<union> B"} provided |
239 which solves equations of the form @{term "X = A ;; X \<union> B"} in case |
240 @{term "[] \<notin> A"}. However we will need the following ``reverse'' |
240 @{term "[] \<notin> A"}. However we will need the following ``reverse'' |
241 version of Arden's lemma. |
241 version of Arden's lemma. |
242 |
242 |
243 \begin{lemma}[Reverse Arden's Lemma]\label{arden}\mbox{}\\ |
243 \begin{lemma}[Reverse Arden's Lemma]\label{arden}\mbox{}\\ |
244 If @{thm (prem 1) arden} then |
244 If @{thm (prem 1) arden} then |
309 @{thm (rhs) L_rexp.simps(6)[where r="r"]}\\ |
309 @{thm (rhs) L_rexp.simps(6)[where r="r"]}\\ |
310 \end{tabular} |
310 \end{tabular} |
311 \end{tabular} |
311 \end{tabular} |
312 \end{center} |
312 \end{center} |
313 |
313 |
314 \noindent |
314 Given a set of regular expressions @{text rs}, we will need the operation of generating |
315 Given a set or regular expressions @{text rs}, we will need the operation of generating |
315 a corresponding regular expressions that matches all languages of @{text rs}. We only need the existence |
316 a regular expressions that matches all languages of @{text rs}. We only need the existence |
316 of such a regular expressions and therefore we use Isabelle's @{const "fold_graph"} and Hilbert's |
317 of such an regular expressions therefore we use Isabelle's @{const "fold_graph"} and Hilbert's |
317 @{text "\<epsilon>"} to define @{term "\<Uplus>rs"}. This function, roughly speaking, folds @{const ALT} over the |
318 @{text "\<epsilon>"} to define @{term "\<Uplus>rs"} which, roughly speaking, folds @{const ALT} over the |
|
319 set @{text rs} with @{const NULL} for the empty set. We can prove that for finite sets @{text rs} |
318 set @{text rs} with @{const NULL} for the empty set. We can prove that for finite sets @{text rs} |
320 |
319 |
321 \begin{center} |
320 \begin{center} |
322 @{thm (lhs) folds_alt_simp}@{text "= \<Union> (\<calL> ` rs)"} |
321 @{thm (lhs) folds_alt_simp}@{text "= \<Union> (\<calL> ` rs)"} |
323 \end{center} |
322 \end{center} |
324 |
323 |
325 \noindent |
324 \noindent |
326 holds. (whereby @{text "\<calL> ` rs"} stands for the |
325 holds, whereby @{text "\<calL> ` rs"} stands for the |
327 image of the set @{text rs} under function @{text "\<calL>"}). |
326 image of the set @{text rs} under function @{text "\<calL>"}. |
328 |
327 |
329 |
328 |
330 *} |
329 *} |
331 |
330 |
332 section {* Finite Partitions Imply Regularity of a Language *} |
331 section {* Finite Partitions Imply Regularity of a Language *} |
342 \end{definition} |
341 \end{definition} |
343 |
342 |
344 \noindent |
343 \noindent |
345 It is easy to see that @{term "\<approx>A"} is an equivalence relation, which |
344 It is easy to see that @{term "\<approx>A"} is an equivalence relation, which |
346 partitions the set of all strings, @{text "UNIV"}, into a set of disjoint |
345 partitions the set of all strings, @{text "UNIV"}, into a set of disjoint |
347 equivalence classes. One direction of the Myhill-Nerode theorem establishes |
346 equivalence classes. An example is the regular language containing just |
|
347 the string @{text "[c]"}, then @{term "\<approx>({[c]})"} partitions @{text UNIV} |
|
348 into the three equivalence classes @{text "X\<^isub>1"}, @{text "X\<^isub>2"} and @{text "X\<^isub>3"} |
|
349 as follows |
|
350 |
|
351 \begin{center} |
|
352 @{text "X\<^isub>1 = {[]}"}\hspace{5mm} |
|
353 @{text "X\<^isub>2 = {[c]}"}\hspace{5mm} |
|
354 @{text "X\<^isub>3 = UNIV - {[], [c]}"} |
|
355 \end{center} |
|
356 |
|
357 One direction of the Myhill-Nerode theorem establishes |
348 that if there are finitely many equivalence classes, then the language is |
358 that if there are finitely many equivalence classes, then the language is |
349 regular. In our setting we therefore have to show: |
359 regular. In our setting we therefore have to show: |
350 |
360 |
351 \begin{theorem}\label{myhillnerodeone} |
361 \begin{theorem}\label{myhillnerodeone} |
352 @{thm[mode=IfThen] hard_direction} |
362 @{thm[mode=IfThen] hard_direction} |
353 \end{theorem} |
363 \end{theorem} |
354 |
364 |
355 \noindent |
365 \noindent |
356 To prove this theorem, we define the set @{term "finals A"} as those equivalence |
366 To prove this theorem, we first define the set @{term "finals A"} as those equivalence |
357 classes that contain strings of @{text A}, namely |
367 classes that contain strings of @{text A}, namely |
358 % |
368 % |
359 \begin{equation} |
369 \begin{equation} |
360 @{thm finals_def} |
370 @{thm finals_def} |
361 \end{equation} |
371 \end{equation} |
362 |
372 |
363 \noindent |
373 \noindent |
364 It is straightforward to show that @{thm lang_is_union_of_finals} and |
374 In our running example, @{text "X\<^isub>1"} is the only equivalence class in @{term "finals {[c]}"}. |
|
375 It is straightforward to show that in general @{thm lang_is_union_of_finals} and |
365 @{thm finals_in_partitions} hold. |
376 @{thm finals_in_partitions} hold. |
366 Therefore if we know that there exists a regular expression for every |
377 Therefore if we know that there exists a regular expression for every |
367 equivalence class in @{term "finals A"} (which by assumption must be |
378 equivalence class in @{term "finals A"} (which by assumption must be |
368 a finite set), then we can combine these regular expressions with @{const ALT} |
379 a finite set), then we can obtain a regular expression using @{text "\<bigplus>"} |
369 and obtain a regular expression that matches every string in @{text A}. |
380 that matches every string in @{text A}. |
370 |
381 |
371 |
382 |
372 We prove Thm.~\ref{myhillnerodeone} by giving a method that can calculate a |
383 Our proof of Thm.~\ref{myhillnerodeone} relies on a method that can calculate a |
373 regular expression for \emph{every} equivalence class, not just the ones |
384 regular expression for \emph{every} equivalence class, not just the ones |
374 in @{term "finals A"}. We |
385 in @{term "finals A"}. We |
375 first define a notion of \emph{transition} between equivalence classes |
386 first define the notion of \emph{transition} between equivalence classes |
376 % |
387 % |
377 \begin{equation} |
388 \begin{equation} |
378 @{thm transition_def} |
389 @{thm transition_def} |
379 \end{equation} |
390 \end{equation} |
380 |
391 |
381 \noindent |
392 \noindent |
382 which means that if we concatenate all strings matching the regular expression @{text r} |
393 which means that if we concatenate all strings matching the regular expression @{text r} |
383 to the end of all strings in the equivalence class @{text Y}, we obtain a subset of |
394 to the end of all strings in the equivalence class @{text Y}, we obtain a subset of |
384 @{text X}. Note that we do not define an automaton here, we merely relate two sets |
395 @{text X}. Note that we do not define an automaton here, we merely relate two sets |
385 (w.r.t.~a regular expression). |
396 (with the help of a regular expression). In our concrete example we have |
|
397 @{term "X\<^isub>1 \<Turnstile>(CHAR c)\<Rightarrow> X\<^isub>2"} and @{term "X\<^isub>1 \<Turnstile>r\<Rightarrow> X\<^isub>3"} with @{text r} being any |
|
398 other regular expression than @{const EMPTY} and @{term "CHAR c"}. |
386 |
399 |
387 Next we build an equational system that |
400 Next we build an equational system that |
388 contains an equation for each equivalence class. Suppose we have |
401 contains an equation for each equivalence class. Suppose we have |
389 the equivalence classes @{text "X\<^isub>1,\<dots>,X\<^isub>n"}, there must be one and only one that |
402 the equivalence classes @{text "X\<^isub>1,\<dots>,X\<^isub>n"}, there must be one and only one that |
390 contains the empty string @{text "[]"} (since equivalence classes are disjoint). |
403 contains the empty string @{text "[]"} (since equivalence classes are disjoint). |
402 \noindent |
415 \noindent |
403 where the pairs @{text "(Y\<^isub>i\<^isub>j, CHAR c\<^isub>i\<^isub>j)"} stand for all transitions |
416 where the pairs @{text "(Y\<^isub>i\<^isub>j, CHAR c\<^isub>i\<^isub>j)"} stand for all transitions |
404 @{term "Y\<^isub>i\<^isub>j \<Turnstile>(CHAR c\<^isub>i\<^isub>j)\<Rightarrow> X\<^isub>i"}. The term @{text "\<lambda>(EMPTY)"} acts as a marker for the equivalence |
417 @{term "Y\<^isub>i\<^isub>j \<Turnstile>(CHAR c\<^isub>i\<^isub>j)\<Rightarrow> X\<^isub>i"}. The term @{text "\<lambda>(EMPTY)"} acts as a marker for the equivalence |
405 class containing @{text "[]"}. (Note that we mark, roughly speaking, the |
418 class containing @{text "[]"}. (Note that we mark, roughly speaking, the |
406 single ``initial'' state in the equational system, which is different from |
419 single ``initial'' state in the equational system, which is different from |
407 the method by Brzozowski \cite{Brzozowski64}, since for his purposes he needs to mark |
420 the method by Brzozowski \cite{Brzozowski64}, since he marks the ``terminal'' |
408 the ``terminal'' states.) Overloading the function @{text L} for the two kinds of terms in the |
421 states. We are forced to set up the equational system in this way, because |
|
422 the Myhill-Nerode relation determines the ``direction'' of the transitions. |
|
423 The successor ``state'' of an equivalence class @{text Y} can be reached by adding |
|
424 characters to the end of @{text Y}. This is also the reason why we have to use |
|
425 our reverse version of Arden's lemma.) |
|
426 Overloading the function @{text L} for the two kinds of terms in the |
409 equational system as follows |
427 equational system as follows |
410 |
428 |
411 \begin{center} |
429 \begin{center} |
412 @{thm L_rhs_item.simps(2)[where X="Y" and r="r", THEN eq_reflection]}\hspace{10mm} |
430 @{thm L_rhs_item.simps(2)[where X="Y" and r="r", THEN eq_reflection]}\hspace{10mm} |
413 @{thm L_rhs_item.simps(1)[where r="r", THEN eq_reflection]} |
431 @{thm L_rhs_item.simps(1)[where r="r", THEN eq_reflection]} |