src/HOL/WF.ML
author paulson
Thu Aug 06 15:48:13 1998 +0200 (1998-08-06)
changeset 5278 a903b66822e2
parent 5148 74919e8f221c
child 5281 f4d16517b360
permissions -rw-r--r--
even more tidying of Goal commands
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);
wenzelm@4089
    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);
wenzelm@4089
    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);
wenzelm@4089
    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
oheimb@4762
    66
val wf_converse_trancl = prove_goal thy 
oheimb@4762
    67
"!!X. wf (r^-1) ==> wf ((r^+)^-1)" (K [
oheimb@4762
    68
	stac (trancl_converse RS sym) 1,
oheimb@4762
    69
	etac wf_trancl 1]);
oheimb@4762
    70
paulson@3198
    71
(*----------------------------------------------------------------------------
paulson@3198
    72
 * Minimal-element characterization of well-foundedness
paulson@3198
    73
 *---------------------------------------------------------------------------*)
paulson@3198
    74
paulson@3198
    75
val wfr::_ = goalw WF.thy [wf_def]
paulson@3198
    76
    "wf r ==> x:Q --> (? z:Q. ! y. (y,z):r --> y~:Q)";
paulson@3198
    77
by (rtac (wfr RS spec RS mp RS spec) 1);
paulson@3198
    78
by (Blast_tac 1);
paulson@3198
    79
val lemma1 = result();
paulson@3198
    80
wenzelm@5069
    81
Goalw [wf_def]
paulson@5148
    82
    "(! Q x. x:Q --> (? z:Q. ! y. (y,z):r --> y~:Q)) ==> wf r";
paulson@3708
    83
by (Clarify_tac 1);
paulson@3198
    84
by (dres_inst_tac [("x", "{x. ~ P x}")] spec 1);
paulson@3198
    85
by (Blast_tac 1);
paulson@3198
    86
val lemma2 = result();
paulson@3198
    87
wenzelm@5069
    88
Goal "wf r = (! Q x. x:Q --> (? z:Q. ! y. (y,z):r --> y~:Q))";
wenzelm@4089
    89
by (blast_tac (claset() addSIs [lemma1, lemma2]) 1);
paulson@3198
    90
qed "wf_eq_minimal";
paulson@3198
    91
nipkow@3413
    92
(*---------------------------------------------------------------------------
nipkow@3413
    93
 * Wellfoundedness of subsets
nipkow@3413
    94
 *---------------------------------------------------------------------------*)
nipkow@3413
    95
paulson@5143
    96
Goal "[| wf(r);  p<=r |] ==> wf(p)";
wenzelm@4089
    97
by (full_simp_tac (simpset() addsimps [wf_eq_minimal]) 1);
nipkow@3413
    98
by (Fast_tac 1);
nipkow@3413
    99
qed "wf_subset";
nipkow@3413
   100
nipkow@3413
   101
(*---------------------------------------------------------------------------
nipkow@3413
   102
 * Wellfoundedness of the empty relation.
nipkow@3413
   103
 *---------------------------------------------------------------------------*)
nipkow@3413
   104
wenzelm@5069
   105
Goal "wf({})";
wenzelm@4089
   106
by (simp_tac (simpset() addsimps [wf_def]) 1);
nipkow@3413
   107
qed "wf_empty";
nipkow@3413
   108
AddSIs [wf_empty];
nipkow@3413
   109
nipkow@3413
   110
(*---------------------------------------------------------------------------
nipkow@3413
   111
 * Wellfoundedness of `insert'
nipkow@3413
   112
 *---------------------------------------------------------------------------*)
nipkow@3413
   113
wenzelm@5069
   114
Goal "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)";
paulson@3457
   115
by (rtac iffI 1);
paulson@4350
   116
 by (blast_tac (claset() addEs [wf_trancl RS wf_irrefl] 
paulson@4350
   117
	addIs [rtrancl_into_trancl1,wf_subset,impOfSubs rtrancl_mono]) 1);
wenzelm@4089
   118
by (asm_full_simp_tac (simpset() addsimps [wf_eq_minimal]) 1);
paulson@4153
   119
by Safe_tac;
paulson@3457
   120
by (EVERY1[rtac allE, atac, etac impE, Blast_tac]);
paulson@3457
   121
by (etac bexE 1);
paulson@3457
   122
by (rename_tac "a" 1);
paulson@3457
   123
by (case_tac "a = x" 1);
paulson@3457
   124
 by (res_inst_tac [("x","a")]bexI 2);
paulson@3457
   125
  by (assume_tac 3);
paulson@3457
   126
 by (Blast_tac 2);
paulson@3457
   127
by (case_tac "y:Q" 1);
paulson@3457
   128
 by (Blast_tac 2);
paulson@4059
   129
by (res_inst_tac [("x","{z. z:Q & (z,y) : r^*}")] allE 1);
paulson@3457
   130
 by (assume_tac 1);
paulson@4059
   131
by (thin_tac "! Q. (? x. x : Q) --> ?P Q" 1);	(*essential for speed*)
paulson@4350
   132
(*Blast_tac with new substOccur fails*)
paulson@4350
   133
by (best_tac (claset() addIs [rtrancl_into_rtrancl2]) 1);
nipkow@3413
   134
qed "wf_insert";
nipkow@3413
   135
AddIffs [wf_insert];
nipkow@3413
   136
nipkow@3413
   137
(*** acyclic ***)
nipkow@3413
   138
oheimb@4750
   139
val acyclicI = prove_goalw WF.thy [acyclic_def] 
oheimb@4750
   140
"!!r. !x. (x, x) ~: r^+ ==> acyclic r" (K [atac 1]);
oheimb@4750
   141
wenzelm@5069
   142
Goalw [acyclic_def]
paulson@5148
   143
 "wf r ==> acyclic r";
wenzelm@4089
   144
by (blast_tac (claset() addEs [wf_trancl RS wf_irrefl]) 1);
nipkow@3413
   145
qed "wf_acyclic";
nipkow@3413
   146
wenzelm@5069
   147
Goalw [acyclic_def]
nipkow@3413
   148
  "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)";
wenzelm@4089
   149
by (simp_tac (simpset() addsimps [trancl_insert]) 1);
wenzelm@4089
   150
by (blast_tac (claset() addEs [make_elim rtrancl_trans]) 1);
nipkow@3413
   151
qed "acyclic_insert";
nipkow@3413
   152
AddIffs [acyclic_insert];
nipkow@3413
   153
wenzelm@5069
   154
Goalw [acyclic_def] "acyclic(r^-1) = acyclic r";
paulson@4746
   155
by (simp_tac (simpset() addsimps [trancl_converse]) 1);
paulson@4746
   156
qed "acyclic_converse";
paulson@3198
   157
clasohm@923
   158
(** cut **)
clasohm@923
   159
clasohm@923
   160
(*This rewrite rule works upon formulae; thus it requires explicit use of
clasohm@923
   161
  H_cong to expose the equality*)
wenzelm@5069
   162
Goalw [cut_def]
clasohm@972
   163
    "(cut f r x = cut g r x) = (!y. (y,x):r --> f(y)=g(y))";
nipkow@4686
   164
by (simp_tac (HOL_ss addsimps [expand_fun_eq]) 1);
clasohm@1475
   165
qed "cuts_eq";
clasohm@923
   166
paulson@5143
   167
Goalw [cut_def] "(x,a):r ==> (cut f r a)(x) = f(x)";
paulson@1552
   168
by (asm_simp_tac HOL_ss 1);
clasohm@923
   169
qed "cut_apply";
clasohm@923
   170
clasohm@923
   171
(*** is_recfun ***)
clasohm@923
   172
wenzelm@5069
   173
Goalw [is_recfun_def,cut_def]
paulson@5148
   174
    "[| is_recfun r H a f;  ~(b,a):r |] ==> f(b) = arbitrary";
clasohm@923
   175
by (etac ssubst 1);
paulson@1552
   176
by (asm_simp_tac HOL_ss 1);
clasohm@923
   177
qed "is_recfun_undef";
clasohm@923
   178
clasohm@923
   179
(*** NOTE! some simplifications need a different finish_tac!! ***)
clasohm@923
   180
fun indhyp_tac hyps =
clasohm@923
   181
    (cut_facts_tac hyps THEN'
clasohm@923
   182
       DEPTH_SOLVE_1 o (ares_tac [TrueI] ORELSE'
clasohm@1465
   183
                        eresolve_tac [transD, mp, allE]));
oheimb@2637
   184
val wf_super_ss = HOL_ss addSolver indhyp_tac;
clasohm@923
   185
clasohm@923
   186
val prems = goalw WF.thy [is_recfun_def,cut_def]
clasohm@1475
   187
    "[| wf(r);  trans(r);  is_recfun r H a f;  is_recfun r H b g |] ==> \
clasohm@972
   188
    \ (x,a):r --> (x,b):r --> f(x)=g(x)";
clasohm@923
   189
by (cut_facts_tac prems 1);
clasohm@923
   190
by (etac wf_induct 1);
clasohm@923
   191
by (REPEAT (rtac impI 1 ORELSE etac ssubst 1));
clasohm@923
   192
by (asm_simp_tac (wf_super_ss addcongs [if_cong]) 1);
nipkow@1485
   193
qed_spec_mp "is_recfun_equal";
clasohm@923
   194
clasohm@923
   195
clasohm@923
   196
val prems as [wfr,transr,recfa,recgb,_] = goalw WF.thy [cut_def]
clasohm@923
   197
    "[| wf(r);  trans(r); \
clasohm@1475
   198
\       is_recfun r H a f;  is_recfun r H b g;  (b,a):r |] ==> \
clasohm@923
   199
\    cut f r b = g";
clasohm@923
   200
val gundef = recgb RS is_recfun_undef
clasohm@923
   201
and fisg   = recgb RS (recfa RS (transr RS (wfr RS is_recfun_equal)));
clasohm@923
   202
by (cut_facts_tac prems 1);
clasohm@923
   203
by (rtac ext 1);
nipkow@4686
   204
by (asm_simp_tac (wf_super_ss addsimps [gundef,fisg]) 1);
clasohm@923
   205
qed "is_recfun_cut";
clasohm@923
   206
clasohm@923
   207
(*** Main Existence Lemma -- Basic Properties of the_recfun ***)
clasohm@923
   208
clasohm@923
   209
val prems = goalw WF.thy [the_recfun_def]
clasohm@1475
   210
    "is_recfun r H a f ==> is_recfun r H a (the_recfun r H a)";
clasohm@1475
   211
by (res_inst_tac [("P", "is_recfun r H a")] selectI 1);
clasohm@923
   212
by (resolve_tac prems 1);
clasohm@923
   213
qed "is_the_recfun";
clasohm@923
   214
clasohm@923
   215
val prems = goal WF.thy
nipkow@4821
   216
 "!!r. [| wf(r);  trans(r) |] ==> is_recfun r H a (the_recfun r H a)";
nipkow@4821
   217
by (wf_ind_tac "a" prems 1);
nipkow@4821
   218
by (res_inst_tac [("f","cut (%y. H (the_recfun r H y) y) r a1")]
nipkow@4821
   219
                 is_the_recfun 1);
nipkow@4821
   220
by (rewtac is_recfun_def);
nipkow@4821
   221
by (stac cuts_eq 1);
nipkow@4821
   222
by (Clarify_tac 1);
nipkow@4821
   223
by (rtac (refl RSN (2,H_cong)) 1);
nipkow@4821
   224
by (subgoal_tac
clasohm@1475
   225
         "the_recfun r H y = cut(%x. H(cut(the_recfun r H y) r x) x) r y" 1);
nipkow@4821
   226
 by (etac allE 2);
nipkow@4821
   227
 by (dtac impE 2);
nipkow@4821
   228
   by (atac 2);
clasohm@1475
   229
  by (atac 3);
nipkow@4821
   230
 by (atac 2);
nipkow@4821
   231
by (etac ssubst 1);
nipkow@4821
   232
by (simp_tac (HOL_ss addsimps [cuts_eq]) 1);
nipkow@4821
   233
by (Clarify_tac 1);
nipkow@4821
   234
by (stac cut_apply 1);
wenzelm@5132
   235
 by (fast_tac (claset() addDs [transD]) 1);
nipkow@4821
   236
by (rtac (refl RSN (2,H_cong)) 1);
nipkow@4821
   237
by (fold_tac [is_recfun_def]);
nipkow@4821
   238
by (asm_simp_tac (wf_super_ss addsimps[is_recfun_cut]) 1);
clasohm@923
   239
qed "unfold_the_recfun";
clasohm@923
   240
clasohm@1475
   241
val unwind1_the_recfun = rewrite_rule[is_recfun_def] unfold_the_recfun;
clasohm@923
   242
clasohm@1475
   243
(*--------------Old proof-----------------------------------------------------
clasohm@923
   244
val prems = goal WF.thy
clasohm@1475
   245
    "[| wf(r);  trans(r) |] ==> is_recfun r H a (the_recfun r H a)";
clasohm@1475
   246
by (cut_facts_tac prems 1);
clasohm@1475
   247
by (wf_ind_tac "a" prems 1);
clasohm@1475
   248
by (res_inst_tac [("f", "cut (%y. wftrec r H y) r a1")] is_the_recfun 1); 
clasohm@1475
   249
by (rewrite_goals_tac [is_recfun_def, wftrec_def]);
paulson@2031
   250
by (stac cuts_eq 1);
clasohm@1475
   251
(*Applying the substitution: must keep the quantified assumption!!*)
paulson@3708
   252
by (EVERY1 [Clarify_tac, rtac H_cong1, rtac allE, atac,
clasohm@1475
   253
            etac (mp RS ssubst), atac]); 
clasohm@1475
   254
by (fold_tac [is_recfun_def]);
clasohm@1475
   255
by (asm_simp_tac (wf_super_ss addsimps[cut_apply,is_recfun_cut,cuts_eq]) 1);
clasohm@1475
   256
qed "unfold_the_recfun";
clasohm@1475
   257
---------------------------------------------------------------------------*)
clasohm@923
   258
clasohm@923
   259
(** Removal of the premise trans(r) **)
clasohm@1475
   260
val th = rewrite_rule[is_recfun_def]
clasohm@1475
   261
                     (trans_trancl RSN (2,(wf_trancl RS unfold_the_recfun)));
clasohm@923
   262
wenzelm@5069
   263
Goalw [wfrec_def]
paulson@5148
   264
    "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a";
clasohm@1475
   265
by (rtac H_cong 1);
clasohm@1475
   266
by (rtac refl 2);
clasohm@1475
   267
by (simp_tac (HOL_ss addsimps [cuts_eq]) 1);
clasohm@1475
   268
by (rtac allI 1);
clasohm@1475
   269
by (rtac impI 1);
clasohm@1475
   270
by (simp_tac(HOL_ss addsimps [wfrec_def]) 1);
clasohm@1475
   271
by (res_inst_tac [("a1","a")] (th RS ssubst) 1);
clasohm@1475
   272
by (atac 1);
clasohm@1475
   273
by (forward_tac[wf_trancl] 1);
clasohm@1475
   274
by (forward_tac[r_into_trancl] 1);
clasohm@1475
   275
by (asm_simp_tac (HOL_ss addsimps [cut_apply]) 1);
clasohm@1475
   276
by (rtac H_cong 1);    (*expose the equality of cuts*)
clasohm@1475
   277
by (rtac refl 2);
clasohm@1475
   278
by (simp_tac (HOL_ss addsimps [cuts_eq, cut_apply, r_into_trancl]) 1);
paulson@3708
   279
by (Clarify_tac 1);
nipkow@1485
   280
by (res_inst_tac [("r","r^+")] is_recfun_equal 1);
clasohm@1475
   281
by (atac 1);
clasohm@1475
   282
by (rtac trans_trancl 1);
clasohm@1475
   283
by (rtac unfold_the_recfun 1);
clasohm@1475
   284
by (atac 1);
clasohm@1475
   285
by (rtac trans_trancl 1);
clasohm@1475
   286
by (rtac unfold_the_recfun 1);
clasohm@1475
   287
by (atac 1);
clasohm@1475
   288
by (rtac trans_trancl 1);
clasohm@1475
   289
by (rtac transD 1);
clasohm@1475
   290
by (rtac trans_trancl 1);
oheimb@4762
   291
by (forw_inst_tac [("p","(ya,y)")] r_into_trancl 1);
clasohm@1475
   292
by (atac 1);
clasohm@1475
   293
by (atac 1);
oheimb@4762
   294
by (forw_inst_tac [("p","(ya,y)")] r_into_trancl 1);
clasohm@1475
   295
by (atac 1);
clasohm@1475
   296
qed "wfrec";
clasohm@1475
   297
clasohm@1475
   298
(*--------------Old proof-----------------------------------------------------
wenzelm@5069
   299
Goalw [wfrec_def]
paulson@5148
   300
    "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a";
clasohm@923
   301
by (etac (wf_trancl RS wftrec RS ssubst) 1);
clasohm@923
   302
by (rtac trans_trancl 1);
clasohm@923
   303
by (rtac (refl RS H_cong) 1);    (*expose the equality of cuts*)
clasohm@1475
   304
by (simp_tac (HOL_ss addsimps [cuts_eq, cut_apply, r_into_trancl]) 1);
clasohm@923
   305
qed "wfrec";
clasohm@1475
   306
---------------------------------------------------------------------------*)
clasohm@923
   307
clasohm@1475
   308
(*---------------------------------------------------------------------------
clasohm@1475
   309
 * This form avoids giant explosions in proofs.  NOTE USE OF == 
clasohm@1475
   310
 *---------------------------------------------------------------------------*)
clasohm@923
   311
val rew::prems = goal WF.thy
clasohm@1475
   312
    "[| f==wfrec r H;  wf(r) |] ==> f(a) = H (cut f r a) a";
clasohm@923
   313
by (rewtac rew);
clasohm@923
   314
by (REPEAT (resolve_tac (prems@[wfrec]) 1));
clasohm@923
   315
qed "def_wfrec";
clasohm@1475
   316
paulson@3198
   317
paulson@3198
   318
(**** TFL variants ****)
paulson@3198
   319
paulson@5278
   320
Goal "!R. wf R --> (!P. (!x. (!y. (y,x):R --> P y) --> P x) --> (!x. P x))";
paulson@3708
   321
by (Clarify_tac 1);
paulson@3198
   322
by (res_inst_tac [("r","R"),("P","P"), ("a","x")] wf_induct 1);
paulson@3198
   323
by (assume_tac 1);
paulson@3198
   324
by (Blast_tac 1);
paulson@3198
   325
qed"tfl_wf_induct";
paulson@3198
   326
wenzelm@5069
   327
Goal "!f R. (x,a):R --> (cut f R a)(x) = f(x)";
paulson@3708
   328
by (Clarify_tac 1);
paulson@3198
   329
by (rtac cut_apply 1);
paulson@3198
   330
by (assume_tac 1);
paulson@3198
   331
qed"tfl_cut_apply";
paulson@3198
   332
wenzelm@5069
   333
Goal "!M R f. (f=wfrec R M) --> wf R --> (!x. f x = M (cut f R x) x)";
paulson@3708
   334
by (Clarify_tac 1);
paulson@4153
   335
by (etac wfrec 1);
paulson@3198
   336
qed "tfl_wfrec";