Paper/Paper.thy
changeset 101 d3fe0597080a
parent 100 2409827d8eb8
child 103 f460d5f75cb5
equal deleted inserted replaced
100:2409827d8eb8 101:d3fe0597080a
    44   Regular languages are an important and well-understood subject in Computer
    44   Regular languages are an important and well-understood subject in Computer
    45   Science, with many beautiful theorems and many useful algorithms. There is a
    45   Science, with many beautiful theorems and many useful algorithms. There is a
    46   wide range of textbooks on this subject, many of which are aimed at students
    46   wide range of textbooks on this subject, many of which are aimed at students
    47   and contain very detailed ``pencil-and-paper'' proofs
    47   and contain very detailed ``pencil-and-paper'' proofs
    48   (e.g.~\cite{Kozen97}). It seems natural to exercise theorem provers by
    48   (e.g.~\cite{Kozen97}). It seems natural to exercise theorem provers by
    49   formalising these theorems and by verifying formally the algorithms.
    49   formalising the theorems and by verifying formally the algorithms.
    50 
    50 
    51   There is however a problem: the typical approach to regular languages is to
    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
    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
    53   example, a regular language is normally defined as one whose strings are
    54   recognised by a finite deterministic automaton. This approach has many
    54   recognised by a finite deterministic automaton. This approach has many
   145   constructions, which are not pleasant to reason about.
   145   constructions, which are not pleasant to reason about.
   146 
   146 
   147   Functions are much better supported in Isabelle/HOL, but they still lead to similar
   147   Functions are much better supported in Isabelle/HOL, but they still lead to similar
   148   problems as with graphs.  Composing, for example, two non-deterministic automata in parallel
   148   problems as with graphs.  Composing, for example, two non-deterministic automata in parallel
   149   requires also the formalisation of disjoint unions. Nipkow \cite{Nipkow98} 
   149   requires also the formalisation of disjoint unions. Nipkow \cite{Nipkow98} 
   150   dismisses the option of using identities, because it leads according to him to ``messy proofs''. He
   150   dismisses for this the option of using identities, because it leads according to 
   151   opts for a variant of \eqref{disjointunion} using bitlists, but writes
   151   him to ``messy proofs''. He
   152 
   152   opts for a variant of \eqref{disjointunion} using bitlists, but writes 
       
   153 
       
   154   \begin{quote}
       
   155   \it%
       
   156   \begin{tabular}{@ {}l@ {}p{0.88\textwidth}@ {}}
       
   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   
   153   \begin{quote}
   165   \begin{quote}
   154   \it%
   166   \it%
   155   \begin{tabular}{@ {}l@ {}p{0.88\textwidth}@ {}}
   167   \begin{tabular}{@ {}l@ {}p{0.88\textwidth}@ {}}
   156   `` & If the reader finds the above treatment in terms of bit lists revoltingly
   168   `` & If the reader finds the above treatment in terms of bit lists revoltingly
   157        concrete, I cannot disagree. A more abstract approach is clearly desirable.''\smallskip\\
   169        concrete, I cannot disagree. A more abstract approach is clearly desirable.''
   158   `` & All lemmas appear obvious given a picture of the composition of automata\ldots
       
   159        Yet their proofs require a painful amount of detail.''
       
   160   \end{tabular}
   170   \end{tabular}
   161   \end{quote}
   171   \end{quote}
   162   
   172 
       
   173 
   163   \noindent
   174   \noindent
   164   Moreover, it is not so clear how to conveniently impose a finiteness condition 
   175   Moreover, it is not so clear how to conveniently impose a finiteness condition 
   165   upon functions in order to represent \emph{finite} automata. The best is
   176   upon functions in order to represent \emph{finite} automata. The best is
   166   probably to resort to more advanced reasoning frameworks, such as \emph{locales}
   177   probably to resort to more advanced reasoning frameworks, such as \emph{locales}
   167   or \emph{type classes},
   178   or \emph{type classes},
   188   
   199   
   189   \noindent
   200   \noindent
   190   The reason is that regular expressions, unlike graphs, matrices and functons, can
   201   The reason is that regular expressions, unlike graphs, matrices and functons, can
   191   be easily defined as inductive datatype. Consequently a corresponding reasoning 
   202   be easily defined as inductive datatype. Consequently a corresponding reasoning 
   192   infrastructure comes for free. This has recently been exploited in HOL4 with a formalisation
   203   infrastructure comes for free. This has recently been exploited in HOL4 with a formalisation
   193   of regular expression matching based on derivatives \cite{OwensSlind08}.  The purpose of this paper is to
   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
   194   show that a central result about regular languages---the Myhill-Nerode theorem---can 
   207   show that a central result about regular languages---the Myhill-Nerode theorem---can 
   195   be recreated by only using regular expressions. This theorem gives necessary
   208   be recreated by only using regular expressions. This theorem gives necessary
   196   and sufficient conditions for when a language is regular. As a corollary of this
   209   and sufficient conditions for when a language is regular. As a corollary of this
   197   theorem we can easily establish the usual closure properties, including 
   210   theorem we can easily establish the usual closure properties, including 
   198   complementation, for regular languages.\smallskip
   211   complementation, for regular languages.\smallskip
   250   @{text "\<lbrakk>x\<rbrakk>\<^isub>\<approx>"} for the equivalence class defined 
   263   @{text "\<lbrakk>x\<rbrakk>\<^isub>\<approx>"} for the equivalence class defined 
   251   as @{text "{y | y \<approx> x}"}.
   264   as @{text "{y | y \<approx> x}"}.
   252 
   265 
   253 
   266 
   254   Central to our proof will be the solution of equational systems
   267   Central to our proof will be the solution of equational systems
   255   involving sets of languages. For this we will use Arden's lemma \cite{Brzozowski64}
   268   involving equivalence classes of languages. For this we will use Arden's lemma \cite{Brzozowski64}
   256   which solves equations of the form @{term "X = A ;; X \<union> B"} provided
   269   which solves equations of the form @{term "X = A ;; X \<union> B"} provided
   257   @{term "[] \<notin> A"}. However we will need the following ``reverse'' 
   270   @{term "[] \<notin> A"}. However we will need the following ``reverse'' 
   258   version of Arden's lemma.
   271   version of Arden's lemma.
   259 
   272 
   260   \begin{lemma}[Reverse Arden's Lemma]\label{arden}\mbox{}\\
   273   \begin{lemma}[Reverse Arden's Lemma]\label{arden}\mbox{}\\
   356   \end{definition}
   369   \end{definition}
   357 
   370 
   358   \noindent
   371   \noindent
   359   It is easy to see that @{term "\<approx>A"} is an equivalence relation, which
   372   It is easy to see that @{term "\<approx>A"} is an equivalence relation, which
   360   partitions the set of all strings, @{text "UNIV"}, into a set of disjoint
   373   partitions the set of all strings, @{text "UNIV"}, into a set of disjoint
   361   equivalence classes. Let us give an example: consider the regular language containing just
   374   equivalence classes. To illustrate this quotient construction, let us give an 
       
   375   example: consider the regular language containing just
   362   the string @{text "[c]"}. The relation @{term "\<approx>({[c]})"} partitions @{text UNIV}
   376   the string @{text "[c]"}. The relation @{term "\<approx>({[c]})"} partitions @{text UNIV}
   363   into the three equivalence classes @{text "X\<^isub>1"}, @{text "X\<^isub>2"} and  @{text "X\<^isub>3"}
   377   into three equivalence classes @{text "X\<^isub>1"}, @{text "X\<^isub>2"} and  @{text "X\<^isub>3"}
   364   as follows
   378   as follows
   365   
   379   
   366   \begin{center}
   380   \begin{center}
   367   @{text "X\<^isub>1 = {[]}"}\hspace{5mm}
   381   @{text "X\<^isub>1 = {[]}"}\hspace{5mm}
   368   @{text "X\<^isub>2 = {[c]}"}\hspace{5mm}
   382   @{text "X\<^isub>2 = {[c]}"}\hspace{5mm}
   465   \begin{equation}\label{inv2}
   479   \begin{equation}\label{inv2}
   466   @{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))"}.
   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))"}.
   467   \end{equation}
   481   \end{equation}
   468 
   482 
   469   \noindent
   483   \noindent
   470   The reason for adding the @{text \<lambda>}-marker to our equational system is 
   484   The reason for adding the @{text \<lambda>}-marker to our initial equational system is 
   471   to obtain this equation: it only holds in this form since none of 
   485   to obtain this equation: it only holds with the marker since none of 
   472   the other terms contain the empty string. 
   486   the other terms contain the empty string. 
   473 
   487 
   474   Our represeantation of the equations are pairs,
   488   Our representation for the equations in Isabelle/HOL are pairs,
   475   where the first component is an equivalence class and the second component
   489   where the first component is an equivalence class and the second component
   476   is a set of terms standing for the right-hand side. Given a set of equivalence
   490   is a set of terms. Given a set of equivalence
   477   classes @{text CS}, our initial equational system @{term "Init CS"} is thus 
   491   classes @{text CS}, our initial equational system @{term "Init CS"} is thus 
   478   defined as
   492   formally defined as
   479 
   493 
   480   \begin{center}
   494   \begin{center}
   481   \begin{tabular}{rcl}     
   495   \begin{tabular}{rcl}     
   482   @{thm (lhs) Init_rhs_def} & @{text "\<equiv>"} & 
   496   @{thm (lhs) Init_rhs_def} & @{text "\<equiv>"} & 
   483   @{text "if"}~@{term "[] \<in> X"}\\
   497   @{text "if"}~@{term "[] \<in> X"}\\
   489 
   503 
   490   
   504   
   491 
   505 
   492   \noindent
   506   \noindent
   493   Because we use sets of terms 
   507   Because we use sets of terms 
   494   for representing the right-hand sides in the equational system we can 
   508   for representing the right-hand sides of equations, we can 
   495   prove \eqref{inv1} and \eqref{inv2} more concisely as
   509   prove \eqref{inv1} and \eqref{inv2} more concisely as
   496   %
   510   %
   497   \begin{lemma}\label{inv}
   511   \begin{lemma}\label{inv}
   498   If @{thm (prem 1) test} then @{text "X = \<Union> \<calL> ` rhs"}.
   512   If @{thm (prem 1) test} then @{text "X = \<Union> \<calL> ` rhs"}.
   499   \end{lemma}
   513   \end{lemma}
   518   \end{center}
   532   \end{center}
   519 
   533 
   520   \noindent
   534   \noindent
   521   which we also lift to entire right-hand sides of equations, written as
   535   which we also lift to entire right-hand sides of equations, written as
   522   @{thm (lhs) append_rhs_rexp_def[where rexp="r"]}. With this we can define
   536   @{thm (lhs) append_rhs_rexp_def[where rexp="r"]}. With this we can define
   523   the \emph{arden-operation} for an equation of the form @{text "X = rhs"}:
   537   the \emph{arden-operation} for an equation of the form @{text "X = rhs"} as:
   524   
   538   
   525   \begin{center}
   539   \begin{center}
   526   \begin{tabular}{rc@ {\hspace{2mm}}r@ {\hspace{1mm}}l}
   540   \begin{tabular}{rc@ {\hspace{2mm}}r@ {\hspace{1mm}}l}
   527   @{thm (lhs) Arden_def} & @{text "\<equiv>"}~~\mbox{} & \multicolumn{2}{@ {\hspace{-2mm}}l}{@{text "let"}}\\ 
   541   @{thm (lhs) Arden_def} & @{text "\<equiv>"}~~\mbox{} & \multicolumn{2}{@ {\hspace{-2mm}}l}{@{text "let"}}\\ 
   528    & & @{text "rhs' ="} & @{term "rhs - {Trn X r | r. Trn X r \<in> rhs}"} \\
   542    & & @{text "rhs' ="} & @{term "rhs - {Trn X r | r. Trn X r \<in> rhs}"} \\
   530    & &  \multicolumn{2}{@ {\hspace{-2mm}}l}{@{text "in"}~~@{term "append_rhs_rexp rhs' r'"}}\\ 
   544    & &  \multicolumn{2}{@ {\hspace{-2mm}}l}{@{text "in"}~~@{term "append_rhs_rexp rhs' r'"}}\\ 
   531   \end{tabular}
   545   \end{tabular}
   532   \end{center}
   546   \end{center}
   533 
   547 
   534   \noindent
   548   \noindent
   535   We first delete all terms of the form @{text "(X, r)"} from @{text rhs};
   549   In this definition, we first delete all terms of the form @{text "(X, r)"} from @{text rhs};
   536   then we calculate the combinded regular expressions for all @{text r} coming 
   550   then we calculate the combinded regular expressions for all @{text r} coming 
   537   from the deleted @{text "(X, r)"}, and take the @{const STAR} of it;
   551   from the deleted @{text "(X, r)"}, and take the @{const STAR} of it;
   538   finally we append this regular expression to @{text rhs'}. It can be easily seen 
   552   finally we append this regular expression to @{text rhs'}. It can be easily seen 
   539   that this operation mimics Arden's lemma on the level of equations.  
   553   that this operation mimics Arden's lemma on the level of equations.  
   540   The \emph{substituion-operation} takes an equation
   554   The \emph{substituion-operation} takes an equation
   570   \end{tabular}
   584   \end{tabular}
   571   \end{center}
   585   \end{center}
   572 
   586 
   573   \noindent
   587   \noindent
   574   Finially, we can define how an equational system should be solved. For this 
   588   Finially, we can define how an equational system should be solved. For this 
   575   we will iterate the elimination of an equation until only one equation
   589   we will need to iterate the elimination of an equation until only one equation
   576   will be left in the system. However, we not just want to have any equation
   590   will be left in the system. However, we not just want to have any equation
   577   as being the last one, but the one for which we want to calculate the regular 
   591   as being the last one, but the one for which we want to calculate the regular 
   578   expression. Therefore we define the iteration step so that it chooses an
   592   expression. Therefore we define the iteration step so that it chooses an
   579   equation with an equivalence class that is not @{text X}. This allows us to 
   593   equation with an equivalence class that is not @{text X}. This allows us to 
   580   control, which equation will be the last. We use again Hilbert's choice operator, 
   594   control, which equation will be the last. We use Hilbert's choice operator, 
   581   written @{text SOME}, to chose an equation in a equational system @{text ES}.
   595   written @{text SOME}, to chose an equation to be eliminated in @{text ES}.
   582   
   596   
   583   \begin{center}
   597   \begin{center}
   584   \begin{tabular}{rc@ {\hspace{4mm}}r@ {\hspace{1mm}}l}
   598   \begin{tabular}{rc@ {\hspace{4mm}}r@ {\hspace{1mm}}l}
   585   @{thm (lhs) Iter_def} & @{text "\<equiv>"}~~\mbox{} & \multicolumn{2}{@ {\hspace{-4mm}}l}{@{text "let"}}\\ 
   599   @{thm (lhs) Iter_def} & @{text "\<equiv>"}~~\mbox{} & \multicolumn{2}{@ {\hspace{-4mm}}l}{@{text "let"}}\\ 
   586    & & @{text "(Y, yrhs) ="} & @{term "SOME (Y, yrhs). (Y, yrhs) \<in> ES \<and> X \<noteq> Y"} \\
   600    & & @{text "(Y, yrhs) ="} & @{term "SOME (Y, yrhs). (Y, yrhs) \<in> ES \<and> X \<noteq> Y"} \\
   587    & &  \multicolumn{2}{@ {\hspace{-4mm}}l}{@{text "in"}~~@{term "Remove ES Y yrhs"}}\\ 
   601    & &  \multicolumn{2}{@ {\hspace{-4mm}}l}{@{text "in"}~~@{term "Remove ES Y yrhs"}}\\ 
   588   \end{tabular}
   602   \end{tabular}
   589   \end{center}
   603   \end{center}
   590 
   604 
   591   \noindent
   605   \noindent
   592   The last definition in our
   606   The last definition applies @{term Iter} over and over again a condition 
   593 
   607   @{text COND} is \emph{not} satisfied anymore. The condition states that there
       
   608   are more than one equation in the equational system @{text ES}. The 
       
   609   iteration is defined in terms of HOL's @{text while}-operator as follows:
       
   610   
   594   \begin{center}
   611   \begin{center}
   595   @{thm Solve_def}
   612   @{thm Solve_def}
   596   \end{center}
   613   \end{center}
   597 
   614 
       
   615   \noindent
       
   616   We are not concerned here with the definition of this operator in this paper
       
   617   (see \cite{BerghoferNipkow00}).
   598 
   618 
   599   \begin{center}
   619   \begin{center}
   600   @{thm while_rule}
   620   @{thm while_rule}
   601   \end{center}
   621   \end{center}
   602 *}
   622 *}