Paper/Paper.thy
changeset 113 ec774952190c
parent 112 62fdb4bf7239
child 114 c5eb5f3065ae
equal deleted inserted replaced
112:62fdb4bf7239 113:ec774952190c
   238   \hspace{7mm}
   238   \hspace{7mm}
   239   @{thm pow.simps(2)[THEN eq_reflection, where A1="A" and n1="n"]}
   239   @{thm pow.simps(2)[THEN eq_reflection, where A1="A" and n1="n"]}
   240   \end{center}
   240   \end{center}
   241 
   241 
   242   \noindent
   242   \noindent
   243   where @{text "@"} is the usual list-append operation. The Kleene-star of a language @{text A}
   243   where @{text "@"} is the list-append operation. The Kleene-star of a language @{text A}
   244   is defined as the union over all powers, namely @{thm Star_def}. In the paper
   244   is defined as the union over all powers, namely @{thm Star_def}. In the paper
   245   we will make use of the following properties of these constructions.
   245   we will make use of the following properties of these constructions.
   246   
   246   
   247   \begin{proposition}\label{langprops}\mbox{}\\
   247   \begin{proposition}\label{langprops}\mbox{}\\
   248   \begin{tabular}{@ {}ll}
   248   \begin{tabular}{@ {}ll}
   252   \end{tabular}
   252   \end{tabular}
   253   \end{proposition}
   253   \end{proposition}
   254 
   254 
   255   \noindent
   255   \noindent
   256   In @{text "(ii)"} we use the notation @{term "length s"} for the length of a
   256   In @{text "(ii)"} we use the notation @{term "length s"} for the length of a
   257   string.  This property states that if @{term "[] \<notin> A"} then the lengths of
   257   string; this property states that if @{term "[] \<notin> A"} then the lengths of
   258   the strings in @{term "A \<up> (Suc n)"} must be longer than @{text n}.  We omit
   258   the strings in @{term "A \<up> (Suc n)"} must be longer than @{text n}.  We omit
   259   the proofs for these properties, but invite the reader to consult our
   259   the proofs for these properties, but invite the reader to consult our
   260   formalisation.\footnote{Available at ???}
   260   formalisation.\footnote{Available at ???}
   261 
   261 
   262   The notation in Isabelle/HOL for the quotient of a language @{text A} according to an 
   262   The notation in Isabelle/HOL for the quotient of a language @{text A} according to an 
   563 
   563 
   564   \noindent
   564   \noindent
   565   This allows us to prove
   565   This allows us to prove
   566 
   566 
   567   \begin{lemma}\label{ardenable}
   567   \begin{lemma}\label{ardenable}
       
   568   Given an equation @{text "X = rhs"}.
   568   If @{text "X = \<Union>\<calL> ` rhs"},
   569   If @{text "X = \<Union>\<calL> ` rhs"},
   569   @{thm (prem 2) Arden_keeps_eq} and
   570   @{thm (prem 2) Arden_keeps_eq} and
   570   @{thm (prem 3) Arden_keeps_eq}, then
   571   @{thm (prem 3) Arden_keeps_eq}, then
   571   @{text "X = \<Union>\<calL> ` (Arden X rhs)"}
   572   @{text "X = \<Union>\<calL> ` (Arden X rhs)"}
   572   \end{lemma}
   573   \end{lemma}
   660   \noindent
   661   \noindent
   661   This principle states that given an invariant (which we will specify below) 
   662   This principle states that given an invariant (which we will specify below) 
   662   we can prove a property
   663   we can prove a property
   663   @{text "P"} involving @{const Solve}. For this we have to discharge the following
   664   @{text "P"} involving @{const Solve}. For this we have to discharge the following
   664   proof obligations: first the
   665   proof obligations: first the
   665   initial equational system satisfies the invariant; second that the iteration
   666   initial equational system satisfies the invariant; second the iteration
   666   step @{text "Iter"} preserves the the invariant as long as the condition @{term Cond} holds;
   667   step @{text "Iter"} preserves the the invariant as long as the condition @{term Cond} holds;
   667   third that @{text "Iter"} decreases the termination order, and fourth that
   668   third @{text "Iter"} decreases the termination order, and fourth that
   668   once the condition does not hold anymore then the property @{text P} must hold.
   669   once the condition does not hold anymore then the property @{text P} must hold.
   669 
   670 
   670   The property @{term P} in our proof will state that @{term "Solve X (Init (UNIV // \<approx>A))"}
   671   The property @{term P} in our proof will state that @{term "Solve X (Init (UNIV // \<approx>A))"}
   671   returns with a single equation @{text "X = xrhs"} for some @{text "xrhs"}, and
   672   returns with a single equation @{text "X = xrhs"} for some @{text "xrhs"}, and
   672   that this equational system still satisfies the invariant. In order to get
   673   that this equational system still satisfies the invariant. In order to get
   709 
   710 
   710   \begin{proof}
   711   \begin{proof}
   711   Finiteness is given by the assumption and the way how we set up the 
   712   Finiteness is given by the assumption and the way how we set up the 
   712   initial equational system. Soundness is proved in Lem.~\ref{inv}. Distinctness
   713   initial equational system. Soundness is proved in Lem.~\ref{inv}. Distinctness
   713   follows from the fact that the equivalence classes are disjoint. The ardenable
   714   follows from the fact that the equivalence classes are disjoint. The ardenable
   714   property also follows from the setup of the equational system, as does 
   715   property also follows from the setup of the initial equational system, as does 
   715   validity.\qed
   716   validity.\qed
   716   \end{proof}
   717   \end{proof}
       
   718 
       
   719   \noindent
       
   720   Next we show that @{text Iter} preserves the invariant.
   717 
   721 
   718   \begin{lemma}\label{iterone}
   722   \begin{lemma}\label{iterone}
   719   @{thm[mode=IfThen] iteration_step_invariant[where xrhs="rhs"]}
   723   @{thm[mode=IfThen] iteration_step_invariant[where xrhs="rhs"]}
   720   \end{lemma}
   724   \end{lemma}
   721 
   725 
   731   \end{center}
   735   \end{center}
   732 
   736 
   733   \noindent
   737   \noindent
   734   Finiteness is straightforward, as @{const Subst} and @{const Arden} operations 
   738   Finiteness is straightforward, as @{const Subst} and @{const Arden} operations 
   735   keep the equational system finite. These operation also preserve soundness 
   739   keep the equational system finite. These operation also preserve soundness 
   736   distinctness (we proved soundness for @{const Arden} in Lem.~\ref{ardenable}).
   740   and distinctness (we proved soundness for @{const Arden} in Lem.~\ref{ardenable}).
   737   The property Ardenable is clearly preserved because the append-operation
   741   The property ardenable is clearly preserved because the append-operation
   738   cannot make a regular expression to match the empty string. Validity is
   742   cannot make a regular expression to match the empty string. Validity is
   739   given because @{const Arden} removes an equivalence class from @{text yrhs}
   743   given because @{const Arden} removes an equivalence class from @{text yrhs}
   740   and then @{const Subst_all} removes @{text Y} from the equational system.
   744   and then @{const Subst_all} removes @{text Y} from the equational system.
   741   Having proved the implication above, we can replace @{text "ES"} with @{text "ES - {(Y, yrhs)}"}
   745   Having proved the implication above, we can replace @{text "ES"} with @{text "ES - {(Y, yrhs)}"}
   742   which matches with our proof-obligation of @{const "Subst_all"}. Since
   746   which matches with our proof-obligation of @{const "Subst_all"}. Since
   743   \mbox{@{term "ES = ES - {(Y, yrhs)} \<union> {(Y, yrhs)}"}}, we can use our assumption 
   747   \mbox{@{term "ES = ES - {(Y, yrhs)} \<union> {(Y, yrhs)}"}}, we can use our assumption 
   744   to complete the proof.\qed 
   748   to complete the proof.\qed 
   745   \end{proof}
   749   \end{proof}
   746 
   750 
       
   751   \noindent
       
   752   We also need the fact that @{text Iter} decreases the termination measure.
       
   753 
   747   \begin{lemma}\label{itertwo}
   754   \begin{lemma}\label{itertwo}
   748   @{thm[mode=IfThen] iteration_step_measure[simplified (no_asm), where xrhs="rhs"]}
   755   @{thm[mode=IfThen] iteration_step_measure[simplified (no_asm), where xrhs="rhs"]}
   749   \end{lemma}
   756   \end{lemma}
   750 
   757 
   751   \begin{proof}
   758   \begin{proof}
   755   that @{term "Y \<noteq> X"}. We further know that @{text "Remove ES Y yrhs"}
   762   that @{term "Y \<noteq> X"}. We further know that @{text "Remove ES Y yrhs"}
   756   removes the equation @{text "Y = yrhs"} from the system, and therefore 
   763   removes the equation @{text "Y = yrhs"} from the system, and therefore 
   757   the cardinality of @{const Iter} strictly decreases.\qed
   764   the cardinality of @{const Iter} strictly decreases.\qed
   758   \end{proof}
   765   \end{proof}
   759 
   766 
       
   767   \noindent
       
   768   This brings us to our property we want establish for @{text Solve}.
       
   769 
       
   770 
   760   \begin{lemma}
   771   \begin{lemma}
   761   If @{thm (prem 1) Solve} and @{thm (prem 2) Solve} then there exists
   772   If @{thm (prem 1) Solve} and @{thm (prem 2) Solve} then there exists
   762   a @{text rhs} such that  @{term "Solve X (Init (UNIV // \<approx>A)) = {(X, rhs)}"}
   773   a @{text rhs} such that  @{term "Solve X (Init (UNIV // \<approx>A)) = {(X, rhs)}"}
   763   and @{term "invariant {(X, rhs)}"}.
   774   and @{term "invariant {(X, rhs)}"}.
   764   \end{lemma}
   775   \end{lemma}
   766   \begin{proof} 
   777   \begin{proof} 
   767   In order to prove this lemma using \eqref{whileprinciple}, we have to use a slightly
   778   In order to prove this lemma using \eqref{whileprinciple}, we have to use a slightly
   768   stronger invariant since Lem.~\ref{iterone} and \ref{itertwo} have the precondition 
   779   stronger invariant since Lem.~\ref{iterone} and \ref{itertwo} have the precondition 
   769   that @{term "(X, rhs) \<in> ES"} for some @{text rhs}. This precondition is needed
   780   that @{term "(X, rhs) \<in> ES"} for some @{text rhs}. This precondition is needed
   770   in order to choose in the @{const Iter}-step an equation that is not \mbox{@{term "X = rhs"}}.
   781   in order to choose in the @{const Iter}-step an equation that is not \mbox{@{term "X = rhs"}}.
   771   Therefore our invariant is cannot be just @{term "invariant ES"}, but must be 
   782   Therefore our invariant cannot be just @{term "invariant ES"}, but must be 
   772   @{term "invariant ES \<and> (\<exists>rhs. (X, rhs) \<in> ES)"}. By assumption 
   783   @{term "invariant ES \<and> (\<exists>rhs. (X, rhs) \<in> ES)"}. By assumption 
   773   @{thm (prem 2) Solve} and Lem.~\ref{invzero}, the more general invariant holds for
   784   @{thm (prem 2) Solve} and Lem.~\ref{invzero}, the more general invariant holds for
   774   the initial equational system. This is premise 1 of~\eqref{whileprinciple}.
   785   the initial equational system. This is premise 1 of~\eqref{whileprinciple}.
   775   Premise 2 is given by Lem.~\ref{iterone} and the fact that @{const Iter} might
   786   Premise 2 is given by Lem.~\ref{iterone} and the fact that @{const Iter} might
   776   modify the @{text rhs} in the equation @{term "X = rhs"}, but does not remove it.
   787   modify the @{text rhs} in the equation @{term "X = rhs"}, but does not remove it.
   777   Premise 3 of~\eqref{whileprinciple} is by Lem.~\ref{itertwo}. Now in premise 4
   788   Premise 3 of~\eqref{whileprinciple} is by Lem.~\ref{itertwo}. Now in premise 4
   778   we like to show that there exists a @{text rhs} such that @{term "ES = {(X, rhs)}"}
   789   we like to show that there exists a @{text rhs} such that @{term "ES = {(X, rhs)}"}
   779   and that @{text "invariant {(X, rhs)}"} holds, provided the condition @{text "Cond"}
   790   and that @{text "invariant {(X, rhs)}"} holds, provided the condition @{text "Cond"}
   780   does not hols. By the stronger invariant we know there exists such a @{text "rhs"}
   791   does not holds. By the stronger invariant we know there exists such a @{text "rhs"}
   781   with @{term "(X, rhs) \<in> ES"}. Because @{text Cond} is not true, we know the cardinality
   792   with @{term "(X, rhs) \<in> ES"}. Because @{text Cond} is not true, we know the cardinality
   782   of @{text ES} is @{text 1}. This means @{text "ES"} must actually be the equation @{text "X = rhs"},
   793   of @{text ES} is @{text 1}. This means @{text "ES"} must actually be the equation @{text "X = rhs"},
   783   for which the invariant holds. This allows us to conclude that 
   794   for which the invariant holds. This allows us to conclude that 
   784   @{term "Solve X (Init (UNIV // \<approx>A)) = {(X, rhs)}"} and @{term "invariant {(X, rhs)}"} hold.\qed
   795   @{term "Solve X (Init (UNIV // \<approx>A)) = {(X, rhs)}"} and @{term "invariant {(X, rhs)}"} hold,
       
   796   as needed.\qed
   785   \end{proof}
   797   \end{proof}
   786 
   798 
   787   \noindent
   799   \noindent
   788   With this lemma in place we can show that for every equivalence class in @{term "UNIV // \<approx>A"}
   800   With this lemma in place we can show that for every equivalence class in @{term "UNIV // \<approx>A"}
   789   there exists a regular expression.
   801   there exists a regular expression.
   799   know @{text "X = \<Union>\<calL> ` rhs"}. We further know that
   811   know @{text "X = \<Union>\<calL> ` rhs"}. We further know that
   800   this is equal to \mbox{@{text "\<Union>\<calL> ` (Arden X rhs)"}} using the properties of the 
   812   this is equal to \mbox{@{text "\<Union>\<calL> ` (Arden X rhs)"}} using the properties of the 
   801   invariant and Lem.\ref{ardenable}. Using the validity property for the equation @{text "X = rhs"},
   813   invariant and Lem.\ref{ardenable}. Using the validity property for the equation @{text "X = rhs"},
   802   we can infer that @{term "rhss rhs \<subseteq> {X}"} and because the arden operation
   814   we can infer that @{term "rhss rhs \<subseteq> {X}"} and because the arden operation
   803   removes that @{text X} from @{text rhs}, that @{term "rhss (Arden X rhs) = {}"}.
   815   removes that @{text X} from @{text rhs}, that @{term "rhss (Arden X rhs) = {}"}.
   804   That means @{term "Arden X rhs"} can only consist of terms of the form @{term "Lam r"}.
   816   This means the right-hand side @{term "Arden X rhs"} can only consist of terms of the form @{term "Lam r"}.
   805   So we can collect those (finitely many) regular expressions and have @{term "X = L (\<Uplus>rs)"}.
   817   So we can collect those (finitely many) regular expressions and have @{term "X = L (\<Uplus>rs)"}.
   806   With this we can conclude the proof.\qed
   818   With this we can conclude the proof.\qed
   807   \end{proof}
   819   \end{proof}
   808 
   820 
   809   \noindent
   821   \noindent
   879   equivalence classes generated by the Myhill-Nerode relation (this is usually
   891   equivalence classes generated by the Myhill-Nerode relation (this is usually
   880   the purpose of the pumping lemma \cite{Kozen97}).  We can also use it to
   892   the purpose of the pumping lemma \cite{Kozen97}).  We can also use it to
   881   establish the standard textbook results about closure properties of regular
   893   establish the standard textbook results about closure properties of regular
   882   languages. Interesting is the case of closure under complement, because
   894   languages. Interesting is the case of closure under complement, because
   883   it seems difficult to construct a regular expression for the complement
   895   it seems difficult to construct a regular expression for the complement
   884   language by direct means. However the existence can be easily proved using
   896   language by direct means. However the existence of such a regular expression
   885   the Myhill-Nerode theorem since clearly
   897   can be easily proved using the Myhill-Nerode theorem since 
   886 
   898 
   887   \begin{center}
   899   \begin{center}
   888   @{term "s\<^isub>1 \<approx>A s\<^isub>2"} if and only if @{term "s\<^isub>1 \<approx>(-A) s\<^isub>2"}
   900   @{term "s\<^isub>1 \<approx>A s\<^isub>2"} if and only if @{term "s\<^isub>1 \<approx>(-A) s\<^isub>2"}
   889   \end{center}
   901   \end{center}
   890  
   902  
   891   \noindent
   903   \noindent
   892   holds for any strings @{text "s\<^isub>1"} and @{text
   904   holds for any strings @{text "s\<^isub>1"} and @{text
   893   "s\<^isub>2"}. Therefore @{text A} and @{term "-A"} give rise to the same
   905   "s\<^isub>2"}. Therefore @{text A} and @{term "-A"} give rise to the same
   894   partitions.  From the closure under complementation follows also the closure
   906   partitions. 
   895   under intersection and set difference by some simple set calculations.
       
   896   Proving the same result via automata would be quite involved. It includes the
   907   Proving the same result via automata would be quite involved. It includes the
   897   steps: regular expression @{text "\<Rightarrow>"} non-deterministic automaton @{text
   908   steps: regular expression @{text "\<Rightarrow>"} non-deterministic automaton @{text
   898   "\<Rightarrow>"} deterministic automaton @{text "\<Rightarrow>"} complement automaton @{text "\<Rightarrow>"}
   909   "\<Rightarrow>"} deterministic automaton @{text "\<Rightarrow>"} complement automaton @{text "\<Rightarrow>"}
   899   regular expression.
   910   regular expression.
   900 
   911 
   901   Our formalisation consists of ??? lines of Isar code for the first
   912   Our formalisation consists of ??? lines of Isabelle/Isar code for the first
   902   direction and ??? for the second. While this might be seen as too large
   913   direction and ??? for the second. While this might be seen as too large to
   903   to count as a concise proof pearl, this should be seen in the context 
   914   count as a concise proof pearl, this should be seen in the context of the
   904   of the work done by Constable at al \cite{Constable00} who formalised
   915   work done by Constable at al \cite{Constable00} who formalised the
   905   the Myhill-Nerode theorem in Nuprl using automata. They write that 
   916   Myhill-Nerode theorem in Nuprl using automata. This is the most 
   906   their four-member team needed something on the magnitute of 18 months 
   917   They write that their
   907   to formalise the Myhill-Nerode theorem. Our estimate is that we needed
   918   four-member team needed something on the magnitute of 18 months to formalise
   908   approximately 3 months for our fomalisation and this included the time
   919   the Myhill-Nerode theorem. The estimate for our formalisation is that we
   909   to find our proof arguments, as we could not find them in the literature.
   920   needed approximately 3 months and this included the time to find our proof
   910   So for us the formalisation was not the bottleneck. It is hard for us 
   921   arguments. Unlike Constable et al, who were able to follow the proofs from
   911   to gauge the size of a formalisation in Nurpl, but from what is shown in
   922   \cite{???}, we had to find our own arguments.  So for us the formalisation
   912   the Nuprl Math Library their development seems substantially larger.
   923   was not the bottleneck. It is hard to gauge the size of a formalisation in
       
   924   Nurpl, but from what is shown in the Nuprl Math Library about their
       
   925   development it seems substantially larger than ours. The code of
       
   926   ours can be found under
       
   927 
       
   928   \begin{center}
       
   929   ???
       
   930   \end{center}
       
   931 
   913 
   932 
   914   Our proof of the first direction is very much inspired by \emph{Brzozowski's
   933   Our proof of the first direction is very much inspired by \emph{Brzozowski's
   915   algebraic mehod} used to convert a finite automaton to a regular
   934   algebraic mehod} used to convert a finite automaton to a regular
   916   expression. The close connection can be seen by considering the equivalence
   935   expression \cite{Brzozowski64}. The close connection can be seen by considering the equivalence
   917   classes as the states of the minimal automaton for the regular language.
   936   classes as the states of the minimal automaton for the regular language.
   918   However there are some subtle differences. If we identify equivalence
   937   However there are some subtle differences. If we identify equivalence
   919   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
   920   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
   921   state leading up to that state. Usually the states are characterised as the
   940   state leading up to that state. Usually, however, the states are characterised as the
   922   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
   923   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
   924   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
   925   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
   926   lemma.
   945   lemma.
   927 
   946 
   928   We briefly considered using the method Brzozowski presented in the Appendix
   947   We briefly considered using the method Brzozowski presented in the Appendix
   929   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
   930   Myhill-Nerode theorem. There he calculates the derivatives for regular
   949   Myhill-Nerode theorem. There he calculates the derivatives for regular
   931   expressions and shows that there can be only finitely many of them. We could
   950   expressions and shows that there can be only finitely many of them. We could
   932   use as the tag of a string @{text s} the derivative of a regular expression
   951   use as the tag of a string @{text s} the derivative of a regular expression
   933   generated with respect to @{text s}.  Using the fact that two strings are
   952   generated with respect to @{text s}.  Using the fact that two strings are
   934   Myhill-Nerode related whenever their derivative is the same together with
   953   Myhill-Nerode related whenever their derivative is the same together with
   935   the fact that there are only finitely many derivatives for a regular
   954   the fact that there are only finitely many derivatives for a regular
   936   expression would give us the same argument. However it seems not so easy to
   955   expression would give us the same argument as ours. However it seems not so easy to
   937   calculate the derivatives and then to count them. Therefore we preferred our
   956   calculate the derivatives and then to count them. Therefore we preferred our
   938   direct method of using tagging-functions involving equivalence classes. This
   957   direct method of using tagging-functions involving equivalence classes. This
   939   is also where our method shines, because we can completely side-step the
   958   is also where our method shines, because we can completely side-step the
   940   standard argument \cite{Kozen97} where automata need to be composed, which
   959   standard argument \cite{Kozen97} where automata need to be composed, which
   941   is not so convenient to formalise in a HOL-based theorem prover.
   960   as stated in the Introduction is not so convenient to formalise in a 
       
   961   HOL-based theorem prover.
   942 
   962 
   943   While regular expressions are convenient in formalisations, they have some
   963   While regular expressions are convenient in formalisations, they have some
   944   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
   945   expression, like there is a notion of a minimal automaton for a regular
   965   expression, like there is for automata. For an implementation of a simple
   946   expression.
   966   regular expression matcher, whose correctness has been formally
       
   967   established, we refer the reader to \cite{OwensSlind08}.
   947 
   968 
   948 *}
   969 *}
   949 
   970 
   950 
   971 
   951 (*<*)
   972 (*<*)