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