src/HOL/Wellfounded.thy
 author haftmann Tue Jun 23 12:09:30 2009 +0200 (2009-06-23) changeset 31775 2b04504fcb69 parent 30989 1f39aea228b0 child 32205 49db434c157f permissions -rw-r--r--
uniformly capitialized names for subdirectories
1 (*  Author:     Tobias Nipkow
2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
3     Author:     Konrad Slind, Alexander Krauss
4     Copyright   1992-2008  University of Cambridge and TU Muenchen
5 *)
7 header {*Well-founded Recursion*}
9 theory Wellfounded
10 imports Finite_Set Transitive_Closure
11 uses ("Tools/Function/size.ML")
12 begin
14 subsection {* Basic Definitions *}
16 inductive
17   wfrec_rel :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b => bool"
18   for R :: "('a * 'a) set"
19   and F :: "('a => 'b) => 'a => 'b"
20 where
21   wfrecI: "ALL z. (z, x) : R --> wfrec_rel R F z (g z) ==>
22             wfrec_rel R F x (F g x)"
24 constdefs
25   wf         :: "('a * 'a)set => bool"
26   "wf(r) == (!P. (!x. (!y. (y,x):r --> P(y)) --> P(x)) --> (!x. P(x)))"
28   wfP :: "('a => 'a => bool) => bool"
29   "wfP r == wf {(x, y). r x y}"
31   acyclic :: "('a*'a)set => bool"
32   "acyclic r == !x. (x,x) ~: r^+"
34   cut        :: "('a => 'b) => ('a * 'a)set => 'a => 'a => 'b"
35   "cut f r x == (%y. if (y,x):r then f y else undefined)"
37   adm_wf :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => bool"
38   "adm_wf R F == ALL f g x.
39      (ALL z. (z, x) : R --> f z = g z) --> F f x = F g x"
41   wfrec :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b"
42   [code del]: "wfrec R F == %x. THE y. wfrec_rel R (%f x. F (cut f R x) x) x y"
44 abbreviation acyclicP :: "('a => 'a => bool) => bool" where
45   "acyclicP r == acyclic {(x, y). r x y}"
47 lemma wfP_wf_eq [pred_set_conv]: "wfP (\<lambda>x y. (x, y) \<in> r) = wf r"
48   by (simp add: wfP_def)
50 lemma wfUNIVI:
51    "(!!P x. (ALL x. (ALL y. (y,x) : r --> P(y)) --> P(x)) ==> P(x)) ==> wf(r)"
52   unfolding wf_def by blast
54 lemmas wfPUNIVI = wfUNIVI [to_pred]
56 text{*Restriction to domain @{term A} and range @{term B}.  If @{term r} is
57     well-founded over their intersection, then @{term "wf r"}*}
58 lemma wfI:
59  "[| r \<subseteq> A <*> B;
60      !!x P. [|\<forall>x. (\<forall>y. (y,x) : r --> P y) --> P x;  x : A; x : B |] ==> P x |]
61   ==>  wf r"
62   unfolding wf_def by blast
64 lemma wf_induct:
65     "[| wf(r);
66         !!x.[| ALL y. (y,x): r --> P(y) |] ==> P(x)
67      |]  ==>  P(a)"
68   unfolding wf_def by blast
70 lemmas wfP_induct = wf_induct [to_pred]
72 lemmas wf_induct_rule = wf_induct [rule_format, consumes 1, case_names less, induct set: wf]
74 lemmas wfP_induct_rule = wf_induct_rule [to_pred, induct set: wfP]
76 lemma wf_not_sym: "wf r ==> (a, x) : r ==> (x, a) ~: r"
77   by (induct a arbitrary: x set: wf) blast
79 (* [| wf r;  ~Z ==> (a,x) : r;  (x,a) ~: r ==> Z |] ==> Z *)
80 lemmas wf_asym = wf_not_sym [elim_format]
82 lemma wf_not_refl [simp]: "wf r ==> (a, a) ~: r"
83   by (blast elim: wf_asym)
85 (* [| wf r;  (a,a) ~: r ==> PROP W |] ==> PROP W *)
86 lemmas wf_irrefl = wf_not_refl [elim_format]
88 lemma wf_wellorderI:
89   assumes wf: "wf {(x::'a::ord, y). x < y}"
90   assumes lin: "OFCLASS('a::ord, linorder_class)"
91   shows "OFCLASS('a::ord, wellorder_class)"
92 using lin by (rule wellorder_class.intro)
93   (blast intro: wellorder_axioms.intro wf_induct_rule [OF wf])
95 lemma (in wellorder) wf:
96   "wf {(x, y). x < y}"
97 unfolding wf_def by (blast intro: less_induct)
100 subsection {* Basic Results *}
102 text{*transitive closure of a well-founded relation is well-founded! *}
103 lemma wf_trancl:
104   assumes "wf r"
105   shows "wf (r^+)"
106 proof -
107   {
108     fix P and x
109     assume induct_step: "!!x. (!!y. (y, x) : r^+ ==> P y) ==> P x"
110     have "P x"
111     proof (rule induct_step)
112       fix y assume "(y, x) : r^+"
113       with `wf r` show "P y"
114       proof (induct x arbitrary: y)
115 	case (less x)
116 	note hyp = `\<And>x' y'. (x', x) : r ==> (y', x') : r^+ ==> P y'`
117 	from `(y, x) : r^+` show "P y"
118 	proof cases
119 	  case base
120 	  show "P y"
121 	  proof (rule induct_step)
122 	    fix y' assume "(y', y) : r^+"
123 	    with `(y, x) : r` show "P y'" by (rule hyp [of y y'])
124 	  qed
125 	next
126 	  case step
127 	  then obtain x' where "(x', x) : r" and "(y, x') : r^+" by simp
128 	  then show "P y" by (rule hyp [of x' y])
129 	qed
130       qed
131     qed
132   } then show ?thesis unfolding wf_def by blast
133 qed
135 lemmas wfP_trancl = wf_trancl [to_pred]
137 lemma wf_converse_trancl: "wf (r^-1) ==> wf ((r^+)^-1)"
138   apply (subst trancl_converse [symmetric])
139   apply (erule wf_trancl)
140   done
143 text{*Minimal-element characterization of well-foundedness*}
144 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))"
145 proof (intro iffI strip)
146   fix Q :: "'a set" and x
147   assume "wf r" and "x \<in> Q"
148   then show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> r \<longrightarrow> y \<notin> Q"
149     unfolding wf_def
150     by (blast dest: spec [of _ "%x. x\<in>Q \<longrightarrow> (\<exists>z\<in>Q. \<forall>y. (y,z) \<in> r \<longrightarrow> y\<notin>Q)"])
151 next
152   assume 1: "\<forall>Q x. x \<in> Q \<longrightarrow> (\<exists>z\<in>Q. \<forall>y. (y, z) \<in> r \<longrightarrow> y \<notin> Q)"
153   show "wf r"
154   proof (rule wfUNIVI)
155     fix P :: "'a \<Rightarrow> bool" and x
156     assume 2: "\<forall>x. (\<forall>y. (y, x) \<in> r \<longrightarrow> P y) \<longrightarrow> P x"
157     let ?Q = "{x. \<not> P x}"
158     have "x \<in> ?Q \<longrightarrow> (\<exists>z \<in> ?Q. \<forall>y. (y, z) \<in> r \<longrightarrow> y \<notin> ?Q)"
159       by (rule 1 [THEN spec, THEN spec])
160     then have "\<not> P x \<longrightarrow> (\<exists>z. \<not> P z \<and> (\<forall>y. (y, z) \<in> r \<longrightarrow> P y))" by simp
161     with 2 have "\<not> P x \<longrightarrow> (\<exists>z. \<not> P z \<and> P z)" by fast
162     then show "P x" by simp
163   qed
164 qed
166 lemma wfE_min:
167   assumes "wf R" "x \<in> Q"
168   obtains z where "z \<in> Q" "\<And>y. (y, z) \<in> R \<Longrightarrow> y \<notin> Q"
169   using assms unfolding wf_eq_minimal by blast
171 lemma wfI_min:
172   "(\<And>x Q. x \<in> Q \<Longrightarrow> \<exists>z\<in>Q. \<forall>y. (y, z) \<in> R \<longrightarrow> y \<notin> Q)
173   \<Longrightarrow> wf R"
174   unfolding wf_eq_minimal by blast
176 lemmas wfP_eq_minimal = wf_eq_minimal [to_pred]
178 text {* Well-foundedness of subsets *}
179 lemma wf_subset: "[| wf(r);  p<=r |] ==> wf(p)"
180   apply (simp (no_asm_use) add: wf_eq_minimal)
181   apply fast
182   done
184 lemmas wfP_subset = wf_subset [to_pred]
186 text {* Well-foundedness of the empty relation *}
187 lemma wf_empty [iff]: "wf({})"
188   by (simp add: wf_def)
190 lemmas wfP_empty [iff] =
191   wf_empty [to_pred bot_empty_eq2, simplified bot_fun_eq bot_bool_eq]
193 lemma wf_Int1: "wf r ==> wf (r Int r')"
194   apply (erule wf_subset)
195   apply (rule Int_lower1)
196   done
198 lemma wf_Int2: "wf r ==> wf (r' Int r)"
199   apply (erule wf_subset)
200   apply (rule Int_lower2)
201   done
203 text{*Well-foundedness of insert*}
204 lemma wf_insert [iff]: "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)"
205 apply (rule iffI)
206  apply (blast elim: wf_trancl [THEN wf_irrefl]
207               intro: rtrancl_into_trancl1 wf_subset
208                      rtrancl_mono [THEN  rev_subsetD])
209 apply (simp add: wf_eq_minimal, safe)
210 apply (rule allE, assumption, erule impE, blast)
211 apply (erule bexE)
212 apply (rename_tac "a", case_tac "a = x")
213  prefer 2
214 apply blast
215 apply (case_tac "y:Q")
216  prefer 2 apply blast
217 apply (rule_tac x = "{z. z:Q & (z,y) : r^*}" in allE)
218  apply assumption
219 apply (erule_tac V = "ALL Q. (EX x. x : Q) --> ?P Q" in thin_rl)
220   --{*essential for speed*}
221 txt{*Blast with new substOccur fails*}
222 apply (fast intro: converse_rtrancl_into_rtrancl)
223 done
225 text{*Well-foundedness of image*}
226 lemma wf_prod_fun_image: "[| wf r; inj f |] ==> wf(prod_fun f f ` r)"
227 apply (simp only: wf_eq_minimal, clarify)
228 apply (case_tac "EX p. f p : Q")
229 apply (erule_tac x = "{p. f p : Q}" in allE)
230 apply (fast dest: inj_onD, blast)
231 done
234 subsection {* Well-Foundedness Results for Unions *}
236 lemma wf_union_compatible:
237   assumes "wf R" "wf S"
238   assumes "S O R \<subseteq> R"
239   shows "wf (R \<union> S)"
240 proof (rule wfI_min)
241   fix x :: 'a and Q
242   let ?Q' = "{x \<in> Q. \<forall>y. (y, x) \<in> R \<longrightarrow> y \<notin> Q}"
243   assume "x \<in> Q"
244   obtain a where "a \<in> ?Q'"
245     by (rule wfE_min [OF `wf R` `x \<in> Q`]) blast
246   with `wf S`
247   obtain z where "z \<in> ?Q'" and zmin: "\<And>y. (y, z) \<in> S \<Longrightarrow> y \<notin> ?Q'" by (erule wfE_min)
248   {
249     fix y assume "(y, z) \<in> S"
250     then have "y \<notin> ?Q'" by (rule zmin)
252     have "y \<notin> Q"
253     proof
254       assume "y \<in> Q"
255       with `y \<notin> ?Q'`
256       obtain w where "(w, y) \<in> R" and "w \<in> Q" by auto
257       from `(w, y) \<in> R` `(y, z) \<in> S` have "(w, z) \<in> S O R" by (rule rel_compI)
258       with `S O R \<subseteq> R` have "(w, z) \<in> R" ..
259       with `z \<in> ?Q'` have "w \<notin> Q" by blast
260       with `w \<in> Q` show False by contradiction
261     qed
262   }
263   with `z \<in> ?Q'` show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> R \<union> S \<longrightarrow> y \<notin> Q" by blast
264 qed
267 text {* Well-foundedness of indexed union with disjoint domains and ranges *}
269 lemma wf_UN: "[| ALL i:I. wf(r i);
270          ALL i:I. ALL j:I. r i ~= r j --> Domain(r i) Int Range(r j) = {}
271       |] ==> wf(UN i:I. r i)"
272 apply (simp only: wf_eq_minimal, clarify)
273 apply (rename_tac A a, case_tac "EX i:I. EX a:A. EX b:A. (b,a) : r i")
274  prefer 2
275  apply force
276 apply clarify
277 apply (drule bspec, assumption)
278 apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r i) }" in allE)
279 apply (blast elim!: allE)
280 done
282 lemmas wfP_SUP = wf_UN [where I=UNIV and r="\<lambda>i. {(x, y). r i x y}",
283   to_pred SUP_UN_eq2 bot_empty_eq pred_equals_eq, simplified, standard]
285 lemma wf_Union:
286  "[| ALL r:R. wf r;
287      ALL r:R. ALL s:R. r ~= s --> Domain r Int Range s = {}
288   |] ==> wf(Union R)"
289 apply (simp add: Union_def)
290 apply (blast intro: wf_UN)
291 done
293 (*Intuition: we find an (R u S)-min element of a nonempty subset A
294              by case distinction.
295   1. There is a step a -R-> b with a,b : A.
296      Pick an R-min element z of the (nonempty) set {a:A | EX b:A. a -R-> b}.
297      By definition, there is z':A s.t. z -R-> z'. Because z is R-min in the
298      subset, z' must be R-min in A. Because z' has an R-predecessor, it cannot
299      have an S-successor and is thus S-min in A as well.
300   2. There is no such step.
301      Pick an S-min element of A. In this case it must be an R-min
302      element of A as well.
304 *)
305 lemma wf_Un:
306      "[| wf r; wf s; Domain r Int Range s = {} |] ==> wf(r Un s)"
307   using wf_union_compatible[of s r]
308   by (auto simp: Un_ac)
310 lemma wf_union_merge:
311   "wf (R \<union> S) = wf (R O R \<union> R O S \<union> S)" (is "wf ?A = wf ?B")
312 proof
313   assume "wf ?A"
314   with wf_trancl have wfT: "wf (?A^+)" .
315   moreover have "?B \<subseteq> ?A^+"
316     by (subst trancl_unfold, subst trancl_unfold) blast
317   ultimately show "wf ?B" by (rule wf_subset)
318 next
319   assume "wf ?B"
321   show "wf ?A"
322   proof (rule wfI_min)
323     fix Q :: "'a set" and x
324     assume "x \<in> Q"
326     with `wf ?B`
327     obtain z where "z \<in> Q" and "\<And>y. (y, z) \<in> ?B \<Longrightarrow> y \<notin> Q"
328       by (erule wfE_min)
329     then have A1: "\<And>y. (y, z) \<in> R O R \<Longrightarrow> y \<notin> Q"
330       and A2: "\<And>y. (y, z) \<in> R O S \<Longrightarrow> y \<notin> Q"
331       and A3: "\<And>y. (y, z) \<in> S \<Longrightarrow> y \<notin> Q"
332       by auto
334     show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> ?A \<longrightarrow> y \<notin> Q"
335     proof (cases "\<forall>y. (y, z) \<in> R \<longrightarrow> y \<notin> Q")
336       case True
337       with `z \<in> Q` A3 show ?thesis by blast
338     next
339       case False
340       then obtain z' where "z'\<in>Q" "(z', z) \<in> R" by blast
342       have "\<forall>y. (y, z') \<in> ?A \<longrightarrow> y \<notin> Q"
343       proof (intro allI impI)
344         fix y assume "(y, z') \<in> ?A"
345         then show "y \<notin> Q"
346         proof
347           assume "(y, z') \<in> R"
348           then have "(y, z) \<in> R O R" using `(z', z) \<in> R` ..
349           with A1 show "y \<notin> Q" .
350         next
351           assume "(y, z') \<in> S"
352           then have "(y, z) \<in> R O S" using  `(z', z) \<in> R` ..
353           with A2 show "y \<notin> Q" .
354         qed
355       qed
356       with `z' \<in> Q` show ?thesis ..
357     qed
358   qed
359 qed
361 lemma wf_comp_self: "wf R = wf (R O R)"  -- {* special case *}
362   by (rule wf_union_merge [where S = "{}", simplified])
365 subsubsection {* acyclic *}
367 lemma acyclicI: "ALL x. (x, x) ~: r^+ ==> acyclic r"
368   by (simp add: acyclic_def)
370 lemma wf_acyclic: "wf r ==> acyclic r"
371 apply (simp add: acyclic_def)
372 apply (blast elim: wf_trancl [THEN wf_irrefl])
373 done
375 lemmas wfP_acyclicP = wf_acyclic [to_pred]
377 lemma acyclic_insert [iff]:
378      "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)"
379 apply (simp add: acyclic_def trancl_insert)
380 apply (blast intro: rtrancl_trans)
381 done
383 lemma acyclic_converse [iff]: "acyclic(r^-1) = acyclic r"
384 by (simp add: acyclic_def trancl_converse)
386 lemmas acyclicP_converse [iff] = acyclic_converse [to_pred]
388 lemma acyclic_impl_antisym_rtrancl: "acyclic r ==> antisym(r^*)"
389 apply (simp add: acyclic_def antisym_def)
390 apply (blast elim: rtranclE intro: rtrancl_into_trancl1 rtrancl_trancl_trancl)
391 done
393 (* Other direction:
394 acyclic = no loops
395 antisym = only self loops
396 Goalw [acyclic_def,antisym_def] "antisym( r^* ) ==> acyclic(r - Id)
397 ==> antisym( r^* ) = acyclic(r - Id)";
398 *)
400 lemma acyclic_subset: "[| acyclic s; r <= s |] ==> acyclic r"
401 apply (simp add: acyclic_def)
402 apply (blast intro: trancl_mono)
403 done
405 text{* Wellfoundedness of finite acyclic relations*}
407 lemma finite_acyclic_wf [rule_format]: "finite r ==> acyclic r --> wf r"
408 apply (erule finite_induct, blast)
409 apply (simp (no_asm_simp) only: split_tupled_all)
410 apply simp
411 done
413 lemma finite_acyclic_wf_converse: "[|finite r; acyclic r|] ==> wf (r^-1)"
414 apply (erule finite_converse [THEN iffD2, THEN finite_acyclic_wf])
415 apply (erule acyclic_converse [THEN iffD2])
416 done
418 lemma wf_iff_acyclic_if_finite: "finite r ==> wf r = acyclic r"
419 by (blast intro: finite_acyclic_wf wf_acyclic)
422 subsection{*Well-Founded Recursion*}
424 text{*cut*}
426 lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))"
427 by (simp add: expand_fun_eq cut_def)
429 lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)"
430 by (simp add: cut_def)
432 text{*Inductive characterization of wfrec combinator; for details see:
433 John Harrison, "Inductive definitions: automation and application"*}
435 lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. wfrec_rel R F x y"
437 apply (erule_tac a=x in wf_induct)
438 apply (rule ex1I)
439 apply (rule_tac g = "%x. THE y. wfrec_rel R F x y" in wfrec_rel.wfrecI)
440 apply (fast dest!: theI')
441 apply (erule wfrec_rel.cases, simp)
442 apply (erule allE, erule allE, erule allE, erule mp)
443 apply (fast intro: the_equality [symmetric])
444 done
446 lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
448 apply (intro strip)
449 apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
450 apply (rule refl)
451 done
453 lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"
454 apply (simp add: wfrec_def)
455 apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
456 apply (rule wfrec_rel.wfrecI)
457 apply (intro strip)
458 apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
459 done
461 subsection {* Code generator setup *}
463 consts_code
464   "wfrec"   ("\<module>wfrec?")
465 attach {*
466 fun wfrec f x = f (wfrec f) x;
467 *}
470 subsection {* @{typ nat} is well-founded *}
472 lemma less_nat_rel: "op < = (\<lambda>m n. n = Suc m)^++"
473 proof (rule ext, rule ext, rule iffI)
474   fix n m :: nat
475   assume "m < n"
476   then show "(\<lambda>m n. n = Suc m)^++ m n"
477   proof (induct n)
478     case 0 then show ?case by auto
479   next
480     case (Suc n) then show ?case
481       by (auto simp add: less_Suc_eq_le le_less intro: tranclp.trancl_into_trancl)
482   qed
483 next
484   fix n m :: nat
485   assume "(\<lambda>m n. n = Suc m)^++ m n"
486   then show "m < n"
487     by (induct n)
488       (simp_all add: less_Suc_eq_le reflexive le_less)
489 qed
491 definition
492   pred_nat :: "(nat * nat) set" where
493   "pred_nat = {(m, n). n = Suc m}"
495 definition
496   less_than :: "(nat * nat) set" where
497   "less_than = pred_nat^+"
499 lemma less_eq: "(m, n) \<in> pred_nat^+ \<longleftrightarrow> m < n"
500   unfolding less_nat_rel pred_nat_def trancl_def by simp
502 lemma pred_nat_trancl_eq_le:
503   "(m, n) \<in> pred_nat^* \<longleftrightarrow> m \<le> n"
504   unfolding less_eq rtrancl_eq_or_trancl by auto
506 lemma wf_pred_nat: "wf pred_nat"
507   apply (unfold wf_def pred_nat_def, clarify)
508   apply (induct_tac x, blast+)
509   done
511 lemma wf_less_than [iff]: "wf less_than"
512   by (simp add: less_than_def wf_pred_nat [THEN wf_trancl])
514 lemma trans_less_than [iff]: "trans less_than"
515   by (simp add: less_than_def trans_trancl)
517 lemma less_than_iff [iff]: "((x,y): less_than) = (x<y)"
518   by (simp add: less_than_def less_eq)
520 lemma wf_less: "wf {(x, y::nat). x < y}"
521   using wf_less_than by (simp add: less_than_def less_eq [symmetric])
524 subsection {* Accessible Part *}
526 text {*
527  Inductive definition of the accessible part @{term "acc r"} of a
529 *}
531 inductive_set
532   acc :: "('a * 'a) set => 'a set"
533   for r :: "('a * 'a) set"
534   where
535     accI: "(!!y. (y, x) : r ==> y : acc r) ==> x : acc r"
537 abbreviation
538   termip :: "('a => 'a => bool) => 'a => bool" where
539   "termip r == accp (r\<inverse>\<inverse>)"
541 abbreviation
542   termi :: "('a * 'a) set => 'a set" where
543   "termi r == acc (r\<inverse>)"
545 lemmas accpI = accp.accI
547 text {* Induction rules *}
549 theorem accp_induct:
550   assumes major: "accp r a"
551   assumes hyp: "!!x. accp r x ==> \<forall>y. r y x --> P y ==> P x"
552   shows "P a"
553   apply (rule major [THEN accp.induct])
554   apply (rule hyp)
555    apply (rule accp.accI)
556    apply fast
557   apply fast
558   done
560 theorems accp_induct_rule = accp_induct [rule_format, induct set: accp]
562 theorem accp_downward: "accp r b ==> r a b ==> accp r a"
563   apply (erule accp.cases)
564   apply fast
565   done
567 lemma not_accp_down:
568   assumes na: "\<not> accp R x"
569   obtains z where "R z x" and "\<not> accp R z"
570 proof -
571   assume a: "\<And>z. \<lbrakk>R z x; \<not> accp R z\<rbrakk> \<Longrightarrow> thesis"
573   show thesis
574   proof (cases "\<forall>z. R z x \<longrightarrow> accp R z")
575     case True
576     hence "\<And>z. R z x \<Longrightarrow> accp R z" by auto
577     hence "accp R x"
578       by (rule accp.accI)
579     with na show thesis ..
580   next
581     case False then obtain z where "R z x" and "\<not> accp R z"
582       by auto
583     with a show thesis .
584   qed
585 qed
587 lemma accp_downwards_aux: "r\<^sup>*\<^sup>* b a ==> accp r a --> accp r b"
588   apply (erule rtranclp_induct)
589    apply blast
590   apply (blast dest: accp_downward)
591   done
593 theorem accp_downwards: "accp r a ==> r\<^sup>*\<^sup>* b a ==> accp r b"
594   apply (blast dest: accp_downwards_aux)
595   done
597 theorem accp_wfPI: "\<forall>x. accp r x ==> wfP r"
598   apply (rule wfPUNIVI)
599   apply (induct_tac P x rule: accp_induct)
600    apply blast
601   apply blast
602   done
604 theorem accp_wfPD: "wfP r ==> accp r x"
605   apply (erule wfP_induct_rule)
606   apply (rule accp.accI)
607   apply blast
608   done
610 theorem wfP_accp_iff: "wfP r = (\<forall>x. accp r x)"
611   apply (blast intro: accp_wfPI dest: accp_wfPD)
612   done
615 text {* Smaller relations have bigger accessible parts: *}
617 lemma accp_subset:
618   assumes sub: "R1 \<le> R2"
619   shows "accp R2 \<le> accp R1"
620 proof (rule predicate1I)
621   fix x assume "accp R2 x"
622   then show "accp R1 x"
623   proof (induct x)
624     fix x
625     assume ih: "\<And>y. R2 y x \<Longrightarrow> accp R1 y"
626     with sub show "accp R1 x"
627       by (blast intro: accp.accI)
628   qed
629 qed
632 text {* This is a generalized induction theorem that works on
633   subsets of the accessible part. *}
635 lemma accp_subset_induct:
636   assumes subset: "D \<le> accp R"
637     and dcl: "\<And>x z. \<lbrakk>D x; R z x\<rbrakk> \<Longrightarrow> D z"
638     and "D x"
639     and istep: "\<And>x. \<lbrakk>D x; (\<And>z. R z x \<Longrightarrow> P z)\<rbrakk> \<Longrightarrow> P x"
640   shows "P x"
641 proof -
642   from subset and `D x`
643   have "accp R x" ..
644   then show "P x" using `D x`
645   proof (induct x)
646     fix x
647     assume "D x"
648       and "\<And>y. R y x \<Longrightarrow> D y \<Longrightarrow> P y"
649     with dcl and istep show "P x" by blast
650   qed
651 qed
654 text {* Set versions of the above theorems *}
656 lemmas acc_induct = accp_induct [to_set]
658 lemmas acc_induct_rule = acc_induct [rule_format, induct set: acc]
660 lemmas acc_downward = accp_downward [to_set]
662 lemmas not_acc_down = not_accp_down [to_set]
664 lemmas acc_downwards_aux = accp_downwards_aux [to_set]
666 lemmas acc_downwards = accp_downwards [to_set]
668 lemmas acc_wfI = accp_wfPI [to_set]
670 lemmas acc_wfD = accp_wfPD [to_set]
672 lemmas wf_acc_iff = wfP_accp_iff [to_set]
674 lemmas acc_subset = accp_subset [to_set pred_subset_eq]
676 lemmas acc_subset_induct = accp_subset_induct [to_set pred_subset_eq]
679 subsection {* Tools for building wellfounded relations *}
681 text {* Inverse Image *}
683 lemma wf_inv_image [simp,intro!]: "wf(r) ==> wf(inv_image r (f::'a=>'b))"
684 apply (simp (no_asm_use) add: inv_image_def wf_eq_minimal)
685 apply clarify
686 apply (subgoal_tac "EX (w::'b) . w : {w. EX (x::'a) . x: Q & (f x = w) }")
687 prefer 2 apply (blast del: allE)
688 apply (erule allE)
689 apply (erule (1) notE impE)
690 apply blast
691 done
693 lemma in_inv_image[simp]: "((x,y) : inv_image r f) = ((f x, f y) : r)"
694   by (auto simp:inv_image_def)
696 text {* Measure Datatypes into @{typ nat} *}
698 definition measure :: "('a => nat) => ('a * 'a)set"
699 where "measure == inv_image less_than"
701 lemma in_measure[simp]: "((x,y) : measure f) = (f x < f y)"
702   by (simp add:measure_def)
704 lemma wf_measure [iff]: "wf (measure f)"
705 apply (unfold measure_def)
706 apply (rule wf_less_than [THEN wf_inv_image])
707 done
709 text{* Lexicographic combinations *}
711 definition
712  lex_prod  :: "[('a*'a)set, ('b*'b)set] => (('a*'b)*('a*'b))set"
713                (infixr "<*lex*>" 80)
714 where
715     "ra <*lex*> rb == {((a,b),(a',b')). (a,a') : ra | a=a' & (b,b') : rb}"
717 lemma wf_lex_prod [intro!]: "[| wf(ra); wf(rb) |] ==> wf(ra <*lex*> rb)"
718 apply (unfold wf_def lex_prod_def)
719 apply (rule allI, rule impI)
720 apply (simp (no_asm_use) only: split_paired_All)
721 apply (drule spec, erule mp)
722 apply (rule allI, rule impI)
723 apply (drule spec, erule mp, blast)
724 done
726 lemma in_lex_prod[simp]:
727   "(((a,b),(a',b')): r <*lex*> s) = ((a,a'): r \<or> (a = a' \<and> (b, b') : s))"
728   by (auto simp:lex_prod_def)
730 text{* @{term "op <*lex*>"} preserves transitivity *}
732 lemma trans_lex_prod [intro!]:
733     "[| trans R1; trans R2 |] ==> trans (R1 <*lex*> R2)"
734 by (unfold trans_def lex_prod_def, blast)
736 text {* lexicographic combinations with measure Datatypes *}
738 definition
739   mlex_prod :: "('a \<Rightarrow> nat) \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set" (infixr "<*mlex*>" 80)
740 where
741   "f <*mlex*> R = inv_image (less_than <*lex*> R) (%x. (f x, x))"
743 lemma wf_mlex: "wf R \<Longrightarrow> wf (f <*mlex*> R)"
744 unfolding mlex_prod_def
745 by auto
747 lemma mlex_less: "f x < f y \<Longrightarrow> (x, y) \<in> f <*mlex*> R"
748 unfolding mlex_prod_def by simp
750 lemma mlex_leq: "f x \<le> f y \<Longrightarrow> (x, y) \<in> R \<Longrightarrow> (x, y) \<in> f <*mlex*> R"
751 unfolding mlex_prod_def by auto
753 text {* proper subset relation on finite sets *}
755 definition finite_psubset  :: "('a set * 'a set) set"
756 where "finite_psubset == {(A,B). A < B & finite B}"
758 lemma wf_finite_psubset[simp]: "wf(finite_psubset)"
759 apply (unfold finite_psubset_def)
760 apply (rule wf_measure [THEN wf_subset])
761 apply (simp add: measure_def inv_image_def less_than_def less_eq)
762 apply (fast elim!: psubset_card_mono)
763 done
765 lemma trans_finite_psubset: "trans finite_psubset"
766 by (simp add: finite_psubset_def less_le trans_def, blast)
768 lemma in_finite_psubset[simp]: "(A, B) \<in> finite_psubset = (A < B & finite B)"
769 unfolding finite_psubset_def by auto
771 text {* max- and min-extension of order to finite sets *}
773 inductive_set max_ext :: "('a \<times> 'a) set \<Rightarrow> ('a set \<times> 'a set) set"
774 for R :: "('a \<times> 'a) set"
775 where
776   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"
778 lemma max_ext_wf:
779   assumes wf: "wf r"
780   shows "wf (max_ext r)"
781 proof (rule acc_wfI, intro allI)
782   fix M show "M \<in> acc (max_ext r)" (is "_ \<in> ?W")
783   proof cases
784     assume "finite M"
785     thus ?thesis
786     proof (induct M)
787       show "{} \<in> ?W"
788         by (rule accI) (auto elim: max_ext.cases)
789     next
790       fix M a assume "M \<in> ?W" "finite M"
791       with wf show "insert a M \<in> ?W"
792       proof (induct arbitrary: M)
793         fix M a
794         assume "M \<in> ?W"  and  [intro]: "finite M"
795         assume hyp: "\<And>b M. (b, a) \<in> r \<Longrightarrow> M \<in> ?W \<Longrightarrow> finite M \<Longrightarrow> insert b M \<in> ?W"
796         {
797           fix N M :: "'a set"
798           assume "finite N" "finite M"
799           then
800           have "\<lbrakk>M \<in> ?W ; (\<And>y. y \<in> N \<Longrightarrow> (y, a) \<in> r)\<rbrakk> \<Longrightarrow>  N \<union> M \<in> ?W"
801             by (induct N arbitrary: M) (auto simp: hyp)
802         }
803         note add_less = this
805         show "insert a M \<in> ?W"
806         proof (rule accI)
807           fix N assume Nless: "(N, insert a M) \<in> max_ext r"
808           hence asm1: "\<And>x. x \<in> N \<Longrightarrow> (x, a) \<in> r \<or> (\<exists>y \<in> M. (x, y) \<in> r)"
809             by (auto elim!: max_ext.cases)
811           let ?N1 = "{ n \<in> N. (n, a) \<in> r }"
812           let ?N2 = "{ n \<in> N. (n, a) \<notin> r }"
813           have N: "?N1 \<union> ?N2 = N" by (rule set_ext) auto
814           from Nless have "finite N" by (auto elim: max_ext.cases)
815           then have finites: "finite ?N1" "finite ?N2" by auto
817           have "?N2 \<in> ?W"
818           proof cases
819             assume [simp]: "M = {}"
820             have Mw: "{} \<in> ?W" by (rule accI) (auto elim: max_ext.cases)
822             from asm1 have "?N2 = {}" by auto
823             with Mw show "?N2 \<in> ?W" by (simp only:)
824           next
825             assume "M \<noteq> {}"
826             have N2: "(?N2, M) \<in> max_ext r"
827               by (rule max_extI[OF _ _ `M \<noteq> {}`]) (insert asm1, auto intro: finites)
829             with `M \<in> ?W` show "?N2 \<in> ?W" by (rule acc_downward)
830           qed
831           with finites have "?N1 \<union> ?N2 \<in> ?W"
832             by (rule add_less) simp
833           then show "N \<in> ?W" by (simp only: N)
834         qed
835       qed
836     qed
837   next
838     assume [simp]: "\<not> finite M"
839     show ?thesis
840       by (rule accI) (auto elim: max_ext.cases)
841   qed
842 qed
845  "(A, B) \<in> max_ext R \<Longrightarrow> (C, D) \<in> max_ext R \<Longrightarrow>
846   (A \<union> C, B \<union> D) \<in> max_ext R"
847 by (force elim!: max_ext.cases)
850 definition
851   min_ext :: "('a \<times> 'a) set \<Rightarrow> ('a set \<times> 'a set) set"
852 where
853   [code del]: "min_ext r = {(X, Y) | X Y. X \<noteq> {} \<and> (\<forall>y \<in> Y. (\<exists>x \<in> X. (x, y) \<in> r))}"
855 lemma min_ext_wf:
856   assumes "wf r"
857   shows "wf (min_ext r)"
858 proof (rule wfI_min)
859   fix Q :: "'a set set"
860   fix x
861   assume nonempty: "x \<in> Q"
862   show "\<exists>m \<in> Q. (\<forall> n. (n, m) \<in> min_ext r \<longrightarrow> n \<notin> Q)"
863   proof cases
864     assume "Q = {{}}" thus ?thesis by (simp add: min_ext_def)
865   next
866     assume "Q \<noteq> {{}}"
867     with nonempty
868     obtain e x where "x \<in> Q" "e \<in> x" by force
869     then have eU: "e \<in> \<Union>Q" by auto
870     with `wf r`
871     obtain z where z: "z \<in> \<Union>Q" "\<And>y. (y, z) \<in> r \<Longrightarrow> y \<notin> \<Union>Q"
872       by (erule wfE_min)
873     from z obtain m where "m \<in> Q" "z \<in> m" by auto
874     from `m \<in> Q`
875     show ?thesis
876     proof (rule, intro bexI allI impI)
877       fix n
878       assume smaller: "(n, m) \<in> min_ext r"
879       with `z \<in> m` obtain y where y: "y \<in> n" "(y, z) \<in> r" by (auto simp: min_ext_def)
880       then show "n \<notin> Q" using z(2) by auto
881     qed
882   qed
883 qed
885 text {*Wellfoundedness of @{text same_fst}*}
887 definition
888  same_fst :: "('a => bool) => ('a => ('b * 'b)set) => (('a*'b)*('a*'b))set"
889 where
890     "same_fst P R == {((x',y'),(x,y)) . x'=x & P x & (y',y) : R x}"
891    --{*For @{text rec_def} declarations where the first n parameters
892        stay unchanged in the recursive call. *}
894 lemma same_fstI [intro!]:
895      "[| P x; (y',y) : R x |] ==> ((x,y'),(x,y)) : same_fst P R"
896 by (simp add: same_fst_def)
898 lemma wf_same_fst:
899   assumes prem: "(!!x. P x ==> wf(R x))"
900   shows "wf(same_fst P R)"
901 apply (simp cong del: imp_cong add: wf_def same_fst_def)
902 apply (intro strip)
903 apply (rename_tac a b)
904 apply (case_tac "wf (R a)")
905  apply (erule_tac a = b in wf_induct, blast)
906 apply (blast intro: prem)
907 done
910 subsection{*Weakly decreasing sequences (w.r.t. some well-founded order)
911    stabilize.*}
913 text{*This material does not appear to be used any longer.*}
915 lemma sequence_trans: "[| ALL i. (f (Suc i), f i) : r^* |] ==> (f (i+k), f i) : r^*"
916 by (induct k) (auto intro: rtrancl_trans)
918 lemma wf_weak_decr_stable:
919   assumes as: "ALL i. (f (Suc i), f i) : r^*" "wf (r^+)"
920   shows "EX i. ALL k. f (i+k) = f i"
921 proof -
922   have lem: "!!x. [| ALL i. (f (Suc i), f i) : r^*; wf (r^+) |]
923       ==> ALL m. f m = x --> (EX i. ALL k. f (m+i+k) = f (m+i))"
924   apply (erule wf_induct, clarify)
925   apply (case_tac "EX j. (f (m+j), f m) : r^+")
926    apply clarify
927    apply (subgoal_tac "EX i. ALL k. f ((m+j) +i+k) = f ( (m+j) +i) ")
928     apply clarify
929     apply (rule_tac x = "j+i" in exI)
931   apply (rule_tac x = 0 in exI, clarsimp)
932   apply (drule_tac i = m and k = k in sequence_trans)
933   apply (blast elim: rtranclE dest: rtrancl_into_trancl1)
934   done
936   from lem[OF as, THEN spec, of 0, simplified]
937   show ?thesis by auto
938 qed
940 (* special case of the theorem above: <= *)
941 lemma weak_decr_stable:
942      "ALL i. f (Suc i) <= ((f i)::nat) ==> EX i. ALL k. f (i+k) = f i"
943 apply (rule_tac r = pred_nat in wf_weak_decr_stable)
944 apply (simp add: pred_nat_trancl_eq_le)
945 apply (intro wf_trancl wf_pred_nat)
946 done
949 subsection {* size of a datatype value *}
951 use "Tools/Function/size.ML"
953 setup Size.setup
955 lemma size_bool [code]:
956   "size (b\<Colon>bool) = 0" by (cases b) auto
958 lemma nat_size [simp, code]: "size (n\<Colon>nat) = n"
959   by (induct n) simp_all
961 declare "prod.size" [noatp]
963 lemma [code]:
964   "size (P :: 'a Predicate.pred) = 0" by (cases P) simp
966 lemma [code]:
967   "pred_size f P = 0" by (cases P) simp
969 end