Paper/Paper.thy
changeset 115 c5f138b5fc88
parent 114 c5eb5f3065ae
child 116 342983676c8f
equal deleted inserted replaced
114:c5eb5f3065ae 115:c5f138b5fc88
    43 
    43 
    44 text {*
    44 text {*
    45   Regular languages are an important and well-understood subject in Computer
    45   Regular languages are an important and well-understood subject in Computer
    46   Science, with many beautiful theorems and many useful algorithms. There is a
    46   Science, with many beautiful theorems and many useful algorithms. There is a
    47   wide range of textbooks on this subject, many of which are aimed at students
    47   wide range of textbooks on this subject, many of which are aimed at students
    48   and contain very detailed ``pencil-and-paper'' proofs
    48   and contain very detailed `pencil-and-paper' proofs
    49   (e.g.~\cite{Kozen97}). It seems natural to exercise theorem provers by
    49   (e.g.~\cite{Kozen97}). It seems natural to exercise theorem provers by
    50   formalising the theorems and by verifying formally the algorithms.
    50   formalising the theorems and by verifying formally the algorithms.
    51 
    51 
    52   There is however a problem: the typical approach to regular languages is to
    52   There is however a problem: the typical approach to regular languages is to
    53   introduce finite automata and then define everything in terms of them.  For
    53   introduce finite automata and then define everything in terms of them.  For
    56   benefits. Among them is the fact that it is easy to convince oneself that
    56   benefits. Among them is the fact that it is easy to convince oneself that
    57   regular languages are closed under complementation: one just has to exchange
    57   regular languages are closed under complementation: one just has to exchange
    58   the accepting and non-accepting states in the corresponding automaton to
    58   the accepting and non-accepting states in the corresponding automaton to
    59   obtain an automaton for the complement language.  The problem, however, lies with
    59   obtain an automaton for the complement language.  The problem, however, lies with
    60   formalising such reasoning in a HOL-based theorem prover, in our case
    60   formalising such reasoning in a HOL-based theorem prover, in our case
    61   Isabelle/HOL. Automata are build up from states and transitions that 
    61   Isabelle/HOL. Automata are built up from states and transitions that 
    62   need to be represented as graphs, matrices or functions, none
    62   need to be represented as graphs, matrices or functions, none
    63   of which can be defined as inductive datatype. 
    63   of which can be defined as inductive datatype. 
    64 
    64 
    65   In case of graphs and matrices, this means we have to build our own
    65   In case of graphs and matrices, this means we have to build our own
    66   reasoning infrastructure for them, as neither Isabelle/HOL nor HOL4 nor
    66   reasoning infrastructure for them, as neither Isabelle/HOL nor HOL4 nor
   122 
   122 
   123   \end{tabular}
   123   \end{tabular}
   124   \end{center}
   124   \end{center}
   125 
   125 
   126   \noindent
   126   \noindent
   127   On ``paper'' we can define the corresponding graph in terms of the disjoint 
   127   On `paper' we can define the corresponding graph in terms of the disjoint 
   128   union of the state nodes. Unfortunately in HOL, the standard definition for disjoint 
   128   union of the state nodes. Unfortunately in HOL, the standard definition for disjoint 
   129   union, namely 
   129   union, namely 
   130   %
   130   %
   131   \begin{equation}\label{disjointunion}
   131   \begin{equation}\label{disjointunion}
   132   @{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}"}
   132   @{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}"}
   179   or \emph{type classes},
   179   or \emph{type classes},
   180   which are \emph{not} avaiable in all HOL-based theorem provers.
   180   which are \emph{not} avaiable in all HOL-based theorem provers.
   181 
   181 
   182   Because of these problems to do with representing automata, there seems
   182   Because of these problems to do with representing automata, there seems
   183   to be no substantial formalisation of automata theory and regular languages 
   183   to be no substantial formalisation of automata theory and regular languages 
   184   carried out in HOL-based theorem provers. Nipkow establishes in 
   184   carried out in HOL-based theorem provers. Nipkow  \cite{Nipkow98} establishes
   185   \cite{Nipkow98} the link between regular expressions and automata in
   185   the link between regular expressions and automata in
   186   the context of lexing. Berghofer and Reiter formalise automata working over 
   186   the context of lexing. Berghofer and Reiter \cite{BerghoferReiter09} 
   187   bit strings in the context of Presburger arithmetic \cite{BerghoferReiter09}.
   187   formalise automata working over 
       
   188   bit strings in the context of Presburger arithmetic.
   188   The only larger formalisations of automata theory 
   189   The only larger formalisations of automata theory 
   189   are carried out in Nuprl \cite{Constable00} and in Coq (for example 
   190   are carried out in Nuprl \cite{Constable00} and in Coq \cite{Filliatre97}.
   190   \cite{Filliatre97}).
       
   191   
   191   
   192   In this paper, we will not attempt to formalise automata theory in
   192   In this paper, we will not attempt to formalise automata theory in
   193   Isabelle/HOL, but take a completely different approach to regular
   193   Isabelle/HOL, but take a completely different approach to regular
   194   languages. Instead of defining a regular language as one where there exists
   194   languages. Instead of defining a regular language as one where there exists
   195   an automaton that recognises all strings of the language, we define a
   195   an automaton that recognises all strings of the language, we define a
   266   @{text "\<lbrakk>x\<rbrakk>\<^isub>\<approx>"} for the equivalence class defined 
   266   @{text "\<lbrakk>x\<rbrakk>\<^isub>\<approx>"} for the equivalence class defined 
   267   as @{text "{y | y \<approx> x}"}.
   267   as @{text "{y | y \<approx> x}"}.
   268 
   268 
   269 
   269 
   270   Central to our proof will be the solution of equational systems
   270   Central to our proof will be the solution of equational systems
   271   involving equivalence classes of languages. For this we will use Arden's lemma \cite{Brzozowski64}
   271   involving equivalence classes of languages. For this we will use Arden's lemma \cite{Brzozowski64},
   272   which solves equations of the form @{term "X = A ;; X \<union> B"} provided
   272   which solves equations of the form @{term "X = A ;; X \<union> B"} provided
   273   @{term "[] \<notin> A"}. However we will need the following ``reverse'' 
   273   @{term "[] \<notin> A"}. However we will need the following `reverse' 
   274   version of Arden's lemma.
   274   version of Arden's lemma.
   275 
   275 
   276   \begin{lemma}[Reverse Arden's Lemma]\label{arden}\mbox{}\\
   276   \begin{lemma}[Reverse Arden's Lemma]\label{arden}\mbox{}\\
   277   If @{thm (prem 1) arden} then
   277   If @{thm (prem 1) arden} then
   278   @{thm (lhs) arden} has the unique solution
   278   @{thm (lhs) arden} if and only if
   279   @{thm (rhs) arden}.
   279   @{thm (rhs) arden}.
   280   \end{lemma}
   280   \end{lemma}
   281 
   281 
   282   \begin{proof}
   282   \begin{proof}
   283   For the right-to-left direction we assume @{thm (rhs) arden} and show
   283   For the right-to-left direction we assume @{thm (rhs) arden} and show
   363 
   363 
   364 text {*
   364 text {*
   365   The key definition in the Myhill-Nerode theorem is the
   365   The key definition in the Myhill-Nerode theorem is the
   366   \emph{Myhill-Nerode relation}, which states that w.r.t.~a language two 
   366   \emph{Myhill-Nerode relation}, which states that w.r.t.~a language two 
   367   strings are related, provided there is no distinguishing extension in this
   367   strings are related, provided there is no distinguishing extension in this
   368   language. This can be defined as:
   368   language. This can be defined as tertiary relation:
   369 
   369 
   370   \begin{definition}[Myhill-Nerode Relation]\mbox{}\\
   370   \begin{definition}[Myhill-Nerode Relation]\mbox{}\\
   371   @{thm str_eq_def[simplified str_eq_rel_def Pair_Collect]}
   371   @{thm str_eq_def[simplified str_eq_rel_def Pair_Collect]}
   372   \end{definition}
   372   \end{definition}
   373 
   373 
   451   X\<^isub>i"}.   There can only be
   451   X\<^isub>i"}.   There can only be
   452   finitely many such terms in a right-hand side since by assumption there are only finitely many
   452   finitely many such terms in a right-hand side since by assumption there are only finitely many
   453   equivalence classes and only finitely many characters.  The term @{text
   453   equivalence classes and only finitely many characters.  The term @{text
   454   "\<lambda>(EMPTY)"} in the first equation acts as a marker for the equivalence class
   454   "\<lambda>(EMPTY)"} in the first equation acts as a marker for the equivalence class
   455   containing @{text "[]"}.\footnote{Note that we mark, roughly speaking, the
   455   containing @{text "[]"}.\footnote{Note that we mark, roughly speaking, the
   456   single ``initial'' state in the equational system, which is different from
   456   single `initial' state in the equational system, which is different from
   457   the method by Brzozowski \cite{Brzozowski64}, where he marks the
   457   the method by Brzozowski \cite{Brzozowski64}, where he marks the
   458   ``terminal'' states. We are forced to set up the equational system in our
   458   `terminal' states. We are forced to set up the equational system in our
   459   way, because the Myhill-Nerode relation determines the ``direction'' of the
   459   way, because the Myhill-Nerode relation determines the `direction' of the
   460   transitions. The successor ``state'' of an equivalence class @{text Y} can
   460   transitions. The successor `state' of an equivalence class @{text Y} can
   461   be reached by adding characters to the end of @{text Y}. This is also the
   461   be reached by adding characters to the end of @{text Y}. This is also the
   462   reason why we have to use our reverse version of Arden's lemma.}
   462   reason why we have to use our reverse version of Arden's lemma.}
   463   Overloading the function @{text \<calL>} for the two kinds of terms in the
   463   Overloading the function @{text \<calL>} for the two kinds of terms in the
   464   equational system, we have
   464   equational system, we have
   465   
   465   
   567   This allows us to prove
   567   This allows us to prove
   568 
   568 
   569   \begin{lemma}\label{ardenable}
   569   \begin{lemma}\label{ardenable}
   570   Given an equation @{text "X = rhs"}.
   570   Given an equation @{text "X = rhs"}.
   571   If @{text "X = \<Union>\<calL> ` rhs"},
   571   If @{text "X = \<Union>\<calL> ` rhs"},
   572   @{thm (prem 2) Arden_keeps_eq} and
   572   @{thm (prem 2) Arden_keeps_eq}, and
   573   @{thm (prem 3) Arden_keeps_eq}, then
   573   @{thm (prem 3) Arden_keeps_eq}, then
   574   @{text "X = \<Union>\<calL> ` (Arden X rhs)"}
   574   @{text "X = \<Union>\<calL> ` (Arden X rhs)"}
   575   \end{lemma}
   575   \end{lemma}
   576   
   576   
   577   \noindent
   577   \noindent
   638   @{thm Solve_def}
   638   @{thm Solve_def}
   639   \end{center}
   639   \end{center}
   640 
   640 
   641   \noindent
   641   \noindent
   642   We are not concerned here with the definition of this operator
   642   We are not concerned here with the definition of this operator
   643   (see \cite{BerghoferNipkow00}), but note that we eliminate
   643   (see Berghofer and Nipkow \cite{BerghoferNipkow00}), but note that we eliminate
   644   in each @{const Iter}-step a single equation, and therefore 
   644   in each @{const Iter}-step a single equation, and therefore 
   645   have a well-founded termination order by taking the cardinality 
   645   have a well-founded termination order by taking the cardinality 
   646   of the equational system @{text ES}. This enables us to prove
   646   of the equational system @{text ES}. This enables us to prove
   647   properties about our definition of @{const Solve} when we ``call'' it with
   647   properties about our definition of @{const Solve} when we `call' it with
   648   the equivalence class @{text X} and the initial equational system 
   648   the equivalence class @{text X} and the initial equational system 
   649   @{term "Init (UNIV // \<approx>A)"} from
   649   @{term "Init (UNIV // \<approx>A)"} from
   650   \eqref{initcs} using the principle:
   650   \eqref{initcs} using the principle:
   651   %
   651   %
   652   \begin{equation}\label{whileprinciple}
   652   \begin{equation}\label{whileprinciple}
   688   \end{tabular}
   688   \end{tabular}
   689   \end{center}
   689   \end{center}
   690  
   690  
   691   \noindent
   691   \noindent
   692   The first two ensure that the equational system is always finite (number of equations
   692   The first two ensure that the equational system is always finite (number of equations
   693   and number of terms in each equation); the second makes sure the ``meaning'' of the 
   693   and number of terms in each equation); the second makes sure the `meaning' of the 
   694   equations is preserved under our transformations. The other properties are a bit more
   694   equations is preserved under our transformations. The other properties are a bit more
   695   technical, but are needed to get our proof through. Distinctness states that every
   695   technical, but are needed to get our proof through. Distinctness states that every
   696   equation in the system is distinct. Ardenable ensures that we can always
   696   equation in the system is distinct. Ardenable ensures that we can always
   697   apply the arden operation. 
   697   apply the arden operation. 
   698   The last property states that every @{text rhs} can only contain equivalence classes
   698   The last property states that every @{text rhs} can only contain equivalence classes
   878 
   878 
   879 section {* Conclusion and Related Work *}
   879 section {* Conclusion and Related Work *}
   880 
   880 
   881 text {*
   881 text {*
   882   In this paper we took the view that a regular language is one where there
   882   In this paper we took the view that a regular language is one where there
   883   exists a regular expression that matches all its strings. Regular
   883   exists a regular expression that matches all of its strings. Regular
   884   expressions can be conveniently defined as a datatype in a HOL-based theorem
   884   expressions can conveniently be defined as a datatype in a HOL-based theorem
   885   prover. For us it was therefore interesting to find out how far we can push
   885   prover. For us it was therefore interesting to find out how far we can push
   886   this point of view. 
   886   this point of view. 
   887 
   887 
   888   Having formalised the Myhill-Nerode theorem means we
   888   Having formalised the Myhill-Nerode theorem means we
   889   pushed quite far. Using this theorem we can obviously prove when a language
   889   pushed quite far. Using this theorem we can obviously prove when a language
   938   classes with the states of the automaton, then the most natural choice is to
   938   classes with the states of the automaton, then the most natural choice is to
   939   characterise each state with the set of strings starting from the initial
   939   characterise each state with the set of strings starting from the initial
   940   state leading up to that state. Usually, however, the states are characterised as the
   940   state leading up to that state. Usually, however, the states are characterised as the
   941   ones starting from that state leading to the terminal states.  The first
   941   ones starting from that state leading to the terminal states.  The first
   942   choice has consequences how the initial equational system is set up. We have
   942   choice has consequences how the initial equational system is set up. We have
   943   the $\lambda$-term on our ``initial state'', while Brzozowski has it on the
   943   the $\lambda$-term on our `initial state', while Brzozowski has it on the
   944   terminal states. This means we also need to reverse the direction of Arden's
   944   terminal states. This means we also need to reverse the direction of Arden's
   945   lemma.
   945   lemma.
   946 
   946 
   947   We briefly considered using the method Brzozowski presented in the Appendix
   947   We briefly considered using the method Brzozowski presented in the Appendix
   948   of~\cite{Brzozowski64} in order to prove the second direction of the
   948   of~\cite{Brzozowski64} in order to prove the second direction of the
   962 
   962 
   963   While regular expressions are convenient in formalisations, they have some
   963   While regular expressions are convenient in formalisations, they have some
   964   limitations. One is that there seems to be no notion of a minimal regular
   964   limitations. One is that there seems to be no notion of a minimal regular
   965   expression, like there is for automata. For an implementation of a simple
   965   expression, like there is for automata. For an implementation of a simple
   966   regular expression matcher, whose correctness has been formally
   966   regular expression matcher, whose correctness has been formally
   967   established, we refer the reader to \cite{OwensSlind08}.
   967   established, we refer the reader to Owens and Slind \cite{OwensSlind08}.
   968 
   968 
   969 *}
   969 *}
   970 
   970 
   971 
   971 
   972 (*<*)
   972 (*<*)