src/FOL/IFOL.ML
changeset 0 a5a9c433f639
child 12 f17d542276b6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/FOL/IFOL.ML	Thu Sep 16 12:20:38 1993 +0200
     1.3 @@ -0,0 +1,408 @@
     1.4 +(*  Title: 	FOL/ifol.ML
     1.5 +    ID:         $Id$
     1.6 +    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
     1.7 +    Copyright   1992  University of Cambridge
     1.8 +
     1.9 +Tactics and lemmas for ifol.thy (intuitionistic first-order logic)
    1.10 +*)
    1.11 +
    1.12 +open IFOL;
    1.13 +
    1.14 +signature IFOL_LEMMAS = 
    1.15 +  sig
    1.16 +  val allE: thm
    1.17 +  val all_cong: thm
    1.18 +  val all_dupE: thm
    1.19 +  val all_impE: thm
    1.20 +  val box_equals: thm
    1.21 +  val conjE: thm
    1.22 +  val conj_cong: thm
    1.23 +  val conj_impE: thm
    1.24 +  val contrapos: thm
    1.25 +  val disj_cong: thm
    1.26 +  val disj_impE: thm
    1.27 +  val eq_cong: thm
    1.28 +  val eq_mp_tac: int -> tactic
    1.29 +  val ex1I: thm
    1.30 +  val ex1E: thm
    1.31 +  val ex1_equalsE: thm
    1.32 +  val ex1_cong: thm
    1.33 +  val ex_cong: thm
    1.34 +  val ex_impE: thm
    1.35 +  val iffD1: thm
    1.36 +  val iffD2: thm
    1.37 +  val iffE: thm
    1.38 +  val iffI: thm
    1.39 +  val iff_cong: thm
    1.40 +  val iff_impE: thm
    1.41 +  val iff_refl: thm
    1.42 +  val iff_sym: thm
    1.43 +  val iff_trans: thm
    1.44 +  val impE: thm
    1.45 +  val imp_cong: thm
    1.46 +  val imp_impE: thm
    1.47 +  val mp_tac: int -> tactic
    1.48 +  val notE: thm
    1.49 +  val notI: thm
    1.50 +  val not_cong: thm
    1.51 +  val not_impE: thm
    1.52 +  val not_sym: thm
    1.53 +  val not_to_imp: thm
    1.54 +  val pred1_cong: thm
    1.55 +  val pred2_cong: thm
    1.56 +  val pred3_cong: thm
    1.57 +  val pred_congs: thm list
    1.58 +  val rev_mp: thm
    1.59 +  val simp_equals: thm
    1.60 +  val ssubst: thm
    1.61 +  val subst_context: thm
    1.62 +  val subst_context2: thm
    1.63 +  val subst_context3: thm
    1.64 +  val sym: thm
    1.65 +  val trans: thm
    1.66 +  val TrueI: thm
    1.67 +  end;
    1.68 +
    1.69 +
    1.70 +structure IFOL_Lemmas : IFOL_LEMMAS =
    1.71 +struct
    1.72 +
    1.73 +val TrueI = prove_goalw IFOL.thy [True_def] "True"
    1.74 + (fn _ => [ (REPEAT (ares_tac [impI] 1)) ]);
    1.75 +
    1.76 +(*** Sequent-style elimination rules for & --> and ALL ***)
    1.77 +
    1.78 +val conjE = prove_goal IFOL.thy 
    1.79 +    "[| P&Q; [| P; Q |] ==> R |] ==> R"
    1.80 + (fn prems=>
    1.81 +  [ (REPEAT (resolve_tac prems 1
    1.82 +      ORELSE (resolve_tac [conjunct1, conjunct2] 1 THEN
    1.83 +              resolve_tac prems 1))) ]);
    1.84 +
    1.85 +val impE = prove_goal IFOL.thy 
    1.86 +    "[| P-->Q;  P;  Q ==> R |] ==> R"
    1.87 + (fn prems=> [ (REPEAT (resolve_tac (prems@[mp]) 1)) ]);
    1.88 +
    1.89 +val allE = prove_goal IFOL.thy 
    1.90 +    "[| ALL x.P(x); P(x) ==> R |] ==> R"
    1.91 + (fn prems=> [ (REPEAT (resolve_tac (prems@[spec]) 1)) ]);
    1.92 +
    1.93 +(*Duplicates the quantifier; for use with eresolve_tac*)
    1.94 +val all_dupE = prove_goal IFOL.thy 
    1.95 +    "[| ALL x.P(x);  [| P(x); ALL x.P(x) |] ==> R \
    1.96 +\    |] ==> R"
    1.97 + (fn prems=> [ (REPEAT (resolve_tac (prems@[spec]) 1)) ]);
    1.98 +
    1.99 +
   1.100 +(*** Negation rules, which translate between ~P and P-->False ***)
   1.101 +
   1.102 +val notI = prove_goalw IFOL.thy [not_def] "(P ==> False) ==> ~P"
   1.103 + (fn prems=> [ (REPEAT (ares_tac (prems@[impI]) 1)) ]);
   1.104 +
   1.105 +val notE = prove_goalw IFOL.thy [not_def] "[| ~P;  P |] ==> R"
   1.106 + (fn prems=>
   1.107 +  [ (resolve_tac [mp RS FalseE] 1),
   1.108 +    (REPEAT (resolve_tac prems 1)) ]);
   1.109 +
   1.110 +(*This is useful with the special implication rules for each kind of P. *)
   1.111 +val not_to_imp = prove_goal IFOL.thy 
   1.112 +    "[| ~P;  (P-->False) ==> Q |] ==> Q"
   1.113 + (fn prems=> [ (REPEAT (ares_tac (prems@[impI,notE]) 1)) ]);
   1.114 +
   1.115 +
   1.116 +(* For substitution int an assumption P, reduce Q to P-->Q, substitute into
   1.117 +   this implication, then apply impI to move P back into the assumptions.
   1.118 +   To specify P use something like
   1.119 +      eres_inst_tac [ ("P","ALL y. ?S(x,y)") ] rev_mp 1   *)
   1.120 +val rev_mp = prove_goal IFOL.thy "[| P;  P --> Q |] ==> Q"
   1.121 + (fn prems=> [ (REPEAT (resolve_tac (prems@[mp]) 1)) ]);
   1.122 +
   1.123 +
   1.124 +(*Contrapositive of an inference rule*)
   1.125 +val contrapos = prove_goal IFOL.thy "[| ~Q;  P==>Q |] ==> ~P"
   1.126 + (fn [major,minor]=> 
   1.127 +  [ (rtac (major RS notE RS notI) 1), 
   1.128 +    (etac minor 1) ]);
   1.129 +
   1.130 +
   1.131 +(*** Modus Ponens Tactics ***)
   1.132 +
   1.133 +(*Finds P-->Q and P in the assumptions, replaces implication by Q *)
   1.134 +fun mp_tac i = eresolve_tac [notE,impE] i  THEN  assume_tac i;
   1.135 +
   1.136 +(*Like mp_tac but instantiates no variables*)
   1.137 +fun eq_mp_tac i = eresolve_tac [notE,impE] i  THEN  eq_assume_tac i;
   1.138 +
   1.139 +
   1.140 +(*** If-and-only-if ***)
   1.141 +
   1.142 +val iffI = prove_goalw IFOL.thy [iff_def]
   1.143 +   "[| P ==> Q;  Q ==> P |] ==> P<->Q"
   1.144 + (fn prems=> [ (REPEAT (ares_tac (prems@[conjI, impI]) 1)) ]);
   1.145 +
   1.146 +
   1.147 +(*Observe use of rewrite_rule to unfold "<->" in meta-assumptions (prems) *)
   1.148 +val iffE = prove_goalw IFOL.thy [iff_def]
   1.149 +    "[| P <-> Q;  [| P-->Q; Q-->P |] ==> R |] ==> R"
   1.150 + (fn prems => [ (resolve_tac [conjE] 1), (REPEAT (ares_tac prems 1)) ]);
   1.151 +
   1.152 +(* Destruct rules for <-> similar to Modus Ponens *)
   1.153 +
   1.154 +val iffD1 = prove_goalw IFOL.thy [iff_def] "[| P <-> Q;  P |] ==> Q"
   1.155 + (fn prems => [ (rtac (conjunct1 RS mp) 1), (REPEAT (ares_tac prems 1)) ]);
   1.156 +
   1.157 +val iffD2 = prove_goalw IFOL.thy [iff_def] "[| P <-> Q;  Q |] ==> P"
   1.158 + (fn prems => [ (rtac (conjunct2 RS mp) 1), (REPEAT (ares_tac prems 1)) ]);
   1.159 +
   1.160 +val iff_refl = prove_goal IFOL.thy "P <-> P"
   1.161 + (fn _ => [ (REPEAT (ares_tac [iffI] 1)) ]);
   1.162 +
   1.163 +val iff_sym = prove_goal IFOL.thy "Q <-> P ==> P <-> Q"
   1.164 + (fn [major] =>
   1.165 +  [ (rtac (major RS iffE) 1),
   1.166 +    (rtac iffI 1),
   1.167 +    (REPEAT (eresolve_tac [asm_rl,mp] 1)) ]);
   1.168 +
   1.169 +val iff_trans = prove_goal IFOL.thy
   1.170 +    "!!P Q R. [| P <-> Q;  Q<-> R |] ==> P <-> R"
   1.171 + (fn _ =>
   1.172 +  [ (rtac iffI 1),
   1.173 +    (REPEAT (eresolve_tac [asm_rl,iffE] 1 ORELSE mp_tac 1)) ]);
   1.174 +
   1.175 +
   1.176 +(*** Unique existence.  NOTE THAT the following 2 quantifications
   1.177 +   EX!x such that [EX!y such that P(x,y)]     (sequential)
   1.178 +   EX!x,y such that P(x,y)                    (simultaneous)
   1.179 + do NOT mean the same thing.  The parser treats EX!x y.P(x,y) as sequential.
   1.180 +***)
   1.181 +
   1.182 +val ex1I = prove_goalw IFOL.thy [ex1_def]
   1.183 +    "[| P(a);  !!x. P(x) ==> x=a |] ==> EX! x. P(x)"
   1.184 + (fn prems => [ (REPEAT (ares_tac (prems@[exI,conjI,allI,impI]) 1)) ]);
   1.185 +
   1.186 +val ex1E = prove_goalw IFOL.thy [ex1_def]
   1.187 +    "[| EX! x.P(x);  !!x. [| P(x);  ALL y. P(y) --> y=x |] ==> R |] ==> R"
   1.188 + (fn prems =>
   1.189 +  [ (cut_facts_tac prems 1),
   1.190 +    (REPEAT (eresolve_tac [exE,conjE] 1 ORELSE ares_tac prems 1)) ]);
   1.191 +
   1.192 +
   1.193 +(*** <-> congruence rules for simplification ***)
   1.194 +
   1.195 +(*Use iffE on a premise.  For conj_cong, imp_cong, all_cong, ex_cong*)
   1.196 +fun iff_tac prems i =
   1.197 +    resolve_tac (prems RL [iffE]) i THEN
   1.198 +    REPEAT1 (eresolve_tac [asm_rl,mp] i);
   1.199 +
   1.200 +val conj_cong = prove_goal IFOL.thy 
   1.201 +    "[| P <-> P';  P' ==> Q <-> Q' |] ==> (P&Q) <-> (P'&Q')"
   1.202 + (fn prems =>
   1.203 +  [ (cut_facts_tac prems 1),
   1.204 +    (REPEAT  (ares_tac [iffI,conjI] 1
   1.205 +      ORELSE  eresolve_tac [iffE,conjE,mp] 1
   1.206 +      ORELSE  iff_tac prems 1)) ]);
   1.207 +
   1.208 +val disj_cong = prove_goal IFOL.thy 
   1.209 +    "[| P <-> P';  Q <-> Q' |] ==> (P|Q) <-> (P'|Q')"
   1.210 + (fn prems =>
   1.211 +  [ (cut_facts_tac prems 1),
   1.212 +    (REPEAT  (eresolve_tac [iffE,disjE,disjI1,disjI2] 1
   1.213 +      ORELSE  ares_tac [iffI] 1
   1.214 +      ORELSE  mp_tac 1)) ]);
   1.215 +
   1.216 +val imp_cong = prove_goal IFOL.thy 
   1.217 +    "[| P <-> P';  P' ==> Q <-> Q' |] ==> (P-->Q) <-> (P'-->Q')"
   1.218 + (fn prems =>
   1.219 +  [ (cut_facts_tac prems 1),
   1.220 +    (REPEAT   (ares_tac [iffI,impI] 1
   1.221 +      ORELSE  eresolve_tac [iffE] 1
   1.222 +      ORELSE  mp_tac 1 ORELSE iff_tac prems 1)) ]);
   1.223 +
   1.224 +val iff_cong = prove_goal IFOL.thy 
   1.225 +    "[| P <-> P';  Q <-> Q' |] ==> (P<->Q) <-> (P'<->Q')"
   1.226 + (fn prems =>
   1.227 +  [ (cut_facts_tac prems 1),
   1.228 +    (REPEAT   (eresolve_tac [iffE] 1
   1.229 +      ORELSE  ares_tac [iffI] 1
   1.230 +      ORELSE  mp_tac 1)) ]);
   1.231 +
   1.232 +val not_cong = prove_goal IFOL.thy 
   1.233 +    "P <-> P' ==> ~P <-> ~P'"
   1.234 + (fn prems =>
   1.235 +  [ (cut_facts_tac prems 1),
   1.236 +    (REPEAT   (ares_tac [iffI,notI] 1
   1.237 +      ORELSE  mp_tac 1
   1.238 +      ORELSE  eresolve_tac [iffE,notE] 1)) ]);
   1.239 +
   1.240 +val all_cong = prove_goal IFOL.thy 
   1.241 +    "(!!x.P(x) <-> Q(x)) ==> (ALL x.P(x)) <-> (ALL x.Q(x))"
   1.242 + (fn prems =>
   1.243 +  [ (REPEAT   (ares_tac [iffI,allI] 1
   1.244 +      ORELSE   mp_tac 1
   1.245 +      ORELSE   eresolve_tac [allE] 1 ORELSE iff_tac prems 1)) ]);
   1.246 +
   1.247 +val ex_cong = prove_goal IFOL.thy 
   1.248 +    "(!!x.P(x) <-> Q(x)) ==> (EX x.P(x)) <-> (EX x.Q(x))"
   1.249 + (fn prems =>
   1.250 +  [ (REPEAT   (eresolve_tac [exE] 1 ORELSE ares_tac [iffI,exI] 1
   1.251 +      ORELSE   mp_tac 1
   1.252 +      ORELSE   iff_tac prems 1)) ]);
   1.253 +
   1.254 +val ex1_cong = prove_goal IFOL.thy 
   1.255 +    "(!!x.P(x) <-> Q(x)) ==> (EX! x.P(x)) <-> (EX! x.Q(x))"
   1.256 + (fn prems =>
   1.257 +  [ (REPEAT   (eresolve_tac [ex1E, spec RS mp] 1 ORELSE ares_tac [iffI,ex1I] 1
   1.258 +      ORELSE   mp_tac 1
   1.259 +      ORELSE   iff_tac prems 1)) ]);
   1.260 +
   1.261 +(*** Equality rules ***)
   1.262 +
   1.263 +val sym = prove_goal IFOL.thy "a=b ==> b=a"
   1.264 + (fn [major] => [ (rtac (major RS subst) 1), (rtac refl 1) ]);
   1.265 +
   1.266 +val trans = prove_goal IFOL.thy "[| a=b;  b=c |] ==> a=c"
   1.267 + (fn [prem1,prem2] => [ (rtac (prem2 RS subst) 1), (rtac prem1 1) ]);
   1.268 +
   1.269 +(** ~ b=a ==> ~ a=b **)
   1.270 +val [not_sym] = compose(sym,2,contrapos);
   1.271 +
   1.272 +(*calling "standard" reduces maxidx to 0*)
   1.273 +val ssubst = standard (sym RS subst);
   1.274 +
   1.275 +(*A special case of ex1E that would otherwise need quantifier expansion*)
   1.276 +val ex1_equalsE = prove_goal IFOL.thy
   1.277 +    "[| EX! x.P(x);  P(a);  P(b) |] ==> a=b"
   1.278 + (fn prems =>
   1.279 +  [ (cut_facts_tac prems 1),
   1.280 +    (etac ex1E 1),
   1.281 +    (rtac trans 1),
   1.282 +    (rtac sym 2),
   1.283 +    (REPEAT (eresolve_tac [asm_rl, spec RS mp] 1)) ]);
   1.284 +
   1.285 +(** Polymorphic congruence rules **)
   1.286 +
   1.287 +val subst_context = prove_goal IFOL.thy 
   1.288 +   "[| a=b |]  ==>  t(a)=t(b)"
   1.289 + (fn prems=>
   1.290 +  [ (resolve_tac (prems RL [ssubst]) 1),
   1.291 +    (resolve_tac [refl] 1) ]);
   1.292 +
   1.293 +val subst_context2 = prove_goal IFOL.thy 
   1.294 +   "[| a=b;  c=d |]  ==>  t(a,c)=t(b,d)"
   1.295 + (fn prems=>
   1.296 +  [ (EVERY1 (map rtac ((prems RL [ssubst]) @ [refl]))) ]);
   1.297 +
   1.298 +val subst_context3 = prove_goal IFOL.thy 
   1.299 +   "[| a=b;  c=d;  e=f |]  ==>  t(a,c,e)=t(b,d,f)"
   1.300 + (fn prems=>
   1.301 +  [ (EVERY1 (map rtac ((prems RL [ssubst]) @ [refl]))) ]);
   1.302 +
   1.303 +(*Useful with eresolve_tac for proving equalties from known equalities.
   1.304 +	a = b
   1.305 +	|   |
   1.306 +	c = d	*)
   1.307 +val box_equals = prove_goal IFOL.thy
   1.308 +    "[| a=b;  a=c;  b=d |] ==> c=d"  
   1.309 + (fn prems=>
   1.310 +  [ (resolve_tac [trans] 1),
   1.311 +    (resolve_tac [trans] 1),
   1.312 +    (resolve_tac [sym] 1),
   1.313 +    (REPEAT (resolve_tac prems 1)) ]);
   1.314 +
   1.315 +(*Dual of box_equals: for proving equalities backwards*)
   1.316 +val simp_equals = prove_goal IFOL.thy
   1.317 +    "[| a=c;  b=d;  c=d |] ==> a=b"  
   1.318 + (fn prems=>
   1.319 +  [ (resolve_tac [trans] 1),
   1.320 +    (resolve_tac [trans] 1),
   1.321 +    (REPEAT (resolve_tac (prems @ (prems RL [sym])) 1)) ]);
   1.322 +
   1.323 +(** Congruence rules for predicate letters **)
   1.324 +
   1.325 +val pred1_cong = prove_goal IFOL.thy
   1.326 +    "a=a' ==> P(a) <-> P(a')"
   1.327 + (fn prems =>
   1.328 +  [ (cut_facts_tac prems 1),
   1.329 +    (rtac iffI 1),
   1.330 +    (DEPTH_SOLVE (eresolve_tac [asm_rl, subst, ssubst] 1)) ]);
   1.331 +
   1.332 +val pred2_cong = prove_goal IFOL.thy
   1.333 +    "[| a=a';  b=b' |] ==> P(a,b) <-> P(a',b')"
   1.334 + (fn prems =>
   1.335 +  [ (cut_facts_tac prems 1),
   1.336 +    (rtac iffI 1),
   1.337 +    (DEPTH_SOLVE (eresolve_tac [asm_rl, subst, ssubst] 1)) ]);
   1.338 +
   1.339 +val pred3_cong = prove_goal IFOL.thy
   1.340 +    "[| a=a';  b=b';  c=c' |] ==> P(a,b,c) <-> P(a',b',c')"
   1.341 + (fn prems =>
   1.342 +  [ (cut_facts_tac prems 1),
   1.343 +    (rtac iffI 1),
   1.344 +    (DEPTH_SOLVE (eresolve_tac [asm_rl, subst, ssubst] 1)) ]);
   1.345 +
   1.346 +(*special cases for free variables P, Q, R, S -- up to 3 arguments*)
   1.347 +
   1.348 +val pred_congs = 
   1.349 +    flat (map (fn c => 
   1.350 +	       map (fn th => read_instantiate [("P",c)] th)
   1.351 +		   [pred1_cong,pred2_cong,pred3_cong])
   1.352 +	       (explode"PQRS"));
   1.353 +
   1.354 +(*special case for the equality predicate!*)
   1.355 +val eq_cong = read_instantiate [("P","op =")] pred2_cong;
   1.356 +
   1.357 +
   1.358 +(*** Simplifications of assumed implications.
   1.359 +     Roy Dyckhoff has proved that conj_impE, disj_impE, and imp_impE
   1.360 +     used with mp_tac (restricted to atomic formulae) is COMPLETE for 
   1.361 +     intuitionistic propositional logic.  See
   1.362 +   R. Dyckhoff, Contraction-free sequent calculi for intuitionistic logic
   1.363 +    (preprint, University of St Andrews, 1991)  ***)
   1.364 +
   1.365 +val conj_impE = prove_goal IFOL.thy 
   1.366 +    "[| (P&Q)-->S;  P-->(Q-->S) ==> R |] ==> R"
   1.367 + (fn major::prems=>
   1.368 +  [ (REPEAT (ares_tac ([conjI, impI, major RS mp]@prems) 1)) ]);
   1.369 +
   1.370 +val disj_impE = prove_goal IFOL.thy 
   1.371 +    "[| (P|Q)-->S;  [| P-->S; Q-->S |] ==> R |] ==> R"
   1.372 + (fn major::prems=>
   1.373 +  [ (DEPTH_SOLVE (ares_tac ([disjI1, disjI2, impI, major RS mp]@prems) 1)) ]);
   1.374 +
   1.375 +(*Simplifies the implication.  Classical version is stronger. 
   1.376 +  Still UNSAFE since Q must be provable -- backtracking needed.  *)
   1.377 +val imp_impE = prove_goal IFOL.thy 
   1.378 +    "[| (P-->Q)-->S;  [| P; Q-->S |] ==> Q;  S ==> R |] ==> R"
   1.379 + (fn major::prems=>
   1.380 +  [ (REPEAT (ares_tac ([impI, major RS mp]@prems) 1)) ]);
   1.381 +
   1.382 +(*Simplifies the implication.  Classical version is stronger. 
   1.383 +  Still UNSAFE since ~P must be provable -- backtracking needed.  *)
   1.384 +val not_impE = prove_goal IFOL.thy
   1.385 +    "[| ~P --> S;  P ==> False;  S ==> R |] ==> R"
   1.386 + (fn major::prems=>
   1.387 +  [ (REPEAT (ares_tac ([notI, impI, major RS mp]@prems) 1)) ]);
   1.388 +
   1.389 +(*Simplifies the implication.   UNSAFE.  *)
   1.390 +val iff_impE = prove_goal IFOL.thy 
   1.391 +    "[| (P<->Q)-->S;  [| P; Q-->S |] ==> Q;  [| Q; P-->S |] ==> P;  \
   1.392 +\       S ==> R |] ==> R"
   1.393 + (fn major::prems=>
   1.394 +  [ (REPEAT (ares_tac ([iffI, impI, major RS mp]@prems) 1)) ]);
   1.395 +
   1.396 +(*What if (ALL x.~~P(x)) --> ~~(ALL x.P(x)) is an assumption? UNSAFE*)
   1.397 +val all_impE = prove_goal IFOL.thy 
   1.398 +    "[| (ALL x.P(x))-->S;  !!x.P(x);  S ==> R |] ==> R"
   1.399 + (fn major::prems=>
   1.400 +  [ (REPEAT (ares_tac ([allI, impI, major RS mp]@prems) 1)) ]);
   1.401 +
   1.402 +(*Unsafe: (EX x.P(x))-->S  is equivalent to  ALL x.P(x)-->S.  *)
   1.403 +val ex_impE = prove_goal IFOL.thy 
   1.404 +    "[| (EX x.P(x))-->S;  P(x)-->S ==> R |] ==> R"
   1.405 + (fn major::prems=>
   1.406 +  [ (REPEAT (ares_tac ([exI, impI, major RS mp]@prems) 1)) ]);
   1.407 +
   1.408 +end;
   1.409 +
   1.410 +open IFOL_Lemmas;
   1.411 +