Myhill.thy
author wu
Fri, 31 Dec 2010 13:47:53 +0000
changeset 28 cef2893f353b
parent 27 90a57a533b0c
child 29 c64241fa4dff
permissions -rw-r--r--
Rewritten of hard direction once more. To make it looking better.

theory MyhillNerode
  imports "Main" "List_Prefix"
begin

text {* sequential composition of languages *}
definition
  Seq :: "string set \<Rightarrow> string set \<Rightarrow> string set" ("_ ;; _" [100,100] 100)
where 
  "L1 ;; L2 = {s1 @ s2 | s1 s2. s1 \<in> L1 \<and> s2 \<in> L2}"

inductive_set
  Star :: "string set \<Rightarrow> string set" ("_\<star>" [101] 102)
  for L :: "string set"
where
  start[intro]: "[] \<in> L\<star>"
| step[intro]:  "\<lbrakk>s1 \<in> L; s2 \<in> L\<star>\<rbrakk> \<Longrightarrow> s1@s2 \<in> L\<star>" 

lemma seq_union_distrib:
  "(A \<union> B) ;; C = (A ;; C) \<union> (B ;; C)"
by (auto simp:Seq_def)

lemma seq_assoc:
  "(A ;; B) ;; C = A ;; (B ;; C)"
unfolding Seq_def
apply(auto)
apply(metis)
by (metis append_assoc)

lemma union_seq:
  "\<Union> {f x y ;; z| x y. P x y } = (\<Union> {f x y|x y. P x y });; z"
apply (auto simp add:Seq_def)
apply metis
done

theorem ardens_revised:
  assumes nemp: "[] \<notin> A"
  shows "(X = X ;; A \<union> B) \<longleftrightarrow> (X = B ;; A\<star>)"
proof
  assume eq: "X = B ;; A\<star>"
  have "A\<star> =  {[]} \<union> A\<star> ;; A" sorry
  then have "B ;; A\<star> = B ;; ({[]} \<union> A\<star> ;; A)" unfolding Seq_def by simp
  also have "\<dots> = B \<union> B ;; (A\<star> ;; A)"  unfolding Seq_def by auto
  also have "\<dots> = B \<union> (B ;; A\<star>) ;; A"  unfolding Seq_def
    by (auto) (metis append_assoc)+
  finally show "X = X ;; A \<union> B" using eq by auto
next
  assume "X = X ;; A \<union> B"
  then have "B \<subseteq> X" "X ;; A \<subseteq> X" by auto
  show "X = B ;; A\<star>" sorry
qed

datatype rexp =
  NULL
| EMPTY
| CHAR char
| SEQ rexp rexp
| ALT rexp rexp
| STAR rexp

consts L:: "'a \<Rightarrow> string set"

overloading L_rexp \<equiv> "L::  rexp \<Rightarrow> string set"
begin

fun
  L_rexp :: "rexp \<Rightarrow> string set"
where
    "L_rexp (NULL) = {}"
  | "L_rexp (EMPTY) = {[]}"
  | "L_rexp (CHAR c) = {[c]}"
  | "L_rexp (SEQ r1 r2) = (L_rexp r1) ;; (L_rexp r2)"
  | "L_rexp (ALT r1 r2) = (L_rexp r1) \<union> (L_rexp r2)"
  | "L_rexp (STAR r) = (L_rexp r)\<star>"
end

definition 
  folds :: "('a \<Rightarrow> 'b \<Rightarrow> 'b) \<Rightarrow> 'b \<Rightarrow> 'a set \<Rightarrow> 'b"
where
  "folds f z S \<equiv> SOME x. fold_graph f z S x"

lemma folds_alt_simp [simp]:
  "finite rs \<Longrightarrow> L (folds ALT NULL rs) = \<Union> (L ` rs)"
apply (rule set_ext, simp add:folds_def)
apply (rule someI2_ex, erule finite_imp_fold_graph)
by (erule fold_graph.induct, auto)

lemma [simp]:
  shows "(x, y) \<in> {(x, y). P x y} \<longleftrightarrow> P x y"
by simp

definition
  str_eq ("_ \<approx>_ _")
where
  "x \<approx>Lang y \<equiv> (\<forall>z. x @ z \<in> Lang \<longleftrightarrow> y @ z \<in> Lang)"

definition
  str_eq_rel ("\<approx>_")
where
  "\<approx>Lang \<equiv> {(x, y). x \<approx>Lang y}"

definition
  final :: "string set \<Rightarrow> string set \<Rightarrow> bool"
where
  "final X Lang \<equiv> (X \<in> UNIV // \<approx>Lang) \<and> (\<forall>s \<in> X. s \<in> Lang)"

lemma lang_is_union_of_finals: 
  "Lang = \<Union> {X. final X Lang}"
proof 
  show "Lang \<subseteq> \<Union> {X. final X Lang}"
  proof
    fix x
    assume "x \<in> Lang"   
    thus "x \<in> \<Union> {X. final X Lang}"
      apply (simp, rule_tac x = "(\<approx>Lang) `` {x}" in exI)      
      apply (auto simp:final_def quotient_def Image_def str_eq_rel_def str_eq_def)
      by (drule_tac x = "[]" in spec, simp)
  qed
next
  show "\<Union>{X. final X Lang} \<subseteq> Lang"
    by (auto simp:final_def)
qed

section {* finite \<Rightarrow> regular *}

datatype rhs_item = 
   Lam "rexp"                           (* Lambda *)
 | Trn "string set" "rexp"              (* Transition *)

fun the_Trn:: "rhs_item \<Rightarrow> (string set \<times> rexp)"
where "the_Trn (Trn Y r) = (Y, r)"

fun the_r :: "rhs_item \<Rightarrow> rexp"
where "the_r (Lam r) = r"

overloading L_rhs_e \<equiv> "L:: rhs_item \<Rightarrow> string set"
begin
fun L_rhs_e:: "rhs_item \<Rightarrow> string set"
where
  "L_rhs_e (Lam r) = L r" |
  "L_rhs_e (Trn X r) = X ;; L r"
end

overloading L_rhs \<equiv> "L:: rhs_item set \<Rightarrow> string set"
begin
fun L_rhs:: "rhs_item set \<Rightarrow> string set"
where
  "L_rhs rhs = \<Union> (L ` rhs)"
end

definition
  "init_rhs CS X \<equiv>  if ([] \<in> X)
                    then {Lam EMPTY} \<union> {Trn Y (CHAR c)| Y c. Y \<in> CS \<and> Y ;; {[c]} \<subseteq> X}
                    else {Trn Y (CHAR c)| Y c. Y \<in> CS \<and> Y ;; {[c]} \<subseteq> X}"

definition
  "eqs CS \<equiv> {(X, init_rhs CS X)|X.  X \<in> CS}"

(************ arden's lemma variation ********************)

definition
  "items_of rhs X \<equiv> {Trn X r | r. (Trn X r) \<in> rhs}"

definition
  "lam_of rhs \<equiv> {Lam r | r. Lam r \<in> rhs}"

definition 
  "rexp_of rhs X \<equiv> folds ALT NULL ((snd o the_Trn) ` items_of rhs X)"

definition
  "rexp_of_lam rhs \<equiv> folds ALT NULL (the_r ` lam_of rhs)"

fun attach_rexp :: "rexp \<Rightarrow> rhs_item \<Rightarrow> rhs_item"
where
  "attach_rexp r' (Lam r)   = Lam (SEQ r r')"
| "attach_rexp r' (Trn X r) = Trn X (SEQ r r')"

definition
  "append_rhs_rexp rhs r \<equiv> (attach_rexp r) ` rhs"

definition 
  "arden_variate X rhs \<equiv> append_rhs_rexp (rhs - items_of rhs X) (STAR (rexp_of rhs X))"


(*********** substitution of ES *************)

text {* rhs_subst rhs X xrhs: substitude all occurence of X in rhs with xrhs *}
definition 
  "rhs_subst rhs X xrhs \<equiv> (rhs - (items_of rhs X)) \<union> (append_rhs_rexp xrhs (rexp_of rhs X))"

definition
  "eqs_subst ES X xrhs \<equiv> {(Y, rhs_subst yrhs X xrhs) | Y yrhs. (Y, yrhs) \<in> ES}"

text {*
  Inv: Invairance of the equation-system, during the decrease of the equation-system, Inv holds.
*}

definition 
  "distinct_equas ES \<equiv> \<forall> X rhs rhs'. (X, rhs) \<in> ES \<and> (X, rhs') \<in> ES \<longrightarrow> rhs = rhs'"

definition 
  "valid_eqns ES \<equiv> \<forall> X rhs. (X, rhs) \<in> ES \<longrightarrow> (X = L rhs)"

definition 
  "rhs_nonempty rhs \<equiv> (\<forall> Y r. Trn Y r \<in> rhs \<longrightarrow> [] \<notin> L r)"

definition 
  "ardenable ES \<equiv> \<forall> X rhs. (X, rhs) \<in> ES \<longrightarrow> rhs_nonempty rhs"

definition 
  "non_empty ES \<equiv> \<forall> X rhs. (X, rhs) \<in> ES \<longrightarrow> X \<noteq> {}"

definition
  "finite_rhs ES \<equiv> \<forall> X rhs. (X, rhs) \<in> ES \<longrightarrow> finite rhs"

definition 
  "classes_of rhs \<equiv> {X. \<exists> r. Trn X r \<in> rhs}"

definition
  "lefts_of ES \<equiv> {Y | Y yrhs. (Y, yrhs) \<in> ES}"

definition 
  "self_contained ES \<equiv> \<forall> (X, xrhs) \<in> ES. classes_of xrhs \<subseteq> lefts_of ES"

definition 
  "Inv ES \<equiv> valid_eqns ES \<and> finite ES \<and> distinct_equas ES \<and> ardenable ES \<and> 
            non_empty ES \<and> finite_rhs ES \<and> self_contained ES"

lemma wf_iter [rule_format]: 
  fixes f
  assumes step: "\<And> e. \<lbrakk>P e; \<not> Q e\<rbrakk> \<Longrightarrow> (\<exists> e'. P e' \<and>  (f(e'), f(e)) \<in> less_than)"
  shows pe:     "P e \<longrightarrow> (\<exists> e'. P e' \<and>  Q e')"
proof(induct e rule: wf_induct 
           [OF wf_inv_image[OF wf_less_than, where f = "f"]], clarify)
  fix x 
  assume h [rule_format]: 
    "\<forall>y. (y, x) \<in> inv_image less_than f \<longrightarrow> P y \<longrightarrow> (\<exists>e'. P e' \<and> Q e')"
    and px: "P x"
  show "\<exists>e'. P e' \<and> Q e'"
  proof(cases "Q x")
    assume "Q x" with px show ?thesis by blast
  next
    assume nq: "\<not> Q x"
    from step [OF px nq]
    obtain e' where pe': "P e'" and ltf: "(f e', f x) \<in> less_than" by auto
    show ?thesis
    proof(rule h)
      from ltf show "(e', x) \<in> inv_image less_than f" 
	by (simp add:inv_image_def)
    next
      from pe' show "P e'" .
    qed
  qed
qed

text {* ************* basic properties of definitions above ************************ *}

lemma L_rhs_union_distrib:
  " L (A::rhs_item set) \<union> L B = L (A \<union> B)"
by simp

lemma finite_snd_Trn:
  assumes finite:"finite rhs"
  shows "finite {r\<^isub>2. Trn Y r\<^isub>2 \<in> rhs}" (is "finite ?B")
proof-
  def rhs' \<equiv> "{e \<in> rhs. \<exists> r. e = Trn Y r}"
  have "?B = (snd o the_Trn) ` rhs'" using rhs'_def by (auto simp:image_def)
  moreover have "finite rhs'" using finite rhs'_def by auto
  ultimately show ?thesis by simp
qed

lemma rexp_of_empty:
  assumes finite:"finite rhs"
  and nonempty:"rhs_nonempty rhs"
  shows "[] \<notin> L (rexp_of rhs X)"
using finite nonempty rhs_nonempty_def
by (drule_tac finite_snd_Trn[where Y = X], auto simp:rexp_of_def items_of_def)

lemma [intro!]:
  "P (Trn X r) \<Longrightarrow> (\<exists>a. (\<exists>r. a = Trn X r \<and> P a))" by auto

lemma finite_items_of:
  "finite rhs \<Longrightarrow> finite (items_of rhs X)"
by (auto simp:items_of_def intro:finite_subset)

lemma lang_of_rexp_of:
  assumes finite:"finite rhs"
  shows "L (items_of rhs X) = X ;; (L (rexp_of rhs X))"
proof -
  have "finite ((snd \<circ> the_Trn) ` items_of rhs X)" using finite_items_of[OF finite] by auto
  thus ?thesis
    apply (auto simp:rexp_of_def Seq_def items_of_def)
    apply (rule_tac x = s1 in exI, rule_tac x = s2 in exI, auto)
    by (rule_tac x= "Trn X r" in exI, auto simp:Seq_def)
qed

lemma rexp_of_lam_eq_lam_set:
  assumes finite: "finite rhs"
  shows "L (rexp_of_lam rhs) = L (lam_of rhs)"
proof -
  have "finite (the_r ` {Lam r |r. Lam r \<in> rhs})" using finite
    by (rule_tac finite_imageI, auto intro:finite_subset)
  thus ?thesis by (auto simp:rexp_of_lam_def lam_of_def)
qed

lemma [simp]:
  " L (attach_rexp r xb) = L xb ;; L r"
apply (cases xb, auto simp:Seq_def)
by (rule_tac x = "s1 @ s1a" in exI, rule_tac x = s2a in exI,auto simp:Seq_def)

lemma lang_of_append_rhs:
  "L (append_rhs_rexp rhs r) = L rhs ;; L r"
apply (auto simp:append_rhs_rexp_def image_def)
apply (auto simp:Seq_def)
apply (rule_tac x = "L xb ;; L r" in exI, auto simp add:Seq_def)
by (rule_tac x = "attach_rexp r xb" in exI, auto simp:Seq_def)

lemma classes_of_union_distrib:
  "classes_of A \<union> classes_of B = classes_of (A \<union> B)"
by (auto simp add:classes_of_def)

lemma lefts_of_union_distrib:
  "lefts_of A \<union> lefts_of B = lefts_of (A \<union> B)"
by (auto simp:lefts_of_def)


text {* ******BEGIN: proving the initial equation-system satisfies Inv ****** *}

lemma defined_by_str:
  "\<lbrakk>s \<in> X; X \<in> UNIV // (\<approx>Lang)\<rbrakk> \<Longrightarrow> X = (\<approx>Lang) `` {s}"
by (auto simp:quotient_def Image_def str_eq_rel_def str_eq_def)

lemma every_eqclass_has_transition:
  assumes has_str: "s @ [c] \<in> X"
  and     in_CS:   "X \<in> UNIV // (\<approx>Lang)"
  obtains Y where "Y \<in> UNIV // (\<approx>Lang)" and "Y ;; {[c]} \<subseteq> X" and "s \<in> Y"
proof -
  def Y \<equiv> "(\<approx>Lang) `` {s}"
  have "Y \<in> UNIV // (\<approx>Lang)" 
    unfolding Y_def quotient_def by auto
  moreover
  have "X = (\<approx>Lang) `` {s @ [c]}" 
    using has_str in_CS defined_by_str by blast
  then have "Y ;; {[c]} \<subseteq> X" 
    unfolding Y_def Image_def Seq_def
    unfolding str_eq_rel_def
    by (auto) (simp add: str_eq_def)
  moreover
  have "s \<in> Y" unfolding Y_def 
    unfolding Image_def str_eq_rel_def str_eq_def by simp
  ultimately show thesis by (blast intro: that)
qed

lemma l_eq_r_in_eqs:
  assumes X_in_eqs: "(X, xrhs) \<in> (eqs (UNIV // (\<approx>Lang)))"
  shows "X = L xrhs"
proof 
  show "X \<subseteq> L xrhs"
  proof
    fix x
    assume "(1)": "x \<in> X"
    show "x \<in> L xrhs"          
    proof (cases "x = []")
      assume empty: "x = []"
      thus ?thesis using X_in_eqs "(1)"
        by (auto simp:eqs_def init_rhs_def)
    next
      assume not_empty: "x \<noteq> []"
      then obtain clist c where decom: "x = clist @ [c]"
        by (case_tac x rule:rev_cases, auto)
      have "X \<in> UNIV // (\<approx>Lang)" using X_in_eqs by (auto simp:eqs_def)
      then obtain Y 
        where "Y \<in> UNIV // (\<approx>Lang)" 
        and "Y ;; {[c]} \<subseteq> X"
        and "clist \<in> Y"
        using decom "(1)" every_eqclass_has_transition by blast
      hence "x \<in> L {Trn Y (CHAR c)| Y c. Y \<in> UNIV // (\<approx>Lang) \<and> Y ;; {[c]} \<subseteq> X}"
        using "(1)" decom
        by (simp, rule_tac x = "Trn Y (CHAR c)" in exI, simp add:Seq_def)
      thus ?thesis using X_in_eqs "(1)"
        by (simp add:eqs_def init_rhs_def)
    qed
  qed
next
  show "L xrhs \<subseteq> X" using X_in_eqs
    by (auto simp:eqs_def init_rhs_def) 
qed

lemma finite_init_rhs: 
  assumes finite: "finite CS"
  shows "finite (init_rhs CS X)"
proof-
  have "finite {Trn Y (CHAR c) |Y c. Y \<in> CS \<and> Y ;; {[c]} \<subseteq> X}" (is "finite ?A")
  proof -
    def S \<equiv> "{(Y, c)| Y c. Y \<in> CS \<and> Y ;; {[c]} \<subseteq> X}" 
    def h \<equiv> "\<lambda> (Y, c). Trn Y (CHAR c)"
    have "finite (CS \<times> (UNIV::char set))" using finite by auto
    hence "finite S" using S_def 
      by (rule_tac B = "CS \<times> UNIV" in finite_subset, auto)
    moreover have "?A = h ` S" by (auto simp: S_def h_def image_def)
    ultimately show ?thesis 
      by auto
  qed
  thus ?thesis by (simp add:init_rhs_def)
qed

lemma init_ES_satisfy_Inv:
  assumes finite_CS: "finite (UNIV // (\<approx>Lang))"
  shows "Inv (eqs (UNIV // (\<approx>Lang)))"
proof -
  have "finite (eqs (UNIV // (\<approx>Lang)))" using finite_CS
    by (simp add:eqs_def)
  moreover have "distinct_equas (eqs (UNIV // (\<approx>Lang)))"     
    by (simp add:distinct_equas_def eqs_def)
  moreover have "ardenable (eqs (UNIV // (\<approx>Lang)))"
    by (auto simp add:ardenable_def eqs_def init_rhs_def rhs_nonempty_def del:L_rhs.simps)
  moreover have "valid_eqns (eqs (UNIV // (\<approx>Lang)))"
    using l_eq_r_in_eqs by (simp add:valid_eqns_def)
  moreover have "non_empty (eqs (UNIV // (\<approx>Lang)))"
    by (auto simp:non_empty_def eqs_def quotient_def Image_def str_eq_rel_def str_eq_def)
  moreover have "finite_rhs (eqs (UNIV // (\<approx>Lang)))"
    using finite_init_rhs[OF finite_CS] 
    by (auto simp:finite_rhs_def eqs_def)
  moreover have "self_contained (eqs (UNIV // (\<approx>Lang)))"
    by (auto simp:self_contained_def eqs_def init_rhs_def classes_of_def lefts_of_def)
  ultimately show ?thesis by (simp add:Inv_def)
qed

text {* ****** BEGIN: proving every equation-system's iteration step satisfies Inv ***** *}

lemma arden_variate_keeps_eq:
  assumes l_eq_r: "X = L rhs"
  and not_empty: "[] \<notin> L (rexp_of rhs X)"
  and finite: "finite rhs"
  shows "X = L (arden_variate X rhs)"
proof -
  def A \<equiv> "L (rexp_of rhs X)"
  def b \<equiv> "rhs - items_of rhs X"
  def B \<equiv> "L b" 
  have "X = B ;; A\<star>"
  proof-
    have "rhs = items_of rhs X \<union> b" by (auto simp:b_def items_of_def)
    hence "L rhs = L(items_of rhs X \<union> b)" by simp
    hence "L rhs = L(items_of rhs X) \<union> B" by (simp only:L_rhs_union_distrib B_def)
    with lang_of_rexp_of
    have "L rhs = X ;; A \<union> B " using finite by (simp only:B_def b_def A_def)
    thus ?thesis
      using l_eq_r not_empty
      apply (drule_tac B = B and X = X in ardens_revised)
      by (auto simp:A_def simp del:L_rhs.simps)
  qed
  moreover have "L (arden_variate X rhs) = (B ;; A\<star>)" (is "?L = ?R")
    by (simp only:arden_variate_def L_rhs_union_distrib lang_of_append_rhs 
                  B_def A_def b_def L_rexp.simps seq_union_distrib)
   ultimately show ?thesis by simp
qed 

lemma append_keeps_finite:
  "finite rhs \<Longrightarrow> finite (append_rhs_rexp rhs r)"
by (auto simp:append_rhs_rexp_def)

lemma arden_variate_keeps_finite:
  "finite rhs \<Longrightarrow> finite (arden_variate X rhs)"
by (auto simp:arden_variate_def append_keeps_finite)

lemma append_keeps_nonempty:
  "rhs_nonempty rhs \<Longrightarrow> rhs_nonempty (append_rhs_rexp rhs r)"
apply (auto simp:rhs_nonempty_def append_rhs_rexp_def)
by (case_tac x, auto simp:Seq_def)

lemma nonempty_set_sub:
  "rhs_nonempty rhs \<Longrightarrow> rhs_nonempty (rhs - A)"
by (auto simp:rhs_nonempty_def)

lemma nonempty_set_union:
  "\<lbrakk>rhs_nonempty rhs; rhs_nonempty rhs'\<rbrakk> \<Longrightarrow> rhs_nonempty (rhs \<union> rhs')"
by (auto simp:rhs_nonempty_def)

lemma arden_variate_keeps_nonempty:
  "rhs_nonempty rhs \<Longrightarrow> rhs_nonempty (arden_variate X rhs)"
by (simp only:arden_variate_def append_keeps_nonempty nonempty_set_sub)


lemma rhs_subst_keeps_nonempty:
  "\<lbrakk>rhs_nonempty rhs; rhs_nonempty xrhs\<rbrakk> \<Longrightarrow> rhs_nonempty (rhs_subst rhs X xrhs)"
by (simp only:rhs_subst_def append_keeps_nonempty  nonempty_set_union nonempty_set_sub)

lemma rhs_subst_keeps_eq:
  assumes substor: "X = L xrhs"
  and finite: "finite rhs"
  shows "L (rhs_subst rhs X xrhs) = L rhs" (is "?Left = ?Right")
proof-
  def A \<equiv> "L (rhs - items_of rhs X)"
  have "?Left = A \<union> L (append_rhs_rexp xrhs (rexp_of rhs X))"
    by (simp only:rhs_subst_def L_rhs_union_distrib A_def)
  moreover have "?Right = A \<union> L (items_of rhs X)"
  proof-
    have "rhs = (rhs - items_of rhs X) \<union> (items_of rhs X)" by (auto simp:items_of_def)
    thus ?thesis by (simp only:L_rhs_union_distrib A_def)
  qed
  moreover have "L (append_rhs_rexp xrhs (rexp_of rhs X)) = L (items_of rhs X)" 
    using finite substor  by (simp only:lang_of_append_rhs lang_of_rexp_of)
  ultimately show ?thesis by simp
qed

lemma rhs_subst_keeps_finite_rhs:
  "\<lbrakk>finite rhs; finite yrhs\<rbrakk> \<Longrightarrow> finite (rhs_subst rhs Y yrhs)"
by (auto simp:rhs_subst_def append_keeps_finite)

lemma eqs_subst_keeps_finite:
  assumes finite:"finite (ES:: (string set \<times> rhs_item set) set)"
  shows "finite (eqs_subst ES Y yrhs)"
proof -
  have "finite {(Ya, rhs_subst yrhsa Y yrhs) |Ya yrhsa. (Ya, yrhsa) \<in> ES}" (is "finite ?A")
  proof-
    def eqns' \<equiv> "{((Ya::string set), yrhsa)| Ya yrhsa. (Ya, yrhsa) \<in> ES}"
    def h \<equiv> "\<lambda> ((Ya::string set), yrhsa). (Ya, rhs_subst yrhsa Y yrhs)"
    have "finite (h ` eqns')" using finite h_def eqns'_def by auto
    moreover have "?A = h ` eqns'" by (auto simp:h_def eqns'_def)
    ultimately show ?thesis by auto      
  qed
  thus ?thesis by (simp add:eqs_subst_def)
qed

lemma eqs_subst_keeps_finite_rhs:
  "\<lbrakk>finite_rhs ES; finite yrhs\<rbrakk> \<Longrightarrow> finite_rhs (eqs_subst ES Y yrhs)"
by (auto intro:rhs_subst_keeps_finite_rhs simp add:eqs_subst_def finite_rhs_def)

lemma append_rhs_keeps_cls:
  "classes_of (append_rhs_rexp rhs r) = classes_of rhs"
apply (auto simp:classes_of_def append_rhs_rexp_def)
apply (case_tac xa, auto simp:image_def)
by (rule_tac x = "SEQ ra r" in exI, rule_tac x = "Trn x ra" in bexI, simp+)

lemma arden_variate_removes_cl:
  "classes_of (arden_variate Y yrhs) = classes_of yrhs - {Y}"
apply (simp add:arden_variate_def append_rhs_keeps_cls items_of_def)
by (auto simp:classes_of_def)

lemma lefts_of_keeps_cls:
  "lefts_of (eqs_subst ES Y yrhs) = lefts_of ES"
by (auto simp:lefts_of_def eqs_subst_def)

lemma rhs_subst_updates_cls:
  "X \<notin> classes_of xrhs \<Longrightarrow> classes_of (rhs_subst rhs X xrhs) = classes_of rhs \<union> classes_of xrhs - {X}"
apply (simp only:rhs_subst_def append_rhs_keeps_cls classes_of_union_distrib[THEN sym])
by (auto simp:classes_of_def items_of_def)

lemma eqs_subst_keeps_self_contained:
  fixes Y
  assumes sc: "self_contained (ES \<union> {(Y, yrhs)})" (is "self_contained ?A")
  shows "self_contained (eqs_subst ES Y (arden_variate Y yrhs))" (is "self_contained ?B")
proof-
  { fix X xrhs'
    assume "(X, xrhs') \<in> ?B"
    then obtain xrhs 
      where xrhs_xrhs': "xrhs' = rhs_subst xrhs Y (arden_variate Y yrhs)"
      and X_in: "(X, xrhs) \<in> ES" by (simp add:eqs_subst_def, blast)    
    have "classes_of xrhs' \<subseteq> lefts_of ?B"
    proof-
      have "lefts_of ?B = lefts_of ES" by (auto simp add:lefts_of_def eqs_subst_def)
      moreover have "classes_of xrhs' \<subseteq> lefts_of ES"
      proof-
        have "classes_of xrhs' \<subseteq> classes_of xrhs \<union> classes_of (arden_variate Y yrhs) - {Y}"
        proof-
          have "Y \<notin> classes_of (arden_variate Y yrhs)" using arden_variate_removes_cl by simp
          thus ?thesis using xrhs_xrhs' by (auto simp:rhs_subst_updates_cls)
        qed
        moreover have "classes_of xrhs \<subseteq> lefts_of ES \<union> {Y}" using X_in sc
          apply (simp only:self_contained_def lefts_of_union_distrib[THEN sym])
          by (drule_tac x = "(X, xrhs)" in bspec, auto simp:lefts_of_def)
        moreover have "classes_of (arden_variate Y yrhs) \<subseteq> lefts_of ES \<union> {Y}" using sc
          by (auto simp add:arden_variate_removes_cl self_contained_def lefts_of_def)
        ultimately show ?thesis by auto
      qed
      ultimately show ?thesis by simp
    qed
  } thus ?thesis by (auto simp only:eqs_subst_def self_contained_def)
qed

lemma eqs_subst_satisfy_Inv:
  assumes Inv_ES: "Inv (ES \<union> {(Y, yrhs)})"
  shows "Inv (eqs_subst ES Y (arden_variate Y yrhs))"
proof -  
  have finite_yrhs: "finite yrhs" using Inv_ES by (auto simp:Inv_def finite_rhs_def)
  have nonempty_yrhs: "rhs_nonempty yrhs" using Inv_ES by (auto simp:Inv_def ardenable_def)
  have Y_eq_yrhs: "Y = L yrhs" using Inv_ES by (simp only:Inv_def valid_eqns_def, blast)

  have "distinct_equas (eqs_subst ES Y (arden_variate Y yrhs))" using Inv_ES
    by (auto simp:distinct_equas_def eqs_subst_def Inv_def)
  moreover have "finite (eqs_subst ES Y (arden_variate Y yrhs))" using Inv_ES 
    by (simp add:Inv_def eqs_subst_keeps_finite)
  moreover have "finite_rhs (eqs_subst ES Y (arden_variate Y yrhs))"
  proof-
    have "finite_rhs ES" using Inv_ES by (simp add:Inv_def finite_rhs_def)
    moreover have "finite (arden_variate Y yrhs)"
    proof -
      have "finite yrhs" using Inv_ES by (auto simp:Inv_def finite_rhs_def)
      thus ?thesis using arden_variate_keeps_finite by simp
    qed
    ultimately show ?thesis by (simp add:eqs_subst_keeps_finite_rhs)
  qed
  moreover have "ardenable (eqs_subst ES Y (arden_variate Y yrhs))"
  proof - 
    { fix X rhs
      assume "(X, rhs) \<in> ES"
      hence "rhs_nonempty rhs"  using prems Inv_ES  by (simp add:Inv_def ardenable_def)
      with nonempty_yrhs have "rhs_nonempty (rhs_subst rhs Y (arden_variate Y yrhs))"
        by (simp add:nonempty_yrhs rhs_subst_keeps_nonempty arden_variate_keeps_nonempty)
    } thus ?thesis by (auto simp add:ardenable_def eqs_subst_def)
  qed
  moreover have "valid_eqns (eqs_subst ES Y (arden_variate Y yrhs))"
  proof-
    have "Y = L (arden_variate Y yrhs)" using Y_eq_yrhs Inv_ES finite_yrhs nonempty_yrhs      
        by (rule_tac arden_variate_keeps_eq, (simp add:rexp_of_empty)+)
    thus ?thesis using Inv_ES 
      by (clarsimp simp add:valid_eqns_def eqs_subst_def rhs_subst_keeps_eq Inv_def finite_rhs_def
                   simp del:L_rhs.simps)
  qed
  moreover have non_empty_subst: "non_empty (eqs_subst ES Y (arden_variate Y yrhs))"
    using Inv_ES by (auto simp:Inv_def non_empty_def eqs_subst_def)
  moreover have self_subst: "self_contained (eqs_subst ES Y (arden_variate Y yrhs))"
    using Inv_ES eqs_subst_keeps_self_contained by (simp add:Inv_def)
  ultimately show ?thesis using Inv_ES by (simp add:Inv_def)
qed

lemma eqs_subst_card_le: 
  assumes finite: "finite (ES::(string set \<times> rhs_item set) set)"
  shows "card (eqs_subst ES Y yrhs) <= card ES"
proof-
  def f \<equiv> "\<lambda> x. ((fst x)::string set, rhs_subst (snd x) Y yrhs)"
  have "eqs_subst ES Y yrhs = f ` ES" 
    apply (auto simp:eqs_subst_def f_def image_def)
    by (rule_tac x = "(Ya, yrhsa)" in bexI, simp+)
  thus ?thesis using finite by (auto intro:card_image_le)
qed

lemma eqs_subst_cls_remains: 
  "(X, xrhs) \<in> ES \<Longrightarrow> \<exists> xrhs'. (X, xrhs') \<in> (eqs_subst ES Y yrhs)"
by (auto simp:eqs_subst_def)

lemma card_noteq_1_has_more:
  assumes card:"card S \<noteq> 1"
  and e_in: "e \<in> S"
  and finite: "finite S"
  obtains e' where "e' \<in> S \<and> e \<noteq> e'" 
proof-
  have "card (S - {e}) > 0"
  proof -
    have "card S > 1" using card e_in finite  by (case_tac "card S", auto) 
    thus ?thesis using finite e_in by auto
  qed
  hence "S - {e} \<noteq> {}" using finite by (rule_tac notI, simp)
  thus "(\<And>e'. e' \<in> S \<and> e \<noteq> e' \<Longrightarrow> thesis) \<Longrightarrow> thesis" by auto
qed

lemma iteration_step: 
  assumes Inv_ES: "Inv ES"
  and    X_in_ES: "(X, xrhs) \<in> ES"
  and    not_T: "card ES \<noteq> 1"
  shows "\<exists> ES'. (Inv ES' \<and> (\<exists> xrhs'.(X, xrhs') \<in> ES')) \<and> (card ES', card ES) \<in> less_than" (is "\<exists> ES'. ?P ES'")
proof -
  have finite_ES: "finite ES" using Inv_ES by (simp add:Inv_def)
  then obtain Y yrhs where Y_in_ES: "(Y, yrhs) \<in> ES" and not_eq: "(X, xrhs) \<noteq> (Y, yrhs)" 
    using not_T X_in_ES by (drule_tac card_noteq_1_has_more, auto)
  def ES' == "ES - {(Y, yrhs)}"
  let ?ES'' = "eqs_subst ES' Y (arden_variate Y yrhs)"
  have "?P ?ES''"
  proof -
    have "Inv ?ES''" using Y_in_ES Inv_ES
      by (rule_tac eqs_subst_satisfy_Inv, simp add:ES'_def insert_absorb)
    moreover have "\<exists>xrhs'. (X, xrhs') \<in> ?ES''"  using not_eq X_in_ES
      by (rule_tac ES = ES' in eqs_subst_cls_remains, auto simp add:ES'_def)
    moreover have "(card ?ES'', card ES) \<in> less_than" 
    proof -
      have "finite ES'" using finite_ES ES'_def by auto
      moreover have "card ES' < card ES" using finite_ES Y_in_ES
        by (auto simp:ES'_def card_gt_0_iff intro:diff_Suc_less)
      ultimately show ?thesis 
        by (auto dest:eqs_subst_card_le elim:le_less_trans)
    qed
    ultimately show ?thesis by simp
  qed
  thus ?thesis by blast
qed

text {* ***** END: proving every equation-system's iteration step satisfies Inv ************** *}

lemma iteration_conc: 
  assumes history: "Inv ES"
  and    X_in_ES: "\<exists> xrhs. (X, xrhs) \<in> ES"
  shows "\<exists> ES'. (Inv ES' \<and> (\<exists> xrhs'. (X, xrhs') \<in> ES')) \<and> card ES' = 1" (is "\<exists> ES'. ?P ES'")
proof (cases "card ES = 1")
  case True
  thus ?thesis using history X_in_ES
    by blast
next
  case False  
  thus ?thesis using history iteration_step X_in_ES
    by (rule_tac f = card in wf_iter, auto)
qed
  
lemma last_cl_exists_rexp:
  assumes ES_single: "ES = {(X, xrhs)}" 
  and Inv_ES: "Inv ES"
  shows "\<exists> (r::rexp). L r = X" (is "\<exists> r. ?P r")
proof-
  let ?A = "arden_variate X xrhs"
  have "?P (rexp_of_lam ?A)"
  proof -
    have "L (rexp_of_lam ?A) = L (lam_of ?A)"
    proof(rule rexp_of_lam_eq_lam_set)
      show "finite (arden_variate X xrhs)" using Inv_ES ES_single 
        by (rule_tac arden_variate_keeps_finite, auto simp add:Inv_def finite_rhs_def)
    qed
    also have "\<dots> = L ?A"
    proof-
      have "lam_of ?A = ?A"
      proof-
        have "classes_of ?A = {}" using Inv_ES ES_single
          by (simp add:arden_variate_removes_cl self_contained_def Inv_def lefts_of_def) 
        thus ?thesis by (auto simp only:lam_of_def classes_of_def, case_tac x, auto)
      qed
      thus ?thesis by simp
    qed
    also have "\<dots> = X"
    proof(rule arden_variate_keeps_eq [THEN sym])
      show "X = L xrhs" using Inv_ES ES_single by (auto simp only:Inv_def valid_eqns_def)  
    next
      from Inv_ES ES_single show "[] \<notin> L (rexp_of xrhs X)"
        by(simp add:Inv_def ardenable_def rexp_of_empty finite_rhs_def)
    next
      from Inv_ES ES_single show "finite xrhs" by (simp add:Inv_def finite_rhs_def)
    qed
    finally show ?thesis by simp
  qed
  thus ?thesis by auto
qed
   
lemma every_eqcl_has_reg: 
  assumes finite_CS: "finite (UNIV // (\<approx>Lang))"
  and X_in_CS: "X \<in> (UNIV // (\<approx>Lang))"
  shows "\<exists> (reg::rexp). L reg = X" (is "\<exists> r. ?E r")
proof -
  from X_in_CS have "\<exists> xrhs. (X, xrhs) \<in> (eqs (UNIV  // (\<approx>Lang)))"
    by (auto simp:eqs_def init_rhs_def)
  then obtain ES xrhs where Inv_ES: "Inv ES" 
    and X_in_ES: "(X, xrhs) \<in> ES"
    and card_ES: "card ES = 1"
    using finite_CS X_in_CS init_ES_satisfy_Inv iteration_conc
    by blast
  hence ES_single_equa: "ES = {(X, xrhs)}" 
    by (auto simp:Inv_def dest!:card_Suc_Diff1 simp:card_eq_0_iff) 
  thus ?thesis using Inv_ES
    by (rule last_cl_exists_rexp)
qed

theorem hard_direction: 
  assumes finite_CS: "finite (UNIV // (\<approx>Lang))"
  shows   "\<exists> (reg::rexp). Lang = L reg"
proof -
  have "\<forall> X \<in> (UNIV // (\<approx>Lang)). \<exists> (reg::rexp). X = L reg" 
    using finite_CS every_eqcl_has_reg by blast
  then obtain f where f_prop: "\<forall> X \<in> (UNIV // (\<approx>Lang)). X = L ((f X)::rexp)" 
    by (auto dest:bchoice)
  def rs \<equiv> "f ` {X. final X Lang}"  
  have "Lang = \<Union> {X. final X Lang}" using lang_is_union_of_finals by simp
  also have "\<dots> = L (folds ALT NULL rs)" 
  proof -
    have "finite {X. final X Lang}" using finite_CS by (auto simp:final_def)
    thus ?thesis  using f_prop by (auto simp:rs_def final_def)
  qed
  finally show ?thesis by blast
qed 

section {* regular \<Rightarrow> finite*}

lemma quot_empty_subset:
  "UNIV // (\<approx>{[]}) \<subseteq> {{[]}, UNIV - {[]}}"
proof
  fix x
  assume h: "x \<in> UNIV // \<approx>{[]}"
  show "x \<in> {{[]}, UNIV - {[]}}"
    
 
  have "\<And> s. s \<approx>{[]} [] \<Longrightarrow> s = []"
    apply (auto simp add:str_eq_def)
    apply blast
  
  hence "False"
    apply (simp add:quotient_def)


lemma other_direction:
  "Lang = L (r::rexp) \<Longrightarrow> finite (UNIV // (\<approx>Lang))"
proof (induct arbitrary:Lang rule:rexp.induct)
  case NULL
  have "UNIV // (\<approx>{}) \<subseteq> {UNIV} "
    by (auto simp:quotient_def str_eq_rel_def str_eq_def)
  with prems show "?case" by (auto intro:finite_subset)
next
  case EMPTY
  have "UNIV // (\<approx>{[]}) \<subseteq> {{[]}, UNIV - {[]}}"
    sorry
  with prems show ?case by (auto intro:finite_subset)
next
  case (CHAR c)
  have "UNIV // (\<approx>{[c]}) \<subseteq> {{[]},{[c]}, UNIV - {[], [c]}}"
    sorry
  with prems show ?case by (auto intro:finite_subset)
next
  case (SEQ r1 r2)
  show ?case sorry
next
  case (ALT r1 r1)
  show ?case sorry
next
  case (STAR r)
  show ?case sorry
qed
    

      

















apply (induct arbitrary:Lang rule:rexp.induct)
apply (simp add:QUOT_def equiv_class_def equiv_str_def)
by (simp_all add:quot_lambda quot_single quot_seq quot_alt quot_star)  

(* Alternative definition for Quo *)
definition 
  QUOT :: "string set \<Rightarrow> (string set) set"  
where
  "QUOT Lang \<equiv> (\<Union>x. {\<lbrakk>x\<rbrakk>Lang})"

lemma test: 
  "UNIV Quo Lang = QUOT Lang"
by (auto simp add: quot_def QUOT_def)

lemma test1:
  assumes finite_CS: "finite (QUOT Lang)"
  shows "reg Lang"
using finite_CS
unfolding test[symmetric]
by (auto dest: myhill_nerode)

lemma cons_one: "x @ y \<in> {z} \<Longrightarrow> x @ y = z"
by simp

lemma quot_lambda: "QUOT {[]} = {{[]}, UNIV - {[]}}"
proof 
  show "QUOT {[]} \<subseteq> {{[]}, UNIV - {[]}}"
  proof 
    fix x 
    assume in_quot: "x \<in> QUOT {[]}"
    show "x \<in> {{[]}, UNIV - {[]}}"
    proof -
      from in_quot 
      have "x = {[]} \<or> x = UNIV - {[]}"
        unfolding QUOT_def equiv_class_def
      proof 
        fix xa
        assume in_univ: "xa \<in> UNIV"
           and in_eqiv: "x \<in> {{y. xa \<equiv>{[]}\<equiv> y}}"
        show "x = {[]} \<or> x = UNIV - {[]}"
        proof(cases "xa = []")
          case True
          hence "{y. xa \<equiv>{[]}\<equiv> y} = {[]}" using in_eqiv
            by (auto simp add:equiv_str_def)
          thus ?thesis using in_eqiv by (rule_tac disjI1, simp)
        next
          case False
          hence "{y. xa \<equiv>{[]}\<equiv> y} = UNIV - {[]}" using in_eqiv
            by (auto simp:equiv_str_def)
          thus ?thesis using in_eqiv by (rule_tac disjI2, simp)
        qed
      qed
      thus ?thesis by simp
    qed
  qed
next
  show "{{[]}, UNIV - {[]}} \<subseteq> QUOT {[]}"
  proof
    fix x
    assume in_res: "x \<in> {{[]}, (UNIV::string set) - {[]}}"
    show "x \<in> (QUOT {[]})"
    proof -
      have "x = {[]} \<Longrightarrow> x \<in> QUOT {[]}"
        apply (simp add:QUOT_def equiv_class_def equiv_str_def)
        by (rule_tac x = "[]" in exI, auto)
      moreover have "x = UNIV - {[]} \<Longrightarrow> x \<in> QUOT {[]}"
        apply (simp add:QUOT_def equiv_class_def equiv_str_def)
        by (rule_tac x = "''a''" in exI, auto)
      ultimately show ?thesis using in_res by blast
    qed
  qed
qed

lemma quot_single_aux: "\<lbrakk>x \<noteq> []; x \<noteq> [c]\<rbrakk> \<Longrightarrow> x @ z \<noteq> [c]"
by (induct x, auto)

lemma quot_single: "\<And> (c::char). QUOT {[c]} = {{[]}, {[c]}, UNIV - {[], [c]}}"
proof - 
  fix c::"char" 
  have exist_another: "\<exists> a. a \<noteq> c" 
    apply (case_tac "c = CHR ''a''")
    apply (rule_tac x = "CHR ''b''" in exI, simp)
    by (rule_tac x = "CHR ''a''" in exI, simp)  
  show "QUOT {[c]} = {{[]}, {[c]}, UNIV - {[], [c]}}"
  proof
    show "QUOT {[c]} \<subseteq> {{[]},{[c]}, UNIV - {[], [c]}}"
    proof 
      fix x 
      assume in_quot: "x \<in> QUOT {[c]}"
      show "x \<in> {{[]}, {[c]}, UNIV - {[],[c]}}"
      proof -
        from in_quot 
        have "x = {[]} \<or> x = {[c]} \<or> x = UNIV - {[],[c]}"
          unfolding QUOT_def equiv_class_def
        proof 
          fix xa
          assume in_eqiv: "x \<in> {{y. xa \<equiv>{[c]}\<equiv> y}}"
          show "x = {[]} \<or> x = {[c]} \<or> x = UNIV - {[], [c]}"
          proof-
            have "xa = [] \<Longrightarrow> x = {[]}" using in_eqiv 
              by (auto simp add:equiv_str_def)
            moreover have "xa = [c] \<Longrightarrow> x = {[c]}"
            proof -
              have "xa = [c] \<Longrightarrow> {y. xa \<equiv>{[c]}\<equiv> y} = {[c]}" using in_eqiv
                apply (simp add:equiv_str_def)
                apply (rule set_ext, rule iffI, simp)
                apply (drule_tac x = "[]" in spec, auto)
                done   
              thus "xa = [c] \<Longrightarrow> x = {[c]}" using in_eqiv by simp 
            qed
            moreover have "\<lbrakk>xa \<noteq> []; xa \<noteq> [c]\<rbrakk> \<Longrightarrow> x = UNIV - {[],[c]}"
            proof -
              have "\<lbrakk>xa \<noteq> []; xa \<noteq> [c]\<rbrakk> \<Longrightarrow> {y. xa \<equiv>{[c]}\<equiv> y} = UNIV - {[],[c]}" 
                apply (clarsimp simp add:equiv_str_def)
                apply (rule set_ext, rule iffI, simp)
                apply (rule conjI)
                apply (drule_tac x = "[c]" in spec, simp)
                apply (drule_tac x = "[]" in spec, simp)
                by (auto dest:quot_single_aux)
              thus "\<lbrakk>xa \<noteq> []; xa \<noteq> [c]\<rbrakk> \<Longrightarrow> x = UNIV - {[],[c]}" using in_eqiv by simp
            qed
            ultimately show ?thesis by blast
          qed
        qed
        thus ?thesis by simp
      qed
    qed
  next
    show "{{[]}, {[c]}, UNIV - {[],[c]}} \<subseteq> QUOT {[c]}"
    proof
      fix x
      assume in_res: "x \<in> {{[]},{[c]}, (UNIV::string set) - {[],[c]}}"
      show "x \<in> (QUOT {[c]})"
      proof -
        have "x = {[]} \<Longrightarrow> x \<in> QUOT {[c]}"
          apply (simp add:QUOT_def equiv_class_def equiv_str_def)
          by (rule_tac x = "[]" in exI, auto)
        moreover have "x = {[c]} \<Longrightarrow> x \<in> QUOT {[c]}"
          apply (simp add:QUOT_def equiv_class_def equiv_str_def)
          apply (rule_tac x = "[c]" in exI, simp)
          apply (rule set_ext, rule iffI, simp+)
          by (drule_tac x = "[]" in spec, simp)
        moreover have "x = UNIV - {[],[c]} \<Longrightarrow> x \<in> QUOT {[c]}"
          using exist_another
          apply (clarsimp simp add:QUOT_def equiv_class_def equiv_str_def)        
          apply (rule_tac x = "[a]" in exI, simp)
          apply (rule set_ext, rule iffI, simp)
          apply (clarsimp simp:quot_single_aux, simp)
          apply (rule conjI)
          apply (drule_tac x = "[c]" in spec, simp)
          by (drule_tac x = "[]" in spec, simp)     
        ultimately show ?thesis using in_res by blast
      qed
    qed
  qed
qed

lemma eq_class_imp_eq_str:
  "\<lbrakk>x\<rbrakk>lang = \<lbrakk>y\<rbrakk>lang \<Longrightarrow> x \<equiv>lang\<equiv> y"
by (auto simp:equiv_class_def equiv_str_def)

lemma finite_tag_image: 
  "finite (range tag) \<Longrightarrow> finite (((op `) tag) ` S)"
apply (rule_tac B = "Pow (tag ` UNIV)" in finite_subset)
by (auto simp add:image_def Pow_def)

lemma str_inj_imps:
  assumes str_inj: "\<And> m n. tag m = tag (n::string) \<Longrightarrow> m \<equiv>lang\<equiv> n"
  shows "inj_on ((op `) tag) (QUOT lang)"
proof (clarsimp simp add:inj_on_def QUOT_def)
  fix x y
  assume eq_tag: "tag ` \<lbrakk>x\<rbrakk>lang = tag ` \<lbrakk>y\<rbrakk>lang"
  show "\<lbrakk>x\<rbrakk>lang = \<lbrakk>y\<rbrakk>lang"
  proof -
    have aux1:"\<And>a b. a \<in> (\<lbrakk>b\<rbrakk>lang) \<Longrightarrow> (a \<equiv>lang\<equiv> b)"
      by (simp add:equiv_class_def equiv_str_def)
    have aux2: "\<And> A B f. \<lbrakk>f ` A = f ` B; A \<noteq> {}\<rbrakk> \<Longrightarrow> \<exists> a b. a \<in> A \<and> b \<in> B \<and> f a = f b"
      by auto
    have aux3: "\<And> a l. \<lbrakk>a\<rbrakk>l \<noteq> {}" 
      by (auto simp:equiv_class_def equiv_str_def)
    show ?thesis using eq_tag
      apply (drule_tac aux2, simp add:aux3, clarsimp)
      apply (drule_tac str_inj, (drule_tac aux1)+)
      by (simp add:equiv_str_def equiv_class_def)
  qed
qed

definition tag_str_ALT :: "string set \<Rightarrow> string set \<Rightarrow> string \<Rightarrow> (string set \<times> string set)"
where
  "tag_str_ALT L\<^isub>1 L\<^isub>2 x \<equiv> (\<lbrakk>x\<rbrakk>L\<^isub>1, \<lbrakk>x\<rbrakk>L\<^isub>2)"

lemma tag_str_alt_range_finite:
  assumes finite1: "finite (QUOT L\<^isub>1)"
  and finite2: "finite (QUOT L\<^isub>2)"
  shows "finite (range (tag_str_ALT L\<^isub>1 L\<^isub>2))"
proof -
  have "{y. \<exists>x. y = (\<lbrakk>x\<rbrakk>L\<^isub>1, \<lbrakk>x\<rbrakk>L\<^isub>2)} \<subseteq> (QUOT L\<^isub>1) \<times> (QUOT L\<^isub>2)"
    by (auto simp:QUOT_def)
  thus ?thesis using finite1 finite2
    by (auto simp: image_def tag_str_ALT_def UNION_def 
            intro: finite_subset[where B = "(QUOT L\<^isub>1) \<times> (QUOT L\<^isub>2)"])
qed

lemma tag_str_alt_inj:
  "tag_str_ALT L\<^isub>1 L\<^isub>2 x = tag_str_ALT L\<^isub>1 L\<^isub>2 y \<Longrightarrow> x \<equiv>(L\<^isub>1 \<union> L\<^isub>2)\<equiv> y"
apply (simp add:tag_str_ALT_def equiv_class_def equiv_str_def)
by blast
  
lemma quot_alt:
  assumes finite1: "finite (QUOT L\<^isub>1)"
  and finite2: "finite (QUOT L\<^isub>2)"
  shows "finite (QUOT (L\<^isub>1 \<union> L\<^isub>2))"
proof (rule_tac f = "(op `) (tag_str_ALT L\<^isub>1 L\<^isub>2)" in finite_imageD)
  show "finite (op ` (tag_str_ALT L\<^isub>1 L\<^isub>2) ` QUOT (L\<^isub>1 \<union> L\<^isub>2))"
    using finite_tag_image tag_str_alt_range_finite finite1 finite2
    by auto
next
  show "inj_on (op ` (tag_str_ALT L\<^isub>1 L\<^isub>2)) (QUOT (L\<^isub>1 \<union> L\<^isub>2))"
    apply (rule_tac str_inj_imps)
    by (erule_tac tag_str_alt_inj)
qed

(* list_diff:: list substract, once different return tailer *)
fun list_diff :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" (infix "-" 51)
where
  "list_diff []  xs = []" |
  "list_diff (x#xs) [] = x#xs" |
  "list_diff (x#xs) (y#ys) = (if x = y then list_diff xs ys else (x#xs))"

lemma [simp]: "(x @ y) - x = y"
apply (induct x)
by (case_tac y, simp+)

lemma [simp]: "x - x = []"
by (induct x, auto)

lemma [simp]: "x = xa @ y \<Longrightarrow> x - xa = y "
by (induct x, auto)

lemma [simp]: "x - [] = x"
by (induct x, auto)

lemma [simp]: "xa \<le> x \<Longrightarrow> (x @ y) - xa = (x - xa) @ y"
by (auto elim:prefixE)

definition tag_str_SEQ:: "string set \<Rightarrow> string set \<Rightarrow> string \<Rightarrow> (string set \<times> string set set)"
where
  "tag_str_SEQ L\<^isub>1 L\<^isub>2 x \<equiv> if (\<exists> xa \<le> x. xa \<in> L\<^isub>1)
                         then (\<lbrakk>x\<rbrakk>L\<^isub>1, {\<lbrakk>(x - xa)\<rbrakk>L\<^isub>2 | xa.  xa \<le> x \<and> xa \<in> L\<^isub>1})
                         else (\<lbrakk>x\<rbrakk>L\<^isub>1, {})"

lemma tag_seq_eq_E:
  "tag_str_SEQ L\<^isub>1 L\<^isub>2 x = tag_str_SEQ L\<^isub>1 L\<^isub>2 y \<Longrightarrow>
   ((\<exists> xa \<le> x. xa \<in> L\<^isub>1) \<and> \<lbrakk>x\<rbrakk>L\<^isub>1 = \<lbrakk>y\<rbrakk>L\<^isub>1 \<and> 
    {\<lbrakk>(x - xa)\<rbrakk>L\<^isub>2 | xa. xa \<le> x \<and> xa \<in> L\<^isub>1} = {\<lbrakk>(y - ya)\<rbrakk>L\<^isub>2 | ya. ya \<le> y \<and> ya \<in> L\<^isub>1} ) \<or>
   ((\<forall> xa \<le> x. xa \<notin> L\<^isub>1) \<and> \<lbrakk>x\<rbrakk>L\<^isub>1 = \<lbrakk>y\<rbrakk>L\<^isub>1)"
by (simp add:tag_str_SEQ_def split:if_splits, blast)

lemma tag_str_seq_range_finite:
  assumes finite1: "finite (QUOT L\<^isub>1)"
  and finite2: "finite (QUOT L\<^isub>2)"
  shows "finite (range (tag_str_SEQ L\<^isub>1 L\<^isub>2))"
proof -
  have "(range (tag_str_SEQ L\<^isub>1 L\<^isub>2)) \<subseteq> (QUOT L\<^isub>1) \<times> (Pow (QUOT L\<^isub>2))"
    by (auto simp:image_def tag_str_SEQ_def QUOT_def)
  thus ?thesis using finite1 finite2 
    by (rule_tac B = "(QUOT L\<^isub>1) \<times> (Pow (QUOT L\<^isub>2))" in finite_subset, auto)
qed
  
lemma app_in_seq_decom[rule_format]:
  "\<forall> x. x @ z \<in> L\<^isub>1 ; L\<^isub>2 \<longrightarrow> (\<exists> xa \<le> x. xa \<in> L\<^isub>1 \<and> (x - xa) @ z \<in> L\<^isub>2) \<or> 
                            (\<exists> za \<le> z. (x @ za) \<in> L\<^isub>1 \<and> (z - za) \<in> L\<^isub>2)"
apply (induct z)
apply (simp, rule allI, rule impI, rule disjI1)
apply (clarsimp simp add:lang_seq_def)
apply (rule_tac x = s1 in exI, simp)
apply (rule allI | rule impI)+
apply (drule_tac x = "x @ [a]" in spec, simp)
apply (erule exE | erule conjE | erule disjE)+
apply (rule disjI2, rule_tac x = "[a]" in exI, simp)
apply (rule disjI1, rule_tac x = xa in exI, simp) 
apply (erule exE | erule conjE)+
apply (rule disjI2, rule_tac x = "a # za" in exI, simp)
done

lemma tag_str_seq_inj:
  assumes tag_eq: "tag_str_SEQ L\<^isub>1 L\<^isub>2 x = tag_str_SEQ L\<^isub>1 L\<^isub>2 y"
  shows "(x::string) \<equiv>(L\<^isub>1 ; L\<^isub>2)\<equiv> y"
proof -
  have aux: "\<And> x y z. \<lbrakk>tag_str_SEQ L\<^isub>1 L\<^isub>2 x = tag_str_SEQ L\<^isub>1 L\<^isub>2 y; x @ z \<in> L\<^isub>1 ; L\<^isub>2\<rbrakk> 
                       \<Longrightarrow> y @ z \<in> L\<^isub>1 ; L\<^isub>2"
  proof (drule app_in_seq_decom, erule disjE)
    fix x y z
    assume tag_eq: "tag_str_SEQ L\<^isub>1 L\<^isub>2 x = tag_str_SEQ L\<^isub>1 L\<^isub>2 y"
      and x_gets_l2: "\<exists>xa\<le>x. xa \<in> L\<^isub>1 \<and> (x - xa) @ z \<in> L\<^isub>2"
    from x_gets_l2 have "\<exists> xa \<le> x. xa \<in> L\<^isub>1" by blast
    hence xy_l2:"{\<lbrakk>(x - xa)\<rbrakk>L\<^isub>2 | xa. xa \<le> x \<and> xa \<in> L\<^isub>1} = {\<lbrakk>(y - ya)\<rbrakk>L\<^isub>2 | ya. ya \<le> y \<and> ya \<in> L\<^isub>1}"
      using tag_eq tag_seq_eq_E by blast
    from x_gets_l2 obtain xa where xa_le_x: "xa \<le> x"
                               and xa_in_l1: "xa \<in> L\<^isub>1"
                               and rest_in_l2: "(x - xa) @ z \<in> L\<^isub>2" by blast
    hence "\<exists> ya. \<lbrakk>(x - xa)\<rbrakk>L\<^isub>2 = \<lbrakk>(y - ya)\<rbrakk>L\<^isub>2 \<and> ya \<le> y \<and> ya \<in> L\<^isub>1" using xy_l2 by auto
    then obtain ya where ya_le_x: "ya \<le> y"
                     and ya_in_l1: "ya \<in> L\<^isub>1"
                     and rest_eq: "\<lbrakk>(x - xa)\<rbrakk>L\<^isub>2 = \<lbrakk>(y - ya)\<rbrakk>L\<^isub>2" by blast
    from rest_eq rest_in_l2 have "(y - ya) @ z \<in> L\<^isub>2" 
      by (auto simp:equiv_class_def equiv_str_def)
    hence "ya @ ((y - ya) @ z) \<in> L\<^isub>1 ; L\<^isub>2" using ya_in_l1
      by (auto simp:lang_seq_def)
    thus "y @ z \<in> L\<^isub>1 ; L\<^isub>2" using ya_le_x 
      by (erule_tac prefixE, simp)
  next
    fix x y z
    assume tag_eq: "tag_str_SEQ L\<^isub>1 L\<^isub>2 x = tag_str_SEQ L\<^isub>1 L\<^isub>2 y"
      and x_gets_l1: "\<exists>za\<le>z. x @ za \<in> L\<^isub>1 \<and> z - za \<in> L\<^isub>2"
    from tag_eq tag_seq_eq_E have x_y_eq: "\<lbrakk>x\<rbrakk>L\<^isub>1 = \<lbrakk>y\<rbrakk>L\<^isub>1" by blast
    from x_gets_l1 obtain za where za_le_z: "za \<le> z"
                               and x_za_in_l1: "(x @ za) \<in> L\<^isub>1"
                               and rest_in_l2: "z - za \<in> L\<^isub>2" by blast
    from x_y_eq x_za_in_l1 have y_za_in_l1: "y @ za \<in> L\<^isub>1"
      by (auto simp:equiv_class_def equiv_str_def)
    hence "(y @ za) @ (z - za) \<in> L\<^isub>1 ; L\<^isub>2" using rest_in_l2
      apply (simp add:lang_seq_def)
      by (rule_tac x = "y @ za" in exI, rule_tac x = "z - za" in exI, simp)
    thus "y @ z \<in> L\<^isub>1 ; L\<^isub>2" using za_le_z
      by (erule_tac prefixE, simp)
  qed
  show ?thesis using tag_eq
    apply (simp add:equiv_str_def)
    by (auto intro:aux)
qed

lemma quot_seq: 
  assumes finite1: "finite (QUOT L\<^isub>1)"
  and finite2: "finite (QUOT L\<^isub>2)"
  shows "finite (QUOT (L\<^isub>1;L\<^isub>2))"
proof (rule_tac f = "(op `) (tag_str_SEQ L\<^isub>1 L\<^isub>2)" in finite_imageD)
  show "finite (op ` (tag_str_SEQ L\<^isub>1 L\<^isub>2) ` QUOT (L\<^isub>1 ; L\<^isub>2))"
    using finite_tag_image tag_str_seq_range_finite finite1 finite2
    by auto
next
  show "inj_on (op ` (tag_str_SEQ L\<^isub>1 L\<^isub>2)) (QUOT (L\<^isub>1 ; L\<^isub>2))"
    apply (rule_tac str_inj_imps)
    by (erule_tac tag_str_seq_inj)
qed

(****************** the STAR case ************************)

lemma app_eq_elim[rule_format]:
  "\<And> a. \<forall> b x y. a @ b = x @ y \<longrightarrow> (\<exists> aa ab. a = aa @ ab \<and> x = aa \<and> y = ab @ b) \<or>
                                   (\<exists> ba bb. b = ba @ bb \<and> x = a @ ba \<and> y = bb \<and> ba \<noteq> [])"
  apply (induct_tac a rule:List.induct, simp)
  apply (rule allI | rule impI)+
  by (case_tac x, auto)

definition tag_str_STAR:: "string set \<Rightarrow> string \<Rightarrow> string set set"
where
  "tag_str_STAR L\<^isub>1 x \<equiv> if (x = []) 
                       then {}
                       else {\<lbrakk>x\<^isub>2\<rbrakk>L\<^isub>1 | x\<^isub>1 x\<^isub>2. x =  x\<^isub>1 @ x\<^isub>2 \<and> x\<^isub>1 \<in> L\<^isub>1\<star> \<and> x\<^isub>2 \<noteq> []}"

lemma tag_str_star_range_finite:
  assumes finite1: "finite (QUOT L\<^isub>1)"
  shows "finite (range (tag_str_STAR L\<^isub>1))"
proof -
  have "range (tag_str_STAR L\<^isub>1) \<subseteq> Pow (QUOT L\<^isub>1)"
    by (auto simp:image_def tag_str_STAR_def QUOT_def)
  thus ?thesis using finite1
    by (rule_tac B = "Pow (QUOT L\<^isub>1)" in finite_subset, auto)
qed

lemma star_prop[rule_format]: "x \<in> lang\<star> \<Longrightarrow> \<forall> y. y \<in> lang\<star> \<longrightarrow> x @ y \<in> lang\<star>"
by (erule Star.induct, auto)

lemma star_prop2: "y \<in> lang \<Longrightarrow> y \<in> lang\<star>"
by (drule step[of y lang "[]"], auto simp:start)

lemma star_prop3[rule_format]: "x \<in> lang\<star> \<Longrightarrow> \<forall>y . y \<in> lang \<longrightarrow> x @ y \<in> lang\<star>"
by (erule Star.induct, auto intro:star_prop2)

lemma postfix_prop: "y >>= (x @ y) \<Longrightarrow> x = []"
by (erule postfixE, induct x arbitrary:y, auto)

lemma inj_aux:
  "\<lbrakk>(m @ z) \<in> L\<^isub>1\<star>; m \<equiv>L\<^isub>1\<equiv> yb; xa @ m = x; xa \<in> L\<^isub>1\<star>; m \<noteq> [];
    \<forall> xa xb. x = xa @ xb \<and> xa \<in> L\<^isub>1\<star> \<and> xb \<noteq> [] \<and> xb @ z \<in> L\<^isub>1\<star> \<longrightarrow> xb >>= m\<rbrakk> 
  \<Longrightarrow> (yb @ z) \<in> L\<^isub>1\<star>"
proof- 
  have "\<And>s. s \<in> L\<^isub>1\<star> \<Longrightarrow> \<forall> m z yb. (s = m @ z \<and> m \<equiv>L\<^isub>1\<equiv> yb \<and> x = xa @ m \<and> xa \<in> L\<^isub>1\<star> \<and> m \<noteq> [] \<and>  
    (\<forall> xa xb. x = xa @ xb \<and> xa \<in> L\<^isub>1\<star> \<and> xb \<noteq> [] \<and> xb @ z \<in> L\<^isub>1\<star> \<longrightarrow> xb >>= m) \<longrightarrow> (yb @ z) \<in> L\<^isub>1\<star>)"    
    apply (erule Star.induct, simp)
    apply (rule allI | rule impI | erule conjE)+
    apply (drule app_eq_elim)
    apply (erule disjE | erule exE | erule conjE)+
    apply simp
    apply (simp (no_asm) only:append_assoc[THEN sym])
    apply (rule step) 
    apply (simp add:equiv_str_def)
    apply simp

    apply (erule exE | erule conjE)+    
    apply (rotate_tac 3)
    apply (frule_tac x = "xa @ s1" in spec)
    apply (rotate_tac 12)
    apply (drule_tac x = ba in spec)
    apply (erule impE)
    apply ( simp add:star_prop3)
    apply (simp)
    apply (drule postfix_prop)
    apply simp
    done
  thus "\<lbrakk>(m @ z) \<in> L\<^isub>1\<star>; m \<equiv>L\<^isub>1\<equiv> yb; xa @ m = x; xa \<in> L\<^isub>1\<star>; m \<noteq> [];
         \<forall> xa xb. x = xa @ xb \<and> xa \<in> L\<^isub>1\<star> \<and> xb \<noteq> [] \<and> xb @ z \<in> L\<^isub>1\<star> \<longrightarrow> xb >>= m\<rbrakk> 
        \<Longrightarrow> (yb @ z) \<in> L\<^isub>1\<star>" by blast
qed


lemma min_postfix_exists[rule_format]:
  "finite A \<Longrightarrow> A \<noteq> {} \<and> (\<forall> a \<in> A. \<forall> b \<in> A. ((b >>= a) \<or> (a >>= b))) 
                \<longrightarrow> (\<exists> min. (min \<in> A \<and> (\<forall> a \<in> A. a >>= min)))"
apply (erule finite.induct)
apply simp
apply simp
apply (case_tac "A = {}")
apply simp
apply clarsimp
apply (case_tac "a >>= min")
apply (rule_tac x = min in exI, simp)
apply (rule_tac x = a in exI, simp)
apply clarify
apply (rotate_tac 5)
apply (erule_tac x = aa in ballE) defer apply simp
apply (erule_tac ys = min in postfix_trans)
apply (erule_tac x = min in ballE) 
by simp+

lemma tag_str_star_inj:
  "tag_str_STAR L\<^isub>1 x = tag_str_STAR L\<^isub>1 (y::string) \<Longrightarrow> x \<equiv>L\<^isub>1\<star>\<equiv> y"
proof -
  have aux: "\<And> x y z. \<lbrakk>tag_str_STAR L\<^isub>1 x = tag_str_STAR L\<^isub>1 y; x @ z \<in> L\<^isub>1\<star>\<rbrakk> \<Longrightarrow> y @ z \<in> L\<^isub>1\<star>"
  proof-
    fix x y z
    assume tag_eq: "tag_str_STAR L\<^isub>1 x = tag_str_STAR L\<^isub>1 y"
      and x_z: "x @ z \<in> L\<^isub>1\<star>"
    show "y @ z \<in> L\<^isub>1\<star>"
    proof (cases "x = []")
      case True
      with tag_eq have "y = []" by (simp add:tag_str_STAR_def split:if_splits, blast)
      thus ?thesis using x_z True by simp
    next
      case False
      hence not_empty: "{xb. \<exists> xa. x = xa @ xb \<and> xa \<in> L\<^isub>1\<star> \<and> xb \<noteq> [] \<and> xb @ z \<in> L\<^isub>1\<star>} \<noteq> {}" using x_z
        by (simp, rule_tac x = x in exI, rule_tac x = "[]" in exI, simp add:start)
      have finite_set: "finite {xb. \<exists> xa. x = xa @ xb \<and> xa \<in> L\<^isub>1\<star> \<and> xb \<noteq> [] \<and> xb @ z \<in> L\<^isub>1\<star>}"
        apply (rule_tac B = "{xb. \<exists> xa. x = xa @ xb}" in finite_subset)
        apply auto
        apply (induct x, simp)
        apply (subgoal_tac "{xb. \<exists>xa. a # x = xa @ xb} = insert (a # x) {xb. \<exists>xa. x = xa @ xb}")
        apply auto
        by (case_tac xaa, simp+)
      have comparable: "\<forall> a \<in> {xb. \<exists> xa. x = xa @ xb \<and> xa \<in> L\<^isub>1\<star> \<and> xb \<noteq> [] \<and> xb @ z \<in> L\<^isub>1\<star>}. 
                        \<forall> b \<in> {xb. \<exists> xa. x = xa @ xb \<and> xa \<in> L\<^isub>1\<star> \<and> xb \<noteq> [] \<and> xb @ z \<in> L\<^isub>1\<star>}.
                          ((b >>= a) \<or> (a >>= b))"
        by (auto simp:postfix_def, drule app_eq_elim, blast)
      hence "\<exists> min. min \<in> {xb. \<exists> xa. x = xa @ xb \<and> xa \<in> L\<^isub>1\<star> \<and> xb \<noteq> [] \<and> xb @ z \<in> L\<^isub>1\<star>} 
                \<and> (\<forall> xa xb. x = xa @ xb \<and> xa \<in> L\<^isub>1\<star> \<and> xb \<noteq> [] \<and> xb @ z \<in> L\<^isub>1\<star> \<longrightarrow> xb >>= min)"
        using finite_set not_empty comparable
        apply (drule_tac min_postfix_exists, simp)
        by (erule exE, rule_tac x = min in exI, auto)
      then obtain min xa where x_decom: "x = xa @ min \<and> xa \<in> L\<^isub>1\<star>"
        and min_not_empty: "min \<noteq> []"
        and min_z_in_star: "min @ z \<in> L\<^isub>1\<star>"
        and is_min: "\<forall> xa xb. x = xa @ xb \<and> xa \<in> L\<^isub>1\<star> \<and> xb \<noteq> [] \<and> xb @ z \<in> L\<^isub>1\<star> \<longrightarrow> xb >>= min"  by blast
      from x_decom min_not_empty have "\<lbrakk>min\<rbrakk>L\<^isub>1 \<in> tag_str_STAR L\<^isub>1 x"  by (auto simp:tag_str_STAR_def)
      hence "\<exists> yb. \<lbrakk>yb\<rbrakk>L\<^isub>1 \<in> tag_str_STAR L\<^isub>1 y \<and> \<lbrakk>min\<rbrakk>L\<^isub>1 = \<lbrakk>yb\<rbrakk>L\<^isub>1" using tag_eq by auto
      hence "\<exists> ya yb. y = ya @ yb \<and> ya \<in> L\<^isub>1\<star> \<and> min \<equiv>L\<^isub>1\<equiv> yb \<and> yb \<noteq> [] " 
        by (simp add:tag_str_STAR_def equiv_class_def equiv_str_def split:if_splits, blast)        
      then obtain ya yb where y_decom: "y = ya @ yb"
                          and ya_in_star: "ya \<in> L\<^isub>1\<star>"
                          and yb_not_empty: "yb \<noteq> []"
                          and min_yb_eq: "min \<equiv>L\<^isub>1\<equiv> yb"  by blast
      from min_z_in_star min_yb_eq min_not_empty is_min x_decom
      have "yb @ z \<in> L\<^isub>1\<star>"
        by (rule_tac x = x and xa = xa in inj_aux, simp+)
      thus ?thesis using ya_in_star y_decom
        by (auto dest:star_prop)        
    qed
  qed
  show "tag_str_STAR L\<^isub>1 x = tag_str_STAR L\<^isub>1 (y::string) \<Longrightarrow> x \<equiv>L\<^isub>1\<star>\<equiv> y"
    by (auto intro:aux simp:equiv_str_def)
qed

lemma quot_star:  
  assumes finite1: "finite (QUOT L\<^isub>1)"
  shows "finite (QUOT (L\<^isub>1\<star>))"
proof (rule_tac f = "(op `) (tag_str_STAR L\<^isub>1)" in finite_imageD)
  show "finite (op ` (tag_str_STAR L\<^isub>1) ` QUOT (L\<^isub>1\<star>))"
    using finite_tag_image tag_str_star_range_finite finite1
    by auto
next
  show "inj_on (op ` (tag_str_STAR L\<^isub>1)) (QUOT (L\<^isub>1\<star>))"
    apply (rule_tac str_inj_imps)
    by (erule_tac tag_str_star_inj)
qed

lemma other_direction:
  "Lang = L (r::rexp) \<Longrightarrow> finite (QUOT Lang)"
apply (induct arbitrary:Lang rule:rexp.induct)
apply (simp add:QUOT_def equiv_class_def equiv_str_def)
by (simp_all add:quot_lambda quot_single quot_seq quot_alt quot_star)  

end