
wenzelm@32960: (*  Title:      HOL/Wellfounded.thy
wenzelm@32960:     Author:     Tobias Nipkow
wenzelm@32960:     Author:     Lawrence C Paulson
wenzelm@32960:     Author:     Konrad Slind
wenzelm@32960:     Author:     Alexander Krauss
blanchet@55027:     Author:     Andrei Popescu, TU Muenchen
krauss@26748: *)
krauss@26748: 
wenzelm@60758: section \<open>Well-founded Recursion\<close>
krauss@26748: 
krauss@26748: theory Wellfounded
haftmann@35727: imports Transitive_Closure
krauss@26748: begin
krauss@26748: 
wenzelm@60758: subsection \<open>Basic Definitions\<close>
krauss@26976: 
krauss@33217: definition wf :: "('a * 'a) set => bool" where
haftmann@45137:   "wf r \<longleftrightarrow> (!P. (!x. (!y. (y,x):r --> P(y)) --> P(x)) --> (!x. P(x)))"
krauss@26748: 
krauss@33217: definition wfP :: "('a => 'a => bool) => bool" where
haftmann@45137:   "wfP r \<longleftrightarrow> wf {(x, y). r x y}"
krauss@26748: 
krauss@26748: lemma wfP_wf_eq [pred_set_conv]: "wfP (\<lambda>x y. (x, y) \<in> r) = wf r"
krauss@26748:   by (simp add: wfP_def)
krauss@26748: 
krauss@26748: lemma wfUNIVI: 
krauss@26748:    "(!!P x. (ALL x. (ALL y. (y,x) : r --> P(y)) --> P(x)) ==> P(x)) ==> wf(r)"
krauss@26748:   unfolding wf_def by blast
krauss@26748: 
krauss@26748: lemmas wfPUNIVI = wfUNIVI [to_pred]
krauss@26748: 
wenzelm@60758: text\<open>Restriction to domain @{term A} and range @{term B}.  If @{term r} is
wenzelm@60758:     well-founded over their intersection, then @{term "wf r"}\<close>
krauss@26748: lemma wfI: 
krauss@26748:  "[| r \<subseteq> A <*> B; 
krauss@26748:      !!x P. [|\<forall>x. (\<forall>y. (y,x) : r --> P y) --> P x;  x : A; x : B |] ==> P x |]
krauss@26748:   ==>  wf r"
krauss@26748:   unfolding wf_def by blast
krauss@26748: 
krauss@26748: lemma wf_induct: 
krauss@26748:     "[| wf(r);           
krauss@26748:         !!x.[| ALL y. (y,x): r --> P(y) |] ==> P(x)  
krauss@26748:      |]  ==>  P(a)"
krauss@26748:   unfolding wf_def by blast
krauss@26748: 
krauss@26748: lemmas wfP_induct = wf_induct [to_pred]
krauss@26748: 
krauss@26748: lemmas wf_induct_rule = wf_induct [rule_format, consumes 1, case_names less, induct set: wf]
krauss@26748: 
krauss@26748: lemmas wfP_induct_rule = wf_induct_rule [to_pred, induct set: wfP]
krauss@26748: 
krauss@26748: lemma wf_not_sym: "wf r ==> (a, x) : r ==> (x, a) ~: r"
krauss@26748:   by (induct a arbitrary: x set: wf) blast
krauss@26748: 
krauss@33215: lemma wf_asym:
krauss@33215:   assumes "wf r" "(a, x) \<in> r"
krauss@33215:   obtains "(x, a) \<notin> r"
krauss@33215:   by (drule wf_not_sym[OF assms])
krauss@26748: 
krauss@26748: lemma wf_not_refl [simp]: "wf r ==> (a, a) ~: r"
krauss@26748:   by (blast elim: wf_asym)
krauss@26748: 
krauss@33215: lemma wf_irrefl: assumes "wf r" obtains "(a, a) \<notin> r"
krauss@33215: by (drule wf_not_refl[OF assms])
krauss@26748: 
haftmann@27823: lemma wf_wellorderI:
haftmann@27823:   assumes wf: "wf {(x::'a::ord, y). x < y}"
haftmann@27823:   assumes lin: "OFCLASS('a::ord, linorder_class)"
haftmann@27823:   shows "OFCLASS('a::ord, wellorder_class)"
haftmann@27823: using lin by (rule wellorder_class.intro)
haftmann@36635:   (blast intro: class.wellorder_axioms.intro wf_induct_rule [OF wf])
haftmann@27823: 
haftmann@27823: lemma (in wellorder) wf:
haftmann@27823:   "wf {(x, y). x < y}"
haftmann@27823: unfolding wf_def by (blast intro: less_induct)
haftmann@27823: 
haftmann@27823: 
wenzelm@60758: subsection \<open>Basic Results\<close>
krauss@26976: 
wenzelm@60758: text \<open>Point-free characterization of well-foundedness\<close>
krauss@33216: 
krauss@33216: lemma wfE_pf:
krauss@33216:   assumes wf: "wf R"
krauss@33216:   assumes a: "A \<subseteq> R `` A"
krauss@33216:   shows "A = {}"
krauss@33216: proof -
krauss@33216:   { fix x
krauss@33216:     from wf have "x \<notin> A"
krauss@33216:     proof induct
krauss@33216:       fix x assume "\<And>y. (y, x) \<in> R \<Longrightarrow> y \<notin> A"
krauss@33216:       then have "x \<notin> R `` A" by blast
krauss@33216:       with a show "x \<notin> A" by blast
krauss@33216:     qed
krauss@33216:   } thus ?thesis by auto
krauss@33216: qed
krauss@33216: 
krauss@33216: lemma wfI_pf:
krauss@33216:   assumes a: "\<And>A. A \<subseteq> R `` A \<Longrightarrow> A = {}"
krauss@33216:   shows "wf R"
krauss@33216: proof (rule wfUNIVI)
krauss@33216:   fix P :: "'a \<Rightarrow> bool" and x
krauss@33216:   let ?A = "{x. \<not> P x}"
krauss@33216:   assume "\<forall>x. (\<forall>y. (y, x) \<in> R \<longrightarrow> P y) \<longrightarrow> P x"
krauss@33216:   then have "?A \<subseteq> R `` ?A" by blast
krauss@33216:   with a show "P x" by blast
krauss@33216: qed
krauss@33216: 
wenzelm@60758: text\<open>Minimal-element characterization of well-foundedness\<close>
krauss@33216: 
krauss@33216: lemma wfE_min:
krauss@33216:   assumes wf: "wf R" and Q: "x \<in> Q"
krauss@33216:   obtains z where "z \<in> Q" "\<And>y. (y, z) \<in> R \<Longrightarrow> y \<notin> Q"
krauss@33216:   using Q wfE_pf[OF wf, of Q] by blast
krauss@33216: 
krauss@33216: lemma wfI_min:
krauss@33216:   assumes a: "\<And>x Q. x \<in> Q \<Longrightarrow> \<exists>z\<in>Q. \<forall>y. (y, z) \<in> R \<longrightarrow> y \<notin> Q"
krauss@33216:   shows "wf R"
krauss@33216: proof (rule wfI_pf)
krauss@33216:   fix A assume b: "A \<subseteq> R `` A"
krauss@33216:   { fix x assume "x \<in> A"
krauss@33216:     from a[OF this] b have "False" by blast
krauss@33216:   }
krauss@33216:   thus "A = {}" by blast
krauss@33216: qed
krauss@33216: 
krauss@33216: lemma wf_eq_minimal: "wf r = (\<forall>Q x. x\<in>Q --> (\<exists>z\<in>Q. \<forall>y. (y,z)\<in>r --> y\<notin>Q))"
krauss@33216: apply auto
krauss@33216: apply (erule wfE_min, assumption, blast)
krauss@33216: apply (rule wfI_min, auto)
krauss@33216: done
krauss@33216: 
krauss@33216: lemmas wfP_eq_minimal = wf_eq_minimal [to_pred]
krauss@33216: 
wenzelm@60758: text\<open>Well-foundedness of transitive closure\<close>
krauss@33216: 
krauss@26748: lemma wf_trancl:
krauss@26748:   assumes "wf r"
krauss@26748:   shows "wf (r^+)"
krauss@26748: proof -
krauss@26748:   {
krauss@26748:     fix P and x
krauss@26748:     assume induct_step: "!!x. (!!y. (y, x) : r^+ ==> P y) ==> P x"
krauss@26748:     have "P x"
krauss@26748:     proof (rule induct_step)
krauss@26748:       fix y assume "(y, x) : r^+"
wenzelm@60758:       with \<open>wf r\<close> show "P y"
krauss@26748:       proof (induct x arbitrary: y)
wenzelm@32960:         case (less x)
wenzelm@60758:         note hyp = \<open>\<And>x' y'. (x', x) : r ==> (y', x') : r^+ ==> P y'\<close>
wenzelm@60758:         from \<open>(y, x) : r^+\<close> show "P y"
wenzelm@32960:         proof cases
wenzelm@32960:           case base
wenzelm@32960:           show "P y"
wenzelm@32960:           proof (rule induct_step)
wenzelm@32960:             fix y' assume "(y', y) : r^+"
wenzelm@60758:             with \<open>(y, x) : r\<close> show "P y'" by (rule hyp [of y y'])
wenzelm@32960:           qed
wenzelm@32960:         next
wenzelm@32960:           case step
wenzelm@32960:           then obtain x' where "(x', x) : r" and "(y, x') : r^+" by simp
wenzelm@32960:           then show "P y" by (rule hyp [of x' y])
wenzelm@32960:         qed
krauss@26748:       qed
krauss@26748:     qed
krauss@26748:   } then show ?thesis unfolding wf_def by blast
krauss@26748: qed
krauss@26748: 
krauss@26748: lemmas wfP_trancl = wf_trancl [to_pred]
krauss@26748: 
krauss@26748: lemma wf_converse_trancl: "wf (r^-1) ==> wf ((r^+)^-1)"
krauss@26748:   apply (subst trancl_converse [symmetric])
krauss@26748:   apply (erule wf_trancl)
krauss@26748:   done
krauss@26748: 
wenzelm@60758: text \<open>Well-foundedness of subsets\<close>
krauss@26748: 
krauss@26748: lemma wf_subset: "[| wf(r);  p<=r |] ==> wf(p)"
krauss@26748:   apply (simp (no_asm_use) add: wf_eq_minimal)
krauss@26748:   apply fast
krauss@26748:   done
krauss@26748: 
krauss@26748: lemmas wfP_subset = wf_subset [to_pred]
krauss@26748: 
wenzelm@60758: text \<open>Well-foundedness of the empty relation\<close>
krauss@33216: 
krauss@33216: lemma wf_empty [iff]: "wf {}"
krauss@26748:   by (simp add: wf_def)
krauss@26748: 
haftmann@32205: lemma wfP_empty [iff]:
haftmann@32205:   "wfP (\<lambda>x y. False)"
haftmann@32205: proof -
haftmann@32205:   have "wfP bot" by (fact wf_empty [to_pred bot_empty_eq2])
huffman@44921:   then show ?thesis by (simp add: bot_fun_def)
haftmann@32205: qed
krauss@26748: 
krauss@26748: lemma wf_Int1: "wf r ==> wf (r Int r')"
krauss@26748:   apply (erule wf_subset)
krauss@26748:   apply (rule Int_lower1)
krauss@26748:   done
krauss@26748: 
krauss@26748: lemma wf_Int2: "wf r ==> wf (r' Int r)"
krauss@26748:   apply (erule wf_subset)
krauss@26748:   apply (rule Int_lower2)
krauss@26748:   done  
krauss@26748: 
wenzelm@60758: text \<open>Exponentiation\<close>
krauss@33216: 
krauss@33216: lemma wf_exp:
krauss@33216:   assumes "wf (R ^^ n)"
krauss@33216:   shows "wf R"
krauss@33216: proof (rule wfI_pf)
krauss@33216:   fix A assume "A \<subseteq> R `` A"
krauss@33216:   then have "A \<subseteq> (R ^^ n) `` A" by (induct n) force+
wenzelm@60758:   with \<open>wf (R ^^ n)\<close>
krauss@33216:   show "A = {}" by (rule wfE_pf)
krauss@33216: qed
krauss@33216: 
wenzelm@60758: text \<open>Well-foundedness of insert\<close>
krauss@33216: 
krauss@26748: lemma wf_insert [iff]: "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)"
krauss@26748: apply (rule iffI)
krauss@26748:  apply (blast elim: wf_trancl [THEN wf_irrefl]
krauss@26748:               intro: rtrancl_into_trancl1 wf_subset 
krauss@26748:                      rtrancl_mono [THEN [2] rev_subsetD])
krauss@26748: apply (simp add: wf_eq_minimal, safe)
krauss@26748: apply (rule allE, assumption, erule impE, blast) 
krauss@26748: apply (erule bexE)
krauss@26748: apply (rename_tac "a", case_tac "a = x")
krauss@26748:  prefer 2
krauss@26748: apply blast 
krauss@26748: apply (case_tac "y:Q")
krauss@26748:  prefer 2 apply blast
krauss@26748: apply (rule_tac x = "{z. z:Q & (z,y) : r^*}" in allE)
krauss@26748:  apply assumption
wenzelm@59807: apply (erule_tac V = "ALL Q. (EX x. x : Q) --> P Q" for P in thin_rl) 
wenzelm@60758:   --\<open>essential for speed\<close>
wenzelm@60758: txt\<open>Blast with new substOccur fails\<close>
krauss@26748: apply (fast intro: converse_rtrancl_into_rtrancl)
krauss@26748: done
krauss@26748: 
wenzelm@60758: text\<open>Well-foundedness of image\<close>
krauss@33216: 
blanchet@55932: lemma wf_map_prod_image: "[| wf r; inj f |] ==> wf (map_prod f f ` r)"
krauss@26748: apply (simp only: wf_eq_minimal, clarify)
krauss@26748: apply (case_tac "EX p. f p : Q")
krauss@26748: apply (erule_tac x = "{p. f p : Q}" in allE)
krauss@26748: apply (fast dest: inj_onD, blast)
krauss@26748: done
krauss@26748: 
krauss@26748: 
wenzelm@60758: subsection \<open>Well-Foundedness Results for Unions\<close>
krauss@26748: 
krauss@26748: lemma wf_union_compatible:
krauss@26748:   assumes "wf R" "wf S"
krauss@32235:   assumes "R O S \<subseteq> R"
krauss@26748:   shows "wf (R \<union> S)"
krauss@26748: proof (rule wfI_min)
krauss@26748:   fix x :: 'a and Q 
krauss@26748:   let ?Q' = "{x \<in> Q. \<forall>y. (y, x) \<in> R \<longrightarrow> y \<notin> Q}"
krauss@26748:   assume "x \<in> Q"
krauss@26748:   obtain a where "a \<in> ?Q'"
wenzelm@60758:     by (rule wfE_min [OF \<open>wf R\<close> \<open>x \<in> Q\<close>]) blast
wenzelm@60758:   with \<open>wf S\<close>
krauss@26748:   obtain z where "z \<in> ?Q'" and zmin: "\<And>y. (y, z) \<in> S \<Longrightarrow> y \<notin> ?Q'" by (erule wfE_min)
krauss@26748:   { 
krauss@26748:     fix y assume "(y, z) \<in> S"
krauss@26748:     then have "y \<notin> ?Q'" by (rule zmin)
krauss@26748: 
krauss@26748:     have "y \<notin> Q"
krauss@26748:     proof 
krauss@26748:       assume "y \<in> Q"
wenzelm@60758:       with \<open>y \<notin> ?Q'\<close> 
krauss@26748:       obtain w where "(w, y) \<in> R" and "w \<in> Q" by auto
wenzelm@60758:       from \<open>(w, y) \<in> R\<close> \<open>(y, z) \<in> S\<close> have "(w, z) \<in> R O S" by (rule relcompI)
wenzelm@60758:       with \<open>R O S \<subseteq> R\<close> have "(w, z) \<in> R" ..
wenzelm@60758:       with \<open>z \<in> ?Q'\<close> have "w \<notin> Q" by blast 
wenzelm@60758:       with \<open>w \<in> Q\<close> show False by contradiction
krauss@26748:     qed
krauss@26748:   }
wenzelm@60758:   with \<open>z \<in> ?Q'\<close> show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> R \<union> S \<longrightarrow> y \<notin> Q" by blast
krauss@26748: qed
krauss@26748: 
krauss@26748: 
wenzelm@60758: text \<open>Well-foundedness of indexed union with disjoint domains and ranges\<close>
krauss@26748: 
krauss@26748: lemma wf_UN: "[| ALL i:I. wf(r i);  
krauss@26748:          ALL i:I. ALL j:I. r i ~= r j --> Domain(r i) Int Range(r j) = {}  
krauss@26748:       |] ==> wf(UN i:I. r i)"
krauss@26748: apply (simp only: wf_eq_minimal, clarify)
krauss@26748: apply (rename_tac A a, case_tac "EX i:I. EX a:A. EX b:A. (b,a) : r i")
krauss@26748:  prefer 2
krauss@26748:  apply force 
krauss@26748: apply clarify
krauss@26748: apply (drule bspec, assumption)  
krauss@26748: apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r i) }" in allE)
krauss@26748: apply (blast elim!: allE)  
krauss@26748: done
krauss@26748: 
haftmann@32263: lemma wfP_SUP:
haftmann@56218:   "\<forall>i. wfP (r i) \<Longrightarrow> \<forall>i j. r i \<noteq> r j \<longrightarrow> inf (DomainP (r i)) (RangeP (r j)) = bot \<Longrightarrow> wfP (SUPREMUM UNIV r)"
noschinl@46883:   apply (rule wf_UN[to_pred])
noschinl@46882:   apply simp_all
haftmann@45970:   done
krauss@26748: 
krauss@26748: lemma wf_Union: 
krauss@26748:  "[| ALL r:R. wf r;  
krauss@26748:      ALL r:R. ALL s:R. r ~= s --> Domain r Int Range s = {}  
krauss@26748:   |] ==> wf(Union R)"
haftmann@56166:   using wf_UN[of R "\<lambda>i. i"] by simp
krauss@26748: 
krauss@26748: (*Intuition: we find an (R u S)-min element of a nonempty subset A
krauss@26748:              by case distinction.
krauss@26748:   1. There is a step a -R-> b with a,b : A.
krauss@26748:      Pick an R-min element z of the (nonempty) set {a:A | EX b:A. a -R-> b}.
krauss@26748:      By definition, there is z':A s.t. z -R-> z'. Because z is R-min in the
krauss@26748:      subset, z' must be R-min in A. Because z' has an R-predecessor, it cannot
krauss@26748:      have an S-successor and is thus S-min in A as well.
krauss@26748:   2. There is no such step.
krauss@26748:      Pick an S-min element of A. In this case it must be an R-min
krauss@26748:      element of A as well.
krauss@26748: *)
krauss@26748: lemma wf_Un:
krauss@26748:      "[| wf r; wf s; Domain r Int Range s = {} |] ==> wf(r Un s)"
krauss@26748:   using wf_union_compatible[of s r] 
krauss@26748:   by (auto simp: Un_ac)
krauss@26748: 
krauss@26748: lemma wf_union_merge: 
krauss@32235:   "wf (R \<union> S) = wf (R O R \<union> S O R \<union> S)" (is "wf ?A = wf ?B")
krauss@26748: proof
krauss@26748:   assume "wf ?A"
krauss@26748:   with wf_trancl have wfT: "wf (?A^+)" .
krauss@26748:   moreover have "?B \<subseteq> ?A^+"
krauss@26748:     by (subst trancl_unfold, subst trancl_unfold) blast
krauss@26748:   ultimately show "wf ?B" by (rule wf_subset)
krauss@26748: next
krauss@26748:   assume "wf ?B"
krauss@26748: 
krauss@26748:   show "wf ?A"
krauss@26748:   proof (rule wfI_min)
krauss@26748:     fix Q :: "'a set" and x 
krauss@26748:     assume "x \<in> Q"
krauss@26748: 
wenzelm@60758:     with \<open>wf ?B\<close>
krauss@26748:     obtain z where "z \<in> Q" and "\<And>y. (y, z) \<in> ?B \<Longrightarrow> y \<notin> Q" 
krauss@26748:       by (erule wfE_min)
krauss@26748:     then have A1: "\<And>y. (y, z) \<in> R O R \<Longrightarrow> y \<notin> Q"
krauss@32235:       and A2: "\<And>y. (y, z) \<in> S O R \<Longrightarrow> y \<notin> Q"
krauss@26748:       and A3: "\<And>y. (y, z) \<in> S \<Longrightarrow> y \<notin> Q"
krauss@26748:       by auto
krauss@26748:     
krauss@26748:     show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> ?A \<longrightarrow> y \<notin> Q"
krauss@26748:     proof (cases "\<forall>y. (y, z) \<in> R \<longrightarrow> y \<notin> Q")
krauss@26748:       case True
wenzelm@60758:       with \<open>z \<in> Q\<close> A3 show ?thesis by blast
krauss@26748:     next
krauss@26748:       case False 
krauss@26748:       then obtain z' where "z'\<in>Q" "(z', z) \<in> R" by blast
krauss@26748: 
krauss@26748:       have "\<forall>y. (y, z') \<in> ?A \<longrightarrow> y \<notin> Q"
krauss@26748:       proof (intro allI impI)
krauss@26748:         fix y assume "(y, z') \<in> ?A"
krauss@26748:         then show "y \<notin> Q"
krauss@26748:         proof
krauss@26748:           assume "(y, z') \<in> R" 
wenzelm@60758:           then have "(y, z) \<in> R O R" using \<open>(z', z) \<in> R\<close> ..
krauss@26748:           with A1 show "y \<notin> Q" .
krauss@26748:         next
krauss@26748:           assume "(y, z') \<in> S" 
wenzelm@60758:           then have "(y, z) \<in> S O R" using  \<open>(z', z) \<in> R\<close> ..
krauss@26748:           with A2 show "y \<notin> Q" .
krauss@26748:         qed
krauss@26748:       qed
wenzelm@60758:       with \<open>z' \<in> Q\<close> show ?thesis ..
krauss@26748:     qed
krauss@26748:   qed
krauss@26748: qed
krauss@26748: 
wenzelm@60758: lemma wf_comp_self: "wf R = wf (R O R)"  -- \<open>special case\<close>
krauss@26748:   by (rule wf_union_merge [where S = "{}", simplified])
krauss@26748: 
krauss@26748: 
wenzelm@60758: subsection \<open>Well-Foundedness of Composition\<close>
nipkow@60148: 
lp15@60493: text \<open>Bachmair and Dershowitz 1986, Lemma 2. [Provided by Tjark Weber]\<close>
nipkow@60148: 
lp15@60493: lemma qc_wf_relto_iff:
lp15@60493:   assumes "R O S \<subseteq> (R \<union> S)\<^sup>* O R" -- \<open>R quasi-commutes over S\<close>
lp15@60493:   shows "wf (S\<^sup>* O R O S\<^sup>*) \<longleftrightarrow> wf R" (is "wf ?S \<longleftrightarrow> _")
lp15@60493: proof
lp15@60493:   assume "wf ?S"
lp15@60493:   moreover have "R \<subseteq> ?S" by auto
lp15@60493:   ultimately show "wf R" using wf_subset by auto
lp15@60493: next
lp15@60493:   assume "wf R"
lp15@60493:   show "wf ?S"
lp15@60493:   proof (rule wfI_pf)
lp15@60493:     fix A assume A: "A \<subseteq> ?S `` A"
lp15@60493:     let ?X = "(R \<union> S)\<^sup>* `` A"
lp15@60493:     have *: "R O (R \<union> S)\<^sup>* \<subseteq> (R \<union> S)\<^sup>* O R"
lp15@60493:       proof -
lp15@60493:         { fix x y z assume "(y, z) \<in> (R \<union> S)\<^sup>*" and "(x, y) \<in> R"
lp15@60493:           then have "(x, z) \<in> (R \<union> S)\<^sup>* O R"
lp15@60493:           proof (induct y z)
lp15@60493:             case rtrancl_refl then show ?case by auto
lp15@60493:           next
lp15@60493:             case (rtrancl_into_rtrancl a b c)
lp15@60493:             then have "(x, c) \<in> ((R \<union> S)\<^sup>* O (R \<union> S)\<^sup>*) O R" using assms by blast
lp15@60493:             then show ?case by simp
lp15@60493:           qed }
lp15@60493:         then show ?thesis by auto
lp15@60493:       qed
lp15@60493:     then have "R O S\<^sup>* \<subseteq> (R \<union> S)\<^sup>* O R" using rtrancl_Un_subset by blast
lp15@60493:     then have "?S \<subseteq> (R \<union> S)\<^sup>* O (R \<union> S)\<^sup>* O R" by (simp add: relcomp_mono rtrancl_mono)
lp15@60493:     also have "\<dots> = (R \<union> S)\<^sup>* O R" by (simp add: O_assoc[symmetric])
lp15@60493:     finally have "?S O (R \<union> S)\<^sup>* \<subseteq> (R \<union> S)\<^sup>* O R O (R \<union> S)\<^sup>*" by (simp add: O_assoc[symmetric] relcomp_mono)
lp15@60493:     also have "\<dots> \<subseteq> (R \<union> S)\<^sup>* O (R \<union> S)\<^sup>* O R" using * by (simp add: relcomp_mono)
lp15@60493:     finally have "?S O (R \<union> S)\<^sup>* \<subseteq> (R \<union> S)\<^sup>* O R" by (simp add: O_assoc[symmetric])
lp15@60493:     then have "(?S O (R \<union> S)\<^sup>*) `` A \<subseteq> ((R \<union> S)\<^sup>* O R) `` A" by (simp add: Image_mono)
lp15@60493:     moreover have "?X \<subseteq> (?S O (R \<union> S)\<^sup>*) `` A" using A by (auto simp: relcomp_Image)
lp15@60493:     ultimately have "?X \<subseteq> R `` ?X" by (auto simp: relcomp_Image)
lp15@60493:     then have "?X = {}" using \<open>wf R\<close> by (simp add: wfE_pf)
lp15@60493:     moreover have "A \<subseteq> ?X" by auto
lp15@60493:     ultimately show "A = {}" by simp
lp15@60493:   qed
lp15@60493: qed
lp15@60493: 
lp15@60493: corollary wf_relcomp_compatible:
nipkow@60148:   assumes "wf R" and "R O S \<subseteq> S O R"
nipkow@60148:   shows "wf (S O R)"
lp15@60493: proof -
lp15@60493:   have "R O S \<subseteq> (R \<union> S)\<^sup>* O R"
lp15@60493:     using assms by blast
lp15@60493:   then have "wf (S\<^sup>* O R O S\<^sup>*)"
lp15@60493:     by (simp add: assms qc_wf_relto_iff)
lp15@60493:   then show ?thesis
lp15@60493:     by (rule Wellfounded.wf_subset) blast
nipkow@60148: qed
nipkow@60148: 
nipkow@60148: 
wenzelm@60758: subsection \<open>Acyclic relations\<close>
krauss@33217: 
krauss@26748: lemma wf_acyclic: "wf r ==> acyclic r"
krauss@26748: apply (simp add: acyclic_def)
krauss@26748: apply (blast elim: wf_trancl [THEN wf_irrefl])
krauss@26748: done
krauss@26748: 
krauss@26748: lemmas wfP_acyclicP = wf_acyclic [to_pred]
krauss@26748: 
wenzelm@60758: text\<open>Wellfoundedness of finite acyclic relations\<close>
krauss@26748: 
krauss@26748: lemma finite_acyclic_wf [rule_format]: "finite r ==> acyclic r --> wf r"
krauss@26748: apply (erule finite_induct, blast)
krauss@26748: apply (simp (no_asm_simp) only: split_tupled_all)
krauss@26748: apply simp
krauss@26748: done
krauss@26748: 
krauss@26748: lemma finite_acyclic_wf_converse: "[|finite r; acyclic r|] ==> wf (r^-1)"
krauss@26748: apply (erule finite_converse [THEN iffD2, THEN finite_acyclic_wf])
krauss@26748: apply (erule acyclic_converse [THEN iffD2])
krauss@26748: done
krauss@26748: 
krauss@26748: lemma wf_iff_acyclic_if_finite: "finite r ==> wf r = acyclic r"
krauss@26748: by (blast intro: finite_acyclic_wf wf_acyclic)
krauss@26748: 
krauss@26748: 
wenzelm@60758: subsection \<open>@{typ nat} is well-founded\<close>
krauss@26748: 
krauss@26748: lemma less_nat_rel: "op < = (\<lambda>m n. n = Suc m)^++"
krauss@26748: proof (rule ext, rule ext, rule iffI)
krauss@26748:   fix n m :: nat
krauss@26748:   assume "m < n"
krauss@26748:   then show "(\<lambda>m n. n = Suc m)^++ m n"
krauss@26748:   proof (induct n)
krauss@26748:     case 0 then show ?case by auto
krauss@26748:   next
krauss@26748:     case (Suc n) then show ?case
krauss@26748:       by (auto simp add: less_Suc_eq_le le_less intro: tranclp.trancl_into_trancl)
krauss@26748:   qed
krauss@26748: next
krauss@26748:   fix n m :: nat
krauss@26748:   assume "(\<lambda>m n. n = Suc m)^++ m n"
krauss@26748:   then show "m < n"
krauss@26748:     by (induct n)
krauss@26748:       (simp_all add: less_Suc_eq_le reflexive le_less)
krauss@26748: qed
krauss@26748: 
krauss@26748: definition
krauss@26748:   pred_nat :: "(nat * nat) set" where
krauss@26748:   "pred_nat = {(m, n). n = Suc m}"
krauss@26748: 
krauss@26748: definition
krauss@26748:   less_than :: "(nat * nat) set" where
krauss@26748:   "less_than = pred_nat^+"
krauss@26748: 
krauss@26748: lemma less_eq: "(m, n) \<in> pred_nat^+ \<longleftrightarrow> m < n"
krauss@26748:   unfolding less_nat_rel pred_nat_def trancl_def by simp
krauss@26748: 
krauss@26748: lemma pred_nat_trancl_eq_le:
krauss@26748:   "(m, n) \<in> pred_nat^* \<longleftrightarrow> m \<le> n"
krauss@26748:   unfolding less_eq rtrancl_eq_or_trancl by auto
krauss@26748: 
krauss@26748: lemma wf_pred_nat: "wf pred_nat"
krauss@26748:   apply (unfold wf_def pred_nat_def, clarify)
krauss@26748:   apply (induct_tac x, blast+)
krauss@26748:   done
krauss@26748: 
krauss@26748: lemma wf_less_than [iff]: "wf less_than"
krauss@26748:   by (simp add: less_than_def wf_pred_nat [THEN wf_trancl])
krauss@26748: 
krauss@26748: lemma trans_less_than [iff]: "trans less_than"
huffman@35216:   by (simp add: less_than_def)
krauss@26748: 
krauss@26748: lemma less_than_iff [iff]: "((x,y): less_than) = (x<y)"
krauss@26748:   by (simp add: less_than_def less_eq)
krauss@26748: 
krauss@26748: lemma wf_less: "wf {(x, y::nat). x < y}"
lp15@60493:   by (rule Wellfounded.wellorder_class.wf)
krauss@26748: 
krauss@26748: 
wenzelm@60758: subsection \<open>Accessible Part\<close>
krauss@26748: 
wenzelm@60758: text \<open>
krauss@26748:  Inductive definition of the accessible part @{term "acc r"} of a
wenzelm@58623:  relation; see also @{cite "paulin-tlca"}.
wenzelm@60758: \<close>
krauss@26748: 
krauss@26748: inductive_set
krauss@26748:   acc :: "('a * 'a) set => 'a set"
krauss@26748:   for r :: "('a * 'a) set"
krauss@26748:   where
krauss@26748:     accI: "(!!y. (y, x) : r ==> y : acc r) ==> x : acc r"
krauss@26748: 
krauss@26748: abbreviation
krauss@26748:   termip :: "('a => 'a => bool) => 'a => bool" where
haftmann@45137:   "termip r \<equiv> accp (r\<inverse>\<inverse>)"
krauss@26748: 
krauss@26748: abbreviation
krauss@26748:   termi :: "('a * 'a) set => 'a set" where
haftmann@45137:   "termi r \<equiv> acc (r\<inverse>)"
krauss@26748: 
krauss@26748: lemmas accpI = accp.accI
krauss@26748: 
haftmann@54295: lemma accp_eq_acc [code]:
haftmann@54295:   "accp r = (\<lambda>x. x \<in> Wellfounded.acc {(x, y). r x y})"
haftmann@54295:   by (simp add: acc_def)
haftmann@54295: 
haftmann@54295: 
wenzelm@60758: text \<open>Induction rules\<close>
krauss@26748: 
krauss@26748: theorem accp_induct:
krauss@26748:   assumes major: "accp r a"
krauss@26748:   assumes hyp: "!!x. accp r x ==> \<forall>y. r y x --> P y ==> P x"
krauss@26748:   shows "P a"
krauss@26748:   apply (rule major [THEN accp.induct])
krauss@26748:   apply (rule hyp)
krauss@26748:    apply (rule accp.accI)
krauss@26748:    apply fast
krauss@26748:   apply fast
krauss@26748:   done
krauss@26748: 
krauss@26748: theorems accp_induct_rule = accp_induct [rule_format, induct set: accp]
krauss@26748: 
krauss@26748: theorem accp_downward: "accp r b ==> r a b ==> accp r a"
krauss@26748:   apply (erule accp.cases)
krauss@26748:   apply fast
krauss@26748:   done
krauss@26748: 
krauss@26748: lemma not_accp_down:
krauss@26748:   assumes na: "\<not> accp R x"
krauss@26748:   obtains z where "R z x" and "\<not> accp R z"
krauss@26748: proof -
krauss@26748:   assume a: "\<And>z. \<lbrakk>R z x; \<not> accp R z\<rbrakk> \<Longrightarrow> thesis"
krauss@26748: 
krauss@26748:   show thesis
krauss@26748:   proof (cases "\<forall>z. R z x \<longrightarrow> accp R z")
krauss@26748:     case True
krauss@26748:     hence "\<And>z. R z x \<Longrightarrow> accp R z" by auto
krauss@26748:     hence "accp R x"
krauss@26748:       by (rule accp.accI)
krauss@26748:     with na show thesis ..
krauss@26748:   next
krauss@26748:     case False then obtain z where "R z x" and "\<not> accp R z"
krauss@26748:       by auto
krauss@26748:     with a show thesis .
krauss@26748:   qed
krauss@26748: qed
krauss@26748: 
krauss@26748: lemma accp_downwards_aux: "r\<^sup>*\<^sup>* b a ==> accp r a --> accp r b"
krauss@26748:   apply (erule rtranclp_induct)
krauss@26748:    apply blast
krauss@26748:   apply (blast dest: accp_downward)
krauss@26748:   done
krauss@26748: 
krauss@26748: theorem accp_downwards: "accp r a ==> r\<^sup>*\<^sup>* b a ==> accp r b"
krauss@26748:   apply (blast dest: accp_downwards_aux)
krauss@26748:   done
krauss@26748: 
krauss@26748: theorem accp_wfPI: "\<forall>x. accp r x ==> wfP r"
krauss@26748:   apply (rule wfPUNIVI)
huffman@44921:   apply (rule_tac P=P in accp_induct)
krauss@26748:    apply blast
krauss@26748:   apply blast
krauss@26748:   done
krauss@26748: 
krauss@26748: theorem accp_wfPD: "wfP r ==> accp r x"
krauss@26748:   apply (erule wfP_induct_rule)
krauss@26748:   apply (rule accp.accI)
krauss@26748:   apply blast
krauss@26748:   done
krauss@26748: 
krauss@26748: theorem wfP_accp_iff: "wfP r = (\<forall>x. accp r x)"
krauss@26748:   apply (blast intro: accp_wfPI dest: accp_wfPD)
krauss@26748:   done
krauss@26748: 
krauss@26748: 
wenzelm@60758: text \<open>Smaller relations have bigger accessible parts:\<close>
krauss@26748: 
krauss@26748: lemma accp_subset:
krauss@26748:   assumes sub: "R1 \<le> R2"
krauss@26748:   shows "accp R2 \<le> accp R1"
berghofe@26803: proof (rule predicate1I)
krauss@26748:   fix x assume "accp R2 x"
krauss@26748:   then show "accp R1 x"
krauss@26748:   proof (induct x)
krauss@26748:     fix x
krauss@26748:     assume ih: "\<And>y. R2 y x \<Longrightarrow> accp R1 y"
krauss@26748:     with sub show "accp R1 x"
krauss@26748:       by (blast intro: accp.accI)
krauss@26748:   qed
krauss@26748: qed
krauss@26748: 
krauss@26748: 
wenzelm@60758: text \<open>This is a generalized induction theorem that works on
wenzelm@60758:   subsets of the accessible part.\<close>
krauss@26748: 
krauss@26748: lemma accp_subset_induct:
krauss@26748:   assumes subset: "D \<le> accp R"
krauss@26748:     and dcl: "\<And>x z. \<lbrakk>D x; R z x\<rbrakk> \<Longrightarrow> D z"
krauss@26748:     and "D x"
krauss@26748:     and istep: "\<And>x. \<lbrakk>D x; (\<And>z. R z x \<Longrightarrow> P z)\<rbrakk> \<Longrightarrow> P x"
krauss@26748:   shows "P x"
krauss@26748: proof -
wenzelm@60758:   from subset and \<open>D x\<close>
krauss@26748:   have "accp R x" ..
wenzelm@60758:   then show "P x" using \<open>D x\<close>
krauss@26748:   proof (induct x)
krauss@26748:     fix x
krauss@26748:     assume "D x"
krauss@26748:       and "\<And>y. R y x \<Longrightarrow> D y \<Longrightarrow> P y"
krauss@26748:     with dcl and istep show "P x" by blast
krauss@26748:   qed
krauss@26748: qed
krauss@26748: 
krauss@26748: 
wenzelm@60758: text \<open>Set versions of the above theorems\<close>
krauss@26748: 
krauss@26748: lemmas acc_induct = accp_induct [to_set]
krauss@26748: 
krauss@26748: lemmas acc_induct_rule = acc_induct [rule_format, induct set: acc]
krauss@26748: 
krauss@26748: lemmas acc_downward = accp_downward [to_set]
krauss@26748: 
krauss@26748: lemmas not_acc_down = not_accp_down [to_set]
krauss@26748: 
krauss@26748: lemmas acc_downwards_aux = accp_downwards_aux [to_set]
krauss@26748: 
krauss@26748: lemmas acc_downwards = accp_downwards [to_set]
krauss@26748: 
krauss@26748: lemmas acc_wfI = accp_wfPI [to_set]
krauss@26748: 
krauss@26748: lemmas acc_wfD = accp_wfPD [to_set]
krauss@26748: 
krauss@26748: lemmas wf_acc_iff = wfP_accp_iff [to_set]
krauss@26748: 
berghofe@46177: lemmas acc_subset = accp_subset [to_set]
krauss@26748: 
berghofe@46177: lemmas acc_subset_induct = accp_subset_induct [to_set]
krauss@26748: 
krauss@26748: 
wenzelm@60758: subsection \<open>Tools for building wellfounded relations\<close>
krauss@26748: 
wenzelm@60758: text \<open>Inverse Image\<close>
krauss@26748: 
krauss@26748: lemma wf_inv_image [simp,intro!]: "wf(r) ==> wf(inv_image r (f::'a=>'b))"
krauss@26748: apply (simp (no_asm_use) add: inv_image_def wf_eq_minimal)
krauss@26748: apply clarify
krauss@26748: apply (subgoal_tac "EX (w::'b) . w : {w. EX (x::'a) . x: Q & (f x = w) }")
krauss@26748: prefer 2 apply (blast del: allE)
krauss@26748: apply (erule allE)
krauss@26748: apply (erule (1) notE impE)
krauss@26748: apply blast
krauss@26748: done
krauss@26748: 
wenzelm@60758: text \<open>Measure functions into @{typ nat}\<close>
krauss@26748: 
krauss@26748: definition measure :: "('a => nat) => ('a * 'a)set"
haftmann@45137: where "measure = inv_image less_than"
krauss@26748: 
bulwahn@46356: lemma in_measure[simp, code_unfold]: "((x,y) : measure f) = (f x < f y)"
krauss@26748:   by (simp add:measure_def)
krauss@26748: 
krauss@26748: lemma wf_measure [iff]: "wf (measure f)"
krauss@26748: apply (unfold measure_def)
krauss@26748: apply (rule wf_less_than [THEN wf_inv_image])
krauss@26748: done
krauss@26748: 
nipkow@41720: lemma wf_if_measure: fixes f :: "'a \<Rightarrow> nat"
nipkow@41720: shows "(!!x. P x \<Longrightarrow> f(g x) < f x) \<Longrightarrow> wf {(y,x). P x \<and> y = g x}"
nipkow@41720: apply(insert wf_measure[of f])
nipkow@41720: apply(simp only: measure_def inv_image_def less_than_def less_eq)
nipkow@41720: apply(erule wf_subset)
nipkow@41720: apply auto
nipkow@41720: done
nipkow@41720: 
nipkow@41720: 
wenzelm@60758: text\<open>Lexicographic combinations\<close>
krauss@26748: 
haftmann@37767: definition lex_prod :: "('a \<times>'a) set \<Rightarrow> ('b \<times> 'b) set \<Rightarrow> (('a \<times> 'b) \<times> ('a \<times> 'b)) set" (infixr "<*lex*>" 80) where
haftmann@37767:   "ra <*lex*> rb = {((a, b), (a', b')). (a, a') \<in> ra \<or> a = a' \<and> (b, b') \<in> rb}"
krauss@26748: 
krauss@26748: lemma wf_lex_prod [intro!]: "[| wf(ra); wf(rb) |] ==> wf(ra <*lex*> rb)"
krauss@26748: apply (unfold wf_def lex_prod_def) 
krauss@26748: apply (rule allI, rule impI)
krauss@26748: apply (simp (no_asm_use) only: split_paired_All)
krauss@26748: apply (drule spec, erule mp) 
krauss@26748: apply (rule allI, rule impI)
krauss@26748: apply (drule spec, erule mp, blast) 
krauss@26748: done
krauss@26748: 
krauss@26748: lemma in_lex_prod[simp]: 
krauss@26748:   "(((a,b),(a',b')): r <*lex*> s) = ((a,a'): r \<or> (a = a' \<and> (b, b') : s))"
krauss@26748:   by (auto simp:lex_prod_def)
krauss@26748: 
wenzelm@60758: text\<open>@{term "op <*lex*>"} preserves transitivity\<close>
krauss@26748: 
krauss@26748: lemma trans_lex_prod [intro!]: 
krauss@26748:     "[| trans R1; trans R2 |] ==> trans (R1 <*lex*> R2)"
krauss@26748: by (unfold trans_def lex_prod_def, blast) 
krauss@26748: 
wenzelm@60758: text \<open>lexicographic combinations with measure functions\<close>
krauss@26748: 
krauss@26748: definition 
krauss@26748:   mlex_prod :: "('a \<Rightarrow> nat) \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set" (infixr "<*mlex*>" 80)
krauss@26748: where
krauss@26748:   "f <*mlex*> R = inv_image (less_than <*lex*> R) (%x. (f x, x))"
krauss@26748: 
krauss@26748: lemma wf_mlex: "wf R \<Longrightarrow> wf (f <*mlex*> R)"
krauss@26748: unfolding mlex_prod_def
krauss@26748: by auto
krauss@26748: 
krauss@26748: lemma mlex_less: "f x < f y \<Longrightarrow> (x, y) \<in> f <*mlex*> R"
krauss@26748: unfolding mlex_prod_def by simp
krauss@26748: 
krauss@26748: lemma mlex_leq: "f x \<le> f y \<Longrightarrow> (x, y) \<in> R \<Longrightarrow> (x, y) \<in> f <*mlex*> R"
krauss@26748: unfolding mlex_prod_def by auto
krauss@26748: 
wenzelm@60758: text \<open>proper subset relation on finite sets\<close>
krauss@26748: 
krauss@26748: definition finite_psubset  :: "('a set * 'a set) set"
haftmann@45137: where "finite_psubset = {(A,B). A < B & finite B}"
krauss@26748: 
krauss@28260: lemma wf_finite_psubset[simp]: "wf(finite_psubset)"
krauss@26748: apply (unfold finite_psubset_def)
krauss@26748: apply (rule wf_measure [THEN wf_subset])
krauss@26748: apply (simp add: measure_def inv_image_def less_than_def less_eq)
krauss@26748: apply (fast elim!: psubset_card_mono)
krauss@26748: done
krauss@26748: 
krauss@26748: lemma trans_finite_psubset: "trans finite_psubset"
berghofe@26803: by (simp add: finite_psubset_def less_le trans_def, blast)
krauss@26748: 
krauss@28260: lemma in_finite_psubset[simp]: "(A, B) \<in> finite_psubset = (A < B & finite B)"
krauss@28260: unfolding finite_psubset_def by auto
krauss@26748: 
wenzelm@60758: text \<open>max- and min-extension of order to finite sets\<close>
krauss@28735: 
krauss@28735: inductive_set max_ext :: "('a \<times> 'a) set \<Rightarrow> ('a set \<times> 'a set) set" 
krauss@28735: for R :: "('a \<times> 'a) set"
krauss@28735: where
krauss@28735:   max_extI[intro]: "finite X \<Longrightarrow> finite Y \<Longrightarrow> Y \<noteq> {} \<Longrightarrow> (\<And>x. x \<in> X \<Longrightarrow> \<exists>y\<in>Y. (x, y) \<in> R) \<Longrightarrow> (X, Y) \<in> max_ext R"
krauss@28735: 
krauss@28735: lemma max_ext_wf:
krauss@28735:   assumes wf: "wf r"
krauss@28735:   shows "wf (max_ext r)"
krauss@28735: proof (rule acc_wfI, intro allI)
krauss@28735:   fix M show "M \<in> acc (max_ext r)" (is "_ \<in> ?W")
krauss@28735:   proof cases
krauss@28735:     assume "finite M"
krauss@28735:     thus ?thesis
krauss@28735:     proof (induct M)
krauss@28735:       show "{} \<in> ?W"
krauss@28735:         by (rule accI) (auto elim: max_ext.cases)
krauss@28735:     next
krauss@28735:       fix M a assume "M \<in> ?W" "finite M"
krauss@28735:       with wf show "insert a M \<in> ?W"
krauss@28735:       proof (induct arbitrary: M)
krauss@28735:         fix M a
krauss@28735:         assume "M \<in> ?W"  and  [intro]: "finite M"
krauss@28735:         assume hyp: "\<And>b M. (b, a) \<in> r \<Longrightarrow> M \<in> ?W \<Longrightarrow> finite M \<Longrightarrow> insert b M \<in> ?W"
krauss@28735:         {
krauss@28735:           fix N M :: "'a set"
krauss@28735:           assume "finite N" "finite M"
krauss@28735:           then
krauss@28735:           have "\<lbrakk>M \<in> ?W ; (\<And>y. y \<in> N \<Longrightarrow> (y, a) \<in> r)\<rbrakk> \<Longrightarrow>  N \<union> M \<in> ?W"
krauss@28735:             by (induct N arbitrary: M) (auto simp: hyp)
krauss@28735:         }
krauss@28735:         note add_less = this
krauss@28735:         
krauss@28735:         show "insert a M \<in> ?W"
krauss@28735:         proof (rule accI)
krauss@28735:           fix N assume Nless: "(N, insert a M) \<in> max_ext r"
krauss@28735:           hence asm1: "\<And>x. x \<in> N \<Longrightarrow> (x, a) \<in> r \<or> (\<exists>y \<in> M. (x, y) \<in> r)"
krauss@28735:             by (auto elim!: max_ext.cases)
krauss@28735: 
krauss@28735:           let ?N1 = "{ n \<in> N. (n, a) \<in> r }"
krauss@28735:           let ?N2 = "{ n \<in> N. (n, a) \<notin> r }"
nipkow@39302:           have N: "?N1 \<union> ?N2 = N" by (rule set_eqI) auto
krauss@28735:           from Nless have "finite N" by (auto elim: max_ext.cases)
krauss@28735:           then have finites: "finite ?N1" "finite ?N2" by auto
krauss@28735:           
krauss@28735:           have "?N2 \<in> ?W"
krauss@28735:           proof cases
krauss@28735:             assume [simp]: "M = {}"
krauss@28735:             have Mw: "{} \<in> ?W" by (rule accI) (auto elim: max_ext.cases)
krauss@28735: 
krauss@28735:             from asm1 have "?N2 = {}" by auto
krauss@28735:             with Mw show "?N2 \<in> ?W" by (simp only:)
krauss@28735:           next
krauss@28735:             assume "M \<noteq> {}"
bulwahn@49945:             from asm1 finites have N2: "(?N2, M) \<in> max_ext r" 
wenzelm@60758:               by (rule_tac max_extI[OF _ _ \<open>M \<noteq> {}\<close>]) auto
bulwahn@49945: 
wenzelm@60758:             with \<open>M \<in> ?W\<close> show "?N2 \<in> ?W" by (rule acc_downward)
krauss@28735:           qed
krauss@28735:           with finites have "?N1 \<union> ?N2 \<in> ?W" 
krauss@28735:             by (rule add_less) simp
krauss@28735:           then show "N \<in> ?W" by (simp only: N)
krauss@28735:         qed
krauss@28735:       qed
krauss@28735:     qed
krauss@28735:   next
krauss@28735:     assume [simp]: "\<not> finite M"
krauss@28735:     show ?thesis
krauss@28735:       by (rule accI) (auto elim: max_ext.cases)
krauss@28735:   qed
krauss@28735: qed
krauss@28735: 
krauss@29125: lemma max_ext_additive: 
krauss@29125:  "(A, B) \<in> max_ext R \<Longrightarrow> (C, D) \<in> max_ext R \<Longrightarrow>
krauss@29125:   (A \<union> C, B \<union> D) \<in> max_ext R"
krauss@29125: by (force elim!: max_ext.cases)
krauss@29125: 
krauss@28735: 
haftmann@37767: definition min_ext :: "('a \<times> 'a) set \<Rightarrow> ('a set \<times> 'a set) set"  where
haftmann@37767:   "min_ext r = {(X, Y) | X Y. X \<noteq> {} \<and> (\<forall>y \<in> Y. (\<exists>x \<in> X. (x, y) \<in> r))}"
krauss@28735: 
krauss@28735: lemma min_ext_wf:
krauss@28735:   assumes "wf r"
krauss@28735:   shows "wf (min_ext r)"
krauss@28735: proof (rule wfI_min)
krauss@28735:   fix Q :: "'a set set"
krauss@28735:   fix x
krauss@28735:   assume nonempty: "x \<in> Q"
krauss@28735:   show "\<exists>m \<in> Q. (\<forall> n. (n, m) \<in> min_ext r \<longrightarrow> n \<notin> Q)"
krauss@28735:   proof cases
krauss@28735:     assume "Q = {{}}" thus ?thesis by (simp add: min_ext_def)
krauss@28735:   next
krauss@28735:     assume "Q \<noteq> {{}}"
krauss@28735:     with nonempty
krauss@28735:     obtain e x where "x \<in> Q" "e \<in> x" by force
krauss@28735:     then have eU: "e \<in> \<Union>Q" by auto
wenzelm@60758:     with \<open>wf r\<close> 
krauss@28735:     obtain z where z: "z \<in> \<Union>Q" "\<And>y. (y, z) \<in> r \<Longrightarrow> y \<notin> \<Union>Q" 
krauss@28735:       by (erule wfE_min)
krauss@28735:     from z obtain m where "m \<in> Q" "z \<in> m" by auto
wenzelm@60758:     from \<open>m \<in> Q\<close>
krauss@28735:     show ?thesis
krauss@28735:     proof (rule, intro bexI allI impI)
krauss@28735:       fix n
krauss@28735:       assume smaller: "(n, m) \<in> min_ext r"
wenzelm@60758:       with \<open>z \<in> m\<close> obtain y where y: "y \<in> n" "(y, z) \<in> r" by (auto simp: min_ext_def)
krauss@28735:       then show "n \<notin> Q" using z(2) by auto
krauss@28735:     qed      
krauss@28735:   qed
krauss@28735: qed
krauss@26748: 
wenzelm@60758: text\<open>Bounded increase must terminate:\<close>
nipkow@43137: 
nipkow@43137: lemma wf_bounded_measure:
nipkow@43137: fixes ub :: "'a \<Rightarrow> nat" and f :: "'a \<Rightarrow> nat"
nipkow@43140: assumes "!!a b. (b,a) : r \<Longrightarrow> ub b \<le> ub a & ub a \<ge> f b & f b > f a"
nipkow@43137: shows "wf r"
nipkow@43137: apply(rule wf_subset[OF wf_measure[of "%a. ub a - f a"]])
nipkow@43137: apply (auto dest: assms)
nipkow@43137: done
nipkow@43137: 
nipkow@43137: lemma wf_bounded_set:
nipkow@43137: fixes ub :: "'a \<Rightarrow> 'b set" and f :: "'a \<Rightarrow> 'b set"
nipkow@43137: assumes "!!a b. (b,a) : r \<Longrightarrow>
nipkow@43140:   finite(ub a) & ub b \<subseteq> ub a & ub a \<supseteq> f b & f b \<supset> f a"
nipkow@43137: shows "wf r"
nipkow@43137: apply(rule wf_bounded_measure[of r "%a. card(ub a)" "%a. card(f a)"])
nipkow@43137: apply(drule assms)
nipkow@43140: apply (blast intro: card_mono finite_subset psubset_card_mono dest: psubset_eq[THEN iffD2])
nipkow@43137: done
nipkow@43137: 
krauss@26748: 
haftmann@54295: hide_const (open) acc accp
haftmann@54295: 
krauss@26748: end




