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