src/Pure/drule.ML
author paulson
Mon Dec 07 18:26:25 1998 +0100 (1998-12-07)
changeset 6019 0e55c2fb2ebb
parent 5903 5d9beee36fbe
child 6086 8cd4190e633a
permissions -rw-r--r--
tidying
     1 (*  Title:      Pure/drule.ML
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1993  University of Cambridge
     5 
     6 Derived rules and other operations on theorems.
     7 *)
     8 
     9 infix 0 RS RSN RL RLN MRS MRL COMP;
    10 
    11 signature BASIC_DRULE =
    12 sig
    13   val dest_implies      : cterm -> cterm * cterm
    14   val skip_flexpairs	: cterm -> cterm
    15   val strip_imp_prems	: cterm -> cterm list
    16   val cprems_of		: thm -> cterm list
    17   val read_insts	:
    18           Sign.sg -> (indexname -> typ option) * (indexname -> sort option)
    19                   -> (indexname -> typ option) * (indexname -> sort option)
    20                   -> string list -> (string*string)list
    21                   -> (indexname*ctyp)list * (cterm*cterm)list
    22   val types_sorts: thm -> (indexname-> typ option) * (indexname-> sort option)
    23   val forall_intr_list	: cterm list -> thm -> thm
    24   val forall_intr_frees	: thm -> thm
    25   val forall_intr_vars	: thm -> thm
    26   val forall_elim_list	: cterm list -> thm -> thm
    27   val forall_elim_var	: int -> thm -> thm
    28   val forall_elim_vars	: int -> thm -> thm
    29   val freeze_thaw	: thm -> thm * (thm -> thm)
    30   val implies_elim_list	: thm -> thm list -> thm
    31   val implies_intr_list	: cterm list -> thm -> thm
    32   val zero_var_indexes	: thm -> thm
    33   val standard		: thm -> thm
    34   val rotate_prems      : int -> thm -> thm
    35   val assume_ax		: theory -> string -> thm
    36   val RSN		: thm * (int * thm) -> thm
    37   val RS		: thm * thm -> thm
    38   val RLN		: thm list * (int * thm list) -> thm list
    39   val RL		: thm list * thm list -> thm list
    40   val MRS		: thm list * thm -> thm
    41   val MRL		: thm list list * thm list -> thm list
    42   val compose		: thm * int * thm -> thm list
    43   val COMP		: thm * thm -> thm
    44   val read_instantiate_sg: Sign.sg -> (string*string)list -> thm -> thm
    45   val read_instantiate	: (string*string)list -> thm -> thm
    46   val cterm_instantiate	: (cterm*cterm)list -> thm -> thm
    47   val weak_eq_thm	: thm * thm -> bool
    48   val eq_thm_sg		: thm * thm -> bool
    49   val size_of_thm	: thm -> int
    50   val reflexive_thm	: thm
    51   val symmetric_thm	: thm
    52   val transitive_thm	: thm
    53   val refl_implies      : thm
    54   val symmetric_fun     : thm -> thm
    55   val rewrite_rule_aux	: (meta_simpset -> thm -> thm option) -> thm list -> thm -> thm
    56   val rewrite_thm	: bool * bool * bool
    57                           -> (meta_simpset -> thm -> thm option)
    58                           -> meta_simpset -> thm -> thm
    59   val rewrite_cterm	: bool * bool * bool
    60                           -> (meta_simpset -> thm -> thm option)
    61                           -> meta_simpset -> cterm -> thm
    62   val rewrite_goals_rule_aux: (meta_simpset -> thm -> thm option) -> thm list -> thm -> thm
    63   val rewrite_goal_rule	: bool* bool * bool
    64                           -> (meta_simpset -> thm -> thm option)
    65                           -> meta_simpset -> int -> thm -> thm
    66   val equal_abs_elim	: cterm  -> thm -> thm
    67   val equal_abs_elim_list: cterm list -> thm -> thm
    68   val flexpair_abs_elim_list: cterm list -> thm -> thm
    69   val asm_rl		: thm
    70   val cut_rl		: thm
    71   val revcut_rl		: thm
    72   val thin_rl		: thm
    73   val triv_forall_equality: thm
    74   val swap_prems_rl     : thm
    75   val equal_intr_rule   : thm
    76   val instantiate'	: ctyp option list -> cterm option list -> thm -> thm
    77 end;
    78 
    79 signature DRULE =
    80 sig
    81   include BASIC_DRULE
    82   val triv_goal		: thm
    83   val rev_triv_goal	: thm
    84   val mk_triv_goal      : cterm -> thm
    85   val tvars_of_terms	: term list -> (indexname * sort) list
    86   val vars_of_terms	: term list -> (indexname * typ) list
    87   val tvars_of		: thm -> (indexname * sort) list
    88   val vars_of		: thm -> (indexname * typ) list
    89   val unvarifyT		: thm -> thm
    90   val unvarify		: thm -> thm
    91 end;
    92 
    93 structure Drule: DRULE =
    94 struct
    95 
    96 
    97 (** some cterm->cterm operations: much faster than calling cterm_of! **)
    98 
    99 (** SAME NAMES as in structure Logic: use compound identifiers! **)
   100 
   101 (*dest_implies for cterms. Note T=prop below*)
   102 fun dest_implies ct =
   103     case term_of ct of 
   104 	(Const("==>", _) $ _ $ _) => 
   105 	    let val (ct1,ct2) = dest_comb ct
   106 	    in  (#2 (dest_comb ct1), ct2)  end	     
   107       | _ => raise TERM ("dest_implies", [term_of ct]) ;
   108 
   109 
   110 (*Discard flexflex pairs; return a cterm*)
   111 fun skip_flexpairs ct =
   112     case term_of ct of
   113 	(Const("==>", _) $ (Const("=?=",_)$_$_) $ _) =>
   114 	    skip_flexpairs (#2 (dest_implies ct))
   115       | _ => ct;
   116 
   117 (* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
   118 fun strip_imp_prems ct =
   119     let val (cA,cB) = dest_implies ct
   120     in  cA :: strip_imp_prems cB  end
   121     handle TERM _ => [];
   122 
   123 (* A1==>...An==>B  goes to B, where B is not an implication *)
   124 fun strip_imp_concl ct =
   125     case term_of ct of (Const("==>", _) $ _ $ _) => 
   126 	strip_imp_concl (#2 (dest_comb ct))
   127   | _ => ct;
   128 
   129 (*The premises of a theorem, as a cterm list*)
   130 val cprems_of = strip_imp_prems o skip_flexpairs o cprop_of;
   131 
   132 
   133 (** reading of instantiations **)
   134 
   135 fun absent ixn =
   136   error("No such variable in term: " ^ Syntax.string_of_vname ixn);
   137 
   138 fun inst_failure ixn =
   139   error("Instantiation of " ^ Syntax.string_of_vname ixn ^ " fails");
   140 
   141 fun read_insts sign (rtypes,rsorts) (types,sorts) used insts =
   142 let val {tsig,...} = Sign.rep_sg sign
   143     fun split([],tvs,vs) = (tvs,vs)
   144       | split((sv,st)::l,tvs,vs) = (case Symbol.explode sv of
   145                   "'"::cs => split(l,(Syntax.indexname cs,st)::tvs,vs)
   146                 | cs => split(l,tvs,(Syntax.indexname cs,st)::vs));
   147     val (tvs,vs) = split(insts,[],[]);
   148     fun readT((a,i),st) =
   149         let val ixn = ("'" ^ a,i);
   150             val S = case rsorts ixn of Some S => S | None => absent ixn;
   151             val T = Sign.read_typ (sign,sorts) st;
   152         in if Type.typ_instance(tsig,T,TVar(ixn,S)) then (ixn,T)
   153            else inst_failure ixn
   154         end
   155     val tye = map readT tvs;
   156     fun mkty(ixn,st) = (case rtypes ixn of
   157                           Some T => (ixn,(st,typ_subst_TVars tye T))
   158                         | None => absent ixn);
   159     val ixnsTs = map mkty vs;
   160     val ixns = map fst ixnsTs
   161     and sTs  = map snd ixnsTs
   162     val (cts,tye2) = read_def_cterms(sign,types,sorts) used false sTs;
   163     fun mkcVar(ixn,T) =
   164         let val U = typ_subst_TVars tye2 T
   165         in cterm_of sign (Var(ixn,U)) end
   166     val ixnTs = ListPair.zip(ixns, map snd sTs)
   167 in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) (tye2 @ tye),
   168     ListPair.zip(map mkcVar ixnTs,cts))
   169 end;
   170 
   171 
   172 (*** Find the type (sort) associated with a (T)Var or (T)Free in a term
   173      Used for establishing default types (of variables) and sorts (of
   174      type variables) when reading another term.
   175      Index -1 indicates that a (T)Free rather than a (T)Var is wanted.
   176 ***)
   177 
   178 fun types_sorts thm =
   179     let val {prop,hyps,...} = rep_thm thm;
   180         val big = list_comb(prop,hyps); (* bogus term! *)
   181         val vars = map dest_Var (term_vars big);
   182         val frees = map dest_Free (term_frees big);
   183         val tvars = term_tvars big;
   184         val tfrees = term_tfrees big;
   185         fun typ(a,i) = if i<0 then assoc(frees,a) else assoc(vars,(a,i));
   186         fun sort(a,i) = if i<0 then assoc(tfrees,a) else assoc(tvars,(a,i));
   187     in (typ,sort) end;
   188 
   189 (** Standardization of rules **)
   190 
   191 (*Generalization over a list of variables, IGNORING bad ones*)
   192 fun forall_intr_list [] th = th
   193   | forall_intr_list (y::ys) th =
   194         let val gth = forall_intr_list ys th
   195         in  forall_intr y gth   handle THM _ =>  gth  end;
   196 
   197 (*Generalization over all suitable Free variables*)
   198 fun forall_intr_frees th =
   199     let val {prop,sign,...} = rep_thm th
   200     in  forall_intr_list
   201          (map (cterm_of sign) (sort (make_ord atless) (term_frees prop)))
   202          th
   203     end;
   204 
   205 (*Replace outermost quantified variable by Var of given index.
   206     Could clash with Vars already present.*)
   207 fun forall_elim_var i th =
   208     let val {prop,sign,...} = rep_thm th
   209     in case prop of
   210           Const("all",_) $ Abs(a,T,_) =>
   211               forall_elim (cterm_of sign (Var((a,i), T)))  th
   212         | _ => raise THM("forall_elim_var", i, [th])
   213     end;
   214 
   215 (*Repeat forall_elim_var until all outer quantifiers are removed*)
   216 fun forall_elim_vars i th =
   217     forall_elim_vars i (forall_elim_var i th)
   218         handle THM _ => th;
   219 
   220 (*Specialization over a list of cterms*)
   221 fun forall_elim_list cts th = foldr (uncurry forall_elim) (rev cts, th);
   222 
   223 (* maps [A1,...,An], B   to   [| A1;...;An |] ==> B  *)
   224 fun implies_intr_list cAs th = foldr (uncurry implies_intr) (cAs,th);
   225 
   226 (* maps [| A1;...;An |] ==> B and [A1,...,An]   to   B *)
   227 fun implies_elim_list impth ths = foldl (uncurry implies_elim) (impth,ths);
   228 
   229 (*Reset Var indexes to zero, renaming to preserve distinctness*)
   230 fun zero_var_indexes th =
   231     let val {prop,sign,...} = rep_thm th;
   232         val vars = term_vars prop
   233         val bs = foldl add_new_id ([], map (fn Var((a,_),_)=>a) vars)
   234         val inrs = add_term_tvars(prop,[]);
   235         val nms' = rev(foldl add_new_id ([], map (#1 o #1) inrs));
   236         val tye = ListPair.map (fn ((v,rs),a) => (v, TVar((a,0),rs)))
   237 	             (inrs, nms')
   238         val ctye = map (fn (v,T) => (v,ctyp_of sign T)) tye;
   239         fun varpairs([],[]) = []
   240           | varpairs((var as Var(v,T)) :: vars, b::bs) =
   241                 let val T' = typ_subst_TVars tye T
   242                 in (cterm_of sign (Var(v,T')),
   243                     cterm_of sign (Var((b,0),T'))) :: varpairs(vars,bs)
   244                 end
   245           | varpairs _ = raise TERM("varpairs", []);
   246     in instantiate (ctye, varpairs(vars,rev bs)) th end;
   247 
   248 
   249 (*Standard form of object-rule: no hypotheses, Frees, or outer quantifiers;
   250     all generality expressed by Vars having index 0.*)
   251 fun standard th =
   252   let val {maxidx,...} = rep_thm th
   253   in
   254     th |> implies_intr_hyps
   255        |> forall_intr_frees |> forall_elim_vars (maxidx + 1)
   256        |> Thm.strip_shyps |> Thm.implies_intr_shyps
   257        |> zero_var_indexes |> Thm.varifyT |> Thm.compress
   258   end;
   259 
   260 
   261 (*Convert all Vars in a theorem to Frees.  Also return a function for 
   262   reversing that operation.  DOES NOT WORK FOR TYPE VARIABLES.
   263   Similar code in type/freeze_thaw*)
   264 fun freeze_thaw th =
   265   let val fth = freezeT th
   266       val {prop,sign,...} = rep_thm fth
   267       val used = add_term_names (prop, [])
   268       and vars = term_vars prop
   269       fun newName (Var(ix,_), (pairs,used)) = 
   270 	    let val v = variant used (string_of_indexname ix)
   271 	    in  ((ix,v)::pairs, v::used)  end;
   272       val (alist, _) = foldr newName (vars, ([], used))
   273       fun mk_inst (Var(v,T)) = 
   274 	  (cterm_of sign (Var(v,T)),
   275 	   cterm_of sign (Free(the (assoc(alist,v)), T)))
   276       val insts = map mk_inst vars
   277       fun thaw th' = 
   278 	  th' |> forall_intr_list (map #2 insts)
   279 	      |> forall_elim_list (map #1 insts)
   280   in  (instantiate ([],insts) fth, thaw)  end;
   281 
   282 
   283 (*Rotates a rule's premises to the left by k.  Does nothing if k=0 or
   284   if k equals the number of premises.  Useful, for instance, with etac.
   285   Similar to tactic/defer_tac*)
   286 fun rotate_prems k rl = 
   287     let val (rl',thaw) = freeze_thaw rl
   288 	val hyps = strip_imp_prems (adjust_maxidx (cprop_of rl'))
   289 	val hyps' = List.drop(hyps, k)
   290     in  implies_elim_list rl' (map assume hyps)
   291         |> implies_intr_list (hyps' @ List.take(hyps, k))
   292         |> thaw |> varifyT
   293     end;
   294 
   295 
   296 (*Assume a new formula, read following the same conventions as axioms.
   297   Generalizes over Free variables,
   298   creates the assumption, and then strips quantifiers.
   299   Example is [| ALL x:?A. ?P(x) |] ==> [| ?P(?a) |]
   300              [ !(A,P,a)[| ALL x:A. P(x) |] ==> [| P(a) |] ]    *)
   301 fun assume_ax thy sP =
   302     let val sign = sign_of thy
   303         val prop = Logic.close_form (term_of (read_cterm sign (sP, propT)))
   304     in forall_elim_vars 0 (assume (cterm_of sign prop))  end;
   305 
   306 (*Resolution: exactly one resolvent must be produced.*)
   307 fun tha RSN (i,thb) =
   308   case Seq.chop (2, biresolution false [(false,tha)] i thb) of
   309       ([th],_) => th
   310     | ([],_)   => raise THM("RSN: no unifiers", i, [tha,thb])
   311     |      _   => raise THM("RSN: multiple unifiers", i, [tha,thb]);
   312 
   313 (*resolution: P==>Q, Q==>R gives P==>R. *)
   314 fun tha RS thb = tha RSN (1,thb);
   315 
   316 (*For joining lists of rules*)
   317 fun thas RLN (i,thbs) =
   318   let val resolve = biresolution false (map (pair false) thas) i
   319       fun resb thb = Seq.list_of (resolve thb) handle THM _ => []
   320   in  List.concat (map resb thbs)  end;
   321 
   322 fun thas RL thbs = thas RLN (1,thbs);
   323 
   324 (*Resolve a list of rules against bottom_rl from right to left;
   325   makes proof trees*)
   326 fun rls MRS bottom_rl =
   327   let fun rs_aux i [] = bottom_rl
   328         | rs_aux i (rl::rls) = rl RSN (i, rs_aux (i+1) rls)
   329   in  rs_aux 1 rls  end;
   330 
   331 (*As above, but for rule lists*)
   332 fun rlss MRL bottom_rls =
   333   let fun rs_aux i [] = bottom_rls
   334         | rs_aux i (rls::rlss) = rls RLN (i, rs_aux (i+1) rlss)
   335   in  rs_aux 1 rlss  end;
   336 
   337 (*compose Q and [...,Qi,Q(i+1),...]==>R to [...,Q(i+1),...]==>R
   338   with no lifting or renaming!  Q may contain ==> or meta-quants
   339   ALWAYS deletes premise i *)
   340 fun compose(tha,i,thb) =
   341     Seq.list_of (bicompose false (false,tha,0) i thb);
   342 
   343 (*compose Q and [Q1,Q2,...,Qk]==>R to [Q2,...,Qk]==>R getting unique result*)
   344 fun tha COMP thb =
   345     case compose(tha,1,thb) of
   346         [th] => th
   347       | _ =>   raise THM("COMP", 1, [tha,thb]);
   348 
   349 (*Instantiate theorem th, reading instantiations under signature sg*)
   350 fun read_instantiate_sg sg sinsts th =
   351     let val ts = types_sorts th;
   352         val used = add_term_tvarnames(#prop(rep_thm th),[]);
   353     in  instantiate (read_insts sg ts ts used sinsts) th  end;
   354 
   355 (*Instantiate theorem th, reading instantiations under theory of th*)
   356 fun read_instantiate sinsts th =
   357     read_instantiate_sg (#sign (rep_thm th)) sinsts th;
   358 
   359 
   360 (*Left-to-right replacements: tpairs = [...,(vi,ti),...].
   361   Instantiates distinct Vars by terms, inferring type instantiations. *)
   362 local
   363   fun add_types ((ct,cu), (sign,tye,maxidx)) =
   364     let val {sign=signt, t=t, T= T, maxidx=maxt,...} = rep_cterm ct
   365         and {sign=signu, t=u, T= U, maxidx=maxu,...} = rep_cterm cu;
   366         val maxi = Int.max(maxidx, Int.max(maxt, maxu));
   367         val sign' = Sign.merge(sign, Sign.merge(signt, signu))
   368         val (tye',maxi') = Type.unify (#tsig(Sign.rep_sg sign')) maxi tye (T,U)
   369           handle Type.TUNIFY => raise TYPE("add_types", [T,U], [t,u])
   370     in  (sign', tye', maxi')  end;
   371 in
   372 fun cterm_instantiate ctpairs0 th =
   373   let val (sign,tye,_) = foldr add_types (ctpairs0, (#sign(rep_thm th),[],0))
   374       val tsig = #tsig(Sign.rep_sg sign);
   375       fun instT(ct,cu) = let val inst = subst_TVars tye
   376                          in (cterm_fun inst ct, cterm_fun inst cu) end
   377       fun ctyp2 (ix,T) = (ix, ctyp_of sign T)
   378   in  instantiate (map ctyp2 tye, map instT ctpairs0) th  end
   379   handle TERM _ =>
   380            raise THM("cterm_instantiate: incompatible signatures",0,[th])
   381        | TYPE (msg, _, _) => raise THM("cterm_instantiate: " ^ msg, 0, [th])
   382 end;
   383 
   384 
   385 (** theorem equality **)
   386 
   387 (*Do the two theorems have the same signature?*)
   388 fun eq_thm_sg (th1,th2) = Sign.eq_sg(#sign(rep_thm th1), #sign(rep_thm th2));
   389 
   390 (*Useful "distance" function for BEST_FIRST*)
   391 val size_of_thm = size_of_term o #prop o rep_thm;
   392 
   393 
   394 (** Mark Staples's weaker version of eq_thm: ignores variable renaming and
   395     (some) type variable renaming **)
   396 
   397  (* Can't use term_vars, because it sorts the resulting list of variable names.
   398     We instead need the unique list noramlised by the order of appearance
   399     in the term. *)
   400 fun term_vars' (t as Var(v,T)) = [t]
   401   | term_vars' (Abs(_,_,b)) = term_vars' b
   402   | term_vars' (f$a) = (term_vars' f) @ (term_vars' a)
   403   | term_vars' _ = [];
   404 
   405 fun forall_intr_vars th =
   406   let val {prop,sign,...} = rep_thm th;
   407       val vars = distinct (term_vars' prop);
   408   in forall_intr_list (map (cterm_of sign) vars) th end;
   409 
   410 fun weak_eq_thm (tha,thb) =
   411     eq_thm(forall_intr_vars (freezeT tha), forall_intr_vars (freezeT thb));
   412 
   413 
   414 
   415 (*** Meta-Rewriting Rules ***)
   416 
   417 val proto_sign = sign_of ProtoPure.thy;
   418 
   419 fun read_prop s = read_cterm proto_sign (s, propT);
   420 
   421 fun store_thm name thm = PureThy.smart_store_thm (name, standard thm);
   422 
   423 val reflexive_thm =
   424   let val cx = cterm_of proto_sign (Var(("x",0),TVar(("'a",0),logicS)))
   425   in store_thm "reflexive" (Thm.reflexive cx) end;
   426 
   427 val symmetric_thm =
   428   let val xy = read_prop "x::'a::logic == y"
   429   in store_thm "symmetric" 
   430       (Thm.implies_intr_hyps(Thm.symmetric(Thm.assume xy)))
   431    end;
   432 
   433 val transitive_thm =
   434   let val xy = read_prop "x::'a::logic == y"
   435       val yz = read_prop "y::'a::logic == z"
   436       val xythm = Thm.assume xy and yzthm = Thm.assume yz
   437   in store_thm "transitive" (Thm.implies_intr yz (Thm.transitive xythm yzthm))
   438   end;
   439 
   440 fun symmetric_fun thm = thm RS symmetric_thm;
   441 
   442 (** Below, a "conversion" has type cterm -> thm **)
   443 
   444 val refl_implies = reflexive (cterm_of proto_sign implies);
   445 
   446 (*In [A1,...,An]==>B, rewrite the selected A's only -- for rewrite_goals_tac*)
   447 (*Do not rewrite flex-flex pairs*)
   448 fun goals_conv pred cv =
   449   let fun gconv i ct =
   450         let val (A,B) = dest_implies ct
   451             val (thA,j) = case term_of A of
   452                   Const("=?=",_)$_$_ => (reflexive A, i)
   453                 | _ => (if pred i then cv A else reflexive A, i+1)
   454         in  combination (combination refl_implies thA) (gconv j B) end
   455         handle TERM _ => reflexive ct
   456   in gconv 1 end;
   457 
   458 (*Use a conversion to transform a theorem*)
   459 fun fconv_rule cv th = equal_elim (cv (cprop_of th)) th;
   460 
   461 (*rewriting conversion*)
   462 fun rew_conv mode prover mss = rewrite_cterm mode mss prover;
   463 
   464 (*Rewrite a theorem*)
   465 fun rewrite_rule_aux _ []   th = th
   466   | rewrite_rule_aux prover thms th =
   467       fconv_rule (rew_conv (true,false,false) prover (Thm.mss_of thms)) th;
   468 
   469 fun rewrite_thm mode prover mss = fconv_rule (rew_conv mode prover mss);
   470 fun rewrite_cterm mode prover mss = Thm.rewrite_cterm mode mss prover;
   471 
   472 (*Rewrite the subgoals of a proof state (represented by a theorem) *)
   473 fun rewrite_goals_rule_aux _ []   th = th
   474   | rewrite_goals_rule_aux prover thms th =
   475       fconv_rule (goals_conv (K true) (rew_conv (true, true, false) prover
   476         (Thm.mss_of thms))) th;
   477 
   478 (*Rewrite the subgoal of a proof state (represented by a theorem) *)
   479 fun rewrite_goal_rule mode prover mss i thm =
   480   if 0 < i  andalso  i <= nprems_of thm
   481   then fconv_rule (goals_conv (fn j => j=i) (rew_conv mode prover mss)) thm
   482   else raise THM("rewrite_goal_rule",i,[thm]);
   483 
   484 
   485 (** Derived rules mainly for METAHYPS **)
   486 
   487 (*Given the term "a", takes (%x.t)==(%x.u) to t[a/x]==u[a/x]*)
   488 fun equal_abs_elim ca eqth =
   489   let val {sign=signa, t=a, ...} = rep_cterm ca
   490       and combth = combination eqth (reflexive ca)
   491       val {sign,prop,...} = rep_thm eqth
   492       val (abst,absu) = Logic.dest_equals prop
   493       val cterm = cterm_of (Sign.merge (sign,signa))
   494   in  transitive (symmetric (beta_conversion (cterm (abst$a))))
   495            (transitive combth (beta_conversion (cterm (absu$a))))
   496   end
   497   handle THM _ => raise THM("equal_abs_elim", 0, [eqth]);
   498 
   499 (*Calling equal_abs_elim with multiple terms*)
   500 fun equal_abs_elim_list cts th = foldr (uncurry equal_abs_elim) (rev cts, th);
   501 
   502 local
   503   val alpha = TVar(("'a",0), [])     (*  type ?'a::{}  *)
   504   fun err th = raise THM("flexpair_inst: ", 0, [th])
   505   fun flexpair_inst def th =
   506     let val {prop = Const _ $ t $ u,  sign,...} = rep_thm th
   507         val cterm = cterm_of sign
   508         fun cvar a = cterm(Var((a,0),alpha))
   509         val def' = cterm_instantiate [(cvar"t", cterm t), (cvar"u", cterm u)]
   510                    def
   511     in  equal_elim def' th
   512     end
   513     handle THM _ => err th | bind => err th
   514 in
   515 val flexpair_intr = flexpair_inst (symmetric ProtoPure.flexpair_def)
   516 and flexpair_elim = flexpair_inst ProtoPure.flexpair_def
   517 end;
   518 
   519 (*Version for flexflex pairs -- this supports lifting.*)
   520 fun flexpair_abs_elim_list cts =
   521     flexpair_intr o equal_abs_elim_list cts o flexpair_elim;
   522 
   523 
   524 (*** Some useful meta-theorems ***)
   525 
   526 (*The rule V/V, obtains assumption solving for eresolve_tac*)
   527 val asm_rl =
   528   store_thm "asm_rl" (trivial(read_prop "PROP ?psi"));
   529 
   530 (*Meta-level cut rule: [| V==>W; V |] ==> W *)
   531 val cut_rl =
   532   store_thm "cut_rl"
   533     (trivial(read_prop "PROP ?psi ==> PROP ?theta"));
   534 
   535 (*Generalized elim rule for one conclusion; cut_rl with reversed premises:
   536      [| PROP V;  PROP V ==> PROP W |] ==> PROP W *)
   537 val revcut_rl =
   538   let val V = read_prop "PROP V"
   539       and VW = read_prop "PROP V ==> PROP W";
   540   in
   541     store_thm "revcut_rl"
   542       (implies_intr V (implies_intr VW (implies_elim (assume VW) (assume V))))
   543   end;
   544 
   545 (*for deleting an unwanted assumption*)
   546 val thin_rl =
   547   let val V = read_prop "PROP V"
   548       and W = read_prop "PROP W";
   549   in  store_thm "thin_rl" (implies_intr V (implies_intr W (assume W)))
   550   end;
   551 
   552 (* (!!x. PROP ?V) == PROP ?V       Allows removal of redundant parameters*)
   553 val triv_forall_equality =
   554   let val V  = read_prop "PROP V"
   555       and QV = read_prop "!!x::'a. PROP V"
   556       and x  = read_cterm proto_sign ("x", TFree("'a",logicS));
   557   in
   558     store_thm "triv_forall_equality"
   559       (equal_intr (implies_intr QV (forall_elim x (assume QV)))
   560         (implies_intr V  (forall_intr x (assume V))))
   561   end;
   562 
   563 (* (PROP ?PhiA ==> PROP ?PhiB ==> PROP ?Psi) ==>
   564    (PROP ?PhiB ==> PROP ?PhiA ==> PROP ?Psi)
   565    `thm COMP swap_prems_rl' swaps the first two premises of `thm'
   566 *)
   567 val swap_prems_rl =
   568   let val cmajor = read_prop "PROP PhiA ==> PROP PhiB ==> PROP Psi";
   569       val major = assume cmajor;
   570       val cminor1 = read_prop "PROP PhiA";
   571       val minor1 = assume cminor1;
   572       val cminor2 = read_prop "PROP PhiB";
   573       val minor2 = assume cminor2;
   574   in store_thm "swap_prems_rl"
   575        (implies_intr cmajor (implies_intr cminor2 (implies_intr cminor1
   576          (implies_elim (implies_elim major minor1) minor2))))
   577   end;
   578 
   579 (* [| PROP ?phi ==> PROP ?psi; PROP ?psi ==> PROP ?phi |]
   580    ==> PROP ?phi == PROP ?psi
   581    Introduction rule for == as a meta-theorem.  
   582 *)
   583 val equal_intr_rule =
   584   let val PQ = read_prop "PROP phi ==> PROP psi"
   585       and QP = read_prop "PROP psi ==> PROP phi"
   586   in
   587     store_thm "equal_intr_rule"
   588       (implies_intr PQ (implies_intr QP (equal_intr (assume PQ) (assume QP))))
   589   end;
   590 
   591 
   592 (* GOAL (PROP A) <==> PROP A *)
   593 
   594 local
   595   val A = read_prop "PROP A";
   596   val G = read_prop "GOAL (PROP A)";
   597   val (G_def, _) = freeze_thaw ProtoPure.Goal_def;
   598 in
   599   val triv_goal = store_thm "triv_goal" (Thm.equal_elim (Thm.symmetric G_def) (Thm.assume A));
   600   val rev_triv_goal = store_thm "rev_triv_goal" (Thm.equal_elim G_def (Thm.assume G));
   601 end;
   602 
   603 
   604 
   605 (** variations on instantiate **)
   606 
   607 (* collect vars *)
   608 
   609 val add_tvarsT = foldl_atyps (fn (vs, TVar v) => v ins vs | (vs, _) => vs);
   610 val add_tvars = foldl_types add_tvarsT;
   611 val add_vars = foldl_aterms (fn (vs, Var v) => v ins vs | (vs, _) => vs);
   612 
   613 fun tvars_of_terms ts = rev (foldl add_tvars ([], ts));
   614 fun vars_of_terms ts = rev (foldl add_vars ([], ts));
   615 
   616 fun tvars_of thm = tvars_of_terms [#prop (Thm.rep_thm thm)];
   617 fun vars_of thm = vars_of_terms [#prop (Thm.rep_thm thm)];
   618 
   619 
   620 (* instantiate by left-to-right occurrence of variables *)
   621 
   622 fun instantiate' cTs cts thm =
   623   let
   624     fun err msg =
   625       raise TYPE ("instantiate': " ^ msg,
   626         mapfilter (apsome Thm.typ_of) cTs,
   627         mapfilter (apsome Thm.term_of) cts);
   628 
   629     fun inst_of (v, ct) =
   630       (Thm.cterm_of (#sign (Thm.rep_cterm ct)) (Var v), ct)
   631         handle TYPE (msg, _, _) => err msg;
   632 
   633     fun zip_vars _ [] = []
   634       | zip_vars (_ :: vs) (None :: opt_ts) = zip_vars vs opt_ts
   635       | zip_vars (v :: vs) (Some t :: opt_ts) = (v, t) :: zip_vars vs opt_ts
   636       | zip_vars [] _ = err "more instantiations than variables in thm";
   637 
   638     (*instantiate types first!*)
   639     val thm' =
   640       if forall is_none cTs then thm
   641       else Thm.instantiate (zip_vars (map fst (tvars_of thm)) cTs, []) thm;
   642     in
   643       if forall is_none cts then thm'
   644       else Thm.instantiate ([], map inst_of (zip_vars (vars_of thm') cts)) thm'
   645     end;
   646 
   647 
   648 (* unvarify(T) *)
   649 
   650 (*assume thm in standard form, i.e. no frees, 0 var indexes*)
   651 
   652 fun unvarifyT thm =
   653   let
   654     val cT = Thm.ctyp_of (Thm.sign_of_thm thm);
   655     val tfrees = map (fn ((x, _), S) => Some (cT (TFree (x, S)))) (tvars_of thm);
   656   in instantiate' tfrees [] thm end;
   657 
   658 fun unvarify raw_thm =
   659   let
   660     val thm = unvarifyT raw_thm;
   661     val ct = Thm.cterm_of (Thm.sign_of_thm thm);
   662     val frees = map (fn ((x, _), T) => Some (ct (Free (x, T)))) (vars_of thm);
   663   in instantiate' [] frees thm end;
   664 
   665 
   666 (* mk_triv_goal *)
   667 
   668 (*make an initial proof state, "PROP A ==> (PROP A)" *)
   669 fun mk_triv_goal ct = instantiate' [] [Some ct] triv_goal;
   670 
   671 
   672 end;
   673 
   674 
   675 structure BasicDrule: BASIC_DRULE = Drule;
   676 open BasicDrule;