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