Myhill_1.thy
changeset 162 e93760534354
parent 149 e122cb146ecc
child 166 7743d2ad71d1
equal deleted inserted replaced
161:a8a442ba0dbf 162:e93760534354
     1 theory Myhill_1
     1 theory Myhill_1
     2 imports Main Folds 
     2 imports Main Folds Regular
     3         "~~/src/HOL/Library/While_Combinator"
     3         "~~/src/HOL/Library/While_Combinator" 
     4 begin
     4 begin
     5 
     5 
     6 section {* Preliminary definitions *}
       
     7 
       
     8 types lang = "string set"
       
     9 
       
    10 
       
    11 text {*  Sequential composition of two languages *}
       
    12 
       
    13 definition 
       
    14   Seq :: "lang \<Rightarrow> lang \<Rightarrow> lang" (infixr ";;" 100)
       
    15 where 
       
    16   "A ;; B = {s\<^isub>1 @ s\<^isub>2 | s\<^isub>1 s\<^isub>2. s\<^isub>1 \<in> A \<and> s\<^isub>2 \<in> B}"
       
    17 
       
    18 
       
    19 text {* Some properties of operator @{text ";;"}. *}
       
    20 
       
    21 lemma seq_add_left:
       
    22   assumes a: "A = B"
       
    23   shows "C ;; A = C ;; B"
       
    24 using a by simp
       
    25 
       
    26 lemma seq_union_distrib_right:
       
    27   shows "(A \<union> B) ;; C = (A ;; C) \<union> (B ;; C)"
       
    28 unfolding Seq_def by auto
       
    29 
       
    30 lemma seq_union_distrib_left:
       
    31   shows "C ;; (A \<union> B) = (C ;; A) \<union> (C ;; B)"
       
    32 unfolding Seq_def by  auto
       
    33 
       
    34 lemma seq_intro:
       
    35   assumes a: "x \<in> A" "y \<in> B"
       
    36   shows "x @ y \<in> A ;; B "
       
    37 using a by (auto simp: Seq_def)
       
    38 
       
    39 lemma seq_assoc:
       
    40   shows "(A ;; B) ;; C = A ;; (B ;; C)"
       
    41 unfolding Seq_def
       
    42 apply(auto)
       
    43 apply(blast)
       
    44 by (metis append_assoc)
       
    45 
       
    46 lemma seq_empty [simp]:
       
    47   shows "A ;; {[]} = A"
       
    48   and   "{[]} ;; A = A"
       
    49 by (simp_all add: Seq_def)
       
    50 
       
    51 
       
    52 text {* Power and Star of a language *}
       
    53 
       
    54 fun 
       
    55   pow :: "lang \<Rightarrow> nat \<Rightarrow> lang" (infixl "\<up>" 100)
       
    56 where
       
    57   "A \<up> 0 = {[]}"
       
    58 | "A \<up> (Suc n) =  A ;; (A \<up> n)" 
       
    59 
       
    60 definition
       
    61   Star :: "lang \<Rightarrow> lang" ("_\<star>" [101] 102)
       
    62 where
       
    63   "A\<star> \<equiv> (\<Union>n. A \<up> n)"
       
    64 
       
    65 
       
    66 lemma star_start[intro]:
       
    67   shows "[] \<in> A\<star>"
       
    68 proof -
       
    69   have "[] \<in> A \<up> 0" by auto
       
    70   then show "[] \<in> A\<star>" unfolding Star_def by blast
       
    71 qed
       
    72 
       
    73 lemma star_step [intro]:
       
    74   assumes a: "s1 \<in> A" 
       
    75   and     b: "s2 \<in> A\<star>"
       
    76   shows "s1 @ s2 \<in> A\<star>"
       
    77 proof -
       
    78   from b obtain n where "s2 \<in> A \<up> n" unfolding Star_def by auto
       
    79   then have "s1 @ s2 \<in> A \<up> (Suc n)" using a by (auto simp add: Seq_def)
       
    80   then show "s1 @ s2 \<in> A\<star>" unfolding Star_def by blast
       
    81 qed
       
    82 
       
    83 lemma star_induct[consumes 1, case_names start step]:
       
    84   assumes a: "x \<in> A\<star>" 
       
    85   and     b: "P []"
       
    86   and     c: "\<And>s1 s2. \<lbrakk>s1 \<in> A; s2 \<in> A\<star>; P s2\<rbrakk> \<Longrightarrow> P (s1 @ s2)"
       
    87   shows "P x"
       
    88 proof -
       
    89   from a obtain n where "x \<in> A \<up> n" unfolding Star_def by auto
       
    90   then show "P x"
       
    91     by (induct n arbitrary: x)
       
    92        (auto intro!: b c simp add: Seq_def Star_def)
       
    93 qed
       
    94     
       
    95 lemma star_intro1:
       
    96   assumes a: "x \<in> A\<star>"
       
    97   and     b: "y \<in> A\<star>"
       
    98   shows "x @ y \<in> A\<star>"
       
    99 using a b
       
   100 by (induct rule: star_induct) (auto)
       
   101 
       
   102 lemma star_intro2: 
       
   103   assumes a: "y \<in> A"
       
   104   shows "y \<in> A\<star>"
       
   105 proof -
       
   106   from a have "y @ [] \<in> A\<star>" by blast
       
   107   then show "y \<in> A\<star>" by simp
       
   108 qed
       
   109 
       
   110 lemma star_intro3:
       
   111   assumes a: "x \<in> A\<star>"
       
   112   and     b: "y \<in> A"
       
   113   shows "x @ y \<in> A\<star>"
       
   114 using a b by (blast intro: star_intro1 star_intro2)
       
   115 
       
   116 lemma star_cases:
       
   117   shows "A\<star> =  {[]} \<union> A ;; A\<star>"
       
   118 proof
       
   119   { fix x
       
   120     have "x \<in> A\<star> \<Longrightarrow> x \<in> {[]} \<union> A ;; A\<star>"
       
   121       unfolding Seq_def
       
   122     by (induct rule: star_induct) (auto)
       
   123   }
       
   124   then show "A\<star> \<subseteq> {[]} \<union> A ;; A\<star>" by auto
       
   125 next
       
   126   show "{[]} \<union> A ;; A\<star> \<subseteq> A\<star>"
       
   127     unfolding Seq_def by auto
       
   128 qed
       
   129 
       
   130 lemma star_decom: 
       
   131   assumes a: "x \<in> A\<star>" "x \<noteq> []"
       
   132   shows "\<exists>a b. x = a @ b \<and> a \<noteq> [] \<and> a \<in> A \<and> b \<in> A\<star>"
       
   133 using a
       
   134 by (induct rule: star_induct) (blast)+
       
   135 
       
   136 lemma
       
   137   shows seq_Union_left:  "B ;; (\<Union>n. A \<up> n) = (\<Union>n. B ;; (A \<up> n))"
       
   138   and   seq_Union_right: "(\<Union>n. A \<up> n) ;; B = (\<Union>n. (A \<up> n) ;; B)"
       
   139 unfolding Seq_def by auto
       
   140 
       
   141 lemma seq_pow_comm:
       
   142   shows "A ;; (A \<up> n) = (A \<up> n) ;; A"
       
   143 by (induct n) (simp_all add: seq_assoc[symmetric])
       
   144 
       
   145 lemma seq_star_comm:
       
   146   shows "A ;; A\<star> = A\<star> ;; A"
       
   147 unfolding Star_def seq_Union_left
       
   148 unfolding seq_pow_comm seq_Union_right 
       
   149 by simp
       
   150 
       
   151 
       
   152 text {* Two lemmas about the length of strings in @{text "A \<up> n"} *}
       
   153 
       
   154 lemma pow_length:
       
   155   assumes a: "[] \<notin> A"
       
   156   and     b: "s \<in> A \<up> Suc n"
       
   157   shows "n < length s"
       
   158 using b
       
   159 proof (induct n arbitrary: s)
       
   160   case 0
       
   161   have "s \<in> A \<up> Suc 0" by fact
       
   162   with a have "s \<noteq> []" by auto
       
   163   then show "0 < length s" by auto
       
   164 next
       
   165   case (Suc n)
       
   166   have ih: "\<And>s. s \<in> A \<up> Suc n \<Longrightarrow> n < length s" by fact
       
   167   have "s \<in> A \<up> Suc (Suc n)" by fact
       
   168   then obtain s1 s2 where eq: "s = s1 @ s2" and *: "s1 \<in> A" and **: "s2 \<in> A \<up> Suc n"
       
   169     by (auto simp add: Seq_def)
       
   170   from ih ** have "n < length s2" by simp
       
   171   moreover have "0 < length s1" using * a by auto
       
   172   ultimately show "Suc n < length s" unfolding eq 
       
   173     by (simp only: length_append)
       
   174 qed
       
   175 
       
   176 lemma seq_pow_length:
       
   177   assumes a: "[] \<notin> A"
       
   178   and     b: "s \<in> B ;; (A \<up> Suc n)"
       
   179   shows "n < length s"
       
   180 proof -
       
   181   from b obtain s1 s2 where eq: "s = s1 @ s2" and *: "s2 \<in> A \<up> Suc n"
       
   182     unfolding Seq_def by auto
       
   183   from * have " n < length s2" by (rule pow_length[OF a])
       
   184   then show "n < length s" using eq by simp
       
   185 qed
       
   186 
       
   187 
       
   188 section {* A modified version of Arden's lemma *}
       
   189 
       
   190 text {*  A helper lemma for Arden *}
       
   191 
       
   192 lemma arden_helper:
       
   193   assumes eq: "X = X ;; A \<union> B"
       
   194   shows "X = X ;; (A \<up> Suc n) \<union> (\<Union>m\<in>{0..n}. B ;; (A \<up> m))"
       
   195 proof (induct n)
       
   196   case 0 
       
   197   show "X = X ;; (A \<up> Suc 0) \<union> (\<Union>(m::nat)\<in>{0..0}. B ;; (A \<up> m))"
       
   198     using eq by simp
       
   199 next
       
   200   case (Suc n)
       
   201   have ih: "X = X ;; (A \<up> Suc n) \<union> (\<Union>m\<in>{0..n}. B ;; (A \<up> m))" by fact
       
   202   also have "\<dots> = (X ;; A \<union> B) ;; (A \<up> Suc n) \<union> (\<Union>m\<in>{0..n}. B ;; (A \<up> m))" using eq by simp
       
   203   also have "\<dots> = X ;; (A \<up> Suc (Suc n)) \<union> (B ;; (A \<up> Suc n)) \<union> (\<Union>m\<in>{0..n}. B ;; (A \<up> m))"
       
   204     by (simp add: seq_union_distrib_right seq_assoc)
       
   205   also have "\<dots> = X ;; (A \<up> Suc (Suc n)) \<union> (\<Union>m\<in>{0..Suc n}. B ;; (A \<up> m))"
       
   206     by (auto simp add: le_Suc_eq)
       
   207   finally show "X = X ;; (A \<up> Suc (Suc n)) \<union> (\<Union>m\<in>{0..Suc n}. B ;; (A \<up> m))" .
       
   208 qed
       
   209 
       
   210 theorem arden:
       
   211   assumes nemp: "[] \<notin> A"
       
   212   shows "X = X ;; A \<union> B \<longleftrightarrow> X = B ;; A\<star>"
       
   213 proof
       
   214   assume eq: "X = B ;; A\<star>"
       
   215   have "A\<star> = {[]} \<union> A\<star> ;; A" 
       
   216     unfolding seq_star_comm[symmetric]
       
   217     by (rule star_cases)
       
   218   then have "B ;; A\<star> = B ;; ({[]} \<union> A\<star> ;; A)"
       
   219     by (rule seq_add_left)
       
   220   also have "\<dots> = B \<union> B ;; (A\<star> ;; A)"
       
   221     unfolding seq_union_distrib_left by simp
       
   222   also have "\<dots> = B \<union> (B ;; A\<star>) ;; A" 
       
   223     by (simp only: seq_assoc)
       
   224   finally show "X = X ;; A \<union> B" 
       
   225     using eq by blast 
       
   226 next
       
   227   assume eq: "X = X ;; A \<union> B"
       
   228   { fix n::nat
       
   229     have "B ;; (A \<up> n) \<subseteq> X" using arden_helper[OF eq, of "n"] by auto }
       
   230   then have "B ;; A\<star> \<subseteq> X" 
       
   231     unfolding Seq_def Star_def UNION_def by auto
       
   232   moreover
       
   233   { fix s::string
       
   234     obtain k where "k = length s" by auto
       
   235     then have not_in: "s \<notin> X ;; (A \<up> Suc k)" 
       
   236       using seq_pow_length[OF nemp] by blast
       
   237     assume "s \<in> X"
       
   238     then have "s \<in> X ;; (A \<up> Suc k) \<union> (\<Union>m\<in>{0..k}. B ;; (A \<up> m))"
       
   239       using arden_helper[OF eq, of "k"] by auto
       
   240     then have "s \<in> (\<Union>m\<in>{0..k}. B ;; (A \<up> m))" using not_in by auto
       
   241     moreover
       
   242     have "(\<Union>m\<in>{0..k}. B ;; (A \<up> m)) \<subseteq> (\<Union>n. B ;; (A \<up> n))" by auto
       
   243     ultimately 
       
   244     have "s \<in> B ;; A\<star>" 
       
   245       unfolding seq_Union_left Star_def by auto }
       
   246   then have "X \<subseteq> B ;; A\<star>" by auto
       
   247   ultimately 
       
   248   show "X = B ;; A\<star>" by simp
       
   249 qed
       
   250 
       
   251 
       
   252 section {* Regular Expressions *}
       
   253 
       
   254 datatype rexp =
       
   255   NULL
       
   256 | EMPTY
       
   257 | CHAR char
       
   258 | SEQ rexp rexp
       
   259 | ALT rexp rexp
       
   260 | STAR rexp
       
   261 
       
   262 
       
   263 text {* 
       
   264   The function @{text L} is overloaded, with the idea that @{text "L x"} 
       
   265   evaluates to the language represented by the object @{text x}.
       
   266 *}
       
   267 
       
   268 consts L:: "'a \<Rightarrow> lang"
       
   269 
       
   270 overloading L_rexp \<equiv> "L::  rexp \<Rightarrow> lang"
       
   271 begin
       
   272 fun
       
   273   L_rexp :: "rexp \<Rightarrow> lang"
       
   274 where
       
   275     "L_rexp (NULL) = {}"
       
   276   | "L_rexp (EMPTY) = {[]}"
       
   277   | "L_rexp (CHAR c) = {[c]}"
       
   278   | "L_rexp (SEQ r1 r2) = (L_rexp r1) ;; (L_rexp r2)"
       
   279   | "L_rexp (ALT r1 r2) = (L_rexp r1) \<union> (L_rexp r2)"
       
   280   | "L_rexp (STAR r) = (L_rexp r)\<star>"
       
   281 end
       
   282 
       
   283 
       
   284 text {* ALT-combination of a set or regulare expressions *}
       
   285 
       
   286 abbreviation
       
   287   Setalt  ("\<Uplus>_" [1000] 999) 
       
   288 where
       
   289   "\<Uplus>A \<equiv> folds ALT NULL A"
       
   290 
       
   291 text {* 
       
   292   For finite sets, @{term Setalt} is preserved under @{term L}.
       
   293 *}
       
   294 
       
   295 lemma folds_alt_simp [simp]:
       
   296   fixes rs::"rexp set"
       
   297   assumes a: "finite rs"
       
   298   shows "L (\<Uplus>rs) = \<Union> (L ` rs)"
       
   299 unfolding folds_def
       
   300 apply(rule set_eqI)
       
   301 apply(rule someI2_ex)
       
   302 apply(rule_tac finite_imp_fold_graph[OF a])
       
   303 apply(erule fold_graph.induct)
       
   304 apply(auto)
       
   305 done
       
   306 
       
   307 
       
   308 section {* Direction @{text "finite partition \<Rightarrow> regular language"} *}
     6 section {* Direction @{text "finite partition \<Rightarrow> regular language"} *}
   309 
       
   310 
       
   311 text {* Just a technical lemma for collections and pairs *}
       
   312 
     7 
   313 lemma Pair_Collect[simp]:
     8 lemma Pair_Collect[simp]:
   314   shows "(x, y) \<in> {(x, y). P x y} \<longleftrightarrow> P x y"
     9   shows "(x, y) \<in> {(x, y). P x y} \<longleftrightarrow> P x y"
   315 by simp
    10 by simp
   316 
    11 
   319 definition
    14 definition
   320   str_eq_rel :: "lang \<Rightarrow> (string \<times> string) set" ("\<approx>_" [100] 100)
    15   str_eq_rel :: "lang \<Rightarrow> (string \<times> string) set" ("\<approx>_" [100] 100)
   321 where
    16 where
   322   "\<approx>A \<equiv> {(x, y).  (\<forall>z. x @ z \<in> A \<longleftrightarrow> y @ z \<in> A)}"
    17   "\<approx>A \<equiv> {(x, y).  (\<forall>z. x @ z \<in> A \<longleftrightarrow> y @ z \<in> A)}"
   323 
    18 
   324 text {* 
       
   325   Among the equivalence clases of @{text "\<approx>A"}, the set @{text "finals A"} 
       
   326   singles out those which contains the strings from @{text A}.
       
   327 *}
       
   328 
       
   329 definition 
    19 definition 
   330   finals :: "lang \<Rightarrow> lang set"
    20   finals :: "lang \<Rightarrow> lang set"
   331 where
    21 where
   332   "finals A \<equiv> {\<approx>A `` {s} | s . s \<in> A}"
    22   "finals A \<equiv> {\<approx>A `` {s} | s . s \<in> A}"
   333 
       
   334 
    23 
   335 lemma lang_is_union_of_finals: 
    24 lemma lang_is_union_of_finals: 
   336   shows "A = \<Union> finals A"
    25   shows "A = \<Union> finals A"
   337 unfolding finals_def
    26 unfolding finals_def
   338 unfolding Image_def
    27 unfolding Image_def
   339 unfolding str_eq_rel_def
    28 unfolding str_eq_rel_def
   340 apply(auto)
    29 by (auto) (metis append_Nil2)
   341 apply(drule_tac x = "[]" in spec)
       
   342 apply(auto)
       
   343 done
       
   344 
    30 
   345 lemma finals_in_partitions:
    31 lemma finals_in_partitions:
   346   shows "finals A \<subseteq> (UNIV // \<approx>A)"
    32   shows "finals A \<subseteq> (UNIV // \<approx>A)"
   347 unfolding finals_def quotient_def
    33 unfolding finals_def quotient_def
   348 by auto
    34 by auto
   349 
    35 
   350 section {* Equational systems *}
    36 section {* Equational systems *}
   351 
    37 
   352 text {* The two kinds of terms in the rhs of equations. *}
    38 text {* The two kinds of terms in the rhs of equations. *}
   353 
    39 
   354 datatype rhs_item = 
    40 datatype rhs_trm = 
   355    Lam "rexp"            (* Lambda-marker *)
    41    Lam "rexp"            (* Lambda-marker *)
   356  | Trn "lang" "rexp"     (* Transition *)
    42  | Trn "lang" "rexp"     (* Transition *)
   357 
    43 
   358 
    44 
   359 overloading L_rhs_item \<equiv> "L:: rhs_item \<Rightarrow> lang"
    45 overloading L_rhs_trm \<equiv> "L:: rhs_trm \<Rightarrow> lang"
   360 begin
    46 begin
   361   fun L_rhs_item:: "rhs_item \<Rightarrow> lang"
    47   fun L_rhs_trm:: "rhs_trm \<Rightarrow> lang"
   362   where
    48   where
   363     "L_rhs_item (Lam r) = L r" 
    49     "L_rhs_trm (Lam r) = L r" 
   364   | "L_rhs_item (Trn X r) = X ;; L r"
    50   | "L_rhs_trm (Trn X r) = X ;; L r"
   365 end
    51 end
   366 
    52 
   367 overloading L_rhs \<equiv> "L:: rhs_item set \<Rightarrow> lang"
    53 overloading L_rhs \<equiv> "L:: rhs_trm set \<Rightarrow> lang"
   368 begin
    54 begin
   369    fun L_rhs:: "rhs_item set \<Rightarrow> lang"
    55    fun L_rhs:: "rhs_trm set \<Rightarrow> lang"
   370    where 
    56    where 
   371      "L_rhs rhs = \<Union> (L ` rhs)"
    57      "L_rhs rhs = \<Union> (L ` rhs)"
   372 end
    58 end
   373 
    59 
       
    60 lemma L_rhs_set:
       
    61   shows "L {Trn X r | r. P r} = \<Union>{L (Trn X r) | r. P r}"
       
    62 by (auto simp del: L_rhs_trm.simps)
       
    63 
   374 lemma L_rhs_union_distrib:
    64 lemma L_rhs_union_distrib:
   375   fixes A B::"rhs_item set"
    65   fixes A B::"rhs_trm set"
   376   shows "L A \<union> L B = L (A \<union> B)"
    66   shows "L A \<union> L B = L (A \<union> B)"
   377 by simp
    67 by simp
   378 
    68 
   379 
    69 
   380 
    70 
   396 
    86 
   397 definition 
    87 definition 
   398   "Init CS \<equiv> {(X, Init_rhs CS X) | X.  X \<in> CS}"
    88   "Init CS \<equiv> {(X, Init_rhs CS X) | X.  X \<in> CS}"
   399 
    89 
   400 
    90 
   401 
       
   402 section {* Arden Operation on equations *}
    91 section {* Arden Operation on equations *}
   403 
    92 
   404 text {*
       
   405   The function @{text "attach_rexp r item"} SEQ-composes @{text r} to the
       
   406   right of every rhs-item.
       
   407 *}
       
   408 
       
   409 fun 
    93 fun 
   410   append_rexp :: "rexp \<Rightarrow> rhs_item \<Rightarrow> rhs_item"
    94   Append_rexp :: "rexp \<Rightarrow> rhs_trm \<Rightarrow> rhs_trm"
   411 where
    95 where
   412   "append_rexp r (Lam rexp)   = Lam (SEQ rexp r)"
    96   "Append_rexp r (Lam rexp)   = Lam (SEQ rexp r)"
   413 | "append_rexp r (Trn X rexp) = Trn X (SEQ rexp r)"
    97 | "Append_rexp r (Trn X rexp) = Trn X (SEQ rexp r)"
   414 
    98 
   415 
    99 
   416 definition
   100 definition
   417   "append_rhs_rexp rhs rexp \<equiv> (append_rexp rexp) ` rhs"
   101   "Append_rexp_rhs rhs rexp \<equiv> (Append_rexp rexp) ` rhs"
   418 
   102 
   419 definition 
   103 definition 
   420   "Arden X rhs \<equiv> 
   104   "Arden X rhs \<equiv> 
   421      append_rhs_rexp (rhs - {Trn X r | r. Trn X r \<in> rhs}) (STAR (\<Uplus> {r. Trn X r \<in> rhs}))"
   105      Append_rexp_rhs (rhs - {Trn X r | r. Trn X r \<in> rhs}) (STAR (\<Uplus> {r. Trn X r \<in> rhs}))"
   422 
   106 
   423 
   107 
   424 section {* Substitution Operation on equations *}
   108 section {* Substitution Operation on equations *}
   425 
   109 
   426 text {* 
       
   427   Suppose and equation @{text "X = xrhs"}, @{text "Subst"} substitutes 
       
   428   all occurences of @{text "X"} in @{text "rhs"} by @{text "xrhs"}.
       
   429 *}
       
   430 
       
   431 definition 
   110 definition 
   432   "Subst rhs X xrhs \<equiv> 
   111   "Subst rhs X xrhs \<equiv> 
   433         (rhs - {Trn X r | r. Trn X r \<in> rhs}) \<union> (append_rhs_rexp xrhs (\<Uplus> {r. Trn X r \<in> rhs}))"
   112         (rhs - {Trn X r | r. Trn X r \<in> rhs}) \<union> (Append_rexp_rhs xrhs (\<Uplus> {r. Trn X r \<in> rhs}))"
   434 
       
   435 text {*
       
   436   @{text "eqs_subst ES X xrhs"} substitutes @{text xrhs} into every 
       
   437   equation of the equational system @{text ES}.
       
   438 *}
       
   439 
       
   440 types esystem = "(lang \<times> rhs_item set) set"
       
   441 
   113 
   442 definition
   114 definition
   443   Subst_all :: "esystem \<Rightarrow> lang \<Rightarrow> rhs_item set \<Rightarrow> esystem"
   115   Subst_all :: "(lang \<times> rhs_trm set) set \<Rightarrow> lang \<Rightarrow> rhs_trm set \<Rightarrow> (lang \<times> rhs_trm set) set"
   444 where
   116 where
   445   "Subst_all ES X xrhs \<equiv> {(Y, Subst yrhs X xrhs) | Y yrhs. (Y, yrhs) \<in> ES}"
   117   "Subst_all ES X xrhs \<equiv> {(Y, Subst yrhs X xrhs) | Y yrhs. (Y, yrhs) \<in> ES}"
   446 
       
   447 text {*
       
   448   The following term @{text "remove ES Y yrhs"} removes the equation
       
   449   @{text "Y = yrhs"} from equational system @{text "ES"} by replacing
       
   450   all occurences of @{text "Y"} by its definition (using @{text "eqs_subst"}).
       
   451   The @{text "Y"}-definition is made non-recursive using Arden's transformation
       
   452   @{text "arden_variate Y yrhs"}.
       
   453   *}
       
   454 
   118 
   455 definition
   119 definition
   456   "Remove ES X xrhs \<equiv> 
   120   "Remove ES X xrhs \<equiv> 
   457       Subst_all  (ES - {(X, xrhs)}) X (Arden X xrhs)"
   121       Subst_all  (ES - {(X, xrhs)}) X (Arden X xrhs)"
   458 
   122 
   459 
   123 
   460 section {* While-combinator *}
   124 section {* While-combinator *}
   461 
       
   462 text {*
       
   463   The following term @{text "Iter X ES"} represents one iteration in the while loop.
       
   464   It arbitrarily chooses a @{text "Y"} different from @{text "X"} to remove.
       
   465 *}
       
   466 
   125 
   467 definition 
   126 definition 
   468   "Iter X ES \<equiv> (let (Y, yrhs) = SOME (Y, yrhs). (Y, yrhs) \<in> ES \<and> X \<noteq> Y
   127   "Iter X ES \<equiv> (let (Y, yrhs) = SOME (Y, yrhs). (Y, yrhs) \<in> ES \<and> X \<noteq> Y
   469                 in Remove ES Y yrhs)"
   128                 in Remove ES Y yrhs)"
   470 
   129 
   474   and     "\<And>Y yrhs. \<lbrakk>(Y, yrhs) \<in> ES; X \<noteq> Y\<rbrakk> \<Longrightarrow> Q (Remove ES Y yrhs)"
   133   and     "\<And>Y yrhs. \<lbrakk>(Y, yrhs) \<in> ES; X \<noteq> Y\<rbrakk> \<Longrightarrow> Q (Remove ES Y yrhs)"
   475   shows "Q (Iter X ES)"
   134   shows "Q (Iter X ES)"
   476 unfolding Iter_def using assms
   135 unfolding Iter_def using assms
   477 by (rule_tac a="(Y, yrhs)" in someI2) (auto)
   136 by (rule_tac a="(Y, yrhs)" in someI2) (auto)
   478 
   137 
   479 
       
   480 text {*
       
   481   The following term @{text "Reduce X ES"} repeatedly removes characteriztion equations
       
   482   for unknowns other than @{text "X"} until one is left.
       
   483 *}
       
   484 
       
   485 abbreviation
   138 abbreviation
   486   "Cond ES \<equiv> card ES \<noteq> 1"
   139   "Cond ES \<equiv> card ES \<noteq> 1"
   487 
   140 
   488 definition 
   141 definition 
   489   "Solve X ES \<equiv> while Cond (Iter X) ES"
   142   "Solve X ES \<equiv> while Cond (Iter X) ES"
   490 
   143 
   491 text {*
       
   492   Since the @{text "while"} combinator from HOL library is used to implement @{text "Solve X ES"},
       
   493   the induction principle @{thm [source] while_rule} is used to proved the desired properties
       
   494   of @{text "Solve X ES"}. For this purpose, an invariant predicate @{text "invariant"} is defined
       
   495   in terms of a series of auxilliary predicates:
       
   496 *}
       
   497 
   144 
   498 section {* Invariants *}
   145 section {* Invariants *}
   499 
   146 
   500 text {* Every variable is defined at most once in @{text ES}. *}
   147 definition 
   501 
   148   "distinctness ES \<equiv> 
   502 definition 
       
   503   "distinct_equas ES \<equiv> 
       
   504      \<forall> X rhs rhs'. (X, rhs) \<in> ES \<and> (X, rhs') \<in> ES \<longrightarrow> rhs = rhs'"
   149      \<forall> X rhs rhs'. (X, rhs) \<in> ES \<and> (X, rhs') \<in> ES \<longrightarrow> rhs = rhs'"
   505 
   150 
   506 
   151 definition 
   507 text {* 
   152   "soundness ES \<equiv> \<forall>(X, rhs) \<in> ES. X = L rhs"
   508   Every equation in @{text ES} (represented by @{text "(X, rhs)"}) 
       
   509   is valid, i.e. @{text "X = L rhs"}.
       
   510 *}
       
   511 
       
   512 definition 
       
   513   "sound_eqs ES \<equiv> \<forall>(X, rhs) \<in> ES. X = L rhs"
       
   514 
       
   515 text {*
       
   516   @{text "ardenable rhs"} requires regular expressions occuring in 
       
   517   transitional items of @{text "rhs"} do not contain empty string. This is 
       
   518   necessary for the application of Arden's transformation to @{text "rhs"}.
       
   519 *}
       
   520 
   153 
   521 definition 
   154 definition 
   522   "ardenable rhs \<equiv> (\<forall> Y r. Trn Y r \<in> rhs \<longrightarrow> [] \<notin> L r)"
   155   "ardenable rhs \<equiv> (\<forall> Y r. Trn Y r \<in> rhs \<longrightarrow> [] \<notin> L r)"
   523 
   156 
   524 text {*
       
   525   The following @{text "ardenable_all ES"} requires that Arden's transformation 
       
   526   is applicable to every equation of equational system @{text "ES"}.
       
   527 *}
       
   528 
       
   529 definition 
   157 definition 
   530   "ardenable_all ES \<equiv> \<forall>(X, rhs) \<in> ES. ardenable rhs"
   158   "ardenable_all ES \<equiv> \<forall>(X, rhs) \<in> ES. ardenable rhs"
   531 
   159 
   532 
       
   533 text {* 
       
   534   @{text "finite_rhs ES"} requires every equation in @{text "rhs"} 
       
   535   be finite.
       
   536 *}
       
   537 definition
   160 definition
   538   "finite_rhs ES \<equiv> \<forall>(X, rhs) \<in> ES. finite rhs"
   161   "finite_rhs ES \<equiv> \<forall>(X, rhs) \<in> ES. finite rhs"
   539 
   162 
   540 lemma finite_rhs_def2:
   163 lemma finite_rhs_def2:
   541   "finite_rhs ES = (\<forall> X rhs. (X, rhs) \<in> ES \<longrightarrow> finite rhs)"
   164   "finite_rhs ES = (\<forall> X rhs. (X, rhs) \<in> ES \<longrightarrow> finite rhs)"
   542 unfolding finite_rhs_def by auto
   165 unfolding finite_rhs_def by auto
   543 
   166 
   544 text {*
       
   545   @{text "classes_of rhs"} returns all variables (or equivalent classes)
       
   546   occuring in @{text "rhs"}.
       
   547   *}
       
   548 
       
   549 definition 
   167 definition 
   550   "rhss rhs \<equiv> {X | X r. Trn X r \<in> rhs}"
   168   "rhss rhs \<equiv> {X | X r. Trn X r \<in> rhs}"
   551 
   169 
   552 text {*
       
   553   @{text "lefts_of ES"} returns all variables defined by an 
       
   554   equational system @{text "ES"}.
       
   555 *}
       
   556 definition
   170 definition
   557   "lhss ES \<equiv> {Y | Y yrhs. (Y, yrhs) \<in> ES}"
   171   "lhss ES \<equiv> {Y | Y yrhs. (Y, yrhs) \<in> ES}"
   558 
   172 
   559 text {*
   173 definition 
   560   The following @{text "valid_eqs ES"} requires that every variable occuring 
   174   "validity ES \<equiv> \<forall>(X, rhs) \<in> ES. rhss rhs \<subseteq> lhss ES"
   561   on the right hand side of equations is already defined by some equation in @{text "ES"}.
   175 
   562 *}
   176 lemma rhss_union_distrib:
   563 definition 
   177   shows "rhss (A \<union> B) = rhss A \<union> rhss B"
   564   "valid_eqs ES \<equiv> \<forall>(X, rhs) \<in> ES. rhss rhs \<subseteq> lhss ES"
   178 by (auto simp add: rhss_def)
   565 
   179 
   566 
   180 lemma lhss_union_distrib:
   567 text {*
   181   shows "lhss (A \<union> B) = lhss A \<union> lhss B"
   568   The invariant @{text "invariant(ES)"} is a conjunction of all the previously defined constaints.
   182 by (auto simp add: lhss_def)
   569   *}
   183 
       
   184 
   570 definition 
   185 definition 
   571   "invariant ES \<equiv> finite ES
   186   "invariant ES \<equiv> finite ES
   572                 \<and> finite_rhs ES
   187                 \<and> finite_rhs ES
   573                 \<and> sound_eqs ES 
   188                 \<and> soundness ES 
   574                 \<and> distinct_equas ES 
   189                 \<and> distinctness ES 
   575                 \<and> ardenable_all ES 
   190                 \<and> ardenable_all ES 
   576                 \<and> valid_eqs ES"
   191                 \<and> validity ES"
   577 
   192 
   578 
   193 
   579 lemma invariantI:
   194 lemma invariantI:
   580   assumes "sound_eqs ES" "finite ES" "distinct_equas ES" "ardenable_all ES" 
   195   assumes "soundness ES" "finite ES" "distinctness ES" "ardenable_all ES" 
   581           "finite_rhs ES" "valid_eqs ES"
   196           "finite_rhs ES" "validity ES"
   582   shows "invariant ES"
   197   shows "invariant ES"
   583 using assms by (simp add: invariant_def)
   198 using assms by (simp add: invariant_def)
   584 
   199 
       
   200 
   585 subsection {* The proof of this direction *}
   201 subsection {* The proof of this direction *}
   586 
       
   587 subsubsection {* Basic properties *}
       
   588 
       
   589 text {*
       
   590   The following are some basic properties of the above definitions.
       
   591 *}
       
   592 
       
   593 
   202 
   594 lemma finite_Trn:
   203 lemma finite_Trn:
   595   assumes fin: "finite rhs"
   204   assumes fin: "finite rhs"
   596   shows "finite {r. Trn Y r \<in> rhs}"
   205   shows "finite {r. Trn Y r \<in> rhs}"
   597 proof -
   206 proof -
   616     apply(erule finite_imageD)
   225     apply(erule finite_imageD)
   617     apply(auto simp add: inj_on_def)
   226     apply(auto simp add: inj_on_def)
   618     done
   227     done
   619 qed
   228 qed
   620 
   229 
   621 lemma rexp_of_empty:
   230 lemma rhs_trm_soundness:
   622   assumes finite: "finite rhs"
       
   623   and nonempty: "ardenable rhs"
       
   624   shows "[] \<notin> L (\<Uplus> {r. Trn X r \<in> rhs})"
       
   625 using finite nonempty ardenable_def
       
   626 using finite_Trn[OF finite]
       
   627 by auto
       
   628 
       
   629 lemma lang_of_rexp_of:
       
   630   assumes finite:"finite rhs"
   231   assumes finite:"finite rhs"
   631   shows "L ({Trn X r| r. Trn X r \<in> rhs}) = X ;; (L (\<Uplus>{r. Trn X r \<in> rhs}))"
   232   shows "L ({Trn X r| r. Trn X r \<in> rhs}) = X ;; (L (\<Uplus>{r. Trn X r \<in> rhs}))"
   632 proof -
   233 proof -
   633   have "finite {r. Trn X r \<in> rhs}" 
   234   have "finite {r. Trn X r \<in> rhs}" 
   634     by (rule finite_Trn[OF finite]) 
   235     by (rule finite_Trn[OF finite]) 
   635   then show ?thesis
   236   then show "L ({Trn X r| r. Trn X r \<in> rhs}) = X ;; (L (\<Uplus>{r. Trn X r \<in> rhs}))"
   636     apply(auto simp add: Seq_def)
   237     by (simp only: L_rhs_set L_rhs_trm.simps) (auto simp add: Seq_def)
   637     apply(rule_tac x = "s\<^isub>1" in exI, rule_tac x = "s\<^isub>2" in exI)
   238 qed
   638     apply(auto)
   239 
   639     apply(rule_tac x= "Trn X xa" in exI)
   240 lemma lang_of_append_rexp:
   640     apply(auto simp add: Seq_def)
   241   "L (Append_rexp r rhs_trm) = L rhs_trm ;; L r"
   641     done
   242 by (induct rule: Append_rexp.induct)
   642 qed
       
   643 
       
   644 lemma lang_of_append:
       
   645   "L (append_rexp r rhs_item) = L rhs_item ;; L r"
       
   646 by (induct rule: append_rexp.induct)
       
   647    (auto simp add: seq_assoc)
   243    (auto simp add: seq_assoc)
   648 
   244 
   649 lemma lang_of_append_rhs:
   245 lemma lang_of_append_rexp_rhs:
   650   "L (append_rhs_rexp rhs r) = L rhs ;; L r"
   246   "L (Append_rexp_rhs rhs r) = L rhs ;; L r"
   651 unfolding append_rhs_rexp_def
   247 unfolding Append_rexp_rhs_def
   652 by (auto simp add: Seq_def lang_of_append)
   248 by (auto simp add: Seq_def lang_of_append_rexp)
   653 
   249 
   654 lemma rhss_union_distrib:
       
   655   shows "rhss (A \<union> B) = rhss A \<union> rhss B"
       
   656 by (auto simp add: rhss_def)
       
   657 
       
   658 lemma lhss_union_distrib:
       
   659   shows "lhss (A \<union> B) = lhss A \<union> lhss B"
       
   660 by (auto simp add: lhss_def)
       
   661 
   250 
   662 
   251 
   663 subsubsection {* Intialization *}
   252 subsubsection {* Intialization *}
   664 
       
   665 text {*
       
   666   The following several lemmas until @{text "init_ES_satisfy_invariant"} shows that
       
   667   the initial equational system satisfies invariant @{text "invariant"}.
       
   668 *}
       
   669 
   253 
   670 lemma defined_by_str:
   254 lemma defined_by_str:
   671   assumes "s \<in> X" "X \<in> UNIV // \<approx>A" 
   255   assumes "s \<in> X" "X \<in> UNIV // \<approx>A" 
   672   shows "X = \<approx>A `` {s}"
   256   shows "X = \<approx>A `` {s}"
   673 using assms
   257 using assms
   700   shows "X = L rhs"
   284   shows "X = L rhs"
   701 proof 
   285 proof 
   702   show "X \<subseteq> L rhs"
   286   show "X \<subseteq> L rhs"
   703   proof
   287   proof
   704     fix x
   288     fix x
   705     assume "(1)": "x \<in> X"
   289     assume in_X: "x \<in> X"
   706     show "x \<in> L rhs"          
   290     { assume empty: "x = []"
   707     proof (cases "x = []")
   291       then have "x \<in> L rhs" using X_in_eqs in_X
   708       assume empty: "x = []"
   292 	unfolding Init_def Init_rhs_def
   709       thus ?thesis using X_in_eqs "(1)"
   293         by auto
   710         by (auto simp: Init_def Init_rhs_def)
   294     }
   711     next
   295     moreover
   712       assume not_empty: "x \<noteq> []"
   296     { assume not_empty: "x \<noteq> []"
   713       then obtain clist c where decom: "x = clist @ [c]"
   297       then obtain s c where decom: "x = s @ [c]"
   714         by (case_tac x rule:rev_cases, auto)
   298 	using rev_cases by blast
   715       have "X \<in> UNIV // \<approx>A" using X_in_eqs by (auto simp:Init_def)
   299       have "X \<in> UNIV // \<approx>A" using X_in_eqs unfolding Init_def by auto
   716       then obtain Y 
   300       then obtain Y where "Y \<in> UNIV // \<approx>A" "Y ;; {[c]} \<subseteq> X" "s \<in> Y"
   717         where "Y \<in> UNIV // \<approx>A" 
   301         using decom in_X every_eqclass_has_transition by blast
   718         and "Y ;; {[c]} \<subseteq> X"
   302       then have "x \<in> L {Trn Y (CHAR c)| Y c. Y \<in> UNIV // \<approx>A \<and> Y \<Turnstile>c\<Rightarrow> X}"
   719         and "clist \<in> Y"
       
   720         using decom "(1)" every_eqclass_has_transition by blast
       
   721       hence 
       
   722         "x \<in> L {Trn Y (CHAR c)| Y c. Y \<in> UNIV // \<approx>A \<and> Y \<Turnstile>c\<Rightarrow> X}"
       
   723         unfolding transition_def
   303         unfolding transition_def
   724 	using "(1)" decom
   304 	using decom by (force simp add: Seq_def)
   725         by (simp, rule_tac x = "Trn Y (CHAR c)" in exI, simp add:Seq_def)
   305       then have "x \<in> L rhs" using X_in_eqs in_X
   726       thus ?thesis using X_in_eqs "(1)"	
   306 	unfolding Init_def Init_rhs_def by simp
   727         by (simp add: Init_def Init_rhs_def)
   307     }
   728     qed
   308     ultimately show "x \<in> L rhs" by blast
   729   qed
   309   qed
   730 next
   310 next
   731   show "L rhs \<subseteq> X" using X_in_eqs
   311   show "L rhs \<subseteq> X" using X_in_eqs
   732     by (auto simp:Init_def Init_rhs_def transition_def) 
   312     unfolding Init_def Init_rhs_def transition_def
       
   313     by auto 
   733 qed
   314 qed
   734 
   315 
   735 lemma test:
   316 lemma test:
   736   assumes X_in_eqs: "(X, rhs) \<in> Init (UNIV // \<approx>A)"
   317   assumes X_in_eqs: "(X, rhs) \<in> Init (UNIV // \<approx>A)"
   737   shows "X = \<Union> (L `  rhs)"
   318   shows "X = \<Union> (L `  rhs)"
   738 using assms
   319 using assms l_eq_r_in_eqs by (simp)
   739 by (drule_tac l_eq_r_in_eqs) (simp)
       
   740 
       
   741 
   320 
   742 lemma finite_Init_rhs: 
   321 lemma finite_Init_rhs: 
   743   assumes finite: "finite CS"
   322   assumes finite: "finite CS"
   744   shows "finite (Init_rhs CS X)"
   323   shows "finite (Init_rhs CS X)"
   745 proof-
   324 proof-
   757 
   336 
   758 lemma Init_ES_satisfies_invariant:
   337 lemma Init_ES_satisfies_invariant:
   759   assumes finite_CS: "finite (UNIV // \<approx>A)"
   338   assumes finite_CS: "finite (UNIV // \<approx>A)"
   760   shows "invariant (Init (UNIV // \<approx>A))"
   339   shows "invariant (Init (UNIV // \<approx>A))"
   761 proof (rule invariantI)
   340 proof (rule invariantI)
   762   show "sound_eqs (Init (UNIV // \<approx>A))"
   341   show "soundness (Init (UNIV // \<approx>A))"
   763     unfolding sound_eqs_def 
   342     unfolding soundness_def 
   764     using l_eq_r_in_eqs by auto
   343     using l_eq_r_in_eqs by auto
   765   show "finite (Init (UNIV // \<approx>A))" using finite_CS
   344   show "finite (Init (UNIV // \<approx>A))" using finite_CS
   766     unfolding Init_def by simp
   345     unfolding Init_def by simp
   767   show "distinct_equas (Init (UNIV // \<approx>A))"     
   346   show "distinctness (Init (UNIV // \<approx>A))"     
   768     unfolding distinct_equas_def Init_def by simp
   347     unfolding distinctness_def Init_def by simp
   769   show "ardenable_all (Init (UNIV // \<approx>A))"
   348   show "ardenable_all (Init (UNIV // \<approx>A))"
   770     unfolding ardenable_all_def Init_def Init_rhs_def ardenable_def
   349     unfolding ardenable_all_def Init_def Init_rhs_def ardenable_def
   771    by auto 
   350    by auto 
   772   show "finite_rhs (Init (UNIV // \<approx>A))"
   351   show "finite_rhs (Init (UNIV // \<approx>A))"
   773     using finite_Init_rhs[OF finite_CS]
   352     using finite_Init_rhs[OF finite_CS]
   774     unfolding finite_rhs_def Init_def by auto
   353     unfolding finite_rhs_def Init_def by auto
   775   show "valid_eqs (Init (UNIV // \<approx>A))"
   354   show "validity (Init (UNIV // \<approx>A))"
   776     unfolding valid_eqs_def Init_def Init_rhs_def rhss_def lhss_def
   355     unfolding validity_def Init_def Init_rhs_def rhss_def lhss_def
   777     by auto
   356     by auto
   778 qed
   357 qed
   779 
   358 
   780 subsubsection {* Interation step *}
   359 subsubsection {* Interation step *}
   781 
       
   782 text {*
       
   783   From this point until @{text "iteration_step"}, 
       
   784   the correctness of the iteration step @{text "Iter X ES"} is proved.
       
   785 *}
       
   786 
   360 
   787 lemma Arden_keeps_eq:
   361 lemma Arden_keeps_eq:
   788   assumes l_eq_r: "X = L rhs"
   362   assumes l_eq_r: "X = L rhs"
   789   and not_empty: "ardenable rhs"
   363   and not_empty: "ardenable rhs"
   790   and finite: "finite rhs"
   364   and finite: "finite rhs"
   791   shows "X = L (Arden X rhs)"
   365   shows "X = L (Arden X rhs)"
   792 proof -
   366 proof -
   793   def A \<equiv> "L (\<Uplus>{r. Trn X r \<in> rhs})"
   367   def A \<equiv> "L (\<Uplus>{r. Trn X r \<in> rhs})"
   794   def b \<equiv> "rhs - {Trn X r | r. Trn X r \<in> rhs}"
   368   def b \<equiv> "{Trn X r | r. Trn X r \<in> rhs}"
   795   def B \<equiv> "L b" 
   369   def B \<equiv> "L (rhs - b)"
   796   have "X = B ;; A\<star>"
   370   have not_empty2: "[] \<notin> A" 
   797   proof -
   371     using finite_Trn[OF finite] not_empty
   798     have "L rhs = L({Trn X r | r. Trn X r \<in> rhs} \<union> b)" by (auto simp: b_def)
   372     unfolding A_def ardenable_def by simp
   799     also have "\<dots> = X ;; A \<union> B"
   373   have "X = L rhs" using l_eq_r by simp
   800       unfolding L_rhs_union_distrib[symmetric]
   374   also have "\<dots> = L (b \<union> (rhs - b))" unfolding b_def by auto
   801       by (simp only: lang_of_rexp_of finite B_def A_def)
   375   also have "\<dots> = L b \<union> B" unfolding B_def by (simp only: L_rhs_union_distrib)
   802     finally show ?thesis
   376   also have "\<dots> = X ;; A \<union> B"
   803       apply(rule_tac arden[THEN iffD1])
   377     unfolding b_def
   804       apply(simp (no_asm) add: A_def)
   378     unfolding rhs_trm_soundness[OF finite]
   805       using finite_Trn[OF finite] not_empty
   379     unfolding A_def
   806       apply(simp add: ardenable_def)
   380     by blast
   807       using l_eq_r
   381   finally have "X = X ;; A \<union> B" . 
   808       apply(simp)
   382   then have "X = B ;; A\<star>"
   809       done
   383     by (simp add: arden[OF not_empty2])
   810   qed
   384   also have "\<dots> = L (Arden X rhs)"
   811   moreover have "L (Arden X rhs) = B ;; A\<star>"
   385     unfolding Arden_def A_def B_def b_def
   812     by (simp only:Arden_def L_rhs_union_distrib lang_of_append_rhs 
   386     by (simp only: lang_of_append_rexp_rhs L_rexp.simps)
   813                   B_def A_def b_def L_rexp.simps seq_union_distrib_left)
   387   finally show "X = L (Arden X rhs)" by simp
   814    ultimately show ?thesis by simp
       
   815 qed 
   388 qed 
   816 
   389 
   817 lemma append_keeps_finite:
   390 lemma Append_keeps_finite:
   818   "finite rhs \<Longrightarrow> finite (append_rhs_rexp rhs r)"
   391   "finite rhs \<Longrightarrow> finite (Append_rexp_rhs rhs r)"
   819 by (auto simp:append_rhs_rexp_def)
   392 by (auto simp:Append_rexp_rhs_def)
   820 
   393 
   821 lemma Arden_keeps_finite:
   394 lemma Arden_keeps_finite:
   822   "finite rhs \<Longrightarrow> finite (Arden X rhs)"
   395   "finite rhs \<Longrightarrow> finite (Arden X rhs)"
   823 by (auto simp:Arden_def append_keeps_finite)
   396 by (auto simp:Arden_def Append_keeps_finite)
   824 
   397 
   825 lemma append_keeps_nonempty:
   398 lemma Append_keeps_nonempty:
   826   "ardenable rhs \<Longrightarrow> ardenable (append_rhs_rexp rhs r)"
   399   "ardenable rhs \<Longrightarrow> ardenable (Append_rexp_rhs rhs r)"
   827 apply (auto simp:ardenable_def append_rhs_rexp_def)
   400 apply (auto simp:ardenable_def Append_rexp_rhs_def)
   828 by (case_tac x, auto simp:Seq_def)
   401 by (case_tac x, auto simp:Seq_def)
   829 
   402 
   830 lemma nonempty_set_sub:
   403 lemma nonempty_set_sub:
   831   "ardenable rhs \<Longrightarrow> ardenable (rhs - A)"
   404   "ardenable rhs \<Longrightarrow> ardenable (rhs - A)"
   832 by (auto simp:ardenable_def)
   405 by (auto simp:ardenable_def)
   835   "\<lbrakk>ardenable rhs; ardenable rhs'\<rbrakk> \<Longrightarrow> ardenable (rhs \<union> rhs')"
   408   "\<lbrakk>ardenable rhs; ardenable rhs'\<rbrakk> \<Longrightarrow> ardenable (rhs \<union> rhs')"
   836 by (auto simp:ardenable_def)
   409 by (auto simp:ardenable_def)
   837 
   410 
   838 lemma Arden_keeps_nonempty:
   411 lemma Arden_keeps_nonempty:
   839   "ardenable rhs \<Longrightarrow> ardenable (Arden X rhs)"
   412   "ardenable rhs \<Longrightarrow> ardenable (Arden X rhs)"
   840 by (simp only:Arden_def append_keeps_nonempty nonempty_set_sub)
   413 by (simp only:Arden_def Append_keeps_nonempty nonempty_set_sub)
   841 
   414 
   842 
   415 
   843 lemma Subst_keeps_nonempty:
   416 lemma Subst_keeps_nonempty:
   844   "\<lbrakk>ardenable rhs; ardenable xrhs\<rbrakk> \<Longrightarrow> ardenable (Subst rhs X xrhs)"
   417   "\<lbrakk>ardenable rhs; ardenable xrhs\<rbrakk> \<Longrightarrow> ardenable (Subst rhs X xrhs)"
   845 by (simp only:Subst_def append_keeps_nonempty  nonempty_set_union nonempty_set_sub)
   418 by (simp only: Subst_def Append_keeps_nonempty nonempty_set_union nonempty_set_sub)
   846 
   419 
   847 lemma Subst_keeps_eq:
   420 lemma Subst_keeps_eq:
   848   assumes substor: "X = L xrhs"
   421   assumes substor: "X = L xrhs"
   849   and finite: "finite rhs"
   422   and finite: "finite rhs"
   850   shows "L (Subst rhs X xrhs) = L rhs" (is "?Left = ?Right")
   423   shows "L (Subst rhs X xrhs) = L rhs" (is "?Left = ?Right")
   851 proof-
   424 proof-
   852   def A \<equiv> "L (rhs - {Trn X r | r. Trn X r \<in> rhs})"
   425   def A \<equiv> "L (rhs - {Trn X r | r. Trn X r \<in> rhs})"
   853   have "?Left = A \<union> L (append_rhs_rexp xrhs (\<Uplus>{r. Trn X r \<in> rhs}))"
   426   have "?Left = A \<union> L (Append_rexp_rhs xrhs (\<Uplus>{r. Trn X r \<in> rhs}))"
   854     unfolding Subst_def
   427     unfolding Subst_def
   855     unfolding L_rhs_union_distrib[symmetric]
   428     unfolding L_rhs_union_distrib[symmetric]
   856     by (simp add: A_def)
   429     by (simp add: A_def)
   857   moreover have "?Right = A \<union> L ({Trn X r | r. Trn X r \<in> rhs})"
   430   moreover have "?Right = A \<union> L ({Trn X r | r. Trn X r \<in> rhs})"
   858   proof-
   431   proof-
   860     thus ?thesis 
   433     thus ?thesis 
   861       unfolding A_def
   434       unfolding A_def
   862       unfolding L_rhs_union_distrib
   435       unfolding L_rhs_union_distrib
   863       by simp
   436       by simp
   864   qed
   437   qed
   865   moreover have "L (append_rhs_rexp xrhs (\<Uplus>{r. Trn X r \<in> rhs})) = L ({Trn X r | r. Trn X r \<in> rhs})" 
   438   moreover have "L (Append_rexp_rhs xrhs (\<Uplus>{r. Trn X r \<in> rhs})) = L ({Trn X r | r. Trn X r \<in> rhs})" 
   866     using finite substor  by (simp only:lang_of_append_rhs lang_of_rexp_of)
   439     using finite substor by (simp only: lang_of_append_rexp_rhs rhs_trm_soundness)
   867   ultimately show ?thesis by simp
   440   ultimately show ?thesis by simp
   868 qed
   441 qed
   869 
   442 
   870 lemma Subst_keeps_finite_rhs:
   443 lemma Subst_keeps_finite_rhs:
   871   "\<lbrakk>finite rhs; finite yrhs\<rbrakk> \<Longrightarrow> finite (Subst rhs Y yrhs)"
   444   "\<lbrakk>finite rhs; finite yrhs\<rbrakk> \<Longrightarrow> finite (Subst rhs Y yrhs)"
   872 by (auto simp:Subst_def append_keeps_finite)
   445 by (auto simp: Subst_def Append_keeps_finite)
   873 
   446 
   874 lemma Subst_all_keeps_finite:
   447 lemma Subst_all_keeps_finite:
   875   assumes finite: "finite ES"
   448   assumes finite: "finite ES"
   876   shows "finite (Subst_all ES Y yrhs)"
   449   shows "finite (Subst_all ES Y yrhs)"
   877 proof -
   450 proof -
   887 lemma Subst_all_keeps_finite_rhs:
   460 lemma Subst_all_keeps_finite_rhs:
   888   "\<lbrakk>finite_rhs ES; finite yrhs\<rbrakk> \<Longrightarrow> finite_rhs (Subst_all ES Y yrhs)"
   461   "\<lbrakk>finite_rhs ES; finite yrhs\<rbrakk> \<Longrightarrow> finite_rhs (Subst_all ES Y yrhs)"
   889 by (auto intro:Subst_keeps_finite_rhs simp add:Subst_all_def finite_rhs_def)
   462 by (auto intro:Subst_keeps_finite_rhs simp add:Subst_all_def finite_rhs_def)
   890 
   463 
   891 lemma append_rhs_keeps_cls:
   464 lemma append_rhs_keeps_cls:
   892   "rhss (append_rhs_rexp rhs r) = rhss rhs"
   465   "rhss (Append_rexp_rhs rhs r) = rhss rhs"
   893 apply (auto simp:rhss_def append_rhs_rexp_def)
   466 apply (auto simp:rhss_def Append_rexp_rhs_def)
   894 apply (case_tac xa, auto simp:image_def)
   467 apply (case_tac xa, auto simp:image_def)
   895 by (rule_tac x = "SEQ ra r" in exI, rule_tac x = "Trn x ra" in bexI, simp+)
   468 by (rule_tac x = "SEQ ra r" in exI, rule_tac x = "Trn x ra" in bexI, simp+)
   896 
   469 
   897 lemma Arden_removes_cl:
   470 lemma Arden_removes_cl:
   898   "rhss (Arden Y yrhs) = rhss yrhs - {Y}"
   471   "rhss (Arden Y yrhs) = rhss yrhs - {Y}"
   907   "X \<notin> rhss xrhs \<Longrightarrow> 
   480   "X \<notin> rhss xrhs \<Longrightarrow> 
   908       rhss (Subst rhs X xrhs) = rhss rhs \<union> rhss xrhs - {X}"
   481       rhss (Subst rhs X xrhs) = rhss rhs \<union> rhss xrhs - {X}"
   909 apply (simp only:Subst_def append_rhs_keeps_cls rhss_union_distrib)
   482 apply (simp only:Subst_def append_rhs_keeps_cls rhss_union_distrib)
   910 by (auto simp:rhss_def)
   483 by (auto simp:rhss_def)
   911 
   484 
   912 lemma Subst_all_keeps_valid_eqs:
   485 lemma Subst_all_keeps_validity:
   913   assumes sc: "valid_eqs (ES \<union> {(Y, yrhs)})"        (is "valid_eqs ?A")
   486   assumes sc: "validity (ES \<union> {(Y, yrhs)})"        (is "validity ?A")
   914   shows "valid_eqs (Subst_all ES Y (Arden Y yrhs))"  (is "valid_eqs ?B")
   487   shows "validity (Subst_all ES Y (Arden Y yrhs))"  (is "validity ?B")
   915 proof -
   488 proof -
   916   { fix X xrhs'
   489   { fix X xrhs'
   917     assume "(X, xrhs') \<in> ?B"
   490     assume "(X, xrhs') \<in> ?B"
   918     then obtain xrhs 
   491     then obtain xrhs 
   919       where xrhs_xrhs': "xrhs' = Subst xrhs Y (Arden Y yrhs)"
   492       where xrhs_xrhs': "xrhs' = Subst xrhs Y (Arden Y yrhs)"
   928           have "Y \<notin> rhss (Arden Y yrhs)" 
   501           have "Y \<notin> rhss (Arden Y yrhs)" 
   929             using Arden_removes_cl by simp
   502             using Arden_removes_cl by simp
   930           thus ?thesis using xrhs_xrhs' by (auto simp:Subst_updates_cls)
   503           thus ?thesis using xrhs_xrhs' by (auto simp:Subst_updates_cls)
   931         qed
   504         qed
   932         moreover have "rhss xrhs \<subseteq> lhss ES \<union> {Y}" using X_in sc
   505         moreover have "rhss xrhs \<subseteq> lhss ES \<union> {Y}" using X_in sc
   933           apply (simp only:valid_eqs_def lhss_union_distrib)
   506           apply (simp only:validity_def lhss_union_distrib)
   934           by (drule_tac x = "(X, xrhs)" in bspec, auto simp:lhss_def)
   507           by (drule_tac x = "(X, xrhs)" in bspec, auto simp:lhss_def)
   935         moreover have "rhss (Arden Y yrhs) \<subseteq> lhss ES \<union> {Y}" 
   508         moreover have "rhss (Arden Y yrhs) \<subseteq> lhss ES \<union> {Y}" 
   936           using sc 
   509           using sc 
   937           by (auto simp add:Arden_removes_cl valid_eqs_def lhss_def)
   510           by (auto simp add:Arden_removes_cl validity_def lhss_def)
   938         ultimately show ?thesis by auto
   511         ultimately show ?thesis by auto
   939       qed
   512       qed
   940       ultimately show ?thesis by simp
   513       ultimately show ?thesis by simp
   941     qed
   514     qed
   942   } thus ?thesis by (auto simp only:Subst_all_def valid_eqs_def)
   515   } thus ?thesis by (auto simp only:Subst_all_def validity_def)
   943 qed
   516 qed
   944 
   517 
   945 lemma Subst_all_satisfies_invariant:
   518 lemma Subst_all_satisfies_invariant:
   946   assumes invariant_ES: "invariant (ES \<union> {(Y, yrhs)})"
   519   assumes invariant_ES: "invariant (ES \<union> {(Y, yrhs)})"
   947   shows "invariant (Subst_all ES Y (Arden Y yrhs))"
   520   shows "invariant (Subst_all ES Y (Arden Y yrhs))"
   948 proof (rule invariantI)
   521 proof (rule invariantI)
   949   have Y_eq_yrhs: "Y = L yrhs" 
   522   have Y_eq_yrhs: "Y = L yrhs" 
   950     using invariant_ES by (simp only:invariant_def sound_eqs_def, blast)
   523     using invariant_ES by (simp only:invariant_def soundness_def, blast)
   951    have finite_yrhs: "finite yrhs" 
   524    have finite_yrhs: "finite yrhs" 
   952     using invariant_ES by (auto simp:invariant_def finite_rhs_def)
   525     using invariant_ES by (auto simp:invariant_def finite_rhs_def)
   953   have nonempty_yrhs: "ardenable yrhs" 
   526   have nonempty_yrhs: "ardenable yrhs" 
   954     using invariant_ES by (auto simp:invariant_def ardenable_all_def)
   527     using invariant_ES by (auto simp:invariant_def ardenable_all_def)
   955   show "sound_eqs (Subst_all ES Y (Arden Y yrhs))"
   528   show "soundness (Subst_all ES Y (Arden Y yrhs))"
   956   proof -
   529   proof -
   957     have "Y = L (Arden Y yrhs)" 
   530     have "Y = L (Arden Y yrhs)" 
   958       using Y_eq_yrhs invariant_ES finite_yrhs
   531       using Y_eq_yrhs invariant_ES finite_yrhs
   959       using finite_Trn[OF finite_yrhs]
   532       using finite_Trn[OF finite_yrhs]
   960       apply(rule_tac Arden_keeps_eq)
   533       apply(rule_tac Arden_keeps_eq)
   961       apply(simp_all)
   534       apply(simp_all)
   962       unfolding invariant_def ardenable_all_def ardenable_def
   535       unfolding invariant_def ardenable_all_def ardenable_def
   963       apply(auto)
   536       apply(auto)
   964       done
   537       done
   965     thus ?thesis using invariant_ES
   538     thus ?thesis using invariant_ES
   966       unfolding invariant_def finite_rhs_def2 sound_eqs_def Subst_all_def
   539       unfolding invariant_def finite_rhs_def2 soundness_def Subst_all_def
   967       by (auto simp add: Subst_keeps_eq simp del: L_rhs.simps)
   540       by (auto simp add: Subst_keeps_eq simp del: L_rhs.simps)
   968   qed
   541   qed
   969   show "finite (Subst_all ES Y (Arden Y yrhs))" 
   542   show "finite (Subst_all ES Y (Arden Y yrhs))" 
   970     using invariant_ES by (simp add:invariant_def Subst_all_keeps_finite)
   543     using invariant_ES by (simp add:invariant_def Subst_all_keeps_finite)
   971   show "distinct_equas (Subst_all ES Y (Arden Y yrhs))" 
   544   show "distinctness (Subst_all ES Y (Arden Y yrhs))" 
   972     using invariant_ES 
   545     using invariant_ES 
   973     unfolding distinct_equas_def Subst_all_def invariant_def by auto
   546     unfolding distinctness_def Subst_all_def invariant_def by auto
   974   show "ardenable_all (Subst_all ES Y (Arden Y yrhs))"
   547   show "ardenable_all (Subst_all ES Y (Arden Y yrhs))"
   975   proof - 
   548   proof - 
   976     { fix X rhs
   549     { fix X rhs
   977       assume "(X, rhs) \<in> ES"
   550       assume "(X, rhs) \<in> ES"
   978       hence "ardenable rhs"  using prems invariant_ES  
   551       hence "ardenable rhs"  using invariant_ES  
   979         by (auto simp add:invariant_def ardenable_all_def)
   552         by (auto simp add:invariant_def ardenable_all_def)
   980       with nonempty_yrhs 
   553       with nonempty_yrhs 
   981       have "ardenable (Subst rhs Y (Arden Y yrhs))"
   554       have "ardenable (Subst rhs Y (Arden Y yrhs))"
   982         by (simp add:nonempty_yrhs 
   555         by (simp add:nonempty_yrhs 
   983                Subst_keeps_nonempty Arden_keeps_nonempty)
   556                Subst_keeps_nonempty Arden_keeps_nonempty)
   994       thus ?thesis using Arden_keeps_finite by simp
   567       thus ?thesis using Arden_keeps_finite by simp
   995     qed
   568     qed
   996     ultimately show ?thesis 
   569     ultimately show ?thesis 
   997       by (simp add:Subst_all_keeps_finite_rhs)
   570       by (simp add:Subst_all_keeps_finite_rhs)
   998   qed
   571   qed
   999   show "valid_eqs (Subst_all ES Y (Arden Y yrhs))"
   572   show "validity (Subst_all ES Y (Arden Y yrhs))"
  1000     using invariant_ES Subst_all_keeps_valid_eqs by (simp add:invariant_def)
   573     using invariant_ES Subst_all_keeps_validity by (simp add:invariant_def)
  1001 qed
   574 qed
  1002 
   575 
  1003 lemma Remove_in_card_measure:
   576 lemma Remove_in_card_measure:
  1004   assumes finite: "finite ES"
   577   assumes finite: "finite ES"
  1005   and     in_ES: "(X, rhs) \<in> ES"
   578   and     in_ES: "(X, rhs) \<in> ES"
  1047   have fin: "finite ES" using Inv_ES unfolding invariant_def by simp
   620   have fin: "finite ES" using Inv_ES unfolding invariant_def by simp
  1048   then obtain Y yrhs 
   621   then obtain Y yrhs 
  1049     where Y_in_ES: "(Y, yrhs) \<in> ES" and not_eq: "(X, xrhs) \<noteq> (Y, yrhs)" 
   622     where Y_in_ES: "(Y, yrhs) \<in> ES" and not_eq: "(X, xrhs) \<noteq> (Y, yrhs)" 
  1050     using Cnd X_in_ES by (drule_tac card_noteq_1_has_more) (auto)
   623     using Cnd X_in_ES by (drule_tac card_noteq_1_has_more) (auto)
  1051   then have "(Y, yrhs) \<in> ES " "X \<noteq> Y"  
   624   then have "(Y, yrhs) \<in> ES " "X \<noteq> Y"  
  1052     using X_in_ES Inv_ES unfolding invariant_def distinct_equas_def
   625     using X_in_ES Inv_ES unfolding invariant_def distinctness_def
  1053     by auto
   626     by auto
  1054   then show "(Iter X ES, ES) \<in> measure card" 
   627   then show "(Iter X ES, ES) \<in> measure card" 
  1055   apply(rule IterI2)
   628   apply(rule IterI2)
  1056   apply(rule Remove_in_card_measure)
   629   apply(rule Remove_in_card_measure)
  1057   apply(simp_all add: fin)
   630   apply(simp_all add: fin)
  1067   have finite_ES: "finite ES" using Inv_ES by (simp add: invariant_def)
   640   have finite_ES: "finite ES" using Inv_ES by (simp add: invariant_def)
  1068   then obtain Y yrhs 
   641   then obtain Y yrhs 
  1069     where Y_in_ES: "(Y, yrhs) \<in> ES" and not_eq: "(X, xrhs) \<noteq> (Y, yrhs)" 
   642     where Y_in_ES: "(Y, yrhs) \<in> ES" and not_eq: "(X, xrhs) \<noteq> (Y, yrhs)" 
  1070     using Cnd X_in_ES by (drule_tac card_noteq_1_has_more) (auto)
   643     using Cnd X_in_ES by (drule_tac card_noteq_1_has_more) (auto)
  1071   then have "(Y, yrhs) \<in> ES" "X \<noteq> Y" 
   644   then have "(Y, yrhs) \<in> ES" "X \<noteq> Y" 
  1072     using X_in_ES Inv_ES unfolding invariant_def distinct_equas_def
   645     using X_in_ES Inv_ES unfolding invariant_def distinctness_def
  1073     by auto
   646     by auto
  1074   then show "invariant (Iter X ES)" 
   647   then show "invariant (Iter X ES)" 
  1075   proof(rule IterI2)
   648   proof(rule IterI2)
  1076     fix Y yrhs
   649     fix Y yrhs
  1077     assume h: "(Y, yrhs) \<in> ES" "X \<noteq> Y"
   650     assume h: "(Y, yrhs) \<in> ES" "X \<noteq> Y"
  1078     then have "ES - {(Y, yrhs)} \<union> {(Y, yrhs)} = ES" by auto
   651     then have "ES - {(Y, yrhs)} \<union> {(Y, yrhs)} = ES" by auto
  1079     then show "invariant (Remove ES Y yrhs)" unfolding Remove_def
   652     then show "invariant (Remove ES Y yrhs)" unfolding Remove_def
  1080       using Inv_ES
   653       using Inv_ES
  1081       thm Subst_all_satisfies_invariant
       
  1082       by (rule_tac Subst_all_satisfies_invariant) (simp) 
   654       by (rule_tac Subst_all_satisfies_invariant) (simp) 
  1083   qed
   655   qed
  1084 qed
   656 qed
  1085 
   657 
  1086 lemma iteration_step_ex:
   658 lemma iteration_step_ex:
  1089   and    Cnd: "Cond ES"
   661   and    Cnd: "Cond ES"
  1090   shows "\<exists>xrhs'. (X, xrhs') \<in> (Iter X ES)"
   662   shows "\<exists>xrhs'. (X, xrhs') \<in> (Iter X ES)"
  1091 proof -
   663 proof -
  1092   have finite_ES: "finite ES" using Inv_ES by (simp add: invariant_def)
   664   have finite_ES: "finite ES" using Inv_ES by (simp add: invariant_def)
  1093   then obtain Y yrhs 
   665   then obtain Y yrhs 
  1094     where Y_in_ES: "(Y, yrhs) \<in> ES" and not_eq: "(X, xrhs) \<noteq> (Y, yrhs)" 
   666     where "(Y, yrhs) \<in> ES" "(X, xrhs) \<noteq> (Y, yrhs)" 
  1095     using Cnd X_in_ES by (drule_tac card_noteq_1_has_more) (auto)
   667     using Cnd X_in_ES by (drule_tac card_noteq_1_has_more) (auto)
  1096   then have "(Y, yrhs) \<in> ES " "X \<noteq> Y"  
   668   then have "(Y, yrhs) \<in> ES " "X \<noteq> Y"  
  1097     using X_in_ES Inv_ES unfolding invariant_def distinct_equas_def
   669     using X_in_ES Inv_ES unfolding invariant_def distinctness_def
  1098     by auto
   670     by auto
  1099   then show "\<exists>xrhs'. (X, xrhs') \<in> (Iter X ES)" 
   671   then show "\<exists>xrhs'. (X, xrhs') \<in> (Iter X ES)" 
  1100   apply(rule IterI2)
   672   apply(rule IterI2)
  1101   unfolding Remove_def
   673   unfolding Remove_def
  1102   apply(rule Subst_all_cls_remains)
   674   apply(rule Subst_all_cls_remains)
  1157   obtain xrhs where Inv_ES: "invariant {(X, xrhs)}"
   729   obtain xrhs where Inv_ES: "invariant {(X, xrhs)}"
  1158     using Solve by metis
   730     using Solve by metis
  1159 
   731 
  1160   def A \<equiv> "Arden X xrhs"
   732   def A \<equiv> "Arden X xrhs"
  1161   have "rhss xrhs \<subseteq> {X}" using Inv_ES 
   733   have "rhss xrhs \<subseteq> {X}" using Inv_ES 
  1162     unfolding valid_eqs_def invariant_def rhss_def lhss_def
   734     unfolding validity_def invariant_def rhss_def lhss_def
  1163     by auto
   735     by auto
  1164   then have "rhss A = {}" unfolding A_def 
   736   then have "rhss A = {}" unfolding A_def 
  1165     by (simp add: Arden_removes_cl)
   737     by (simp add: Arden_removes_cl)
  1166   then have eq: "{Lam r | r. Lam r \<in> A} = A" unfolding rhss_def
   738   then have eq: "{Lam r | r. Lam r \<in> A} = A" unfolding rhss_def
  1167     by (auto, case_tac x, auto)
   739     by (auto, case_tac x, auto)
  1168   
   740   
  1169   have "finite A" using Inv_ES unfolding A_def invariant_def finite_rhs_def
   741   have "finite A" using Inv_ES unfolding A_def invariant_def finite_rhs_def
  1170     using Arden_keeps_finite by auto
   742     using Arden_keeps_finite by auto
  1171   then have fin: "finite {r. Lam r \<in> A}" by (rule finite_Lam)
   743   then have fin: "finite {r. Lam r \<in> A}" by (rule finite_Lam)
  1172 
   744 
  1173   have "X = L xrhs" using Inv_ES unfolding invariant_def sound_eqs_def
   745   have "X = L xrhs" using Inv_ES unfolding invariant_def soundness_def
  1174     by simp
   746     by simp
  1175   then have "X = L A" using Inv_ES 
   747   then have "X = L A" using Inv_ES 
  1176     unfolding A_def invariant_def ardenable_all_def finite_rhs_def 
   748     unfolding A_def invariant_def ardenable_all_def finite_rhs_def 
  1177     by (rule_tac Arden_keeps_eq) (simp_all add: finite_Trn)
   749     by (rule_tac Arden_keeps_eq) (simp_all add: finite_Trn)
  1178   then have "X = L {Lam r | r. Lam r \<in> A}" using eq by simp
   750   then have "X = L {Lam r | r. Lam r \<in> A}" using eq by simp