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