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