src/HOL/Wellfounded_Recursion.thy
author wenzelm
Thu Oct 04 20:29:42 2007 +0200 (2007-10-04)
changeset 24850 0cfd722ab579
parent 23744 7c9e6e2fe249
child 25207 d58c14280367
permissions -rw-r--r--
Name.uu, Name.aT;
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
paulson@15341
     9
imports Transitive_Closure
paulson@15341
    10
begin
nipkow@10213
    11
berghofe@23744
    12
inductive
berghofe@22263
    13
  wfrec_rel :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b => bool"
berghofe@22263
    14
  for R :: "('a * 'a) set"
berghofe@22263
    15
  and F :: "('a => 'b) => 'a => 'b"
berghofe@22263
    16
where
berghofe@22263
    17
  wfrecI: "ALL z. (z, x) : R --> wfrec_rel R F z (g z) ==>
berghofe@22263
    18
            wfrec_rel R F x (F g x)"
berghofe@11328
    19
nipkow@10213
    20
constdefs
nipkow@10213
    21
  wf         :: "('a * 'a)set => bool"
nipkow@10213
    22
  "wf(r) == (!P. (!x. (!y. (y,x):r --> P(y)) --> P(x)) --> (!x. P(x)))"
nipkow@10213
    23
berghofe@22263
    24
  wfP :: "('a => 'a => bool) => bool"
berghofe@23744
    25
  "wfP r == wf {(x, y). r x y}"
berghofe@22263
    26
nipkow@10213
    27
  acyclic :: "('a*'a)set => bool"
nipkow@10213
    28
  "acyclic r == !x. (x,x) ~: r^+"
nipkow@10213
    29
nipkow@10213
    30
  cut        :: "('a => 'b) => ('a * 'a)set => 'a => 'a => 'b"
nipkow@10213
    31
  "cut f r x == (%y. if (y,x):r then f y else arbitrary)"
nipkow@10213
    32
berghofe@11328
    33
  adm_wf :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => bool"
berghofe@11328
    34
  "adm_wf R F == ALL f g x.
berghofe@11328
    35
     (ALL z. (z, x) : R --> f z = g z) --> F f x = F g x"
nipkow@10213
    36
berghofe@11328
    37
  wfrec :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b"
haftmann@22845
    38
  [code func del]: "wfrec R F == %x. THE y. wfrec_rel R (%f x. F (cut f R x) x) x y"
berghofe@22263
    39
berghofe@22263
    40
abbreviation acyclicP :: "('a => 'a => bool) => bool" where
berghofe@23744
    41
  "acyclicP r == acyclic {(x, y). r x y}"
nipkow@10213
    42
haftmann@22390
    43
class wellorder = linorder +
haftmann@22390
    44
  assumes wf: "wf {(x, y). x \<sqsubset> y}"
paulson@15341
    45
paulson@15341
    46
berghofe@23744
    47
lemma wfP_wf_eq [pred_set_conv]: "wfP (\<lambda>x y. (x, y) \<in> r) = wf r"
berghofe@22263
    48
  by (simp add: wfP_def)
berghofe@22263
    49
paulson@15341
    50
lemma wfUNIVI: 
paulson@15341
    51
   "(!!P x. (ALL x. (ALL y. (y,x) : r --> P(y)) --> P(x)) ==> P(x)) ==> wf(r)"
paulson@15341
    52
by (unfold wf_def, blast)
paulson@15341
    53
berghofe@22263
    54
lemmas wfPUNIVI = wfUNIVI [to_pred]
berghofe@22263
    55
paulson@19766
    56
text{*Restriction to domain @{term A} and range @{term B}.  If @{term r} is
paulson@19766
    57
    well-founded over their intersection, then @{term "wf r"}*}
paulson@15341
    58
lemma wfI: 
paulson@19766
    59
 "[| r \<subseteq> A <*> B; 
paulson@19766
    60
     !!x P. [|\<forall>x. (\<forall>y. (y,x) : r --> P y) --> P x;  x : A; x : B |] ==> P x |]
paulson@15341
    61
  ==>  wf r"
paulson@15341
    62
by (unfold wf_def, blast)
paulson@15341
    63
paulson@15341
    64
lemma wf_induct: 
paulson@15341
    65
    "[| wf(r);           
paulson@15341
    66
        !!x.[| ALL y. (y,x): r --> P(y) |] ==> P(x)  
paulson@15341
    67
     |]  ==>  P(a)"
paulson@15341
    68
by (unfold wf_def, blast)
paulson@15341
    69
berghofe@22263
    70
lemmas wfP_induct = wf_induct [to_pred]
berghofe@22263
    71
wenzelm@18458
    72
lemmas wf_induct_rule = wf_induct [rule_format, consumes 1, case_names less, induct set: wf]
nipkow@17042
    73
berghofe@22263
    74
lemmas wfP_induct_rule =
berghofe@22263
    75
  wf_induct_rule [to_pred, consumes 1, case_names less, induct set: wfP]
berghofe@22263
    76
paulson@15341
    77
lemma wf_not_sym [rule_format]: "wf(r) ==> ALL x. (a,x):r --> (x,a)~:r"
paulson@15341
    78
by (erule_tac a=a in wf_induct, 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
paulson@15341
    83
lemma wf_not_refl [simp]: "wf(r) ==> (a,a) ~: r"
paulson@15341
    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! *}
paulson@15341
    90
lemma wf_trancl: "wf(r) ==> wf(r^+)"
paulson@15341
    91
apply (subst wf_def, clarify)
paulson@15341
    92
apply (rule allE, assumption)
paulson@15341
    93
  --{*Retains the universal formula for later use!*}
paulson@15341
    94
apply (erule mp)
paulson@15341
    95
apply (erule_tac a = x in wf_induct)
paulson@15341
    96
apply (blast elim: tranclE)
paulson@15341
    97
done
paulson@15341
    98
berghofe@22263
    99
lemmas wfP_trancl = wf_trancl [to_pred]
berghofe@22263
   100
paulson@15341
   101
lemma wf_converse_trancl: "wf (r^-1) ==> wf ((r^+)^-1)"
paulson@15341
   102
apply (subst trancl_converse [symmetric])
paulson@15341
   103
apply (erule wf_trancl)
paulson@15341
   104
done
paulson@15341
   105
paulson@15341
   106
paulson@15341
   107
subsubsection{*Other simple well-foundedness results*}
paulson@15341
   108
paulson@15341
   109
paulson@19870
   110
text{*Minimal-element characterization of well-foundedness*}
paulson@19870
   111
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
   112
proof (intro iffI strip)
paulson@19870
   113
  fix Q::"'a set" and x
paulson@19870
   114
  assume "wf r" and "x \<in> Q"
paulson@19870
   115
  thus "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> r \<longrightarrow> y \<notin> Q"
paulson@19870
   116
    by (unfold wf_def, 
paulson@19870
   117
        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
   118
next
huffman@22766
   119
  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
   120
  show "wf r"
huffman@22766
   121
  proof (rule wfUNIVI)
huffman@22766
   122
    fix P :: "'a \<Rightarrow> bool" and x
huffman@22766
   123
    assume 2: "\<forall>x. (\<forall>y. (y, x) \<in> r \<longrightarrow> P y) \<longrightarrow> P x"
huffman@22766
   124
    let ?Q = "{x. \<not> P x}"
huffman@22766
   125
    have "x \<in> ?Q \<longrightarrow> (\<exists>z\<in>?Q. \<forall>y. (y, z) \<in> r \<longrightarrow> y \<notin> ?Q)"
huffman@22766
   126
      by (rule 1 [THEN spec, THEN spec])
huffman@22766
   127
    hence "\<not> P x \<longrightarrow> (\<exists>z. \<not> P z \<and> (\<forall>y. (y, z) \<in> r \<longrightarrow> P y))" by simp
huffman@22766
   128
    with 2 have "\<not> P x \<longrightarrow> (\<exists>z. \<not> P z \<and> P z)" by fast
huffman@22766
   129
    thus "P x" by simp
huffman@22766
   130
  qed
paulson@19870
   131
qed
paulson@19870
   132
krauss@23186
   133
lemma wfE_min: 
krauss@23186
   134
  assumes p:"wf R" "x \<in> Q"
krauss@23186
   135
  obtains z where "z \<in> Q" "\<And>y. (y, z) \<in> R \<Longrightarrow> y \<notin> Q"
krauss@23186
   136
  using p
krauss@23186
   137
  unfolding wf_eq_minimal
krauss@23186
   138
  by blast
krauss@23186
   139
krauss@23186
   140
lemma wfI_min:
krauss@23186
   141
  "(\<And>x Q. x \<in> Q \<Longrightarrow> \<exists>z\<in>Q. \<forall>y. (y, z) \<in> R \<longrightarrow> y \<notin> Q)
krauss@23186
   142
  \<Longrightarrow> wf R"
krauss@23186
   143
  unfolding wf_eq_minimal
krauss@23186
   144
  by blast
krauss@23186
   145
berghofe@22263
   146
lemmas wfP_eq_minimal = wf_eq_minimal [to_pred]
berghofe@22263
   147
paulson@15341
   148
text{*Well-foundedness of subsets*}
paulson@15341
   149
lemma wf_subset: "[| wf(r);  p<=r |] ==> wf(p)"
paulson@15341
   150
apply (simp (no_asm_use) add: wf_eq_minimal)
paulson@15341
   151
apply fast
paulson@15341
   152
done
paulson@15341
   153
berghofe@22263
   154
lemmas wfP_subset = wf_subset [to_pred]
berghofe@22263
   155
paulson@15341
   156
text{*Well-foundedness of the empty relation*}
paulson@15341
   157
lemma wf_empty [iff]: "wf({})"
paulson@15341
   158
by (simp add: wf_def)
paulson@15341
   159
berghofe@23744
   160
lemmas wfP_empty [iff] =
berghofe@23744
   161
  wf_empty [to_pred bot_empty_eq2, simplified bot_fun_eq bot_bool_eq]
berghofe@22263
   162
paulson@19870
   163
lemma wf_Int1: "wf r ==> wf (r Int r')"
paulson@19870
   164
by (erule wf_subset, rule Int_lower1)
paulson@19870
   165
paulson@19870
   166
lemma wf_Int2: "wf r ==> wf (r' Int r)"
paulson@19870
   167
by (erule wf_subset, rule Int_lower2)
paulson@19870
   168
paulson@15341
   169
text{*Well-foundedness of insert*}
paulson@15341
   170
lemma wf_insert [iff]: "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)"
paulson@15341
   171
apply (rule iffI)
paulson@15341
   172
 apply (blast elim: wf_trancl [THEN wf_irrefl]
paulson@15341
   173
              intro: rtrancl_into_trancl1 wf_subset 
paulson@15341
   174
                     rtrancl_mono [THEN [2] rev_subsetD])
paulson@15341
   175
apply (simp add: wf_eq_minimal, safe)
paulson@15341
   176
apply (rule allE, assumption, erule impE, blast) 
paulson@15341
   177
apply (erule bexE)
paulson@15341
   178
apply (rename_tac "a", case_tac "a = x")
paulson@15341
   179
 prefer 2
paulson@15341
   180
apply blast 
paulson@15341
   181
apply (case_tac "y:Q")
paulson@15341
   182
 prefer 2 apply blast
paulson@15341
   183
apply (rule_tac x = "{z. z:Q & (z,y) : r^*}" in allE)
paulson@15341
   184
 apply assumption
paulson@15341
   185
apply (erule_tac V = "ALL Q. (EX x. x : Q) --> ?P Q" in thin_rl) 
paulson@15341
   186
  --{*essential for speed*}
kleing@15343
   187
txt{*Blast with new substOccur fails*}
paulson@15341
   188
apply (fast intro: converse_rtrancl_into_rtrancl)
paulson@15341
   189
done
paulson@15341
   190
paulson@15341
   191
text{*Well-foundedness of image*}
paulson@15341
   192
lemma wf_prod_fun_image: "[| wf r; inj f |] ==> wf(prod_fun f f ` r)"
paulson@15341
   193
apply (simp only: wf_eq_minimal, clarify)
paulson@15341
   194
apply (case_tac "EX p. f p : Q")
paulson@15341
   195
apply (erule_tac x = "{p. f p : Q}" in allE)
paulson@15341
   196
apply (fast dest: inj_onD, blast)
paulson@15341
   197
done
paulson@15341
   198
paulson@15341
   199
paulson@15341
   200
subsubsection{*Well-Foundedness Results for Unions*}
paulson@15341
   201
paulson@15341
   202
text{*Well-foundedness of indexed union with disjoint domains and ranges*}
paulson@15341
   203
paulson@15341
   204
lemma wf_UN: "[| ALL i:I. wf(r i);  
paulson@15341
   205
         ALL i:I. ALL j:I. r i ~= r j --> Domain(r i) Int Range(r j) = {}  
paulson@15341
   206
      |] ==> wf(UN i:I. r i)"
paulson@15341
   207
apply (simp only: wf_eq_minimal, clarify)
paulson@15341
   208
apply (rename_tac A a, case_tac "EX i:I. EX a:A. EX b:A. (b,a) : r i")
paulson@15341
   209
 prefer 2
paulson@15341
   210
 apply force 
paulson@15341
   211
apply clarify
paulson@15341
   212
apply (drule bspec, assumption)  
paulson@15341
   213
apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r i) }" in allE)
paulson@15341
   214
apply (blast elim!: allE)  
paulson@15341
   215
done
paulson@15341
   216
berghofe@23744
   217
lemmas wfP_SUP = wf_UN [where I=UNIV and r="\<lambda>i. {(x, y). r i x y}",
berghofe@23744
   218
  to_pred SUP_UN_eq2 bot_empty_eq, simplified, standard]
berghofe@22263
   219
paulson@15341
   220
lemma wf_Union: 
paulson@15341
   221
 "[| ALL r:R. wf r;  
paulson@15341
   222
     ALL r:R. ALL s:R. r ~= s --> Domain r Int Range s = {}  
paulson@15341
   223
  |] ==> wf(Union R)"
paulson@15341
   224
apply (simp add: Union_def)
paulson@15341
   225
apply (blast intro: wf_UN)
paulson@15341
   226
done
paulson@15341
   227
paulson@15341
   228
(*Intuition: we find an (R u S)-min element of a nonempty subset A
paulson@15341
   229
             by case distinction.
paulson@15341
   230
  1. There is a step a -R-> b with a,b : A.
paulson@15341
   231
     Pick an R-min element z of the (nonempty) set {a:A | EX b:A. a -R-> b}.
paulson@15341
   232
     By definition, there is z':A s.t. z -R-> z'. Because z is R-min in the
paulson@15341
   233
     subset, z' must be R-min in A. Because z' has an R-predecessor, it cannot
paulson@15341
   234
     have an S-successor and is thus S-min in A as well.
paulson@15341
   235
  2. There is no such step.
paulson@15341
   236
     Pick an S-min element of A. In this case it must be an R-min
paulson@15341
   237
     element of A as well.
paulson@15341
   238
paulson@15341
   239
*)
paulson@15341
   240
lemma wf_Un:
paulson@15341
   241
     "[| wf r; wf s; Domain r Int Range s = {} |] ==> wf(r Un s)"
paulson@15341
   242
apply (simp only: wf_eq_minimal, clarify) 
paulson@15341
   243
apply (rename_tac A a)
paulson@15341
   244
apply (case_tac "EX a:A. EX b:A. (b,a) : r") 
paulson@15341
   245
 prefer 2
paulson@15341
   246
 apply simp
paulson@15341
   247
 apply (drule_tac x=A in spec)+
paulson@15341
   248
 apply blast 
paulson@15341
   249
apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r) }" in allE)+
paulson@15341
   250
apply (blast elim!: allE)  
paulson@15341
   251
done
paulson@15341
   252
krauss@23186
   253
lemma wf_union_merge: 
krauss@23186
   254
  "wf (R \<union> S) = wf (R O R \<union> R O S \<union> S)" (is "wf ?A = wf ?B")
krauss@23186
   255
proof
krauss@23186
   256
  assume "wf ?A"
krauss@23186
   257
  with wf_trancl have wfT: "wf (?A^+)" .
krauss@23186
   258
  moreover have "?B \<subseteq> ?A^+"
krauss@23186
   259
    by  (subst trancl_unfold, subst trancl_unfold) blast
krauss@23186
   260
  ultimately show "wf ?B" by (rule wf_subset)
krauss@23186
   261
next
krauss@23186
   262
  assume "wf ?B"
krauss@23186
   263
krauss@23186
   264
  show "wf ?A"
krauss@23186
   265
  proof (rule wfI_min)
krauss@23186
   266
    fix Q :: "'a set" and x 
krauss@23186
   267
    assume "x \<in> Q"
krauss@23186
   268
krauss@23186
   269
    with `wf ?B`
krauss@23186
   270
    obtain z where "z \<in> Q" and "\<And>y. (y, z) \<in> ?B \<Longrightarrow> y \<notin> Q" 
krauss@23186
   271
      by (erule wfE_min)
krauss@23186
   272
    hence A1: "\<And>y. (y, z) \<in> R O R \<Longrightarrow> y \<notin> Q"
krauss@23186
   273
      and A2: "\<And>y. (y, z) \<in> R O S \<Longrightarrow> y \<notin> Q"
krauss@23186
   274
      and A3: "\<And>y. (y, z) \<in> S \<Longrightarrow> y \<notin> Q"
krauss@23186
   275
      by auto
krauss@23186
   276
    
krauss@23186
   277
    show "\<exists>z\<in>Q. \<forall>y. (y, z) \<in> ?A \<longrightarrow> y \<notin> Q"
krauss@23186
   278
    proof (cases "\<forall>y. (y, z) \<in> R \<longrightarrow> y \<notin> Q")
krauss@23186
   279
      case True
krauss@23186
   280
      with `z \<in> Q` A3 show ?thesis by blast
krauss@23186
   281
    next
krauss@23186
   282
      case False 
krauss@23186
   283
      then obtain z' where "z'\<in>Q" "(z', z) \<in> R" by blast
krauss@23186
   284
krauss@23186
   285
      have "\<forall>y. (y, z') \<in> ?A \<longrightarrow> y \<notin> Q"
krauss@23186
   286
      proof (intro allI impI)
krauss@23186
   287
        fix y assume "(y, z') \<in> ?A"
krauss@23186
   288
        thus "y \<notin> Q"
krauss@23186
   289
        proof
krauss@23186
   290
          assume "(y, z') \<in> R" 
krauss@23186
   291
          hence "(y, z) \<in> R O R" using `(z', z) \<in> R` ..
krauss@23186
   292
          with A1 show "y \<notin> Q" .
krauss@23186
   293
        next
krauss@23186
   294
          assume "(y, z') \<in> S" 
krauss@23186
   295
          hence "(y, z) \<in> R O S" using  `(z', z) \<in> R` ..
krauss@23186
   296
          with A2 show "y \<notin> Q" .
krauss@23186
   297
        qed
krauss@23186
   298
      qed
wenzelm@23389
   299
      with `z' \<in> Q` show ?thesis ..
krauss@23186
   300
    qed
krauss@23186
   301
  qed
krauss@23186
   302
qed
krauss@23186
   303
krauss@23186
   304
lemma wf_comp_self: "wf R = wf (R O R)" (* special case *)
krauss@23186
   305
  by (fact wf_union_merge[where S = "{}", simplified])
krauss@23186
   306
paulson@15341
   307
subsubsection {*acyclic*}
paulson@15341
   308
paulson@15341
   309
lemma acyclicI: "ALL x. (x, x) ~: r^+ ==> acyclic r"
paulson@15341
   310
by (simp add: acyclic_def)
paulson@15341
   311
paulson@15341
   312
lemma wf_acyclic: "wf r ==> acyclic r"
paulson@15341
   313
apply (simp add: acyclic_def)
paulson@15341
   314
apply (blast elim: wf_trancl [THEN wf_irrefl])
paulson@15341
   315
done
paulson@15341
   316
berghofe@22263
   317
lemmas wfP_acyclicP = wf_acyclic [to_pred]
berghofe@22263
   318
paulson@15341
   319
lemma acyclic_insert [iff]:
paulson@15341
   320
     "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)"
paulson@15341
   321
apply (simp add: acyclic_def trancl_insert)
paulson@15341
   322
apply (blast intro: rtrancl_trans)
paulson@15341
   323
done
paulson@15341
   324
paulson@15341
   325
lemma acyclic_converse [iff]: "acyclic(r^-1) = acyclic r"
paulson@15341
   326
by (simp add: acyclic_def trancl_converse)
paulson@15341
   327
berghofe@22263
   328
lemmas acyclicP_converse [iff] = acyclic_converse [to_pred]
berghofe@22263
   329
paulson@15341
   330
lemma acyclic_impl_antisym_rtrancl: "acyclic r ==> antisym(r^*)"
paulson@15341
   331
apply (simp add: acyclic_def antisym_def)
paulson@15341
   332
apply (blast elim: rtranclE intro: rtrancl_into_trancl1 rtrancl_trancl_trancl)
paulson@15341
   333
done
paulson@15341
   334
paulson@15341
   335
(* Other direction:
paulson@15341
   336
acyclic = no loops
paulson@15341
   337
antisym = only self loops
paulson@15341
   338
Goalw [acyclic_def,antisym_def] "antisym( r^* ) ==> acyclic(r - Id)
paulson@15341
   339
==> antisym( r^* ) = acyclic(r - Id)";
paulson@15341
   340
*)
paulson@15341
   341
paulson@15341
   342
lemma acyclic_subset: "[| acyclic s; r <= s |] ==> acyclic r"
paulson@15341
   343
apply (simp add: acyclic_def)
paulson@15341
   344
apply (blast intro: trancl_mono)
paulson@15341
   345
done
paulson@15341
   346
paulson@15341
   347
paulson@15341
   348
subsection{*Well-Founded Recursion*}
paulson@15341
   349
paulson@15341
   350
text{*cut*}
paulson@15341
   351
paulson@15341
   352
lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))"
paulson@15341
   353
by (simp add: expand_fun_eq cut_def)
paulson@15341
   354
paulson@15341
   355
lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)"
paulson@15341
   356
by (simp add: cut_def)
paulson@15341
   357
paulson@15341
   358
text{*Inductive characterization of wfrec combinator; for details see:  
paulson@15341
   359
John Harrison, "Inductive definitions: automation and application"*}
paulson@15341
   360
berghofe@22263
   361
lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. wfrec_rel R F x y"
paulson@15341
   362
apply (simp add: adm_wf_def)
paulson@15341
   363
apply (erule_tac a=x in wf_induct) 
paulson@15341
   364
apply (rule ex1I)
berghofe@22263
   365
apply (rule_tac g = "%x. THE y. wfrec_rel R F x y" in wfrec_rel.wfrecI)
paulson@15341
   366
apply (fast dest!: theI')
paulson@15341
   367
apply (erule wfrec_rel.cases, simp)
paulson@15341
   368
apply (erule allE, erule allE, erule allE, erule mp)
paulson@15341
   369
apply (fast intro: the_equality [symmetric])
paulson@15341
   370
done
paulson@15341
   371
paulson@15341
   372
lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
paulson@15341
   373
apply (simp add: adm_wf_def)
paulson@15341
   374
apply (intro strip)
paulson@15341
   375
apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
paulson@15341
   376
apply (rule refl)
paulson@15341
   377
done
paulson@15341
   378
paulson@15341
   379
lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"
paulson@15341
   380
apply (simp add: wfrec_def)
paulson@15341
   381
apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
paulson@15341
   382
apply (rule wfrec_rel.wfrecI)
paulson@15341
   383
apply (intro strip)
paulson@15341
   384
apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
paulson@15341
   385
done
paulson@15341
   386
paulson@15341
   387
paulson@15341
   388
text{** This form avoids giant explosions in proofs.  NOTE USE OF ==*}
paulson@15341
   389
lemma def_wfrec: "[| f==wfrec r H;  wf(r) |] ==> f(a) = H (cut f r a) a"
paulson@15341
   390
apply auto
paulson@15341
   391
apply (blast intro: wfrec)
paulson@15341
   392
done
paulson@15341
   393
paulson@15341
   394
wenzelm@17459
   395
subsection {* Code generator setup *}
wenzelm@17459
   396
wenzelm@17459
   397
consts_code
berghofe@17654
   398
  "wfrec"   ("\<module>wfrec?")
wenzelm@17459
   399
attach {*
berghofe@17654
   400
fun wfrec f x = f (wfrec f) x;
wenzelm@17459
   401
*}
wenzelm@17459
   402
wenzelm@17459
   403
paulson@15341
   404
subsection{*Variants for TFL: the Recdef Package*}
paulson@15341
   405
paulson@15341
   406
lemma tfl_wf_induct: "ALL R. wf R -->  
paulson@15341
   407
       (ALL P. (ALL x. (ALL y. (y,x):R --> P y) --> P x) --> (ALL x. P x))"
paulson@15341
   408
apply clarify
paulson@15341
   409
apply (rule_tac r = R and P = P and a = x in wf_induct, assumption, blast)
paulson@15341
   410
done
paulson@15341
   411
paulson@15341
   412
lemma tfl_cut_apply: "ALL f R. (x,a):R --> (cut f R a)(x) = f(x)"
paulson@15341
   413
apply clarify
paulson@15341
   414
apply (rule cut_apply, assumption)
paulson@15341
   415
done
paulson@15341
   416
paulson@15341
   417
lemma tfl_wfrec:
paulson@15341
   418
     "ALL M R f. (f=wfrec R M) --> wf R --> (ALL x. f x = M (cut f R x) x)"
paulson@15341
   419
apply clarify
paulson@15341
   420
apply (erule wfrec)
paulson@15341
   421
done
paulson@15341
   422
paulson@15341
   423
subsection {*LEAST and wellorderings*}
paulson@15341
   424
paulson@15341
   425
text{* See also @{text wf_linord_ex_has_least} and its consequences in
paulson@15341
   426
 @{text Wellfounded_Relations.ML}*}
paulson@15341
   427
paulson@15341
   428
lemma wellorder_Least_lemma [rule_format]:
paulson@15341
   429
     "P (k::'a::wellorder) --> P (LEAST x. P(x)) & (LEAST x. P(x)) <= k"
paulson@15341
   430
apply (rule_tac a = k in wf [THEN wf_induct])
paulson@15341
   431
apply (rule impI)
paulson@15341
   432
apply (rule classical)
paulson@15341
   433
apply (rule_tac s = x in Least_equality [THEN ssubst], auto)
paulson@15341
   434
apply (auto simp add: linorder_not_less [symmetric])
paulson@15341
   435
done
paulson@15341
   436
paulson@15341
   437
lemmas LeastI   = wellorder_Least_lemma [THEN conjunct1, standard]
paulson@15341
   438
lemmas Least_le = wellorder_Least_lemma [THEN conjunct2, standard]
paulson@15341
   439
nipkow@15950
   440
-- "The following 3 lemmas are due to Brian Huffman"
nipkow@15950
   441
lemma LeastI_ex: "EX x::'a::wellorder. P x ==> P (Least P)"
nipkow@15950
   442
apply (erule exE)
nipkow@15950
   443
apply (erule LeastI)
nipkow@15950
   444
done
nipkow@15950
   445
nipkow@15950
   446
lemma LeastI2:
nipkow@15950
   447
  "[| P (a::'a::wellorder); !!x. P x ==> Q x |] ==> Q (Least P)"
nipkow@15950
   448
by (blast intro: LeastI)
nipkow@15950
   449
nipkow@15950
   450
lemma LeastI2_ex:
nipkow@15950
   451
  "[| EX a::'a::wellorder. P a; !!x. P x ==> Q x |] ==> Q (Least P)"
nipkow@15950
   452
by (blast intro: LeastI_ex)
nipkow@15950
   453
paulson@15341
   454
lemma not_less_Least: "[| k < (LEAST x. P x) |] ==> ~P (k::'a::wellorder)"
paulson@15341
   455
apply (simp (no_asm_use) add: linorder_not_le [symmetric])
paulson@15341
   456
apply (erule contrapos_nn)
paulson@15341
   457
apply (erule Least_le)
paulson@15341
   458
done
paulson@15341
   459
paulson@15341
   460
ML
paulson@15341
   461
{*
paulson@15341
   462
val wf_def = thm "wf_def";
paulson@15341
   463
val wfUNIVI = thm "wfUNIVI";
paulson@15341
   464
val wfI = thm "wfI";
paulson@15341
   465
val wf_induct = thm "wf_induct";
paulson@15341
   466
val wf_not_sym = thm "wf_not_sym";
paulson@15341
   467
val wf_asym = thm "wf_asym";
paulson@15341
   468
val wf_not_refl = thm "wf_not_refl";
paulson@15341
   469
val wf_irrefl = thm "wf_irrefl";
paulson@15341
   470
val wf_trancl = thm "wf_trancl";
paulson@15341
   471
val wf_converse_trancl = thm "wf_converse_trancl";
paulson@15341
   472
val wf_eq_minimal = thm "wf_eq_minimal";
paulson@15341
   473
val wf_subset = thm "wf_subset";
paulson@15341
   474
val wf_empty = thm "wf_empty";
paulson@15341
   475
val wf_insert = thm "wf_insert";
paulson@15341
   476
val wf_UN = thm "wf_UN";
paulson@15341
   477
val wf_Union = thm "wf_Union";
paulson@15341
   478
val wf_Un = thm "wf_Un";
paulson@15341
   479
val wf_prod_fun_image = thm "wf_prod_fun_image";
paulson@15341
   480
val acyclicI = thm "acyclicI";
paulson@15341
   481
val wf_acyclic = thm "wf_acyclic";
paulson@15341
   482
val acyclic_insert = thm "acyclic_insert";
paulson@15341
   483
val acyclic_converse = thm "acyclic_converse";
paulson@15341
   484
val acyclic_impl_antisym_rtrancl = thm "acyclic_impl_antisym_rtrancl";
paulson@15341
   485
val acyclic_subset = thm "acyclic_subset";
paulson@15341
   486
val cuts_eq = thm "cuts_eq";
paulson@15341
   487
val cut_apply = thm "cut_apply";
paulson@15341
   488
val wfrec_unique = thm "wfrec_unique";
paulson@15341
   489
val wfrec = thm "wfrec";
paulson@15341
   490
val def_wfrec = thm "def_wfrec";
paulson@15341
   491
val tfl_wf_induct = thm "tfl_wf_induct";
paulson@15341
   492
val tfl_cut_apply = thm "tfl_cut_apply";
paulson@15341
   493
val tfl_wfrec = thm "tfl_wfrec";
paulson@15341
   494
val LeastI = thm "LeastI";
paulson@15341
   495
val Least_le = thm "Least_le";
paulson@15341
   496
val not_less_Least = thm "not_less_Least";
paulson@15341
   497
*}
oheimb@11137
   498
nipkow@10213
   499
end