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