src/HOL/Wellfounded.thy
author wenzelm
Tue Sep 01 22:32:58 2015 +0200 (2015-09-01)
changeset 61076 bdc1e2f0a86a
parent 60758 d8d85a8172b5
child 61337 4645502c3c64
permissions -rw-r--r--
eliminated \<Colon>;
     1 (*  Title:      HOL/Wellfounded.thy
     2     Author:     Tobias Nipkow
     3     Author:     Lawrence C Paulson
     4     Author:     Konrad Slind
     5     Author:     Alexander Krauss
     6     Author:     Andrei Popescu, TU Muenchen
     7 *)
     8 
     9 section \<open>Well-founded Recursion\<close>
    10 
    11 theory Wellfounded
    12 imports Transitive_Closure
    13 begin
    14 
    15 subsection \<open>Basic Definitions\<close>
    16 
    17 definition wf :: "('a * 'a) set => bool" where
    18   "wf r \<longleftrightarrow> (!P. (!x. (!y. (y,x):r --> P(y)) --> P(x)) --> (!x. P(x)))"
    19 
    20 definition wfP :: "('a => 'a => bool) => bool" where
    21   "wfP r \<longleftrightarrow> wf {(x, y). r x y}"
    22 
    23 lemma wfP_wf_eq [pred_set_conv]: "wfP (\<lambda>x y. (x, y) \<in> r) = wf r"
    24   by (simp add: wfP_def)
    25 
    26 lemma wfUNIVI: 
    27    "(!!P x. (ALL x. (ALL y. (y,x) : r --> P(y)) --> P(x)) ==> P(x)) ==> wf(r)"
    28   unfolding wf_def by blast
    29 
    30 lemmas wfPUNIVI = wfUNIVI [to_pred]
    31 
    32 text\<open>Restriction to domain @{term A} and range @{term B}.  If @{term r} is
    33     well-founded over their intersection, then @{term "wf r"}\<close>
    34 lemma wfI: 
    35  "[| r \<subseteq> A <*> B; 
    36      !!x P. [|\<forall>x. (\<forall>y. (y,x) : r --> P y) --> P x;  x : A; x : B |] ==> P x |]
    37   ==>  wf r"
    38   unfolding wf_def by blast
    39 
    40 lemma wf_induct: 
    41     "[| wf(r);           
    42         !!x.[| ALL y. (y,x): r --> P(y) |] ==> P(x)  
    43      |]  ==>  P(a)"
    44   unfolding wf_def by blast
    45 
    46 lemmas wfP_induct = wf_induct [to_pred]
    47 
    48 lemmas wf_induct_rule = wf_induct [rule_format, consumes 1, case_names less, induct set: wf]
    49 
    50 lemmas wfP_induct_rule = wf_induct_rule [to_pred, induct set: wfP]
    51 
    52 lemma wf_not_sym: "wf r ==> (a, x) : r ==> (x, a) ~: r"
    53   by (induct a arbitrary: x set: wf) blast
    54 
    55 lemma wf_asym:
    56   assumes "wf r" "(a, x) \<in> r"
    57   obtains "(x, a) \<notin> r"
    58   by (drule wf_not_sym[OF assms])
    59 
    60 lemma wf_not_refl [simp]: "wf r ==> (a, a) ~: r"
    61   by (blast elim: wf_asym)
    62 
    63 lemma wf_irrefl: assumes "wf r" obtains "(a, a) \<notin> r"
    64 by (drule wf_not_refl[OF assms])
    65 
    66 lemma wf_wellorderI:
    67   assumes wf: "wf {(x::'a::ord, y). x < y}"
    68   assumes lin: "OFCLASS('a::ord, linorder_class)"
    69   shows "OFCLASS('a::ord, wellorder_class)"
    70 using lin by (rule wellorder_class.intro)
    71   (blast intro: class.wellorder_axioms.intro wf_induct_rule [OF wf])
    72 
    73 lemma (in wellorder) wf:
    74   "wf {(x, y). x < y}"
    75 unfolding wf_def by (blast intro: less_induct)
    76 
    77 
    78 subsection \<open>Basic Results\<close>
    79 
    80 text \<open>Point-free characterization of well-foundedness\<close>
    81 
    82 lemma wfE_pf:
    83   assumes wf: "wf R"
    84   assumes a: "A \<subseteq> R `` A"
    85   shows "A = {}"
    86 proof -
    87   { fix x
    88     from wf have "x \<notin> A"
    89     proof induct
    90       fix x assume "\<And>y. (y, x) \<in> R \<Longrightarrow> y \<notin> A"
    91       then have "x \<notin> R `` A" by blast
    92       with a show "x \<notin> A" by blast
    93     qed
    94   } thus ?thesis by auto
    95 qed
    96 
    97 lemma wfI_pf:
    98   assumes a: "\<And>A. A \<subseteq> R `` A \<Longrightarrow> A = {}"
    99   shows "wf R"
   100 proof (rule wfUNIVI)
   101   fix P :: "'a \<Rightarrow> bool" and x
   102   let ?A = "{x. \<not> P x}"
   103   assume "\<forall>x. (\<forall>y. (y, x) \<in> R \<longrightarrow> P y) \<longrightarrow> P x"
   104   then have "?A \<subseteq> R `` ?A" by blast
   105   with a show "P x" by blast
   106 qed
   107 
   108 text\<open>Minimal-element characterization of well-foundedness\<close>
   109 
   110 lemma wfE_min:
   111   assumes wf: "wf R" and Q: "x \<in> Q"
   112   obtains z where "z \<in> Q" "\<And>y. (y, z) \<in> R \<Longrightarrow> y \<notin> Q"
   113   using Q wfE_pf[OF wf, of Q] by blast
   114 
   115 lemma wfI_min:
   116   assumes a: "\<And>x Q. x \<in> Q \<Longrightarrow> \<exists>z\<in>Q. \<forall>y. (y, z) \<in> R \<longrightarrow> y \<notin> Q"
   117   shows "wf R"
   118 proof (rule wfI_pf)
   119   fix A assume b: "A \<subseteq> R `` A"
   120   { fix x assume "x \<in> A"
   121     from a[OF this] b have "False" by blast
   122   }
   123   thus "A = {}" by blast
   124 qed
   125 
   126 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))"
   127 apply auto
   128 apply (erule wfE_min, assumption, blast)
   129 apply (rule wfI_min, auto)
   130 done
   131 
   132 lemmas wfP_eq_minimal = wf_eq_minimal [to_pred]
   133 
   134 text\<open>Well-foundedness of transitive closure\<close>
   135 
   136 lemma wf_trancl:
   137   assumes "wf r"
   138   shows "wf (r^+)"
   139 proof -
   140   {
   141     fix P and x
   142     assume induct_step: "!!x. (!!y. (y, x) : r^+ ==> P y) ==> P x"
   143     have "P x"
   144     proof (rule induct_step)
   145       fix y assume "(y, x) : r^+"
   146       with \<open>wf r\<close> show "P y"
   147       proof (induct x arbitrary: y)
   148         case (less x)
   149         note hyp = \<open>\<And>x' y'. (x', x) : r ==> (y', x') : r^+ ==> P y'\<close>
   150         from \<open>(y, x) : r^+\<close> show "P y"
   151         proof cases
   152           case base
   153           show "P y"
   154           proof (rule induct_step)
   155             fix y' assume "(y', y) : r^+"
   156             with \<open>(y, x) : r\<close> show "P y'" by (rule hyp [of y y'])
   157           qed
   158         next
   159           case step
   160           then obtain x' where "(x', x) : r" and "(y, x') : r^+" by simp
   161           then show "P y" by (rule hyp [of x' y])
   162         qed
   163       qed
   164     qed
   165   } then show ?thesis unfolding wf_def by blast
   166 qed
   167 
   168 lemmas wfP_trancl = wf_trancl [to_pred]
   169 
   170 lemma wf_converse_trancl: "wf (r^-1) ==> wf ((r^+)^-1)"
   171   apply (subst trancl_converse [symmetric])
   172   apply (erule wf_trancl)
   173   done
   174 
   175 text \<open>Well-foundedness of subsets\<close>
   176 
   177 lemma wf_subset: "[| wf(r);  p<=r |] ==> wf(p)"
   178   apply (simp (no_asm_use) add: wf_eq_minimal)
   179   apply fast
   180   done
   181 
   182 lemmas wfP_subset = wf_subset [to_pred]
   183 
   184 text \<open>Well-foundedness of the empty relation\<close>
   185 
   186 lemma wf_empty [iff]: "wf {}"
   187   by (simp add: wf_def)
   188 
   189 lemma wfP_empty [iff]:
   190   "wfP (\<lambda>x y. False)"
   191 proof -
   192   have "wfP bot" by (fact wf_empty [to_pred bot_empty_eq2])
   193   then show ?thesis by (simp add: bot_fun_def)
   194 qed
   195 
   196 lemma wf_Int1: "wf r ==> wf (r Int r')"
   197   apply (erule wf_subset)
   198   apply (rule Int_lower1)
   199   done
   200 
   201 lemma wf_Int2: "wf r ==> wf (r' Int r)"
   202   apply (erule wf_subset)
   203   apply (rule Int_lower2)
   204   done  
   205 
   206 text \<open>Exponentiation\<close>
   207 
   208 lemma wf_exp:
   209   assumes "wf (R ^^ n)"
   210   shows "wf R"
   211 proof (rule wfI_pf)
   212   fix A assume "A \<subseteq> R `` A"
   213   then have "A \<subseteq> (R ^^ n) `` A" by (induct n) force+
   214   with \<open>wf (R ^^ n)\<close>
   215   show "A = {}" by (rule wfE_pf)
   216 qed
   217 
   218 text \<open>Well-foundedness of insert\<close>
   219 
   220 lemma wf_insert [iff]: "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)"
   221 apply (rule iffI)
   222  apply (blast elim: wf_trancl [THEN wf_irrefl]
   223               intro: rtrancl_into_trancl1 wf_subset 
   224                      rtrancl_mono [THEN [2] rev_subsetD])
   225 apply (simp add: wf_eq_minimal, safe)
   226 apply (rule allE, assumption, erule impE, blast) 
   227 apply (erule bexE)
   228 apply (rename_tac "a", case_tac "a = x")
   229  prefer 2
   230 apply blast 
   231 apply (case_tac "y:Q")
   232  prefer 2 apply blast
   233 apply (rule_tac x = "{z. z:Q & (z,y) : r^*}" in allE)
   234  apply assumption
   235 apply (erule_tac V = "ALL Q. (EX x. x : Q) --> P Q" for P in thin_rl) 
   236   --\<open>essential for speed\<close>
   237 txt\<open>Blast with new substOccur fails\<close>
   238 apply (fast intro: converse_rtrancl_into_rtrancl)
   239 done
   240 
   241 text\<open>Well-foundedness of image\<close>
   242 
   243 lemma wf_map_prod_image: "[| wf r; inj f |] ==> wf (map_prod f f ` r)"
   244 apply (simp only: wf_eq_minimal, clarify)
   245 apply (case_tac "EX p. f p : Q")
   246 apply (erule_tac x = "{p. f p : Q}" in allE)
   247 apply (fast dest: inj_onD, blast)
   248 done
   249 
   250 
   251 subsection \<open>Well-Foundedness Results for Unions\<close>
   252 
   253 lemma wf_union_compatible:
   254   assumes "wf R" "wf S"
   255   assumes "R O S \<subseteq> R"
   256   shows "wf (R \<union> S)"
   257 proof (rule wfI_min)
   258   fix x :: 'a and Q 
   259   let ?Q' = "{x \<in> Q. \<forall>y. (y, x) \<in> R \<longrightarrow> y \<notin> Q}"
   260   assume "x \<in> Q"
   261   obtain a where "a \<in> ?Q'"
   262     by (rule wfE_min [OF \<open>wf R\<close> \<open>x \<in> Q\<close>]) blast
   263   with \<open>wf S\<close>
   264   obtain z where "z \<in> ?Q'" and zmin: "\<And>y. (y, z) \<in> S \<Longrightarrow> y \<notin> ?Q'" by (erule wfE_min)
   265   { 
   266     fix y assume "(y, z) \<in> S"
   267     then have "y \<notin> ?Q'" by (rule zmin)
   268 
   269     have "y \<notin> Q"
   270     proof 
   271       assume "y \<in> Q"
   272       with \<open>y \<notin> ?Q'\<close> 
   273       obtain w where "(w, y) \<in> R" and "w \<in> Q" by auto
   274       from \<open>(w, y) \<in> R\<close> \<open>(y, z) \<in> S\<close> have "(w, z) \<in> R O S" by (rule relcompI)
   275       with \<open>R O S \<subseteq> R\<close> have "(w, z) \<in> R" ..
   276       with \<open>z \<in> ?Q'\<close> have "w \<notin> Q" by blast 
   277       with \<open>w \<in> Q\<close> show False by contradiction
   278     qed
   279   }
   280   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
   281 qed
   282 
   283 
   284 text \<open>Well-foundedness of indexed union with disjoint domains and ranges\<close>
   285 
   286 lemma wf_UN: "[| ALL i:I. wf(r i);  
   287          ALL i:I. ALL j:I. r i ~= r j --> Domain(r i) Int Range(r j) = {}  
   288       |] ==> wf(UN i:I. r i)"
   289 apply (simp only: wf_eq_minimal, clarify)
   290 apply (rename_tac A a, case_tac "EX i:I. EX a:A. EX b:A. (b,a) : r i")
   291  prefer 2
   292  apply force 
   293 apply clarify
   294 apply (drule bspec, assumption)  
   295 apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r i) }" in allE)
   296 apply (blast elim!: allE)  
   297 done
   298 
   299 lemma wfP_SUP:
   300   "\<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)"
   301   apply (rule wf_UN[to_pred])
   302   apply simp_all
   303   done
   304 
   305 lemma wf_Union: 
   306  "[| ALL r:R. wf r;  
   307      ALL r:R. ALL s:R. r ~= s --> Domain r Int Range s = {}  
   308   |] ==> wf(Union R)"
   309   using wf_UN[of R "\<lambda>i. i"] by simp
   310 
   311 (*Intuition: we find an (R u S)-min element of a nonempty subset A
   312              by case distinction.
   313   1. There is a step a -R-> b with a,b : A.
   314      Pick an R-min element z of the (nonempty) set {a:A | EX b:A. a -R-> b}.
   315      By definition, there is z':A s.t. z -R-> z'. Because z is R-min in the
   316      subset, z' must be R-min in A. Because z' has an R-predecessor, it cannot
   317      have an S-successor and is thus S-min in A as well.
   318   2. There is no such step.
   319      Pick an S-min element of A. In this case it must be an R-min
   320      element of A as well.
   321 *)
   322 lemma wf_Un:
   323      "[| wf r; wf s; Domain r Int Range s = {} |] ==> wf(r Un s)"
   324   using wf_union_compatible[of s r] 
   325   by (auto simp: Un_ac)
   326 
   327 lemma wf_union_merge: 
   328   "wf (R \<union> S) = wf (R O R \<union> S O R \<union> S)" (is "wf ?A = wf ?B")
   329 proof
   330   assume "wf ?A"
   331   with wf_trancl have wfT: "wf (?A^+)" .
   332   moreover have "?B \<subseteq> ?A^+"
   333     by (subst trancl_unfold, subst trancl_unfold) blast
   334   ultimately show "wf ?B" by (rule wf_subset)
   335 next
   336   assume "wf ?B"
   337 
   338   show "wf ?A"
   339   proof (rule wfI_min)
   340     fix Q :: "'a set" and x 
   341     assume "x \<in> Q"
   342 
   343     with \<open>wf ?B\<close>
   344     obtain z where "z \<in> Q" and "\<And>y. (y, z) \<in> ?B \<Longrightarrow> y \<notin> Q" 
   345       by (erule wfE_min)
   346     then have A1: "\<And>y. (y, z) \<in> R O R \<Longrightarrow> y \<notin> Q"
   347       and A2: "\<And>y. (y, z) \<in> S O R \<Longrightarrow> y \<notin> Q"
   348       and A3: "\<And>y. (y, z) \<in> S \<Longrightarrow> y \<notin> Q"
   349       by auto
   350     
   351     show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> ?A \<longrightarrow> y \<notin> Q"
   352     proof (cases "\<forall>y. (y, z) \<in> R \<longrightarrow> y \<notin> Q")
   353       case True
   354       with \<open>z \<in> Q\<close> A3 show ?thesis by blast
   355     next
   356       case False 
   357       then obtain z' where "z'\<in>Q" "(z', z) \<in> R" by blast
   358 
   359       have "\<forall>y. (y, z') \<in> ?A \<longrightarrow> y \<notin> Q"
   360       proof (intro allI impI)
   361         fix y assume "(y, z') \<in> ?A"
   362         then show "y \<notin> Q"
   363         proof
   364           assume "(y, z') \<in> R" 
   365           then have "(y, z) \<in> R O R" using \<open>(z', z) \<in> R\<close> ..
   366           with A1 show "y \<notin> Q" .
   367         next
   368           assume "(y, z') \<in> S" 
   369           then have "(y, z) \<in> S O R" using  \<open>(z', z) \<in> R\<close> ..
   370           with A2 show "y \<notin> Q" .
   371         qed
   372       qed
   373       with \<open>z' \<in> Q\<close> show ?thesis ..
   374     qed
   375   qed
   376 qed
   377 
   378 lemma wf_comp_self: "wf R = wf (R O R)"  -- \<open>special case\<close>
   379   by (rule wf_union_merge [where S = "{}", simplified])
   380 
   381 
   382 subsection \<open>Well-Foundedness of Composition\<close>
   383 
   384 text \<open>Bachmair and Dershowitz 1986, Lemma 2. [Provided by Tjark Weber]\<close>
   385 
   386 lemma qc_wf_relto_iff:
   387   assumes "R O S \<subseteq> (R \<union> S)\<^sup>* O R" -- \<open>R quasi-commutes over S\<close>
   388   shows "wf (S\<^sup>* O R O S\<^sup>*) \<longleftrightarrow> wf R" (is "wf ?S \<longleftrightarrow> _")
   389 proof
   390   assume "wf ?S"
   391   moreover have "R \<subseteq> ?S" by auto
   392   ultimately show "wf R" using wf_subset by auto
   393 next
   394   assume "wf R"
   395   show "wf ?S"
   396   proof (rule wfI_pf)
   397     fix A assume A: "A \<subseteq> ?S `` A"
   398     let ?X = "(R \<union> S)\<^sup>* `` A"
   399     have *: "R O (R \<union> S)\<^sup>* \<subseteq> (R \<union> S)\<^sup>* O R"
   400       proof -
   401         { fix x y z assume "(y, z) \<in> (R \<union> S)\<^sup>*" and "(x, y) \<in> R"
   402           then have "(x, z) \<in> (R \<union> S)\<^sup>* O R"
   403           proof (induct y z)
   404             case rtrancl_refl then show ?case by auto
   405           next
   406             case (rtrancl_into_rtrancl a b c)
   407             then have "(x, c) \<in> ((R \<union> S)\<^sup>* O (R \<union> S)\<^sup>*) O R" using assms by blast
   408             then show ?case by simp
   409           qed }
   410         then show ?thesis by auto
   411       qed
   412     then have "R O S\<^sup>* \<subseteq> (R \<union> S)\<^sup>* O R" using rtrancl_Un_subset by blast
   413     then have "?S \<subseteq> (R \<union> S)\<^sup>* O (R \<union> S)\<^sup>* O R" by (simp add: relcomp_mono rtrancl_mono)
   414     also have "\<dots> = (R \<union> S)\<^sup>* O R" by (simp add: O_assoc[symmetric])
   415     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)
   416     also have "\<dots> \<subseteq> (R \<union> S)\<^sup>* O (R \<union> S)\<^sup>* O R" using * by (simp add: relcomp_mono)
   417     finally have "?S O (R \<union> S)\<^sup>* \<subseteq> (R \<union> S)\<^sup>* O R" by (simp add: O_assoc[symmetric])
   418     then have "(?S O (R \<union> S)\<^sup>*) `` A \<subseteq> ((R \<union> S)\<^sup>* O R) `` A" by (simp add: Image_mono)
   419     moreover have "?X \<subseteq> (?S O (R \<union> S)\<^sup>*) `` A" using A by (auto simp: relcomp_Image)
   420     ultimately have "?X \<subseteq> R `` ?X" by (auto simp: relcomp_Image)
   421     then have "?X = {}" using \<open>wf R\<close> by (simp add: wfE_pf)
   422     moreover have "A \<subseteq> ?X" by auto
   423     ultimately show "A = {}" by simp
   424   qed
   425 qed
   426 
   427 corollary wf_relcomp_compatible:
   428   assumes "wf R" and "R O S \<subseteq> S O R"
   429   shows "wf (S O R)"
   430 proof -
   431   have "R O S \<subseteq> (R \<union> S)\<^sup>* O R"
   432     using assms by blast
   433   then have "wf (S\<^sup>* O R O S\<^sup>*)"
   434     by (simp add: assms qc_wf_relto_iff)
   435   then show ?thesis
   436     by (rule Wellfounded.wf_subset) blast
   437 qed
   438 
   439 
   440 subsection \<open>Acyclic relations\<close>
   441 
   442 lemma wf_acyclic: "wf r ==> acyclic r"
   443 apply (simp add: acyclic_def)
   444 apply (blast elim: wf_trancl [THEN wf_irrefl])
   445 done
   446 
   447 lemmas wfP_acyclicP = wf_acyclic [to_pred]
   448 
   449 text\<open>Wellfoundedness of finite acyclic relations\<close>
   450 
   451 lemma finite_acyclic_wf [rule_format]: "finite r ==> acyclic r --> wf r"
   452 apply (erule finite_induct, blast)
   453 apply (simp (no_asm_simp) only: split_tupled_all)
   454 apply simp
   455 done
   456 
   457 lemma finite_acyclic_wf_converse: "[|finite r; acyclic r|] ==> wf (r^-1)"
   458 apply (erule finite_converse [THEN iffD2, THEN finite_acyclic_wf])
   459 apply (erule acyclic_converse [THEN iffD2])
   460 done
   461 
   462 lemma wf_iff_acyclic_if_finite: "finite r ==> wf r = acyclic r"
   463 by (blast intro: finite_acyclic_wf wf_acyclic)
   464 
   465 
   466 subsection \<open>@{typ nat} is well-founded\<close>
   467 
   468 lemma less_nat_rel: "op < = (\<lambda>m n. n = Suc m)^++"
   469 proof (rule ext, rule ext, rule iffI)
   470   fix n m :: nat
   471   assume "m < n"
   472   then show "(\<lambda>m n. n = Suc m)^++ m n"
   473   proof (induct n)
   474     case 0 then show ?case by auto
   475   next
   476     case (Suc n) then show ?case
   477       by (auto simp add: less_Suc_eq_le le_less intro: tranclp.trancl_into_trancl)
   478   qed
   479 next
   480   fix n m :: nat
   481   assume "(\<lambda>m n. n = Suc m)^++ m n"
   482   then show "m < n"
   483     by (induct n)
   484       (simp_all add: less_Suc_eq_le reflexive le_less)
   485 qed
   486 
   487 definition
   488   pred_nat :: "(nat * nat) set" where
   489   "pred_nat = {(m, n). n = Suc m}"
   490 
   491 definition
   492   less_than :: "(nat * nat) set" where
   493   "less_than = pred_nat^+"
   494 
   495 lemma less_eq: "(m, n) \<in> pred_nat^+ \<longleftrightarrow> m < n"
   496   unfolding less_nat_rel pred_nat_def trancl_def by simp
   497 
   498 lemma pred_nat_trancl_eq_le:
   499   "(m, n) \<in> pred_nat^* \<longleftrightarrow> m \<le> n"
   500   unfolding less_eq rtrancl_eq_or_trancl by auto
   501 
   502 lemma wf_pred_nat: "wf pred_nat"
   503   apply (unfold wf_def pred_nat_def, clarify)
   504   apply (induct_tac x, blast+)
   505   done
   506 
   507 lemma wf_less_than [iff]: "wf less_than"
   508   by (simp add: less_than_def wf_pred_nat [THEN wf_trancl])
   509 
   510 lemma trans_less_than [iff]: "trans less_than"
   511   by (simp add: less_than_def)
   512 
   513 lemma less_than_iff [iff]: "((x,y): less_than) = (x<y)"
   514   by (simp add: less_than_def less_eq)
   515 
   516 lemma wf_less: "wf {(x, y::nat). x < y}"
   517   by (rule Wellfounded.wellorder_class.wf)
   518 
   519 
   520 subsection \<open>Accessible Part\<close>
   521 
   522 text \<open>
   523  Inductive definition of the accessible part @{term "acc r"} of a
   524  relation; see also @{cite "paulin-tlca"}.
   525 \<close>
   526 
   527 inductive_set
   528   acc :: "('a * 'a) set => 'a set"
   529   for r :: "('a * 'a) set"
   530   where
   531     accI: "(!!y. (y, x) : r ==> y : acc r) ==> x : acc r"
   532 
   533 abbreviation
   534   termip :: "('a => 'a => bool) => 'a => bool" where
   535   "termip r \<equiv> accp (r\<inverse>\<inverse>)"
   536 
   537 abbreviation
   538   termi :: "('a * 'a) set => 'a set" where
   539   "termi r \<equiv> acc (r\<inverse>)"
   540 
   541 lemmas accpI = accp.accI
   542 
   543 lemma accp_eq_acc [code]:
   544   "accp r = (\<lambda>x. x \<in> Wellfounded.acc {(x, y). r x y})"
   545   by (simp add: acc_def)
   546 
   547 
   548 text \<open>Induction rules\<close>
   549 
   550 theorem accp_induct:
   551   assumes major: "accp r a"
   552   assumes hyp: "!!x. accp r x ==> \<forall>y. r y x --> P y ==> P x"
   553   shows "P a"
   554   apply (rule major [THEN accp.induct])
   555   apply (rule hyp)
   556    apply (rule accp.accI)
   557    apply fast
   558   apply fast
   559   done
   560 
   561 theorems accp_induct_rule = accp_induct [rule_format, induct set: accp]
   562 
   563 theorem accp_downward: "accp r b ==> r a b ==> accp r a"
   564   apply (erule accp.cases)
   565   apply fast
   566   done
   567 
   568 lemma not_accp_down:
   569   assumes na: "\<not> accp R x"
   570   obtains z where "R z x" and "\<not> accp R z"
   571 proof -
   572   assume a: "\<And>z. \<lbrakk>R z x; \<not> accp R z\<rbrakk> \<Longrightarrow> thesis"
   573 
   574   show thesis
   575   proof (cases "\<forall>z. R z x \<longrightarrow> accp R z")
   576     case True
   577     hence "\<And>z. R z x \<Longrightarrow> accp R z" by auto
   578     hence "accp R x"
   579       by (rule accp.accI)
   580     with na show thesis ..
   581   next
   582     case False then obtain z where "R z x" and "\<not> accp R z"
   583       by auto
   584     with a show thesis .
   585   qed
   586 qed
   587 
   588 lemma accp_downwards_aux: "r\<^sup>*\<^sup>* b a ==> accp r a --> accp r b"
   589   apply (erule rtranclp_induct)
   590    apply blast
   591   apply (blast dest: accp_downward)
   592   done
   593 
   594 theorem accp_downwards: "accp r a ==> r\<^sup>*\<^sup>* b a ==> accp r b"
   595   apply (blast dest: accp_downwards_aux)
   596   done
   597 
   598 theorem accp_wfPI: "\<forall>x. accp r x ==> wfP r"
   599   apply (rule wfPUNIVI)
   600   apply (rule_tac P=P in accp_induct)
   601    apply blast
   602   apply blast
   603   done
   604 
   605 theorem accp_wfPD: "wfP r ==> accp r x"
   606   apply (erule wfP_induct_rule)
   607   apply (rule accp.accI)
   608   apply blast
   609   done
   610 
   611 theorem wfP_accp_iff: "wfP r = (\<forall>x. accp r x)"
   612   apply (blast intro: accp_wfPI dest: accp_wfPD)
   613   done
   614 
   615 
   616 text \<open>Smaller relations have bigger accessible parts:\<close>
   617 
   618 lemma accp_subset:
   619   assumes sub: "R1 \<le> R2"
   620   shows "accp R2 \<le> accp R1"
   621 proof (rule predicate1I)
   622   fix x assume "accp R2 x"
   623   then show "accp R1 x"
   624   proof (induct x)
   625     fix x
   626     assume ih: "\<And>y. R2 y x \<Longrightarrow> accp R1 y"
   627     with sub show "accp R1 x"
   628       by (blast intro: accp.accI)
   629   qed
   630 qed
   631 
   632 
   633 text \<open>This is a generalized induction theorem that works on
   634   subsets of the accessible part.\<close>
   635 
   636 lemma accp_subset_induct:
   637   assumes subset: "D \<le> accp R"
   638     and dcl: "\<And>x z. \<lbrakk>D x; R z x\<rbrakk> \<Longrightarrow> D z"
   639     and "D x"
   640     and istep: "\<And>x. \<lbrakk>D x; (\<And>z. R z x \<Longrightarrow> P z)\<rbrakk> \<Longrightarrow> P x"
   641   shows "P x"
   642 proof -
   643   from subset and \<open>D x\<close>
   644   have "accp R x" ..
   645   then show "P x" using \<open>D x\<close>
   646   proof (induct x)
   647     fix x
   648     assume "D x"
   649       and "\<And>y. R y x \<Longrightarrow> D y \<Longrightarrow> P y"
   650     with dcl and istep show "P x" by blast
   651   qed
   652 qed
   653 
   654 
   655 text \<open>Set versions of the above theorems\<close>
   656 
   657 lemmas acc_induct = accp_induct [to_set]
   658 
   659 lemmas acc_induct_rule = acc_induct [rule_format, induct set: acc]
   660 
   661 lemmas acc_downward = accp_downward [to_set]
   662 
   663 lemmas not_acc_down = not_accp_down [to_set]
   664 
   665 lemmas acc_downwards_aux = accp_downwards_aux [to_set]
   666 
   667 lemmas acc_downwards = accp_downwards [to_set]
   668 
   669 lemmas acc_wfI = accp_wfPI [to_set]
   670 
   671 lemmas acc_wfD = accp_wfPD [to_set]
   672 
   673 lemmas wf_acc_iff = wfP_accp_iff [to_set]
   674 
   675 lemmas acc_subset = accp_subset [to_set]
   676 
   677 lemmas acc_subset_induct = accp_subset_induct [to_set]
   678 
   679 
   680 subsection \<open>Tools for building wellfounded relations\<close>
   681 
   682 text \<open>Inverse Image\<close>
   683 
   684 lemma wf_inv_image [simp,intro!]: "wf(r) ==> wf(inv_image r (f::'a=>'b))"
   685 apply (simp (no_asm_use) add: inv_image_def wf_eq_minimal)
   686 apply clarify
   687 apply (subgoal_tac "EX (w::'b) . w : {w. EX (x::'a) . x: Q & (f x = w) }")
   688 prefer 2 apply (blast del: allE)
   689 apply (erule allE)
   690 apply (erule (1) notE impE)
   691 apply blast
   692 done
   693 
   694 text \<open>Measure functions into @{typ nat}\<close>
   695 
   696 definition measure :: "('a => nat) => ('a * 'a)set"
   697 where "measure = inv_image less_than"
   698 
   699 lemma in_measure[simp, code_unfold]: "((x,y) : measure f) = (f x < f y)"
   700   by (simp add:measure_def)
   701 
   702 lemma wf_measure [iff]: "wf (measure f)"
   703 apply (unfold measure_def)
   704 apply (rule wf_less_than [THEN wf_inv_image])
   705 done
   706 
   707 lemma wf_if_measure: fixes f :: "'a \<Rightarrow> nat"
   708 shows "(!!x. P x \<Longrightarrow> f(g x) < f x) \<Longrightarrow> wf {(y,x). P x \<and> y = g x}"
   709 apply(insert wf_measure[of f])
   710 apply(simp only: measure_def inv_image_def less_than_def less_eq)
   711 apply(erule wf_subset)
   712 apply auto
   713 done
   714 
   715 
   716 text\<open>Lexicographic combinations\<close>
   717 
   718 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
   719   "ra <*lex*> rb = {((a, b), (a', b')). (a, a') \<in> ra \<or> a = a' \<and> (b, b') \<in> rb}"
   720 
   721 lemma wf_lex_prod [intro!]: "[| wf(ra); wf(rb) |] ==> wf(ra <*lex*> rb)"
   722 apply (unfold wf_def lex_prod_def) 
   723 apply (rule allI, rule impI)
   724 apply (simp (no_asm_use) only: split_paired_All)
   725 apply (drule spec, erule mp) 
   726 apply (rule allI, rule impI)
   727 apply (drule spec, erule mp, blast) 
   728 done
   729 
   730 lemma in_lex_prod[simp]: 
   731   "(((a,b),(a',b')): r <*lex*> s) = ((a,a'): r \<or> (a = a' \<and> (b, b') : s))"
   732   by (auto simp:lex_prod_def)
   733 
   734 text\<open>@{term "op <*lex*>"} preserves transitivity\<close>
   735 
   736 lemma trans_lex_prod [intro!]: 
   737     "[| trans R1; trans R2 |] ==> trans (R1 <*lex*> R2)"
   738 by (unfold trans_def lex_prod_def, blast) 
   739 
   740 text \<open>lexicographic combinations with measure functions\<close>
   741 
   742 definition 
   743   mlex_prod :: "('a \<Rightarrow> nat) \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set" (infixr "<*mlex*>" 80)
   744 where
   745   "f <*mlex*> R = inv_image (less_than <*lex*> R) (%x. (f x, x))"
   746 
   747 lemma wf_mlex: "wf R \<Longrightarrow> wf (f <*mlex*> R)"
   748 unfolding mlex_prod_def
   749 by auto
   750 
   751 lemma mlex_less: "f x < f y \<Longrightarrow> (x, y) \<in> f <*mlex*> R"
   752 unfolding mlex_prod_def by simp
   753 
   754 lemma mlex_leq: "f x \<le> f y \<Longrightarrow> (x, y) \<in> R \<Longrightarrow> (x, y) \<in> f <*mlex*> R"
   755 unfolding mlex_prod_def by auto
   756 
   757 text \<open>proper subset relation on finite sets\<close>
   758 
   759 definition finite_psubset  :: "('a set * 'a set) set"
   760 where "finite_psubset = {(A,B). A < B & finite B}"
   761 
   762 lemma wf_finite_psubset[simp]: "wf(finite_psubset)"
   763 apply (unfold finite_psubset_def)
   764 apply (rule wf_measure [THEN wf_subset])
   765 apply (simp add: measure_def inv_image_def less_than_def less_eq)
   766 apply (fast elim!: psubset_card_mono)
   767 done
   768 
   769 lemma trans_finite_psubset: "trans finite_psubset"
   770 by (simp add: finite_psubset_def less_le trans_def, blast)
   771 
   772 lemma in_finite_psubset[simp]: "(A, B) \<in> finite_psubset = (A < B & finite B)"
   773 unfolding finite_psubset_def by auto
   774 
   775 text \<open>max- and min-extension of order to finite sets\<close>
   776 
   777 inductive_set max_ext :: "('a \<times> 'a) set \<Rightarrow> ('a set \<times> 'a set) set" 
   778 for R :: "('a \<times> 'a) set"
   779 where
   780   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"
   781 
   782 lemma max_ext_wf:
   783   assumes wf: "wf r"
   784   shows "wf (max_ext r)"
   785 proof (rule acc_wfI, intro allI)
   786   fix M show "M \<in> acc (max_ext r)" (is "_ \<in> ?W")
   787   proof cases
   788     assume "finite M"
   789     thus ?thesis
   790     proof (induct M)
   791       show "{} \<in> ?W"
   792         by (rule accI) (auto elim: max_ext.cases)
   793     next
   794       fix M a assume "M \<in> ?W" "finite M"
   795       with wf show "insert a M \<in> ?W"
   796       proof (induct arbitrary: M)
   797         fix M a
   798         assume "M \<in> ?W"  and  [intro]: "finite M"
   799         assume hyp: "\<And>b M. (b, a) \<in> r \<Longrightarrow> M \<in> ?W \<Longrightarrow> finite M \<Longrightarrow> insert b M \<in> ?W"
   800         {
   801           fix N M :: "'a set"
   802           assume "finite N" "finite M"
   803           then
   804           have "\<lbrakk>M \<in> ?W ; (\<And>y. y \<in> N \<Longrightarrow> (y, a) \<in> r)\<rbrakk> \<Longrightarrow>  N \<union> M \<in> ?W"
   805             by (induct N arbitrary: M) (auto simp: hyp)
   806         }
   807         note add_less = this
   808         
   809         show "insert a M \<in> ?W"
   810         proof (rule accI)
   811           fix N assume Nless: "(N, insert a M) \<in> max_ext r"
   812           hence asm1: "\<And>x. x \<in> N \<Longrightarrow> (x, a) \<in> r \<or> (\<exists>y \<in> M. (x, y) \<in> r)"
   813             by (auto elim!: max_ext.cases)
   814 
   815           let ?N1 = "{ n \<in> N. (n, a) \<in> r }"
   816           let ?N2 = "{ n \<in> N. (n, a) \<notin> r }"
   817           have N: "?N1 \<union> ?N2 = N" by (rule set_eqI) auto
   818           from Nless have "finite N" by (auto elim: max_ext.cases)
   819           then have finites: "finite ?N1" "finite ?N2" by auto
   820           
   821           have "?N2 \<in> ?W"
   822           proof cases
   823             assume [simp]: "M = {}"
   824             have Mw: "{} \<in> ?W" by (rule accI) (auto elim: max_ext.cases)
   825 
   826             from asm1 have "?N2 = {}" by auto
   827             with Mw show "?N2 \<in> ?W" by (simp only:)
   828           next
   829             assume "M \<noteq> {}"
   830             from asm1 finites have N2: "(?N2, M) \<in> max_ext r" 
   831               by (rule_tac max_extI[OF _ _ \<open>M \<noteq> {}\<close>]) auto
   832 
   833             with \<open>M \<in> ?W\<close> show "?N2 \<in> ?W" by (rule acc_downward)
   834           qed
   835           with finites have "?N1 \<union> ?N2 \<in> ?W" 
   836             by (rule add_less) simp
   837           then show "N \<in> ?W" by (simp only: N)
   838         qed
   839       qed
   840     qed
   841   next
   842     assume [simp]: "\<not> finite M"
   843     show ?thesis
   844       by (rule accI) (auto elim: max_ext.cases)
   845   qed
   846 qed
   847 
   848 lemma max_ext_additive: 
   849  "(A, B) \<in> max_ext R \<Longrightarrow> (C, D) \<in> max_ext R \<Longrightarrow>
   850   (A \<union> C, B \<union> D) \<in> max_ext R"
   851 by (force elim!: max_ext.cases)
   852 
   853 
   854 definition min_ext :: "('a \<times> 'a) set \<Rightarrow> ('a set \<times> 'a set) set"  where
   855   "min_ext r = {(X, Y) | X Y. X \<noteq> {} \<and> (\<forall>y \<in> Y. (\<exists>x \<in> X. (x, y) \<in> r))}"
   856 
   857 lemma min_ext_wf:
   858   assumes "wf r"
   859   shows "wf (min_ext r)"
   860 proof (rule wfI_min)
   861   fix Q :: "'a set set"
   862   fix x
   863   assume nonempty: "x \<in> Q"
   864   show "\<exists>m \<in> Q. (\<forall> n. (n, m) \<in> min_ext r \<longrightarrow> n \<notin> Q)"
   865   proof cases
   866     assume "Q = {{}}" thus ?thesis by (simp add: min_ext_def)
   867   next
   868     assume "Q \<noteq> {{}}"
   869     with nonempty
   870     obtain e x where "x \<in> Q" "e \<in> x" by force
   871     then have eU: "e \<in> \<Union>Q" by auto
   872     with \<open>wf r\<close> 
   873     obtain z where z: "z \<in> \<Union>Q" "\<And>y. (y, z) \<in> r \<Longrightarrow> y \<notin> \<Union>Q" 
   874       by (erule wfE_min)
   875     from z obtain m where "m \<in> Q" "z \<in> m" by auto
   876     from \<open>m \<in> Q\<close>
   877     show ?thesis
   878     proof (rule, intro bexI allI impI)
   879       fix n
   880       assume smaller: "(n, m) \<in> min_ext r"
   881       with \<open>z \<in> m\<close> obtain y where y: "y \<in> n" "(y, z) \<in> r" by (auto simp: min_ext_def)
   882       then show "n \<notin> Q" using z(2) by auto
   883     qed      
   884   qed
   885 qed
   886 
   887 text\<open>Bounded increase must terminate:\<close>
   888 
   889 lemma wf_bounded_measure:
   890 fixes ub :: "'a \<Rightarrow> nat" and f :: "'a \<Rightarrow> nat"
   891 assumes "!!a b. (b,a) : r \<Longrightarrow> ub b \<le> ub a & ub a \<ge> f b & f b > f a"
   892 shows "wf r"
   893 apply(rule wf_subset[OF wf_measure[of "%a. ub a - f a"]])
   894 apply (auto dest: assms)
   895 done
   896 
   897 lemma wf_bounded_set:
   898 fixes ub :: "'a \<Rightarrow> 'b set" and f :: "'a \<Rightarrow> 'b set"
   899 assumes "!!a b. (b,a) : r \<Longrightarrow>
   900   finite(ub a) & ub b \<subseteq> ub a & ub a \<supseteq> f b & f b \<supset> f a"
   901 shows "wf r"
   902 apply(rule wf_bounded_measure[of r "%a. card(ub a)" "%a. card(f a)"])
   903 apply(drule assms)
   904 apply (blast intro: card_mono finite_subset psubset_card_mono dest: psubset_eq[THEN iffD2])
   905 done
   906 
   907 
   908 hide_const (open) acc accp
   909 
   910 end