Myhill_1.thy
changeset 42 f809cb54de4e
child 43 cb4403fabda7
equal deleted inserted replaced
41:dbbc7989e753 42:f809cb54de4e
       
     1 theory Myhill_1
       
     2   imports Main List_Prefix Prefix_subtract
       
     3 begin
       
     4 
       
     5 (*
       
     6 text {*
       
     7      \begin{figure}
       
     8     \centering
       
     9     \scalebox{0.95}{
       
    10     \begin{tikzpicture}[->,>=latex,shorten >=1pt,auto,node distance=1.2cm, semithick]
       
    11         \node[state,initial] (n1)                   {$1$};
       
    12         \node[state,accepting] (n2) [right = 10em of n1]   {$2$};
       
    13 
       
    14         \path (n1) edge [bend left] node {$0$} (n2)
       
    15             (n1) edge [loop above] node{$1$} (n1)
       
    16             (n2) edge [loop above] node{$0$} (n2)
       
    17             (n2) edge [bend left]  node {$1$} (n1)
       
    18             ;
       
    19     \end{tikzpicture}}
       
    20     \caption{An example automaton (or partition)}\label{fig:example_automata}
       
    21     \end{figure}
       
    22 *}
       
    23 
       
    24 *)
       
    25 
       
    26 
       
    27 section {* Preliminary definitions *}
       
    28 
       
    29 text {* Sequential composition of two languages @{text "L1"} and @{text "L2"} *}
       
    30 definition Seq :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ;; _" [100,100] 100)
       
    31 where 
       
    32   "L1 ;; L2 = {s1 @ s2 | s1 s2. s1 \<in> L1 \<and> s2 \<in> L2}"
       
    33 
       
    34 text {* Transitive closure of language @{text "L"}. *}
       
    35 inductive_set
       
    36   Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102)
       
    37   for L :: "string set"
       
    38 where
       
    39   start[intro]: "[] \<in> L\<star>"
       
    40 | step[intro]:  "\<lbrakk>s1 \<in> L; s2 \<in> L\<star>\<rbrakk> \<Longrightarrow> s1@s2 \<in> L\<star>" 
       
    41 
       
    42 text {* Some properties of operator @{text ";;"}.*}
       
    43 
       
    44 lemma seq_union_distrib:
       
    45   "(A \<union> B) ;; C = (A ;; C) \<union> (B ;; C)"
       
    46 by (auto simp:Seq_def)
       
    47 
       
    48 lemma seq_intro:
       
    49   "\<lbrakk>x \<in> A; y \<in> B\<rbrakk> \<Longrightarrow> x @ y \<in> A ;; B "
       
    50 by (auto simp:Seq_def)
       
    51 
       
    52 lemma seq_assoc:
       
    53   "(A ;; B) ;; C = A ;; (B ;; C)"
       
    54 apply(auto simp:Seq_def)
       
    55 apply blast
       
    56 by (metis append_assoc)
       
    57 
       
    58 lemma star_intro1[rule_format]: "x \<in> lang\<star> \<Longrightarrow> \<forall> y. y \<in> lang\<star> \<longrightarrow> x @ y \<in> lang\<star>"
       
    59 by (erule Star.induct, auto)
       
    60 
       
    61 lemma star_intro2: "y \<in> lang \<Longrightarrow> y \<in> lang\<star>"
       
    62 by (drule step[of y lang "[]"], auto simp:start)
       
    63 
       
    64 lemma star_intro3[rule_format]: 
       
    65   "x \<in> lang\<star> \<Longrightarrow> \<forall>y . y \<in> lang \<longrightarrow> x @ y \<in> lang\<star>"
       
    66 by (erule Star.induct, auto intro:star_intro2)
       
    67 
       
    68 lemma star_decom: 
       
    69   "\<lbrakk>x \<in> lang\<star>; x \<noteq> []\<rbrakk> \<Longrightarrow>(\<exists> a b. x = a @ b \<and> a \<noteq> [] \<and> a \<in> lang \<and> b \<in> lang\<star>)"
       
    70 by (induct x rule: Star.induct, simp, blast)
       
    71 
       
    72 lemma star_decom': 
       
    73   "\<lbrakk>x \<in> lang\<star>; x \<noteq> []\<rbrakk> \<Longrightarrow> \<exists>a b. x = a @ b \<and> a \<in> lang\<star> \<and> b \<in> lang"
       
    74 apply (induct x rule:Star.induct, simp)
       
    75 apply (case_tac "s2 = []")
       
    76 apply (rule_tac x = "[]" in exI, rule_tac x = s1 in exI, simp add:start)
       
    77 apply (simp, (erule exE| erule conjE)+)
       
    78 by (rule_tac x = "s1 @ a" in exI, rule_tac x = b in exI, simp add:step)
       
    79 
       
    80 text {* Ardens lemma expressed at the level of language, rather than the level of regular expression. *}
       
    81 
       
    82 theorem ardens_revised:
       
    83   assumes nemp: "[] \<notin> A"
       
    84   shows "(X = X ;; A \<union> B) \<longleftrightarrow> (X = B ;; A\<star>)"
       
    85 proof
       
    86   assume eq: "X = B ;; A\<star>"
       
    87   have "A\<star> =  {[]} \<union> A\<star> ;; A" 
       
    88     by (auto simp:Seq_def star_intro3 star_decom')  
       
    89   then have "B ;; A\<star> = B ;; ({[]} \<union> A\<star> ;; A)" 
       
    90     unfolding Seq_def by simp
       
    91   also have "\<dots> = B \<union> B ;; (A\<star> ;; A)"  
       
    92     unfolding Seq_def by auto
       
    93   also have "\<dots> = B \<union> (B ;; A\<star>) ;; A" 
       
    94     by (simp only:seq_assoc)
       
    95   finally show "X = X ;; A \<union> B" 
       
    96     using eq by blast 
       
    97 next
       
    98   assume eq': "X = X ;; A \<union> B"
       
    99   hence c1': "\<And> x. x \<in> B \<Longrightarrow> x \<in> X" 
       
   100     and c2': "\<And> x y. \<lbrakk>x \<in> X; y \<in> A\<rbrakk> \<Longrightarrow> x @ y \<in> X" 
       
   101     using Seq_def by auto
       
   102   show "X = B ;; A\<star>" 
       
   103   proof
       
   104     show "B ;; A\<star> \<subseteq> X"
       
   105     proof-
       
   106       { fix x y
       
   107         have "\<lbrakk>y \<in> A\<star>; x \<in> X\<rbrakk> \<Longrightarrow> x @ y \<in> X "
       
   108           apply (induct arbitrary:x rule:Star.induct, simp)
       
   109           by (auto simp only:append_assoc[THEN sym] dest:c2')
       
   110       } thus ?thesis using c1' by (auto simp:Seq_def) 
       
   111     qed
       
   112   next
       
   113     show "X \<subseteq> B ;; A\<star>"
       
   114     proof-
       
   115       { fix x 
       
   116         have "x \<in> X \<Longrightarrow> x \<in> B ;; A\<star>"
       
   117         proof (induct x taking:length rule:measure_induct)
       
   118           fix z
       
   119           assume hyps: 
       
   120             "\<forall>y. length y < length z \<longrightarrow> y \<in> X \<longrightarrow> y \<in> B ;; A\<star>" 
       
   121             and z_in: "z \<in> X"
       
   122           show "z \<in> B ;; A\<star>"
       
   123           proof (cases "z \<in> B")
       
   124             case True thus ?thesis by (auto simp:Seq_def start)
       
   125           next
       
   126             case False hence "z \<in> X ;; A" using eq' z_in by auto
       
   127             then obtain za zb where za_in: "za \<in> X" 
       
   128               and zab: "z = za @ zb \<and> zb \<in> A" and zbne: "zb \<noteq> []" 
       
   129               using nemp unfolding Seq_def by blast
       
   130             from zbne zab have "length za < length z" by auto
       
   131             with za_in hyps have "za \<in> B ;; A\<star>" by blast
       
   132             hence "za @ zb \<in> B ;; A\<star>" using zab 
       
   133               by (clarsimp simp:Seq_def, blast dest:star_intro3)
       
   134             thus ?thesis using zab by simp       
       
   135           qed
       
   136         qed 
       
   137       } thus ?thesis by blast
       
   138     qed
       
   139   qed
       
   140 qed
       
   141 
       
   142 
       
   143 text {* The syntax of regular expressions is defined by the datatype @{text "rexp"}. *}
       
   144 datatype rexp =
       
   145   NULL
       
   146 | EMPTY
       
   147 | CHAR char
       
   148 | SEQ rexp rexp
       
   149 | ALT rexp rexp
       
   150 | STAR rexp
       
   151 
       
   152 
       
   153 text {* 
       
   154   The following @{text "L"} is an overloaded operator, where @{text "L(x)"} evaluates to 
       
   155   the language represented by the syntactic object @{text "x"}.
       
   156 *}
       
   157 consts L:: "'a \<Rightarrow> string set"
       
   158 
       
   159 
       
   160 text {* 
       
   161   The @{text "L(rexp)"} for regular expression @{text "rexp"} is defined by the 
       
   162   following overloading function @{text "L_rexp"}.
       
   163 *}
       
   164 overloading L_rexp \<equiv> "L::  rexp \<Rightarrow> string set"
       
   165 begin
       
   166 fun
       
   167   L_rexp :: "rexp \<Rightarrow> string set"
       
   168 where
       
   169     "L_rexp (NULL) = {}"
       
   170   | "L_rexp (EMPTY) = {[]}"
       
   171   | "L_rexp (CHAR c) = {[c]}"
       
   172   | "L_rexp (SEQ r1 r2) = (L_rexp r1) ;; (L_rexp r2)"
       
   173   | "L_rexp (ALT r1 r2) = (L_rexp r1) \<union> (L_rexp r2)"
       
   174   | "L_rexp (STAR r) = (L_rexp r)\<star>"
       
   175 end
       
   176 
       
   177 text {*
       
   178   To obtain equational system out of finite set of equivalent classes, a fold operation
       
   179   on finite set @{text "folds"} is defined. The use of @{text "SOME"} makes @{text "fold"}
       
   180   more robust than the @{text "fold"} in Isabelle library. The expression @{text "folds f"}
       
   181   makes sense when @{text "f"} is not @{text "associative"} and @{text "commutitive"},
       
   182   while @{text "fold f"} does not.  
       
   183 *}
       
   184 
       
   185 definition 
       
   186   folds :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b"
       
   187 where
       
   188   "folds f z S \<equiv> SOME x. fold_graph f z S x"
       
   189 
       
   190 text {* 
       
   191   The following lemma assures that the arbitrary choice made by the @{text "SOME"} in @{text "folds"}
       
   192   does not affect the @{text "L"}-value of the resultant regular expression. 
       
   193   *}
       
   194 lemma folds_alt_simp [simp]:
       
   195   "finite rs \<Longrightarrow> L (folds ALT NULL rs) = \<Union> (L ` rs)"
       
   196 apply (rule set_ext, simp add:folds_def)
       
   197 apply (rule someI2_ex, erule finite_imp_fold_graph)
       
   198 by (erule fold_graph.induct, auto)
       
   199 
       
   200 (* Just a technical lemma. *)
       
   201 lemma [simp]:
       
   202   shows "(x, y) \<in> {(x, y). P x y} \<longleftrightarrow> P x y"
       
   203 by simp
       
   204 
       
   205 text {*
       
   206   @{text "\<approx>L"} is an equivalent class defined by language @{text "Lang"}.
       
   207 *}
       
   208 definition
       
   209   str_eq_rel ("\<approx>_")
       
   210 where
       
   211   "\<approx>Lang \<equiv> {(x, y).  (\<forall>z. x @ z \<in> Lang \<longleftrightarrow> y @ z \<in> Lang)}"
       
   212 
       
   213 text {* 
       
   214   Among equivlant clases of @{text "\<approx>Lang"}, the set @{text "finals(Lang)"} singles out 
       
   215   those which contains strings from @{text "Lang"}.
       
   216 *}
       
   217 
       
   218 definition 
       
   219    "finals Lang \<equiv> {\<approx>Lang `` {x} | x . x \<in> Lang}"
       
   220 
       
   221 text {* 
       
   222   The following lemma show the relationshipt between @{text "finals(Lang)"} and @{text "Lang"}.
       
   223 *}
       
   224 lemma lang_is_union_of_finals: 
       
   225   "Lang = \<Union> finals(Lang)"
       
   226 proof 
       
   227   show "Lang \<subseteq> \<Union> (finals Lang)"
       
   228   proof
       
   229     fix x
       
   230     assume "x \<in> Lang"   
       
   231     thus "x \<in> \<Union> (finals Lang)"
       
   232       apply (simp add:finals_def, rule_tac x = "(\<approx>Lang) `` {x}" in exI)
       
   233       by (auto simp:Image_def str_eq_rel_def)    
       
   234   qed
       
   235 next
       
   236   show "\<Union> (finals Lang) \<subseteq> Lang"
       
   237     apply (clarsimp simp:finals_def str_eq_rel_def)
       
   238     by (drule_tac x = "[]" in spec, auto)
       
   239 qed
       
   240 
       
   241 section {* Direction @{text "finite partition \<Rightarrow> regular language"}*}
       
   242 
       
   243 text {* 
       
   244   The relationship between equivalent classes can be described by an
       
   245   equational system.
       
   246   For example, in equational system \eqref{example_eqns},  $X_0, X_1$ are equivalent 
       
   247   classes. The first equation says every string in $X_0$ is obtained either by
       
   248   appending one $b$ to a string in $X_0$ or by appending one $a$ to a string in
       
   249   $X_1$ or just be an empty string (represented by the regular expression $\lambda$). Similary,
       
   250   the second equation tells how the strings inside $X_1$ are composed.
       
   251   \begin{equation}\label{example_eqns}
       
   252     \begin{aligned}
       
   253       X_0 & = X_0 b + X_1 a + \lambda \\
       
   254       X_1 & = X_0 a + X_1 b
       
   255     \end{aligned}
       
   256   \end{equation}
       
   257   The summands on the right hand side is represented by the following data type
       
   258   @{text "rhs_item"}, mnemonic for 'right hand side item'.
       
   259   Generally, there are two kinds of right hand side items, one kind corresponds to
       
   260   pure regular expressions, like the $\lambda$ in \eqref{example_eqns}, the other kind corresponds to
       
   261   transitions from one one equivalent class to another, like the $X_0 b, X_1 a$ etc.
       
   262   *}
       
   263 
       
   264 datatype rhs_item = 
       
   265    Lam "rexp"                           (* Lambda *)
       
   266  | Trn "(string set)" "rexp"              (* Transition *)
       
   267 
       
   268 text {*
       
   269   In this formalization, pure regular expressions like $\lambda$ is 
       
   270   repsented by @{text "Lam(EMPTY)"}, while transitions like $X_0 a$ is represented by $Trn~X_0~(CHAR~a)$.
       
   271   *}
       
   272 
       
   273 text {*
       
   274   The functions @{text "the_r"} and @{text "the_Trn"} are used to extract
       
   275   subcomponents from right hand side items.
       
   276   *}
       
   277 
       
   278 fun the_r :: "rhs_item \<Rightarrow> rexp"
       
   279 where "the_r (Lam r) = r"
       
   280 
       
   281 fun the_Trn:: "rhs_item \<Rightarrow> (string set \<times> rexp)"
       
   282 where "the_Trn (Trn Y r) = (Y, r)"
       
   283 
       
   284 text {*
       
   285   Every right hand side item @{text "itm"} defines a string set given 
       
   286   @{text "L(itm)"}, defined as:
       
   287 *}
       
   288 overloading L_rhs_e \<equiv> "L:: rhs_item \<Rightarrow> string set"
       
   289 begin
       
   290   fun L_rhs_e:: "rhs_item \<Rightarrow> string set"
       
   291   where
       
   292      "L_rhs_e (Lam r) = L r" |
       
   293      "L_rhs_e (Trn X r) = X ;; L r"
       
   294 end
       
   295 
       
   296 text {*
       
   297   The right hand side of every equation is represented by a set of
       
   298   items. The string set defined by such a set @{text "itms"} is given
       
   299   by @{text "L(itms)"}, defined as:
       
   300 *}
       
   301 
       
   302 overloading L_rhs \<equiv> "L:: rhs_item set \<Rightarrow> string set"
       
   303 begin
       
   304    fun L_rhs:: "rhs_item set \<Rightarrow> string set"
       
   305    where "L_rhs rhs = \<Union> (L ` rhs)"
       
   306 end
       
   307 
       
   308 text {* 
       
   309   Given a set of equivalent classses @{text "CS"} and one equivalent class @{text "X"} among
       
   310   @{text "CS"}, the term @{text "init_rhs CS X"} is used to extract the right hand side of
       
   311   the equation describing the formation of @{text "X"}. The definition of @{text "init_rhs"}
       
   312   is:
       
   313   *}
       
   314 
       
   315 definition
       
   316   "init_rhs CS X \<equiv>  
       
   317       if ([] \<in> X) then 
       
   318           {Lam(EMPTY)} \<union> {Trn Y (CHAR c) | Y c. Y \<in> CS \<and> Y ;; {[c]} \<subseteq> X}
       
   319       else 
       
   320           {Trn Y (CHAR c)| Y c. Y \<in> CS \<and> Y ;; {[c]} \<subseteq> X}"
       
   321 
       
   322 text {*
       
   323   In the definition of @{text "init_rhs"}, the term 
       
   324   @{text "{Trn Y (CHAR c)| Y c. Y \<in> CS \<and> Y ;; {[c]} \<subseteq> X}"} appearing on both branches
       
   325   describes the formation of strings in @{text "X"} out of transitions, while 
       
   326   the term @{text "{Lam(EMPTY)}"} describes the empty string which is intrinsically contained in
       
   327   @{text "X"} rather than by transition. This @{text "{Lam(EMPTY)}"} corresponds to 
       
   328   the $\lambda$ in \eqref{example_eqns}.
       
   329 
       
   330   With the help of @{text "init_rhs"}, the equitional system descrbing the formation of every
       
   331   equivalent class inside @{text "CS"} is given by the following @{text "eqs(CS)"}.
       
   332   *}
       
   333 
       
   334 definition "eqs CS \<equiv> {(X, init_rhs CS X) | X.  X \<in> CS}"
       
   335 (************ arden's lemma variation ********************)
       
   336 
       
   337 text {* 
       
   338   The following @{text "items_of rhs X"} returns all @{text "X"}-items in @{text "rhs"}.
       
   339   *}
       
   340 definition
       
   341   "items_of rhs X \<equiv> {Trn X r | r. (Trn X r) \<in> rhs}"
       
   342 
       
   343 text {* 
       
   344   The following @{text "rexp_of rhs X"} combines all regular expressions in @{text "X"}-items
       
   345   using @{text "ALT"} to form a single regular expression. 
       
   346   It will be used later to implement @{text "arden_variate"} and @{text "rhs_subst"}.
       
   347   *}
       
   348 
       
   349 definition 
       
   350   "rexp_of rhs X \<equiv> folds ALT NULL ((snd o the_Trn) ` items_of rhs X)"
       
   351 
       
   352 text {* 
       
   353   The following @{text "lam_of rhs"} returns all pure regular expression items in @{text "rhs"}.
       
   354   *}
       
   355 
       
   356 definition
       
   357   "lam_of rhs \<equiv> {Lam r | r. Lam r \<in> rhs}"
       
   358 
       
   359 text {*
       
   360   The following @{text "rexp_of_lam rhs"} combines pure regular expression items in @{text "rhs"}
       
   361   using @{text "ALT"} to form a single regular expression. 
       
   362   When all variables inside @{text "rhs"} are eliminated, @{text "rexp_of_lam rhs"}
       
   363   is used to compute compute the regular expression corresponds to @{text "rhs"}.
       
   364   *}
       
   365 
       
   366 definition
       
   367   "rexp_of_lam rhs \<equiv> folds ALT NULL (the_r ` lam_of rhs)"
       
   368 
       
   369 text {*
       
   370   The following @{text "attach_rexp rexp' itm"} attach 
       
   371   the regular expression @{text "rexp'"} to
       
   372   the right of right hand side item @{text "itm"}.
       
   373   *}
       
   374 
       
   375 fun attach_rexp :: "rexp \<Rightarrow> rhs_item \<Rightarrow> rhs_item"
       
   376 where
       
   377   "attach_rexp rexp' (Lam rexp)   = Lam (SEQ rexp rexp')"
       
   378 | "attach_rexp rexp' (Trn X rexp) = Trn X (SEQ rexp rexp')"
       
   379 
       
   380 text {* 
       
   381   The following @{text "append_rhs_rexp rhs rexp"} attaches 
       
   382   @{text "rexp"} to every item in @{text "rhs"}.
       
   383   *}
       
   384 
       
   385 definition
       
   386   "append_rhs_rexp rhs rexp \<equiv> (attach_rexp rexp) ` rhs"
       
   387 
       
   388 text {*
       
   389   With the help of the two functions immediately above, Ardens'
       
   390   transformation on right hand side @{text "rhs"} is implemented
       
   391   by the following function @{text "arden_variate X rhs"}.
       
   392   After this transformation, the recursive occurent of @{text "X"}
       
   393   in @{text "rhs"} will be eliminated, while the 
       
   394   string set defined by @{text "rhs"} is kept unchanged.
       
   395   *}
       
   396 definition 
       
   397   "arden_variate X rhs \<equiv> 
       
   398         append_rhs_rexp (rhs - items_of rhs X) (STAR (rexp_of rhs X))"
       
   399 
       
   400 
       
   401 (*********** substitution of ES *************)
       
   402 
       
   403 text {* 
       
   404   Suppose the equation defining @{text "X"} is $X = xrhs$,
       
   405   the purpose of @{text "rhs_subst"} is to substitute all occurences of @{text "X"} in
       
   406   @{text "rhs"} by @{text "xrhs"}.
       
   407   A litte thought may reveal that the final result
       
   408   should be: first append $(a_1 | a_2 | \ldots | a_n)$ to every item of @{text "xrhs"} and then
       
   409   union the result with all non-@{text "X"}-items of @{text "rhs"}.
       
   410  *}
       
   411 definition 
       
   412   "rhs_subst rhs X xrhs \<equiv> 
       
   413         (rhs - (items_of rhs X)) \<union> (append_rhs_rexp xrhs (rexp_of rhs X))"
       
   414 
       
   415 text {*
       
   416   Suppose the equation defining @{text "X"} is $X = xrhs$, the follwing
       
   417   @{text "eqs_subst ES X xrhs"} substitute @{text "xrhs"} into every equation
       
   418   of the equational system @{text "ES"}.
       
   419   *}
       
   420 
       
   421 definition
       
   422   "eqs_subst ES X xrhs \<equiv> {(Y, rhs_subst yrhs X xrhs) | Y yrhs. (Y, yrhs) \<in> ES}"
       
   423 
       
   424 text {*
       
   425   The computation of regular expressions for equivalent classes is accomplished
       
   426   using a iteration principle given by the following lemma.
       
   427   *}
       
   428 
       
   429 lemma wf_iter [rule_format]: 
       
   430   fixes f
       
   431   assumes step: "\<And> e. \<lbrakk>P e; \<not> Q e\<rbrakk> \<Longrightarrow> (\<exists> e'. P e' \<and>  (f(e'), f(e)) \<in> less_than)"
       
   432   shows pe:     "P e \<longrightarrow> (\<exists> e'. P e' \<and>  Q e')"
       
   433 proof(induct e rule: wf_induct 
       
   434            [OF wf_inv_image[OF wf_less_than, where f = "f"]], clarify)
       
   435   fix x 
       
   436   assume h [rule_format]: 
       
   437     "\<forall>y. (y, x) \<in> inv_image less_than f \<longrightarrow> P y \<longrightarrow> (\<exists>e'. P e' \<and> Q e')"
       
   438     and px: "P x"
       
   439   show "\<exists>e'. P e' \<and> Q e'"
       
   440   proof(cases "Q x")
       
   441     assume "Q x" with px show ?thesis by blast
       
   442   next
       
   443     assume nq: "\<not> Q x"
       
   444     from step [OF px nq]
       
   445     obtain e' where pe': "P e'" and ltf: "(f e', f x) \<in> less_than" by auto
       
   446     show ?thesis
       
   447     proof(rule h)
       
   448       from ltf show "(e', x) \<in> inv_image less_than f" 
       
   449 	by (simp add:inv_image_def)
       
   450     next
       
   451       from pe' show "P e'" .
       
   452     qed
       
   453   qed
       
   454 qed
       
   455 
       
   456 text {*
       
   457   The @{text "P"} in lemma @{text "wf_iter"} is an invaiant kept throughout the iteration procedure.
       
   458   The particular invariant used to solve our problem is defined by function @{text "Inv(ES)"},
       
   459   an invariant over equal system @{text "ES"}.
       
   460   Every definition starting next till @{text "Inv"} stipulates a property to be satisfied by @{text "ES"}.
       
   461 *}
       
   462 
       
   463 text {* 
       
   464   Every variable is defined at most onece in @{text "ES"}.
       
   465   *}
       
   466 definition 
       
   467   "distinct_equas ES \<equiv> 
       
   468             \<forall> X rhs rhs'. (X, rhs) \<in> ES \<and> (X, rhs') \<in> ES \<longrightarrow> rhs = rhs'"
       
   469 text {* 
       
   470   Every equation in @{text "ES"} (represented by @{text "(X, rhs)"}) is valid, i.e. @{text "(X = L rhs)"}.
       
   471   *}
       
   472 definition 
       
   473   "valid_eqns ES \<equiv> \<forall> X rhs. (X, rhs) \<in> ES \<longrightarrow> (X = L rhs)"
       
   474 
       
   475 text {*
       
   476   The following @{text "rhs_nonempty rhs"} requires regular expressions occuring in transitional 
       
   477   items of @{text "rhs"} does not contain empty string. This is necessary for
       
   478   the application of Arden's transformation to @{text "rhs"}.
       
   479   *}
       
   480 definition 
       
   481   "rhs_nonempty rhs \<equiv> (\<forall> Y r. Trn Y r \<in> rhs \<longrightarrow> [] \<notin> L r)"
       
   482 
       
   483 text {*
       
   484   The following @{text "ardenable ES"} requires that Arden's transformation is applicable
       
   485   to every equation of equational system @{text "ES"}.
       
   486   *}
       
   487 definition 
       
   488   "ardenable ES \<equiv> \<forall> X rhs. (X, rhs) \<in> ES \<longrightarrow> rhs_nonempty rhs"
       
   489 
       
   490 (* The following non_empty seems useless. *)
       
   491 definition 
       
   492   "non_empty ES \<equiv> \<forall> X rhs. (X, rhs) \<in> ES \<longrightarrow> X \<noteq> {}"
       
   493 
       
   494 text {*
       
   495   The following @{text "finite_rhs ES"} requires every equation in @{text "rhs"} be finite.
       
   496   *}
       
   497 definition
       
   498   "finite_rhs ES \<equiv> \<forall> X rhs. (X, rhs) \<in> ES \<longrightarrow> finite rhs"
       
   499 
       
   500 text {*
       
   501   The following @{text "classes_of rhs"} returns all variables (or equivalent classes)
       
   502   occuring in @{text "rhs"}.
       
   503   *}
       
   504 definition 
       
   505   "classes_of rhs \<equiv> {X. \<exists> r. Trn X r \<in> rhs}"
       
   506 
       
   507 text {*
       
   508   The following @{text "lefts_of ES"} returns all variables 
       
   509   defined by equational system @{text "ES"}.
       
   510   *}
       
   511 definition
       
   512   "lefts_of ES \<equiv> {Y | Y yrhs. (Y, yrhs) \<in> ES}"
       
   513 
       
   514 text {*
       
   515   The following @{text "self_contained ES"} requires that every
       
   516   variable occuring on the right hand side of equations is already defined by some
       
   517   equation in @{text "ES"}.
       
   518   *}
       
   519 definition 
       
   520   "self_contained ES \<equiv> \<forall> (X, xrhs) \<in> ES. classes_of xrhs \<subseteq> lefts_of ES"
       
   521 
       
   522 
       
   523 text {*
       
   524   The invariant @{text "Inv(ES)"} is a conjunction of all the previously defined constaints.
       
   525   *}
       
   526 definition 
       
   527   "Inv ES \<equiv> valid_eqns ES \<and> finite ES \<and> distinct_equas ES \<and> ardenable ES \<and> 
       
   528                 non_empty ES \<and> finite_rhs ES \<and> self_contained ES"
       
   529 
       
   530 subsection {* The proof of this direction *}
       
   531 
       
   532 subsubsection {* Basic properties *}
       
   533 
       
   534 text {*
       
   535   The following are some basic properties of the above definitions.
       
   536 *}
       
   537 
       
   538 lemma L_rhs_union_distrib:
       
   539   " L (A::rhs_item set) \<union> L B = L (A \<union> B)"
       
   540 by simp
       
   541 
       
   542 lemma finite_snd_Trn:
       
   543   assumes finite:"finite rhs"
       
   544   shows "finite {r\<^isub>2. Trn Y r\<^isub>2 \<in> rhs}" (is "finite ?B")
       
   545 proof-
       
   546   def rhs' \<equiv> "{e \<in> rhs. \<exists> r. e = Trn Y r}"
       
   547   have "?B = (snd o the_Trn) ` rhs'" using rhs'_def by (auto simp:image_def)
       
   548   moreover have "finite rhs'" using finite rhs'_def by auto
       
   549   ultimately show ?thesis by simp
       
   550 qed
       
   551 
       
   552 lemma rexp_of_empty:
       
   553   assumes finite:"finite rhs"
       
   554   and nonempty:"rhs_nonempty rhs"
       
   555   shows "[] \<notin> L (rexp_of rhs X)"
       
   556 using finite nonempty rhs_nonempty_def
       
   557 by (drule_tac finite_snd_Trn[where Y = X], auto simp:rexp_of_def items_of_def)
       
   558 
       
   559 lemma [intro!]:
       
   560   "P (Trn X r) \<Longrightarrow> (\<exists>a. (\<exists>r. a = Trn X r \<and> P a))" by auto
       
   561 
       
   562 lemma finite_items_of:
       
   563   "finite rhs \<Longrightarrow> finite (items_of rhs X)"
       
   564 by (auto simp:items_of_def intro:finite_subset)
       
   565 
       
   566 lemma lang_of_rexp_of:
       
   567   assumes finite:"finite rhs"
       
   568   shows "L (items_of rhs X) = X ;; (L (rexp_of rhs X))"
       
   569 proof -
       
   570   have "finite ((snd \<circ> the_Trn) ` items_of rhs X)" using finite_items_of[OF finite] by auto
       
   571   thus ?thesis
       
   572     apply (auto simp:rexp_of_def Seq_def items_of_def)
       
   573     apply (rule_tac x = s1 in exI, rule_tac x = s2 in exI, auto)
       
   574     by (rule_tac x= "Trn X r" in exI, auto simp:Seq_def)
       
   575 qed
       
   576 
       
   577 lemma rexp_of_lam_eq_lam_set:
       
   578   assumes finite: "finite rhs"
       
   579   shows "L (rexp_of_lam rhs) = L (lam_of rhs)"
       
   580 proof -
       
   581   have "finite (the_r ` {Lam r |r. Lam r \<in> rhs})" using finite
       
   582     by (rule_tac finite_imageI, auto intro:finite_subset)
       
   583   thus ?thesis by (auto simp:rexp_of_lam_def lam_of_def)
       
   584 qed
       
   585 
       
   586 lemma [simp]:
       
   587   " L (attach_rexp r xb) = L xb ;; L r"
       
   588 apply (cases xb, auto simp:Seq_def)
       
   589 by (rule_tac x = "s1 @ s1a" in exI, rule_tac x = s2a in exI,auto simp:Seq_def)
       
   590 
       
   591 lemma lang_of_append_rhs:
       
   592   "L (append_rhs_rexp rhs r) = L rhs ;; L r"
       
   593 apply (auto simp:append_rhs_rexp_def image_def)
       
   594 apply (auto simp:Seq_def)
       
   595 apply (rule_tac x = "L xb ;; L r" in exI, auto simp add:Seq_def)
       
   596 by (rule_tac x = "attach_rexp r xb" in exI, auto simp:Seq_def)
       
   597 
       
   598 lemma classes_of_union_distrib:
       
   599   "classes_of A \<union> classes_of B = classes_of (A \<union> B)"
       
   600 by (auto simp add:classes_of_def)
       
   601 
       
   602 lemma lefts_of_union_distrib:
       
   603   "lefts_of A \<union> lefts_of B = lefts_of (A \<union> B)"
       
   604 by (auto simp:lefts_of_def)
       
   605 
       
   606 
       
   607 subsubsection {* Intialization *}
       
   608 
       
   609 text {*
       
   610   The following several lemmas until @{text "init_ES_satisfy_Inv"} shows that
       
   611   the initial equational system satisfies invariant @{text "Inv"}.
       
   612   *}
       
   613 
       
   614 lemma defined_by_str:
       
   615   "\<lbrakk>s \<in> X; X \<in> UNIV // (\<approx>Lang)\<rbrakk> \<Longrightarrow> X = (\<approx>Lang) `` {s}"
       
   616 by (auto simp:quotient_def Image_def str_eq_rel_def)
       
   617 
       
   618 lemma every_eqclass_has_transition:
       
   619   assumes has_str: "s @ [c] \<in> X"
       
   620   and     in_CS:   "X \<in> UNIV // (\<approx>Lang)"
       
   621   obtains Y where "Y \<in> UNIV // (\<approx>Lang)" and "Y ;; {[c]} \<subseteq> X" and "s \<in> Y"
       
   622 proof -
       
   623   def Y \<equiv> "(\<approx>Lang) `` {s}"
       
   624   have "Y \<in> UNIV // (\<approx>Lang)" 
       
   625     unfolding Y_def quotient_def by auto
       
   626   moreover
       
   627   have "X = (\<approx>Lang) `` {s @ [c]}" 
       
   628     using has_str in_CS defined_by_str by blast
       
   629   then have "Y ;; {[c]} \<subseteq> X" 
       
   630     unfolding Y_def Image_def Seq_def
       
   631     unfolding str_eq_rel_def
       
   632     by clarsimp
       
   633   moreover
       
   634   have "s \<in> Y" unfolding Y_def 
       
   635     unfolding Image_def str_eq_rel_def by simp
       
   636   ultimately show thesis by (blast intro: that)
       
   637 qed
       
   638 
       
   639 lemma l_eq_r_in_eqs:
       
   640   assumes X_in_eqs: "(X, xrhs) \<in> (eqs (UNIV // (\<approx>Lang)))"
       
   641   shows "X = L xrhs"
       
   642 proof 
       
   643   show "X \<subseteq> L xrhs"
       
   644   proof
       
   645     fix x
       
   646     assume "(1)": "x \<in> X"
       
   647     show "x \<in> L xrhs"          
       
   648     proof (cases "x = []")
       
   649       assume empty: "x = []"
       
   650       thus ?thesis using X_in_eqs "(1)"
       
   651         by (auto simp:eqs_def init_rhs_def)
       
   652     next
       
   653       assume not_empty: "x \<noteq> []"
       
   654       then obtain clist c where decom: "x = clist @ [c]"
       
   655         by (case_tac x rule:rev_cases, auto)
       
   656       have "X \<in> UNIV // (\<approx>Lang)" using X_in_eqs by (auto simp:eqs_def)
       
   657       then obtain Y 
       
   658         where "Y \<in> UNIV // (\<approx>Lang)" 
       
   659         and "Y ;; {[c]} \<subseteq> X"
       
   660         and "clist \<in> Y"
       
   661         using decom "(1)" every_eqclass_has_transition by blast
       
   662       hence 
       
   663         "x \<in> L {Trn Y (CHAR c)| Y c. Y \<in> UNIV // (\<approx>Lang) \<and> Y ;; {[c]} \<subseteq> X}"
       
   664         using "(1)" decom
       
   665         by (simp, rule_tac x = "Trn Y (CHAR c)" in exI, simp add:Seq_def)
       
   666       thus ?thesis using X_in_eqs "(1)"
       
   667         by (simp add:eqs_def init_rhs_def)
       
   668     qed
       
   669   qed
       
   670 next
       
   671   show "L xrhs \<subseteq> X" using X_in_eqs
       
   672     by (auto simp:eqs_def init_rhs_def) 
       
   673 qed
       
   674 
       
   675 lemma finite_init_rhs: 
       
   676   assumes finite: "finite CS"
       
   677   shows "finite (init_rhs CS X)"
       
   678 proof-
       
   679   have "finite {Trn Y (CHAR c) |Y c. Y \<in> CS \<and> Y ;; {[c]} \<subseteq> X}" (is "finite ?A")
       
   680   proof -
       
   681     def S \<equiv> "{(Y, c)| Y c. Y \<in> CS \<and> Y ;; {[c]} \<subseteq> X}" 
       
   682     def h \<equiv> "\<lambda> (Y, c). Trn Y (CHAR c)"
       
   683     have "finite (CS \<times> (UNIV::char set))" using finite by auto
       
   684     hence "finite S" using S_def 
       
   685       by (rule_tac B = "CS \<times> UNIV" in finite_subset, auto)
       
   686     moreover have "?A = h ` S" by (auto simp: S_def h_def image_def)
       
   687     ultimately show ?thesis 
       
   688       by auto
       
   689   qed
       
   690   thus ?thesis by (simp add:init_rhs_def)
       
   691 qed
       
   692 
       
   693 lemma init_ES_satisfy_Inv:
       
   694   assumes finite_CS: "finite (UNIV // (\<approx>Lang))"
       
   695   shows "Inv (eqs (UNIV // (\<approx>Lang)))"
       
   696 proof -
       
   697   have "finite (eqs (UNIV // (\<approx>Lang)))" using finite_CS
       
   698     by (simp add:eqs_def)
       
   699   moreover have "distinct_equas (eqs (UNIV // (\<approx>Lang)))"     
       
   700     by (simp add:distinct_equas_def eqs_def)
       
   701   moreover have "ardenable (eqs (UNIV // (\<approx>Lang)))"
       
   702     by (auto simp add:ardenable_def eqs_def init_rhs_def rhs_nonempty_def del:L_rhs.simps)
       
   703   moreover have "valid_eqns (eqs (UNIV // (\<approx>Lang)))"
       
   704     using l_eq_r_in_eqs by (simp add:valid_eqns_def)
       
   705   moreover have "non_empty (eqs (UNIV // (\<approx>Lang)))"
       
   706     by (auto simp:non_empty_def eqs_def quotient_def Image_def str_eq_rel_def)
       
   707   moreover have "finite_rhs (eqs (UNIV // (\<approx>Lang)))"
       
   708     using finite_init_rhs[OF finite_CS] 
       
   709     by (auto simp:finite_rhs_def eqs_def)
       
   710   moreover have "self_contained (eqs (UNIV // (\<approx>Lang)))"
       
   711     by (auto simp:self_contained_def eqs_def init_rhs_def classes_of_def lefts_of_def)
       
   712   ultimately show ?thesis by (simp add:Inv_def)
       
   713 qed
       
   714 
       
   715 subsubsection {* 
       
   716   Interation step
       
   717   *}
       
   718 
       
   719 text {*
       
   720   From this point until @{text "iteration_step"}, it is proved
       
   721   that there exists iteration steps which keep @{text "Inv(ES)"} while
       
   722   decreasing the size of @{text "ES"}.
       
   723   *}
       
   724 lemma arden_variate_keeps_eq:
       
   725   assumes l_eq_r: "X = L rhs"
       
   726   and not_empty: "[] \<notin> L (rexp_of rhs X)"
       
   727   and finite: "finite rhs"
       
   728   shows "X = L (arden_variate X rhs)"
       
   729 proof -
       
   730   def A \<equiv> "L (rexp_of rhs X)"
       
   731   def b \<equiv> "rhs - items_of rhs X"
       
   732   def B \<equiv> "L b" 
       
   733   have "X = B ;; A\<star>"
       
   734   proof-
       
   735     have "rhs = items_of rhs X \<union> b" by (auto simp:b_def items_of_def)
       
   736     hence "L rhs = L(items_of rhs X \<union> b)" by simp
       
   737     hence "L rhs = L(items_of rhs X) \<union> B" by (simp only:L_rhs_union_distrib B_def)
       
   738     with lang_of_rexp_of
       
   739     have "L rhs = X ;; A \<union> B " using finite by (simp only:B_def b_def A_def)
       
   740     thus ?thesis
       
   741       using l_eq_r not_empty
       
   742       apply (drule_tac B = B and X = X in ardens_revised)
       
   743       by (auto simp:A_def simp del:L_rhs.simps)
       
   744   qed
       
   745   moreover have "L (arden_variate X rhs) = (B ;; A\<star>)" (is "?L = ?R")
       
   746     by (simp only:arden_variate_def L_rhs_union_distrib lang_of_append_rhs 
       
   747                   B_def A_def b_def L_rexp.simps seq_union_distrib)
       
   748    ultimately show ?thesis by simp
       
   749 qed 
       
   750 
       
   751 lemma append_keeps_finite:
       
   752   "finite rhs \<Longrightarrow> finite (append_rhs_rexp rhs r)"
       
   753 by (auto simp:append_rhs_rexp_def)
       
   754 
       
   755 lemma arden_variate_keeps_finite:
       
   756   "finite rhs \<Longrightarrow> finite (arden_variate X rhs)"
       
   757 by (auto simp:arden_variate_def append_keeps_finite)
       
   758 
       
   759 lemma append_keeps_nonempty:
       
   760   "rhs_nonempty rhs \<Longrightarrow> rhs_nonempty (append_rhs_rexp rhs r)"
       
   761 apply (auto simp:rhs_nonempty_def append_rhs_rexp_def)
       
   762 by (case_tac x, auto simp:Seq_def)
       
   763 
       
   764 lemma nonempty_set_sub:
       
   765   "rhs_nonempty rhs \<Longrightarrow> rhs_nonempty (rhs - A)"
       
   766 by (auto simp:rhs_nonempty_def)
       
   767 
       
   768 lemma nonempty_set_union:
       
   769   "\<lbrakk>rhs_nonempty rhs; rhs_nonempty rhs'\<rbrakk> \<Longrightarrow> rhs_nonempty (rhs \<union> rhs')"
       
   770 by (auto simp:rhs_nonempty_def)
       
   771 
       
   772 lemma arden_variate_keeps_nonempty:
       
   773   "rhs_nonempty rhs \<Longrightarrow> rhs_nonempty (arden_variate X rhs)"
       
   774 by (simp only:arden_variate_def append_keeps_nonempty nonempty_set_sub)
       
   775 
       
   776 
       
   777 lemma rhs_subst_keeps_nonempty:
       
   778   "\<lbrakk>rhs_nonempty rhs; rhs_nonempty xrhs\<rbrakk> \<Longrightarrow> rhs_nonempty (rhs_subst rhs X xrhs)"
       
   779 by (simp only:rhs_subst_def append_keeps_nonempty  nonempty_set_union nonempty_set_sub)
       
   780 
       
   781 lemma rhs_subst_keeps_eq:
       
   782   assumes substor: "X = L xrhs"
       
   783   and finite: "finite rhs"
       
   784   shows "L (rhs_subst rhs X xrhs) = L rhs" (is "?Left = ?Right")
       
   785 proof-
       
   786   def A \<equiv> "L (rhs - items_of rhs X)"
       
   787   have "?Left = A \<union> L (append_rhs_rexp xrhs (rexp_of rhs X))"
       
   788     by (simp only:rhs_subst_def L_rhs_union_distrib A_def)
       
   789   moreover have "?Right = A \<union> L (items_of rhs X)"
       
   790   proof-
       
   791     have "rhs = (rhs - items_of rhs X) \<union> (items_of rhs X)" by (auto simp:items_of_def)
       
   792     thus ?thesis by (simp only:L_rhs_union_distrib A_def)
       
   793   qed
       
   794   moreover have "L (append_rhs_rexp xrhs (rexp_of rhs X)) = L (items_of rhs X)" 
       
   795     using finite substor  by (simp only:lang_of_append_rhs lang_of_rexp_of)
       
   796   ultimately show ?thesis by simp
       
   797 qed
       
   798 
       
   799 lemma rhs_subst_keeps_finite_rhs:
       
   800   "\<lbrakk>finite rhs; finite yrhs\<rbrakk> \<Longrightarrow> finite (rhs_subst rhs Y yrhs)"
       
   801 by (auto simp:rhs_subst_def append_keeps_finite)
       
   802 
       
   803 lemma eqs_subst_keeps_finite:
       
   804   assumes finite:"finite (ES:: (string set \<times> rhs_item set) set)"
       
   805   shows "finite (eqs_subst ES Y yrhs)"
       
   806 proof -
       
   807   have "finite {(Ya, rhs_subst yrhsa Y yrhs) |Ya yrhsa. (Ya, yrhsa) \<in> ES}" 
       
   808                                                                   (is "finite ?A")
       
   809   proof-
       
   810     def eqns' \<equiv> "{((Ya::string set), yrhsa)| Ya yrhsa. (Ya, yrhsa) \<in> ES}"
       
   811     def h \<equiv> "\<lambda> ((Ya::string set), yrhsa). (Ya, rhs_subst yrhsa Y yrhs)"
       
   812     have "finite (h ` eqns')" using finite h_def eqns'_def by auto
       
   813     moreover have "?A = h ` eqns'" by (auto simp:h_def eqns'_def)
       
   814     ultimately show ?thesis by auto      
       
   815   qed
       
   816   thus ?thesis by (simp add:eqs_subst_def)
       
   817 qed
       
   818 
       
   819 lemma eqs_subst_keeps_finite_rhs:
       
   820   "\<lbrakk>finite_rhs ES; finite yrhs\<rbrakk> \<Longrightarrow> finite_rhs (eqs_subst ES Y yrhs)"
       
   821 by (auto intro:rhs_subst_keeps_finite_rhs simp add:eqs_subst_def finite_rhs_def)
       
   822 
       
   823 lemma append_rhs_keeps_cls:
       
   824   "classes_of (append_rhs_rexp rhs r) = classes_of rhs"
       
   825 apply (auto simp:classes_of_def append_rhs_rexp_def)
       
   826 apply (case_tac xa, auto simp:image_def)
       
   827 by (rule_tac x = "SEQ ra r" in exI, rule_tac x = "Trn x ra" in bexI, simp+)
       
   828 
       
   829 lemma arden_variate_removes_cl:
       
   830   "classes_of (arden_variate Y yrhs) = classes_of yrhs - {Y}"
       
   831 apply (simp add:arden_variate_def append_rhs_keeps_cls items_of_def)
       
   832 by (auto simp:classes_of_def)
       
   833 
       
   834 lemma lefts_of_keeps_cls:
       
   835   "lefts_of (eqs_subst ES Y yrhs) = lefts_of ES"
       
   836 by (auto simp:lefts_of_def eqs_subst_def)
       
   837 
       
   838 lemma rhs_subst_updates_cls:
       
   839   "X \<notin> classes_of xrhs \<Longrightarrow> 
       
   840       classes_of (rhs_subst rhs X xrhs) = classes_of rhs \<union> classes_of xrhs - {X}"
       
   841 apply (simp only:rhs_subst_def append_rhs_keeps_cls 
       
   842                               classes_of_union_distrib[THEN sym])
       
   843 by (auto simp:classes_of_def items_of_def)
       
   844 
       
   845 lemma eqs_subst_keeps_self_contained:
       
   846   fixes Y
       
   847   assumes sc: "self_contained (ES \<union> {(Y, yrhs)})" (is "self_contained ?A")
       
   848   shows "self_contained (eqs_subst ES Y (arden_variate Y yrhs))" 
       
   849                                                    (is "self_contained ?B")
       
   850 proof-
       
   851   { fix X xrhs'
       
   852     assume "(X, xrhs') \<in> ?B"
       
   853     then obtain xrhs 
       
   854       where xrhs_xrhs': "xrhs' = rhs_subst xrhs Y (arden_variate Y yrhs)"
       
   855       and X_in: "(X, xrhs) \<in> ES" by (simp add:eqs_subst_def, blast)    
       
   856     have "classes_of xrhs' \<subseteq> lefts_of ?B"
       
   857     proof-
       
   858       have "lefts_of ?B = lefts_of ES" by (auto simp add:lefts_of_def eqs_subst_def)
       
   859       moreover have "classes_of xrhs' \<subseteq> lefts_of ES"
       
   860       proof-
       
   861         have "classes_of xrhs' \<subseteq> 
       
   862                         classes_of xrhs \<union> classes_of (arden_variate Y yrhs) - {Y}"
       
   863         proof-
       
   864           have "Y \<notin> classes_of (arden_variate Y yrhs)" 
       
   865             using arden_variate_removes_cl by simp
       
   866           thus ?thesis using xrhs_xrhs' by (auto simp:rhs_subst_updates_cls)
       
   867         qed
       
   868         moreover have "classes_of xrhs \<subseteq> lefts_of ES \<union> {Y}" using X_in sc
       
   869           apply (simp only:self_contained_def lefts_of_union_distrib[THEN sym])
       
   870           by (drule_tac x = "(X, xrhs)" in bspec, auto simp:lefts_of_def)
       
   871         moreover have "classes_of (arden_variate Y yrhs) \<subseteq> lefts_of ES \<union> {Y}" 
       
   872           using sc 
       
   873           by (auto simp add:arden_variate_removes_cl self_contained_def lefts_of_def)
       
   874         ultimately show ?thesis by auto
       
   875       qed
       
   876       ultimately show ?thesis by simp
       
   877     qed
       
   878   } thus ?thesis by (auto simp only:eqs_subst_def self_contained_def)
       
   879 qed
       
   880 
       
   881 lemma eqs_subst_satisfy_Inv:
       
   882   assumes Inv_ES: "Inv (ES \<union> {(Y, yrhs)})"
       
   883   shows "Inv (eqs_subst ES Y (arden_variate Y yrhs))"
       
   884 proof -  
       
   885   have finite_yrhs: "finite yrhs" 
       
   886     using Inv_ES by (auto simp:Inv_def finite_rhs_def)
       
   887   have nonempty_yrhs: "rhs_nonempty yrhs" 
       
   888     using Inv_ES by (auto simp:Inv_def ardenable_def)
       
   889   have Y_eq_yrhs: "Y = L yrhs" 
       
   890     using Inv_ES by (simp only:Inv_def valid_eqns_def, blast)
       
   891   have "distinct_equas (eqs_subst ES Y (arden_variate Y yrhs))" 
       
   892     using Inv_ES
       
   893     by (auto simp:distinct_equas_def eqs_subst_def Inv_def)
       
   894   moreover have "finite (eqs_subst ES Y (arden_variate Y yrhs))" 
       
   895     using Inv_ES by (simp add:Inv_def eqs_subst_keeps_finite)
       
   896   moreover have "finite_rhs (eqs_subst ES Y (arden_variate Y yrhs))"
       
   897   proof-
       
   898     have "finite_rhs ES" using Inv_ES 
       
   899       by (simp add:Inv_def finite_rhs_def)
       
   900     moreover have "finite (arden_variate Y yrhs)"
       
   901     proof -
       
   902       have "finite yrhs" using Inv_ES 
       
   903         by (auto simp:Inv_def finite_rhs_def)
       
   904       thus ?thesis using arden_variate_keeps_finite by simp
       
   905     qed
       
   906     ultimately show ?thesis 
       
   907       by (simp add:eqs_subst_keeps_finite_rhs)
       
   908   qed
       
   909   moreover have "ardenable (eqs_subst ES Y (arden_variate Y yrhs))"
       
   910   proof - 
       
   911     { fix X rhs
       
   912       assume "(X, rhs) \<in> ES"
       
   913       hence "rhs_nonempty rhs"  using prems Inv_ES  
       
   914         by (simp add:Inv_def ardenable_def)
       
   915       with nonempty_yrhs 
       
   916       have "rhs_nonempty (rhs_subst rhs Y (arden_variate Y yrhs))"
       
   917         by (simp add:nonempty_yrhs 
       
   918                rhs_subst_keeps_nonempty arden_variate_keeps_nonempty)
       
   919     } thus ?thesis by (auto simp add:ardenable_def eqs_subst_def)
       
   920   qed
       
   921   moreover have "valid_eqns (eqs_subst ES Y (arden_variate Y yrhs))"
       
   922   proof-
       
   923     have "Y = L (arden_variate Y yrhs)" 
       
   924       using Y_eq_yrhs Inv_ES finite_yrhs nonempty_yrhs      
       
   925       by (rule_tac arden_variate_keeps_eq, (simp add:rexp_of_empty)+)
       
   926     thus ?thesis using Inv_ES 
       
   927       by (clarsimp simp add:valid_eqns_def 
       
   928               eqs_subst_def rhs_subst_keeps_eq Inv_def finite_rhs_def
       
   929                    simp del:L_rhs.simps)
       
   930   qed
       
   931   moreover have 
       
   932     non_empty_subst: "non_empty (eqs_subst ES Y (arden_variate Y yrhs))"
       
   933     using Inv_ES by (auto simp:Inv_def non_empty_def eqs_subst_def)
       
   934   moreover 
       
   935   have self_subst: "self_contained (eqs_subst ES Y (arden_variate Y yrhs))"
       
   936     using Inv_ES eqs_subst_keeps_self_contained by (simp add:Inv_def)
       
   937   ultimately show ?thesis using Inv_ES by (simp add:Inv_def)
       
   938 qed
       
   939 
       
   940 lemma eqs_subst_card_le: 
       
   941   assumes finite: "finite (ES::(string set \<times> rhs_item set) set)"
       
   942   shows "card (eqs_subst ES Y yrhs) <= card ES"
       
   943 proof-
       
   944   def f \<equiv> "\<lambda> x. ((fst x)::string set, rhs_subst (snd x) Y yrhs)"
       
   945   have "eqs_subst ES Y yrhs = f ` ES" 
       
   946     apply (auto simp:eqs_subst_def f_def image_def)
       
   947     by (rule_tac x = "(Ya, yrhsa)" in bexI, simp+)
       
   948   thus ?thesis using finite by (auto intro:card_image_le)
       
   949 qed
       
   950 
       
   951 lemma eqs_subst_cls_remains: 
       
   952   "(X, xrhs) \<in> ES \<Longrightarrow> \<exists> xrhs'. (X, xrhs') \<in> (eqs_subst ES Y yrhs)"
       
   953 by (auto simp:eqs_subst_def)
       
   954 
       
   955 lemma card_noteq_1_has_more:
       
   956   assumes card:"card S \<noteq> 1"
       
   957   and e_in: "e \<in> S"
       
   958   and finite: "finite S"
       
   959   obtains e' where "e' \<in> S \<and> e \<noteq> e'" 
       
   960 proof-
       
   961   have "card (S - {e}) > 0"
       
   962   proof -
       
   963     have "card S > 1" using card e_in finite  
       
   964       by (case_tac "card S", auto) 
       
   965     thus ?thesis using finite e_in by auto
       
   966   qed
       
   967   hence "S - {e} \<noteq> {}" using finite by (rule_tac notI, simp)
       
   968   thus "(\<And>e'. e' \<in> S \<and> e \<noteq> e' \<Longrightarrow> thesis) \<Longrightarrow> thesis" by auto
       
   969 qed
       
   970 
       
   971 lemma iteration_step: 
       
   972   assumes Inv_ES: "Inv ES"
       
   973   and    X_in_ES: "(X, xrhs) \<in> ES"
       
   974   and    not_T: "card ES \<noteq> 1"
       
   975   shows "\<exists> ES'. (Inv ES' \<and> (\<exists> xrhs'.(X, xrhs') \<in> ES')) \<and> 
       
   976                 (card ES', card ES) \<in> less_than" (is "\<exists> ES'. ?P ES'")
       
   977 proof -
       
   978   have finite_ES: "finite ES" using Inv_ES by (simp add:Inv_def)
       
   979   then obtain Y yrhs 
       
   980     where Y_in_ES: "(Y, yrhs) \<in> ES" and not_eq: "(X, xrhs) \<noteq> (Y, yrhs)" 
       
   981     using not_T X_in_ES by (drule_tac card_noteq_1_has_more, auto)
       
   982   def ES' == "ES - {(Y, yrhs)}"
       
   983   let ?ES'' = "eqs_subst ES' Y (arden_variate Y yrhs)"
       
   984   have "?P ?ES''"
       
   985   proof -
       
   986     have "Inv ?ES''" using Y_in_ES Inv_ES
       
   987       by (rule_tac eqs_subst_satisfy_Inv, simp add:ES'_def insert_absorb)
       
   988     moreover have "\<exists>xrhs'. (X, xrhs') \<in> ?ES''"  using not_eq X_in_ES
       
   989       by (rule_tac ES = ES' in eqs_subst_cls_remains, auto simp add:ES'_def)
       
   990     moreover have "(card ?ES'', card ES) \<in> less_than" 
       
   991     proof -
       
   992       have "finite ES'" using finite_ES ES'_def by auto
       
   993       moreover have "card ES' < card ES" using finite_ES Y_in_ES
       
   994         by (auto simp:ES'_def card_gt_0_iff intro:diff_Suc_less)
       
   995       ultimately show ?thesis 
       
   996         by (auto dest:eqs_subst_card_le elim:le_less_trans)
       
   997     qed
       
   998     ultimately show ?thesis by simp
       
   999   qed
       
  1000   thus ?thesis by blast
       
  1001 qed
       
  1002 
       
  1003 subsubsection {*
       
  1004   Conclusion of the proof
       
  1005   *}
       
  1006 
       
  1007 text {*
       
  1008   From this point until @{text "hard_direction"}, the hard direction is proved
       
  1009   through a simple application of the iteration principle.
       
  1010 *}
       
  1011 
       
  1012 lemma iteration_conc: 
       
  1013   assumes history: "Inv ES"
       
  1014   and    X_in_ES: "\<exists> xrhs. (X, xrhs) \<in> ES"
       
  1015   shows 
       
  1016   "\<exists> ES'. (Inv ES' \<and> (\<exists> xrhs'. (X, xrhs') \<in> ES')) \<and> card ES' = 1" 
       
  1017                                                           (is "\<exists> ES'. ?P ES'")
       
  1018 proof (cases "card ES = 1")
       
  1019   case True
       
  1020   thus ?thesis using history X_in_ES
       
  1021     by blast
       
  1022 next
       
  1023   case False  
       
  1024   thus ?thesis using history iteration_step X_in_ES
       
  1025     by (rule_tac f = card in wf_iter, auto)
       
  1026 qed
       
  1027   
       
  1028 lemma last_cl_exists_rexp:
       
  1029   assumes ES_single: "ES = {(X, xrhs)}" 
       
  1030   and Inv_ES: "Inv ES"
       
  1031   shows "\<exists> (r::rexp). L r = X" (is "\<exists> r. ?P r")
       
  1032 proof-
       
  1033   let ?A = "arden_variate X xrhs"
       
  1034   have "?P (rexp_of_lam ?A)"
       
  1035   proof -
       
  1036     have "L (rexp_of_lam ?A) = L (lam_of ?A)"
       
  1037     proof(rule rexp_of_lam_eq_lam_set)
       
  1038       show "finite (arden_variate X xrhs)" using Inv_ES ES_single 
       
  1039         by (rule_tac arden_variate_keeps_finite, 
       
  1040                        auto simp add:Inv_def finite_rhs_def)
       
  1041     qed
       
  1042     also have "\<dots> = L ?A"
       
  1043     proof-
       
  1044       have "lam_of ?A = ?A"
       
  1045       proof-
       
  1046         have "classes_of ?A = {}" using Inv_ES ES_single
       
  1047           by (simp add:arden_variate_removes_cl 
       
  1048                        self_contained_def Inv_def lefts_of_def) 
       
  1049         thus ?thesis 
       
  1050           by (auto simp only:lam_of_def classes_of_def, case_tac x, auto)
       
  1051       qed
       
  1052       thus ?thesis by simp
       
  1053     qed
       
  1054     also have "\<dots> = X"
       
  1055     proof(rule arden_variate_keeps_eq [THEN sym])
       
  1056       show "X = L xrhs" using Inv_ES ES_single 
       
  1057         by (auto simp only:Inv_def valid_eqns_def)  
       
  1058     next
       
  1059       from Inv_ES ES_single show "[] \<notin> L (rexp_of xrhs X)"
       
  1060         by(simp add:Inv_def ardenable_def rexp_of_empty finite_rhs_def)
       
  1061     next
       
  1062       from Inv_ES ES_single show "finite xrhs" 
       
  1063         by (simp add:Inv_def finite_rhs_def)
       
  1064     qed
       
  1065     finally show ?thesis by simp
       
  1066   qed
       
  1067   thus ?thesis by auto
       
  1068 qed
       
  1069    
       
  1070 lemma every_eqcl_has_reg: 
       
  1071   assumes finite_CS: "finite (UNIV // (\<approx>Lang))"
       
  1072   and X_in_CS: "X \<in> (UNIV // (\<approx>Lang))"
       
  1073   shows "\<exists> (reg::rexp). L reg = X" (is "\<exists> r. ?E r")
       
  1074 proof -
       
  1075   from X_in_CS have "\<exists> xrhs. (X, xrhs) \<in> (eqs (UNIV  // (\<approx>Lang)))"
       
  1076     by (auto simp:eqs_def init_rhs_def)
       
  1077   then obtain ES xrhs where Inv_ES: "Inv ES" 
       
  1078     and X_in_ES: "(X, xrhs) \<in> ES"
       
  1079     and card_ES: "card ES = 1"
       
  1080     using finite_CS X_in_CS init_ES_satisfy_Inv iteration_conc
       
  1081     by blast
       
  1082   hence ES_single_equa: "ES = {(X, xrhs)}" 
       
  1083     by (auto simp:Inv_def dest!:card_Suc_Diff1 simp:card_eq_0_iff) 
       
  1084   thus ?thesis using Inv_ES
       
  1085     by (rule last_cl_exists_rexp)
       
  1086 qed
       
  1087 
       
  1088 lemma finals_in_partitions:
       
  1089   "finals Lang \<subseteq> (UNIV // (\<approx>Lang))"
       
  1090   by (auto simp:finals_def quotient_def)   
       
  1091 
       
  1092 theorem hard_direction: 
       
  1093   assumes finite_CS: "finite (UNIV // (\<approx>Lang))"
       
  1094   shows   "\<exists> (reg::rexp). Lang = L reg"
       
  1095 proof -
       
  1096   have "\<forall> X \<in> (UNIV // (\<approx>Lang)). \<exists> (reg::rexp). X = L reg" 
       
  1097     using finite_CS every_eqcl_has_reg by blast
       
  1098   then obtain f 
       
  1099     where f_prop: "\<forall> X \<in> (UNIV // (\<approx>Lang)). X = L ((f X)::rexp)" 
       
  1100     by (auto dest:bchoice)
       
  1101   def rs \<equiv> "f ` (finals Lang)"  
       
  1102   have "Lang = \<Union> (finals Lang)" using lang_is_union_of_finals by auto
       
  1103   also have "\<dots> = L (folds ALT NULL rs)" 
       
  1104   proof -
       
  1105     have "finite rs"
       
  1106     proof -
       
  1107       have "finite (finals Lang)" 
       
  1108         using finite_CS finals_in_partitions[of "Lang"]   
       
  1109         by (erule_tac finite_subset, simp)
       
  1110       thus ?thesis using rs_def by auto
       
  1111     qed
       
  1112     thus ?thesis 
       
  1113       using f_prop rs_def finals_in_partitions[of "Lang"] by auto
       
  1114   qed
       
  1115   finally show ?thesis by blast
       
  1116 qed 
       
  1117 
       
  1118 end