src/HOL/WF.ML
author clasohm
Fri Mar 03 12:02:25 1995 +0100 (1995-03-03)
changeset 923 ff1574a81019
child 950 323f8ca4587a
permissions -rw-r--r--
new version of HOL with curried function application
clasohm@923
     1
(*  Title: 	HOL/wf.ML
clasohm@923
     2
    ID:         $Id$
clasohm@923
     3
    Author: 	Tobias Nipkow
clasohm@923
     4
    Copyright   1992  University of Cambridge
clasohm@923
     5
clasohm@923
     6
For wf.thy.  Well-founded Recursion
clasohm@923
     7
*)
clasohm@923
     8
clasohm@923
     9
open WF;
clasohm@923
    10
clasohm@923
    11
val H_cong = read_instantiate [("f","H::[?'a, ?'a=>?'b]=>?'b")]
clasohm@923
    12
               (standard(refl RS cong RS cong));
clasohm@923
    13
val H_cong1 = refl RS H_cong;
clasohm@923
    14
clasohm@923
    15
(*Restriction to domain A.  If r is well-founded over A then wf(r)*)
clasohm@923
    16
val [prem1,prem2] = goalw WF.thy [wf_def]
clasohm@923
    17
 "[| r <= Sigma A (%u.A);  \
clasohm@923
    18
\    !!x P. [| ! x. (! y. <y,x> : r --> P(y)) --> P(x);  x:A |] ==> P(x) |]  \
clasohm@923
    19
\ ==>  wf(r)";
clasohm@923
    20
by (strip_tac 1);
clasohm@923
    21
by (rtac allE 1);
clasohm@923
    22
by (assume_tac 1);
clasohm@923
    23
by (best_tac (HOL_cs addSEs [prem1 RS subsetD RS SigmaE2] addIs [prem2]) 1);
clasohm@923
    24
qed "wfI";
clasohm@923
    25
clasohm@923
    26
val major::prems = goalw WF.thy [wf_def]
clasohm@923
    27
    "[| wf(r);          \
clasohm@923
    28
\       !!x.[| ! y. <y,x>: r --> P(y) |] ==> P(x) \
clasohm@923
    29
\    |]  ==>  P(a)";
clasohm@923
    30
by (rtac (major RS spec RS mp RS spec) 1);
clasohm@923
    31
by (fast_tac (HOL_cs addEs prems) 1);
clasohm@923
    32
qed "wf_induct";
clasohm@923
    33
clasohm@923
    34
(*Perform induction on i, then prove the wf(r) subgoal using prems. *)
clasohm@923
    35
fun wf_ind_tac a prems i = 
clasohm@923
    36
    EVERY [res_inst_tac [("a",a)] wf_induct i,
clasohm@923
    37
	   rename_last_tac a ["1"] (i+1),
clasohm@923
    38
	   ares_tac prems i];
clasohm@923
    39
clasohm@923
    40
val prems = goal WF.thy "[| wf(r);  <a,x>:r;  <x,a>:r |] ==> P";
clasohm@923
    41
by (subgoal_tac "! x. <a,x>:r --> <x,a>:r --> P" 1);
clasohm@923
    42
by (fast_tac (HOL_cs addIs prems) 1);
clasohm@923
    43
by (wf_ind_tac "a" prems 1);
clasohm@923
    44
by (fast_tac set_cs 1);
clasohm@923
    45
qed "wf_asym";
clasohm@923
    46
clasohm@923
    47
val prems = goal WF.thy "[| wf(r);  <a,a>: r |] ==> P";
clasohm@923
    48
by (rtac wf_asym 1);
clasohm@923
    49
by (REPEAT (resolve_tac prems 1));
clasohm@923
    50
qed "wf_anti_refl";
clasohm@923
    51
clasohm@923
    52
(*transitive closure of a WF relation is WF!*)
clasohm@923
    53
val [prem] = goal WF.thy "wf(r) ==> wf(r^+)";
clasohm@923
    54
by (rewtac wf_def);
clasohm@923
    55
by (strip_tac 1);
clasohm@923
    56
(*must retain the universal formula for later use!*)
clasohm@923
    57
by (rtac allE 1 THEN assume_tac 1);
clasohm@923
    58
by (etac mp 1);
clasohm@923
    59
by (res_inst_tac [("a","x")] (prem RS wf_induct) 1);
clasohm@923
    60
by (rtac (impI RS allI) 1);
clasohm@923
    61
by (etac tranclE 1);
clasohm@923
    62
by (fast_tac HOL_cs 1);
clasohm@923
    63
by (fast_tac HOL_cs 1);
clasohm@923
    64
qed "wf_trancl";
clasohm@923
    65
clasohm@923
    66
clasohm@923
    67
(** cut **)
clasohm@923
    68
clasohm@923
    69
(*This rewrite rule works upon formulae; thus it requires explicit use of
clasohm@923
    70
  H_cong to expose the equality*)
clasohm@923
    71
goalw WF.thy [cut_def]
clasohm@923
    72
    "(cut f r x = cut g r x) = (!y. <y,x>:r --> f(y)=g(y))";
clasohm@923
    73
by(simp_tac (HOL_ss addsimps [expand_fun_eq]
clasohm@923
    74
                    setloop (split_tac [expand_if])) 1);
clasohm@923
    75
qed "cut_cut_eq";
clasohm@923
    76
clasohm@923
    77
goalw WF.thy [cut_def] "!!x. <x,a>:r ==> (cut f r a)(x) = f(x)";
clasohm@923
    78
by(asm_simp_tac HOL_ss 1);
clasohm@923
    79
qed "cut_apply";
clasohm@923
    80
clasohm@923
    81
clasohm@923
    82
(*** is_recfun ***)
clasohm@923
    83
clasohm@923
    84
goalw WF.thy [is_recfun_def,cut_def]
clasohm@923
    85
    "!!f. [| is_recfun r a H f;  ~<b,a>:r |] ==> f(b) = (@z.True)";
clasohm@923
    86
by (etac ssubst 1);
clasohm@923
    87
by(asm_simp_tac HOL_ss 1);
clasohm@923
    88
qed "is_recfun_undef";
clasohm@923
    89
clasohm@923
    90
(*eresolve_tac transD solves <a,b>:r using transitivity AT MOST ONCE
clasohm@923
    91
  mp amd allE  instantiate induction hypotheses*)
clasohm@923
    92
fun indhyp_tac hyps =
clasohm@923
    93
    ares_tac (TrueI::hyps) ORELSE' 
clasohm@923
    94
    (cut_facts_tac hyps THEN'
clasohm@923
    95
       DEPTH_SOLVE_1 o (ares_tac [TrueI] ORELSE'
clasohm@923
    96
		        eresolve_tac [transD, mp, allE]));
clasohm@923
    97
clasohm@923
    98
(*** NOTE! some simplifications need a different finish_tac!! ***)
clasohm@923
    99
fun indhyp_tac hyps =
clasohm@923
   100
    resolve_tac (TrueI::refl::hyps) ORELSE' 
clasohm@923
   101
    (cut_facts_tac hyps THEN'
clasohm@923
   102
       DEPTH_SOLVE_1 o (ares_tac [TrueI] ORELSE'
clasohm@923
   103
		        eresolve_tac [transD, mp, allE]));
clasohm@923
   104
val wf_super_ss = HOL_ss setsolver indhyp_tac;
clasohm@923
   105
clasohm@923
   106
val prems = goalw WF.thy [is_recfun_def,cut_def]
clasohm@923
   107
    "[| wf(r);  trans(r);  is_recfun r a H f;  is_recfun r b H g |] ==> \
clasohm@923
   108
    \ <x,a>:r --> <x,b>:r --> f(x)=g(x)";
clasohm@923
   109
by (cut_facts_tac prems 1);
clasohm@923
   110
by (etac wf_induct 1);
clasohm@923
   111
by (REPEAT (rtac impI 1 ORELSE etac ssubst 1));
clasohm@923
   112
by (asm_simp_tac (wf_super_ss addcongs [if_cong]) 1);
clasohm@923
   113
qed "is_recfun_equal_lemma";
clasohm@923
   114
bind_thm ("is_recfun_equal", (is_recfun_equal_lemma RS mp RS mp));
clasohm@923
   115
clasohm@923
   116
clasohm@923
   117
val prems as [wfr,transr,recfa,recgb,_] = goalw WF.thy [cut_def]
clasohm@923
   118
    "[| wf(r);  trans(r); \
clasohm@923
   119
\       is_recfun r a H f;  is_recfun r b H g;  <b,a>:r |] ==> \
clasohm@923
   120
\    cut f r b = g";
clasohm@923
   121
val gundef = recgb RS is_recfun_undef
clasohm@923
   122
and fisg   = recgb RS (recfa RS (transr RS (wfr RS is_recfun_equal)));
clasohm@923
   123
by (cut_facts_tac prems 1);
clasohm@923
   124
by (rtac ext 1);
clasohm@923
   125
by (asm_simp_tac (wf_super_ss addsimps [gundef,fisg]
clasohm@923
   126
                              setloop (split_tac [expand_if])) 1);
clasohm@923
   127
qed "is_recfun_cut";
clasohm@923
   128
clasohm@923
   129
(*** Main Existence Lemma -- Basic Properties of the_recfun ***)
clasohm@923
   130
clasohm@923
   131
val prems = goalw WF.thy [the_recfun_def]
clasohm@923
   132
    "is_recfun r a H f ==> is_recfun r a H (the_recfun r a H)";
clasohm@923
   133
by (res_inst_tac [("P", "is_recfun r a H")] selectI 1);
clasohm@923
   134
by (resolve_tac prems 1);
clasohm@923
   135
qed "is_the_recfun";
clasohm@923
   136
clasohm@923
   137
val prems = goal WF.thy
clasohm@923
   138
    "[| wf(r);  trans(r) |] ==> is_recfun r a H (the_recfun r a H)";
clasohm@923
   139
by (cut_facts_tac prems 1);
clasohm@923
   140
by (wf_ind_tac "a" prems 1);
clasohm@923
   141
by (res_inst_tac [("f", "cut (%y. wftrec r y H) r a1")] is_the_recfun 1);
clasohm@923
   142
by (rewrite_goals_tac [is_recfun_def, wftrec_def]);
clasohm@923
   143
by (rtac (cut_cut_eq RS ssubst) 1);
clasohm@923
   144
(*Applying the substitution: must keep the quantified assumption!!*)
clasohm@923
   145
by (EVERY1 [strip_tac, rtac H_cong1, rtac allE, atac,
clasohm@923
   146
            etac (mp RS ssubst), atac]);
clasohm@923
   147
by (fold_tac [is_recfun_def]);
clasohm@923
   148
by (asm_simp_tac (wf_super_ss addsimps[cut_apply,is_recfun_cut,cut_cut_eq]) 1);
clasohm@923
   149
qed "unfold_the_recfun";
clasohm@923
   150
clasohm@923
   151
clasohm@923
   152
(*Beware incompleteness of unification!*)
clasohm@923
   153
val prems = goal WF.thy
clasohm@923
   154
    "[| wf(r);  trans(r);  <c,a>:r;  <c,b>:r |] \
clasohm@923
   155
\    ==> the_recfun r a H c = the_recfun r b H c";
clasohm@923
   156
by (DEPTH_SOLVE (ares_tac (prems@[is_recfun_equal,unfold_the_recfun]) 1));
clasohm@923
   157
qed "the_recfun_equal";
clasohm@923
   158
clasohm@923
   159
val prems = goal WF.thy
clasohm@923
   160
    "[| wf(r); trans(r); <b,a>:r |] \
clasohm@923
   161
\    ==> cut (the_recfun r a H) r b = the_recfun r b H";
clasohm@923
   162
by (REPEAT (ares_tac (prems@[is_recfun_cut,unfold_the_recfun]) 1));
clasohm@923
   163
qed "the_recfun_cut";
clasohm@923
   164
clasohm@923
   165
(*** Unfolding wftrec ***)
clasohm@923
   166
clasohm@923
   167
goalw WF.thy [wftrec_def]
clasohm@923
   168
    "!!r. [| wf(r);  trans(r) |] ==> \
clasohm@923
   169
\    wftrec r a H = H a (cut (%x.wftrec r x H) r a)";
clasohm@923
   170
by (EVERY1 [stac (rewrite_rule [is_recfun_def] unfold_the_recfun),
clasohm@923
   171
	    REPEAT o atac, rtac H_cong1]);
clasohm@923
   172
by (asm_simp_tac (HOL_ss addsimps [cut_cut_eq,the_recfun_cut]) 1);
clasohm@923
   173
qed "wftrec";
clasohm@923
   174
clasohm@923
   175
(*Unused but perhaps interesting*)
clasohm@923
   176
val prems = goal WF.thy
clasohm@923
   177
    "[| wf(r);  trans(r);  !!f x. H x (cut f r x) = H x f |] ==> \
clasohm@923
   178
\		wftrec r a H = H a (%x.wftrec r x H)";
clasohm@923
   179
by (rtac (wftrec RS trans) 1);
clasohm@923
   180
by (REPEAT (resolve_tac prems 1));
clasohm@923
   181
qed "wftrec2";
clasohm@923
   182
clasohm@923
   183
(** Removal of the premise trans(r) **)
clasohm@923
   184
clasohm@923
   185
goalw WF.thy [wfrec_def]
clasohm@923
   186
    "!!r. wf(r) ==> wfrec r a H = H a (cut (%x.wfrec r x H) r a)";
clasohm@923
   187
by (etac (wf_trancl RS wftrec RS ssubst) 1);
clasohm@923
   188
by (rtac trans_trancl 1);
clasohm@923
   189
by (rtac (refl RS H_cong) 1);    (*expose the equality of cuts*)
clasohm@923
   190
by (simp_tac (HOL_ss addsimps [cut_cut_eq, cut_apply, r_into_trancl]) 1);
clasohm@923
   191
qed "wfrec";
clasohm@923
   192
clasohm@923
   193
(*This form avoids giant explosions in proofs.  NOTE USE OF == *)
clasohm@923
   194
val rew::prems = goal WF.thy
clasohm@923
   195
    "[| !!x. f(x)==wfrec r x H;  wf(r) |] ==> f(a) = H a (cut (%x.f(x)) r a)";
clasohm@923
   196
by (rewtac rew);
clasohm@923
   197
by (REPEAT (resolve_tac (prems@[wfrec]) 1));
clasohm@923
   198
qed "def_wfrec";