Journal/Paper.thy
changeset 245 40b8d485ce8d
parent 242 093e45c44d91
child 247 087e6c255e33
equal deleted inserted replaced
244:a9598a206c41 245:40b8d485ce8d
   380   lexing. Berghofer and Reiter \cite{BerghoferReiter09} formalise automata
   380   lexing. Berghofer and Reiter \cite{BerghoferReiter09} formalise automata
   381   working over bit strings in the context of Presburger arithmetic.  The only
   381   working over bit strings in the context of Presburger arithmetic.  The only
   382   larger formalisations of automata theory are carried out in Nuprl
   382   larger formalisations of automata theory are carried out in Nuprl
   383   \cite{Constable00} and in Coq, e.g.~\cite{Filliatre97,Almeidaetal10}.
   383   \cite{Constable00} and in Coq, e.g.~\cite{Filliatre97,Almeidaetal10}.
   384 
   384 
   385   Also one might consider that automata are convenient `vehicles' for
   385   Also, one might consider automata as just convenient `vehicles' for
   386   establishing properties about regular languages.  However, paper proofs
   386   establishing properties about regular languages.  However, paper proofs
   387   about automata often involve subtle side-conditions which are easily
   387   about automata often involve subtle side-conditions which are easily
   388   overlooked, but which make formal reasoning rather painful. For example
   388   overlooked, but which make formal reasoning rather painful. For example
   389   Kozen's proof of the Myhill-Nerode theorem requires that automata do not
   389   Kozen's proof of the Myhill-Nerode theorem requires that automata do not
   390   have inaccessible states \cite{Kozen97}. Another subtle side-condition is
   390   have inaccessible states \cite{Kozen97}. Another subtle side-condition is
   395   that if we define a regular language as one for which there exists \emph{a}
   395   that if we define a regular language as one for which there exists \emph{a}
   396   finite automaton that recognises all its strings (see
   396   finite automaton that recognises all its strings (see
   397   Definition~\ref{baddef}), then we need a lemma which ensures that another
   397   Definition~\ref{baddef}), then we need a lemma which ensures that another
   398   equivalent one can be found satisfying the side-condition, and also need to
   398   equivalent one can be found satisfying the side-condition, and also need to
   399   make sure our operations on automata preserve them. Unfortunately, such
   399   make sure our operations on automata preserve them. Unfortunately, such
   400   `little' and `obvious' lemmas make a formalisation of automata theory a
   400   `little' and `obvious' lemmas make formalisations of automata theory 
   401   hair-pulling experience.
   401   hair-pulling experiences.
   402 
   402 
   403   In this paper, we will not attempt to formalise automata theory in
   403   In this paper, we will not attempt to formalise automata theory in
   404   Isabelle/HOL nor will we attempt to formalise automata proofs from the
   404   Isabelle/HOL nor will we attempt to formalise automata proofs from the
   405   literature, but take a different approach to regular languages than is
   405   literature, but take a different approach to regular languages than is
   406   usually taken. Instead of defining a regular language as one where there
   406   usually taken. Instead of defining a regular language as one where there
   424   main purpose of this paper is to show that a central result about regular
   424   main purpose of this paper is to show that a central result about regular
   425   languages---the Myhill-Nerode theorem---can be recreated by only using
   425   languages---the Myhill-Nerode theorem---can be recreated by only using
   426   regular expressions. This theorem gives necessary and sufficient conditions
   426   regular expressions. This theorem gives necessary and sufficient conditions
   427   for when a language is regular. As a corollary of this theorem we can easily
   427   for when a language is regular. As a corollary of this theorem we can easily
   428   establish the usual closure properties, including complementation, for
   428   establish the usual closure properties, including complementation, for
   429   regular languages. We also use in one example the continuation lemma, which
   429   regular languages. We use the continuation lemma \cite{Rosenberg06}, 
   430   is based on Myhill-Nerode, for establishing non-regularity of languages 
   430   which is also a corollary of the Myhill-Nerode theorem, for establishing 
   431   \cite{Rosenberg06}.\medskip
   431   the non-regularity of the language @{text "a\<^isup>nb\<^isup>n"}.\medskip
   432   
   432   
   433   \noindent 
   433   \noindent 
   434   {\bf Contributions:} There is an extensive literature on regular languages.
   434   {\bf Contributions:} There is an extensive literature on regular languages.
   435   To our best knowledge, our proof of the Myhill-Nerode theorem is the first
   435   To our best knowledge, our proof of the Myhill-Nerode theorem is the first
   436   that is based on regular expressions, only. The part of this theorem stating
   436   that is based on regular expressions, only. The part of this theorem stating
   437   that finitely many partitions imply regularity of the language is proved by
   437   that finitely many partitions imply regularity of the language is proved by
   438   an argument about solving equational systems.  This argument appears to be
   438   an argument about solving equational systems.  This argument appears to be
   439   folklore. For the other part, we give two proofs: one direct proof using
   439   folklore. For the other part, we give two proofs: one direct proof using
   440   certain tagging-functions, and another indirect proof using Antimirov's
   440   certain tagging-functions, and another indirect proof using Antimirov's
   441   partial derivatives \cite{Antimirov95}. Again to our best knowledge, the
   441   partial derivatives \cite{Antimirov95}. Again to our best knowledge, the
   442   tagging-functions have not been used before to establish the Myhill-Nerode
   442   tagging-functions have not been used before for establishing the Myhill-Nerode
   443   theorem. Derivatives of regular expressions have been used recently quite
   443   theorem. Derivatives of regular expressions have been used recently quite
   444   widely in the literature; partial derivatives, in contrast, attract much
   444   widely in the literature; partial derivatives, in contrast, attract much
   445   less attention. However, partial derivatives are more suitable in the
   445   less attention. However, partial derivatives are more suitable in the
   446   context of the Myhill-Nerode theorem, since it is easier to establish
   446   context of the Myhill-Nerode theorem, since it is easier to establish
   447   formally their finiteness result. We are not aware of any proof that uses
   447   formally their finiteness result. We are not aware of any proof that uses
  1164   Since the left-hand side is equal to @{text A}, we can use @{term "\<Uplus>rs"} 
  1164   Since the left-hand side is equal to @{text A}, we can use @{term "\<Uplus>rs"} 
  1165   as the regular expression that is needed in the theorem.
  1165   as the regular expression that is needed in the theorem.
  1166   \end{proof}
  1166   \end{proof}
  1167 
  1167 
  1168   \noindent
  1168   \noindent
  1169   Note that solving our equational system also gives us a method for
  1169   Note that our algorithm for solving equational systems provides also a method for
  1170   calculating the regular expression for a complement of a regular language:
  1170   calculating a regular expression for the complement of a regular language:
  1171   similar to the construction on automata, if we combine all regular
  1171   if we combine all regular
  1172   expressions corresponding to equivalence classes not in @{term "finals A"},
  1172   expressions corresponding to equivalence classes not in @{term "finals A"},
  1173   we obtain a regular expression for the complement @{term "- A"}.
  1173   then we obtain a regular expression for the complement language @{term "- A"}.
       
  1174   This is similar to the usual construction of a `complement automaton'.
  1174 *}
  1175 *}
  1175 
  1176 
  1176 
  1177 
  1177 
  1178 
  1178 
  1179 
  1250   equivalence classes. To show that there are only finitely many of them, it
  1251   equivalence classes. To show that there are only finitely many of them, it
  1251   suffices to show in each induction step that another relation, say @{text
  1252   suffices to show in each induction step that another relation, say @{text
  1252   R}, has finitely many equivalence classes and refines @{term "\<approx>(lang r)"}. 
  1253   R}, has finitely many equivalence classes and refines @{term "\<approx>(lang r)"}. 
  1253 
  1254 
  1254   \begin{dfntn}
  1255   \begin{dfntn}
  1255   A relation @{text "R\<^isub>1"} is said to \emph{refine} @{text "R\<^isub>2"}
  1256   A relation @{text "R\<^isub>1"} \emph{refines} @{text "R\<^isub>2"}
  1256   provided @{text "R\<^isub>1 \<subseteq> R\<^isub>2"}. 
  1257   provided @{text "R\<^isub>1 \<subseteq> R\<^isub>2"}. 
  1257   \end{dfntn}
  1258   \end{dfntn}
  1258 
  1259 
  1259   \noindent
  1260   \noindent
  1260   For constructing @{text R}, will rely on some \emph{tagging-functions}
  1261   For constructing @{text R}, will rely on some \emph{tagging-functions}
  1696   %   & @{thm (rhs) Derivs_simps(3)[where ?s1.0="s\<^isub>1" and ?s2.0="s\<^isub>2"]}\\
  1697   %   & @{thm (rhs) Derivs_simps(3)[where ?s1.0="s\<^isub>1" and ?s2.0="s\<^isub>2"]}\\
  1697   \end{tabular}}
  1698   \end{tabular}}
  1698   \end{equation}
  1699   \end{equation}
  1699 
  1700 
  1700   \noindent
  1701   \noindent
  1701   where @{text "\<Delta>"} in the fifth line is a function that tests whether the
  1702   Note that in the last equation we use the list-cons operator written
  1702   empty string is in the language and returns @{term "{[]}"} or @{term "{}"},
       
  1703   accordingly.  Note also that in the last equation we use the list-cons operator written
       
  1704   \mbox{@{text "_ :: _"}}.  The only interesting case is the case of @{term "A\<star>"}
  1703   \mbox{@{text "_ :: _"}}.  The only interesting case is the case of @{term "A\<star>"}
  1705   where we use Property~\ref{langprops}@{text "(i)"} in order to infer that
  1704   where we use Property~\ref{langprops}@{text "(i)"} in order to infer that
  1706   @{term "Deriv c (A\<star>) = Deriv c (A \<cdot> A\<star>)"}. We can then complete the proof by
  1705   @{term "Deriv c (A\<star>) = Deriv c (A \<cdot> A\<star>)"}. We can then complete the proof by
  1707   using the fifth equation and noting that @{term "Delta A \<cdot> Deriv c (A\<star>) \<subseteq> (Deriv
  1706   using the fifth equation and noting that @{term "Deriv c (A\<star>) \<subseteq> (Deriv
  1708   c A) \<cdot> A\<star>"}. 
  1707   c A) \<cdot> A\<star>"} provided @{text "[] \<in> A"}. 
  1709 
  1708 
  1710   Brzozowski observed that the left-quotients for languages of
  1709   Brzozowski observed that the left-quotients for languages of
  1711   regular expressions can be calculated directly using the notion of
  1710   regular expressions can be calculated directly using the notion of
  1712   \emph{derivatives of a regular expression} \cite{Brzozowski64}. We define
  1711   \emph{derivatives of a regular expression} \cite{Brzozowski64}. We define
  1713   this notion in Isabelle/HOL as follows:
  1712   this notion in Isabelle/HOL as follows:
  1926   \noindent
  1925   \noindent
  1927   These two properties confirm the observation made earlier 
  1926   These two properties confirm the observation made earlier 
  1928   that by using sets, partial derivatives have the @{text "ACI"}-identities
  1927   that by using sets, partial derivatives have the @{text "ACI"}-identities
  1929   of derivatives already built in. 
  1928   of derivatives already built in. 
  1930 
  1929 
  1931   Antimirov also proved that for every language and regular expression 
  1930   Antimirov also proved that for every language and every regular expression 
  1932   there are only finitely many partial derivatives, whereby the set of partial
  1931   there are only finitely many partial derivatives, whereby the set of partial
  1933   derivatives of @{text r} w.r.t.~a language @{text A} is defined as
  1932   derivatives of @{text r} w.r.t.~a language @{text A} is defined as
  1934 
  1933 
  1935   \begin{equation}\label{Pdersdef}
  1934   \begin{equation}\label{Pdersdef}
  1936   @{thm pderivs_lang_def}
  1935   @{thm pderivs_lang_def}
  2003   \end{center}
  2002   \end{center}
  2004 
  2003 
  2005   \noindent
  2004   \noindent
  2006   Now the range of @{term "\<lambda>x. pderivs x r"} is a subset of @{term "Pow (pderivs_lang UNIV r)"},
  2005   Now the range of @{term "\<lambda>x. pderivs x r"} is a subset of @{term "Pow (pderivs_lang UNIV r)"},
  2007   which we know is finite by Theorem~\ref{antimirov}. Consequently there 
  2006   which we know is finite by Theorem~\ref{antimirov}. Consequently there 
  2008   are only finitely many equivalence classes of @{text "\<^raw:$\threesim$>\<^bsub>(\<lambda>x. pders x r)\<^esub>"},
  2007   are only finitely many equivalence classes of @{text "\<^raw:$\threesim$>\<^bsub>(\<lambda>x. pders x r)\<^esub>"}.
  2009   which refines @{term "\<approx>(lang r)"}, and therefore we can again conclude the 
  2008   This relation refines @{term "\<approx>(lang r)"}, and therefore we can again conclude the 
  2010   second part of the Myhill-Nerode theorem.
  2009   second part of the Myhill-Nerode theorem.
  2011   \end{proof}
  2010   \end{proof}
  2012 *}
  2011 *}
  2013 
  2012 
  2014 section {* Closure Properties of Regular Languages\label{closure} *}
  2013 section {* Closure Properties of Regular Languages\label{closure} *}
  2097   "Deriv_lang B A"} is regular. To see this consider the following argument
  2096   "Deriv_lang B A"} is regular. To see this consider the following argument
  2098   using partial derivatives: From @{text A} being regular we know there exists
  2097   using partial derivatives: From @{text A} being regular we know there exists
  2099   a regular expression @{text r} such that @{term "A = lang r"}. We also know
  2098   a regular expression @{text r} such that @{term "A = lang r"}. We also know
  2100   that @{term "pderivs_lang B r"} is finite for every language @{text B} and 
  2099   that @{term "pderivs_lang B r"} is finite for every language @{text B} and 
  2101   regular expression @{text r} (recall Theorem~\ref{antimirov}). By definition 
  2100   regular expression @{text r} (recall Theorem~\ref{antimirov}). By definition 
  2102   and \eqref{Derspders} therefore
  2101   and \eqref{Derspders} we have
  2103 
  2102 
  2104   
  2103   
  2105   \begin{equation}\label{eqq}
  2104   \begin{equation}\label{eqq}
  2106   @{term "Deriv_lang B (lang r) = (\<Union> lang ` (pderivs_lang B r))"}
  2105   @{term "Deriv_lang B (lang r) = (\<Union> lang ` (pderivs_lang B r))"}
  2107   \end{equation}
  2106   \end{equation}
  2124   @{thm[mode=Rule] emb1[where as="x" and b="c" and bs="y"]}\hspace{10mm} 
  2123   @{thm[mode=Rule] emb1[where as="x" and b="c" and bs="y"]}\hspace{10mm} 
  2125   @{thm[mode=Rule] emb2[where as="x" and a="c" and bs="y"]}
  2124   @{thm[mode=Rule] emb2[where as="x" and a="c" and bs="y"]}
  2126   \end{center}
  2125   \end{center}
  2127 
  2126 
  2128   \noindent
  2127   \noindent
  2129   It can be easily proved that @{text "\<preceq>"} is a partial order. Now define the 
  2128   It is straightforward to prove that @{text "\<preceq>"} is a partial order. Now define the 
  2130   \emph{language of substrings} and \emph{superstrings} of a language @{text A} 
  2129   \emph{language of substrings} and \emph{superstrings} of a language @{text A} 
  2131   respectively as
  2130   respectively as
  2132 
  2131 
  2133   \begin{center}
  2132   \begin{center}
  2134   \begin{tabular}{l}
  2133   \begin{tabular}{l}
  2139   
  2138   
  2140   \noindent
  2139   \noindent
  2141   We like to establish
  2140   We like to establish
  2142 
  2141 
  2143   \begin{lmm}\label{subseqreg}
  2142   \begin{lmm}\label{subseqreg}
  2144   For every language @{text A}, the languages @{term "SUBSEQ A"} and @{term "SUPSEQ A"}
  2143   For every language @{text A}, the languages @{text "(i)"} @{term "SUBSEQ A"} and 
       
  2144   @{text "(ii)"} @{term "SUPSEQ A"}
  2145   are regular.
  2145   are regular.
  2146   \end{lmm}
  2146   \end{lmm}
  2147 
  2147 
  2148   \noindent
  2148   \noindent
  2149   Our proof follows the one given in \cite[92--95]{Shallit08}, except that we use
  2149   Our proof follows the one given in \cite[Pages 92--95]{Shallit08}, except that we use
  2150   Higman's Lemma, which is already proved in the Isabelle/HOL library \cite{Berghofer03}. 
  2150   Higman's Lemma, which is already proved in the Isabelle/HOL library \cite{Berghofer03}. 
  2151   Higman's Lemma allows us to infer that every set @{text A} of antichains, satisfying
  2151   Higman's Lemma allows us to infer that every set @{text A} of antichains, satisfying
  2152 
  2152 
  2153   \begin{equation}\label{higman}
  2153   \begin{equation}\label{higman}
  2154   @{text "\<forall>x, y \<in> A."}~@{term "x \<noteq> y \<longrightarrow> \<not>(x \<preceq> y) \<and> \<not>(y \<preceq> x)"}
  2154   @{text "\<forall>x, y \<in> A."}~@{term "x \<noteq> y \<longrightarrow> \<not>(x \<preceq> y) \<and> \<not>(y \<preceq> x)"}
  2156 
  2156 
  2157   \noindent
  2157   \noindent
  2158   is finite.
  2158   is finite.
  2159 
  2159 
  2160   The first step in our proof of Lemma~\ref{subseqreg} is to establish the 
  2160   The first step in our proof of Lemma~\ref{subseqreg} is to establish the 
  2161   following properties for @{term SUPSEQ}
  2161   following simple properties for @{term SUPSEQ}
  2162 
  2162 
  2163   \begin{equation}\label{supseqprops}
  2163   \begin{equation}\label{supseqprops}
  2164   \mbox{\begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
  2164   \mbox{\begin{tabular}{l@ {\hspace{1mm}}c@ {\hspace{1mm}}l}
  2165   @{thm (lhs) SUPSEQ_simps(1)} & @{text "\<equiv>"} & @{thm (rhs) SUPSEQ_simps(1)}\\  
  2165   @{thm (lhs) SUPSEQ_simps(1)} & @{text "\<equiv>"} & @{thm (rhs) SUPSEQ_simps(1)}\\  
  2166   @{thm (lhs) SUPSEQ_simps(2)} & @{text "\<equiv>"} & @{thm (rhs) SUPSEQ_simps(2)}\\ 
  2166   @{thm (lhs) SUPSEQ_simps(2)} & @{text "\<equiv>"} & @{thm (rhs) SUPSEQ_simps(2)}\\ 
  2224   By Higman's Lemma \eqref{higman} we know
  2224   By Higman's Lemma \eqref{higman} we know
  2225   that @{term "M \<equiv> {x \<in> A. minimal x A}"} is finite, since every minimal element is incomparable, 
  2225   that @{term "M \<equiv> {x \<in> A. minimal x A}"} is finite, since every minimal element is incomparable, 
  2226   except with itself.
  2226   except with itself.
  2227   It is also straightforward to show that @{term "SUPSEQ M \<subseteq> SUPSEQ A"}. For
  2227   It is also straightforward to show that @{term "SUPSEQ M \<subseteq> SUPSEQ A"}. For
  2228   the other direction we have  @{term "x \<in> SUPSEQ A"}. From this we obtain 
  2228   the other direction we have  @{term "x \<in> SUPSEQ A"}. From this we obtain 
  2229   a @{text y} such that @{term "y \<in> A"} and @{term "y \<preceq> x"}. Since we know that
  2229   a @{text y} such that @{term "y \<in> A"} and @{term "y \<preceq> x"}. Since we have that
  2230   the relation \mbox{@{term "{(y, x). y \<preceq> x \<and> x \<noteq> y}"}} is well-founded, there must
  2230   the relation \mbox{@{term "{(y, x). y \<preceq> x \<and> x \<noteq> y}"}} is well-founded, there must
  2231   be a minimal element @{text "z"} such that @{term "z \<in> A"} and @{term "z \<preceq> y"}, 
  2231   be a minimal element @{text "z"} such that @{term "z \<in> A"} and @{term "z \<preceq> y"}, 
  2232   and hence by transitivity also \mbox{@{term "z \<preceq> x"}} (here we deviate from the argument 
  2232   and hence by transitivity also \mbox{@{term "z \<preceq> x"}} (here we deviate from the argument 
  2233   given in \cite{Shallit08}, because Isabelle/HOL provides already an extensive infrastructure
  2233   given in \cite{Shallit08}, because Isabelle/HOL provides already an extensive infrastructure
  2234   for reasoning about well-foundedness). Since @{term "z"} is
  2234   for reasoning about well-foundedness). Since @{term "z"} is
  2263   we established already that regularity is preserved under complement, also @{term "SUBSEQ A"}
  2263   we established already that regularity is preserved under complement, also @{term "SUBSEQ A"}
  2264   must be regular. 
  2264   must be regular. 
  2265   \end{proof}
  2265   \end{proof}
  2266 
  2266 
  2267   Finally we like to show that the Myhill-Nerode theorem is also convenient for establishing 
  2267   Finally we like to show that the Myhill-Nerode theorem is also convenient for establishing 
  2268   non-regularity of languages. For this we use the following version of the Continuation
  2268   the non-regularity of languages. For this we use the following version of the Continuation
  2269   Lemma (see for example~\cite{Rosenberg06}).
  2269   Lemma (see for example~\cite{Rosenberg06}).
  2270 
  2270 
  2271   \begin{lmm}[Continuation Lemma]
  2271   \begin{lmm}[Continuation Lemma]
  2272   If the language @{text A} is regular and the set @{text B} is infinite,
  2272   If a language @{text A} is regular and a set @{text B} is infinite,
  2273   then there exist two distinct strings @{text x} and @{text y} in @{text B} 
  2273   then there exist two distinct strings @{text x} and @{text y} in @{text B} 
  2274   such that @{term "x \<approx>A y"}.
  2274   such that @{term "x \<approx>A y"}.
  2275   \end{lmm}
  2275   \end{lmm}
  2276 
  2276 
  2277   \noindent
  2277   \noindent
  2278   This lemma can be easily deduced from the Myhill-Nerode theorem and the Pigeonhole
  2278   This lemma can be easily deduced from the Myhill-Nerode theorem and the Pigeonhole
  2279   Principle: Since @{text A} is regular, there can be only finitely many 
  2279   Principle: Since @{text A} is regular, there can be only finitely many 
  2280   equivalence classes by the Myhill-Nerode relation. Hence an infinite set must contain 
  2280   equivalence classes. Hence an infinite set must contain 
  2281   at least two strings that are in the same equivalence class, that is
  2281   at least two strings that are in the same equivalence class, that is
  2282   they need to be related by the Myhill-Nerode relation.
  2282   they need to be related by the Myhill-Nerode relation.
  2283 
  2283 
  2284   Using this lemma, it is straightforward to establish that the language 
  2284   Using this lemma, it is straightforward to establish that the language 
  2285   \mbox{@{text "A \<equiv> \<Union>\<^isub>n a\<^sup>n @ b\<^sup>n"}}, where @{text "a\<^sup>n"} stands
  2285   \mbox{@{text "A \<equiv> \<Union>\<^isub>n a\<^sup>n @ b\<^sup>n"}} is not regular (@{text "a\<^sup>n"} stands
  2286   for the strings consisting of @{text n} times the character a, is not
  2286   for the strings consisting of @{text n} times the character a; similarly for
  2287   regular. For this consider the infinite set @{text "B \<equiv> \<Union>\<^isub>n a\<^sup>n"}.
  2287   @{text "b\<^isub>n"}). For this consider the infinite set @{text "B \<equiv> \<Union>\<^isub>n a\<^sup>n"}.
  2288 
  2288 
  2289   \begin{lmm}
  2289   \begin{lmm}
  2290   No two distinct strings in @{text "B"} are Myhill-Nerode related by @{text A}.
  2290   No two distinct strings in @{text "B"} are Myhill-Nerode related by @{text A}.
  2291   \end{lmm} 
  2291   \end{lmm} 
  2292 
  2292 
  2371   (for example in terms of length) for a regular language, like there is for
  2371   (for example in terms of length) for a regular language, like there is for
  2372   automata. On the other hand, efficient regular expression matching, without
  2372   automata. On the other hand, efficient regular expression matching, without
  2373   using automata, poses no problem \cite{OwensReppyTuron09}.  For an
  2373   using automata, poses no problem \cite{OwensReppyTuron09}.  For an
  2374   implementation of a simple regular expression matcher, whose correctness has
  2374   implementation of a simple regular expression matcher, whose correctness has
  2375   been formally established, we refer the reader to Owens and Slind
  2375   been formally established, we refer the reader to Owens and Slind
  2376   \cite{OwensSlind08}.
  2376   \cite{OwensSlind08}. In our opinion, their formalisation is considerably
       
  2377   slicker than for example the approach to regular expression matching taken
       
  2378   in \cite{Harper99} and \cite{Yi06}.
  2377 
  2379 
  2378   Our proof of the first direction is very much inspired by \emph{Brzozowski's
  2380   Our proof of the first direction is very much inspired by \emph{Brzozowski's
  2379   algebraic method} used to convert a finite automaton to a regular expression
  2381   algebraic method} used to convert a finite automaton to a regular expression
  2380   \cite{Brzozowski64}. The close connection can be seen by considering the
  2382   \cite{Brzozowski64}. The close connection can be seen by considering the
  2381   equivalence classes as the states of the minimal automaton for the regular
  2383   equivalence classes as the states of the minimal automaton for the regular
  2400   our first proof based on tagging-functions is new for establishing the
  2402   our first proof based on tagging-functions is new for establishing the
  2401   Myhill-Nerode theorem. All standard proofs of this direction proceed by
  2403   Myhill-Nerode theorem. All standard proofs of this direction proceed by
  2402   arguments over automata.
  2404   arguments over automata.
  2403   
  2405   
  2404   The indirect proof for the second direction arose from our interest in
  2406   The indirect proof for the second direction arose from our interest in
  2405   Brzozowski's derivatives for regular expression matching. A corresponding
  2407   Brzozowski's derivatives for regular expression matching.  While Brzozowski 
  2406   regular expression matcher has been formalised by Owens and Slind in HOL4
  2408   already established that there are only
  2407   \cite{OwensSlind08}. In our opinion, their formalisation is considerably
       
  2408   slicker than for example the approach to regular expression matching taken
       
  2409   in \cite{Harper99} and \cite{Yi06}. While Brzozowski's derivatives lead to a
       
  2410   simple regular expression matcher and he established that there are only
       
  2411   finitely many dissimilar derivatives for every regular expression, this
  2409   finitely many dissimilar derivatives for every regular expression, this
  2412   result is not as straightforward to formalise in a theorem prover as one
  2410   result is not as straightforward to formalise in a theorem prover as one
  2413   might wish. The reason is that the set of dissimilar derivatives is not
  2411   might wish. The reason is that the set of dissimilar derivatives is not
  2414   defined inductively, but in terms of an ACI-equivalence relation. This
  2412   defined inductively, but in terms of an ACI-equivalence relation. This
  2415   difficulty prevented for example Krauss and Nipkow to prove termination of
  2413   difficulty prevented for example Krauss and Nipkow to prove termination of
  2416   their equivalence checker for regular expressions
  2414   their equivalence checker for regular expressions
  2417   \cite{KraussNipkow11}. Their checker is based on Brzozowski's derivatives
  2415   \cite{KraussNipkow11}. Their checker is based on Brzozowski's derivatives
  2418   and for their argument the lack of a formal proof of termination is not
  2416   and for their argument the lack of a formal proof of termination is not
  2419   crucial (it merely lets them ``sleep better'' \cite{KraussNipkow11}).  We
  2417   crucial (it merely lets them ``sleep better'' \cite{KraussNipkow11}).  We
  2420   expect that their development simplifies by using partial derivatives,
  2418   expect that their development simplifies by using partial derivatives,
  2421   instead of derivatives, and that termination of the algorithm can be
  2419   instead of derivatives, and that the termination of the algorithm can be
  2422   formally established (the main incredience is
  2420   formally established (the main ingredient is
  2423   Theorem~\ref{antimirov}). However, since partial derivatives use sets of
  2421   Theorem~\ref{antimirov}). However, since partial derivatives use sets of
  2424   regular expressions, one needs to carefully analyse whether the resulting
  2422   regular expressions, one needs to carefully analyse whether the resulting
  2425   algorithm is still executable. Given the existing infrastructure for
  2423   algorithm is still executable. Given the existing infrastructure for
  2426   executable sets in Isabelle/HOL \cite{Haftmann09}, it should.
  2424   executable sets in Isabelle/HOL \cite{Haftmann09}, it should.
  2427 
  2425 
  2443 
  2441 
  2444   While our formalisation might appear large, it should be seen
  2442   While our formalisation might appear large, it should be seen
  2445   in the context of the work done by Constable at al \cite{Constable00} who
  2443   in the context of the work done by Constable at al \cite{Constable00} who
  2446   formalised the Myhill-Nerode theorem in Nuprl using automata. They write
  2444   formalised the Myhill-Nerode theorem in Nuprl using automata. They write
  2447   that their four-member team needed something on the magnitude of 18 months
  2445   that their four-member team needed something on the magnitude of 18 months
  2448   for their formalisation. Also, Filli\^atre reports that his formalisation in 
  2446   for their formalisation. It is hard to gauge the size of a
  2449   Coq of automata theory and Kleene's theorem is ``rather big''. 
  2447   formalisation in Nurpl, but from what is shown in the Nuprl Math Library
  2450   \cite{Filliatre97} More recently, Almeida et al reported about another 
  2448   about their development it seems substantially larger than ours. We attribute
       
  2449   this to our use of regular expressions, which meant we did not need to `fight' 
       
  2450   the theorem prover.
       
  2451   Also, Filli\^atre reports that his formalisation in 
       
  2452   Coq of automata theory and Kleene's theorem is ``rather big'' 
       
  2453   \cite{Filliatre97}. More recently, Almeida et al reported about another 
  2451   formalisation of regular languages in Coq \cite{Almeidaetal10}. Their 
  2454   formalisation of regular languages in Coq \cite{Almeidaetal10}. Their 
  2452   main result is the
  2455   main result is the
  2453   correctness of Mirkin's construction of an automaton from a regular
  2456   correctness of Mirkin's construction of an automaton from a regular
  2454   expression using partial derivatives. This took approximately 10600 lines
  2457   expression using partial derivatives. This took approximately 10600 lines
  2455   of code.  The estimate for our formalisation is that we
  2458   of code.  In terms of time, the estimate for our formalisation is that we
  2456   needed approximately 3 months and this included the time to find our proof
  2459   needed approximately 3 months and this included the time to find our proof
  2457   arguments. Unlike Constable et al, who were able to follow the Myhill-Nerode 
  2460   arguments. Unlike Constable et al, who were able to follow the Myhill-Nerode 
  2458   proof from \cite{HopcroftUllman69}, we had to find our own arguments.  So for us the
  2461   proof from \cite{HopcroftUllman69}, we had to find our own arguments.  So for us the
  2459   formalisation was not the bottleneck. It is hard to gauge the size of a
  2462   formalisation was not the bottleneck.  The code of
  2460   formalisation in Nurpl, but from what is shown in the Nuprl Math Library
       
  2461   about their development it seems substantially larger than ours. We attribute
       
  2462   this to our use of regular expressions, which meant we did not need to `fight' 
       
  2463   the theorem prover. The code of
       
  2464   our formalisation can be found in the Archive of Formal Proofs at
  2463   our formalisation can be found in the Archive of Formal Proofs at
  2465   \mbox{\url{http://afp.sourceforge.net/devel-entries/Myhill-Nerode.shtml}} \cite{myhillnerodeafp11}.\medskip
  2464   \mbox{\url{http://afp.sourceforge.net/devel-entries/Myhill-Nerode.shtml}} 
       
  2465   \cite{myhillnerodeafp11}.\medskip
  2466   
  2466   
  2467   \noindent
  2467   \noindent
  2468   {\bf Acknowledgements:}
  2468   {\bf Acknowledgements:}
  2469   We are grateful for the comments we received from Larry Paulson.  Tobias
  2469   We are grateful for the comments we received from Larry Paulson.  Tobias
  2470   Nipkow made us aware of the properties in Lemma~\ref{subseqreg} and Tjark
  2470   Nipkow made us aware of the properties in Lemma~\ref{subseqreg} and Tjark
  2471   Weber helped us with their proofs.
  2471   Weber helped us with proving them.
  2472 
       
  2473 
       
  2474 *}
  2472 *}
  2475 
  2473 
  2476 
  2474 
  2477 (*<*)
  2475 (*<*)
  2478 end
  2476 end