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