src/HOL/Wellfounded_Recursion.thy
author wenzelm
Fri Mar 28 19:43:54 2008 +0100 (2008-03-28)
changeset 26462 dac4e2bce00d
parent 26235 96b804999ca7
permissions -rw-r--r--
avoid rebinding of existing facts;
paulson@15341
     1
(*  ID:         $Id$
nipkow@10213
     2
    Author:     Tobias Nipkow
nipkow@10213
     3
    Copyright   1992  University of Cambridge
nipkow@10213
     4
*)
nipkow@10213
     5
paulson@15341
     6
header {*Well-founded Recursion*}
paulson@15341
     7
paulson@15341
     8
theory Wellfounded_Recursion
haftmann@26072
     9
imports Transitive_Closure Nat
haftmann@26072
    10
uses ("Tools/function_package/size.ML")
paulson@15341
    11
begin
nipkow@10213
    12
berghofe@23744
    13
inductive
berghofe@22263
    14
  wfrec_rel :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b => bool"
berghofe@22263
    15
  for R :: "('a * 'a) set"
berghofe@22263
    16
  and F :: "('a => 'b) => 'a => 'b"
berghofe@22263
    17
where
berghofe@22263
    18
  wfrecI: "ALL z. (z, x) : R --> wfrec_rel R F z (g z) ==>
berghofe@22263
    19
            wfrec_rel R F x (F g x)"
berghofe@11328
    20
nipkow@10213
    21
constdefs
nipkow@10213
    22
  wf         :: "('a * 'a)set => bool"
nipkow@10213
    23
  "wf(r) == (!P. (!x. (!y. (y,x):r --> P(y)) --> P(x)) --> (!x. P(x)))"
nipkow@10213
    24
berghofe@22263
    25
  wfP :: "('a => 'a => bool) => bool"
berghofe@23744
    26
  "wfP r == wf {(x, y). r x y}"
berghofe@22263
    27
nipkow@10213
    28
  acyclic :: "('a*'a)set => bool"
nipkow@10213
    29
  "acyclic r == !x. (x,x) ~: r^+"
nipkow@10213
    30
nipkow@10213
    31
  cut        :: "('a => 'b) => ('a * 'a)set => 'a => 'a => 'b"
nipkow@10213
    32
  "cut f r x == (%y. if (y,x):r then f y else arbitrary)"
nipkow@10213
    33
berghofe@11328
    34
  adm_wf :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => bool"
berghofe@11328
    35
  "adm_wf R F == ALL f g x.
berghofe@11328
    36
     (ALL z. (z, x) : R --> f z = g z) --> F f x = F g x"
nipkow@10213
    37
berghofe@11328
    38
  wfrec :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b"
haftmann@22845
    39
  [code func del]: "wfrec R F == %x. THE y. wfrec_rel R (%f x. F (cut f R x) x) x y"
berghofe@22263
    40
berghofe@22263
    41
abbreviation acyclicP :: "('a => 'a => bool) => bool" where
berghofe@23744
    42
  "acyclicP r == acyclic {(x, y). r x y}"
nipkow@10213
    43
haftmann@22390
    44
class wellorder = linorder +
haftmann@25207
    45
  assumes wf: "wf {(x, y). x < y}"
paulson@15341
    46
paulson@15341
    47
berghofe@23744
    48
lemma wfP_wf_eq [pred_set_conv]: "wfP (\<lambda>x y. (x, y) \<in> r) = wf r"
berghofe@22263
    49
  by (simp add: wfP_def)
berghofe@22263
    50
paulson@15341
    51
lemma wfUNIVI: 
paulson@15341
    52
   "(!!P x. (ALL x. (ALL y. (y,x) : r --> P(y)) --> P(x)) ==> P(x)) ==> wf(r)"
wenzelm@26175
    53
  unfolding wf_def by blast
paulson@15341
    54
berghofe@22263
    55
lemmas wfPUNIVI = wfUNIVI [to_pred]
berghofe@22263
    56
paulson@19766
    57
text{*Restriction to domain @{term A} and range @{term B}.  If @{term r} is
paulson@19766
    58
    well-founded over their intersection, then @{term "wf r"}*}
paulson@15341
    59
lemma wfI: 
paulson@19766
    60
 "[| r \<subseteq> A <*> B; 
paulson@19766
    61
     !!x P. [|\<forall>x. (\<forall>y. (y,x) : r --> P y) --> P x;  x : A; x : B |] ==> P x |]
paulson@15341
    62
  ==>  wf r"
wenzelm@26175
    63
  unfolding wf_def by blast
paulson@15341
    64
paulson@15341
    65
lemma wf_induct: 
paulson@15341
    66
    "[| wf(r);           
paulson@15341
    67
        !!x.[| ALL y. (y,x): r --> P(y) |] ==> P(x)  
paulson@15341
    68
     |]  ==>  P(a)"
wenzelm@26175
    69
  unfolding wf_def by blast
paulson@15341
    70
berghofe@22263
    71
lemmas wfP_induct = wf_induct [to_pred]
berghofe@22263
    72
wenzelm@18458
    73
lemmas wf_induct_rule = wf_induct [rule_format, consumes 1, case_names less, induct set: wf]
nipkow@17042
    74
berghofe@25425
    75
lemmas wfP_induct_rule = wf_induct_rule [to_pred, induct set: wfP]
berghofe@22263
    76
wenzelm@26175
    77
lemma wf_not_sym: "wf r ==> (a, x) : r ==> (x, a) ~: r"
wenzelm@26175
    78
  by (induct a arbitrary: x set: wf) blast
paulson@15341
    79
paulson@15341
    80
(* [| wf r;  ~Z ==> (a,x) : r;  (x,a) ~: r ==> Z |] ==> Z *)
paulson@15341
    81
lemmas wf_asym = wf_not_sym [elim_format]
paulson@15341
    82
wenzelm@26175
    83
lemma wf_not_refl [simp]: "wf r ==> (a, a) ~: r"
wenzelm@26175
    84
  by (blast elim: wf_asym)
paulson@15341
    85
paulson@15341
    86
(* [| wf r;  (a,a) ~: r ==> PROP W |] ==> PROP W *)
paulson@15341
    87
lemmas wf_irrefl = wf_not_refl [elim_format]
paulson@15341
    88
paulson@15341
    89
text{*transitive closure of a well-founded relation is well-founded! *}
wenzelm@26175
    90
lemma wf_trancl:
wenzelm@26175
    91
  assumes "wf r"
wenzelm@26175
    92
  shows "wf (r^+)"
wenzelm@26175
    93
proof -
wenzelm@26175
    94
  {
wenzelm@26175
    95
    fix P and x
wenzelm@26175
    96
    assume induct_step: "!!x. (!!y. (y, x) : r^+ ==> P y) ==> P x"
wenzelm@26175
    97
    have "P x"
wenzelm@26175
    98
    proof (rule induct_step)
wenzelm@26175
    99
      fix y assume "(y, x) : r^+"
wenzelm@26175
   100
      with `wf r` show "P y"
wenzelm@26175
   101
      proof (induct x arbitrary: y)
wenzelm@26175
   102
	case (less x)
wenzelm@26175
   103
	note hyp = `\<And>x' y'. (x', x) : r ==> (y', x') : r^+ ==> P y'`
wenzelm@26175
   104
	from `(y, x) : r^+` show "P y"
wenzelm@26175
   105
	proof cases
wenzelm@26175
   106
	  case base
wenzelm@26175
   107
	  show "P y"
wenzelm@26175
   108
	  proof (rule induct_step)
wenzelm@26175
   109
	    fix y' assume "(y', y) : r^+"
wenzelm@26175
   110
	    with `(y, x) : r` show "P y'" by (rule hyp [of y y'])
wenzelm@26175
   111
	  qed
wenzelm@26175
   112
	next
wenzelm@26175
   113
	  case step
wenzelm@26175
   114
	  then obtain x' where "(x', x) : r" and "(y, x') : r^+" by simp
wenzelm@26175
   115
	  then show "P y" by (rule hyp [of x' y])
wenzelm@26175
   116
	qed
wenzelm@26175
   117
      qed
wenzelm@26175
   118
    qed
wenzelm@26175
   119
  } then show ?thesis unfolding wf_def by blast
wenzelm@26175
   120
qed
paulson@15341
   121
berghofe@22263
   122
lemmas wfP_trancl = wf_trancl [to_pred]
berghofe@22263
   123
paulson@15341
   124
lemma wf_converse_trancl: "wf (r^-1) ==> wf ((r^+)^-1)"
wenzelm@26175
   125
  apply (subst trancl_converse [symmetric])
wenzelm@26175
   126
  apply (erule wf_trancl)
wenzelm@26175
   127
  done
paulson@15341
   128
paulson@15341
   129
wenzelm@26175
   130
subsubsection {* Other simple well-foundedness results *}
paulson@15341
   131
paulson@19870
   132
text{*Minimal-element characterization of well-foundedness*}
paulson@19870
   133
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))"
paulson@19870
   134
proof (intro iffI strip)
wenzelm@26175
   135
  fix Q :: "'a set" and x
paulson@19870
   136
  assume "wf r" and "x \<in> Q"
wenzelm@26175
   137
  then show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> r \<longrightarrow> y \<notin> Q"
wenzelm@26175
   138
    unfolding wf_def
wenzelm@26175
   139
    by (blast dest: spec [of _ "%x. x\<in>Q \<longrightarrow> (\<exists>z\<in>Q. \<forall>y. (y,z) \<in> r \<longrightarrow> y\<notin>Q)"]) 
paulson@19870
   140
next
huffman@22766
   141
  assume 1: "\<forall>Q x. x \<in> Q \<longrightarrow> (\<exists>z\<in>Q. \<forall>y. (y, z) \<in> r \<longrightarrow> y \<notin> Q)"
huffman@22766
   142
  show "wf r"
huffman@22766
   143
  proof (rule wfUNIVI)
huffman@22766
   144
    fix P :: "'a \<Rightarrow> bool" and x
huffman@22766
   145
    assume 2: "\<forall>x. (\<forall>y. (y, x) \<in> r \<longrightarrow> P y) \<longrightarrow> P x"
huffman@22766
   146
    let ?Q = "{x. \<not> P x}"
wenzelm@26175
   147
    have "x \<in> ?Q \<longrightarrow> (\<exists>z \<in> ?Q. \<forall>y. (y, z) \<in> r \<longrightarrow> y \<notin> ?Q)"
huffman@22766
   148
      by (rule 1 [THEN spec, THEN spec])
wenzelm@26175
   149
    then have "\<not> P x \<longrightarrow> (\<exists>z. \<not> P z \<and> (\<forall>y. (y, z) \<in> r \<longrightarrow> P y))" by simp
huffman@22766
   150
    with 2 have "\<not> P x \<longrightarrow> (\<exists>z. \<not> P z \<and> P z)" by fast
wenzelm@26175
   151
    then show "P x" by simp
huffman@22766
   152
  qed
paulson@19870
   153
qed
paulson@19870
   154
krauss@23186
   155
lemma wfE_min: 
wenzelm@26175
   156
  assumes "wf R" "x \<in> Q"
krauss@23186
   157
  obtains z where "z \<in> Q" "\<And>y. (y, z) \<in> R \<Longrightarrow> y \<notin> Q"
wenzelm@26175
   158
  using assms unfolding wf_eq_minimal by blast
krauss@23186
   159
krauss@23186
   160
lemma wfI_min:
krauss@23186
   161
  "(\<And>x Q. x \<in> Q \<Longrightarrow> \<exists>z\<in>Q. \<forall>y. (y, z) \<in> R \<longrightarrow> y \<notin> Q)
krauss@23186
   162
  \<Longrightarrow> wf R"
wenzelm@26175
   163
  unfolding wf_eq_minimal by blast
krauss@23186
   164
berghofe@22263
   165
lemmas wfP_eq_minimal = wf_eq_minimal [to_pred]
berghofe@22263
   166
wenzelm@26175
   167
text {* Well-foundedness of subsets *}
paulson@15341
   168
lemma wf_subset: "[| wf(r);  p<=r |] ==> wf(p)"
wenzelm@26175
   169
  apply (simp (no_asm_use) add: wf_eq_minimal)
wenzelm@26175
   170
  apply fast
wenzelm@26175
   171
  done
paulson@15341
   172
berghofe@22263
   173
lemmas wfP_subset = wf_subset [to_pred]
berghofe@22263
   174
wenzelm@26175
   175
text {* Well-foundedness of the empty relation *}
paulson@15341
   176
lemma wf_empty [iff]: "wf({})"
wenzelm@26175
   177
  by (simp add: wf_def)
paulson@15341
   178
berghofe@23744
   179
lemmas wfP_empty [iff] =
berghofe@23744
   180
  wf_empty [to_pred bot_empty_eq2, simplified bot_fun_eq bot_bool_eq]
berghofe@22263
   181
paulson@19870
   182
lemma wf_Int1: "wf r ==> wf (r Int r')"
wenzelm@26175
   183
  apply (erule wf_subset)
wenzelm@26175
   184
  apply (rule Int_lower1)
wenzelm@26175
   185
  done
paulson@19870
   186
paulson@19870
   187
lemma wf_Int2: "wf r ==> wf (r' Int r)"
wenzelm@26175
   188
  apply (erule wf_subset)
wenzelm@26175
   189
  apply (rule Int_lower2)
wenzelm@26175
   190
  done  
paulson@19870
   191
paulson@15341
   192
text{*Well-foundedness of insert*}
paulson@15341
   193
lemma wf_insert [iff]: "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)"
paulson@15341
   194
apply (rule iffI)
paulson@15341
   195
 apply (blast elim: wf_trancl [THEN wf_irrefl]
paulson@15341
   196
              intro: rtrancl_into_trancl1 wf_subset 
paulson@15341
   197
                     rtrancl_mono [THEN [2] rev_subsetD])
paulson@15341
   198
apply (simp add: wf_eq_minimal, safe)
paulson@15341
   199
apply (rule allE, assumption, erule impE, blast) 
paulson@15341
   200
apply (erule bexE)
paulson@15341
   201
apply (rename_tac "a", case_tac "a = x")
paulson@15341
   202
 prefer 2
paulson@15341
   203
apply blast 
paulson@15341
   204
apply (case_tac "y:Q")
paulson@15341
   205
 prefer 2 apply blast
paulson@15341
   206
apply (rule_tac x = "{z. z:Q & (z,y) : r^*}" in allE)
paulson@15341
   207
 apply assumption
paulson@15341
   208
apply (erule_tac V = "ALL Q. (EX x. x : Q) --> ?P Q" in thin_rl) 
paulson@15341
   209
  --{*essential for speed*}
kleing@15343
   210
txt{*Blast with new substOccur fails*}
paulson@15341
   211
apply (fast intro: converse_rtrancl_into_rtrancl)
paulson@15341
   212
done
paulson@15341
   213
paulson@15341
   214
text{*Well-foundedness of image*}
paulson@15341
   215
lemma wf_prod_fun_image: "[| wf r; inj f |] ==> wf(prod_fun f f ` r)"
paulson@15341
   216
apply (simp only: wf_eq_minimal, clarify)
paulson@15341
   217
apply (case_tac "EX p. f p : Q")
paulson@15341
   218
apply (erule_tac x = "{p. f p : Q}" in allE)
paulson@15341
   219
apply (fast dest: inj_onD, blast)
paulson@15341
   220
done
paulson@15341
   221
paulson@15341
   222
wenzelm@26175
   223
subsubsection {* Well-Foundedness Results for Unions *}
paulson@15341
   224
krauss@26044
   225
lemma wf_union_compatible:
krauss@26044
   226
  assumes "wf R" "wf S"
wenzelm@26175
   227
  assumes "S O R \<subseteq> R"
krauss@26044
   228
  shows "wf (R \<union> S)"
krauss@26044
   229
proof (rule wfI_min)
krauss@26044
   230
  fix x :: 'a and Q 
wenzelm@26175
   231
  let ?Q' = "{x \<in> Q. \<forall>y. (y, x) \<in> R \<longrightarrow> y \<notin> Q}"
krauss@26044
   232
  assume "x \<in> Q"
wenzelm@26175
   233
  obtain a where "a \<in> ?Q'"
wenzelm@26175
   234
    by (rule wfE_min [OF `wf R` `x \<in> Q`]) blast
krauss@26044
   235
  with `wf S`
krauss@26044
   236
  obtain z where "z \<in> ?Q'" and zmin: "\<And>y. (y, z) \<in> S \<Longrightarrow> y \<notin> ?Q'" by (erule wfE_min)
krauss@26044
   237
  { 
wenzelm@26175
   238
    fix y assume "(y, z) \<in> S"
krauss@26044
   239
    then have "y \<notin> ?Q'" by (rule zmin)
krauss@26044
   240
krauss@26044
   241
    have "y \<notin> Q"
krauss@26044
   242
    proof 
krauss@26044
   243
      assume "y \<in> Q"
krauss@26044
   244
      with `y \<notin> ?Q'` 
wenzelm@26175
   245
      obtain w where "(w, y) \<in> R" and "w \<in> Q" by auto
wenzelm@26175
   246
      from `(w, y) \<in> R` `(y, z) \<in> S` have "(w, z) \<in> S O R" by (rule rel_compI)
wenzelm@26175
   247
      with `S O R \<subseteq> R` have "(w, z) \<in> R" ..
krauss@26044
   248
      with `z \<in> ?Q'` have "w \<notin> Q" by blast 
wenzelm@26175
   249
      with `w \<in> Q` show False by contradiction
krauss@26044
   250
    qed
krauss@26044
   251
  }
wenzelm@26175
   252
  with `z \<in> ?Q'` show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> R \<union> S \<longrightarrow> y \<notin> Q" by blast
krauss@26044
   253
qed
krauss@26044
   254
wenzelm@26175
   255
wenzelm@26175
   256
text {* Well-foundedness of indexed union with disjoint domains and ranges *}
paulson@15341
   257
paulson@15341
   258
lemma wf_UN: "[| ALL i:I. wf(r i);  
paulson@15341
   259
         ALL i:I. ALL j:I. r i ~= r j --> Domain(r i) Int Range(r j) = {}  
paulson@15341
   260
      |] ==> wf(UN i:I. r i)"
paulson@15341
   261
apply (simp only: wf_eq_minimal, clarify)
paulson@15341
   262
apply (rename_tac A a, case_tac "EX i:I. EX a:A. EX b:A. (b,a) : r i")
paulson@15341
   263
 prefer 2
paulson@15341
   264
 apply force 
paulson@15341
   265
apply clarify
paulson@15341
   266
apply (drule bspec, assumption)  
paulson@15341
   267
apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r i) }" in allE)
paulson@15341
   268
apply (blast elim!: allE)  
paulson@15341
   269
done
paulson@15341
   270
berghofe@23744
   271
lemmas wfP_SUP = wf_UN [where I=UNIV and r="\<lambda>i. {(x, y). r i x y}",
berghofe@23744
   272
  to_pred SUP_UN_eq2 bot_empty_eq, simplified, standard]
berghofe@22263
   273
paulson@15341
   274
lemma wf_Union: 
paulson@15341
   275
 "[| ALL r:R. wf r;  
paulson@15341
   276
     ALL r:R. ALL s:R. r ~= s --> Domain r Int Range s = {}  
paulson@15341
   277
  |] ==> wf(Union R)"
paulson@15341
   278
apply (simp add: Union_def)
paulson@15341
   279
apply (blast intro: wf_UN)
paulson@15341
   280
done
paulson@15341
   281
paulson@15341
   282
(*Intuition: we find an (R u S)-min element of a nonempty subset A
paulson@15341
   283
             by case distinction.
paulson@15341
   284
  1. There is a step a -R-> b with a,b : A.
paulson@15341
   285
     Pick an R-min element z of the (nonempty) set {a:A | EX b:A. a -R-> b}.
paulson@15341
   286
     By definition, there is z':A s.t. z -R-> z'. Because z is R-min in the
paulson@15341
   287
     subset, z' must be R-min in A. Because z' has an R-predecessor, it cannot
paulson@15341
   288
     have an S-successor and is thus S-min in A as well.
paulson@15341
   289
  2. There is no such step.
paulson@15341
   290
     Pick an S-min element of A. In this case it must be an R-min
paulson@15341
   291
     element of A as well.
paulson@15341
   292
paulson@15341
   293
*)
paulson@15341
   294
lemma wf_Un:
paulson@15341
   295
     "[| wf r; wf s; Domain r Int Range s = {} |] ==> wf(r Un s)"
wenzelm@26175
   296
  using wf_union_compatible[of s r] 
wenzelm@26175
   297
  by (auto simp: Un_ac)
paulson@15341
   298
krauss@23186
   299
lemma wf_union_merge: 
krauss@23186
   300
  "wf (R \<union> S) = wf (R O R \<union> R O S \<union> S)" (is "wf ?A = wf ?B")
krauss@23186
   301
proof
krauss@23186
   302
  assume "wf ?A"
krauss@23186
   303
  with wf_trancl have wfT: "wf (?A^+)" .
krauss@23186
   304
  moreover have "?B \<subseteq> ?A^+"
wenzelm@26175
   305
    by (subst trancl_unfold, subst trancl_unfold) blast
krauss@23186
   306
  ultimately show "wf ?B" by (rule wf_subset)
krauss@23186
   307
next
krauss@23186
   308
  assume "wf ?B"
krauss@23186
   309
krauss@23186
   310
  show "wf ?A"
krauss@23186
   311
  proof (rule wfI_min)
krauss@23186
   312
    fix Q :: "'a set" and x 
krauss@23186
   313
    assume "x \<in> Q"
krauss@23186
   314
krauss@23186
   315
    with `wf ?B`
krauss@23186
   316
    obtain z where "z \<in> Q" and "\<And>y. (y, z) \<in> ?B \<Longrightarrow> y \<notin> Q" 
krauss@23186
   317
      by (erule wfE_min)
wenzelm@26175
   318
    then have A1: "\<And>y. (y, z) \<in> R O R \<Longrightarrow> y \<notin> Q"
krauss@23186
   319
      and A2: "\<And>y. (y, z) \<in> R O S \<Longrightarrow> y \<notin> Q"
krauss@23186
   320
      and A3: "\<And>y. (y, z) \<in> S \<Longrightarrow> y \<notin> Q"
krauss@23186
   321
      by auto
krauss@23186
   322
    
krauss@23186
   323
    show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> ?A \<longrightarrow> y \<notin> Q"
krauss@23186
   324
    proof (cases "\<forall>y. (y, z) \<in> R \<longrightarrow> y \<notin> Q")
krauss@23186
   325
      case True
krauss@23186
   326
      with `z \<in> Q` A3 show ?thesis by blast
krauss@23186
   327
    next
krauss@23186
   328
      case False 
krauss@23186
   329
      then obtain z' where "z'\<in>Q" "(z', z) \<in> R" by blast
krauss@23186
   330
krauss@23186
   331
      have "\<forall>y. (y, z') \<in> ?A \<longrightarrow> y \<notin> Q"
krauss@23186
   332
      proof (intro allI impI)
krauss@23186
   333
        fix y assume "(y, z') \<in> ?A"
wenzelm@26175
   334
        then show "y \<notin> Q"
krauss@23186
   335
        proof
krauss@23186
   336
          assume "(y, z') \<in> R" 
wenzelm@26175
   337
          then have "(y, z) \<in> R O R" using `(z', z) \<in> R` ..
krauss@23186
   338
          with A1 show "y \<notin> Q" .
krauss@23186
   339
        next
krauss@23186
   340
          assume "(y, z') \<in> S" 
wenzelm@26175
   341
          then have "(y, z) \<in> R O S" using  `(z', z) \<in> R` ..
krauss@23186
   342
          with A2 show "y \<notin> Q" .
krauss@23186
   343
        qed
krauss@23186
   344
      qed
wenzelm@23389
   345
      with `z' \<in> Q` show ?thesis ..
krauss@23186
   346
    qed
krauss@23186
   347
  qed
krauss@23186
   348
qed
krauss@23186
   349
wenzelm@26175
   350
lemma wf_comp_self: "wf R = wf (R O R)"  -- {* special case *}
wenzelm@26175
   351
  by (rule wf_union_merge [where S = "{}", simplified])
krauss@23186
   352
wenzelm@26175
   353
wenzelm@26175
   354
subsubsection {* acyclic *}
paulson@15341
   355
paulson@15341
   356
lemma acyclicI: "ALL x. (x, x) ~: r^+ ==> acyclic r"
wenzelm@26175
   357
  by (simp add: acyclic_def)
paulson@15341
   358
paulson@15341
   359
lemma wf_acyclic: "wf r ==> acyclic r"
paulson@15341
   360
apply (simp add: acyclic_def)
paulson@15341
   361
apply (blast elim: wf_trancl [THEN wf_irrefl])
paulson@15341
   362
done
paulson@15341
   363
berghofe@22263
   364
lemmas wfP_acyclicP = wf_acyclic [to_pred]
berghofe@22263
   365
paulson@15341
   366
lemma acyclic_insert [iff]:
paulson@15341
   367
     "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)"
paulson@15341
   368
apply (simp add: acyclic_def trancl_insert)
paulson@15341
   369
apply (blast intro: rtrancl_trans)
paulson@15341
   370
done
paulson@15341
   371
paulson@15341
   372
lemma acyclic_converse [iff]: "acyclic(r^-1) = acyclic r"
paulson@15341
   373
by (simp add: acyclic_def trancl_converse)
paulson@15341
   374
berghofe@22263
   375
lemmas acyclicP_converse [iff] = acyclic_converse [to_pred]
berghofe@22263
   376
paulson@15341
   377
lemma acyclic_impl_antisym_rtrancl: "acyclic r ==> antisym(r^*)"
paulson@15341
   378
apply (simp add: acyclic_def antisym_def)
paulson@15341
   379
apply (blast elim: rtranclE intro: rtrancl_into_trancl1 rtrancl_trancl_trancl)
paulson@15341
   380
done
paulson@15341
   381
paulson@15341
   382
(* Other direction:
paulson@15341
   383
acyclic = no loops
paulson@15341
   384
antisym = only self loops
paulson@15341
   385
Goalw [acyclic_def,antisym_def] "antisym( r^* ) ==> acyclic(r - Id)
paulson@15341
   386
==> antisym( r^* ) = acyclic(r - Id)";
paulson@15341
   387
*)
paulson@15341
   388
paulson@15341
   389
lemma acyclic_subset: "[| acyclic s; r <= s |] ==> acyclic r"
paulson@15341
   390
apply (simp add: acyclic_def)
paulson@15341
   391
apply (blast intro: trancl_mono)
paulson@15341
   392
done
paulson@15341
   393
paulson@15341
   394
paulson@15341
   395
subsection{*Well-Founded Recursion*}
paulson@15341
   396
paulson@15341
   397
text{*cut*}
paulson@15341
   398
paulson@15341
   399
lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))"
paulson@15341
   400
by (simp add: expand_fun_eq cut_def)
paulson@15341
   401
paulson@15341
   402
lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)"
paulson@15341
   403
by (simp add: cut_def)
paulson@15341
   404
paulson@15341
   405
text{*Inductive characterization of wfrec combinator; for details see:  
paulson@15341
   406
John Harrison, "Inductive definitions: automation and application"*}
paulson@15341
   407
berghofe@22263
   408
lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. wfrec_rel R F x y"
paulson@15341
   409
apply (simp add: adm_wf_def)
paulson@15341
   410
apply (erule_tac a=x in wf_induct) 
paulson@15341
   411
apply (rule ex1I)
berghofe@22263
   412
apply (rule_tac g = "%x. THE y. wfrec_rel R F x y" in wfrec_rel.wfrecI)
paulson@15341
   413
apply (fast dest!: theI')
paulson@15341
   414
apply (erule wfrec_rel.cases, simp)
paulson@15341
   415
apply (erule allE, erule allE, erule allE, erule mp)
paulson@15341
   416
apply (fast intro: the_equality [symmetric])
paulson@15341
   417
done
paulson@15341
   418
paulson@15341
   419
lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
paulson@15341
   420
apply (simp add: adm_wf_def)
paulson@15341
   421
apply (intro strip)
paulson@15341
   422
apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
paulson@15341
   423
apply (rule refl)
paulson@15341
   424
done
paulson@15341
   425
paulson@15341
   426
lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"
paulson@15341
   427
apply (simp add: wfrec_def)
paulson@15341
   428
apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
paulson@15341
   429
apply (rule wfrec_rel.wfrecI)
paulson@15341
   430
apply (intro strip)
paulson@15341
   431
apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
paulson@15341
   432
done
paulson@15341
   433
paulson@15341
   434
paulson@15341
   435
text{** This form avoids giant explosions in proofs.  NOTE USE OF ==*}
paulson@15341
   436
lemma def_wfrec: "[| f==wfrec r H;  wf(r) |] ==> f(a) = H (cut f r a) a"
paulson@15341
   437
apply auto
paulson@15341
   438
apply (blast intro: wfrec)
paulson@15341
   439
done
paulson@15341
   440
paulson@15341
   441
wenzelm@17459
   442
subsection {* Code generator setup *}
wenzelm@17459
   443
wenzelm@17459
   444
consts_code
berghofe@17654
   445
  "wfrec"   ("\<module>wfrec?")
wenzelm@17459
   446
attach {*
berghofe@17654
   447
fun wfrec f x = f (wfrec f) x;
wenzelm@17459
   448
*}
wenzelm@17459
   449
wenzelm@17459
   450
paulson@15341
   451
subsection{*Variants for TFL: the Recdef Package*}
paulson@15341
   452
paulson@15341
   453
lemma tfl_wf_induct: "ALL R. wf R -->  
paulson@15341
   454
       (ALL P. (ALL x. (ALL y. (y,x):R --> P y) --> P x) --> (ALL x. P x))"
paulson@15341
   455
apply clarify
paulson@15341
   456
apply (rule_tac r = R and P = P and a = x in wf_induct, assumption, blast)
paulson@15341
   457
done
paulson@15341
   458
paulson@15341
   459
lemma tfl_cut_apply: "ALL f R. (x,a):R --> (cut f R a)(x) = f(x)"
paulson@15341
   460
apply clarify
paulson@15341
   461
apply (rule cut_apply, assumption)
paulson@15341
   462
done
paulson@15341
   463
paulson@15341
   464
lemma tfl_wfrec:
paulson@15341
   465
     "ALL M R f. (f=wfrec R M) --> wf R --> (ALL x. f x = M (cut f R x) x)"
paulson@15341
   466
apply clarify
paulson@15341
   467
apply (erule wfrec)
paulson@15341
   468
done
paulson@15341
   469
paulson@15341
   470
subsection {*LEAST and wellorderings*}
paulson@15341
   471
paulson@15341
   472
text{* See also @{text wf_linord_ex_has_least} and its consequences in
paulson@15341
   473
 @{text Wellfounded_Relations.ML}*}
paulson@15341
   474
paulson@15341
   475
lemma wellorder_Least_lemma [rule_format]:
paulson@15341
   476
     "P (k::'a::wellorder) --> P (LEAST x. P(x)) & (LEAST x. P(x)) <= k"
paulson@15341
   477
apply (rule_tac a = k in wf [THEN wf_induct])
paulson@15341
   478
apply (rule impI)
paulson@15341
   479
apply (rule classical)
paulson@15341
   480
apply (rule_tac s = x in Least_equality [THEN ssubst], auto)
paulson@15341
   481
apply (auto simp add: linorder_not_less [symmetric])
paulson@15341
   482
done
paulson@15341
   483
paulson@15341
   484
lemmas LeastI   = wellorder_Least_lemma [THEN conjunct1, standard]
paulson@15341
   485
lemmas Least_le = wellorder_Least_lemma [THEN conjunct2, standard]
paulson@15341
   486
nipkow@15950
   487
-- "The following 3 lemmas are due to Brian Huffman"
nipkow@15950
   488
lemma LeastI_ex: "EX x::'a::wellorder. P x ==> P (Least P)"
nipkow@15950
   489
apply (erule exE)
nipkow@15950
   490
apply (erule LeastI)
nipkow@15950
   491
done
nipkow@15950
   492
nipkow@15950
   493
lemma LeastI2:
nipkow@15950
   494
  "[| P (a::'a::wellorder); !!x. P x ==> Q x |] ==> Q (Least P)"
nipkow@15950
   495
by (blast intro: LeastI)
nipkow@15950
   496
nipkow@15950
   497
lemma LeastI2_ex:
nipkow@15950
   498
  "[| EX a::'a::wellorder. P a; !!x. P x ==> Q x |] ==> Q (Least P)"
nipkow@15950
   499
by (blast intro: LeastI_ex)
nipkow@15950
   500
paulson@15341
   501
lemma not_less_Least: "[| k < (LEAST x. P x) |] ==> ~P (k::'a::wellorder)"
paulson@15341
   502
apply (simp (no_asm_use) add: linorder_not_le [symmetric])
paulson@15341
   503
apply (erule contrapos_nn)
paulson@15341
   504
apply (erule Least_le)
paulson@15341
   505
done
paulson@15341
   506
haftmann@26072
   507
subsection {* @{typ nat} is well-founded *}
haftmann@26072
   508
haftmann@26072
   509
lemma less_nat_rel: "op < = (\<lambda>m n. n = Suc m)^++"
haftmann@26072
   510
proof (rule ext, rule ext, rule iffI)
haftmann@26072
   511
  fix n m :: nat
haftmann@26072
   512
  assume "m < n"
haftmann@26072
   513
  then show "(\<lambda>m n. n = Suc m)^++ m n"
haftmann@26072
   514
  proof (induct n)
haftmann@26072
   515
    case 0 then show ?case by auto
haftmann@26072
   516
  next
haftmann@26072
   517
    case (Suc n) then show ?case
wenzelm@26175
   518
      by (auto simp add: less_Suc_eq_le le_less intro: tranclp.trancl_into_trancl)
haftmann@26072
   519
  qed
haftmann@26072
   520
next
haftmann@26072
   521
  fix n m :: nat
haftmann@26072
   522
  assume "(\<lambda>m n. n = Suc m)^++ m n"
haftmann@26072
   523
  then show "m < n"
haftmann@26072
   524
    by (induct n)
haftmann@26072
   525
      (simp_all add: less_Suc_eq_le reflexive le_less)
haftmann@26072
   526
qed
haftmann@26072
   527
haftmann@26072
   528
definition
haftmann@26072
   529
  pred_nat :: "(nat * nat) set" where
haftmann@26072
   530
  "pred_nat = {(m, n). n = Suc m}"
haftmann@26072
   531
haftmann@26072
   532
definition
haftmann@26235
   533
  less_than :: "(nat * nat) set" where
haftmann@26072
   534
  "less_than = pred_nat^+"
haftmann@26072
   535
haftmann@26072
   536
lemma less_eq: "(m, n) \<in> pred_nat^+ \<longleftrightarrow> m < n"
haftmann@26072
   537
  unfolding less_nat_rel pred_nat_def trancl_def by simp
haftmann@26072
   538
haftmann@26072
   539
lemma pred_nat_trancl_eq_le:
haftmann@26072
   540
  "(m, n) \<in> pred_nat^* \<longleftrightarrow> m \<le> n"
haftmann@26072
   541
  unfolding less_eq rtrancl_eq_or_trancl by auto
haftmann@26072
   542
haftmann@26072
   543
lemma wf_pred_nat: "wf pred_nat"
haftmann@26072
   544
  apply (unfold wf_def pred_nat_def, clarify)
haftmann@26072
   545
  apply (induct_tac x, blast+)
haftmann@26072
   546
  done
haftmann@26072
   547
haftmann@26072
   548
lemma wf_less_than [iff]: "wf less_than"
haftmann@26072
   549
  by (simp add: less_than_def wf_pred_nat [THEN wf_trancl])
haftmann@26072
   550
haftmann@26072
   551
lemma trans_less_than [iff]: "trans less_than"
haftmann@26072
   552
  by (simp add: less_than_def trans_trancl)
haftmann@26072
   553
haftmann@26072
   554
lemma less_than_iff [iff]: "((x,y): less_than) = (x<y)"
haftmann@26072
   555
  by (simp add: less_than_def less_eq)
haftmann@26072
   556
haftmann@26072
   557
lemma wf_less: "wf {(x, y::nat). x < y}"
haftmann@26072
   558
  using wf_less_than by (simp add: less_than_def less_eq [symmetric])
haftmann@26072
   559
haftmann@26072
   560
text {* Complete induction, aka course-of-values induction *}
haftmann@26072
   561
lemma nat_less_induct:
wenzelm@26175
   562
  assumes "!!n. \<forall>m::nat. m < n --> P m ==> P n" shows "P n"
haftmann@26072
   563
  apply (induct n rule: wf_induct [OF wf_pred_nat [THEN wf_trancl]])
wenzelm@26175
   564
  apply (rule assms)
haftmann@26072
   565
  apply (unfold less_eq [symmetric], assumption)
haftmann@26072
   566
  done
haftmann@26072
   567
haftmann@26072
   568
lemmas less_induct = nat_less_induct [rule_format, case_names less]
haftmann@26072
   569
haftmann@26072
   570
text {* Type @{typ nat} is a wellfounded order *}
haftmann@26072
   571
haftmann@26072
   572
instance nat :: wellorder
haftmann@26072
   573
  by intro_classes
haftmann@26072
   574
    (assumption |
haftmann@26072
   575
      rule le_refl le_trans le_anti_sym nat_less_le nat_le_linear wf_less)+
haftmann@26072
   576
haftmann@26072
   577
lemma nat_induct2: "[|P 0; P (Suc 0); !!k. P k ==> P (Suc (Suc k))|] ==> P n"
haftmann@26072
   578
  apply (rule nat_less_induct)
haftmann@26072
   579
  apply (case_tac n)
haftmann@26072
   580
  apply (case_tac [2] nat)
haftmann@26072
   581
  apply (blast intro: less_trans)+
haftmann@26072
   582
  done
haftmann@26072
   583
haftmann@26072
   584
text {* The method of infinite descent, frequently used in number theory.
haftmann@26072
   585
Provided by Roelof Oosterhuis.
haftmann@26072
   586
$P(n)$ is true for all $n\in\mathbb{N}$ if
haftmann@26072
   587
\begin{itemize}
haftmann@26072
   588
  \item case ``0'': given $n=0$ prove $P(n)$,
haftmann@26072
   589
  \item case ``smaller'': given $n>0$ and $\neg P(n)$ prove there exists
haftmann@26072
   590
        a smaller integer $m$ such that $\neg P(m)$.
haftmann@26072
   591
\end{itemize} *}
haftmann@26072
   592
haftmann@26072
   593
lemma infinite_descent0[case_names 0 smaller]: 
haftmann@26072
   594
  "\<lbrakk> P 0; !!n. n>0 \<Longrightarrow> \<not> P n \<Longrightarrow> (\<exists>m::nat. m < n \<and> \<not>P m) \<rbrakk> \<Longrightarrow> P n"
haftmann@26072
   595
by (induct n rule: less_induct, case_tac "n>0", auto)
haftmann@26072
   596
haftmann@26072
   597
text{* A compact version without explicit base case: *}
haftmann@26072
   598
lemma infinite_descent:
haftmann@26072
   599
  "\<lbrakk> !!n::nat. \<not> P n \<Longrightarrow>  \<exists>m<n. \<not>  P m \<rbrakk> \<Longrightarrow>  P n"
haftmann@26072
   600
by (induct n rule: less_induct, auto)
haftmann@26072
   601
wenzelm@26175
   602
text {*
wenzelm@26175
   603
Infinite descent using a mapping to $\mathbb{N}$:
haftmann@26072
   604
$P(x)$ is true for all $x\in D$ if there exists a $V: D \to \mathbb{N}$ and
haftmann@26072
   605
\begin{itemize}
haftmann@26072
   606
\item case ``0'': given $V(x)=0$ prove $P(x)$,
haftmann@26072
   607
\item case ``smaller'': given $V(x)>0$ and $\neg P(x)$ prove there exists a $y \in D$ such that $V(y)<V(x)$ and $~\neg P(y)$.
haftmann@26072
   608
\end{itemize}
haftmann@26072
   609
NB: the proof also shows how to use the previous lemma. *}
wenzelm@26175
   610
haftmann@26072
   611
corollary infinite_descent0_measure [case_names 0 smaller]:
haftmann@26072
   612
  assumes A0: "!!x. V x = (0::nat) \<Longrightarrow> P x"
haftmann@26072
   613
    and   A1: "!!x. V x > 0 \<Longrightarrow> \<not>P x \<Longrightarrow> (\<exists>y. V y < V x \<and> \<not>P y)"
haftmann@26072
   614
  shows "P x"
haftmann@26072
   615
proof -
haftmann@26072
   616
  obtain n where "n = V x" by auto
haftmann@26072
   617
  moreover have "\<And>x. V x = n \<Longrightarrow> P x"
haftmann@26072
   618
  proof (induct n rule: infinite_descent0)
haftmann@26072
   619
    case 0 -- "i.e. $V(x) = 0$"
haftmann@26072
   620
    with A0 show "P x" by auto
haftmann@26072
   621
  next -- "now $n>0$ and $P(x)$ does not hold for some $x$ with $V(x)=n$"
haftmann@26072
   622
    case (smaller n)
haftmann@26072
   623
    then obtain x where vxn: "V x = n " and "V x > 0 \<and> \<not> P x" by auto
haftmann@26072
   624
    with A1 obtain y where "V y < V x \<and> \<not> P y" by auto
haftmann@26072
   625
    with vxn obtain m where "m = V y \<and> m<n \<and> \<not> P y" by auto
wenzelm@26175
   626
    then show ?case by auto
haftmann@26072
   627
  qed
haftmann@26072
   628
  ultimately show "P x" by auto
haftmann@26072
   629
qed
haftmann@26072
   630
haftmann@26072
   631
text{* Again, without explicit base case: *}
haftmann@26072
   632
lemma infinite_descent_measure:
haftmann@26072
   633
assumes "!!x. \<not> P x \<Longrightarrow> \<exists>y. (V::'a\<Rightarrow>nat) y < V x \<and> \<not> P y" shows "P x"
haftmann@26072
   634
proof -
haftmann@26072
   635
  from assms obtain n where "n = V x" by auto
haftmann@26072
   636
  moreover have "!!x. V x = n \<Longrightarrow> P x"
haftmann@26072
   637
  proof (induct n rule: infinite_descent, auto)
haftmann@26072
   638
    fix x assume "\<not> P x"
haftmann@26072
   639
    with assms show "\<exists>m < V x. \<exists>y. V y = m \<and> \<not> P y" by auto
haftmann@26072
   640
  qed
haftmann@26072
   641
  ultimately show "P x" by auto
haftmann@26072
   642
qed
haftmann@26072
   643
haftmann@26072
   644
text {* @{text LEAST} theorems for type @{typ nat}*}
haftmann@26072
   645
haftmann@26072
   646
lemma Least_Suc:
haftmann@26072
   647
     "[| P n; ~ P 0 |] ==> (LEAST n. P n) = Suc (LEAST m. P(Suc m))"
haftmann@26072
   648
  apply (case_tac "n", auto)
haftmann@26072
   649
  apply (frule LeastI)
haftmann@26072
   650
  apply (drule_tac P = "%x. P (Suc x) " in LeastI)
haftmann@26072
   651
  apply (subgoal_tac " (LEAST x. P x) \<le> Suc (LEAST x. P (Suc x))")
haftmann@26072
   652
  apply (erule_tac [2] Least_le)
haftmann@26072
   653
  apply (case_tac "LEAST x. P x", auto)
haftmann@26072
   654
  apply (drule_tac P = "%x. P (Suc x) " in Least_le)
haftmann@26072
   655
  apply (blast intro: order_antisym)
haftmann@26072
   656
  done
haftmann@26072
   657
haftmann@26072
   658
lemma Least_Suc2:
haftmann@26072
   659
   "[|P n; Q m; ~P 0; !k. P (Suc k) = Q k|] ==> Least P = Suc (Least Q)"
wenzelm@26175
   660
  apply (erule (1) Least_Suc [THEN ssubst])
wenzelm@26175
   661
  apply simp
wenzelm@26175
   662
  done
haftmann@26072
   663
nipkow@26105
   664
lemma ex_least_nat_le: "\<not>P(0) \<Longrightarrow> P(n::nat) \<Longrightarrow> \<exists>k\<le>n. (\<forall>i<k. \<not>P i) & P(k)"
wenzelm@26175
   665
  apply (cases n)
wenzelm@26175
   666
   apply blast
wenzelm@26175
   667
  apply (rule_tac x="LEAST k. P(k)" in exI)
wenzelm@26175
   668
  apply (blast intro: Least_le dest: not_less_Least intro: LeastI_ex)
wenzelm@26175
   669
  done
nipkow@26105
   670
nipkow@26105
   671
lemma ex_least_nat_less: "\<not>P(0) \<Longrightarrow> P(n::nat) \<Longrightarrow> \<exists>k<n. (\<forall>i\<le>k. \<not>P i) & P(k+1)"
wenzelm@26175
   672
  apply (cases n)
wenzelm@26175
   673
   apply blast
wenzelm@26175
   674
  apply (frule (1) ex_least_nat_le)
wenzelm@26175
   675
  apply (erule exE)
wenzelm@26175
   676
  apply (case_tac k)
wenzelm@26175
   677
   apply simp
wenzelm@26175
   678
  apply (rename_tac k1)
wenzelm@26175
   679
  apply (rule_tac x=k1 in exI)
wenzelm@26175
   680
  apply fastsimp
wenzelm@26175
   681
  done
nipkow@26105
   682
haftmann@26072
   683
haftmann@26072
   684
subsection {* size of a datatype value *}
haftmann@26072
   685
haftmann@26072
   686
use "Tools/function_package/size.ML"
haftmann@26072
   687
haftmann@26072
   688
setup Size.setup
haftmann@26072
   689
haftmann@26072
   690
lemma nat_size [simp, code func]: "size (n\<Colon>nat) = n"
haftmann@26072
   691
  by (induct n) simp_all
haftmann@26072
   692
nipkow@10213
   693
end