src/Pure/drule.ML
author wenzelm
Thu May 19 16:17:46 1994 +0200 (1994-05-19)
changeset 385 921f87897a76
parent 252 7532f95d7f44
child 400 3c2c40c87112
permissions -rw-r--r--
added print_sign, print_axioms: theory -> unit;
replaced ["logic"] by logicS;
     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 and theories
     7 *)
     8 
     9 infix 0 RS RSN RL RLN MRS MRL COMP;
    10 
    11 signature DRULE =
    12   sig
    13   structure Thm : THM
    14   local open Thm  in
    15   val asm_rl: thm
    16   val assume_ax: theory -> string -> thm
    17   val COMP: thm * thm -> thm
    18   val compose: thm * int * thm -> thm list
    19   val cterm_instantiate: (cterm*cterm)list -> thm -> thm
    20   val cut_rl: thm
    21   val equal_abs_elim: cterm  -> thm -> thm
    22   val equal_abs_elim_list: cterm list -> thm -> thm
    23   val eq_thm: thm * thm -> bool
    24   val eq_thm_sg: thm * thm -> bool
    25   val flexpair_abs_elim_list: cterm list -> thm -> thm
    26   val forall_intr_list: cterm list -> thm -> thm
    27   val forall_intr_frees: thm -> thm
    28   val forall_elim_list: cterm list -> thm -> thm
    29   val forall_elim_var: int -> thm -> thm
    30   val forall_elim_vars: int -> thm -> thm
    31   val implies_elim_list: thm -> thm list -> thm
    32   val implies_intr_list: cterm list -> thm -> thm
    33   val MRL: thm list list * thm list -> thm list
    34   val MRS: thm list * thm -> thm
    35   val pprint_cterm: cterm -> pprint_args -> unit
    36   val pprint_ctyp: ctyp -> pprint_args -> unit
    37   val pprint_theory: theory -> pprint_args -> unit
    38   val pprint_thm: thm -> pprint_args -> unit
    39   val pretty_thm: thm -> Sign.Syntax.Pretty.T
    40   val print_cterm: cterm -> unit
    41   val print_ctyp: ctyp -> unit
    42   val print_goals: int -> thm -> unit
    43   val print_goals_ref: (int -> thm -> unit) ref
    44   val print_sign: theory -> unit
    45   val print_axioms: theory -> unit
    46   val print_theory: theory -> unit
    47   val print_thm: thm -> unit
    48   val prth: thm -> thm
    49   val prthq: thm Sequence.seq -> thm Sequence.seq
    50   val prths: thm list -> thm list
    51   val read_instantiate: (string*string)list -> thm -> thm
    52   val read_instantiate_sg: Sign.sg -> (string*string)list -> thm -> thm
    53   val read_insts:
    54           Sign.sg -> (indexname -> typ option) * (indexname -> sort option)
    55                   -> (indexname -> typ option) * (indexname -> sort option)
    56                   -> (string*string)list
    57                   -> (indexname*ctyp)list * (cterm*cterm)list
    58   val reflexive_thm: thm
    59   val revcut_rl: thm
    60   val rewrite_goal_rule: bool*bool -> (meta_simpset -> thm -> thm option)
    61         -> meta_simpset -> int -> thm -> thm
    62   val rewrite_goals_rule: thm list -> thm -> thm
    63   val rewrite_rule: thm list -> thm -> thm
    64   val RS: thm * thm -> thm
    65   val RSN: thm * (int * thm) -> thm
    66   val RL: thm list * thm list -> thm list
    67   val RLN: thm list * (int * thm list) -> thm list
    68   val show_hyps: bool ref
    69   val size_of_thm: thm -> int
    70   val standard: thm -> thm
    71   val string_of_cterm: cterm -> string
    72   val string_of_ctyp: ctyp -> string
    73   val string_of_thm: thm -> string
    74   val symmetric_thm: thm
    75   val transitive_thm: thm
    76   val triv_forall_equality: thm
    77   val types_sorts: thm -> (indexname-> typ option) * (indexname-> sort option)
    78   val zero_var_indexes: thm -> thm
    79   end
    80   end;
    81 
    82 functor DruleFun (structure Logic: LOGIC and Thm: THM): DRULE =
    83 struct
    84 structure Thm = Thm;
    85 structure Sign = Thm.Sign;
    86 structure Type = Sign.Type;
    87 structure Pretty = Sign.Syntax.Pretty
    88 local open Thm
    89 in
    90 
    91 (**** More derived rules and operations on theorems ****)
    92 
    93 (** reading of instantiations **)
    94 
    95 fun indexname cs = case Syntax.scan_varname cs of (v,[]) => v
    96         | _ => error("Lexical error in variable name " ^ quote (implode cs));
    97 
    98 fun absent ixn =
    99   error("No such variable in term: " ^ Syntax.string_of_vname ixn);
   100 
   101 fun inst_failure ixn =
   102   error("Instantiation of " ^ Syntax.string_of_vname ixn ^ " fails");
   103 
   104 fun read_insts sign (rtypes,rsorts) (types,sorts) insts =
   105 let val {tsig,...} = Sign.rep_sg sign
   106     fun split([],tvs,vs) = (tvs,vs)
   107       | split((sv,st)::l,tvs,vs) = (case explode sv of
   108                   "'"::cs => split(l,(indexname cs,st)::tvs,vs)
   109                 | cs => split(l,tvs,(indexname cs,st)::vs));
   110     val (tvs,vs) = split(insts,[],[]);
   111     fun readT((a,i),st) =
   112         let val ixn = ("'" ^ a,i);
   113             val S = case rsorts ixn of Some S => S | None => absent ixn;
   114             val T = Sign.read_typ (sign,sorts) st;
   115         in if Type.typ_instance(tsig,T,TVar(ixn,S)) then (ixn,T)
   116            else inst_failure ixn
   117         end
   118     val tye = map readT tvs;
   119     fun add_cterm ((cts,tye), (ixn,st)) =
   120         let val T = case rtypes ixn of
   121                       Some T => typ_subst_TVars tye T
   122                     | None => absent ixn;
   123             val (ct,tye2) = read_def_cterm (sign,types,sorts) (st,T);
   124             val cv = cterm_of sign (Var(ixn,typ_subst_TVars tye2 T))
   125         in ((cv,ct)::cts,tye2 @ tye) end
   126     val (cterms,tye') = foldl add_cterm (([],tye), vs);
   127 in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) tye', cterms) end;
   128 
   129 
   130 
   131 (*** Printing of theories, theorems, etc. ***)
   132 
   133 (*If false, hypotheses are printed as dots*)
   134 val show_hyps = ref true;
   135 
   136 fun pretty_thm th =
   137 let val {sign, hyps, prop,...} = rep_thm th
   138     val hsymbs = if null hyps then []
   139                  else if !show_hyps then
   140                       [Pretty.brk 2,
   141                        Pretty.lst("[","]") (map (Sign.pretty_term sign) hyps)]
   142                  else Pretty.str" [" :: map (fn _ => Pretty.str".") hyps @
   143                       [Pretty.str"]"];
   144 in Pretty.blk(0, Sign.pretty_term sign prop :: hsymbs) end;
   145 
   146 val string_of_thm = Pretty.string_of o pretty_thm;
   147 
   148 val pprint_thm = Pretty.pprint o Pretty.quote o pretty_thm;
   149 
   150 
   151 (** Top-level commands for printing theorems **)
   152 val print_thm = writeln o string_of_thm;
   153 
   154 fun prth th = (print_thm th; th);
   155 
   156 (*Print and return a sequence of theorems, separated by blank lines. *)
   157 fun prthq thseq =
   158   (Sequence.prints (fn _ => print_thm) 100000 thseq; thseq);
   159 
   160 (*Print and return a list of theorems, separated by blank lines. *)
   161 fun prths ths = (print_list_ln print_thm ths; ths);
   162 
   163 
   164 (* other printing commands *)
   165 
   166 fun pprint_ctyp cT =
   167   let val {sign, T} = rep_ctyp cT in Sign.pprint_typ sign T end;
   168 
   169 fun string_of_ctyp cT =
   170   let val {sign, T} = rep_ctyp cT in Sign.string_of_typ sign T end;
   171 
   172 val print_ctyp = writeln o string_of_ctyp;
   173 
   174 fun pprint_cterm ct =
   175   let val {sign, t, ...} = rep_cterm ct in Sign.pprint_term sign t end;
   176 
   177 fun string_of_cterm ct =
   178   let val {sign, t, ...} = rep_cterm ct in Sign.string_of_term sign t end;
   179 
   180 val print_cterm = writeln o string_of_cterm;
   181 
   182 
   183 (* print theory *)
   184 
   185 val pprint_theory = Sign.pprint_sg o sign_of;
   186 
   187 val print_sign = Sign.print_sg o sign_of;
   188 
   189 fun print_axioms thy =
   190   let
   191     val {sign, ext_axtab, ...} = rep_theory thy;
   192     val axioms = Symtab.dest ext_axtab;
   193 
   194     fun prt_axm (a, t) = Pretty.block [Pretty.str (a ^ ":"), Pretty.brk 1,
   195       Pretty.quote (Sign.pretty_term sign t)];
   196   in
   197     Pretty.writeln (Pretty.big_list "additional axioms:" (map prt_axm axioms))
   198   end;
   199 
   200 fun print_theory thy = (print_sign thy; print_axioms thy);
   201 
   202 
   203 
   204 (** Print thm A1,...,An/B in "goal style" -- premises as numbered subgoals **)
   205 
   206 fun prettyprints es = writeln(Pretty.string_of(Pretty.blk(0,es)));
   207 
   208 fun print_goals maxgoals th : unit =
   209 let val {sign, hyps, prop,...} = rep_thm th;
   210     fun printgoals (_, []) = ()
   211       | printgoals (n, A::As) =
   212         let val prettyn = Pretty.str(" " ^ string_of_int n ^ ". ");
   213             val prettyA = Sign.pretty_term sign A
   214         in prettyprints[prettyn,prettyA];
   215            printgoals (n+1,As)
   216         end;
   217     fun prettypair(t,u) =
   218         Pretty.blk(0, [Sign.pretty_term sign t, Pretty.str" =?=", Pretty.brk 1,
   219                        Sign.pretty_term sign u]);
   220     fun printff [] = ()
   221       | printff tpairs =
   222          writeln("\nFlex-flex pairs:\n" ^
   223                  Pretty.string_of(Pretty.lst("","") (map prettypair tpairs)))
   224     val (tpairs,As,B) = Logic.strip_horn(prop);
   225     val ngoals = length As
   226 in
   227    writeln (Sign.string_of_term sign B);
   228    if ngoals=0 then writeln"No subgoals!"
   229    else if ngoals>maxgoals
   230         then (printgoals (1, take(maxgoals,As));
   231               writeln("A total of " ^ string_of_int ngoals ^ " subgoals..."))
   232         else printgoals (1, As);
   233    printff tpairs
   234 end;
   235 
   236 (*"hook" for user interfaces: allows print_goals to be replaced*)
   237 val print_goals_ref = ref print_goals;
   238 
   239 (*** Find the type (sort) associated with a (T)Var or (T)Free in a term
   240      Used for establishing default types (of variables) and sorts (of
   241      type variables) when reading another term.
   242      Index -1 indicates that a (T)Free rather than a (T)Var is wanted.
   243 ***)
   244 
   245 fun types_sorts thm =
   246     let val {prop,hyps,...} = rep_thm thm;
   247         val big = list_comb(prop,hyps); (* bogus term! *)
   248         val vars = map dest_Var (term_vars big);
   249         val frees = map dest_Free (term_frees big);
   250         val tvars = term_tvars big;
   251         val tfrees = term_tfrees big;
   252         fun typ(a,i) = if i<0 then assoc(frees,a) else assoc(vars,(a,i));
   253         fun sort(a,i) = if i<0 then assoc(tfrees,a) else assoc(tvars,(a,i));
   254     in (typ,sort) end;
   255 
   256 (** Standardization of rules **)
   257 
   258 (*Generalization over a list of variables, IGNORING bad ones*)
   259 fun forall_intr_list [] th = th
   260   | forall_intr_list (y::ys) th =
   261         let val gth = forall_intr_list ys th
   262         in  forall_intr y gth   handle THM _ =>  gth  end;
   263 
   264 (*Generalization over all suitable Free variables*)
   265 fun forall_intr_frees th =
   266     let val {prop,sign,...} = rep_thm th
   267     in  forall_intr_list
   268          (map (cterm_of sign) (sort atless (term_frees prop)))
   269          th
   270     end;
   271 
   272 (*Replace outermost quantified variable by Var of given index.
   273     Could clash with Vars already present.*)
   274 fun forall_elim_var i th =
   275     let val {prop,sign,...} = rep_thm th
   276     in case prop of
   277           Const("all",_) $ Abs(a,T,_) =>
   278               forall_elim (cterm_of sign (Var((a,i), T)))  th
   279         | _ => raise THM("forall_elim_var", i, [th])
   280     end;
   281 
   282 (*Repeat forall_elim_var until all outer quantifiers are removed*)
   283 fun forall_elim_vars i th =
   284     forall_elim_vars i (forall_elim_var i th)
   285         handle THM _ => th;
   286 
   287 (*Specialization over a list of cterms*)
   288 fun forall_elim_list cts th = foldr (uncurry forall_elim) (rev cts, th);
   289 
   290 (* maps [A1,...,An], B   to   [| A1;...;An |] ==> B  *)
   291 fun implies_intr_list cAs th = foldr (uncurry implies_intr) (cAs,th);
   292 
   293 (* maps [| A1;...;An |] ==> B and [A1,...,An]   to   B *)
   294 fun implies_elim_list impth ths = foldl (uncurry implies_elim) (impth,ths);
   295 
   296 (*Reset Var indexes to zero, renaming to preserve distinctness*)
   297 fun zero_var_indexes th =
   298     let val {prop,sign,...} = rep_thm th;
   299         val vars = term_vars prop
   300         val bs = foldl add_new_id ([], map (fn Var((a,_),_)=>a) vars)
   301         val inrs = add_term_tvars(prop,[]);
   302         val nms' = rev(foldl add_new_id ([], map (#1 o #1) inrs));
   303         val tye = map (fn ((v,rs),a) => (v, TVar((a,0),rs))) (inrs ~~ nms')
   304         val ctye = map (fn (v,T) => (v,ctyp_of sign T)) tye;
   305         fun varpairs([],[]) = []
   306           | varpairs((var as Var(v,T)) :: vars, b::bs) =
   307                 let val T' = typ_subst_TVars tye T
   308                 in (cterm_of sign (Var(v,T')),
   309                     cterm_of sign (Var((b,0),T'))) :: varpairs(vars,bs)
   310                 end
   311           | varpairs _ = raise TERM("varpairs", []);
   312     in instantiate (ctye, varpairs(vars,rev bs)) th end;
   313 
   314 
   315 (*Standard form of object-rule: no hypotheses, Frees, or outer quantifiers;
   316     all generality expressed by Vars having index 0.*)
   317 fun standard th =
   318     let val {maxidx,...} = rep_thm th
   319     in  varifyT (zero_var_indexes (forall_elim_vars(maxidx+1)
   320                          (forall_intr_frees(implies_intr_hyps th))))
   321     end;
   322 
   323 (*Assume a new formula, read following the same conventions as axioms.
   324   Generalizes over Free variables,
   325   creates the assumption, and then strips quantifiers.
   326   Example is [| ALL x:?A. ?P(x) |] ==> [| ?P(?a) |]
   327              [ !(A,P,a)[| ALL x:A. P(x) |] ==> [| P(a) |] ]    *)
   328 fun assume_ax thy sP =
   329     let val sign = sign_of thy
   330         val prop = Logic.close_form (term_of (read_cterm sign
   331                          (sP, propT)))
   332     in forall_elim_vars 0 (assume (cterm_of sign prop))  end;
   333 
   334 (*Resolution: exactly one resolvent must be produced.*)
   335 fun tha RSN (i,thb) =
   336   case Sequence.chop (2, biresolution false [(false,tha)] i thb) of
   337       ([th],_) => th
   338     | ([],_)   => raise THM("RSN: no unifiers", i, [tha,thb])
   339     |      _   => raise THM("RSN: multiple unifiers", i, [tha,thb]);
   340 
   341 (*resolution: P==>Q, Q==>R gives P==>R. *)
   342 fun tha RS thb = tha RSN (1,thb);
   343 
   344 (*For joining lists of rules*)
   345 fun thas RLN (i,thbs) =
   346   let val resolve = biresolution false (map (pair false) thas) i
   347       fun resb thb = Sequence.list_of_s (resolve thb) handle THM _ => []
   348   in  flat (map resb thbs)  end;
   349 
   350 fun thas RL thbs = thas RLN (1,thbs);
   351 
   352 (*Resolve a list of rules against bottom_rl from right to left;
   353   makes proof trees*)
   354 fun rls MRS bottom_rl =
   355   let fun rs_aux i [] = bottom_rl
   356         | rs_aux i (rl::rls) = rl RSN (i, rs_aux (i+1) rls)
   357   in  rs_aux 1 rls  end;
   358 
   359 (*As above, but for rule lists*)
   360 fun rlss MRL bottom_rls =
   361   let fun rs_aux i [] = bottom_rls
   362         | rs_aux i (rls::rlss) = rls RLN (i, rs_aux (i+1) rlss)
   363   in  rs_aux 1 rlss  end;
   364 
   365 (*compose Q and [...,Qi,Q(i+1),...]==>R to [...,Q(i+1),...]==>R
   366   with no lifting or renaming!  Q may contain ==> or meta-quants
   367   ALWAYS deletes premise i *)
   368 fun compose(tha,i,thb) =
   369     Sequence.list_of_s (bicompose false (false,tha,0) i thb);
   370 
   371 (*compose Q and [Q1,Q2,...,Qk]==>R to [Q2,...,Qk]==>R getting unique result*)
   372 fun tha COMP thb =
   373     case compose(tha,1,thb) of
   374         [th] => th
   375       | _ =>   raise THM("COMP", 1, [tha,thb]);
   376 
   377 (*Instantiate theorem th, reading instantiations under signature sg*)
   378 fun read_instantiate_sg sg sinsts th =
   379     let val ts = types_sorts th;
   380     in  instantiate (read_insts sg ts ts sinsts) th  end;
   381 
   382 (*Instantiate theorem th, reading instantiations under theory of th*)
   383 fun read_instantiate sinsts th =
   384     read_instantiate_sg (#sign (rep_thm th)) sinsts th;
   385 
   386 
   387 (*Left-to-right replacements: tpairs = [...,(vi,ti),...].
   388   Instantiates distinct Vars by terms, inferring type instantiations. *)
   389 local
   390   fun add_types ((ct,cu), (sign,tye)) =
   391     let val {sign=signt, t=t, T= T, ...} = rep_cterm ct
   392         and {sign=signu, t=u, T= U, ...} = rep_cterm cu
   393         val sign' = Sign.merge(sign, Sign.merge(signt, signu))
   394         val tye' = Type.unify (#tsig(Sign.rep_sg sign')) ((T,U), tye)
   395           handle Type.TUNIFY => raise TYPE("add_types", [T,U], [t,u])
   396     in  (sign', tye')  end;
   397 in
   398 fun cterm_instantiate ctpairs0 th =
   399   let val (sign,tye) = foldr add_types (ctpairs0, (#sign(rep_thm th),[]))
   400       val tsig = #tsig(Sign.rep_sg sign);
   401       fun instT(ct,cu) = let val inst = subst_TVars tye
   402                          in (cterm_fun inst ct, cterm_fun inst cu) end
   403       fun ctyp2 (ix,T) = (ix, ctyp_of sign T)
   404   in  instantiate (map ctyp2 tye, map instT ctpairs0) th  end
   405   handle TERM _ =>
   406            raise THM("cterm_instantiate: incompatible signatures",0,[th])
   407        | TYPE _ => raise THM("cterm_instantiate: types", 0, [th])
   408 end;
   409 
   410 
   411 (** theorem equality test is exported and used by BEST_FIRST **)
   412 
   413 (*equality of theorems uses equality of signatures and
   414   the a-convertible test for terms*)
   415 fun eq_thm (th1,th2) =
   416     let val {sign=sg1, hyps=hyps1, prop=prop1, ...} = rep_thm th1
   417         and {sign=sg2, hyps=hyps2, prop=prop2, ...} = rep_thm th2
   418     in  Sign.eq_sg (sg1,sg2) andalso
   419         aconvs(hyps1,hyps2) andalso
   420         prop1 aconv prop2
   421     end;
   422 
   423 (*Do the two theorems have the same signature?*)
   424 fun eq_thm_sg (th1,th2) = Sign.eq_sg(#sign(rep_thm th1), #sign(rep_thm th2));
   425 
   426 (*Useful "distance" function for BEST_FIRST*)
   427 val size_of_thm = size_of_term o #prop o rep_thm;
   428 
   429 
   430 (*** Meta-Rewriting Rules ***)
   431 
   432 
   433 val reflexive_thm =
   434   let val cx = cterm_of Sign.pure (Var(("x",0),TVar(("'a",0),logicS)))
   435   in Thm.reflexive cx end;
   436 
   437 val symmetric_thm =
   438   let val xy = read_cterm Sign.pure ("x::'a::logic == y",propT)
   439   in standard(Thm.implies_intr_hyps(Thm.symmetric(Thm.assume xy))) end;
   440 
   441 val transitive_thm =
   442   let val xy = read_cterm Sign.pure ("x::'a::logic == y",propT)
   443       val yz = read_cterm Sign.pure ("y::'a::logic == z",propT)
   444       val xythm = Thm.assume xy and yzthm = Thm.assume yz
   445   in standard(Thm.implies_intr yz (Thm.transitive xythm yzthm)) end;
   446 
   447 (** Below, a "conversion" has type cterm -> thm **)
   448 
   449 val refl_cimplies = reflexive (cterm_of Sign.pure implies);
   450 
   451 (*In [A1,...,An]==>B, rewrite the selected A's only -- for rewrite_goals_tac*)
   452 (*Do not rewrite flex-flex pairs*)
   453 fun goals_conv pred cv =
   454   let fun gconv i ct =
   455         let val (A,B) = Thm.dest_cimplies ct
   456             val (thA,j) = case term_of A of
   457                   Const("=?=",_)$_$_ => (reflexive A, i)
   458                 | _ => (if pred i then cv A else reflexive A, i+1)
   459         in  combination (combination refl_cimplies thA) (gconv j B) end
   460         handle TERM _ => reflexive ct
   461   in gconv 1 end;
   462 
   463 (*Use a conversion to transform a theorem*)
   464 fun fconv_rule cv th = equal_elim (cv (cprop_of th)) th;
   465 
   466 (*rewriting conversion*)
   467 fun rew_conv mode prover mss = rewrite_cterm mode mss prover;
   468 
   469 (*Rewrite a theorem*)
   470 fun rewrite_rule thms =
   471   fconv_rule (rew_conv (true,false) (K(K None)) (Thm.mss_of thms));
   472 
   473 (*Rewrite the subgoals of a proof state (represented by a theorem) *)
   474 fun rewrite_goals_rule thms =
   475   fconv_rule (goals_conv (K true) (rew_conv (true,false) (K(K None))
   476              (Thm.mss_of thms)));
   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   open Logic
   504   val alpha = TVar(("'a",0), [])     (*  type ?'a::{}  *)
   505   fun err th = raise THM("flexpair_inst: ", 0, [th])
   506   fun flexpair_inst def th =
   507     let val {prop = Const _ $ t $ u,  sign,...} = rep_thm th
   508         val cterm = cterm_of sign
   509         fun cvar a = cterm(Var((a,0),alpha))
   510         val def' = cterm_instantiate [(cvar"t", cterm t), (cvar"u", cterm u)]
   511                    def
   512     in  equal_elim def' th
   513     end
   514     handle THM _ => err th | bind => err th
   515 in
   516 val flexpair_intr = flexpair_inst (symmetric flexpair_def)
   517 and flexpair_elim = flexpair_inst flexpair_def
   518 end;
   519 
   520 (*Version for flexflex pairs -- this supports lifting.*)
   521 fun flexpair_abs_elim_list cts =
   522     flexpair_intr o equal_abs_elim_list cts o flexpair_elim;
   523 
   524 
   525 (*** Some useful meta-theorems ***)
   526 
   527 (*The rule V/V, obtains assumption solving for eresolve_tac*)
   528 val asm_rl = trivial(read_cterm Sign.pure ("PROP ?psi",propT));
   529 
   530 (*Meta-level cut rule: [| V==>W; V |] ==> W *)
   531 val cut_rl = trivial(read_cterm Sign.pure
   532         ("PROP ?psi ==> PROP ?theta", propT));
   533 
   534 (*Generalized elim rule for one conclusion; cut_rl with reversed premises:
   535      [| PROP V;  PROP V ==> PROP W |] ==> PROP W *)
   536 val revcut_rl =
   537   let val V = read_cterm Sign.pure ("PROP V", propT)
   538       and VW = read_cterm Sign.pure ("PROP V ==> PROP W", propT);
   539   in  standard (implies_intr V
   540                 (implies_intr VW
   541                  (implies_elim (assume VW) (assume V))))
   542   end;
   543 
   544 (* (!!x. PROP ?V) == PROP ?V       Allows removal of redundant parameters*)
   545 val triv_forall_equality =
   546   let val V  = read_cterm Sign.pure ("PROP V", propT)
   547       and QV = read_cterm Sign.pure ("!!x::'a. PROP V", propT)
   548       and x  = read_cterm Sign.pure ("x", TFree("'a",logicS));
   549   in  standard (equal_intr (implies_intr QV (forall_elim x (assume QV)))
   550                            (implies_intr V  (forall_intr x (assume V))))
   551   end;
   552 
   553 end
   554 end;
   555