src/HOL/Wellfounded_Recursion.thy
author webertj
Mon Mar 07 19:30:53 2005 +0100 (2005-03-07)
changeset 15584 3478bb4f93ff
parent 15343 444bb25d3da0
child 15950 5c067c956a20
permissions -rw-r--r--
refute_params: default value itself=1 added (for type classes)
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@11328
    12
consts
berghofe@11328
    13
  wfrec_rel :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => ('a * 'b) set"
berghofe@11328
    14
berghofe@11328
    15
inductive "wfrec_rel R F"
paulson@15341
    16
intros
paulson@15341
    17
  wfrecI: "ALL z. (z, x) : R --> (z, g z) : wfrec_rel R F ==>
berghofe@11328
    18
            (x, F g x) : wfrec_rel R F"
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
nipkow@10213
    24
  acyclic :: "('a*'a)set => bool"
nipkow@10213
    25
  "acyclic r == !x. (x,x) ~: r^+"
nipkow@10213
    26
nipkow@10213
    27
  cut        :: "('a => 'b) => ('a * 'a)set => 'a => 'a => 'b"
nipkow@10213
    28
  "cut f r x == (%y. if (y,x):r then f y else arbitrary)"
nipkow@10213
    29
berghofe@11328
    30
  adm_wf :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => bool"
berghofe@11328
    31
  "adm_wf R F == ALL f g x.
berghofe@11328
    32
     (ALL z. (z, x) : R --> f z = g z) --> F f x = F g x"
nipkow@10213
    33
berghofe@11328
    34
  wfrec :: "('a * 'a) set => (('a => 'b) => 'a => 'b) => 'a => 'b"
paulson@11451
    35
  "wfrec R F == %x. THE y. (x, y) : wfrec_rel R (%f x. F (cut f R x) x)"
nipkow@10213
    36
paulson@15341
    37
axclass wellorder \<subseteq> linorder
paulson@15341
    38
  wf: "wf {(x,y::'a::ord). x<y}"
paulson@15341
    39
paulson@15341
    40
paulson@15341
    41
lemma wfUNIVI: 
paulson@15341
    42
   "(!!P x. (ALL x. (ALL y. (y,x) : r --> P(y)) --> P(x)) ==> P(x)) ==> wf(r)"
paulson@15341
    43
by (unfold wf_def, blast)
paulson@15341
    44
paulson@15341
    45
text{*Restriction to domain @{term A}.  
paulson@15341
    46
  If @{term r} is well-founded over @{term A} then @{term "wf r"}*}
paulson@15341
    47
lemma wfI: 
paulson@15341
    48
 "[| r <= A <*> A;   
paulson@15341
    49
     !!x P. [| ALL x. (ALL y. (y,x) : r --> P y) --> P x;  x:A |] ==> P x |]   
paulson@15341
    50
  ==>  wf r"
paulson@15341
    51
by (unfold wf_def, blast)
paulson@15341
    52
paulson@15341
    53
lemma wf_induct: 
paulson@15341
    54
    "[| wf(r);           
paulson@15341
    55
        !!x.[| ALL y. (y,x): r --> P(y) |] ==> P(x)  
paulson@15341
    56
     |]  ==>  P(a)"
paulson@15341
    57
by (unfold wf_def, blast)
paulson@15341
    58
paulson@15341
    59
lemma wf_not_sym [rule_format]: "wf(r) ==> ALL x. (a,x):r --> (x,a)~:r"
paulson@15341
    60
by (erule_tac a=a in wf_induct, blast)
paulson@15341
    61
paulson@15341
    62
(* [| wf r;  ~Z ==> (a,x) : r;  (x,a) ~: r ==> Z |] ==> Z *)
paulson@15341
    63
lemmas wf_asym = wf_not_sym [elim_format]
paulson@15341
    64
paulson@15341
    65
lemma wf_not_refl [simp]: "wf(r) ==> (a,a) ~: r"
paulson@15341
    66
by (blast elim: wf_asym)
paulson@15341
    67
paulson@15341
    68
(* [| wf r;  (a,a) ~: r ==> PROP W |] ==> PROP W *)
paulson@15341
    69
lemmas wf_irrefl = wf_not_refl [elim_format]
paulson@15341
    70
paulson@15341
    71
text{*transitive closure of a well-founded relation is well-founded! *}
paulson@15341
    72
lemma wf_trancl: "wf(r) ==> wf(r^+)"
paulson@15341
    73
apply (subst wf_def, clarify)
paulson@15341
    74
apply (rule allE, assumption)
paulson@15341
    75
  --{*Retains the universal formula for later use!*}
paulson@15341
    76
apply (erule mp)
paulson@15341
    77
apply (erule_tac a = x in wf_induct)
paulson@15341
    78
apply (blast elim: tranclE)
paulson@15341
    79
done
paulson@15341
    80
paulson@15341
    81
lemma wf_converse_trancl: "wf (r^-1) ==> wf ((r^+)^-1)"
paulson@15341
    82
apply (subst trancl_converse [symmetric])
paulson@15341
    83
apply (erule wf_trancl)
paulson@15341
    84
done
paulson@15341
    85
paulson@15341
    86
paulson@15341
    87
subsubsection{*Minimal-element characterization of well-foundedness*}
paulson@15341
    88
paulson@15341
    89
lemma lemma1: "wf r ==> x:Q --> (EX z:Q. ALL y. (y,z):r --> y~:Q)"
paulson@15341
    90
apply (unfold wf_def)
paulson@15341
    91
apply (drule spec)
paulson@15341
    92
apply (erule mp [THEN spec], blast)
paulson@15341
    93
done
paulson@15341
    94
paulson@15341
    95
lemma lemma2: "(ALL Q x. x:Q --> (EX z:Q. ALL y. (y,z):r --> y~:Q)) ==> wf r"
paulson@15341
    96
apply (unfold wf_def, clarify)
paulson@15341
    97
apply (drule_tac x = "{x. ~ P x}" in spec, blast)
paulson@15341
    98
done
paulson@15341
    99
paulson@15341
   100
lemma wf_eq_minimal: "wf r = (ALL Q x. x:Q --> (EX z:Q. ALL y. (y,z):r --> y~:Q))"
paulson@15341
   101
by (blast intro!: lemma1 lemma2)
paulson@15341
   102
paulson@15341
   103
subsubsection{*Other simple well-foundedness results*}
paulson@15341
   104
paulson@15341
   105
paulson@15341
   106
text{*Well-foundedness of subsets*}
paulson@15341
   107
lemma wf_subset: "[| wf(r);  p<=r |] ==> wf(p)"
paulson@15341
   108
apply (simp (no_asm_use) add: wf_eq_minimal)
paulson@15341
   109
apply fast
paulson@15341
   110
done
paulson@15341
   111
paulson@15341
   112
text{*Well-foundedness of the empty relation*}
paulson@15341
   113
lemma wf_empty [iff]: "wf({})"
paulson@15341
   114
by (simp add: wf_def)
paulson@15341
   115
paulson@15341
   116
text{*Well-foundedness of insert*}
paulson@15341
   117
lemma wf_insert [iff]: "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)"
paulson@15341
   118
apply (rule iffI)
paulson@15341
   119
 apply (blast elim: wf_trancl [THEN wf_irrefl]
paulson@15341
   120
              intro: rtrancl_into_trancl1 wf_subset 
paulson@15341
   121
                     rtrancl_mono [THEN [2] rev_subsetD])
paulson@15341
   122
apply (simp add: wf_eq_minimal, safe)
paulson@15341
   123
apply (rule allE, assumption, erule impE, blast) 
paulson@15341
   124
apply (erule bexE)
paulson@15341
   125
apply (rename_tac "a", case_tac "a = x")
paulson@15341
   126
 prefer 2
paulson@15341
   127
apply blast 
paulson@15341
   128
apply (case_tac "y:Q")
paulson@15341
   129
 prefer 2 apply blast
paulson@15341
   130
apply (rule_tac x = "{z. z:Q & (z,y) : r^*}" in allE)
paulson@15341
   131
 apply assumption
paulson@15341
   132
apply (erule_tac V = "ALL Q. (EX x. x : Q) --> ?P Q" in thin_rl) 
paulson@15341
   133
  --{*essential for speed*}
kleing@15343
   134
txt{*Blast with new substOccur fails*}
paulson@15341
   135
apply (fast intro: converse_rtrancl_into_rtrancl)
paulson@15341
   136
done
paulson@15341
   137
paulson@15341
   138
text{*Well-foundedness of image*}
paulson@15341
   139
lemma wf_prod_fun_image: "[| wf r; inj f |] ==> wf(prod_fun f f ` r)"
paulson@15341
   140
apply (simp only: wf_eq_minimal, clarify)
paulson@15341
   141
apply (case_tac "EX p. f p : Q")
paulson@15341
   142
apply (erule_tac x = "{p. f p : Q}" in allE)
paulson@15341
   143
apply (fast dest: inj_onD, blast)
paulson@15341
   144
done
paulson@15341
   145
paulson@15341
   146
paulson@15341
   147
subsubsection{*Well-Foundedness Results for Unions*}
paulson@15341
   148
paulson@15341
   149
text{*Well-foundedness of indexed union with disjoint domains and ranges*}
paulson@15341
   150
paulson@15341
   151
lemma wf_UN: "[| ALL i:I. wf(r i);  
paulson@15341
   152
         ALL i:I. ALL j:I. r i ~= r j --> Domain(r i) Int Range(r j) = {}  
paulson@15341
   153
      |] ==> wf(UN i:I. r i)"
paulson@15341
   154
apply (simp only: wf_eq_minimal, clarify)
paulson@15341
   155
apply (rename_tac A a, case_tac "EX i:I. EX a:A. EX b:A. (b,a) : r i")
paulson@15341
   156
 prefer 2
paulson@15341
   157
 apply force 
paulson@15341
   158
apply clarify
paulson@15341
   159
apply (drule bspec, assumption)  
paulson@15341
   160
apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r i) }" in allE)
paulson@15341
   161
apply (blast elim!: allE)  
paulson@15341
   162
done
paulson@15341
   163
paulson@15341
   164
lemma wf_Union: 
paulson@15341
   165
 "[| ALL r:R. wf r;  
paulson@15341
   166
     ALL r:R. ALL s:R. r ~= s --> Domain r Int Range s = {}  
paulson@15341
   167
  |] ==> wf(Union R)"
paulson@15341
   168
apply (simp add: Union_def)
paulson@15341
   169
apply (blast intro: wf_UN)
paulson@15341
   170
done
paulson@15341
   171
paulson@15341
   172
(*Intuition: we find an (R u S)-min element of a nonempty subset A
paulson@15341
   173
             by case distinction.
paulson@15341
   174
  1. There is a step a -R-> b with a,b : A.
paulson@15341
   175
     Pick an R-min element z of the (nonempty) set {a:A | EX b:A. a -R-> b}.
paulson@15341
   176
     By definition, there is z':A s.t. z -R-> z'. Because z is R-min in the
paulson@15341
   177
     subset, z' must be R-min in A. Because z' has an R-predecessor, it cannot
paulson@15341
   178
     have an S-successor and is thus S-min in A as well.
paulson@15341
   179
  2. There is no such step.
paulson@15341
   180
     Pick an S-min element of A. In this case it must be an R-min
paulson@15341
   181
     element of A as well.
paulson@15341
   182
paulson@15341
   183
*)
paulson@15341
   184
lemma wf_Un:
paulson@15341
   185
     "[| wf r; wf s; Domain r Int Range s = {} |] ==> wf(r Un s)"
paulson@15341
   186
apply (simp only: wf_eq_minimal, clarify) 
paulson@15341
   187
apply (rename_tac A a)
paulson@15341
   188
apply (case_tac "EX a:A. EX b:A. (b,a) : r") 
paulson@15341
   189
 prefer 2
paulson@15341
   190
 apply simp
paulson@15341
   191
 apply (drule_tac x=A in spec)+
paulson@15341
   192
 apply blast 
paulson@15341
   193
apply (erule_tac x="{a. a:A & (EX b:A. (b,a) : r) }" in allE)+
paulson@15341
   194
apply (blast elim!: allE)  
paulson@15341
   195
done
paulson@15341
   196
paulson@15341
   197
subsubsection {*acyclic*}
paulson@15341
   198
paulson@15341
   199
lemma acyclicI: "ALL x. (x, x) ~: r^+ ==> acyclic r"
paulson@15341
   200
by (simp add: acyclic_def)
paulson@15341
   201
paulson@15341
   202
lemma wf_acyclic: "wf r ==> acyclic r"
paulson@15341
   203
apply (simp add: acyclic_def)
paulson@15341
   204
apply (blast elim: wf_trancl [THEN wf_irrefl])
paulson@15341
   205
done
paulson@15341
   206
paulson@15341
   207
lemma acyclic_insert [iff]:
paulson@15341
   208
     "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)"
paulson@15341
   209
apply (simp add: acyclic_def trancl_insert)
paulson@15341
   210
apply (blast intro: rtrancl_trans)
paulson@15341
   211
done
paulson@15341
   212
paulson@15341
   213
lemma acyclic_converse [iff]: "acyclic(r^-1) = acyclic r"
paulson@15341
   214
by (simp add: acyclic_def trancl_converse)
paulson@15341
   215
paulson@15341
   216
lemma acyclic_impl_antisym_rtrancl: "acyclic r ==> antisym(r^*)"
paulson@15341
   217
apply (simp add: acyclic_def antisym_def)
paulson@15341
   218
apply (blast elim: rtranclE intro: rtrancl_into_trancl1 rtrancl_trancl_trancl)
paulson@15341
   219
done
paulson@15341
   220
paulson@15341
   221
(* Other direction:
paulson@15341
   222
acyclic = no loops
paulson@15341
   223
antisym = only self loops
paulson@15341
   224
Goalw [acyclic_def,antisym_def] "antisym( r^* ) ==> acyclic(r - Id)
paulson@15341
   225
==> antisym( r^* ) = acyclic(r - Id)";
paulson@15341
   226
*)
paulson@15341
   227
paulson@15341
   228
lemma acyclic_subset: "[| acyclic s; r <= s |] ==> acyclic r"
paulson@15341
   229
apply (simp add: acyclic_def)
paulson@15341
   230
apply (blast intro: trancl_mono)
paulson@15341
   231
done
paulson@15341
   232
paulson@15341
   233
paulson@15341
   234
subsection{*Well-Founded Recursion*}
paulson@15341
   235
paulson@15341
   236
text{*cut*}
paulson@15341
   237
paulson@15341
   238
lemma cuts_eq: "(cut f r x = cut g r x) = (ALL y. (y,x):r --> f(y)=g(y))"
paulson@15341
   239
by (simp add: expand_fun_eq cut_def)
paulson@15341
   240
paulson@15341
   241
lemma cut_apply: "(x,a):r ==> (cut f r a)(x) = f(x)"
paulson@15341
   242
by (simp add: cut_def)
paulson@15341
   243
paulson@15341
   244
text{*Inductive characterization of wfrec combinator; for details see:  
paulson@15341
   245
John Harrison, "Inductive definitions: automation and application"*}
paulson@15341
   246
paulson@15341
   247
lemma wfrec_unique: "[| adm_wf R F; wf R |] ==> EX! y. (x, y) : wfrec_rel R F"
paulson@15341
   248
apply (simp add: adm_wf_def)
paulson@15341
   249
apply (erule_tac a=x in wf_induct) 
paulson@15341
   250
apply (rule ex1I)
paulson@15341
   251
apply (rule_tac g = "%x. THE y. (x, y) : wfrec_rel R F" in wfrec_rel.wfrecI)
paulson@15341
   252
apply (fast dest!: theI')
paulson@15341
   253
apply (erule wfrec_rel.cases, simp)
paulson@15341
   254
apply (erule allE, erule allE, erule allE, erule mp)
paulson@15341
   255
apply (fast intro: the_equality [symmetric])
paulson@15341
   256
done
paulson@15341
   257
paulson@15341
   258
lemma adm_lemma: "adm_wf R (%f x. F (cut f R x) x)"
paulson@15341
   259
apply (simp add: adm_wf_def)
paulson@15341
   260
apply (intro strip)
paulson@15341
   261
apply (rule cuts_eq [THEN iffD2, THEN subst], assumption)
paulson@15341
   262
apply (rule refl)
paulson@15341
   263
done
paulson@15341
   264
paulson@15341
   265
lemma wfrec: "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a"
paulson@15341
   266
apply (simp add: wfrec_def)
paulson@15341
   267
apply (rule adm_lemma [THEN wfrec_unique, THEN the1_equality], assumption)
paulson@15341
   268
apply (rule wfrec_rel.wfrecI)
paulson@15341
   269
apply (intro strip)
paulson@15341
   270
apply (erule adm_lemma [THEN wfrec_unique, THEN theI'])
paulson@15341
   271
done
paulson@15341
   272
paulson@15341
   273
paulson@15341
   274
text{** This form avoids giant explosions in proofs.  NOTE USE OF ==*}
paulson@15341
   275
lemma def_wfrec: "[| f==wfrec r H;  wf(r) |] ==> f(a) = H (cut f r a) a"
paulson@15341
   276
apply auto
paulson@15341
   277
apply (blast intro: wfrec)
paulson@15341
   278
done
paulson@15341
   279
paulson@15341
   280
paulson@15341
   281
subsection{*Variants for TFL: the Recdef Package*}
paulson@15341
   282
paulson@15341
   283
lemma tfl_wf_induct: "ALL R. wf R -->  
paulson@15341
   284
       (ALL P. (ALL x. (ALL y. (y,x):R --> P y) --> P x) --> (ALL x. P x))"
paulson@15341
   285
apply clarify
paulson@15341
   286
apply (rule_tac r = R and P = P and a = x in wf_induct, assumption, blast)
paulson@15341
   287
done
paulson@15341
   288
paulson@15341
   289
lemma tfl_cut_apply: "ALL f R. (x,a):R --> (cut f R a)(x) = f(x)"
paulson@15341
   290
apply clarify
paulson@15341
   291
apply (rule cut_apply, assumption)
paulson@15341
   292
done
paulson@15341
   293
paulson@15341
   294
lemma tfl_wfrec:
paulson@15341
   295
     "ALL M R f. (f=wfrec R M) --> wf R --> (ALL x. f x = M (cut f R x) x)"
paulson@15341
   296
apply clarify
paulson@15341
   297
apply (erule wfrec)
paulson@15341
   298
done
paulson@15341
   299
paulson@15341
   300
subsection {*LEAST and wellorderings*}
paulson@15341
   301
paulson@15341
   302
text{* See also @{text wf_linord_ex_has_least} and its consequences in
paulson@15341
   303
 @{text Wellfounded_Relations.ML}*}
paulson@15341
   304
paulson@15341
   305
lemma wellorder_Least_lemma [rule_format]:
paulson@15341
   306
     "P (k::'a::wellorder) --> P (LEAST x. P(x)) & (LEAST x. P(x)) <= k"
paulson@15341
   307
apply (rule_tac a = k in wf [THEN wf_induct])
paulson@15341
   308
apply (rule impI)
paulson@15341
   309
apply (rule classical)
paulson@15341
   310
apply (rule_tac s = x in Least_equality [THEN ssubst], auto)
paulson@15341
   311
apply (auto simp add: linorder_not_less [symmetric])
paulson@15341
   312
done
paulson@15341
   313
paulson@15341
   314
lemmas LeastI   = wellorder_Least_lemma [THEN conjunct1, standard]
paulson@15341
   315
lemmas Least_le = wellorder_Least_lemma [THEN conjunct2, standard]
paulson@15341
   316
paulson@15341
   317
lemma not_less_Least: "[| k < (LEAST x. P x) |] ==> ~P (k::'a::wellorder)"
paulson@15341
   318
apply (simp (no_asm_use) add: linorder_not_le [symmetric])
paulson@15341
   319
apply (erule contrapos_nn)
paulson@15341
   320
apply (erule Least_le)
paulson@15341
   321
done
paulson@15341
   322
paulson@15341
   323
ML
paulson@15341
   324
{*
paulson@15341
   325
val wf_def = thm "wf_def";
paulson@15341
   326
val wfUNIVI = thm "wfUNIVI";
paulson@15341
   327
val wfI = thm "wfI";
paulson@15341
   328
val wf_induct = thm "wf_induct";
paulson@15341
   329
val wf_not_sym = thm "wf_not_sym";
paulson@15341
   330
val wf_asym = thm "wf_asym";
paulson@15341
   331
val wf_not_refl = thm "wf_not_refl";
paulson@15341
   332
val wf_irrefl = thm "wf_irrefl";
paulson@15341
   333
val wf_trancl = thm "wf_trancl";
paulson@15341
   334
val wf_converse_trancl = thm "wf_converse_trancl";
paulson@15341
   335
val wf_eq_minimal = thm "wf_eq_minimal";
paulson@15341
   336
val wf_subset = thm "wf_subset";
paulson@15341
   337
val wf_empty = thm "wf_empty";
paulson@15341
   338
val wf_insert = thm "wf_insert";
paulson@15341
   339
val wf_UN = thm "wf_UN";
paulson@15341
   340
val wf_Union = thm "wf_Union";
paulson@15341
   341
val wf_Un = thm "wf_Un";
paulson@15341
   342
val wf_prod_fun_image = thm "wf_prod_fun_image";
paulson@15341
   343
val acyclicI = thm "acyclicI";
paulson@15341
   344
val wf_acyclic = thm "wf_acyclic";
paulson@15341
   345
val acyclic_insert = thm "acyclic_insert";
paulson@15341
   346
val acyclic_converse = thm "acyclic_converse";
paulson@15341
   347
val acyclic_impl_antisym_rtrancl = thm "acyclic_impl_antisym_rtrancl";
paulson@15341
   348
val acyclic_subset = thm "acyclic_subset";
paulson@15341
   349
val cuts_eq = thm "cuts_eq";
paulson@15341
   350
val cut_apply = thm "cut_apply";
paulson@15341
   351
val wfrec_unique = thm "wfrec_unique";
paulson@15341
   352
val wfrec = thm "wfrec";
paulson@15341
   353
val def_wfrec = thm "def_wfrec";
paulson@15341
   354
val tfl_wf_induct = thm "tfl_wf_induct";
paulson@15341
   355
val tfl_cut_apply = thm "tfl_cut_apply";
paulson@15341
   356
val tfl_wfrec = thm "tfl_wfrec";
paulson@15341
   357
val LeastI = thm "LeastI";
paulson@15341
   358
val Least_le = thm "Least_le";
paulson@15341
   359
val not_less_Least = thm "not_less_Least";
paulson@15341
   360
*}
oheimb@11137
   361
nipkow@10213
   362
end