src/HOL/HOL.ML
 changeset 7357 d0e16da40ea2 parent 7030 53934985426a child 7529 fa534e4f7e49
```     1.1 --- a/src/HOL/HOL.ML	Wed Aug 25 20:46:40 1999 +0200
1.2 +++ b/src/HOL/HOL.ML	Wed Aug 25 20:49:02 1999 +0200
1.3 @@ -1,456 +1,31 @@
1.4 -(*  Title:      HOL/HOL.ML
1.5 -    ID:         \$Id\$
1.6 -    Author:     Tobias Nipkow
1.7 -    Copyright   1991  University of Cambridge
1.8
1.9 -Derived rules from Appendix of Mike Gordons HOL Report, Cambridge TR 68
1.10 -*)
1.11 -
1.12 -
1.13 -(** Equality **)
1.14 -section "=";
1.15 -
1.16 -qed_goal "sym" HOL.thy "s=t ==> t=s"
1.17 - (fn prems => [cut_facts_tac prems 1, etac subst 1, rtac refl 1]);
1.18 -
1.19 -(*calling "standard" reduces maxidx to 0*)
1.20 -bind_thm ("ssubst", (sym RS subst));
1.21 -
1.22 -qed_goal "trans" HOL.thy "[| r=s; s=t |] ==> r=t"
1.23 - (fn prems =>
1.24 -        [rtac subst 1, resolve_tac prems 1, resolve_tac prems 1]);
1.25 -
1.26 -val prems = goal thy "(A == B) ==> A = B";
1.27 -by (rewrite_goals_tac prems);
1.28 -by (rtac refl 1);
1.29 -qed "def_imp_eq";
1.30 -
1.31 -(*Useful with eresolve_tac for proving equalties from known equalities.
1.32 -        a = b
1.33 -        |   |
1.34 -        c = d   *)
1.35 -Goal "[| a=b;  a=c;  b=d |] ==> c=d";
1.36 -by (rtac trans 1);
1.37 -by (rtac trans 1);
1.38 -by (rtac sym 1);
1.39 -by (REPEAT (assume_tac 1)) ;
1.40 -qed "box_equals";
1.41 -
1.42 -
1.43 -(** Congruence rules for meta-application **)
1.44 -section "Congruence";
1.45 -
1.46 -(*similar to AP_THM in Gordon's HOL*)
1.47 -qed_goal "fun_cong" HOL.thy "(f::'a=>'b) = g ==> f(x)=g(x)"
1.48 -  (fn [prem] => [rtac (prem RS subst) 1, rtac refl 1]);
1.49 -
1.50 -(*similar to AP_TERM in Gordon's HOL and FOL's subst_context*)
1.51 -qed_goal "arg_cong" HOL.thy "x=y ==> f(x)=f(y)"
1.52 - (fn [prem] => [rtac (prem RS subst) 1, rtac refl 1]);
1.53 -
1.54 -qed_goal "cong" HOL.thy
1.55 -   "[| f = g; (x::'a) = y |] ==> f(x) = g(y)"
1.56 - (fn [prem1,prem2] =>
1.57 -   [rtac (prem1 RS subst) 1, rtac (prem2 RS subst) 1, rtac refl 1]);
1.58 -
1.59 -
1.60 -(** Equality of booleans -- iff **)
1.61 -section "iff";
1.62 -
1.63 -val prems = Goal
1.64 -   "[| P ==> Q;  Q ==> P |] ==> P=Q";
1.65 -by (REPEAT (ares_tac (prems@[impI, iff RS mp RS mp]) 1));
1.66 -qed "iffI";
1.67 -
1.68 -qed_goal "iffD2" HOL.thy "[| P=Q; Q |] ==> P"
1.69 - (fn prems =>
1.70 -        [rtac ssubst 1, resolve_tac prems 1, resolve_tac prems 1]);
1.71 -
1.72 -qed_goal "rev_iffD2" HOL.thy "!!P. [| Q; P=Q |] ==> P"
1.73 - (fn _ => [etac iffD2 1, assume_tac 1]);
1.74 -
1.75 -bind_thm ("iffD1", sym RS iffD2);
1.76 -bind_thm ("rev_iffD1", sym RSN (2, rev_iffD2));
1.77 -
1.78 -qed_goal "iffE" HOL.thy
1.79 -    "[| P=Q; [| P --> Q; Q --> P |] ==> R |] ==> R"
1.80 - (fn [p1,p2] => [REPEAT(ares_tac([p1 RS iffD2, p1 RS iffD1, p2, impI])1)]);
1.81 -
1.82 -
1.83 -(** True **)
1.84 -section "True";
1.85 -
1.86 -qed_goalw "TrueI" HOL.thy [True_def] "True"
1.87 -  (fn _ => [(rtac refl 1)]);
1.88 -
1.89 -qed_goal "eqTrueI" HOL.thy "P ==> P=True"
1.90 - (fn prems => [REPEAT(resolve_tac ([iffI,TrueI]@prems) 1)]);
1.91 -
1.92 -qed_goal "eqTrueE" HOL.thy "P=True ==> P"
1.93 - (fn prems => [REPEAT(resolve_tac (prems@[TrueI,iffD2]) 1)]);
1.94 -
1.95 -
1.96 -(** Universal quantifier **)
1.97 -section "!";
1.98 -
1.99 -qed_goalw "allI" HOL.thy [All_def] "(!!x::'a. P(x)) ==> !x. P(x)"
1.100 - (fn prems => [(resolve_tac (prems RL [eqTrueI RS ext]) 1)]);
1.101 -
1.102 -qed_goalw "spec" HOL.thy [All_def] "! x::'a. P(x) ==> P(x)"
1.103 - (fn prems => [rtac eqTrueE 1, resolve_tac (prems RL [fun_cong]) 1]);
1.104 -
1.105 -val major::prems= goal HOL.thy "[| !x. P(x);  P(x) ==> R |] ==> R";
1.106 -by (REPEAT (resolve_tac (prems @ [major RS spec]) 1)) ;
1.107 -qed "allE";
1.108 -
1.109 -val prems = goal HOL.thy
1.110 -    "[| ! x. P(x);  [| P(x); ! x. P(x) |] ==> R |] ==> R";
1.111 -by (REPEAT (resolve_tac (prems @ (prems RL [spec])) 1)) ;
1.112 -qed "all_dupE";
1.113 -
1.114 -
1.115 -(** False ** Depends upon spec; it is impossible to do propositional logic
1.116 -             before quantifiers! **)
1.117 -section "False";
1.118 -
1.119 -qed_goalw "FalseE" HOL.thy [False_def] "False ==> P"
1.120 - (fn [major] => [rtac (major RS spec) 1]);
1.121 -
1.122 -qed_goal "False_neq_True" HOL.thy "False=True ==> P"
1.123 - (fn [prem] => [rtac (prem RS eqTrueE RS FalseE) 1]);
1.124 -
1.125 -
1.126 -(** Negation **)
1.127 -section "~";
1.128 -
1.129 -qed_goalw "notI" HOL.thy [not_def] "(P ==> False) ==> ~P"
1.130 - (fn prems=> [rtac impI 1, eresolve_tac prems 1]);
1.131 -
1.132 -qed_goal "False_not_True" HOL.thy "False ~= True"
1.133 -  (fn _ => [rtac notI 1, etac False_neq_True 1]);
1.134 -
1.135 -qed_goal "True_not_False" HOL.thy "True ~= False"
1.136 -  (fn _ => [rtac notI 1, dtac sym 1, etac False_neq_True 1]);
1.137 -
1.138 -qed_goalw "notE" HOL.thy [not_def] "[| ~P;  P |] ==> R"
1.139 - (fn prems => [rtac (prems MRS mp RS FalseE) 1]);
1.140 -
1.141 -bind_thm ("classical2", notE RS notI);
1.142 -
1.143 -qed_goal "rev_notE" HOL.thy "!!P R. [| P; ~P |] ==> R"
1.144 - (fn _ => [REPEAT (ares_tac [notE] 1)]);
1.145 -
1.146 -
1.147 -(** Implication **)
1.148 -section "-->";
1.149 -
1.150 -val prems = Goal "[| P-->Q;  P;  Q ==> R |] ==> R";
1.151 -by (REPEAT (resolve_tac (prems@[mp]) 1));
1.152 -qed "impE";
1.153 -
1.154 -(* Reduces Q to P-->Q, allowing substitution in P. *)
1.155 -Goal "[| P;  P --> Q |] ==> Q";
1.156 -by (REPEAT (ares_tac [mp] 1)) ;
1.157 -qed "rev_mp";
1.158 -
1.159 -val [major,minor] = Goal "[| ~Q;  P==>Q |] ==> ~P";
1.160 -by (rtac (major RS notE RS notI) 1);
1.161 -by (etac minor 1) ;
1.162 -qed "contrapos";
1.163 -
1.164 -val [major,minor] = Goal "[| P==>Q; ~Q |] ==> ~P";
1.165 -by (rtac (minor RS contrapos) 1);
1.166 -by (etac major 1) ;
1.167 -qed "rev_contrapos";
1.168 -
1.169 -(* ~(?t = ?s) ==> ~(?s = ?t) *)
1.170 -bind_thm("not_sym", sym COMP rev_contrapos);
1.171 -
1.172 -
1.173 -(** Existential quantifier **)
1.174 -section "?";
1.175 -
1.176 -qed_goalw "exI" HOL.thy [Ex_def] "P x ==> ? x::'a. P x"
1.177 - (fn prems => [rtac selectI 1, resolve_tac prems 1]);
1.178 -
1.179 -qed_goalw "exE" HOL.thy [Ex_def]
1.180 -  "[| ? x::'a. P(x); !!x. P(x) ==> Q |] ==> Q"
1.181 -  (fn prems => [REPEAT(resolve_tac prems 1)]);
1.182 -
1.183 -
1.184 -(** Conjunction **)
1.185 -section "&";
1.186 -
1.187 -qed_goalw "conjI" HOL.thy [and_def] "[| P; Q |] ==> P&Q"
1.188 - (fn prems =>
1.189 -  [REPEAT (resolve_tac (prems@[allI,impI]) 1 ORELSE etac (mp RS mp) 1)]);
1.190 -
1.191 -qed_goalw "conjunct1" HOL.thy [and_def] "[| P & Q |] ==> P"
1.192 - (fn prems =>
1.193 -   [resolve_tac (prems RL [spec] RL [mp]) 1, REPEAT(ares_tac [impI] 1)]);
1.194 -
1.195 -qed_goalw "conjunct2" HOL.thy [and_def] "[| P & Q |] ==> Q"
1.196 - (fn prems =>
1.197 -   [resolve_tac (prems RL [spec] RL [mp]) 1, REPEAT(ares_tac [impI] 1)]);
1.198 -
1.199 -qed_goal "conjE" HOL.thy "[| P&Q;  [| P; Q |] ==> R |] ==> R"
1.200 - (fn prems =>
1.201 -         [cut_facts_tac prems 1, resolve_tac prems 1,
1.202 -          etac conjunct1 1, etac conjunct2 1]);
1.203 -
1.204 -qed_goal "context_conjI" HOL.thy  "[| P; P ==> Q |] ==> P & Q"
1.205 - (fn prems => [REPEAT(resolve_tac (conjI::prems) 1)]);
1.206 -
1.207 -
1.208 -(** Disjunction *)
1.209 -section "|";
1.210 -
1.211 -qed_goalw "disjI1" HOL.thy [or_def] "P ==> P|Q"
1.212 - (fn [prem] => [REPEAT(ares_tac [allI,impI, prem RSN (2,mp)] 1)]);
1.213 -
1.214 -qed_goalw "disjI2" HOL.thy [or_def] "Q ==> P|Q"
1.215 - (fn [prem] => [REPEAT(ares_tac [allI,impI, prem RSN (2,mp)] 1)]);
1.216 -
1.217 -qed_goalw "disjE" HOL.thy [or_def] "[| P | Q; P ==> R; Q ==> R |] ==> R"
1.218 - (fn [a1,a2,a3] =>
1.219 -        [rtac (mp RS mp) 1, rtac spec 1, rtac a1 1,
1.220 -         rtac (a2 RS impI) 1, assume_tac 1, rtac (a3 RS impI) 1, assume_tac 1]);
1.221 -
1.222 -
1.223 -(** CCONTR -- classical logic **)
1.224 -section "classical logic";
1.225 -
1.226 -qed_goalw "classical" HOL.thy [not_def]  "(~P ==> P) ==> P"
1.227 - (fn [prem] =>
1.228 -   [rtac (True_or_False RS (disjE RS eqTrueE)) 1,  assume_tac 1,
1.229 -    rtac (impI RS prem RS eqTrueI) 1,
1.230 -    etac subst 1,  assume_tac 1]);
1.231 -
1.232 -val ccontr = FalseE RS classical;
1.233 -
1.234 -(*Double negation law*)
1.235 -Goal "~~P ==> P";
1.236 -by (rtac classical 1);
1.237 -by (etac notE 1);
1.238 -by (assume_tac 1);
1.239 -qed "notnotD";
1.240 -
1.241 -val [p1,p2] = Goal "[| Q; ~ P ==> ~ Q |] ==> P";
1.242 -by (rtac classical 1);
1.243 -by (dtac p2 1);
1.244 -by (etac notE 1);
1.245 -by (rtac p1 1);
1.246 -qed "contrapos2";
1.247 -
1.248 -val [p1,p2] = Goal "[| P;  Q ==> ~ P |] ==> ~ Q";
1.249 -by (rtac notI 1);
1.250 -by (dtac p2 1);
1.251 -by (etac notE 1);
1.252 -by (rtac p1 1);
1.253 -qed "swap2";
1.254 -
1.255 -(** Unique existence **)
1.256 -section "?!";
1.257 -
1.258 -qed_goalw "ex1I" HOL.thy [Ex1_def]
1.259 -            "[| P(a);  !!x. P(x) ==> x=a |] ==> ?! x. P(x)"
1.260 - (fn prems =>
1.261 -  [REPEAT (ares_tac (prems@[exI,conjI,allI,impI]) 1)]);
1.262 -
1.263 -(*Sometimes easier to use: the premises have no shared variables.  Safe!*)
1.264 -val [ex,eq] = Goal
1.265 -    "[| ? x. P(x);  !!x y. [| P(x); P(y) |] ==> x=y |] ==> ?! x. P(x)";
1.266 -by (rtac (ex RS exE) 1);
1.267 -by (REPEAT (ares_tac [ex1I,eq] 1)) ;
1.268 -qed "ex_ex1I";
1.269 -
1.270 -qed_goalw "ex1E" HOL.thy [Ex1_def]
1.271 -    "[| ?! x. P(x);  !!x. [| P(x);  ! y. P(y) --> y=x |] ==> R |] ==> R"
1.272 - (fn major::prems =>
1.273 -  [rtac (major RS exE) 1, REPEAT (etac conjE 1 ORELSE ares_tac prems 1)]);
1.274 -
1.275 -Goal "?! x. P x ==> ? x. P x";
1.276 -by (etac ex1E 1);
1.277 -by (rtac exI 1);
1.278 -by (assume_tac 1);
1.279 -qed "ex1_implies_ex";
1.280 -
1.281 -
1.282 -(** Select: Hilbert's Epsilon-operator **)
1.283 -section "@";
1.284 -
1.285 -(*Easier to apply than selectI: conclusion has only one occurrence of P*)
1.286 -val prems = Goal
1.287 -    "[| P a;  !!x. P x ==> Q x |] ==> Q (@x. P x)";
1.288 -by (resolve_tac prems 1);
1.289 -by (rtac selectI 1);
1.290 -by (resolve_tac prems 1) ;
1.291 -qed "selectI2";
1.292 -
1.293 -(*Easier to apply than selectI2 if witness ?a comes from an EX-formula*)
1.294 -qed_goal "selectI2EX" HOL.thy
1.295 -  "[| ? a. P a; !!x. P x ==> Q x |] ==> Q (Eps P)"
1.296 -(fn [major,minor] => [rtac (major RS exE) 1, etac selectI2 1, etac minor 1]);
1.297 -
1.298 -val prems = Goal
1.299 -    "[| P a;  !!x. P x ==> x=a |] ==> (@x. P x) = a";
1.300 -by (rtac selectI2 1);
1.301 -by (REPEAT (ares_tac prems 1)) ;
1.302 -qed "select_equality";
1.303 -
1.304 -Goalw [Ex1_def] "[| ?!x. P x; P a |] ==> (@x. P x) = a";
1.305 -by (rtac select_equality 1);
1.306 -by (atac 1);
1.307 -by (etac exE 1);
1.308 -by (etac conjE 1);
1.309 -by (rtac allE 1);
1.310 -by (atac 1);
1.311 -by (etac impE 1);
1.312 -by (atac 1);
1.313 -by (etac ssubst 1);
1.314 -by (etac allE 1);
1.315 -by (etac mp 1);
1.316 -by (atac 1);
1.317 -qed "select1_equality";
1.318 -
1.319 -Goal "P (@ x. P x) =  (? x. P x)";
1.320 -by (rtac iffI 1);
1.321 -by (etac exI 1);
1.322 -by (etac exE 1);
1.323 -by (etac selectI 1);
1.324 -qed "select_eq_Ex";
1.325 -
1.326 -Goal "(@y. y=x) = x";
1.327 -by (rtac select_equality 1);
1.328 -by (rtac refl 1);
1.329 -by (atac 1);
1.330 -qed "Eps_eq";
1.331 -
1.332 -Goal "(Eps (op = x)) = x";
1.333 -by (rtac select_equality 1);
1.334 -by (rtac refl 1);
1.335 -by (etac sym 1);
1.336 -qed "Eps_sym_eq";
1.337 -
1.338 -(** Classical intro rules for disjunction and existential quantifiers *)
1.339 -section "classical intro rules";
1.340 -
1.341 -val prems= Goal "(~Q ==> P) ==> P|Q";
1.342 -by (rtac classical 1);
1.343 -by (REPEAT (ares_tac (prems@[disjI1,notI]) 1));
1.344 -by (REPEAT (ares_tac (prems@[disjI2,notE]) 1)) ;
1.345 -qed "disjCI";
1.346 -
1.347 -Goal "~P | P";
1.348 -by (REPEAT (ares_tac [disjCI] 1)) ;
1.349 -qed "excluded_middle";
1.350 -
1.351 -(*For disjunctive case analysis*)
1.352 -fun excluded_middle_tac sP =
1.353 -    res_inst_tac [("Q",sP)] (excluded_middle RS disjE);
1.354 -
1.355 -(*Classical implies (-->) elimination. *)
1.356 -val major::prems = Goal "[| P-->Q; ~P ==> R; Q ==> R |] ==> R";
1.357 -by (rtac (excluded_middle RS disjE) 1);
1.358 -by (REPEAT (DEPTH_SOLVE_1 (ares_tac (prems @ [major RS mp]) 1)));
1.359 -qed "impCE";
1.360 -
1.361 -(*This version of --> elimination works on Q before P.  It works best for
1.362 -  those cases in which P holds "almost everywhere".  Can't install as
1.363 -  default: would break old proofs.*)
1.364 -val major::prems = Goal
1.365 -    "[| P-->Q;  Q ==> R;  ~P ==> R |] ==> R";
1.366 -by (resolve_tac [excluded_middle RS disjE] 1);
1.367 -by (DEPTH_SOLVE (ares_tac (prems@[major RS mp]) 1)) ;
1.368 -qed "impCE'";
1.369 -
1.370 -(*Classical <-> elimination. *)
1.371 -val major::prems = Goal
1.372 -    "[| P=Q;  [| P; Q |] ==> R;  [| ~P; ~Q |] ==> R |] ==> R";
1.373 -by (rtac (major RS iffE) 1);
1.374 -by (REPEAT (DEPTH_SOLVE_1
1.375 -	    (eresolve_tac ([asm_rl,impCE,notE]@prems) 1)));
1.376 -qed "iffCE";
1.377 -
1.378 -val prems = Goal "(! x. ~P(x) ==> P(a)) ==> ? x. P(x)";
1.379 -by (rtac ccontr 1);
1.380 -by (REPEAT (ares_tac (prems@[exI,allI,notI,notE]) 1))  ;
1.381 -qed "exCI";
1.382 -
1.383 -
1.384 -(* case distinction *)
1.385 -
1.386 -qed_goal "case_split_thm" HOL.thy "[| P ==> Q; ~P ==> Q |] ==> Q"
1.387 -  (fn [p1,p2] => [rtac (excluded_middle RS disjE) 1,
1.388 -                  etac p2 1, etac p1 1]);
1.389 -
1.390 -fun case_tac a = res_inst_tac [("P",a)] case_split_thm;
1.391 -
1.392 -
1.393 -(** Standard abbreviations **)
1.394 -
1.395 -(*Apply an equality or definition ONCE.
1.396 -  Fails unless the substitution has an effect*)
1.397 -fun stac th =
1.398 -  let val th' = th RS def_imp_eq handle THM _ => th
1.399 -  in  CHANGED_GOAL (rtac (th' RS ssubst))
1.400 -  end;
1.401 -
1.402 -fun strip_tac i = REPEAT(resolve_tac [impI,allI] i);
1.403 -
1.404 -
1.405 -(** strip ! and --> from proved goal while preserving !-bound var names **)
1.406 -
1.407 -local
1.408 -
1.409 -(* Use XXX to avoid forall_intr failing because of duplicate variable name *)
1.410 -val myspec = read_instantiate [("P","?XXX")] spec;
1.411 -val _ \$ (_ \$ (vx as Var(_,vxT))) = concl_of myspec;
1.412 -val cvx = cterm_of (#sign(rep_thm myspec)) vx;
1.413 -val aspec = forall_intr cvx myspec;
1.414 -
1.415 -in
1.416 -
1.417 -fun RSspec th =
1.418 -  (case concl_of th of
1.419 -     _ \$ (Const("All",_) \$ Abs(a,_,_)) =>
1.420 -         let val ca = cterm_of (#sign(rep_thm th)) (Var((a,0),vxT))
1.421 -         in th RS forall_elim ca aspec end
1.422 -  | _ => raise THM("RSspec",0,[th]));
1.423 -
1.424 -fun RSmp th =
1.425 -  (case concl_of th of
1.426 -     _ \$ (Const("op -->",_)\$_\$_) => th RS mp
1.427 -  | _ => raise THM("RSmp",0,[th]));
1.428 -
1.429 -fun normalize_thm funs =
1.430 -  let fun trans [] th = th
1.431 -	| trans (f::fs) th = (trans funs (f th)) handle THM _ => trans fs th
1.432 -  in zero_var_indexes o trans funs end;
1.433 -
1.434 -fun qed_spec_mp name =
1.435 -  let val thm = normalize_thm [RSspec,RSmp] (result())
1.436 -  in ThmDatabase.ml_store_thm(name, thm) end;
1.437 -
1.438 -fun qed_goal_spec_mp name thy s p =
1.439 -	bind_thm (name, normalize_thm [RSspec,RSmp] (prove_goal thy s p));
1.440 -
1.441 -fun qed_goalw_spec_mp name thy defs s p =
1.442 -	bind_thm (name, normalize_thm [RSspec,RSmp] (prove_goalw thy defs s p));
1.443 -
1.444 +structure HOL =
1.445 +struct
1.446 +  val thy = the_context ();
1.447 +  val plusI = plusI;
1.448 +  val minusI = minusI;
1.449 +  val timesI = timesI;
1.450 +  val powerI = powerI;
1.451 +  val eq_reflection = eq_reflection;
1.452 +  val refl = refl;
1.453 +  val subst = subst;
1.454 +  val ext = ext;
1.455 +  val selectI = selectI;
1.456 +  val impI = impI;
1.457 +  val mp = mp;
1.458 +  val True_def = True_def;
1.459 +  val All_def = All_def;
1.460 +  val Ex_def = Ex_def;
1.461 +  val False_def = False_def;
1.462 +  val not_def = not_def;
1.463 +  val and_def = and_def;
1.464 +  val or_def = or_def;
1.465 +  val Ex1_def = Ex1_def;
1.466 +  val iff = iff;
1.467 +  val True_or_False = True_or_False;
1.468 +  val Let_def = Let_def;
1.469 +  val if_def = if_def;
1.470 +  val arbitrary_def = arbitrary_def;
1.471  end;
1.472
1.473 -
1.474 -(* attributes *)
1.475 -
1.476 -local
1.477 -
1.478 -fun gen_rulify x = Attrib.no_args (Drule.rule_attribute (fn _ => (normalize_thm [RSspec, RSmp]))) x;
1.479 -
1.480 -in
1.481 -
1.482 -val hol_setup =