Myhill_1.thy
author urbanc
Thu, 10 Feb 2011 12:32:45 +0000
changeset 94 5b12cd0a3b3c
parent 92 a9ebc410a5c8
child 95 9540c2f2ea77
permissions -rw-r--r--
latest on the paper

theory Myhill_1
imports Main Folds While_Combinator
begin

section {* Preliminary definitions *}

types lang = "string set"


text {*  Sequential composition of two languages *}

definition 
  Seq :: "lang \<Rightarrow> lang \<Rightarrow> lang" (infixr ";;" 100)
where 
  "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}"


text {* Some properties of operator @{text ";;"}. *}

lemma seq_add_left:
  assumes a: "A = B"
  shows "C ;; A = C ;; B"
using a by simp

lemma seq_union_distrib_right:
  shows "(A \<union> B) ;; C = (A ;; C) \<union> (B ;; C)"
unfolding Seq_def by auto

lemma seq_union_distrib_left:
  shows "C ;; (A \<union> B) = (C ;; A) \<union> (C ;; B)"
unfolding Seq_def by  auto

lemma seq_intro:
  assumes a: "x \<in> A" "y \<in> B"
  shows "x @ y \<in> A ;; B "
using a by (auto simp: Seq_def)

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

lemma seq_empty [simp]:
  shows "A ;; {[]} = A"
  and   "{[]} ;; A = A"
by (simp_all add: Seq_def)


text {* Power and Star of a language *}

fun 
  pow :: "lang \<Rightarrow> nat \<Rightarrow> lang" (infixl "\<up>" 100)
where
  "A \<up> 0 = {[]}"
| "A \<up> (Suc n) =  A ;; (A \<up> n)" 

definition
  Star :: "lang \<Rightarrow> lang" ("_\<star>" [101] 102)
where
  "A\<star> \<equiv> (\<Union>n. A \<up> n)"


lemma star_start[intro]:
  shows "[] \<in> A\<star>"
proof -
  have "[] \<in> A \<up> 0" by auto
  then show "[] \<in> A\<star>" unfolding Star_def by blast
qed

lemma star_step [intro]:
  assumes a: "s1 \<in> A" 
  and     b: "s2 \<in> A\<star>"
  shows "s1 @ s2 \<in> A\<star>"
proof -
  from b obtain n where "s2 \<in> A \<up> n" unfolding Star_def by auto
  then have "s1 @ s2 \<in> A \<up> (Suc n)" using a by (auto simp add: Seq_def)
  then show "s1 @ s2 \<in> A\<star>" unfolding Star_def by blast
qed

lemma star_induct[consumes 1, case_names start step]:
  assumes a: "x \<in> A\<star>" 
  and     b: "P []"
  and     c: "\<And>s1 s2. \<lbrakk>s1 \<in> A; s2 \<in> A\<star>; P s2\<rbrakk> \<Longrightarrow> P (s1 @ s2)"
  shows "P x"
proof -
  from a obtain n where "x \<in> A \<up> n" unfolding Star_def by auto
  then show "P x"
    by (induct n arbitrary: x)
       (auto intro!: b c simp add: Seq_def Star_def)
qed
    
lemma star_intro1:
  assumes a: "x \<in> A\<star>"
  and     b: "y \<in> A\<star>"
  shows "x @ y \<in> A\<star>"
using a b
by (induct rule: star_induct) (auto)

lemma star_intro2: 
  assumes a: "y \<in> A"
  shows "y \<in> A\<star>"
proof -
  from a have "y @ [] \<in> A\<star>" by blast
  then show "y \<in> A\<star>" by simp
qed

lemma star_intro3:
  assumes a: "x \<in> A\<star>"
  and     b: "y \<in> A"
  shows "x @ y \<in> A\<star>"
using a b by (blast intro: star_intro1 star_intro2)

lemma star_cases:
  shows "A\<star> =  {[]} \<union> A ;; A\<star>"
proof
  { fix x
    have "x \<in> A\<star> \<Longrightarrow> x \<in> {[]} \<union> A ;; A\<star>"
      unfolding Seq_def
    by (induct rule: star_induct) (auto)
  }
  then show "A\<star> \<subseteq> {[]} \<union> A ;; A\<star>" by auto
next
  show "{[]} \<union> A ;; A\<star> \<subseteq> A\<star>"
    unfolding Seq_def by auto
qed

lemma star_decom: 
  assumes a: "x \<in> A\<star>" "x \<noteq> []"
  shows "\<exists>a b. x = a @ b \<and> a \<noteq> [] \<and> a \<in> A \<and> b \<in> A\<star>"
using a
by (induct rule: star_induct) (blast)+

lemma
  shows seq_Union_left:  "B ;; (\<Union>n. A \<up> n) = (\<Union>n. B ;; (A \<up> n))"
  and   seq_Union_right: "(\<Union>n. A \<up> n) ;; B = (\<Union>n. (A \<up> n) ;; B)"
unfolding Seq_def by auto

lemma seq_pow_comm:
  shows "A ;; (A \<up> n) = (A \<up> n) ;; A"
by (induct n) (simp_all add: seq_assoc[symmetric])

lemma seq_star_comm:
  shows "A ;; A\<star> = A\<star> ;; A"
unfolding Star_def seq_Union_left
unfolding seq_pow_comm seq_Union_right 
by simp


text {* Two lemmas about the length of strings in @{text "A \<up> n"} *}

lemma pow_length:
  assumes a: "[] \<notin> A"
  and     b: "s \<in> A \<up> Suc n"
  shows "n < length s"
using b
proof (induct n arbitrary: s)
  case 0
  have "s \<in> A \<up> Suc 0" by fact
  with a have "s \<noteq> []" by auto
  then show "0 < length s" by auto
next
  case (Suc n)
  have ih: "\<And>s. s \<in> A \<up> Suc n \<Longrightarrow> n < length s" by fact
  have "s \<in> A \<up> Suc (Suc n)" by fact
  then obtain s1 s2 where eq: "s = s1 @ s2" and *: "s1 \<in> A" and **: "s2 \<in> A \<up> Suc n"
    by (auto simp add: Seq_def)
  from ih ** have "n < length s2" by simp
  moreover have "0 < length s1" using * a by auto
  ultimately show "Suc n < length s" unfolding eq 
    by (simp only: length_append)
qed

lemma seq_pow_length:
  assumes a: "[] \<notin> A"
  and     b: "s \<in> B ;; (A \<up> Suc n)"
  shows "n < length s"
proof -
  from b obtain s1 s2 where eq: "s = s1 @ s2" and *: "s2 \<in> A \<up> Suc n"
    unfolding Seq_def by auto
  from * have " n < length s2" by (rule pow_length[OF a])
  then show "n < length s" using eq by simp
qed



section {* A modified version of Arden's lemma *}


text {*  A helper lemma for Arden *}

lemma arden_helper:
  assumes eq: "X = X ;; A \<union> B"
  shows "X = X ;; (A \<up> Suc n) \<union> (\<Union>m\<in>{0..n}. B ;; (A \<up> m))"
proof (induct n)
  case 0 
  show "X = X ;; (A \<up> Suc 0) \<union> (\<Union>(m::nat)\<in>{0..0}. B ;; (A \<up> m))"
    using eq by simp
next
  case (Suc n)
  have ih: "X = X ;; (A \<up> Suc n) \<union> (\<Union>m\<in>{0..n}. B ;; (A \<up> m))" by fact
  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
  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))"
    by (simp add: seq_union_distrib_right seq_assoc)
  also have "\<dots> = X ;; (A \<up> Suc (Suc n)) \<union> (\<Union>m\<in>{0..Suc n}. B ;; (A \<up> m))"
    by (auto simp add: le_Suc_eq)
  finally show "X = X ;; (A \<up> Suc (Suc n)) \<union> (\<Union>m\<in>{0..Suc n}. B ;; (A \<up> m))" .
qed

theorem arden:
  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" 
    unfolding seq_star_comm[symmetric]
    by (rule star_cases)
  then have "B ;; A\<star> = B ;; ({[]} \<union> A\<star> ;; A)"
    by (rule seq_add_left)
  also have "\<dots> = B \<union> B ;; (A\<star> ;; A)"
    unfolding seq_union_distrib_left by simp
  also have "\<dots> = B \<union> (B ;; A\<star>) ;; A" 
    by (simp only: seq_assoc)
  finally show "X = X ;; A \<union> B" 
    using eq by blast 
next
  assume eq: "X = X ;; A \<union> B"
  { fix n::nat
    have "B ;; (A \<up> n) \<subseteq> X" using arden_helper[OF eq, of "n"] by auto }
  then have "B ;; A\<star> \<subseteq> X" 
    unfolding Seq_def Star_def UNION_def by auto
  moreover
  { fix s::string
    obtain k where "k = length s" by auto
    then have not_in: "s \<notin> X ;; (A \<up> Suc k)" 
      using seq_pow_length[OF nemp] by blast
    assume "s \<in> X"
    then have "s \<in> X ;; (A \<up> Suc k) \<union> (\<Union>m\<in>{0..k}. B ;; (A \<up> m))"
      using arden_helper[OF eq, of "k"] by auto
    then have "s \<in> (\<Union>m\<in>{0..k}. B ;; (A \<up> m))" using not_in by auto
    moreover
    have "(\<Union>m\<in>{0..k}. B ;; (A \<up> m)) \<subseteq> (\<Union>n. B ;; (A \<up> n))" by auto
    ultimately 
    have "s \<in> B ;; A\<star>" 
      unfolding seq_Union_left Star_def by auto }
  then have "X \<subseteq> B ;; A\<star>" by auto
  ultimately 
  show "X = B ;; A\<star>" by simp
qed

(*
corollary arden_eq:
  assumes nemp: "[] \<notin> A"
  shows "(X ;; A \<union> B) = (B ;; A\<star>)"
proof -
  { assume "X = X ;; A \<union> B"
    then have "X = B ;; A\<star>"
    then have ?thesis
      thm arden[THEN iffD1]
apply(rule set_eqI)
thm arden[THEN iffD1]
apply(rule iffI)
apply(rule trans)
apply(rule arden[THEN iffD2, symmetric])
apply(rule arden[OF iffD1, symmetric])
thm trans
proof -
  { assume "X = X ;; A \<union> B"
    then have "X = B ;; A\<star>" using arden[OF nemp] by blast
    moreover 
  
    
using arden[of "A" "X" "B", OF nemp]
apply(erule_tac iffE)
apply(blast)
*)


section {* Regular Expressions *}

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


text {* 
  The function @{text L} is overloaded, with the idea that @{text "L x"} 
  evaluates to the language represented by the object @{text x}.
*}

consts L:: "'a \<Rightarrow> lang"

overloading L_rexp \<equiv> "L::  rexp \<Rightarrow> lang"
begin
fun
  L_rexp :: "rexp \<Rightarrow> lang"
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


text {* ALT-combination of a set or regulare expressions *}

abbreviation
  Setalt  ("\<Uplus>_" [1000] 999) 
where
  "\<Uplus>A \<equiv> folds ALT NULL A"

text {* 
  For finite sets, @{term Setalt} is preserved under @{term L}.
*}

lemma folds_alt_simp [simp]:
  fixes rs::"rexp set"
  assumes a: "finite rs"
  shows "L (\<Uplus>rs) = \<Union> (L ` rs)"
unfolding folds_def
apply(rule set_eqI)
apply(rule someI2_ex)
apply(rule_tac finite_imp_fold_graph[OF a])
apply(erule fold_graph.induct)
apply(auto)
done



section {* Direction @{text "finite partition \<Rightarrow> regular language"} *}


text {* Just a technical lemma for collections and pairs *}

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

text {* Myhill-Nerode relation *}

definition
  str_eq_rel :: "lang \<Rightarrow> (string \<times> string) set" ("\<approx>_" [100] 100)
where
  "\<approx>A \<equiv> {(x, y).  (\<forall>z. x @ z \<in> A \<longleftrightarrow> y @ z \<in> A)}"

text {* 
  Among the equivalence clases of @{text "\<approx>A"}, the set @{text "finals A"} 
  singles out those which contains the strings from @{text A}.
*}

definition 
  finals :: "lang \<Rightarrow> lang set"
where
  "finals A \<equiv> {\<approx>A `` {x} | x . x \<in> A}"


lemma lang_is_union_of_finals: 
  shows "A = \<Union> finals A"
unfolding finals_def
unfolding Image_def
unfolding str_eq_rel_def
apply(auto)
apply(drule_tac x = "[]" in spec)
apply(auto)
done

lemma finals_in_partitions:
  shows "finals A \<subseteq> (UNIV // \<approx>A)"
unfolding finals_def quotient_def
by auto


section {* Equational systems *}


text {* The two kinds of terms in the rhs of equations. *}

datatype rhs_item = 
   Lam "rexp"            (* Lambda-marker *)
 | Trn "lang" "rexp"     (* Transition *)


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

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

text {* Transitions between equivalence classes *}

definition 
  transition :: "lang \<Rightarrow> char \<Rightarrow> lang \<Rightarrow> bool" ("_ \<Turnstile>_\<Rightarrow>_" [100,100,100] 100)
where
  "Y \<Turnstile>c\<Rightarrow> X \<equiv> Y ;; {[c]} \<subseteq> X"

text {* Initial equational system *}

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

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



section {* Arden Operation on equations *}

text {*
  The function @{text "attach_rexp r item"} SEQ-composes @{text r} to the
  right of every rhs-item.
*}

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


definition
  "append_rhs_rexp rhs rexp \<equiv> (append_rexp rexp) ` rhs"

definition 
  "Arden X rhs \<equiv> 
     append_rhs_rexp (rhs - {Trn X r | r. Trn X r \<in> rhs}) (STAR (\<Uplus> {r. Trn X r \<in> rhs}))"


section {* Substitution Operation on equations *}

text {* 
  Suppose and equation @{text "X = xrhs"}, @{text "subst_op"} substitutes 
  all occurences of @{text "X"} in @{text "rhs"} by @{text "xrhs"}.
*}

definition 
  "Subst rhs X xrhs \<equiv> 
        (rhs - {Trn X r | r. Trn X r \<in> rhs}) \<union> (append_rhs_rexp xrhs (\<Uplus> {r. Trn X r \<in> rhs}))"

text {*
  @{text "eqs_subst ES X xrhs"} substitutes @{text xrhs} into every 
  equation of the equational system @{text ES}.
*}

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


section {* While-combinator *}

text {*
  The following term @{text "remove ES Y yrhs"} removes the equation
  @{text "Y = yrhs"} from equational system @{text "ES"} by replacing
  all occurences of @{text "Y"} by its definition (using @{text "eqs_subst"}).
  The @{text "Y"}-definition is made non-recursive using Arden's transformation
  @{text "arden_variate Y yrhs"}.
  *}

definition
  "remove_op ES Y yrhs \<equiv> 
      Subst_all  (ES - {(Y, yrhs)}) Y (Arden Y yrhs)"

text {*
  The following term @{text "iterm X ES"} represents one iteration in the while loop.
  It arbitrarily chooses a @{text "Y"} different from @{text "X"} to remove.
*}

definition 
  "iter X ES \<equiv> (let (Y, yrhs) = SOME (Y, yrhs). (Y, yrhs) \<in> ES \<and> (X \<noteq> Y)
                in remove_op ES Y yrhs)"

text {*
  The following term @{text "reduce X ES"} repeatedly removes characteriztion equations
  for unknowns other than @{text "X"} until one is left.
*}

definition 
  "reduce X ES \<equiv> while (\<lambda> ES. card ES \<noteq> 1) (iter X) ES"

text {*
  Since the @{text "while"} combinator from HOL library is used to implement @{text "reduce X ES"},
  the induction principle @{thm [source] while_rule} is used to proved the desired properties
  of @{text "reduce X ES"}. For this purpose, an invariant predicate @{text "invariant"} is defined
  in terms of a series of auxilliary predicates:
*}

section {* Invariants *}

text {* Every variable is defined at most onece in @{text ES}. *}

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

text {* 
  Every equation in @{text ES} (represented by @{text "(X, rhs)"}) 
  is valid, i.e. @{text "(X = L rhs)"}.
*}

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

text {*
  @{text "rhs_nonempty rhs"} requires regular expressions occuring in 
  transitional items of @{text "rhs"} do not contain empty string. This is 
  necessary for the application of Arden's transformation to @{text "rhs"}.
*}

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

text {*
  The following @{text "ardenable ES"} requires that Arden's transformation 
  is applicable to every equation of equational system @{text "ES"}.
*}

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

text {* 
  @{text "finite_rhs ES"} requires every equation in @{text "rhs"} 
  be finite.
*}
definition
  "finite_rhs ES \<equiv> \<forall> X rhs. (X, rhs) \<in> ES \<longrightarrow> finite rhs"

text {*
  @{text "classes_of rhs"} returns all variables (or equivalent classes)
  occuring in @{text "rhs"}.
  *}

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

text {*
  @{text "lefts_of ES"} returns all variables defined by an 
  equational system @{text "ES"}.
*}
definition
  "lefts_of ES \<equiv> {Y | Y yrhs. (Y, yrhs) \<in> ES}"

text {*
  The following @{text "self_contained ES"} requires that every variable occuring 
  on the right hand side of equations is already defined by some equation in @{text "ES"}.
*}
definition 
  "self_contained ES \<equiv> \<forall> (X, xrhs) \<in> ES. classes_of xrhs \<subseteq> lefts_of ES"


text {*
  The invariant @{text "invariant(ES)"} is a conjunction of all the previously defined constaints.
  *}
definition 
  "invariant ES \<equiv> valid_eqns ES \<and> finite ES \<and> distinct_equas ES \<and> ardenable ES \<and> 
                  finite_rhs ES \<and> self_contained ES"

subsection {* The proof of this direction *}

subsubsection {* Basic properties *}

text {*
  The following are some basic properties of the above definitions.
*}

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

lemma finite_Trn:
  assumes fin: "finite rhs"
  shows "finite {r. Trn Y r \<in> rhs}"
proof -
  have "finite {Trn Y r | Y r. Trn Y r \<in> rhs}"
    by (rule rev_finite_subset[OF fin]) (auto)
  then have "finite ((\<lambda>(Y, r). Trn Y r) ` {(Y, r) | Y r. Trn Y r \<in> rhs})"
    by (simp add: image_Collect)
  then have "finite {(Y, r) | Y r. Trn Y r \<in> rhs}"
    by (erule_tac finite_imageD) (simp add: inj_on_def)
  then show "finite {r. Trn Y r \<in> rhs}"
    by (erule_tac f="snd" in finite_surj) (auto simp add: image_def)
qed

lemma finite_Lam:
  assumes fin:"finite rhs"
  shows "finite {r. Lam r \<in> rhs}"
proof -
  have "finite {Lam r | r. Lam r \<in> rhs}"
    by (rule rev_finite_subset[OF fin]) (auto)
  then show "finite {r. Lam r \<in> rhs}"
    apply(simp add: image_Collect[symmetric])
    apply(erule finite_imageD)
    apply(auto simp add: inj_on_def)
    done
qed

lemma rexp_of_empty:
  assumes finite:"finite rhs"
  and nonempty:"rhs_nonempty rhs"
  shows "[] \<notin> L (\<Uplus> {r. Trn X r \<in> rhs})"
using finite nonempty rhs_nonempty_def
using finite_Trn[OF finite]
by (auto)

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

lemma lang_of_rexp_of:
  assumes finite:"finite rhs"
  shows "L ({Trn X r| r. Trn X r \<in> rhs}) = X ;; (L (\<Uplus>{r. Trn X r \<in> rhs}))"
proof -
  have "finite {r. Trn X r \<in> rhs}" 
    by (rule finite_Trn[OF finite]) 
  then show ?thesis
    apply(auto simp add: Seq_def)
    apply(rule_tac x = "s\<^isub>1" in exI, rule_tac x = "s\<^isub>2" in exI, auto)
    apply(rule_tac x= "Trn X xa" in exI)
    apply(auto simp: Seq_def)
    done
qed

lemma rexp_of_lam_eq_lam_set:
  assumes fin: "finite rhs"
  shows "L (\<Uplus>{r. Lam r \<in> rhs}) = L ({Lam r | r. Lam r \<in> rhs})"
proof -
  have "finite ({r. Lam r \<in> rhs})" using fin by (rule finite_Lam)
  then show ?thesis by auto
qed

lemma [simp]:
  "L (append_rexp r xb) = L xb ;; L r"
apply (cases xb, auto simp: Seq_def)
apply(rule_tac x = "s\<^isub>1 @ s\<^isub>1'" in exI, rule_tac x = "s\<^isub>2'" in exI)
apply(auto simp: Seq_def)
done

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 = "append_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)


subsubsection {* Intialization *}

text {*
  The following several lemmas until @{text "init_ES_satisfy_invariant"} shows that
  the initial equational system satisfies invariant @{text "invariant"}.
*}

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)

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 clarsimp
  moreover
  have "s \<in> Y" unfolding Y_def 
    unfolding Image_def str_eq_rel_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 \<Turnstile>c\<Rightarrow> X}"
        unfolding transition_def
	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 transition_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 transition_def)
qed

lemma init_ES_satisfy_invariant:
  assumes finite_CS: "finite (UNIV // (\<approx>Lang))"
  shows "invariant (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 "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:invariant_def)
qed

subsubsection {* Interation step *}

text {*
  From this point until @{text "iteration_step"}, 
  the correctness of the iteration step @{text "iter X ES"} is proved.
*}

lemma Arden_keeps_eq:
  assumes l_eq_r: "X = L rhs"
  and not_empty: "[] \<notin> L (\<Uplus>{r. Trn X r \<in> rhs})"
  and finite: "finite rhs"
  shows "X = L (Arden X rhs)"
proof -
  def A \<equiv> "L (\<Uplus>{r. Trn X r \<in> rhs})"
  def b \<equiv> "rhs - {Trn X r | r. Trn X r \<in> rhs}"
  def B \<equiv> "L b" 
  have "X = B ;; A\<star>"
  proof-
    have "L rhs = L({Trn X r | r. Trn X r \<in> rhs} \<union> b)" by (auto simp: b_def)
    also have "\<dots> = X ;; A \<union> B"
      unfolding L_rhs_union_distrib[symmetric]
      by (simp only: lang_of_rexp_of finite B_def A_def)
    finally show ?thesis
      using l_eq_r not_empty
      apply(rule_tac arden[THEN iffD1])
      apply(simp add: A_def)
      apply(simp)
      done
  qed
  moreover have "L (Arden X rhs) = B ;; A\<star>"
    by (simp only:Arden_def L_rhs_union_distrib lang_of_append_rhs 
                  B_def A_def b_def L_rexp.simps seq_union_distrib_left)
   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_keeps_finite:
  "finite rhs \<Longrightarrow> finite (Arden X rhs)"
by (auto simp:Arden_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_keeps_nonempty:
  "rhs_nonempty rhs \<Longrightarrow> rhs_nonempty (Arden X rhs)"
by (simp only:Arden_def append_keeps_nonempty nonempty_set_sub)


lemma Subst_keeps_nonempty:
  "\<lbrakk>rhs_nonempty rhs; rhs_nonempty xrhs\<rbrakk> \<Longrightarrow> rhs_nonempty (Subst rhs X xrhs)"
by (simp only:Subst_def append_keeps_nonempty  nonempty_set_union nonempty_set_sub)

lemma Subst_keeps_eq:
  assumes substor: "X = L xrhs"
  and finite: "finite rhs"
  shows "L (Subst rhs X xrhs) = L rhs" (is "?Left = ?Right")
proof-
  def A \<equiv> "L (rhs - {Trn X r | r. Trn X r \<in> rhs})"
  have "?Left = A \<union> L (append_rhs_rexp xrhs (\<Uplus>{r. Trn X r \<in> rhs}))"
    unfolding Subst_def
    unfolding L_rhs_union_distrib[symmetric]
    by (simp add: A_def)
  moreover have "?Right = A \<union> L ({Trn X r | r. Trn X r \<in> rhs})"
  proof-
    have "rhs = (rhs - {Trn X r | r. Trn X r \<in> rhs}) \<union> ({Trn X r | r. Trn X r \<in> rhs})" by auto
    thus ?thesis 
      unfolding A_def
      unfolding L_rhs_union_distrib
      by simp
  qed
  moreover have "L (append_rhs_rexp xrhs (\<Uplus>{r. Trn X r \<in> rhs})) = L ({Trn X r | r. Trn X r \<in> rhs})" 
    using finite substor  by (simp only:lang_of_append_rhs lang_of_rexp_of)
  ultimately show ?thesis by simp
qed

lemma Subst_keeps_finite_rhs:
  "\<lbrakk>finite rhs; finite yrhs\<rbrakk> \<Longrightarrow> finite (Subst rhs Y yrhs)"
by (auto simp:Subst_def append_keeps_finite)

lemma Subst_all_keeps_finite:
  assumes finite:"finite (ES:: (string set \<times> rhs_item set) set)"
  shows "finite (Subst_all ES Y yrhs)"
proof -
  have "finite {(Ya, 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, 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:Subst_all_def)
qed

lemma Subst_all_keeps_finite_rhs:
  "\<lbrakk>finite_rhs ES; finite yrhs\<rbrakk> \<Longrightarrow> finite_rhs (Subst_all ES Y yrhs)"
by (auto intro:Subst_keeps_finite_rhs simp add:Subst_all_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_removes_cl:
  "classes_of (Arden Y yrhs) = classes_of yrhs - {Y}"
apply (simp add:Arden_def append_rhs_keeps_cls)
by (auto simp:classes_of_def)

lemma lefts_of_keeps_cls:
  "lefts_of (Subst_all ES Y yrhs) = lefts_of ES"
by (auto simp:lefts_of_def Subst_all_def)

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

lemma Subst_all_keeps_self_contained:
  fixes Y
  assumes sc: "self_contained (ES \<union> {(Y, yrhs)})" (is "self_contained ?A")
  shows "self_contained (Subst_all ES Y (Arden Y yrhs))" 
                                                   (is "self_contained ?B")
proof-
  { fix X xrhs'
    assume "(X, xrhs') \<in> ?B"
    then obtain xrhs 
      where xrhs_xrhs': "xrhs' = Subst xrhs Y (Arden Y yrhs)"
      and X_in: "(X, xrhs) \<in> ES" by (simp add:Subst_all_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 Subst_all_def)
      moreover have "classes_of xrhs' \<subseteq> lefts_of ES"
      proof-
        have "classes_of xrhs' \<subseteq> 
                        classes_of xrhs \<union> classes_of (Arden Y yrhs) - {Y}"
        proof-
          have "Y \<notin> classes_of (Arden Y yrhs)" 
            using Arden_removes_cl by simp
          thus ?thesis using xrhs_xrhs' by (auto simp: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 Y yrhs) \<subseteq> lefts_of ES \<union> {Y}" 
          using sc 
          by (auto simp add:Arden_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:Subst_all_def self_contained_def)
qed

lemma Subst_all_satisfy_invariant:
  assumes invariant_ES: "invariant (ES \<union> {(Y, yrhs)})"
  shows "invariant (Subst_all ES Y (Arden Y yrhs))"
proof -  
  have finite_yrhs: "finite yrhs" 
    using invariant_ES by (auto simp:invariant_def finite_rhs_def)
  have nonempty_yrhs: "rhs_nonempty yrhs" 
    using invariant_ES by (auto simp:invariant_def ardenable_def)
  have Y_eq_yrhs: "Y = L yrhs" 
    using invariant_ES by (simp only:invariant_def valid_eqns_def, blast)
  have "distinct_equas (Subst_all ES Y (Arden Y yrhs))" 
    using invariant_ES
    by (auto simp:distinct_equas_def Subst_all_def invariant_def)
  moreover have "finite (Subst_all ES Y (Arden Y yrhs))" 
    using invariant_ES by (simp add:invariant_def Subst_all_keeps_finite)
  moreover have "finite_rhs (Subst_all ES Y (Arden Y yrhs))"
  proof-
    have "finite_rhs ES" using invariant_ES 
      by (simp add:invariant_def finite_rhs_def)
    moreover have "finite (Arden Y yrhs)"
    proof -
      have "finite yrhs" using invariant_ES 
        by (auto simp:invariant_def finite_rhs_def)
      thus ?thesis using Arden_keeps_finite by simp
    qed
    ultimately show ?thesis 
      by (simp add:Subst_all_keeps_finite_rhs)
  qed
  moreover have "ardenable (Subst_all ES Y (Arden Y yrhs))"
  proof - 
    { fix X rhs
      assume "(X, rhs) \<in> ES"
      hence "rhs_nonempty rhs"  using prems invariant_ES  
        by (simp add:invariant_def ardenable_def)
      with nonempty_yrhs 
      have "rhs_nonempty (Subst rhs Y (Arden Y yrhs))"
        by (simp add:nonempty_yrhs 
               Subst_keeps_nonempty Arden_keeps_nonempty)
    } thus ?thesis by (auto simp add:ardenable_def Subst_all_def)
  qed
  moreover have "valid_eqns (Subst_all ES Y (Arden Y yrhs))"
  proof-
    have "Y = L (Arden Y yrhs)" 
      using Y_eq_yrhs invariant_ES finite_yrhs nonempty_yrhs      
      by (rule_tac Arden_keeps_eq, (simp add:rexp_of_empty)+)
    thus ?thesis using invariant_ES 
      by (clarsimp simp add:valid_eqns_def 
              Subst_all_def Subst_keeps_eq invariant_def finite_rhs_def
                   simp del:L_rhs.simps)
  qed
  moreover 
  have self_subst: "self_contained (Subst_all ES Y (Arden Y yrhs))"
    using invariant_ES Subst_all_keeps_self_contained by (simp add:invariant_def)
  ultimately show ?thesis using invariant_ES by (simp add:invariant_def)
qed

lemma Subst_all_card_le: 
  assumes finite: "finite (ES::(string set \<times> rhs_item set) set)"
  shows "card (Subst_all ES Y yrhs) <= card ES"
proof-
  def f \<equiv> "\<lambda> x. ((fst x)::string set, Subst (snd x) Y yrhs)"
  have "Subst_all ES Y yrhs = f ` ES" 
    apply (auto simp:Subst_all_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 Subst_all_cls_remains: 
  "(X, xrhs) \<in> ES \<Longrightarrow> \<exists> xrhs'. (X, xrhs') \<in> (Subst_all ES Y yrhs)"
by (auto simp:Subst_all_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: "invariant ES"
  and    X_in_ES: "(X, xrhs) \<in> ES"
  and    not_T: "card ES \<noteq> 1"
  shows "(invariant (iter X ES) \<and> (\<exists> xrhs'.(X, xrhs') \<in> (iter X ES)) \<and> 
                (iter X ES, ES) \<in> measure card)"
proof -
  have finite_ES: "finite ES" using Inv_ES by (simp add: invariant_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)
  let ?ES' = "iter X ES"
  show ?thesis
  proof(unfold iter_def remove_op_def, rule someI2 [where a = "(Y, yrhs)"], clarsimp)
    from X_in_ES Y_in_ES and not_eq and Inv_ES
    show "(Y, yrhs) \<in> ES \<and> X \<noteq> Y"
      by (auto simp: invariant_def distinct_equas_def)
  next
    fix x
    let ?ES' = "let (Y, yrhs) = x in Subst_all (ES - {(Y, yrhs)}) Y (Arden Y yrhs)"
    assume prem: "case x of (Y, yrhs) \<Rightarrow> (Y, yrhs) \<in> ES \<and> (X \<noteq> Y)"
    thus "invariant (?ES') \<and> (\<exists>xrhs'. (X, xrhs') \<in> ?ES') \<and> (?ES', ES) \<in> measure card"
    proof(cases "x", simp)
      fix Y yrhs
      assume h: "(Y, yrhs) \<in> ES \<and>  X \<noteq> Y" 
      show "invariant (Subst_all (ES - {(Y, yrhs)}) Y (Arden Y yrhs)) \<and>
             (\<exists>xrhs'. (X, xrhs') \<in> Subst_all (ES - {(Y, yrhs)}) Y (Arden Y yrhs)) \<and>
             card (Subst_all (ES - {(Y, yrhs)}) Y (Arden Y yrhs)) < card ES"
      proof -
        have "invariant (Subst_all (ES - {(Y, yrhs)}) Y (Arden Y yrhs))" 
        proof(rule Subst_all_satisfy_invariant)
          from h have "(Y, yrhs) \<in> ES" by simp
          hence "ES - {(Y, yrhs)} \<union> {(Y, yrhs)} = ES" by auto
          with Inv_ES show "invariant (ES - {(Y, yrhs)} \<union> {(Y, yrhs)})" by auto
        qed
        moreover have 
          "(\<exists>xrhs'. (X, xrhs') \<in> Subst_all (ES - {(Y, yrhs)}) Y (Arden Y yrhs))"
        proof(rule Subst_all_cls_remains)
          from X_in_ES and h
          show "(X, xrhs) \<in> ES - {(Y, yrhs)}" by auto
        qed
        moreover have 
          "card (Subst_all (ES - {(Y, yrhs)}) Y (Arden Y yrhs)) < card ES"
        proof(rule le_less_trans)
          show 
            "card (Subst_all (ES - {(Y, yrhs)}) Y (Arden Y yrhs)) \<le> 
                                                                card (ES - {(Y, yrhs)})"
          proof(rule Subst_all_card_le)
            show "finite (ES - {(Y, yrhs)})" using finite_ES by auto
          qed
        next
          show "card (ES - {(Y, yrhs)}) < card ES" using finite_ES h
            by (auto simp:card_gt_0_iff intro:diff_Suc_less)
        qed
        ultimately show ?thesis 
          by (auto dest: Subst_all_card_le elim:le_less_trans)
      qed
    qed
  qed
qed


subsubsection {* Conclusion of the proof *}

text {*
  From this point until @{text "hard_direction"}, the hard direction is proved
  through a simple application of the iteration principle.
*}

lemma reduce_x:
  assumes inv: "invariant ES"
  and contain_x: "(X, xrhs) \<in> ES" 
  shows "\<exists> xrhs'. reduce X ES = {(X, xrhs')} \<and> invariant(reduce X ES)"
proof -
  let ?Inv = "\<lambda> ES. (invariant ES \<and> (\<exists> xrhs. (X, xrhs) \<in> ES))"
  show ?thesis
  proof (unfold reduce_def, 
         rule while_rule [where P = ?Inv and r = "measure card"])
    from inv and contain_x show "?Inv ES" by auto
  next
    show "wf (measure card)" by simp
  next
    fix ES
    assume inv: "?Inv ES" and crd: "card ES \<noteq> 1"
    show "(iter X ES, ES) \<in> measure card"
    proof -
      from inv obtain xrhs where x_in: "(X, xrhs) \<in> ES" by auto
      from inv have "invariant ES" by simp
      from iteration_step [OF this x_in crd]
      show ?thesis by auto
    qed
  next
    fix ES
    assume inv: "?Inv ES" and crd: "card ES \<noteq> 1"
    thus "?Inv (iter X ES)" 
    proof -
      from inv obtain xrhs where x_in: "(X, xrhs) \<in> ES" by auto
      from inv have "invariant ES" by simp
      from iteration_step [OF this x_in crd]
      show ?thesis by auto
    qed
  next
    fix ES
    assume "?Inv ES" and "\<not> card ES \<noteq> 1"
    thus "\<exists>xrhs'. ES = {(X, xrhs')} \<and> invariant ES"
      apply (auto, rule_tac x = xrhs in exI)
      by (auto simp: invariant_def dest!:card_Suc_Diff1 simp:card_eq_0_iff) 
  qed
qed

lemma last_cl_exists_rexp:
  assumes Inv_ES: "invariant {(X, xrhs)}"
  shows "\<exists> (r::rexp). L r = X" (is "\<exists> r. ?P r")
proof-
  def A \<equiv> "Arden X xrhs"
  have "?P (\<Uplus>{r. Lam r \<in> A})"
  proof -
    have "L (\<Uplus>{r. Lam r \<in> A}) = L ({Lam r | r. Lam r \<in>  A})"
    proof(rule rexp_of_lam_eq_lam_set)
      show "finite A" 
	unfolding A_def
	using Inv_ES
        by (rule_tac Arden_keeps_finite) 
           (auto simp add: invariant_def finite_rhs_def)
    qed
    also have "\<dots> = L A"
    proof-
      have "{Lam r | r. Lam r \<in> A} = A"
      proof-
        have "classes_of A = {}" using Inv_ES 
	  unfolding A_def
          by (simp add:Arden_removes_cl 
                       self_contained_def invariant_def lefts_of_def) 
        thus ?thesis
	  unfolding A_def
          by (auto simp only: classes_of_def, case_tac x, auto)
      qed
      thus ?thesis by simp
    qed
    also have "\<dots> = X"
    unfolding A_def
    proof(rule Arden_keeps_eq [THEN sym])
      show "X = L xrhs" using Inv_ES 
        by (auto simp only: invariant_def valid_eqns_def)  
    next
      from Inv_ES show "[] \<notin> L (\<Uplus>{r. Trn X r \<in> xrhs})"
        by(simp add: invariant_def ardenable_def rexp_of_empty finite_rhs_def)
    next
      from Inv_ES show "finite xrhs" 
        by (simp add: invariant_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 -
  let ?ES = " eqs (UNIV // \<approx>Lang)"
  from X_in_CS 
  obtain xrhs where "(X, xrhs) \<in> ?ES"
    by (auto simp:eqs_def init_rhs_def)
  from reduce_x [OF init_ES_satisfy_invariant [OF finite_CS] this]
  have "\<exists>xrhs'. reduce X ?ES = {(X, xrhs')} \<and> invariant (reduce X ?ES)" .
  then obtain xrhs' where "invariant {(X, xrhs')}" by auto
  from last_cl_exists_rexp [OF this]
  show ?thesis .
qed


theorem hard_direction: 
  assumes finite_CS: "finite (UNIV // \<approx>A)"
  shows   "\<exists>r::rexp. A = L r"
proof -
  have "\<forall> X \<in> (UNIV // \<approx>A). \<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>A). X = L ((f X)::rexp)"
    by (auto dest: bchoice)
  def rs \<equiv> "f ` (finals A)"  
  have "A = \<Union> (finals A)" using lang_is_union_of_finals by auto
  also have "\<dots> = L (\<Uplus>rs)" 
  proof -
    have "finite rs"
    proof -
      have "finite (finals A)" 
        using finite_CS finals_in_partitions[of "A"]   
        by (erule_tac finite_subset, simp)
      thus ?thesis using rs_def by auto
    qed
    thus ?thesis 
      using f_prop rs_def finals_in_partitions[of "A"] by auto
  qed
  finally show ?thesis by blast
qed 

end