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