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