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