src/HOL/WF.ML
author nipkow
Fri Oct 17 15:25:12 1997 +0200 (1997-10-17)
changeset 3919 c036caebfc75
parent 3708 56facaebf3e3
child 4059 59c1422c9da5
permissions -rw-r--r--
setloop split_tac -> addsplits
clasohm@1475
     1
(*  Title:      HOL/wf.ML
clasohm@923
     2
    ID:         $Id$
clasohm@1475
     3
    Author:     Tobias Nipkow, with minor changes by Konrad Slind
clasohm@1475
     4
    Copyright   1992  University of Cambridge/1995 TU Munich
clasohm@923
     5
paulson@3198
     6
Wellfoundedness, induction, and  recursion
clasohm@923
     7
*)
clasohm@923
     8
clasohm@923
     9
open WF;
clasohm@923
    10
nipkow@950
    11
val H_cong = read_instantiate [("f","H")] (standard(refl RS cong RS cong));
clasohm@923
    12
val H_cong1 = refl RS H_cong;
clasohm@923
    13
clasohm@923
    14
(*Restriction to domain A.  If r is well-founded over A then wf(r)*)
clasohm@923
    15
val [prem1,prem2] = goalw WF.thy [wf_def]
paulson@1642
    16
 "[| r <= A Times A;  \
clasohm@972
    17
\    !!x P. [| ! x. (! y. (y,x) : r --> P(y)) --> P(x);  x:A |] ==> P(x) |]  \
clasohm@923
    18
\ ==>  wf(r)";
paulson@3708
    19
by (Clarify_tac 1);
clasohm@923
    20
by (rtac allE 1);
clasohm@923
    21
by (assume_tac 1);
berghofe@1786
    22
by (best_tac (!claset addSEs [prem1 RS subsetD RS SigmaE2] addIs [prem2]) 1);
clasohm@923
    23
qed "wfI";
clasohm@923
    24
clasohm@923
    25
val major::prems = goalw WF.thy [wf_def]
clasohm@923
    26
    "[| wf(r);          \
clasohm@972
    27
\       !!x.[| ! y. (y,x): r --> P(y) |] ==> P(x) \
clasohm@923
    28
\    |]  ==>  P(a)";
clasohm@923
    29
by (rtac (major RS spec RS mp RS spec) 1);
paulson@2935
    30
by (blast_tac (!claset addIs prems) 1);
clasohm@923
    31
qed "wf_induct";
clasohm@923
    32
clasohm@923
    33
(*Perform induction on i, then prove the wf(r) subgoal using prems. *)
clasohm@923
    34
fun wf_ind_tac a prems i = 
clasohm@923
    35
    EVERY [res_inst_tac [("a",a)] wf_induct i,
clasohm@1465
    36
           rename_last_tac a ["1"] (i+1),
clasohm@1465
    37
           ares_tac prems i];
clasohm@923
    38
clasohm@972
    39
val prems = goal WF.thy "[| wf(r);  (a,x):r;  (x,a):r |] ==> P";
clasohm@972
    40
by (subgoal_tac "! x. (a,x):r --> (x,a):r --> P" 1);
paulson@2935
    41
by (blast_tac (!claset addIs prems) 1);
clasohm@923
    42
by (wf_ind_tac "a" prems 1);
paulson@2935
    43
by (Blast_tac 1);
clasohm@923
    44
qed "wf_asym";
clasohm@923
    45
clasohm@972
    46
val prems = goal WF.thy "[| wf(r);  (a,a): r |] ==> P";
clasohm@923
    47
by (rtac wf_asym 1);
clasohm@923
    48
by (REPEAT (resolve_tac prems 1));
paulson@1618
    49
qed "wf_irrefl";
clasohm@923
    50
clasohm@1475
    51
(*transitive closure of a wf relation is wf! *)
clasohm@923
    52
val [prem] = goal WF.thy "wf(r) ==> wf(r^+)";
clasohm@923
    53
by (rewtac wf_def);
paulson@3708
    54
by (Clarify_tac 1);
clasohm@923
    55
(*must retain the universal formula for later use!*)
clasohm@923
    56
by (rtac allE 1 THEN assume_tac 1);
clasohm@923
    57
by (etac mp 1);
clasohm@923
    58
by (res_inst_tac [("a","x")] (prem RS wf_induct) 1);
clasohm@923
    59
by (rtac (impI RS allI) 1);
clasohm@923
    60
by (etac tranclE 1);
paulson@2935
    61
by (Blast_tac 1);
paulson@2935
    62
by (Blast_tac 1);
clasohm@923
    63
qed "wf_trancl";
clasohm@923
    64
clasohm@923
    65
paulson@3198
    66
(*----------------------------------------------------------------------------
paulson@3198
    67
 * Minimal-element characterization of well-foundedness
paulson@3198
    68
 *---------------------------------------------------------------------------*)
paulson@3198
    69
paulson@3198
    70
val wfr::_ = goalw WF.thy [wf_def]
paulson@3198
    71
    "wf r ==> x:Q --> (? z:Q. ! y. (y,z):r --> y~:Q)";
paulson@3198
    72
by (rtac (wfr RS spec RS mp RS spec) 1);
paulson@3198
    73
by (Blast_tac 1);
paulson@3198
    74
val lemma1 = result();
paulson@3198
    75
paulson@3198
    76
goalw WF.thy [wf_def]
paulson@3198
    77
    "!!r. (! Q x. x:Q --> (? z:Q. ! y. (y,z):r --> y~:Q)) ==> wf r";
paulson@3708
    78
by (Clarify_tac 1);
paulson@3198
    79
by (dres_inst_tac [("x", "{x. ~ P x}")] spec 1);
paulson@3198
    80
by (Blast_tac 1);
paulson@3198
    81
val lemma2 = result();
paulson@3198
    82
paulson@3198
    83
goal WF.thy "wf r = (! Q x. x:Q --> (? z:Q. ! y. (y,z):r --> y~:Q))";
paulson@3198
    84
by (blast_tac (!claset addSIs [lemma1, lemma2]) 1);
paulson@3198
    85
qed "wf_eq_minimal";
paulson@3198
    86
nipkow@3413
    87
(*---------------------------------------------------------------------------
nipkow@3413
    88
 * Wellfoundedness of subsets
nipkow@3413
    89
 *---------------------------------------------------------------------------*)
nipkow@3413
    90
nipkow@3413
    91
goal thy "!!r. [| wf(r);  p<=r |] ==> wf(p)";
nipkow@3413
    92
by (full_simp_tac (!simpset addsimps [wf_eq_minimal]) 1);
nipkow@3413
    93
by (Fast_tac 1);
nipkow@3413
    94
qed "wf_subset";
nipkow@3413
    95
nipkow@3413
    96
(*---------------------------------------------------------------------------
nipkow@3413
    97
 * Wellfoundedness of the empty relation.
nipkow@3413
    98
 *---------------------------------------------------------------------------*)
nipkow@3413
    99
nipkow@3413
   100
goal thy "wf({})";
nipkow@3413
   101
by (simp_tac (!simpset addsimps [wf_def]) 1);
nipkow@3413
   102
qed "wf_empty";
nipkow@3413
   103
AddSIs [wf_empty];
nipkow@3413
   104
nipkow@3413
   105
(*---------------------------------------------------------------------------
nipkow@3413
   106
 * Wellfoundedness of `insert'
nipkow@3413
   107
 *---------------------------------------------------------------------------*)
nipkow@3413
   108
nipkow@3413
   109
goal WF.thy "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)";
paulson@3457
   110
by (rtac iffI 1);
paulson@3457
   111
 by (blast_tac (!claset addEs [wf_trancl RS wf_irrefl] addIs
nipkow@3413
   112
      [rtrancl_into_trancl1,wf_subset,impOfSubs rtrancl_mono]) 1);
paulson@3457
   113
by (asm_full_simp_tac (!simpset addsimps [wf_eq_minimal]) 1);
paulson@3457
   114
by (safe_tac (!claset));
paulson@3457
   115
by (EVERY1[rtac allE, atac, etac impE, Blast_tac]);
paulson@3457
   116
by (etac bexE 1);
paulson@3457
   117
by (rename_tac "a" 1);
paulson@3457
   118
by (case_tac "a = x" 1);
paulson@3457
   119
 by (res_inst_tac [("x","a")]bexI 2);
paulson@3457
   120
  by (assume_tac 3);
paulson@3457
   121
 by (Blast_tac 2);
paulson@3457
   122
by (case_tac "y:Q" 1);
paulson@3457
   123
 by (Blast_tac 2);
paulson@3457
   124
by (res_inst_tac [("x","{z. z:Q & (z,y) : r^*}")]allE 1);
paulson@3457
   125
 by (assume_tac 1);
paulson@3457
   126
by (fast_tac (!claset addIs [rtrancl_into_rtrancl2]) 1);
nipkow@3413
   127
qed "wf_insert";
nipkow@3413
   128
AddIffs [wf_insert];
nipkow@3413
   129
nipkow@3413
   130
(*** acyclic ***)
nipkow@3413
   131
nipkow@3413
   132
goalw WF.thy [acyclic_def]
nipkow@3413
   133
 "!!r. wf r ==> acyclic r";
paulson@3457
   134
by (blast_tac (!claset addEs [wf_trancl RS wf_irrefl]) 1);
nipkow@3413
   135
qed "wf_acyclic";
nipkow@3413
   136
nipkow@3413
   137
goalw WF.thy [acyclic_def]
nipkow@3413
   138
  "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)";
paulson@3457
   139
by (simp_tac (!simpset addsimps [trancl_insert]) 1);
paulson@3457
   140
by (blast_tac (!claset addEs [make_elim rtrancl_trans]) 1);
nipkow@3413
   141
qed "acyclic_insert";
nipkow@3413
   142
AddIffs [acyclic_insert];
nipkow@3413
   143
nipkow@3439
   144
goalw WF.thy [acyclic_def] "acyclic(r^-1) = acyclic r";
paulson@3457
   145
by (simp_tac (!simpset addsimps [trancl_inverse]) 1);
nipkow@3439
   146
qed "acyclic_inverse";
paulson@3198
   147
clasohm@923
   148
(** cut **)
clasohm@923
   149
clasohm@923
   150
(*This rewrite rule works upon formulae; thus it requires explicit use of
clasohm@923
   151
  H_cong to expose the equality*)
clasohm@923
   152
goalw WF.thy [cut_def]
clasohm@972
   153
    "(cut f r x = cut g r x) = (!y. (y,x):r --> f(y)=g(y))";
nipkow@3919
   154
by (simp_tac (HOL_ss addsimps [expand_fun_eq] addsplits [expand_if]) 1);
clasohm@1475
   155
qed "cuts_eq";
clasohm@923
   156
clasohm@972
   157
goalw WF.thy [cut_def] "!!x. (x,a):r ==> (cut f r a)(x) = f(x)";
paulson@1552
   158
by (asm_simp_tac HOL_ss 1);
clasohm@923
   159
qed "cut_apply";
clasohm@923
   160
clasohm@923
   161
(*** is_recfun ***)
clasohm@923
   162
clasohm@923
   163
goalw WF.thy [is_recfun_def,cut_def]
nipkow@3320
   164
    "!!f. [| is_recfun r H a f;  ~(b,a):r |] ==> f(b) = arbitrary";
clasohm@923
   165
by (etac ssubst 1);
paulson@1552
   166
by (asm_simp_tac HOL_ss 1);
clasohm@923
   167
qed "is_recfun_undef";
clasohm@923
   168
clasohm@923
   169
(*** NOTE! some simplifications need a different finish_tac!! ***)
clasohm@923
   170
fun indhyp_tac hyps =
clasohm@923
   171
    (cut_facts_tac hyps THEN'
clasohm@923
   172
       DEPTH_SOLVE_1 o (ares_tac [TrueI] ORELSE'
clasohm@1465
   173
                        eresolve_tac [transD, mp, allE]));
oheimb@2637
   174
val wf_super_ss = HOL_ss addSolver indhyp_tac;
clasohm@923
   175
clasohm@923
   176
val prems = goalw WF.thy [is_recfun_def,cut_def]
clasohm@1475
   177
    "[| wf(r);  trans(r);  is_recfun r H a f;  is_recfun r H b g |] ==> \
clasohm@972
   178
    \ (x,a):r --> (x,b):r --> f(x)=g(x)";
clasohm@923
   179
by (cut_facts_tac prems 1);
clasohm@923
   180
by (etac wf_induct 1);
clasohm@923
   181
by (REPEAT (rtac impI 1 ORELSE etac ssubst 1));
clasohm@923
   182
by (asm_simp_tac (wf_super_ss addcongs [if_cong]) 1);
nipkow@1485
   183
qed_spec_mp "is_recfun_equal";
clasohm@923
   184
clasohm@923
   185
clasohm@923
   186
val prems as [wfr,transr,recfa,recgb,_] = goalw WF.thy [cut_def]
clasohm@923
   187
    "[| wf(r);  trans(r); \
clasohm@1475
   188
\       is_recfun r H a f;  is_recfun r H b g;  (b,a):r |] ==> \
clasohm@923
   189
\    cut f r b = g";
clasohm@923
   190
val gundef = recgb RS is_recfun_undef
clasohm@923
   191
and fisg   = recgb RS (recfa RS (transr RS (wfr RS is_recfun_equal)));
clasohm@923
   192
by (cut_facts_tac prems 1);
clasohm@923
   193
by (rtac ext 1);
clasohm@923
   194
by (asm_simp_tac (wf_super_ss addsimps [gundef,fisg]
nipkow@3919
   195
                              addsplits [expand_if]) 1);
clasohm@923
   196
qed "is_recfun_cut";
clasohm@923
   197
clasohm@923
   198
(*** Main Existence Lemma -- Basic Properties of the_recfun ***)
clasohm@923
   199
clasohm@923
   200
val prems = goalw WF.thy [the_recfun_def]
clasohm@1475
   201
    "is_recfun r H a f ==> is_recfun r H a (the_recfun r H a)";
clasohm@1475
   202
by (res_inst_tac [("P", "is_recfun r H a")] selectI 1);
clasohm@923
   203
by (resolve_tac prems 1);
clasohm@923
   204
qed "is_the_recfun";
clasohm@923
   205
clasohm@923
   206
val prems = goal WF.thy
clasohm@1475
   207
 "[| wf(r);  trans(r) |] ==> is_recfun r H a (the_recfun r H a)";
clasohm@1475
   208
  by (cut_facts_tac prems 1);
clasohm@1475
   209
  by (wf_ind_tac "a" prems 1);
clasohm@1475
   210
  by (res_inst_tac [("f","cut (%y. H (the_recfun r H y) y) r a1")]
clasohm@1475
   211
                   is_the_recfun 1);
paulson@1552
   212
  by (rewtac is_recfun_def);
paulson@2031
   213
  by (stac cuts_eq 1);
clasohm@1475
   214
  by (rtac allI 1);
clasohm@1475
   215
  by (rtac impI 1);
clasohm@1475
   216
  by (res_inst_tac [("f1","H"),("x","y")](arg_cong RS fun_cong) 1);
clasohm@1475
   217
  by (subgoal_tac
clasohm@1475
   218
         "the_recfun r H y = cut(%x. H(cut(the_recfun r H y) r x) x) r y" 1);
clasohm@1475
   219
  by (etac allE 2);
clasohm@1475
   220
  by (dtac impE 2);
clasohm@1475
   221
  by (atac 2);
clasohm@1475
   222
  by (atac 3);
clasohm@1475
   223
  by (atac 2);
clasohm@1475
   224
  by (etac ssubst 1);
clasohm@1475
   225
  by (simp_tac (HOL_ss addsimps [cuts_eq]) 1);
clasohm@1475
   226
  by (rtac allI 1);
clasohm@1475
   227
  by (rtac impI 1);
clasohm@1475
   228
  by (asm_simp_tac (wf_super_ss addsimps[cut_apply,is_recfun_cut,cuts_eq]) 1);
clasohm@1475
   229
  by (res_inst_tac [("f1","H"),("x","ya")](arg_cong RS fun_cong) 1);
clasohm@1475
   230
  by (fold_tac [is_recfun_def]);
clasohm@1475
   231
  by (asm_simp_tac (wf_super_ss addsimps[cut_apply,is_recfun_cut,cuts_eq]) 1);
clasohm@923
   232
qed "unfold_the_recfun";
clasohm@923
   233
clasohm@1475
   234
val unwind1_the_recfun = rewrite_rule[is_recfun_def] unfold_the_recfun;
clasohm@923
   235
clasohm@1475
   236
(*--------------Old proof-----------------------------------------------------
clasohm@923
   237
val prems = goal WF.thy
clasohm@1475
   238
    "[| wf(r);  trans(r) |] ==> is_recfun r H a (the_recfun r H a)";
clasohm@1475
   239
by (cut_facts_tac prems 1);
clasohm@1475
   240
by (wf_ind_tac "a" prems 1);
clasohm@1475
   241
by (res_inst_tac [("f", "cut (%y. wftrec r H y) r a1")] is_the_recfun 1); 
clasohm@1475
   242
by (rewrite_goals_tac [is_recfun_def, wftrec_def]);
paulson@2031
   243
by (stac cuts_eq 1);
clasohm@1475
   244
(*Applying the substitution: must keep the quantified assumption!!*)
paulson@3708
   245
by (EVERY1 [Clarify_tac, rtac H_cong1, rtac allE, atac,
clasohm@1475
   246
            etac (mp RS ssubst), atac]); 
clasohm@1475
   247
by (fold_tac [is_recfun_def]);
clasohm@1475
   248
by (asm_simp_tac (wf_super_ss addsimps[cut_apply,is_recfun_cut,cuts_eq]) 1);
clasohm@1475
   249
qed "unfold_the_recfun";
clasohm@1475
   250
---------------------------------------------------------------------------*)
clasohm@923
   251
clasohm@923
   252
(** Removal of the premise trans(r) **)
clasohm@1475
   253
val th = rewrite_rule[is_recfun_def]
clasohm@1475
   254
                     (trans_trancl RSN (2,(wf_trancl RS unfold_the_recfun)));
clasohm@923
   255
clasohm@923
   256
goalw WF.thy [wfrec_def]
clasohm@1475
   257
    "!!r. wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a";
clasohm@1475
   258
by (rtac H_cong 1);
clasohm@1475
   259
by (rtac refl 2);
clasohm@1475
   260
by (simp_tac (HOL_ss addsimps [cuts_eq]) 1);
clasohm@1475
   261
by (rtac allI 1);
clasohm@1475
   262
by (rtac impI 1);
clasohm@1475
   263
by (simp_tac(HOL_ss addsimps [wfrec_def]) 1);
clasohm@1475
   264
by (res_inst_tac [("a1","a")] (th RS ssubst) 1);
clasohm@1475
   265
by (atac 1);
clasohm@1475
   266
by (forward_tac[wf_trancl] 1);
clasohm@1475
   267
by (forward_tac[r_into_trancl] 1);
clasohm@1475
   268
by (asm_simp_tac (HOL_ss addsimps [cut_apply]) 1);
clasohm@1475
   269
by (rtac H_cong 1);    (*expose the equality of cuts*)
clasohm@1475
   270
by (rtac refl 2);
clasohm@1475
   271
by (simp_tac (HOL_ss addsimps [cuts_eq, cut_apply, r_into_trancl]) 1);
paulson@3708
   272
by (Clarify_tac 1);
nipkow@1485
   273
by (res_inst_tac [("r","r^+")] is_recfun_equal 1);
clasohm@1475
   274
by (atac 1);
clasohm@1475
   275
by (rtac trans_trancl 1);
clasohm@1475
   276
by (rtac unfold_the_recfun 1);
clasohm@1475
   277
by (atac 1);
clasohm@1475
   278
by (rtac trans_trancl 1);
clasohm@1475
   279
by (rtac unfold_the_recfun 1);
clasohm@1475
   280
by (atac 1);
clasohm@1475
   281
by (rtac trans_trancl 1);
clasohm@1475
   282
by (rtac transD 1);
clasohm@1475
   283
by (rtac trans_trancl 1);
clasohm@1475
   284
by (forw_inst_tac [("a","ya")] r_into_trancl 1);
clasohm@1475
   285
by (atac 1);
clasohm@1475
   286
by (atac 1);
clasohm@1475
   287
by (forw_inst_tac [("a","ya")] r_into_trancl 1);
clasohm@1475
   288
by (atac 1);
clasohm@1475
   289
qed "wfrec";
clasohm@1475
   290
clasohm@1475
   291
(*--------------Old proof-----------------------------------------------------
clasohm@1475
   292
goalw WF.thy [wfrec_def]
clasohm@1475
   293
    "!!r. wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a";
clasohm@923
   294
by (etac (wf_trancl RS wftrec RS ssubst) 1);
clasohm@923
   295
by (rtac trans_trancl 1);
clasohm@923
   296
by (rtac (refl RS H_cong) 1);    (*expose the equality of cuts*)
clasohm@1475
   297
by (simp_tac (HOL_ss addsimps [cuts_eq, cut_apply, r_into_trancl]) 1);
clasohm@923
   298
qed "wfrec";
clasohm@1475
   299
---------------------------------------------------------------------------*)
clasohm@923
   300
clasohm@1475
   301
(*---------------------------------------------------------------------------
clasohm@1475
   302
 * This form avoids giant explosions in proofs.  NOTE USE OF == 
clasohm@1475
   303
 *---------------------------------------------------------------------------*)
clasohm@923
   304
val rew::prems = goal WF.thy
clasohm@1475
   305
    "[| f==wfrec r H;  wf(r) |] ==> f(a) = H (cut f r a) a";
clasohm@923
   306
by (rewtac rew);
clasohm@923
   307
by (REPEAT (resolve_tac (prems@[wfrec]) 1));
clasohm@923
   308
qed "def_wfrec";
clasohm@1475
   309
paulson@3198
   310
paulson@3198
   311
(**** TFL variants ****)
paulson@3198
   312
paulson@3198
   313
goal WF.thy
paulson@3198
   314
    "!R. wf R --> (!P. (!x. (!y. (y,x):R --> P y) --> P x) --> (!x. P x))";
paulson@3708
   315
by (Clarify_tac 1);
paulson@3198
   316
by (res_inst_tac [("r","R"),("P","P"), ("a","x")] wf_induct 1);
paulson@3198
   317
by (assume_tac 1);
paulson@3198
   318
by (Blast_tac 1);
paulson@3198
   319
qed"tfl_wf_induct";
paulson@3198
   320
paulson@3198
   321
goal WF.thy "!f R. (x,a):R --> (cut f R a)(x) = f(x)";
paulson@3708
   322
by (Clarify_tac 1);
paulson@3198
   323
by (rtac cut_apply 1);
paulson@3198
   324
by (assume_tac 1);
paulson@3198
   325
qed"tfl_cut_apply";
paulson@3198
   326
paulson@3198
   327
goal WF.thy "!M R f. (f=wfrec R M) --> wf R --> (!x. f x = M (cut f R x) x)";
paulson@3708
   328
by (Clarify_tac 1);
paulson@3708
   329
be wfrec 1;
paulson@3198
   330
qed "tfl_wfrec";