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