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