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