src/HOL/Wellfounded.thy
author haftmann
Tue Jul 28 13:37:08 2009 +0200 (2009-07-28)
changeset 32263 8bc0fd4a23a0
parent 32244 a99723d77ae0
child 32461 eee4fa79398f
permissions -rw-r--r--
explicit is better than implicit
     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 subsection {* Code generator setup *}
   468 
   469 consts_code
   470   "wfrec"   ("\<module>wfrec?")
   471 attach {*
   472 fun wfrec f x = f (wfrec f) x;
   473 *}
   474 
   475 
   476 subsection {* @{typ nat} is well-founded *}
   477 
   478 lemma less_nat_rel: "op < = (\<lambda>m n. n = Suc m)^++"
   479 proof (rule ext, rule ext, rule iffI)
   480   fix n m :: nat
   481   assume "m < n"
   482   then show "(\<lambda>m n. n = Suc m)^++ m n"
   483   proof (induct n)
   484     case 0 then show ?case by auto
   485   next
   486     case (Suc n) then show ?case
   487       by (auto simp add: less_Suc_eq_le le_less intro: tranclp.trancl_into_trancl)
   488   qed
   489 next
   490   fix n m :: nat
   491   assume "(\<lambda>m n. n = Suc m)^++ m n"
   492   then show "m < n"
   493     by (induct n)
   494       (simp_all add: less_Suc_eq_le reflexive le_less)
   495 qed
   496 
   497 definition
   498   pred_nat :: "(nat * nat) set" where
   499   "pred_nat = {(m, n). n = Suc m}"
   500 
   501 definition
   502   less_than :: "(nat * nat) set" where
   503   "less_than = pred_nat^+"
   504 
   505 lemma less_eq: "(m, n) \<in> pred_nat^+ \<longleftrightarrow> m < n"
   506   unfolding less_nat_rel pred_nat_def trancl_def by simp
   507 
   508 lemma pred_nat_trancl_eq_le:
   509   "(m, n) \<in> pred_nat^* \<longleftrightarrow> m \<le> n"
   510   unfolding less_eq rtrancl_eq_or_trancl by auto
   511 
   512 lemma wf_pred_nat: "wf pred_nat"
   513   apply (unfold wf_def pred_nat_def, clarify)
   514   apply (induct_tac x, blast+)
   515   done
   516 
   517 lemma wf_less_than [iff]: "wf less_than"
   518   by (simp add: less_than_def wf_pred_nat [THEN wf_trancl])
   519 
   520 lemma trans_less_than [iff]: "trans less_than"
   521   by (simp add: less_than_def trans_trancl)
   522 
   523 lemma less_than_iff [iff]: "((x,y): less_than) = (x<y)"
   524   by (simp add: less_than_def less_eq)
   525 
   526 lemma wf_less: "wf {(x, y::nat). x < y}"
   527   using wf_less_than by (simp add: less_than_def less_eq [symmetric])
   528 
   529 
   530 subsection {* Accessible Part *}
   531 
   532 text {*
   533  Inductive definition of the accessible part @{term "acc r"} of a
   534  relation; see also \cite{paulin-tlca}.
   535 *}
   536 
   537 inductive_set
   538   acc :: "('a * 'a) set => 'a set"
   539   for r :: "('a * 'a) set"
   540   where
   541     accI: "(!!y. (y, x) : r ==> y : acc r) ==> x : acc r"
   542 
   543 abbreviation
   544   termip :: "('a => 'a => bool) => 'a => bool" where
   545   "termip r == accp (r\<inverse>\<inverse>)"
   546 
   547 abbreviation
   548   termi :: "('a * 'a) set => 'a set" where
   549   "termi r == acc (r\<inverse>)"
   550 
   551 lemmas accpI = accp.accI
   552 
   553 text {* Induction rules *}
   554 
   555 theorem accp_induct:
   556   assumes major: "accp r a"
   557   assumes hyp: "!!x. accp r x ==> \<forall>y. r y x --> P y ==> P x"
   558   shows "P a"
   559   apply (rule major [THEN accp.induct])
   560   apply (rule hyp)
   561    apply (rule accp.accI)
   562    apply fast
   563   apply fast
   564   done
   565 
   566 theorems accp_induct_rule = accp_induct [rule_format, induct set: accp]
   567 
   568 theorem accp_downward: "accp r b ==> r a b ==> accp r a"
   569   apply (erule accp.cases)
   570   apply fast
   571   done
   572 
   573 lemma not_accp_down:
   574   assumes na: "\<not> accp R x"
   575   obtains z where "R z x" and "\<not> accp R z"
   576 proof -
   577   assume a: "\<And>z. \<lbrakk>R z x; \<not> accp R z\<rbrakk> \<Longrightarrow> thesis"
   578 
   579   show thesis
   580   proof (cases "\<forall>z. R z x \<longrightarrow> accp R z")
   581     case True
   582     hence "\<And>z. R z x \<Longrightarrow> accp R z" by auto
   583     hence "accp R x"
   584       by (rule accp.accI)
   585     with na show thesis ..
   586   next
   587     case False then obtain z where "R z x" and "\<not> accp R z"
   588       by auto
   589     with a show thesis .
   590   qed
   591 qed
   592 
   593 lemma accp_downwards_aux: "r\<^sup>*\<^sup>* b a ==> accp r a --> accp r b"
   594   apply (erule rtranclp_induct)
   595    apply blast
   596   apply (blast dest: accp_downward)
   597   done
   598 
   599 theorem accp_downwards: "accp r a ==> r\<^sup>*\<^sup>* b a ==> accp r b"
   600   apply (blast dest: accp_downwards_aux)
   601   done
   602 
   603 theorem accp_wfPI: "\<forall>x. accp r x ==> wfP r"
   604   apply (rule wfPUNIVI)
   605   apply (induct_tac P x rule: accp_induct)
   606    apply blast
   607   apply blast
   608   done
   609 
   610 theorem accp_wfPD: "wfP r ==> accp r x"
   611   apply (erule wfP_induct_rule)
   612   apply (rule accp.accI)
   613   apply blast
   614   done
   615 
   616 theorem wfP_accp_iff: "wfP r = (\<forall>x. accp r x)"
   617   apply (blast intro: accp_wfPI dest: accp_wfPD)
   618   done
   619 
   620 
   621 text {* Smaller relations have bigger accessible parts: *}
   622 
   623 lemma accp_subset:
   624   assumes sub: "R1 \<le> R2"
   625   shows "accp R2 \<le> accp R1"
   626 proof (rule predicate1I)
   627   fix x assume "accp R2 x"
   628   then show "accp R1 x"
   629   proof (induct x)
   630     fix x
   631     assume ih: "\<And>y. R2 y x \<Longrightarrow> accp R1 y"
   632     with sub show "accp R1 x"
   633       by (blast intro: accp.accI)
   634   qed
   635 qed
   636 
   637 
   638 text {* This is a generalized induction theorem that works on
   639   subsets of the accessible part. *}
   640 
   641 lemma accp_subset_induct:
   642   assumes subset: "D \<le> accp R"
   643     and dcl: "\<And>x z. \<lbrakk>D x; R z x\<rbrakk> \<Longrightarrow> D z"
   644     and "D x"
   645     and istep: "\<And>x. \<lbrakk>D x; (\<And>z. R z x \<Longrightarrow> P z)\<rbrakk> \<Longrightarrow> P x"
   646   shows "P x"
   647 proof -
   648   from subset and `D x`
   649   have "accp R x" ..
   650   then show "P x" using `D x`
   651   proof (induct x)
   652     fix x
   653     assume "D x"
   654       and "\<And>y. R y x \<Longrightarrow> D y \<Longrightarrow> P y"
   655     with dcl and istep show "P x" by blast
   656   qed
   657 qed
   658 
   659 
   660 text {* Set versions of the above theorems *}
   661 
   662 lemmas acc_induct = accp_induct [to_set]
   663 
   664 lemmas acc_induct_rule = acc_induct [rule_format, induct set: acc]
   665 
   666 lemmas acc_downward = accp_downward [to_set]
   667 
   668 lemmas not_acc_down = not_accp_down [to_set]
   669 
   670 lemmas acc_downwards_aux = accp_downwards_aux [to_set]
   671 
   672 lemmas acc_downwards = accp_downwards [to_set]
   673 
   674 lemmas acc_wfI = accp_wfPI [to_set]
   675 
   676 lemmas acc_wfD = accp_wfPD [to_set]
   677 
   678 lemmas wf_acc_iff = wfP_accp_iff [to_set]
   679 
   680 lemmas acc_subset = accp_subset [to_set pred_subset_eq]
   681 
   682 lemmas acc_subset_induct = accp_subset_induct [to_set pred_subset_eq]
   683 
   684 
   685 subsection {* Tools for building wellfounded relations *}
   686 
   687 text {* Inverse Image *}
   688 
   689 lemma wf_inv_image [simp,intro!]: "wf(r) ==> wf(inv_image r (f::'a=>'b))"
   690 apply (simp (no_asm_use) add: inv_image_def wf_eq_minimal)
   691 apply clarify
   692 apply (subgoal_tac "EX (w::'b) . w : {w. EX (x::'a) . x: Q & (f x = w) }")
   693 prefer 2 apply (blast del: allE)
   694 apply (erule allE)
   695 apply (erule (1) notE impE)
   696 apply blast
   697 done
   698 
   699 lemma in_inv_image[simp]: "((x,y) : inv_image r f) = ((f x, f y) : r)"
   700   by (auto simp:inv_image_def)
   701 
   702 text {* Measure Datatypes into @{typ nat} *}
   703 
   704 definition measure :: "('a => nat) => ('a * 'a)set"
   705 where "measure == inv_image less_than"
   706 
   707 lemma in_measure[simp]: "((x,y) : measure f) = (f x < f y)"
   708   by (simp add:measure_def)
   709 
   710 lemma wf_measure [iff]: "wf (measure f)"
   711 apply (unfold measure_def)
   712 apply (rule wf_less_than [THEN wf_inv_image])
   713 done
   714 
   715 text{* Lexicographic combinations *}
   716 
   717 definition
   718  lex_prod  :: "[('a*'a)set, ('b*'b)set] => (('a*'b)*('a*'b))set"
   719                (infixr "<*lex*>" 80)
   720 where
   721     "ra <*lex*> rb == {((a,b),(a',b')). (a,a') : ra | a=a' & (b,b') : rb}"
   722 
   723 lemma wf_lex_prod [intro!]: "[| wf(ra); wf(rb) |] ==> wf(ra <*lex*> rb)"
   724 apply (unfold wf_def lex_prod_def) 
   725 apply (rule allI, rule impI)
   726 apply (simp (no_asm_use) only: split_paired_All)
   727 apply (drule spec, erule mp) 
   728 apply (rule allI, rule impI)
   729 apply (drule spec, erule mp, blast) 
   730 done
   731 
   732 lemma in_lex_prod[simp]: 
   733   "(((a,b),(a',b')): r <*lex*> s) = ((a,a'): r \<or> (a = a' \<and> (b, b') : s))"
   734   by (auto simp:lex_prod_def)
   735 
   736 text{* @{term "op <*lex*>"} preserves transitivity *}
   737 
   738 lemma trans_lex_prod [intro!]: 
   739     "[| trans R1; trans R2 |] ==> trans (R1 <*lex*> R2)"
   740 by (unfold trans_def lex_prod_def, blast) 
   741 
   742 text {* lexicographic combinations with measure Datatypes *}
   743 
   744 definition 
   745   mlex_prod :: "('a \<Rightarrow> nat) \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set" (infixr "<*mlex*>" 80)
   746 where
   747   "f <*mlex*> R = inv_image (less_than <*lex*> R) (%x. (f x, x))"
   748 
   749 lemma wf_mlex: "wf R \<Longrightarrow> wf (f <*mlex*> R)"
   750 unfolding mlex_prod_def
   751 by auto
   752 
   753 lemma mlex_less: "f x < f y \<Longrightarrow> (x, y) \<in> f <*mlex*> R"
   754 unfolding mlex_prod_def by simp
   755 
   756 lemma mlex_leq: "f x \<le> f y \<Longrightarrow> (x, y) \<in> R \<Longrightarrow> (x, y) \<in> f <*mlex*> R"
   757 unfolding mlex_prod_def by auto
   758 
   759 text {* proper subset relation on finite sets *}
   760 
   761 definition finite_psubset  :: "('a set * 'a set) set"
   762 where "finite_psubset == {(A,B). A < B & finite B}"
   763 
   764 lemma wf_finite_psubset[simp]: "wf(finite_psubset)"
   765 apply (unfold finite_psubset_def)
   766 apply (rule wf_measure [THEN wf_subset])
   767 apply (simp add: measure_def inv_image_def less_than_def less_eq)
   768 apply (fast elim!: psubset_card_mono)
   769 done
   770 
   771 lemma trans_finite_psubset: "trans finite_psubset"
   772 by (simp add: finite_psubset_def less_le trans_def, blast)
   773 
   774 lemma in_finite_psubset[simp]: "(A, B) \<in> finite_psubset = (A < B & finite B)"
   775 unfolding finite_psubset_def by auto
   776 
   777 text {* max- and min-extension of order to finite sets *}
   778 
   779 inductive_set max_ext :: "('a \<times> 'a) set \<Rightarrow> ('a set \<times> 'a set) set" 
   780 for R :: "('a \<times> 'a) set"
   781 where
   782   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"
   783 
   784 lemma max_ext_wf:
   785   assumes wf: "wf r"
   786   shows "wf (max_ext r)"
   787 proof (rule acc_wfI, intro allI)
   788   fix M show "M \<in> acc (max_ext r)" (is "_ \<in> ?W")
   789   proof cases
   790     assume "finite M"
   791     thus ?thesis
   792     proof (induct M)
   793       show "{} \<in> ?W"
   794         by (rule accI) (auto elim: max_ext.cases)
   795     next
   796       fix M a assume "M \<in> ?W" "finite M"
   797       with wf show "insert a M \<in> ?W"
   798       proof (induct arbitrary: M)
   799         fix M a
   800         assume "M \<in> ?W"  and  [intro]: "finite M"
   801         assume hyp: "\<And>b M. (b, a) \<in> r \<Longrightarrow> M \<in> ?W \<Longrightarrow> finite M \<Longrightarrow> insert b M \<in> ?W"
   802         {
   803           fix N M :: "'a set"
   804           assume "finite N" "finite M"
   805           then
   806           have "\<lbrakk>M \<in> ?W ; (\<And>y. y \<in> N \<Longrightarrow> (y, a) \<in> r)\<rbrakk> \<Longrightarrow>  N \<union> M \<in> ?W"
   807             by (induct N arbitrary: M) (auto simp: hyp)
   808         }
   809         note add_less = this
   810         
   811         show "insert a M \<in> ?W"
   812         proof (rule accI)
   813           fix N assume Nless: "(N, insert a M) \<in> max_ext r"
   814           hence asm1: "\<And>x. x \<in> N \<Longrightarrow> (x, a) \<in> r \<or> (\<exists>y \<in> M. (x, y) \<in> r)"
   815             by (auto elim!: max_ext.cases)
   816 
   817           let ?N1 = "{ n \<in> N. (n, a) \<in> r }"
   818           let ?N2 = "{ n \<in> N. (n, a) \<notin> r }"
   819           have N: "?N1 \<union> ?N2 = N" by (rule set_ext) auto
   820           from Nless have "finite N" by (auto elim: max_ext.cases)
   821           then have finites: "finite ?N1" "finite ?N2" by auto
   822           
   823           have "?N2 \<in> ?W"
   824           proof cases
   825             assume [simp]: "M = {}"
   826             have Mw: "{} \<in> ?W" by (rule accI) (auto elim: max_ext.cases)
   827 
   828             from asm1 have "?N2 = {}" by auto
   829             with Mw show "?N2 \<in> ?W" by (simp only:)
   830           next
   831             assume "M \<noteq> {}"
   832             have N2: "(?N2, M) \<in> max_ext r" 
   833               by (rule max_extI[OF _ _ `M \<noteq> {}`]) (insert asm1, auto intro: finites)
   834             
   835             with `M \<in> ?W` show "?N2 \<in> ?W" by (rule acc_downward)
   836           qed
   837           with finites have "?N1 \<union> ?N2 \<in> ?W" 
   838             by (rule add_less) simp
   839           then show "N \<in> ?W" by (simp only: N)
   840         qed
   841       qed
   842     qed
   843   next
   844     assume [simp]: "\<not> finite M"
   845     show ?thesis
   846       by (rule accI) (auto elim: max_ext.cases)
   847   qed
   848 qed
   849 
   850 lemma max_ext_additive: 
   851  "(A, B) \<in> max_ext R \<Longrightarrow> (C, D) \<in> max_ext R \<Longrightarrow>
   852   (A \<union> C, B \<union> D) \<in> max_ext R"
   853 by (force elim!: max_ext.cases)
   854 
   855 
   856 definition
   857   min_ext :: "('a \<times> 'a) set \<Rightarrow> ('a set \<times> 'a set) set" 
   858 where
   859   [code del]: "min_ext r = {(X, Y) | X Y. X \<noteq> {} \<and> (\<forall>y \<in> Y. (\<exists>x \<in> X. (x, y) \<in> r))}"
   860 
   861 lemma min_ext_wf:
   862   assumes "wf r"
   863   shows "wf (min_ext r)"
   864 proof (rule wfI_min)
   865   fix Q :: "'a set set"
   866   fix x
   867   assume nonempty: "x \<in> Q"
   868   show "\<exists>m \<in> Q. (\<forall> n. (n, m) \<in> min_ext r \<longrightarrow> n \<notin> Q)"
   869   proof cases
   870     assume "Q = {{}}" thus ?thesis by (simp add: min_ext_def)
   871   next
   872     assume "Q \<noteq> {{}}"
   873     with nonempty
   874     obtain e x where "x \<in> Q" "e \<in> x" by force
   875     then have eU: "e \<in> \<Union>Q" by auto
   876     with `wf r` 
   877     obtain z where z: "z \<in> \<Union>Q" "\<And>y. (y, z) \<in> r \<Longrightarrow> y \<notin> \<Union>Q" 
   878       by (erule wfE_min)
   879     from z obtain m where "m \<in> Q" "z \<in> m" by auto
   880     from `m \<in> Q`
   881     show ?thesis
   882     proof (rule, intro bexI allI impI)
   883       fix n
   884       assume smaller: "(n, m) \<in> min_ext r"
   885       with `z \<in> m` obtain y where y: "y \<in> n" "(y, z) \<in> r" by (auto simp: min_ext_def)
   886       then show "n \<notin> Q" using z(2) by auto
   887     qed      
   888   qed
   889 qed
   890 
   891 
   892 subsection{*Weakly decreasing sequences (w.r.t. some well-founded order) 
   893    stabilize.*}
   894 
   895 text{*This material does not appear to be used any longer.*}
   896 
   897 lemma sequence_trans: "[| ALL i. (f (Suc i), f i) : r^* |] ==> (f (i+k), f i) : r^*"
   898 by (induct k) (auto intro: rtrancl_trans)
   899 
   900 lemma wf_weak_decr_stable: 
   901   assumes as: "ALL i. (f (Suc i), f i) : r^*" "wf (r^+)"
   902   shows "EX i. ALL k. f (i+k) = f i"
   903 proof -
   904   have lem: "!!x. [| ALL i. (f (Suc i), f i) : r^*; wf (r^+) |]  
   905       ==> ALL m. f m = x --> (EX i. ALL k. f (m+i+k) = f (m+i))"
   906   apply (erule wf_induct, clarify)
   907   apply (case_tac "EX j. (f (m+j), f m) : r^+")
   908    apply clarify
   909    apply (subgoal_tac "EX i. ALL k. f ((m+j) +i+k) = f ( (m+j) +i) ")
   910     apply clarify
   911     apply (rule_tac x = "j+i" in exI)
   912     apply (simp add: add_ac, blast)
   913   apply (rule_tac x = 0 in exI, clarsimp)
   914   apply (drule_tac i = m and k = k in sequence_trans)
   915   apply (blast elim: rtranclE dest: rtrancl_into_trancl1)
   916   done
   917 
   918   from lem[OF as, THEN spec, of 0, simplified] 
   919   show ?thesis by auto
   920 qed
   921 
   922 (* special case of the theorem above: <= *)
   923 lemma weak_decr_stable:
   924      "ALL i. f (Suc i) <= ((f i)::nat) ==> EX i. ALL k. f (i+k) = f i"
   925 apply (rule_tac r = pred_nat in wf_weak_decr_stable)
   926 apply (simp add: pred_nat_trancl_eq_le)
   927 apply (intro wf_trancl wf_pred_nat)
   928 done
   929 
   930 
   931 subsection {* size of a datatype value *}
   932 
   933 use "Tools/Function/size.ML"
   934 
   935 setup Size.setup
   936 
   937 lemma size_bool [code]:
   938   "size (b\<Colon>bool) = 0" by (cases b) auto
   939 
   940 lemma nat_size [simp, code]: "size (n\<Colon>nat) = n"
   941   by (induct n) simp_all
   942 
   943 declare "prod.size" [noatp]
   944 
   945 lemma [code]:
   946   "size (P :: 'a Predicate.pred) = 0" by (cases P) simp
   947 
   948 lemma [code]:
   949   "pred_size f P = 0" by (cases P) simp
   950 
   951 end