src/HOL/WF.ML
author paulson
Thu May 15 14:28:32 1997 +0200 (1997-05-15)
changeset 3198 295287618e30
parent 2935 998cb95fdd43
child 3320 3a5e4930fb77
permissions -rw-r--r--
New proofs for TFL
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)";
clasohm@923
    19
by (strip_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);
clasohm@923
    54
by (strip_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@3198
    78
by (strip_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
paulson@3198
    87
clasohm@923
    88
(** cut **)
clasohm@923
    89
clasohm@923
    90
(*This rewrite rule works upon formulae; thus it requires explicit use of
clasohm@923
    91
  H_cong to expose the equality*)
clasohm@923
    92
goalw WF.thy [cut_def]
clasohm@972
    93
    "(cut f r x = cut g r x) = (!y. (y,x):r --> f(y)=g(y))";
paulson@1552
    94
by (simp_tac (HOL_ss addsimps [expand_fun_eq]
clasohm@1475
    95
                    setloop (split_tac [expand_if])) 1);
clasohm@1475
    96
qed "cuts_eq";
clasohm@923
    97
clasohm@972
    98
goalw WF.thy [cut_def] "!!x. (x,a):r ==> (cut f r a)(x) = f(x)";
paulson@1552
    99
by (asm_simp_tac HOL_ss 1);
clasohm@923
   100
qed "cut_apply";
clasohm@923
   101
clasohm@923
   102
(*** is_recfun ***)
clasohm@923
   103
clasohm@923
   104
goalw WF.thy [is_recfun_def,cut_def]
clasohm@1475
   105
    "!!f. [| is_recfun r H a f;  ~(b,a):r |] ==> f(b) = (@z.True)";
clasohm@923
   106
by (etac ssubst 1);
paulson@1552
   107
by (asm_simp_tac HOL_ss 1);
clasohm@923
   108
qed "is_recfun_undef";
clasohm@923
   109
clasohm@923
   110
(*** NOTE! some simplifications need a different finish_tac!! ***)
clasohm@923
   111
fun indhyp_tac hyps =
clasohm@923
   112
    (cut_facts_tac hyps THEN'
clasohm@923
   113
       DEPTH_SOLVE_1 o (ares_tac [TrueI] ORELSE'
clasohm@1465
   114
                        eresolve_tac [transD, mp, allE]));
oheimb@2637
   115
val wf_super_ss = HOL_ss addSolver indhyp_tac;
clasohm@923
   116
clasohm@923
   117
val prems = goalw WF.thy [is_recfun_def,cut_def]
clasohm@1475
   118
    "[| wf(r);  trans(r);  is_recfun r H a f;  is_recfun r H b g |] ==> \
clasohm@972
   119
    \ (x,a):r --> (x,b):r --> f(x)=g(x)";
clasohm@923
   120
by (cut_facts_tac prems 1);
clasohm@923
   121
by (etac wf_induct 1);
clasohm@923
   122
by (REPEAT (rtac impI 1 ORELSE etac ssubst 1));
clasohm@923
   123
by (asm_simp_tac (wf_super_ss addcongs [if_cong]) 1);
nipkow@1485
   124
qed_spec_mp "is_recfun_equal";
clasohm@923
   125
clasohm@923
   126
clasohm@923
   127
val prems as [wfr,transr,recfa,recgb,_] = goalw WF.thy [cut_def]
clasohm@923
   128
    "[| wf(r);  trans(r); \
clasohm@1475
   129
\       is_recfun r H a f;  is_recfun r H b g;  (b,a):r |] ==> \
clasohm@923
   130
\    cut f r b = g";
clasohm@923
   131
val gundef = recgb RS is_recfun_undef
clasohm@923
   132
and fisg   = recgb RS (recfa RS (transr RS (wfr RS is_recfun_equal)));
clasohm@923
   133
by (cut_facts_tac prems 1);
clasohm@923
   134
by (rtac ext 1);
clasohm@923
   135
by (asm_simp_tac (wf_super_ss addsimps [gundef,fisg]
clasohm@923
   136
                              setloop (split_tac [expand_if])) 1);
clasohm@923
   137
qed "is_recfun_cut";
clasohm@923
   138
clasohm@923
   139
(*** Main Existence Lemma -- Basic Properties of the_recfun ***)
clasohm@923
   140
clasohm@923
   141
val prems = goalw WF.thy [the_recfun_def]
clasohm@1475
   142
    "is_recfun r H a f ==> is_recfun r H a (the_recfun r H a)";
clasohm@1475
   143
by (res_inst_tac [("P", "is_recfun r H a")] selectI 1);
clasohm@923
   144
by (resolve_tac prems 1);
clasohm@923
   145
qed "is_the_recfun";
clasohm@923
   146
clasohm@923
   147
val prems = goal WF.thy
clasohm@1475
   148
 "[| wf(r);  trans(r) |] ==> is_recfun r H a (the_recfun r H a)";
clasohm@1475
   149
  by (cut_facts_tac prems 1);
clasohm@1475
   150
  by (wf_ind_tac "a" prems 1);
clasohm@1475
   151
  by (res_inst_tac [("f","cut (%y. H (the_recfun r H y) y) r a1")]
clasohm@1475
   152
                   is_the_recfun 1);
paulson@1552
   153
  by (rewtac is_recfun_def);
paulson@2031
   154
  by (stac cuts_eq 1);
clasohm@1475
   155
  by (rtac allI 1);
clasohm@1475
   156
  by (rtac impI 1);
clasohm@1475
   157
  by (res_inst_tac [("f1","H"),("x","y")](arg_cong RS fun_cong) 1);
clasohm@1475
   158
  by (subgoal_tac
clasohm@1475
   159
         "the_recfun r H y = cut(%x. H(cut(the_recfun r H y) r x) x) r y" 1);
clasohm@1475
   160
  by (etac allE 2);
clasohm@1475
   161
  by (dtac impE 2);
clasohm@1475
   162
  by (atac 2);
clasohm@1475
   163
  by (atac 3);
clasohm@1475
   164
  by (atac 2);
clasohm@1475
   165
  by (etac ssubst 1);
clasohm@1475
   166
  by (simp_tac (HOL_ss addsimps [cuts_eq]) 1);
clasohm@1475
   167
  by (rtac allI 1);
clasohm@1475
   168
  by (rtac impI 1);
clasohm@1475
   169
  by (asm_simp_tac (wf_super_ss addsimps[cut_apply,is_recfun_cut,cuts_eq]) 1);
clasohm@1475
   170
  by (res_inst_tac [("f1","H"),("x","ya")](arg_cong RS fun_cong) 1);
clasohm@1475
   171
  by (fold_tac [is_recfun_def]);
clasohm@1475
   172
  by (asm_simp_tac (wf_super_ss addsimps[cut_apply,is_recfun_cut,cuts_eq]) 1);
clasohm@923
   173
qed "unfold_the_recfun";
clasohm@923
   174
clasohm@1475
   175
val unwind1_the_recfun = rewrite_rule[is_recfun_def] unfold_the_recfun;
clasohm@923
   176
clasohm@1475
   177
(*--------------Old proof-----------------------------------------------------
clasohm@923
   178
val prems = goal WF.thy
clasohm@1475
   179
    "[| wf(r);  trans(r) |] ==> is_recfun r H a (the_recfun r H a)";
clasohm@1475
   180
by (cut_facts_tac prems 1);
clasohm@1475
   181
by (wf_ind_tac "a" prems 1);
clasohm@1475
   182
by (res_inst_tac [("f", "cut (%y. wftrec r H y) r a1")] is_the_recfun 1); 
clasohm@1475
   183
by (rewrite_goals_tac [is_recfun_def, wftrec_def]);
paulson@2031
   184
by (stac cuts_eq 1);
clasohm@1475
   185
(*Applying the substitution: must keep the quantified assumption!!*)
clasohm@1475
   186
by (EVERY1 [strip_tac, rtac H_cong1, rtac allE, atac,
clasohm@1475
   187
            etac (mp RS ssubst), atac]); 
clasohm@1475
   188
by (fold_tac [is_recfun_def]);
clasohm@1475
   189
by (asm_simp_tac (wf_super_ss addsimps[cut_apply,is_recfun_cut,cuts_eq]) 1);
clasohm@1475
   190
qed "unfold_the_recfun";
clasohm@1475
   191
---------------------------------------------------------------------------*)
clasohm@923
   192
clasohm@923
   193
(** Removal of the premise trans(r) **)
clasohm@1475
   194
val th = rewrite_rule[is_recfun_def]
clasohm@1475
   195
                     (trans_trancl RSN (2,(wf_trancl RS unfold_the_recfun)));
clasohm@923
   196
clasohm@923
   197
goalw WF.thy [wfrec_def]
clasohm@1475
   198
    "!!r. wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a";
clasohm@1475
   199
by (rtac H_cong 1);
clasohm@1475
   200
by (rtac refl 2);
clasohm@1475
   201
by (simp_tac (HOL_ss addsimps [cuts_eq]) 1);
clasohm@1475
   202
by (rtac allI 1);
clasohm@1475
   203
by (rtac impI 1);
clasohm@1475
   204
by (simp_tac(HOL_ss addsimps [wfrec_def]) 1);
clasohm@1475
   205
by (res_inst_tac [("a1","a")] (th RS ssubst) 1);
clasohm@1475
   206
by (atac 1);
clasohm@1475
   207
by (forward_tac[wf_trancl] 1);
clasohm@1475
   208
by (forward_tac[r_into_trancl] 1);
clasohm@1475
   209
by (asm_simp_tac (HOL_ss addsimps [cut_apply]) 1);
clasohm@1475
   210
by (rtac H_cong 1);    (*expose the equality of cuts*)
clasohm@1475
   211
by (rtac refl 2);
clasohm@1475
   212
by (simp_tac (HOL_ss addsimps [cuts_eq, cut_apply, r_into_trancl]) 1);
clasohm@1475
   213
by (strip_tac 1);
nipkow@1485
   214
by (res_inst_tac [("r","r^+")] is_recfun_equal 1);
clasohm@1475
   215
by (atac 1);
clasohm@1475
   216
by (rtac trans_trancl 1);
clasohm@1475
   217
by (rtac unfold_the_recfun 1);
clasohm@1475
   218
by (atac 1);
clasohm@1475
   219
by (rtac trans_trancl 1);
clasohm@1475
   220
by (rtac unfold_the_recfun 1);
clasohm@1475
   221
by (atac 1);
clasohm@1475
   222
by (rtac trans_trancl 1);
clasohm@1475
   223
by (rtac transD 1);
clasohm@1475
   224
by (rtac trans_trancl 1);
clasohm@1475
   225
by (forw_inst_tac [("a","ya")] r_into_trancl 1);
clasohm@1475
   226
by (atac 1);
clasohm@1475
   227
by (atac 1);
clasohm@1475
   228
by (forw_inst_tac [("a","ya")] r_into_trancl 1);
clasohm@1475
   229
by (atac 1);
clasohm@1475
   230
qed "wfrec";
clasohm@1475
   231
clasohm@1475
   232
(*--------------Old proof-----------------------------------------------------
clasohm@1475
   233
goalw WF.thy [wfrec_def]
clasohm@1475
   234
    "!!r. wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a";
clasohm@923
   235
by (etac (wf_trancl RS wftrec RS ssubst) 1);
clasohm@923
   236
by (rtac trans_trancl 1);
clasohm@923
   237
by (rtac (refl RS H_cong) 1);    (*expose the equality of cuts*)
clasohm@1475
   238
by (simp_tac (HOL_ss addsimps [cuts_eq, cut_apply, r_into_trancl]) 1);
clasohm@923
   239
qed "wfrec";
clasohm@1475
   240
---------------------------------------------------------------------------*)
clasohm@923
   241
clasohm@1475
   242
(*---------------------------------------------------------------------------
clasohm@1475
   243
 * This form avoids giant explosions in proofs.  NOTE USE OF == 
clasohm@1475
   244
 *---------------------------------------------------------------------------*)
clasohm@923
   245
val rew::prems = goal WF.thy
clasohm@1475
   246
    "[| f==wfrec r H;  wf(r) |] ==> f(a) = H (cut f r a) a";
clasohm@923
   247
by (rewtac rew);
clasohm@923
   248
by (REPEAT (resolve_tac (prems@[wfrec]) 1));
clasohm@923
   249
qed "def_wfrec";
clasohm@1475
   250
paulson@3198
   251
paulson@3198
   252
(**** TFL variants ****)
paulson@3198
   253
paulson@3198
   254
goal WF.thy
paulson@3198
   255
    "!R. wf R --> (!P. (!x. (!y. (y,x):R --> P y) --> P x) --> (!x. P x))";
paulson@3198
   256
by (strip_tac 1);
paulson@3198
   257
by (res_inst_tac [("r","R"),("P","P"), ("a","x")] wf_induct 1);
paulson@3198
   258
by (assume_tac 1);
paulson@3198
   259
by (Blast_tac 1);
paulson@3198
   260
qed"tfl_wf_induct";
paulson@3198
   261
paulson@3198
   262
goal WF.thy "!f R. (x,a):R --> (cut f R a)(x) = f(x)";
paulson@3198
   263
by (strip_tac 1);
paulson@3198
   264
by (rtac cut_apply 1);
paulson@3198
   265
by (assume_tac 1);
paulson@3198
   266
qed"tfl_cut_apply";
paulson@3198
   267
paulson@3198
   268
goal WF.thy "!M R f. (f=wfrec R M) --> wf R --> (!x. f x = M (cut f R x) x)";
paulson@3198
   269
by (strip_tac 1);
paulson@3198
   270
by (res_inst_tac [("r","R"), ("H","M"),
paulson@3198
   271
                  ("a","x"), ("f","f")] (eq_reflection RS def_wfrec) 1);
paulson@3198
   272
by (assume_tac 1);
paulson@3198
   273
by (assume_tac 1);
paulson@3198
   274
qed "tfl_wfrec";
paulson@3198
   275