src/HOL/Wellfounded.thy
 author hoelzl Thu Sep 15 12:40:08 2011 -0400 (2011-09-15) changeset 44937 22c0857b8aab parent 44921 58eef4843641 child 45012 060f76635bfe permissions -rw-r--r--
removed further legacy rules from Complete_Lattices
1 (*  Title:      HOL/Wellfounded.thy
2     Author:     Tobias Nipkow
3     Author:     Lawrence C Paulson
4     Author:     Konrad Slind
5     Author:     Alexander Krauss
6 *)
8 header {*Well-founded Recursion*}
10 theory Wellfounded
11 imports Transitive_Closure
12 uses ("Tools/Function/size.ML")
13 begin
15 subsection {* Basic Definitions *}
17 definition wf :: "('a * 'a) set => bool" where
18   "wf(r) == (!P. (!x. (!y. (y,x):r --> P(y)) --> P(x)) --> (!x. P(x)))"
20 definition wfP :: "('a => 'a => bool) => bool" where
21   "wfP r == wf {(x, y). r x y}"
23 lemma wfP_wf_eq [pred_set_conv]: "wfP (\<lambda>x y. (x, y) \<in> r) = wf r"
24   by (simp add: wfP_def)
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
30 lemmas wfPUNIVI = wfUNIVI [to_pred]
32 text{*Restriction to domain @{term A} and range @{term B}.  If @{term r} is
33     well-founded over their intersection, then @{term "wf r"}*}
34 lemma wfI:
35  "[| r \<subseteq> A <*> B;
36      !!x P. [|\<forall>x. (\<forall>y. (y,x) : r --> P y) --> P x;  x : A; x : B |] ==> P x |]
37   ==>  wf r"
38   unfolding wf_def by blast
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
46 lemmas wfP_induct = wf_induct [to_pred]
48 lemmas wf_induct_rule = wf_induct [rule_format, consumes 1, case_names less, induct set: wf]
50 lemmas wfP_induct_rule = wf_induct_rule [to_pred, induct set: wfP]
52 lemma wf_not_sym: "wf r ==> (a, x) : r ==> (x, a) ~: r"
53   by (induct a arbitrary: x set: wf) blast
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])
60 lemma wf_not_refl [simp]: "wf r ==> (a, a) ~: r"
61   by (blast elim: wf_asym)
63 lemma wf_irrefl: assumes "wf r" obtains "(a, a) \<notin> r"
64 by (drule wf_not_refl[OF assms])
66 lemma wf_wellorderI:
67   assumes wf: "wf {(x::'a::ord, y). x < y}"
68   assumes lin: "OFCLASS('a::ord, linorder_class)"
69   shows "OFCLASS('a::ord, wellorder_class)"
70 using lin by (rule wellorder_class.intro)
71   (blast intro: class.wellorder_axioms.intro wf_induct_rule [OF wf])
73 lemma (in wellorder) wf:
74   "wf {(x, y). x < y}"
75 unfolding wf_def by (blast intro: less_induct)
78 subsection {* Basic Results *}
80 text {* Point-free characterization of well-foundedness *}
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
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
108 text{*Minimal-element characterization of well-foundedness*}
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
115 lemma wfI_min:
116   assumes a: "\<And>x Q. x \<in> Q \<Longrightarrow> \<exists>z\<in>Q. \<forall>y. (y, z) \<in> R \<longrightarrow> y \<notin> Q"
117   shows "wf R"
118 proof (rule wfI_pf)
119   fix A assume b: "A \<subseteq> R `` A"
120   { fix x assume "x \<in> A"
121     from a[OF this] b have "False" by blast
122   }
123   thus "A = {}" by blast
124 qed
126 lemma wf_eq_minimal: "wf r = (\<forall>Q x. x\<in>Q --> (\<exists>z\<in>Q. \<forall>y. (y,z)\<in>r --> y\<notin>Q))"
127 apply auto
128 apply (erule wfE_min, assumption, blast)
129 apply (rule wfI_min, auto)
130 done
132 lemmas wfP_eq_minimal = wf_eq_minimal [to_pred]
134 text{* Well-foundedness of transitive closure *}
136 lemma wf_trancl:
137   assumes "wf r"
138   shows "wf (r^+)"
139 proof -
140   {
141     fix P and x
142     assume induct_step: "!!x. (!!y. (y, x) : r^+ ==> P y) ==> P x"
143     have "P x"
144     proof (rule induct_step)
145       fix y assume "(y, x) : r^+"
146       with `wf r` show "P y"
147       proof (induct x arbitrary: y)
148         case (less x)
149         note hyp = `\<And>x' y'. (x', x) : r ==> (y', x') : r^+ ==> P y'`
150         from `(y, x) : r^+` show "P y"
151         proof cases
152           case base
153           show "P y"
154           proof (rule induct_step)
155             fix y' assume "(y', y) : r^+"
156             with `(y, x) : r` show "P y'" by (rule hyp [of y y'])
157           qed
158         next
159           case step
160           then obtain x' where "(x', x) : r" and "(y, x') : r^+" by simp
161           then show "P y" by (rule hyp [of x' y])
162         qed
163       qed
164     qed
165   } then show ?thesis unfolding wf_def by blast
166 qed
168 lemmas wfP_trancl = wf_trancl [to_pred]
170 lemma wf_converse_trancl: "wf (r^-1) ==> wf ((r^+)^-1)"
171   apply (subst trancl_converse [symmetric])
172   apply (erule wf_trancl)
173   done
175 text {* Well-foundedness of subsets *}
177 lemma wf_subset: "[| wf(r);  p<=r |] ==> wf(p)"
178   apply (simp (no_asm_use) add: wf_eq_minimal)
179   apply fast
180   done
182 lemmas wfP_subset = wf_subset [to_pred]
184 text {* Well-foundedness of the empty relation *}
186 lemma wf_empty [iff]: "wf {}"
187   by (simp add: wf_def)
189 lemma wfP_empty [iff]:
190   "wfP (\<lambda>x y. False)"
191 proof -
192   have "wfP bot" by (fact wf_empty [to_pred bot_empty_eq2])
193   then show ?thesis by (simp add: bot_fun_def)
194 qed
196 lemma wf_Int1: "wf r ==> wf (r Int r')"
197   apply (erule wf_subset)
198   apply (rule Int_lower1)
199   done
201 lemma wf_Int2: "wf r ==> wf (r' Int r)"
202   apply (erule wf_subset)
203   apply (rule Int_lower2)
204   done
206 text {* Exponentiation *}
208 lemma wf_exp:
209   assumes "wf (R ^^ n)"
210   shows "wf R"
211 proof (rule wfI_pf)
212   fix A assume "A \<subseteq> R `` A"
213   then have "A \<subseteq> (R ^^ n) `` A" by (induct n) force+
214   with `wf (R ^^ n)`
215   show "A = {}" by (rule wfE_pf)
216 qed
218 text {* Well-foundedness of insert *}
220 lemma wf_insert [iff]: "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)"
221 apply (rule iffI)
222  apply (blast elim: wf_trancl [THEN wf_irrefl]
223               intro: rtrancl_into_trancl1 wf_subset
224                      rtrancl_mono [THEN  rev_subsetD])
225 apply (simp add: wf_eq_minimal, safe)
226 apply (rule allE, assumption, erule impE, blast)
227 apply (erule bexE)
228 apply (rename_tac "a", case_tac "a = x")
229  prefer 2
230 apply blast
231 apply (case_tac "y:Q")
232  prefer 2 apply blast
233 apply (rule_tac x = "{z. z:Q & (z,y) : r^*}" in allE)
234  apply assumption
235 apply (erule_tac V = "ALL Q. (EX x. x : Q) --> ?P Q" in thin_rl)
236   --{*essential for speed*}
237 txt{*Blast with new substOccur fails*}
238 apply (fast intro: converse_rtrancl_into_rtrancl)
239 done
241 text{*Well-foundedness of image*}
243 lemma wf_map_pair_image: "[| wf r; inj f |] ==> wf(map_pair f f ` r)"
244 apply (simp only: wf_eq_minimal, clarify)
245 apply (case_tac "EX p. f p : Q")
246 apply (erule_tac x = "{p. f p : Q}" in allE)
247 apply (fast dest: inj_onD, blast)
248 done
251 subsection {* Well-Foundedness Results for Unions *}
253 lemma wf_union_compatible:
254   assumes "wf R" "wf S"
255   assumes "R O S \<subseteq> R"
256   shows "wf (R \<union> S)"
257 proof (rule wfI_min)
258   fix x :: 'a and Q
259   let ?Q' = "{x \<in> Q. \<forall>y. (y, x) \<in> R \<longrightarrow> y \<notin> Q}"
260   assume "x \<in> Q"
261   obtain a where "a \<in> ?Q'"
262     by (rule wfE_min [OF `wf R` `x \<in> Q`]) blast
263   with `wf S`
264   obtain z where "z \<in> ?Q'" and zmin: "\<And>y. (y, z) \<in> S \<Longrightarrow> y \<notin> ?Q'" by (erule wfE_min)
265   {
266     fix y assume "(y, z) \<in> S"
267     then have "y \<notin> ?Q'" by (rule zmin)
269     have "y \<notin> Q"
270     proof
271       assume "y \<in> Q"
272       with `y \<notin> ?Q'`
273       obtain w where "(w, y) \<in> R" and "w \<in> Q" by auto
274       from `(w, y) \<in> R` `(y, z) \<in> S` have "(w, z) \<in> R O S" by (rule rel_compI)
275       with `R O S \<subseteq> R` have "(w, z) \<in> R" ..
276       with `z \<in> ?Q'` have "w \<notin> Q" by blast
277       with `w \<in> Q` show False by contradiction
278     qed
279   }
280   with `z \<in> ?Q'` show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> R \<union> S \<longrightarrow> y \<notin> Q" by blast
281 qed
284 text {* Well-foundedness of indexed union with disjoint domains and ranges *}
286 lemma wf_UN: "[| ALL i:I. wf(r i);
287          ALL i:I. ALL j:I. r i ~= r j --> Domain(r i) Int Range(r j) = {}
288       |] ==> wf(UN i:I. r i)"
289 apply (simp only: wf_eq_minimal, clarify)
290 apply (rename_tac A a, case_tac "EX i:I. EX a:A. EX b:A. (b,a) : r i")
291  prefer 2
292  apply force
293 apply clarify
294 apply (drule bspec, assumption)
295 apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r i) }" in allE)
296 apply (blast elim!: allE)
297 done
299 lemma wfP_SUP:
300   "\<forall>i. wfP (r i) \<Longrightarrow> \<forall>i j. r i \<noteq> r j \<longrightarrow> inf (DomainP (r i)) (RangeP (r j)) = bot \<Longrightarrow> wfP (SUPR UNIV r)"
301   by (rule wf_UN [where I=UNIV and r="\<lambda>i. {(x, y). r i x y}", to_pred SUP_UN_eq2])
302     (simp_all add: Collect_def)
304 lemma wf_Union:
305  "[| ALL r:R. wf r;
306      ALL r:R. ALL s:R. r ~= s --> Domain r Int Range s = {}
307   |] ==> wf(Union R)"
308   using wf_UN[of R "\<lambda>i. i"] by (simp add: SUP_def)
310 (*Intuition: we find an (R u S)-min element of a nonempty subset A
311              by case distinction.
312   1. There is a step a -R-> b with a,b : A.
313      Pick an R-min element z of the (nonempty) set {a:A | EX b:A. a -R-> b}.
314      By definition, there is z':A s.t. z -R-> z'. Because z is R-min in the
315      subset, z' must be R-min in A. Because z' has an R-predecessor, it cannot
316      have an S-successor and is thus S-min in A as well.
317   2. There is no such step.
318      Pick an S-min element of A. In this case it must be an R-min
319      element of A as well.
321 *)
322 lemma wf_Un:
323      "[| wf r; wf s; Domain r Int Range s = {} |] ==> wf(r Un s)"
324   using wf_union_compatible[of s r]
325   by (auto simp: Un_ac)
327 lemma wf_union_merge:
328   "wf (R \<union> S) = wf (R O R \<union> S O R \<union> S)" (is "wf ?A = wf ?B")
329 proof
330   assume "wf ?A"
331   with wf_trancl have wfT: "wf (?A^+)" .
332   moreover have "?B \<subseteq> ?A^+"
333     by (subst trancl_unfold, subst trancl_unfold) blast
334   ultimately show "wf ?B" by (rule wf_subset)
335 next
336   assume "wf ?B"
338   show "wf ?A"
339   proof (rule wfI_min)
340     fix Q :: "'a set" and x
341     assume "x \<in> Q"
343     with `wf ?B`
344     obtain z where "z \<in> Q" and "\<And>y. (y, z) \<in> ?B \<Longrightarrow> y \<notin> Q"
345       by (erule wfE_min)
346     then have A1: "\<And>y. (y, z) \<in> R O R \<Longrightarrow> y \<notin> Q"
347       and A2: "\<And>y. (y, z) \<in> S O R \<Longrightarrow> y \<notin> Q"
348       and A3: "\<And>y. (y, z) \<in> S \<Longrightarrow> y \<notin> Q"
349       by auto
351     show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> ?A \<longrightarrow> y \<notin> Q"
352     proof (cases "\<forall>y. (y, z) \<in> R \<longrightarrow> y \<notin> Q")
353       case True
354       with `z \<in> Q` A3 show ?thesis by blast
355     next
356       case False
357       then obtain z' where "z'\<in>Q" "(z', z) \<in> R" by blast
359       have "\<forall>y. (y, z') \<in> ?A \<longrightarrow> y \<notin> Q"
360       proof (intro allI impI)
361         fix y assume "(y, z') \<in> ?A"
362         then show "y \<notin> Q"
363         proof
364           assume "(y, z') \<in> R"
365           then have "(y, z) \<in> R O R" using `(z', z) \<in> R` ..
366           with A1 show "y \<notin> Q" .
367         next
368           assume "(y, z') \<in> S"
369           then have "(y, z) \<in> S O R" using  `(z', z) \<in> R` ..
370           with A2 show "y \<notin> Q" .
371         qed
372       qed
373       with `z' \<in> Q` show ?thesis ..
374     qed
375   qed
376 qed
378 lemma wf_comp_self: "wf R = wf (R O R)"  -- {* special case *}
379   by (rule wf_union_merge [where S = "{}", simplified])
382 subsection {* Acyclic relations *}
384 definition acyclic :: "('a * 'a) set => bool" where
385   "acyclic r == !x. (x,x) ~: r^+"
387 abbreviation acyclicP :: "('a => 'a => bool) => bool" where
388   "acyclicP r == acyclic {(x, y). r x y}"
390 lemma acyclicI: "ALL x. (x, x) ~: r^+ ==> acyclic r"
391   by (simp add: acyclic_def)
393 lemma wf_acyclic: "wf r ==> acyclic r"
394 apply (simp add: acyclic_def)
395 apply (blast elim: wf_trancl [THEN wf_irrefl])
396 done
398 lemmas wfP_acyclicP = wf_acyclic [to_pred]
400 lemma acyclic_insert [iff]:
401      "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)"
402 apply (simp add: acyclic_def trancl_insert)
403 apply (blast intro: rtrancl_trans)
404 done
406 lemma acyclic_converse [iff]: "acyclic(r^-1) = acyclic r"
407 by (simp add: acyclic_def trancl_converse)
409 lemmas acyclicP_converse [iff] = acyclic_converse [to_pred]
411 lemma acyclic_impl_antisym_rtrancl: "acyclic r ==> antisym(r^*)"
412 apply (simp add: acyclic_def antisym_def)
413 apply (blast elim: rtranclE intro: rtrancl_into_trancl1 rtrancl_trancl_trancl)
414 done
416 (* Other direction:
417 acyclic = no loops
418 antisym = only self loops
419 Goalw [acyclic_def,antisym_def] "antisym( r^* ) ==> acyclic(r - Id)
420 ==> antisym( r^* ) = acyclic(r - Id)";
421 *)
423 lemma acyclic_subset: "[| acyclic s; r <= s |] ==> acyclic r"
424 apply (simp add: acyclic_def)
425 apply (blast intro: trancl_mono)
426 done
428 text{* Wellfoundedness of finite acyclic relations*}
430 lemma finite_acyclic_wf [rule_format]: "finite r ==> acyclic r --> wf r"
431 apply (erule finite_induct, blast)
432 apply (simp (no_asm_simp) only: split_tupled_all)
433 apply simp
434 done
436 lemma finite_acyclic_wf_converse: "[|finite r; acyclic r|] ==> wf (r^-1)"
437 apply (erule finite_converse [THEN iffD2, THEN finite_acyclic_wf])
438 apply (erule acyclic_converse [THEN iffD2])
439 done
441 lemma wf_iff_acyclic_if_finite: "finite r ==> wf r = acyclic r"
442 by (blast intro: finite_acyclic_wf wf_acyclic)
445 subsection {* @{typ nat} is well-founded *}
447 lemma less_nat_rel: "op < = (\<lambda>m n. n = Suc m)^++"
448 proof (rule ext, rule ext, rule iffI)
449   fix n m :: nat
450   assume "m < n"
451   then show "(\<lambda>m n. n = Suc m)^++ m n"
452   proof (induct n)
453     case 0 then show ?case by auto
454   next
455     case (Suc n) then show ?case
456       by (auto simp add: less_Suc_eq_le le_less intro: tranclp.trancl_into_trancl)
457   qed
458 next
459   fix n m :: nat
460   assume "(\<lambda>m n. n = Suc m)^++ m n"
461   then show "m < n"
462     by (induct n)
463       (simp_all add: less_Suc_eq_le reflexive le_less)
464 qed
466 definition
467   pred_nat :: "(nat * nat) set" where
468   "pred_nat = {(m, n). n = Suc m}"
470 definition
471   less_than :: "(nat * nat) set" where
472   "less_than = pred_nat^+"
474 lemma less_eq: "(m, n) \<in> pred_nat^+ \<longleftrightarrow> m < n"
475   unfolding less_nat_rel pred_nat_def trancl_def by simp
477 lemma pred_nat_trancl_eq_le:
478   "(m, n) \<in> pred_nat^* \<longleftrightarrow> m \<le> n"
479   unfolding less_eq rtrancl_eq_or_trancl by auto
481 lemma wf_pred_nat: "wf pred_nat"
482   apply (unfold wf_def pred_nat_def, clarify)
483   apply (induct_tac x, blast+)
484   done
486 lemma wf_less_than [iff]: "wf less_than"
487   by (simp add: less_than_def wf_pred_nat [THEN wf_trancl])
489 lemma trans_less_than [iff]: "trans less_than"
490   by (simp add: less_than_def)
492 lemma less_than_iff [iff]: "((x,y): less_than) = (x<y)"
493   by (simp add: less_than_def less_eq)
495 lemma wf_less: "wf {(x, y::nat). x < y}"
496   using wf_less_than by (simp add: less_than_def less_eq [symmetric])
499 subsection {* Accessible Part *}
501 text {*
502  Inductive definition of the accessible part @{term "acc r"} of a
504 *}
506 inductive_set
507   acc :: "('a * 'a) set => 'a set"
508   for r :: "('a * 'a) set"
509   where
510     accI: "(!!y. (y, x) : r ==> y : acc r) ==> x : acc r"
512 abbreviation
513   termip :: "('a => 'a => bool) => 'a => bool" where
514   "termip r == accp (r\<inverse>\<inverse>)"
516 abbreviation
517   termi :: "('a * 'a) set => 'a set" where
518   "termi r == acc (r\<inverse>)"
520 lemmas accpI = accp.accI
522 text {* Induction rules *}
524 theorem accp_induct:
525   assumes major: "accp r a"
526   assumes hyp: "!!x. accp r x ==> \<forall>y. r y x --> P y ==> P x"
527   shows "P a"
528   apply (rule major [THEN accp.induct])
529   apply (rule hyp)
530    apply (rule accp.accI)
531    apply fast
532   apply fast
533   done
535 theorems accp_induct_rule = accp_induct [rule_format, induct set: accp]
537 theorem accp_downward: "accp r b ==> r a b ==> accp r a"
538   apply (erule accp.cases)
539   apply fast
540   done
542 lemma not_accp_down:
543   assumes na: "\<not> accp R x"
544   obtains z where "R z x" and "\<not> accp R z"
545 proof -
546   assume a: "\<And>z. \<lbrakk>R z x; \<not> accp R z\<rbrakk> \<Longrightarrow> thesis"
548   show thesis
549   proof (cases "\<forall>z. R z x \<longrightarrow> accp R z")
550     case True
551     hence "\<And>z. R z x \<Longrightarrow> accp R z" by auto
552     hence "accp R x"
553       by (rule accp.accI)
554     with na show thesis ..
555   next
556     case False then obtain z where "R z x" and "\<not> accp R z"
557       by auto
558     with a show thesis .
559   qed
560 qed
562 lemma accp_downwards_aux: "r\<^sup>*\<^sup>* b a ==> accp r a --> accp r b"
563   apply (erule rtranclp_induct)
564    apply blast
565   apply (blast dest: accp_downward)
566   done
568 theorem accp_downwards: "accp r a ==> r\<^sup>*\<^sup>* b a ==> accp r b"
569   apply (blast dest: accp_downwards_aux)
570   done
572 theorem accp_wfPI: "\<forall>x. accp r x ==> wfP r"
573   apply (rule wfPUNIVI)
574   apply (rule_tac P=P in accp_induct)
575    apply blast
576   apply blast
577   done
579 theorem accp_wfPD: "wfP r ==> accp r x"
580   apply (erule wfP_induct_rule)
581   apply (rule accp.accI)
582   apply blast
583   done
585 theorem wfP_accp_iff: "wfP r = (\<forall>x. accp r x)"
586   apply (blast intro: accp_wfPI dest: accp_wfPD)
587   done
590 text {* Smaller relations have bigger accessible parts: *}
592 lemma accp_subset:
593   assumes sub: "R1 \<le> R2"
594   shows "accp R2 \<le> accp R1"
595 proof (rule predicate1I)
596   fix x assume "accp R2 x"
597   then show "accp R1 x"
598   proof (induct x)
599     fix x
600     assume ih: "\<And>y. R2 y x \<Longrightarrow> accp R1 y"
601     with sub show "accp R1 x"
602       by (blast intro: accp.accI)
603   qed
604 qed
607 text {* This is a generalized induction theorem that works on
608   subsets of the accessible part. *}
610 lemma accp_subset_induct:
611   assumes subset: "D \<le> accp R"
612     and dcl: "\<And>x z. \<lbrakk>D x; R z x\<rbrakk> \<Longrightarrow> D z"
613     and "D x"
614     and istep: "\<And>x. \<lbrakk>D x; (\<And>z. R z x \<Longrightarrow> P z)\<rbrakk> \<Longrightarrow> P x"
615   shows "P x"
616 proof -
617   from subset and `D x`
618   have "accp R x" ..
619   then show "P x" using `D x`
620   proof (induct x)
621     fix x
622     assume "D x"
623       and "\<And>y. R y x \<Longrightarrow> D y \<Longrightarrow> P y"
624     with dcl and istep show "P x" by blast
625   qed
626 qed
629 text {* Set versions of the above theorems *}
631 lemmas acc_induct = accp_induct [to_set]
633 lemmas acc_induct_rule = acc_induct [rule_format, induct set: acc]
635 lemmas acc_downward = accp_downward [to_set]
637 lemmas not_acc_down = not_accp_down [to_set]
639 lemmas acc_downwards_aux = accp_downwards_aux [to_set]
641 lemmas acc_downwards = accp_downwards [to_set]
643 lemmas acc_wfI = accp_wfPI [to_set]
645 lemmas acc_wfD = accp_wfPD [to_set]
647 lemmas wf_acc_iff = wfP_accp_iff [to_set]
649 lemmas acc_subset = accp_subset [to_set pred_subset_eq]
651 lemmas acc_subset_induct = accp_subset_induct [to_set pred_subset_eq]
654 subsection {* Tools for building wellfounded relations *}
656 text {* Inverse Image *}
658 lemma wf_inv_image [simp,intro!]: "wf(r) ==> wf(inv_image r (f::'a=>'b))"
659 apply (simp (no_asm_use) add: inv_image_def wf_eq_minimal)
660 apply clarify
661 apply (subgoal_tac "EX (w::'b) . w : {w. EX (x::'a) . x: Q & (f x = w) }")
662 prefer 2 apply (blast del: allE)
663 apply (erule allE)
664 apply (erule (1) notE impE)
665 apply blast
666 done
668 text {* Measure functions into @{typ nat} *}
670 definition measure :: "('a => nat) => ('a * 'a)set"
671 where "measure == inv_image less_than"
673 lemma in_measure[simp]: "((x,y) : measure f) = (f x < f y)"
674   by (simp add:measure_def)
676 lemma wf_measure [iff]: "wf (measure f)"
677 apply (unfold measure_def)
678 apply (rule wf_less_than [THEN wf_inv_image])
679 done
681 lemma wf_if_measure: fixes f :: "'a \<Rightarrow> nat"
682 shows "(!!x. P x \<Longrightarrow> f(g x) < f x) \<Longrightarrow> wf {(y,x). P x \<and> y = g x}"
683 apply(insert wf_measure[of f])
684 apply(simp only: measure_def inv_image_def less_than_def less_eq)
685 apply(erule wf_subset)
686 apply auto
687 done
690 text{* Lexicographic combinations *}
692 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
693   "ra <*lex*> rb = {((a, b), (a', b')). (a, a') \<in> ra \<or> a = a' \<and> (b, b') \<in> rb}"
695 lemma wf_lex_prod [intro!]: "[| wf(ra); wf(rb) |] ==> wf(ra <*lex*> rb)"
696 apply (unfold wf_def lex_prod_def)
697 apply (rule allI, rule impI)
698 apply (simp (no_asm_use) only: split_paired_All)
699 apply (drule spec, erule mp)
700 apply (rule allI, rule impI)
701 apply (drule spec, erule mp, blast)
702 done
704 lemma in_lex_prod[simp]:
705   "(((a,b),(a',b')): r <*lex*> s) = ((a,a'): r \<or> (a = a' \<and> (b, b') : s))"
706   by (auto simp:lex_prod_def)
708 text{* @{term "op <*lex*>"} preserves transitivity *}
710 lemma trans_lex_prod [intro!]:
711     "[| trans R1; trans R2 |] ==> trans (R1 <*lex*> R2)"
712 by (unfold trans_def lex_prod_def, blast)
714 text {* lexicographic combinations with measure functions *}
716 definition
717   mlex_prod :: "('a \<Rightarrow> nat) \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set" (infixr "<*mlex*>" 80)
718 where
719   "f <*mlex*> R = inv_image (less_than <*lex*> R) (%x. (f x, x))"
721 lemma wf_mlex: "wf R \<Longrightarrow> wf (f <*mlex*> R)"
722 unfolding mlex_prod_def
723 by auto
725 lemma mlex_less: "f x < f y \<Longrightarrow> (x, y) \<in> f <*mlex*> R"
726 unfolding mlex_prod_def by simp
728 lemma mlex_leq: "f x \<le> f y \<Longrightarrow> (x, y) \<in> R \<Longrightarrow> (x, y) \<in> f <*mlex*> R"
729 unfolding mlex_prod_def by auto
731 text {* proper subset relation on finite sets *}
733 definition finite_psubset  :: "('a set * 'a set) set"
734 where "finite_psubset == {(A,B). A < B & finite B}"
736 lemma wf_finite_psubset[simp]: "wf(finite_psubset)"
737 apply (unfold finite_psubset_def)
738 apply (rule wf_measure [THEN wf_subset])
739 apply (simp add: measure_def inv_image_def less_than_def less_eq)
740 apply (fast elim!: psubset_card_mono)
741 done
743 lemma trans_finite_psubset: "trans finite_psubset"
744 by (simp add: finite_psubset_def less_le trans_def, blast)
746 lemma in_finite_psubset[simp]: "(A, B) \<in> finite_psubset = (A < B & finite B)"
747 unfolding finite_psubset_def by auto
749 text {* max- and min-extension of order to finite sets *}
751 inductive_set max_ext :: "('a \<times> 'a) set \<Rightarrow> ('a set \<times> 'a set) set"
752 for R :: "('a \<times> 'a) set"
753 where
754   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"
756 lemma max_ext_wf:
757   assumes wf: "wf r"
758   shows "wf (max_ext r)"
759 proof (rule acc_wfI, intro allI)
760   fix M show "M \<in> acc (max_ext r)" (is "_ \<in> ?W")
761   proof cases
762     assume "finite M"
763     thus ?thesis
764     proof (induct M)
765       show "{} \<in> ?W"
766         by (rule accI) (auto elim: max_ext.cases)
767     next
768       fix M a assume "M \<in> ?W" "finite M"
769       with wf show "insert a M \<in> ?W"
770       proof (induct arbitrary: M)
771         fix M a
772         assume "M \<in> ?W"  and  [intro]: "finite M"
773         assume hyp: "\<And>b M. (b, a) \<in> r \<Longrightarrow> M \<in> ?W \<Longrightarrow> finite M \<Longrightarrow> insert b M \<in> ?W"
774         {
775           fix N M :: "'a set"
776           assume "finite N" "finite M"
777           then
778           have "\<lbrakk>M \<in> ?W ; (\<And>y. y \<in> N \<Longrightarrow> (y, a) \<in> r)\<rbrakk> \<Longrightarrow>  N \<union> M \<in> ?W"
779             by (induct N arbitrary: M) (auto simp: hyp)
780         }
781         note add_less = this
783         show "insert a M \<in> ?W"
784         proof (rule accI)
785           fix N assume Nless: "(N, insert a M) \<in> max_ext r"
786           hence asm1: "\<And>x. x \<in> N \<Longrightarrow> (x, a) \<in> r \<or> (\<exists>y \<in> M. (x, y) \<in> r)"
787             by (auto elim!: max_ext.cases)
789           let ?N1 = "{ n \<in> N. (n, a) \<in> r }"
790           let ?N2 = "{ n \<in> N. (n, a) \<notin> r }"
791           have N: "?N1 \<union> ?N2 = N" by (rule set_eqI) auto
792           from Nless have "finite N" by (auto elim: max_ext.cases)
793           then have finites: "finite ?N1" "finite ?N2" by auto
795           have "?N2 \<in> ?W"
796           proof cases
797             assume [simp]: "M = {}"
798             have Mw: "{} \<in> ?W" by (rule accI) (auto elim: max_ext.cases)
800             from asm1 have "?N2 = {}" by auto
801             with Mw show "?N2 \<in> ?W" by (simp only:)
802           next
803             assume "M \<noteq> {}"
804             have N2: "(?N2, M) \<in> max_ext r"
805               by (rule max_extI[OF _ _ `M \<noteq> {}`]) (insert asm1, auto intro: finites)
807             with `M \<in> ?W` show "?N2 \<in> ?W" by (rule acc_downward)
808           qed
809           with finites have "?N1 \<union> ?N2 \<in> ?W"
810             by (rule add_less) simp
811           then show "N \<in> ?W" by (simp only: N)
812         qed
813       qed
814     qed
815   next
816     assume [simp]: "\<not> finite M"
817     show ?thesis
818       by (rule accI) (auto elim: max_ext.cases)
819   qed
820 qed
823  "(A, B) \<in> max_ext R \<Longrightarrow> (C, D) \<in> max_ext R \<Longrightarrow>
824   (A \<union> C, B \<union> D) \<in> max_ext R"
825 by (force elim!: max_ext.cases)
828 definition min_ext :: "('a \<times> 'a) set \<Rightarrow> ('a set \<times> 'a set) set"  where
829   "min_ext r = {(X, Y) | X Y. X \<noteq> {} \<and> (\<forall>y \<in> Y. (\<exists>x \<in> X. (x, y) \<in> r))}"
831 lemma min_ext_wf:
832   assumes "wf r"
833   shows "wf (min_ext r)"
834 proof (rule wfI_min)
835   fix Q :: "'a set set"
836   fix x
837   assume nonempty: "x \<in> Q"
838   show "\<exists>m \<in> Q. (\<forall> n. (n, m) \<in> min_ext r \<longrightarrow> n \<notin> Q)"
839   proof cases
840     assume "Q = {{}}" thus ?thesis by (simp add: min_ext_def)
841   next
842     assume "Q \<noteq> {{}}"
843     with nonempty
844     obtain e x where "x \<in> Q" "e \<in> x" by force
845     then have eU: "e \<in> \<Union>Q" by auto
846     with `wf r`
847     obtain z where z: "z \<in> \<Union>Q" "\<And>y. (y, z) \<in> r \<Longrightarrow> y \<notin> \<Union>Q"
848       by (erule wfE_min)
849     from z obtain m where "m \<in> Q" "z \<in> m" by auto
850     from `m \<in> Q`
851     show ?thesis
852     proof (rule, intro bexI allI impI)
853       fix n
854       assume smaller: "(n, m) \<in> min_ext r"
855       with `z \<in> m` obtain y where y: "y \<in> n" "(y, z) \<in> r" by (auto simp: min_ext_def)
856       then show "n \<notin> Q" using z(2) by auto
857     qed
858   qed
859 qed
861 text{* Bounded increase must terminate: *}
863 lemma wf_bounded_measure:
864 fixes ub :: "'a \<Rightarrow> nat" and f :: "'a \<Rightarrow> nat"
865 assumes "!!a b. (b,a) : r \<Longrightarrow> ub b \<le> ub a & ub a \<ge> f b & f b > f a"
866 shows "wf r"
867 apply(rule wf_subset[OF wf_measure[of "%a. ub a - f a"]])
868 apply (auto dest: assms)
869 done
871 lemma wf_bounded_set:
872 fixes ub :: "'a \<Rightarrow> 'b set" and f :: "'a \<Rightarrow> 'b set"
873 assumes "!!a b. (b,a) : r \<Longrightarrow>
874   finite(ub a) & ub b \<subseteq> ub a & ub a \<supseteq> f b & f b \<supset> f a"
875 shows "wf r"
876 apply(rule wf_bounded_measure[of r "%a. card(ub a)" "%a. card(f a)"])
877 apply(drule assms)
878 apply (blast intro: card_mono finite_subset psubset_card_mono dest: psubset_eq[THEN iffD2])
879 done
882 subsection {* size of a datatype value *}
884 use "Tools/Function/size.ML"
886 setup Size.setup
888 lemma size_bool [code]:
889   "size (b\<Colon>bool) = 0" by (cases b) auto
891 lemma nat_size [simp, code]: "size (n\<Colon>nat) = n"
892   by (induct n) simp_all
894 declare "prod.size" [no_atp]
896 lemma [code]:
897   "size (P :: 'a Predicate.pred) = 0" by (cases P) simp
899 lemma [code]:
900   "pred_size f P = 0" by (cases P) simp
902 end