src/HOL/Wellfounded.thy
author krauss
Tue Jul 28 08:48:56 2009 +0200 (2009-07-28)
changeset 32244 a99723d77ae0
parent 32235 8f9b8d14fc9f
child 32263 8bc0fd4a23a0
permissions -rw-r--r--
moved obsolete same_fst to Recdef.thy
     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 lemmas wfP_SUP = wf_UN [where I=UNIV and r="\<lambda>i. {(x, y). r i x y}",
   287   to_pred SUP_UN_eq2 bot_empty_eq pred_equals_eq, simplified, standard]
   288 
   289 lemma wf_Union: 
   290  "[| ALL r:R. wf r;  
   291      ALL r:R. ALL s:R. r ~= s --> Domain r Int Range s = {}  
   292   |] ==> wf(Union R)"
   293 apply (simp add: Union_def)
   294 apply (blast intro: wf_UN)
   295 done
   296 
   297 (*Intuition: we find an (R u S)-min element of a nonempty subset A
   298              by case distinction.
   299   1. There is a step a -R-> b with a,b : A.
   300      Pick an R-min element z of the (nonempty) set {a:A | EX b:A. a -R-> b}.
   301      By definition, there is z':A s.t. z -R-> z'. Because z is R-min in the
   302      subset, z' must be R-min in A. Because z' has an R-predecessor, it cannot
   303      have an S-successor and is thus S-min in A as well.
   304   2. There is no such step.
   305      Pick an S-min element of A. In this case it must be an R-min
   306      element of A as well.
   307 
   308 *)
   309 lemma wf_Un:
   310      "[| wf r; wf s; Domain r Int Range s = {} |] ==> wf(r Un s)"
   311   using wf_union_compatible[of s r] 
   312   by (auto simp: Un_ac)
   313 
   314 lemma wf_union_merge: 
   315   "wf (R \<union> S) = wf (R O R \<union> S O R \<union> S)" (is "wf ?A = wf ?B")
   316 proof
   317   assume "wf ?A"
   318   with wf_trancl have wfT: "wf (?A^+)" .
   319   moreover have "?B \<subseteq> ?A^+"
   320     by (subst trancl_unfold, subst trancl_unfold) blast
   321   ultimately show "wf ?B" by (rule wf_subset)
   322 next
   323   assume "wf ?B"
   324 
   325   show "wf ?A"
   326   proof (rule wfI_min)
   327     fix Q :: "'a set" and x 
   328     assume "x \<in> Q"
   329 
   330     with `wf ?B`
   331     obtain z where "z \<in> Q" and "\<And>y. (y, z) \<in> ?B \<Longrightarrow> y \<notin> Q" 
   332       by (erule wfE_min)
   333     then have A1: "\<And>y. (y, z) \<in> R O R \<Longrightarrow> y \<notin> Q"
   334       and A2: "\<And>y. (y, z) \<in> S O R \<Longrightarrow> y \<notin> Q"
   335       and A3: "\<And>y. (y, z) \<in> S \<Longrightarrow> y \<notin> Q"
   336       by auto
   337     
   338     show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> ?A \<longrightarrow> y \<notin> Q"
   339     proof (cases "\<forall>y. (y, z) \<in> R \<longrightarrow> y \<notin> Q")
   340       case True
   341       with `z \<in> Q` A3 show ?thesis by blast
   342     next
   343       case False 
   344       then obtain z' where "z'\<in>Q" "(z', z) \<in> R" by blast
   345 
   346       have "\<forall>y. (y, z') \<in> ?A \<longrightarrow> y \<notin> Q"
   347       proof (intro allI impI)
   348         fix y assume "(y, z') \<in> ?A"
   349         then show "y \<notin> Q"
   350         proof
   351           assume "(y, z') \<in> R" 
   352           then have "(y, z) \<in> R O R" using `(z', z) \<in> R` ..
   353           with A1 show "y \<notin> Q" .
   354         next
   355           assume "(y, z') \<in> S" 
   356           then have "(y, z) \<in> S O R" using  `(z', z) \<in> R` ..
   357           with A2 show "y \<notin> Q" .
   358         qed
   359       qed
   360       with `z' \<in> Q` show ?thesis ..
   361     qed
   362   qed
   363 qed
   364 
   365 lemma wf_comp_self: "wf R = wf (R O R)"  -- {* special case *}
   366   by (rule wf_union_merge [where S = "{}", simplified])
   367 
   368 
   369 subsubsection {* acyclic *}
   370 
   371 lemma acyclicI: "ALL x. (x, x) ~: r^+ ==> acyclic r"
   372   by (simp add: acyclic_def)
   373 
   374 lemma wf_acyclic: "wf r ==> acyclic r"
   375 apply (simp add: acyclic_def)
   376 apply (blast elim: wf_trancl [THEN wf_irrefl])
   377 done
   378 
   379 lemmas wfP_acyclicP = wf_acyclic [to_pred]
   380 
   381 lemma acyclic_insert [iff]:
   382      "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)"
   383 apply (simp add: acyclic_def trancl_insert)
   384 apply (blast intro: rtrancl_trans)
   385 done
   386 
   387 lemma acyclic_converse [iff]: "acyclic(r^-1) = acyclic r"
   388 by (simp add: acyclic_def trancl_converse)
   389 
   390 lemmas acyclicP_converse [iff] = acyclic_converse [to_pred]
   391 
   392 lemma acyclic_impl_antisym_rtrancl: "acyclic r ==> antisym(r^*)"
   393 apply (simp add: acyclic_def antisym_def)
   394 apply (blast elim: rtranclE intro: rtrancl_into_trancl1 rtrancl_trancl_trancl)
   395 done
   396 
   397 (* Other direction:
   398 acyclic = no loops
   399 antisym = only self loops
   400 Goalw [acyclic_def,antisym_def] "antisym( r^* ) ==> acyclic(r - Id)
   401 ==> antisym( r^* ) = acyclic(r - Id)";
   402 *)
   403 
   404 lemma acyclic_subset: "[| acyclic s; r <= s |] ==> acyclic r"
   405 apply (simp add: acyclic_def)
   406 apply (blast intro: trancl_mono)
   407 done
   408 
   409 text{* Wellfoundedness of finite acyclic relations*}
   410 
   411 lemma finite_acyclic_wf [rule_format]: "finite r ==> acyclic r --> wf r"
   412 apply (erule finite_induct, blast)
   413 apply (simp (no_asm_simp) only: split_tupled_all)
   414 apply simp
   415 done
   416 
   417 lemma finite_acyclic_wf_converse: "[|finite r; acyclic r|] ==> wf (r^-1)"
   418 apply (erule finite_converse [THEN iffD2, THEN finite_acyclic_wf])
   419 apply (erule acyclic_converse [THEN iffD2])
   420 done
   421 
   422 lemma wf_iff_acyclic_if_finite: "finite r ==> wf r = acyclic r"
   423 by (blast intro: finite_acyclic_wf wf_acyclic)
   424 
   425 
   426 subsection{*Well-Founded Recursion*}
   427 
   428 text{*cut*}
   429 
   430 lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))"
   431 by (simp add: expand_fun_eq cut_def)
   432 
   433 lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)"
   434 by (simp add: cut_def)
   435 
   436 text{*Inductive characterization of wfrec combinator; for details see:  
   437 John Harrison, "Inductive definitions: automation and application"*}
   438 
   439 lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. wfrec_rel R F x y"
   440 apply (simp add: adm_wf_def)
   441 apply (erule_tac a=x in wf_induct) 
   442 apply (rule ex1I)
   443 apply (rule_tac g = "%x. THE y. wfrec_rel R F x y" in wfrec_rel.wfrecI)
   444 apply (fast dest!: theI')
   445 apply (erule wfrec_rel.cases, simp)
   446 apply (erule allE, erule allE, erule allE, erule mp)
   447 apply (fast intro: the_equality [symmetric])
   448 done
   449 
   450 lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
   451 apply (simp add: adm_wf_def)
   452 apply (intro strip)
   453 apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
   454 apply (rule refl)
   455 done
   456 
   457 lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"
   458 apply (simp add: wfrec_def)
   459 apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
   460 apply (rule wfrec_rel.wfrecI)
   461 apply (intro strip)
   462 apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
   463 done
   464 
   465 subsection {* Code generator setup *}
   466 
   467 consts_code
   468   "wfrec"   ("\<module>wfrec?")
   469 attach {*
   470 fun wfrec f x = f (wfrec f) x;
   471 *}
   472 
   473 
   474 subsection {* @{typ nat} is well-founded *}
   475 
   476 lemma less_nat_rel: "op < = (\<lambda>m n. n = Suc m)^++"
   477 proof (rule ext, rule ext, rule iffI)
   478   fix n m :: nat
   479   assume "m < n"
   480   then show "(\<lambda>m n. n = Suc m)^++ m n"
   481   proof (induct n)
   482     case 0 then show ?case by auto
   483   next
   484     case (Suc n) then show ?case
   485       by (auto simp add: less_Suc_eq_le le_less intro: tranclp.trancl_into_trancl)
   486   qed
   487 next
   488   fix n m :: nat
   489   assume "(\<lambda>m n. n = Suc m)^++ m n"
   490   then show "m < n"
   491     by (induct n)
   492       (simp_all add: less_Suc_eq_le reflexive le_less)
   493 qed
   494 
   495 definition
   496   pred_nat :: "(nat * nat) set" where
   497   "pred_nat = {(m, n). n = Suc m}"
   498 
   499 definition
   500   less_than :: "(nat * nat) set" where
   501   "less_than = pred_nat^+"
   502 
   503 lemma less_eq: "(m, n) \<in> pred_nat^+ \<longleftrightarrow> m < n"
   504   unfolding less_nat_rel pred_nat_def trancl_def by simp
   505 
   506 lemma pred_nat_trancl_eq_le:
   507   "(m, n) \<in> pred_nat^* \<longleftrightarrow> m \<le> n"
   508   unfolding less_eq rtrancl_eq_or_trancl by auto
   509 
   510 lemma wf_pred_nat: "wf pred_nat"
   511   apply (unfold wf_def pred_nat_def, clarify)
   512   apply (induct_tac x, blast+)
   513   done
   514 
   515 lemma wf_less_than [iff]: "wf less_than"
   516   by (simp add: less_than_def wf_pred_nat [THEN wf_trancl])
   517 
   518 lemma trans_less_than [iff]: "trans less_than"
   519   by (simp add: less_than_def trans_trancl)
   520 
   521 lemma less_than_iff [iff]: "((x,y): less_than) = (x<y)"
   522   by (simp add: less_than_def less_eq)
   523 
   524 lemma wf_less: "wf {(x, y::nat). x < y}"
   525   using wf_less_than by (simp add: less_than_def less_eq [symmetric])
   526 
   527 
   528 subsection {* Accessible Part *}
   529 
   530 text {*
   531  Inductive definition of the accessible part @{term "acc r"} of a
   532  relation; see also \cite{paulin-tlca}.
   533 *}
   534 
   535 inductive_set
   536   acc :: "('a * 'a) set => 'a set"
   537   for r :: "('a * 'a) set"
   538   where
   539     accI: "(!!y. (y, x) : r ==> y : acc r) ==> x : acc r"
   540 
   541 abbreviation
   542   termip :: "('a => 'a => bool) => 'a => bool" where
   543   "termip r == accp (r\<inverse>\<inverse>)"
   544 
   545 abbreviation
   546   termi :: "('a * 'a) set => 'a set" where
   547   "termi r == acc (r\<inverse>)"
   548 
   549 lemmas accpI = accp.accI
   550 
   551 text {* Induction rules *}
   552 
   553 theorem accp_induct:
   554   assumes major: "accp r a"
   555   assumes hyp: "!!x. accp r x ==> \<forall>y. r y x --> P y ==> P x"
   556   shows "P a"
   557   apply (rule major [THEN accp.induct])
   558   apply (rule hyp)
   559    apply (rule accp.accI)
   560    apply fast
   561   apply fast
   562   done
   563 
   564 theorems accp_induct_rule = accp_induct [rule_format, induct set: accp]
   565 
   566 theorem accp_downward: "accp r b ==> r a b ==> accp r a"
   567   apply (erule accp.cases)
   568   apply fast
   569   done
   570 
   571 lemma not_accp_down:
   572   assumes na: "\<not> accp R x"
   573   obtains z where "R z x" and "\<not> accp R z"
   574 proof -
   575   assume a: "\<And>z. \<lbrakk>R z x; \<not> accp R z\<rbrakk> \<Longrightarrow> thesis"
   576 
   577   show thesis
   578   proof (cases "\<forall>z. R z x \<longrightarrow> accp R z")
   579     case True
   580     hence "\<And>z. R z x \<Longrightarrow> accp R z" by auto
   581     hence "accp R x"
   582       by (rule accp.accI)
   583     with na show thesis ..
   584   next
   585     case False then obtain z where "R z x" and "\<not> accp R z"
   586       by auto
   587     with a show thesis .
   588   qed
   589 qed
   590 
   591 lemma accp_downwards_aux: "r\<^sup>*\<^sup>* b a ==> accp r a --> accp r b"
   592   apply (erule rtranclp_induct)
   593    apply blast
   594   apply (blast dest: accp_downward)
   595   done
   596 
   597 theorem accp_downwards: "accp r a ==> r\<^sup>*\<^sup>* b a ==> accp r b"
   598   apply (blast dest: accp_downwards_aux)
   599   done
   600 
   601 theorem accp_wfPI: "\<forall>x. accp r x ==> wfP r"
   602   apply (rule wfPUNIVI)
   603   apply (induct_tac P x rule: accp_induct)
   604    apply blast
   605   apply blast
   606   done
   607 
   608 theorem accp_wfPD: "wfP r ==> accp r x"
   609   apply (erule wfP_induct_rule)
   610   apply (rule accp.accI)
   611   apply blast
   612   done
   613 
   614 theorem wfP_accp_iff: "wfP r = (\<forall>x. accp r x)"
   615   apply (blast intro: accp_wfPI dest: accp_wfPD)
   616   done
   617 
   618 
   619 text {* Smaller relations have bigger accessible parts: *}
   620 
   621 lemma accp_subset:
   622   assumes sub: "R1 \<le> R2"
   623   shows "accp R2 \<le> accp R1"
   624 proof (rule predicate1I)
   625   fix x assume "accp R2 x"
   626   then show "accp R1 x"
   627   proof (induct x)
   628     fix x
   629     assume ih: "\<And>y. R2 y x \<Longrightarrow> accp R1 y"
   630     with sub show "accp R1 x"
   631       by (blast intro: accp.accI)
   632   qed
   633 qed
   634 
   635 
   636 text {* This is a generalized induction theorem that works on
   637   subsets of the accessible part. *}
   638 
   639 lemma accp_subset_induct:
   640   assumes subset: "D \<le> accp R"
   641     and dcl: "\<And>x z. \<lbrakk>D x; R z x\<rbrakk> \<Longrightarrow> D z"
   642     and "D x"
   643     and istep: "\<And>x. \<lbrakk>D x; (\<And>z. R z x \<Longrightarrow> P z)\<rbrakk> \<Longrightarrow> P x"
   644   shows "P x"
   645 proof -
   646   from subset and `D x`
   647   have "accp R x" ..
   648   then show "P x" using `D x`
   649   proof (induct x)
   650     fix x
   651     assume "D x"
   652       and "\<And>y. R y x \<Longrightarrow> D y \<Longrightarrow> P y"
   653     with dcl and istep show "P x" by blast
   654   qed
   655 qed
   656 
   657 
   658 text {* Set versions of the above theorems *}
   659 
   660 lemmas acc_induct = accp_induct [to_set]
   661 
   662 lemmas acc_induct_rule = acc_induct [rule_format, induct set: acc]
   663 
   664 lemmas acc_downward = accp_downward [to_set]
   665 
   666 lemmas not_acc_down = not_accp_down [to_set]
   667 
   668 lemmas acc_downwards_aux = accp_downwards_aux [to_set]
   669 
   670 lemmas acc_downwards = accp_downwards [to_set]
   671 
   672 lemmas acc_wfI = accp_wfPI [to_set]
   673 
   674 lemmas acc_wfD = accp_wfPD [to_set]
   675 
   676 lemmas wf_acc_iff = wfP_accp_iff [to_set]
   677 
   678 lemmas acc_subset = accp_subset [to_set pred_subset_eq]
   679 
   680 lemmas acc_subset_induct = accp_subset_induct [to_set pred_subset_eq]
   681 
   682 
   683 subsection {* Tools for building wellfounded relations *}
   684 
   685 text {* Inverse Image *}
   686 
   687 lemma wf_inv_image [simp,intro!]: "wf(r) ==> wf(inv_image r (f::'a=>'b))"
   688 apply (simp (no_asm_use) add: inv_image_def wf_eq_minimal)
   689 apply clarify
   690 apply (subgoal_tac "EX (w::'b) . w : {w. EX (x::'a) . x: Q & (f x = w) }")
   691 prefer 2 apply (blast del: allE)
   692 apply (erule allE)
   693 apply (erule (1) notE impE)
   694 apply blast
   695 done
   696 
   697 lemma in_inv_image[simp]: "((x,y) : inv_image r f) = ((f x, f y) : r)"
   698   by (auto simp:inv_image_def)
   699 
   700 text {* Measure Datatypes into @{typ nat} *}
   701 
   702 definition measure :: "('a => nat) => ('a * 'a)set"
   703 where "measure == inv_image less_than"
   704 
   705 lemma in_measure[simp]: "((x,y) : measure f) = (f x < f y)"
   706   by (simp add:measure_def)
   707 
   708 lemma wf_measure [iff]: "wf (measure f)"
   709 apply (unfold measure_def)
   710 apply (rule wf_less_than [THEN wf_inv_image])
   711 done
   712 
   713 text{* Lexicographic combinations *}
   714 
   715 definition
   716  lex_prod  :: "[('a*'a)set, ('b*'b)set] => (('a*'b)*('a*'b))set"
   717                (infixr "<*lex*>" 80)
   718 where
   719     "ra <*lex*> rb == {((a,b),(a',b')). (a,a') : ra | a=a' & (b,b') : rb}"
   720 
   721 lemma wf_lex_prod [intro!]: "[| wf(ra); wf(rb) |] ==> wf(ra <*lex*> rb)"
   722 apply (unfold wf_def lex_prod_def) 
   723 apply (rule allI, rule impI)
   724 apply (simp (no_asm_use) only: split_paired_All)
   725 apply (drule spec, erule mp) 
   726 apply (rule allI, rule impI)
   727 apply (drule spec, erule mp, blast) 
   728 done
   729 
   730 lemma in_lex_prod[simp]: 
   731   "(((a,b),(a',b')): r <*lex*> s) = ((a,a'): r \<or> (a = a' \<and> (b, b') : s))"
   732   by (auto simp:lex_prod_def)
   733 
   734 text{* @{term "op <*lex*>"} preserves transitivity *}
   735 
   736 lemma trans_lex_prod [intro!]: 
   737     "[| trans R1; trans R2 |] ==> trans (R1 <*lex*> R2)"
   738 by (unfold trans_def lex_prod_def, blast) 
   739 
   740 text {* lexicographic combinations with measure Datatypes *}
   741 
   742 definition 
   743   mlex_prod :: "('a \<Rightarrow> nat) \<Rightarrow> ('a \<times> 'a) set \<Rightarrow> ('a \<times> 'a) set" (infixr "<*mlex*>" 80)
   744 where
   745   "f <*mlex*> R = inv_image (less_than <*lex*> R) (%x. (f x, x))"
   746 
   747 lemma wf_mlex: "wf R \<Longrightarrow> wf (f <*mlex*> R)"
   748 unfolding mlex_prod_def
   749 by auto
   750 
   751 lemma mlex_less: "f x < f y \<Longrightarrow> (x, y) \<in> f <*mlex*> R"
   752 unfolding mlex_prod_def by simp
   753 
   754 lemma mlex_leq: "f x \<le> f y \<Longrightarrow> (x, y) \<in> R \<Longrightarrow> (x, y) \<in> f <*mlex*> R"
   755 unfolding mlex_prod_def by auto
   756 
   757 text {* proper subset relation on finite sets *}
   758 
   759 definition finite_psubset  :: "('a set * 'a set) set"
   760 where "finite_psubset == {(A,B). A < B & finite B}"
   761 
   762 lemma wf_finite_psubset[simp]: "wf(finite_psubset)"
   763 apply (unfold finite_psubset_def)
   764 apply (rule wf_measure [THEN wf_subset])
   765 apply (simp add: measure_def inv_image_def less_than_def less_eq)
   766 apply (fast elim!: psubset_card_mono)
   767 done
   768 
   769 lemma trans_finite_psubset: "trans finite_psubset"
   770 by (simp add: finite_psubset_def less_le trans_def, blast)
   771 
   772 lemma in_finite_psubset[simp]: "(A, B) \<in> finite_psubset = (A < B & finite B)"
   773 unfolding finite_psubset_def by auto
   774 
   775 text {* max- and min-extension of order to finite sets *}
   776 
   777 inductive_set max_ext :: "('a \<times> 'a) set \<Rightarrow> ('a set \<times> 'a set) set" 
   778 for R :: "('a \<times> 'a) set"
   779 where
   780   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"
   781 
   782 lemma max_ext_wf:
   783   assumes wf: "wf r"
   784   shows "wf (max_ext r)"
   785 proof (rule acc_wfI, intro allI)
   786   fix M show "M \<in> acc (max_ext r)" (is "_ \<in> ?W")
   787   proof cases
   788     assume "finite M"
   789     thus ?thesis
   790     proof (induct M)
   791       show "{} \<in> ?W"
   792         by (rule accI) (auto elim: max_ext.cases)
   793     next
   794       fix M a assume "M \<in> ?W" "finite M"
   795       with wf show "insert a M \<in> ?W"
   796       proof (induct arbitrary: M)
   797         fix M a
   798         assume "M \<in> ?W"  and  [intro]: "finite M"
   799         assume hyp: "\<And>b M. (b, a) \<in> r \<Longrightarrow> M \<in> ?W \<Longrightarrow> finite M \<Longrightarrow> insert b M \<in> ?W"
   800         {
   801           fix N M :: "'a set"
   802           assume "finite N" "finite M"
   803           then
   804           have "\<lbrakk>M \<in> ?W ; (\<And>y. y \<in> N \<Longrightarrow> (y, a) \<in> r)\<rbrakk> \<Longrightarrow>  N \<union> M \<in> ?W"
   805             by (induct N arbitrary: M) (auto simp: hyp)
   806         }
   807         note add_less = this
   808         
   809         show "insert a M \<in> ?W"
   810         proof (rule accI)
   811           fix N assume Nless: "(N, insert a M) \<in> max_ext r"
   812           hence asm1: "\<And>x. x \<in> N \<Longrightarrow> (x, a) \<in> r \<or> (\<exists>y \<in> M. (x, y) \<in> r)"
   813             by (auto elim!: max_ext.cases)
   814 
   815           let ?N1 = "{ n \<in> N. (n, a) \<in> r }"
   816           let ?N2 = "{ n \<in> N. (n, a) \<notin> r }"
   817           have N: "?N1 \<union> ?N2 = N" by (rule set_ext) auto
   818           from Nless have "finite N" by (auto elim: max_ext.cases)
   819           then have finites: "finite ?N1" "finite ?N2" by auto
   820           
   821           have "?N2 \<in> ?W"
   822           proof cases
   823             assume [simp]: "M = {}"
   824             have Mw: "{} \<in> ?W" by (rule accI) (auto elim: max_ext.cases)
   825 
   826             from asm1 have "?N2 = {}" by auto
   827             with Mw show "?N2 \<in> ?W" by (simp only:)
   828           next
   829             assume "M \<noteq> {}"
   830             have N2: "(?N2, M) \<in> max_ext r" 
   831               by (rule max_extI[OF _ _ `M \<noteq> {}`]) (insert asm1, auto intro: finites)
   832             
   833             with `M \<in> ?W` show "?N2 \<in> ?W" by (rule acc_downward)
   834           qed
   835           with finites have "?N1 \<union> ?N2 \<in> ?W" 
   836             by (rule add_less) simp
   837           then show "N \<in> ?W" by (simp only: N)
   838         qed
   839       qed
   840     qed
   841   next
   842     assume [simp]: "\<not> finite M"
   843     show ?thesis
   844       by (rule accI) (auto elim: max_ext.cases)
   845   qed
   846 qed
   847 
   848 lemma max_ext_additive: 
   849  "(A, B) \<in> max_ext R \<Longrightarrow> (C, D) \<in> max_ext R \<Longrightarrow>
   850   (A \<union> C, B \<union> D) \<in> max_ext R"
   851 by (force elim!: max_ext.cases)
   852 
   853 
   854 definition
   855   min_ext :: "('a \<times> 'a) set \<Rightarrow> ('a set \<times> 'a set) set" 
   856 where
   857   [code del]: "min_ext r = {(X, Y) | X Y. X \<noteq> {} \<and> (\<forall>y \<in> Y. (\<exists>x \<in> X. (x, y) \<in> r))}"
   858 
   859 lemma min_ext_wf:
   860   assumes "wf r"
   861   shows "wf (min_ext r)"
   862 proof (rule wfI_min)
   863   fix Q :: "'a set set"
   864   fix x
   865   assume nonempty: "x \<in> Q"
   866   show "\<exists>m \<in> Q. (\<forall> n. (n, m) \<in> min_ext r \<longrightarrow> n \<notin> Q)"
   867   proof cases
   868     assume "Q = {{}}" thus ?thesis by (simp add: min_ext_def)
   869   next
   870     assume "Q \<noteq> {{}}"
   871     with nonempty
   872     obtain e x where "x \<in> Q" "e \<in> x" by force
   873     then have eU: "e \<in> \<Union>Q" by auto
   874     with `wf r` 
   875     obtain z where z: "z \<in> \<Union>Q" "\<And>y. (y, z) \<in> r \<Longrightarrow> y \<notin> \<Union>Q" 
   876       by (erule wfE_min)
   877     from z obtain m where "m \<in> Q" "z \<in> m" by auto
   878     from `m \<in> Q`
   879     show ?thesis
   880     proof (rule, intro bexI allI impI)
   881       fix n
   882       assume smaller: "(n, m) \<in> min_ext r"
   883       with `z \<in> m` obtain y where y: "y \<in> n" "(y, z) \<in> r" by (auto simp: min_ext_def)
   884       then show "n \<notin> Q" using z(2) by auto
   885     qed      
   886   qed
   887 qed
   888 
   889 
   890 subsection{*Weakly decreasing sequences (w.r.t. some well-founded order) 
   891    stabilize.*}
   892 
   893 text{*This material does not appear to be used any longer.*}
   894 
   895 lemma sequence_trans: "[| ALL i. (f (Suc i), f i) : r^* |] ==> (f (i+k), f i) : r^*"
   896 by (induct k) (auto intro: rtrancl_trans)
   897 
   898 lemma wf_weak_decr_stable: 
   899   assumes as: "ALL i. (f (Suc i), f i) : r^*" "wf (r^+)"
   900   shows "EX i. ALL k. f (i+k) = f i"
   901 proof -
   902   have lem: "!!x. [| ALL i. (f (Suc i), f i) : r^*; wf (r^+) |]  
   903       ==> ALL m. f m = x --> (EX i. ALL k. f (m+i+k) = f (m+i))"
   904   apply (erule wf_induct, clarify)
   905   apply (case_tac "EX j. (f (m+j), f m) : r^+")
   906    apply clarify
   907    apply (subgoal_tac "EX i. ALL k. f ((m+j) +i+k) = f ( (m+j) +i) ")
   908     apply clarify
   909     apply (rule_tac x = "j+i" in exI)
   910     apply (simp add: add_ac, blast)
   911   apply (rule_tac x = 0 in exI, clarsimp)
   912   apply (drule_tac i = m and k = k in sequence_trans)
   913   apply (blast elim: rtranclE dest: rtrancl_into_trancl1)
   914   done
   915 
   916   from lem[OF as, THEN spec, of 0, simplified] 
   917   show ?thesis by auto
   918 qed
   919 
   920 (* special case of the theorem above: <= *)
   921 lemma weak_decr_stable:
   922      "ALL i. f (Suc i) <= ((f i)::nat) ==> EX i. ALL k. f (i+k) = f i"
   923 apply (rule_tac r = pred_nat in wf_weak_decr_stable)
   924 apply (simp add: pred_nat_trancl_eq_le)
   925 apply (intro wf_trancl wf_pred_nat)
   926 done
   927 
   928 
   929 subsection {* size of a datatype value *}
   930 
   931 use "Tools/Function/size.ML"
   932 
   933 setup Size.setup
   934 
   935 lemma size_bool [code]:
   936   "size (b\<Colon>bool) = 0" by (cases b) auto
   937 
   938 lemma nat_size [simp, code]: "size (n\<Colon>nat) = n"
   939   by (induct n) simp_all
   940 
   941 declare "prod.size" [noatp]
   942 
   943 lemma [code]:
   944   "size (P :: 'a Predicate.pred) = 0" by (cases P) simp
   945 
   946 lemma [code]:
   947   "pred_size f P = 0" by (cases P) simp
   948 
   949 end