src/HOL/WF.ML
author paulson
Thu Aug 13 18:14:26 1998 +0200 (1998-08-13)
changeset 5316 7a8975451a89
parent 5281 f4d16517b360
child 5318 72bf8039b53f
permissions -rw-r--r--
even more tidying of Goal commands
     1 (*  Title:      HOL/wf.ML
     2     ID:         $Id$
     3     Author:     Tobias Nipkow, with minor changes by Konrad Slind
     4     Copyright   1992  University of Cambridge/1995 TU Munich
     5 
     6 Wellfoundedness, induction, and  recursion
     7 *)
     8 
     9 open WF;
    10 
    11 val H_cong = read_instantiate [("f","H")] (standard(refl RS cong RS cong));
    12 val H_cong1 = refl RS H_cong;
    13 
    14 (*Restriction to domain A.  If r is well-founded over A then wf(r)*)
    15 val [prem1,prem2] = Goalw [wf_def]
    16  "[| r <= A Times A;  \
    17 \    !!x P. [| ! x. (! y. (y,x) : r --> P(y)) --> P(x);  x:A |] ==> P(x) |]  \
    18 \ ==>  wf(r)";
    19 by (Clarify_tac 1);
    20 by (rtac allE 1);
    21 by (assume_tac 1);
    22 by (best_tac (claset() addSEs [prem1 RS subsetD RS SigmaE2] addIs [prem2]) 1);
    23 qed "wfI";
    24 
    25 val major::prems = Goalw [wf_def]
    26     "[| wf(r);          \
    27 \       !!x.[| ! y. (y,x): r --> P(y) |] ==> P(x) \
    28 \    |]  ==>  P(a)";
    29 by (rtac (major RS spec RS mp RS spec) 1);
    30 by (blast_tac (claset() addIs prems) 1);
    31 qed "wf_induct";
    32 
    33 (*Perform induction on i, then prove the wf(r) subgoal using prems. *)
    34 fun wf_ind_tac a prems i = 
    35     EVERY [res_inst_tac [("a",a)] wf_induct i,
    36            rename_last_tac a ["1"] (i+1),
    37            ares_tac prems i];
    38 
    39 Goal "[| wf(r);  (a,x):r;  (x,a):r |] ==> P";
    40 by (subgoal_tac "! x. (a,x):r --> (x,a):r --> P" 1);
    41 by (Blast_tac 1);
    42 by (wf_ind_tac "a" [] 1);
    43 by (Blast_tac 1);
    44 qed "wf_asym";
    45 
    46 Goal "[| wf(r);  (a,a): r |] ==> P";
    47 by (blast_tac (claset() addEs [wf_asym]) 1);
    48 qed "wf_irrefl";
    49 
    50 (*transitive closure of a wf relation is wf! *)
    51 Goal "wf(r) ==> wf(r^+)";
    52 by (stac wf_def 1);
    53 by (Clarify_tac 1);
    54 (*must retain the universal formula for later use!*)
    55 by (rtac allE 1 THEN assume_tac 1);
    56 by (etac mp 1);
    57 by (eres_inst_tac [("a","x")] wf_induct 1);
    58 by (rtac (impI RS allI) 1);
    59 by (etac tranclE 1);
    60 by (Blast_tac 1);
    61 by (Blast_tac 1);
    62 qed "wf_trancl";
    63 
    64 
    65 val wf_converse_trancl = prove_goal thy 
    66 "!!X. wf (r^-1) ==> wf ((r^+)^-1)" (K [
    67 	stac (trancl_converse RS sym) 1,
    68 	etac wf_trancl 1]);
    69 
    70 (*----------------------------------------------------------------------------
    71  * Minimal-element characterization of well-foundedness
    72  *---------------------------------------------------------------------------*)
    73 
    74 Goalw [wf_def] "wf r ==> x:Q --> (? z:Q. ! y. (y,z):r --> y~:Q)";
    75 bd spec 1;
    76 by (etac (mp RS spec) 1);
    77 by (Blast_tac 1);
    78 val lemma1 = result();
    79 
    80 Goalw [wf_def] "(! Q x. x:Q --> (? z:Q. ! y. (y,z):r --> y~:Q)) ==> wf r";
    81 by (Clarify_tac 1);
    82 by (dres_inst_tac [("x", "{x. ~ P x}")] spec 1);
    83 by (Blast_tac 1);
    84 val lemma2 = result();
    85 
    86 Goal "wf r = (! Q x. x:Q --> (? z:Q. ! y. (y,z):r --> y~:Q))";
    87 by (blast_tac (claset() addSIs [lemma1, lemma2]) 1);
    88 qed "wf_eq_minimal";
    89 
    90 (*---------------------------------------------------------------------------
    91  * Wellfoundedness of subsets
    92  *---------------------------------------------------------------------------*)
    93 
    94 Goal "[| wf(r);  p<=r |] ==> wf(p)";
    95 by (full_simp_tac (simpset() addsimps [wf_eq_minimal]) 1);
    96 by (Fast_tac 1);
    97 qed "wf_subset";
    98 
    99 (*---------------------------------------------------------------------------
   100  * Wellfoundedness of the empty relation.
   101  *---------------------------------------------------------------------------*)
   102 
   103 Goal "wf({})";
   104 by (simp_tac (simpset() addsimps [wf_def]) 1);
   105 qed "wf_empty";
   106 AddIffs [wf_empty];
   107 
   108 (*---------------------------------------------------------------------------
   109  * Wellfoundedness of `insert'
   110  *---------------------------------------------------------------------------*)
   111 
   112 Goal "wf(insert (y,x) r) = (wf(r) & (x,y) ~: r^*)";
   113 by (rtac iffI 1);
   114  by (blast_tac (claset() addEs [wf_trancl RS wf_irrefl] 
   115 	addIs [rtrancl_into_trancl1,wf_subset,impOfSubs rtrancl_mono]) 1);
   116 by (asm_full_simp_tac (simpset() addsimps [wf_eq_minimal]) 1);
   117 by Safe_tac;
   118 by (EVERY1[rtac allE, atac, etac impE, Blast_tac]);
   119 by (etac bexE 1);
   120 by (rename_tac "a" 1);
   121 by (case_tac "a = x" 1);
   122  by (res_inst_tac [("x","a")]bexI 2);
   123   by (assume_tac 3);
   124  by (Blast_tac 2);
   125 by (case_tac "y:Q" 1);
   126  by (Blast_tac 2);
   127 by (res_inst_tac [("x","{z. z:Q & (z,y) : r^*}")] allE 1);
   128  by (assume_tac 1);
   129 by (thin_tac "! Q. (? x. x : Q) --> ?P Q" 1);	(*essential for speed*)
   130 (*Blast_tac with new substOccur fails*)
   131 by (best_tac (claset() addIs [rtrancl_into_rtrancl2]) 1);
   132 qed "wf_insert";
   133 AddIffs [wf_insert];
   134 
   135 (*---------------------------------------------------------------------------
   136  * Wellfoundedness of `disjoint union'
   137  *---------------------------------------------------------------------------*)
   138 
   139 Goal "[| !i:I. wf(r i); \
   140 \        !i:I.!j:I. r i ~= r j --> Domain(r i) Int Range(r j) = {} & \
   141 \                                  Domain(r j) Int Range(r i) = {} \
   142 \     |] ==> wf(UN i:I. r i)";
   143 by(asm_full_simp_tac (HOL_basic_ss addsimps [wf_eq_minimal]) 1);
   144 by(Clarify_tac 1);
   145 by(rename_tac "A a" 1);
   146 by(case_tac "? i:I. ? a:A. ? b:A. (b,a) : r i" 1);
   147  by(Clarify_tac 1);
   148  by(EVERY1[dtac bspec, atac,
   149            eres_inst_tac[("x","{a. a:A & (? b:A. (b,a) : r i)}")]allE]);
   150  by(EVERY1[etac allE,etac impE]);
   151   by(Blast_tac 1);
   152  by(Clarify_tac 1);
   153  by(rename_tac "z'" 1);
   154  by(res_inst_tac [("x","z'")] bexI 1);
   155   ba 2;
   156  by(Clarify_tac 1);
   157  by(rename_tac "j" 1);
   158  by(case_tac "r j = r i" 1);
   159   by(EVERY1[etac allE,etac impE,atac]);
   160   by(Asm_full_simp_tac 1);
   161   by(Blast_tac 1);
   162  by(blast_tac (claset() addEs [equalityE]) 1);
   163 by(Asm_full_simp_tac 1);
   164 by(case_tac "? i. i:I" 1);
   165  by(Clarify_tac 1);
   166  by(EVERY1[dtac bspec, atac, eres_inst_tac[("x","A")]allE]);
   167  by(Blast_tac 1);
   168 by(Blast_tac 1);
   169 qed "wf_UN";
   170 
   171 Goalw [Union_def]
   172  "[| !r:R. wf r; \
   173 \    !r:R.!s:R. r ~= s --> Domain r Int Range s = {} & \
   174 \                          Domain s Int Range r = {} \
   175 \ |] ==> wf(Union R)";
   176 br wf_UN 1;
   177 by(Blast_tac 1);
   178 by(Blast_tac 1);
   179 qed "wf_Union";
   180 
   181 Goal "[| wf r; wf s; Domain r Int Range s = {}; Domain s Int Range r = {} \
   182 \     |] ==> wf(r Un s)";
   183 br(simplify (simpset()) (read_instantiate[("R","{r,s}")]wf_Union)) 1;
   184 by(Blast_tac 1);
   185 by(Blast_tac 1);
   186 qed "wf_Un";
   187 
   188 (*---------------------------------------------------------------------------
   189  * Wellfoundedness of `image'
   190  *---------------------------------------------------------------------------*)
   191 
   192 Goal "[| wf r; inj f |] ==> wf(prod_fun f f `` r)";
   193 by(asm_full_simp_tac (HOL_basic_ss addsimps [wf_eq_minimal]) 1);
   194 by(Clarify_tac 1);
   195 by(case_tac "? p. f p : Q" 1);
   196 by(eres_inst_tac [("x","{p. f p : Q}")]allE 1);
   197 by(fast_tac (claset() addDs [injD]) 1);
   198 by(Blast_tac 1);
   199 qed "wf_prod_fun_image";
   200 
   201 (*** acyclic ***)
   202 
   203 val acyclicI = prove_goalw WF.thy [acyclic_def] 
   204 "!!r. !x. (x, x) ~: r^+ ==> acyclic r" (K [atac 1]);
   205 
   206 Goalw [acyclic_def]
   207  "wf r ==> acyclic r";
   208 by (blast_tac (claset() addEs [wf_trancl RS wf_irrefl]) 1);
   209 qed "wf_acyclic";
   210 
   211 Goalw [acyclic_def]
   212   "acyclic(insert (y,x) r) = (acyclic r & (x,y) ~: r^*)";
   213 by (simp_tac (simpset() addsimps [trancl_insert]) 1);
   214 by (blast_tac (claset() addEs [make_elim rtrancl_trans]) 1);
   215 qed "acyclic_insert";
   216 AddIffs [acyclic_insert];
   217 
   218 Goalw [acyclic_def] "acyclic(r^-1) = acyclic r";
   219 by (simp_tac (simpset() addsimps [trancl_converse]) 1);
   220 qed "acyclic_converse";
   221 
   222 (** cut **)
   223 
   224 (*This rewrite rule works upon formulae; thus it requires explicit use of
   225   H_cong to expose the equality*)
   226 Goalw [cut_def]
   227     "(cut f r x = cut g r x) = (!y. (y,x):r --> f(y)=g(y))";
   228 by (simp_tac (HOL_ss addsimps [expand_fun_eq]) 1);
   229 qed "cuts_eq";
   230 
   231 Goalw [cut_def] "(x,a):r ==> (cut f r a)(x) = f(x)";
   232 by (asm_simp_tac HOL_ss 1);
   233 qed "cut_apply";
   234 
   235 (*** is_recfun ***)
   236 
   237 Goalw [is_recfun_def,cut_def]
   238     "[| is_recfun r H a f;  ~(b,a):r |] ==> f(b) = arbitrary";
   239 by (etac ssubst 1);
   240 by (asm_simp_tac HOL_ss 1);
   241 qed "is_recfun_undef";
   242 
   243 (*** NOTE! some simplifications need a different finish_tac!! ***)
   244 fun indhyp_tac hyps =
   245     (cut_facts_tac hyps THEN'
   246        DEPTH_SOLVE_1 o (ares_tac [TrueI] ORELSE'
   247                         eresolve_tac [transD, mp, allE]));
   248 val wf_super_ss = HOL_ss addSolver indhyp_tac;
   249 
   250 Goalw [is_recfun_def,cut_def]
   251     "[| wf(r);  trans(r);  is_recfun r H a f;  is_recfun r H b g |] ==> \
   252     \ (x,a):r --> (x,b):r --> f(x)=g(x)";
   253 by (etac wf_induct 1);
   254 by (REPEAT (rtac impI 1 ORELSE etac ssubst 1));
   255 by (asm_simp_tac (wf_super_ss addcongs [if_cong]) 1);
   256 qed_spec_mp "is_recfun_equal";
   257 
   258 
   259 val prems as [wfr,transr,recfa,recgb,_] = goalw WF.thy [cut_def]
   260     "[| wf(r);  trans(r); \
   261 \       is_recfun r H a f;  is_recfun r H b g;  (b,a):r |] ==> \
   262 \    cut f r b = g";
   263 val gundef = recgb RS is_recfun_undef
   264 and fisg   = recgb RS (recfa RS (transr RS (wfr RS is_recfun_equal)));
   265 by (cut_facts_tac prems 1);
   266 by (rtac ext 1);
   267 by (asm_simp_tac (wf_super_ss addsimps [gundef,fisg]) 1);
   268 qed "is_recfun_cut";
   269 
   270 (*** Main Existence Lemma -- Basic Properties of the_recfun ***)
   271 
   272 Goalw [the_recfun_def]
   273     "is_recfun r H a f ==> is_recfun r H a (the_recfun r H a)";
   274 by (eres_inst_tac [("P", "is_recfun r H a")] selectI 1);
   275 qed "is_the_recfun";
   276 
   277 Goal "[| wf(r);  trans(r) |] ==> is_recfun r H a (the_recfun r H a)";
   278 by (wf_ind_tac "a" [] 1);
   279 by (res_inst_tac [("f","cut (%y. H (the_recfun r H y) y) r a1")]
   280                  is_the_recfun 1);
   281 by (rewtac is_recfun_def);
   282 by (stac cuts_eq 1);
   283 by (Clarify_tac 1);
   284 by (rtac (refl RSN (2,H_cong)) 1);
   285 by (subgoal_tac
   286          "the_recfun r H y = cut(%x. H(cut(the_recfun r H y) r x) x) r y" 1);
   287  by (etac allE 2);
   288  by (dtac impE 2);
   289    by (atac 2);
   290   by (atac 3);
   291  by (atac 2);
   292 by (etac ssubst 1);
   293 by (simp_tac (HOL_ss addsimps [cuts_eq]) 1);
   294 by (Clarify_tac 1);
   295 by (stac cut_apply 1);
   296  by (fast_tac (claset() addDs [transD]) 1);
   297 by (rtac (refl RSN (2,H_cong)) 1);
   298 by (fold_tac [is_recfun_def]);
   299 by (asm_simp_tac (wf_super_ss addsimps[is_recfun_cut]) 1);
   300 qed "unfold_the_recfun";
   301 
   302 val unwind1_the_recfun = rewrite_rule[is_recfun_def] unfold_the_recfun;
   303 
   304 (*--------------Old proof-----------------------------------------------------
   305 val prems = Goal
   306     "[| wf(r);  trans(r) |] ==> is_recfun r H a (the_recfun r H a)";
   307 by (cut_facts_tac prems 1);
   308 by (wf_ind_tac "a" prems 1);
   309 by (res_inst_tac [("f", "cut (%y. wftrec r H y) r a1")] is_the_recfun 1); 
   310 by (rewrite_goals_tac [is_recfun_def, wftrec_def]);
   311 by (stac cuts_eq 1);
   312 (*Applying the substitution: must keep the quantified assumption!!*)
   313 by (EVERY1 [Clarify_tac, rtac H_cong1, rtac allE, atac,
   314             etac (mp RS ssubst), atac]); 
   315 by (fold_tac [is_recfun_def]);
   316 by (asm_simp_tac (wf_super_ss addsimps[cut_apply,is_recfun_cut,cuts_eq]) 1);
   317 qed "unfold_the_recfun";
   318 ---------------------------------------------------------------------------*)
   319 
   320 (** Removal of the premise trans(r) **)
   321 val th = rewrite_rule[is_recfun_def]
   322                      (trans_trancl RSN (2,(wf_trancl RS unfold_the_recfun)));
   323 
   324 Goalw [wfrec_def]
   325     "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a";
   326 by (rtac H_cong 1);
   327 by (rtac refl 2);
   328 by (simp_tac (HOL_ss addsimps [cuts_eq]) 1);
   329 by (rtac allI 1);
   330 by (rtac impI 1);
   331 by (simp_tac(HOL_ss addsimps [wfrec_def]) 1);
   332 by (res_inst_tac [("a1","a")] (th RS ssubst) 1);
   333 by (atac 1);
   334 by (forward_tac[wf_trancl] 1);
   335 by (forward_tac[r_into_trancl] 1);
   336 by (asm_simp_tac (HOL_ss addsimps [cut_apply]) 1);
   337 by (rtac H_cong 1);    (*expose the equality of cuts*)
   338 by (rtac refl 2);
   339 by (simp_tac (HOL_ss addsimps [cuts_eq, cut_apply, r_into_trancl]) 1);
   340 by (Clarify_tac 1);
   341 by (res_inst_tac [("r","r^+")] is_recfun_equal 1);
   342 by (atac 1);
   343 by (rtac trans_trancl 1);
   344 by (rtac unfold_the_recfun 1);
   345 by (atac 1);
   346 by (rtac trans_trancl 1);
   347 by (rtac unfold_the_recfun 1);
   348 by (atac 1);
   349 by (rtac trans_trancl 1);
   350 by (rtac transD 1);
   351 by (rtac trans_trancl 1);
   352 by (forw_inst_tac [("p","(ya,y)")] r_into_trancl 1);
   353 by (atac 1);
   354 by (atac 1);
   355 by (forw_inst_tac [("p","(ya,y)")] r_into_trancl 1);
   356 by (atac 1);
   357 qed "wfrec";
   358 
   359 (*--------------Old proof-----------------------------------------------------
   360 Goalw [wfrec_def]
   361     "wf(r) ==> wfrec r H a = H (cut (wfrec r H) r a) a";
   362 by (etac (wf_trancl RS wftrec RS ssubst) 1);
   363 by (rtac trans_trancl 1);
   364 by (rtac (refl RS H_cong) 1);    (*expose the equality of cuts*)
   365 by (simp_tac (HOL_ss addsimps [cuts_eq, cut_apply, r_into_trancl]) 1);
   366 qed "wfrec";
   367 ---------------------------------------------------------------------------*)
   368 
   369 (*---------------------------------------------------------------------------
   370  * This form avoids giant explosions in proofs.  NOTE USE OF == 
   371  *---------------------------------------------------------------------------*)
   372 val rew::prems = goal thy
   373     "[| f==wfrec r H;  wf(r) |] ==> f(a) = H (cut f r a) a";
   374 by (rewtac rew);
   375 by (REPEAT (resolve_tac (prems@[wfrec]) 1));
   376 qed "def_wfrec";
   377 
   378 
   379 (**** TFL variants ****)
   380 
   381 Goal "!R. wf R --> (!P. (!x. (!y. (y,x):R --> P y) --> P x) --> (!x. P x))";
   382 by (Clarify_tac 1);
   383 by (res_inst_tac [("r","R"),("P","P"), ("a","x")] wf_induct 1);
   384 by (assume_tac 1);
   385 by (Blast_tac 1);
   386 qed"tfl_wf_induct";
   387 
   388 Goal "!f R. (x,a):R --> (cut f R a)(x) = f(x)";
   389 by (Clarify_tac 1);
   390 by (rtac cut_apply 1);
   391 by (assume_tac 1);
   392 qed"tfl_cut_apply";
   393 
   394 Goal "!M R f. (f=wfrec R M) --> wf R --> (!x. f x = M (cut f R x) x)";
   395 by (Clarify_tac 1);
   396 by (etac wfrec 1);
   397 qed "tfl_wfrec";