src/Pure/drule.ML
author wenzelm
Tue Nov 28 00:35:21 2006 +0100 (2006-11-28)
changeset 21566 af2932baf068
parent 21519 33f109ea389f
child 21578 a89f786b301a
permissions -rw-r--r--
dest_term: strip_imp_concl;
     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 OF COMP;
    10 
    11 signature BASIC_DRULE =
    12 sig
    13   val mk_implies: cterm * cterm -> cterm
    14   val list_implies: cterm list * cterm -> cterm
    15   val dest_implies: cterm -> cterm * cterm
    16   val dest_equals: cterm -> cterm * cterm
    17   val dest_equals_lhs: cterm -> cterm
    18   val dest_equals_rhs: cterm -> cterm
    19   val strip_imp_prems: cterm -> cterm list
    20   val strip_imp_concl: cterm -> cterm
    21   val cprems_of: thm -> cterm list
    22   val cterm_fun: (term -> term) -> (cterm -> cterm)
    23   val ctyp_fun: (typ -> typ) -> (ctyp -> ctyp)
    24   val read_insts: theory -> (indexname -> typ option) * (indexname -> sort option) ->
    25     (indexname -> typ option) * (indexname -> sort option) -> string list ->
    26     (indexname * string) list -> (ctyp * ctyp) list * (cterm * cterm) list
    27   val types_sorts: thm -> (indexname-> typ option) * (indexname-> sort option)
    28   val forall_intr_list: cterm list -> thm -> thm
    29   val forall_intr_frees: thm -> thm
    30   val forall_intr_vars: thm -> thm
    31   val forall_elim_list: cterm list -> thm -> thm
    32   val forall_elim_var: int -> thm -> thm
    33   val forall_elim_vars: int -> thm -> thm
    34   val gen_all: thm -> thm
    35   val lift_all: cterm -> thm -> thm
    36   val freeze_thaw: thm -> thm * (thm -> thm)
    37   val freeze_thaw_robust: thm -> thm * (int -> thm -> thm)
    38   val implies_elim_list: thm -> thm list -> thm
    39   val implies_intr_list: cterm list -> thm -> thm
    40   val instantiate: (ctyp * ctyp) list * (cterm * cterm) list -> thm -> thm
    41   val zero_var_indexes: thm -> thm
    42   val implies_intr_hyps: thm -> thm
    43   val standard: thm -> thm
    44   val standard': thm -> thm
    45   val rotate_prems: int -> thm -> thm
    46   val rearrange_prems: int list -> thm -> thm
    47   val RSN: thm * (int * thm) -> thm
    48   val RS: thm * thm -> thm
    49   val RLN: thm list * (int * thm list) -> thm list
    50   val RL: thm list * thm list -> thm list
    51   val MRS: thm list * thm -> thm
    52   val MRL: thm list list * thm list -> thm list
    53   val OF: thm * thm list -> thm
    54   val compose: thm * int * thm -> thm list
    55   val COMP: thm * thm -> thm
    56   val read_instantiate_sg: theory -> (string*string)list -> thm -> thm
    57   val read_instantiate: (string*string)list -> thm -> thm
    58   val cterm_instantiate: (cterm*cterm)list -> thm -> thm
    59   val eq_thm_thy: thm * thm -> bool
    60   val eq_thm_prop: thm * thm -> bool
    61   val equiv_thm: thm * thm -> bool
    62   val size_of_thm: thm -> int
    63   val reflexive_thm: thm
    64   val symmetric_thm: thm
    65   val transitive_thm: thm
    66   val symmetric_fun: thm -> thm
    67   val extensional: thm -> thm
    68   val equals_cong: thm
    69   val imp_cong: thm
    70   val swap_prems_eq: thm
    71   val asm_rl: thm
    72   val cut_rl: thm
    73   val revcut_rl: thm
    74   val thin_rl: thm
    75   val triv_forall_equality: thm
    76   val distinct_prems_rl: thm
    77   val swap_prems_rl: thm
    78   val equal_intr_rule: thm
    79   val equal_elim_rule1: thm
    80   val equal_elim_rule2: thm
    81   val inst: string -> string -> thm -> thm
    82   val instantiate': ctyp option list -> cterm option list -> thm -> thm
    83 end;
    84 
    85 signature DRULE =
    86 sig
    87   include BASIC_DRULE
    88   val generalize: string list * string list -> thm -> thm
    89   val list_comb: cterm * cterm list -> cterm
    90   val strip_comb: cterm -> cterm * cterm list
    91   val strip_type: ctyp -> ctyp list * ctyp
    92   val lhs_of: thm -> cterm
    93   val rhs_of: thm -> cterm
    94   val beta_conv: cterm -> cterm -> cterm
    95   val plain_prop_of: thm -> term
    96   val fold_terms: (term -> 'a -> 'a) -> thm -> 'a -> 'a
    97   val add_used: thm -> string list -> string list
    98   val flexflex_unique: thm -> thm
    99   val close_derivation: thm -> thm
   100   val local_standard: thm -> thm
   101   val store_thm: bstring -> thm -> thm
   102   val store_standard_thm: bstring -> thm -> thm
   103   val store_thm_open: bstring -> thm -> thm
   104   val store_standard_thm_open: bstring -> thm -> thm
   105   val compose_single: thm * int * thm -> thm
   106   val add_rule: thm -> thm list -> thm list
   107   val del_rule: thm -> thm list -> thm list
   108   val merge_rules: thm list * thm list -> thm list
   109   val imp_cong_rule: thm -> thm -> thm
   110   val beta_eta_conversion: cterm -> thm
   111   val eta_long_conversion: cterm -> thm
   112   val eta_contraction_rule: thm -> thm
   113   val forall_conv: int -> (cterm -> thm) -> cterm -> thm
   114   val concl_conv: int -> (cterm -> thm) -> cterm -> thm
   115   val prems_conv: int -> (int -> cterm -> thm) -> cterm -> thm
   116   val goals_conv: (int -> bool) -> (cterm -> thm) -> cterm -> thm
   117   val fconv_rule: (cterm -> thm) -> thm -> thm
   118   val norm_hhf_eq: thm
   119   val is_norm_hhf: term -> bool
   120   val norm_hhf: theory -> term -> term
   121   val norm_hhf_cterm: cterm -> cterm
   122   val unvarify: thm -> thm
   123   val protect: cterm -> cterm
   124   val protectI: thm
   125   val protectD: thm
   126   val protect_cong: thm
   127   val implies_intr_protected: cterm list -> thm -> thm
   128   val termI: thm
   129   val mk_term: cterm -> thm
   130   val dest_term: thm -> cterm
   131   val cterm_rule: (thm -> thm) -> cterm -> cterm
   132   val term_rule: theory -> (thm -> thm) -> term -> term
   133   val sort_triv: theory -> typ * sort -> thm list
   134   val unconstrainTs: thm -> thm
   135   val rename_bvars: (string * string) list -> thm -> thm
   136   val rename_bvars': string option list -> thm -> thm
   137   val incr_indexes: thm -> thm -> thm
   138   val incr_indexes2: thm -> thm -> thm -> thm
   139   val remdups_rl: thm
   140   val multi_resolve: thm list -> thm -> thm Seq.seq
   141   val multi_resolves: thm list -> thm list -> thm Seq.seq
   142   val abs_def: thm -> thm
   143   val read_instantiate_sg': theory -> (indexname * string) list -> thm -> thm
   144   val read_instantiate': (indexname * string) list -> thm -> thm
   145 end;
   146 
   147 structure Drule: DRULE =
   148 struct
   149 
   150 
   151 (** some cterm->cterm operations: faster than calling cterm_of! **)
   152 
   153 fun dest_implies ct =
   154   (case Thm.term_of ct of
   155     Const ("==>", _) $ _ $ _ => Thm.dest_binop ct
   156   | _ => raise TERM ("dest_implies", [Thm.term_of ct]));
   157 
   158 fun dest_equals ct =
   159   (case Thm.term_of ct of
   160     Const ("==", _) $ _ $ _ => Thm.dest_binop ct
   161   | _ => raise TERM ("dest_equals", [Thm.term_of ct]));
   162 
   163 fun dest_equals_lhs ct =
   164   (case Thm.term_of ct of
   165     Const ("==", _) $ _ $ _ => #1 (Thm.dest_binop ct)
   166   | _ => raise TERM ("dest_equals_lhs", [Thm.term_of ct]));
   167 
   168 fun dest_equals_rhs ct =
   169   (case Thm.term_of ct of
   170     Const ("==", _) $ _ $ _ => Thm.dest_arg ct
   171   | _ => raise TERM ("dest_equals_rhs", [Thm.term_of ct]));
   172 
   173 val lhs_of = dest_equals_lhs o Thm.cprop_of;
   174 val rhs_of = dest_equals_rhs o Thm.cprop_of;
   175 
   176 (* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
   177 fun strip_imp_prems ct =
   178   let val (cA, cB) = dest_implies ct
   179   in cA :: strip_imp_prems cB end
   180   handle TERM _ => [];
   181 
   182 (* A1==>...An==>B  goes to B, where B is not an implication *)
   183 fun strip_imp_concl ct =
   184   (case Thm.term_of ct of
   185     Const ("==>", _) $ _ $ _ => strip_imp_concl (Thm.dest_arg ct)
   186   | _ => ct);
   187 
   188 (*The premises of a theorem, as a cterm list*)
   189 val cprems_of = strip_imp_prems o cprop_of;
   190 
   191 fun cterm_fun f ct =
   192   let val {t, thy, ...} = Thm.rep_cterm ct
   193   in Thm.cterm_of thy (f t) end;
   194 
   195 fun ctyp_fun f cT =
   196   let val {T, thy, ...} = Thm.rep_ctyp cT
   197   in Thm.ctyp_of thy (f T) end;
   198 
   199 val cert = cterm_of ProtoPure.thy;
   200 
   201 val implies = cert Term.implies;
   202 fun mk_implies (A, B) = Thm.capply (Thm.capply implies A) B;
   203 
   204 (*cterm version of list_implies: [A1,...,An], B  goes to [|A1;==>;An|]==>B *)
   205 fun list_implies([], B) = B
   206   | list_implies(A::AS, B) = mk_implies (A, list_implies(AS,B));
   207 
   208 (*cterm version of list_comb: maps  (f, [t1,...,tn])  to  f(t1,...,tn) *)
   209 fun list_comb (f, []) = f
   210   | list_comb (f, t::ts) = list_comb (Thm.capply f t, ts);
   211 
   212 (*cterm version of strip_comb: maps  f(t1,...,tn)  to  (f, [t1,...,tn]) *)
   213 fun strip_comb ct =
   214   let
   215     fun stripc (p as (ct, cts)) =
   216       let val (ct1, ct2) = Thm.dest_comb ct
   217       in stripc (ct1, ct2 :: cts) end handle CTERM _ => p
   218   in stripc (ct, []) end;
   219 
   220 (* cterm version of strip_type: maps  [T1,...,Tn]--->T  to   ([T1,T2,...,Tn], T) *)
   221 fun strip_type cT = (case Thm.typ_of cT of
   222     Type ("fun", _) =>
   223       let
   224         val [cT1, cT2] = Thm.dest_ctyp cT;
   225         val (cTs, cT') = strip_type cT2
   226       in (cT1 :: cTs, cT') end
   227   | _ => ([], cT));
   228 
   229 (*Beta-conversion for cterms, where x is an abstraction. Simply returns the rhs
   230   of the meta-equality returned by the beta_conversion rule.*)
   231 fun beta_conv x y =
   232   Thm.dest_arg (cprop_of (Thm.beta_conversion false (Thm.capply x y)));
   233 
   234 fun plain_prop_of raw_thm =
   235   let
   236     val thm = Thm.strip_shyps raw_thm;
   237     fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
   238     val {hyps, prop, tpairs, ...} = Thm.rep_thm thm;
   239   in
   240     if not (null hyps) then
   241       err "theorem may not contain hypotheses"
   242     else if not (null (Thm.extra_shyps thm)) then
   243       err "theorem may not contain sort hypotheses"
   244     else if not (null tpairs) then
   245       err "theorem may not contain flex-flex pairs"
   246     else prop
   247   end;
   248 
   249 fun fold_terms f th =
   250   let val {tpairs, prop, hyps, ...} = Thm.rep_thm th
   251   in fold (fn (t, u) => f t #> f u) tpairs #> f prop #> fold f hyps end;
   252 
   253 
   254 
   255 (** reading of instantiations **)
   256 
   257 fun absent ixn =
   258   error("No such variable in term: " ^ Syntax.string_of_vname ixn);
   259 
   260 fun inst_failure ixn =
   261   error("Instantiation of " ^ Syntax.string_of_vname ixn ^ " fails");
   262 
   263 fun read_insts thy (rtypes,rsorts) (types,sorts) used insts =
   264 let
   265     fun is_tv ((a, _), _) =
   266       (case Symbol.explode a of "'" :: _ => true | _ => false);
   267     val (tvs, vs) = List.partition is_tv insts;
   268     fun sort_of ixn = case rsorts ixn of SOME S => S | NONE => absent ixn;
   269     fun readT (ixn, st) =
   270         let val S = sort_of ixn;
   271             val T = Sign.read_typ (thy,sorts) st;
   272         in if Sign.typ_instance thy (T, TVar(ixn,S)) then (ixn,T)
   273            else inst_failure ixn
   274         end
   275     val tye = map readT tvs;
   276     fun mkty(ixn,st) = (case rtypes ixn of
   277                           SOME T => (ixn,(st,typ_subst_TVars tye T))
   278                         | NONE => absent ixn);
   279     val ixnsTs = map mkty vs;
   280     val ixns = map fst ixnsTs
   281     and sTs  = map snd ixnsTs
   282     val (cts,tye2) = read_def_cterms(thy,types,sorts) used false sTs;
   283     fun mkcVar(ixn,T) =
   284         let val U = typ_subst_TVars tye2 T
   285         in cterm_of thy (Var(ixn,U)) end
   286     val ixnTs = ListPair.zip(ixns, map snd sTs)
   287 in (map (fn (ixn, T) => (ctyp_of thy (TVar (ixn, sort_of ixn)),
   288       ctyp_of thy T)) (tye2 @ tye),
   289     ListPair.zip(map mkcVar ixnTs,cts))
   290 end;
   291 
   292 
   293 (*** Find the type (sort) associated with a (T)Var or (T)Free in a term
   294      Used for establishing default types (of variables) and sorts (of
   295      type variables) when reading another term.
   296      Index -1 indicates that a (T)Free rather than a (T)Var is wanted.
   297 ***)
   298 
   299 fun types_sorts thm =
   300   let
   301     val vars = fold_terms Term.add_vars thm [];
   302     val frees = fold_terms Term.add_frees thm [];
   303     val tvars = fold_terms Term.add_tvars thm [];
   304     val tfrees = fold_terms Term.add_tfrees thm [];
   305     fun types (a, i) =
   306       if i < 0 then AList.lookup (op =) frees a else AList.lookup (op =) vars (a, i);
   307     fun sorts (a, i) =
   308       if i < 0 then AList.lookup (op =) tfrees a else AList.lookup (op =) tvars (a, i);
   309   in (types, sorts) end;
   310 
   311 val add_used =
   312   (fold_terms o fold_types o fold_atyps)
   313     (fn TFree (a, _) => insert (op =) a
   314       | TVar ((a, _), _) => insert (op =) a
   315       | _ => I);
   316 
   317 
   318 
   319 (** Standardization of rules **)
   320 
   321 (* type classes and sorts *)
   322 
   323 fun sort_triv thy (T, S) =
   324   let
   325     val certT = Thm.ctyp_of thy;
   326     val cT = certT T;
   327     fun class_triv c =
   328       Thm.class_triv thy c
   329       |> Thm.instantiate ([(certT (TVar (("'a", 0), [c])), cT)], []);
   330   in map class_triv S end;
   331 
   332 fun unconstrainTs th =
   333   fold (Thm.unconstrainT o Thm.ctyp_of (Thm.theory_of_thm th) o TVar)
   334     (fold_terms Term.add_tvars th []) th;
   335 
   336 (*Generalization over a list of variables*)
   337 val forall_intr_list = fold_rev forall_intr;
   338 
   339 (*Generalization over all suitable Free variables*)
   340 fun forall_intr_frees th =
   341     let
   342       val {prop, hyps, tpairs, thy,...} = rep_thm th;
   343       val fixed = fold Term.add_frees (Thm.terms_of_tpairs tpairs @ hyps) [];
   344       val frees = Term.fold_aterms (fn Free v =>
   345         if member (op =) fixed v then I else insert (op =) v | _ => I) prop [];
   346     in fold (forall_intr o cterm_of thy o Free) frees th end;
   347 
   348 (*Generalization over Vars -- canonical order*)
   349 fun forall_intr_vars th =
   350   fold forall_intr
   351     (map (Thm.cterm_of (Thm.theory_of_thm th) o Var) (fold_terms Term.add_vars th [])) th;
   352 
   353 val forall_elim_var = PureThy.forall_elim_var;
   354 val forall_elim_vars = PureThy.forall_elim_vars;
   355 
   356 fun outer_params t =
   357   let val vs = Term.strip_all_vars t
   358   in Name.variant_list [] (map (Name.clean o #1) vs) ~~ map #2 vs end;
   359 
   360 (*generalize outermost parameters*)
   361 fun gen_all th =
   362   let
   363     val {thy, prop, maxidx, ...} = Thm.rep_thm th;
   364     val cert = Thm.cterm_of thy;
   365     fun elim (x, T) = Thm.forall_elim (cert (Var ((x, maxidx + 1), T)));
   366   in fold elim (outer_params prop) th end;
   367 
   368 (*lift vars wrt. outermost goal parameters
   369   -- reverses the effect of gen_all modulo higher-order unification*)
   370 fun lift_all goal th =
   371   let
   372     val thy = Theory.merge (Thm.theory_of_cterm goal, Thm.theory_of_thm th);
   373     val cert = Thm.cterm_of thy;
   374     val maxidx = Thm.maxidx_of th;
   375     val ps = outer_params (Thm.term_of goal)
   376       |> map (fn (x, T) => Var ((x, maxidx + 1), Logic.incr_tvar (maxidx + 1) T));
   377     val Ts = map Term.fastype_of ps;
   378     val inst = fold_terms Term.add_vars th [] |> map (fn (xi, T) =>
   379       (cert (Var (xi, T)), cert (Term.list_comb (Var (xi, Ts ---> T), ps))));
   380   in
   381     th |> Thm.instantiate ([], inst)
   382     |> fold_rev (Thm.forall_intr o cert) ps
   383   end;
   384 
   385 (*direct generalization*)
   386 fun generalize names th = Thm.generalize names (Thm.maxidx_of th + 1) th;
   387 
   388 (*specialization over a list of cterms*)
   389 val forall_elim_list = fold forall_elim;
   390 
   391 (*maps A1,...,An |- B  to  [| A1;...;An |] ==> B*)
   392 val implies_intr_list = fold_rev implies_intr;
   393 
   394 (*maps [| A1;...;An |] ==> B and [A1,...,An]  to  B*)
   395 fun implies_elim_list impth ths = Library.foldl (uncurry implies_elim) (impth,ths);
   396 
   397 (*Reset Var indexes to zero, renaming to preserve distinctness*)
   398 fun zero_var_indexes th =
   399   let
   400     val thy = Thm.theory_of_thm th;
   401     val certT = Thm.ctyp_of thy and cert = Thm.cterm_of thy;
   402     val (instT, inst) = TermSubst.zero_var_indexes_inst (Thm.full_prop_of th);
   403     val cinstT = map (fn (v, T) => (certT (TVar v), certT T)) instT;
   404     val cinst = map (fn (v, t) => (cert (Var v), cert t)) inst;
   405   in Thm.adjust_maxidx_thm ~1 (Thm.instantiate (cinstT, cinst) th) end;
   406 
   407 
   408 (** Standard form of object-rule: no hypotheses, flexflex constraints,
   409     Frees, or outer quantifiers; all generality expressed by Vars of index 0.**)
   410 
   411 (*Discharge all hypotheses.*)
   412 fun implies_intr_hyps th =
   413   fold Thm.implies_intr (#hyps (Thm.crep_thm th)) th;
   414 
   415 (*Squash a theorem's flexflex constraints provided it can be done uniquely.
   416   This step can lose information.*)
   417 fun flexflex_unique th =
   418   if null (tpairs_of th) then th else
   419     case Seq.chop 2 (flexflex_rule th) of
   420       ([th],_) => th
   421     | ([],_)   => raise THM("flexflex_unique: impossible constraints", 0, [th])
   422     |      _   => raise THM("flexflex_unique: multiple unifiers", 0, [th]);
   423 
   424 fun close_derivation thm =
   425   if Thm.get_name_tags thm = ("", []) then Thm.name_thm ("", thm)
   426   else thm;
   427 
   428 val standard' =
   429   implies_intr_hyps
   430   #> forall_intr_frees
   431   #> `Thm.maxidx_of
   432   #-> (fn maxidx =>
   433     forall_elim_vars (maxidx + 1)
   434     #> Thm.strip_shyps
   435     #> zero_var_indexes
   436     #> Thm.varifyT
   437     #> Thm.compress);
   438 
   439 val standard =
   440   flexflex_unique
   441   #> standard'
   442   #> close_derivation;
   443 
   444 val local_standard =
   445   flexflex_unique
   446   #> Thm.strip_shyps
   447   #> zero_var_indexes
   448   #> Thm.compress
   449   #> close_derivation;
   450 
   451 
   452 (*Convert all Vars in a theorem to Frees.  Also return a function for
   453   reversing that operation.  DOES NOT WORK FOR TYPE VARIABLES.
   454   Similar code in type/freeze_thaw*)
   455 
   456 fun freeze_thaw_robust th =
   457  let val fth = Thm.freezeT th
   458      val {prop, tpairs, thy, ...} = rep_thm fth
   459  in
   460    case foldr add_term_vars [] (prop :: Thm.terms_of_tpairs tpairs) of
   461        [] => (fth, fn i => fn x => x)   (*No vars: nothing to do!*)
   462      | vars =>
   463          let fun newName (Var(ix,_)) = (ix, gensym (string_of_indexname ix))
   464              val alist = map newName vars
   465              fun mk_inst (Var(v,T)) =
   466                  (cterm_of thy (Var(v,T)),
   467                   cterm_of thy (Free(((the o AList.lookup (op =) alist) v), T)))
   468              val insts = map mk_inst vars
   469              fun thaw i th' = (*i is non-negative increment for Var indexes*)
   470                  th' |> forall_intr_list (map #2 insts)
   471                      |> forall_elim_list (map (Thm.cterm_incr_indexes i o #1) insts)
   472          in  (Thm.instantiate ([],insts) fth, thaw)  end
   473  end;
   474 
   475 (*Basic version of the function above. No option to rename Vars apart in thaw.
   476   The Frees created from Vars have nice names. FIXME: does not check for
   477   clashes with variables in the assumptions, so delete and use freeze_thaw_robust instead?*)
   478 fun freeze_thaw th =
   479  let val fth = Thm.freezeT th
   480      val {prop, tpairs, thy, ...} = rep_thm fth
   481  in
   482    case foldr add_term_vars [] (prop :: Thm.terms_of_tpairs tpairs) of
   483        [] => (fth, fn x => x)
   484      | vars =>
   485          let fun newName (Var(ix,_), (pairs,used)) =
   486                    let val v = Name.variant used (string_of_indexname ix)
   487                    in  ((ix,v)::pairs, v::used)  end;
   488              val (alist, _) = foldr newName ([], Library.foldr add_term_names
   489                (prop :: Thm.terms_of_tpairs tpairs, [])) vars
   490              fun mk_inst (Var(v,T)) =
   491                  (cterm_of thy (Var(v,T)),
   492                   cterm_of thy (Free(((the o AList.lookup (op =) alist) v), T)))
   493              val insts = map mk_inst vars
   494              fun thaw th' =
   495                  th' |> forall_intr_list (map #2 insts)
   496                      |> forall_elim_list (map #1 insts)
   497          in  (Thm.instantiate ([],insts) fth, thaw)  end
   498  end;
   499 
   500 (*Rotates a rule's premises to the left by k*)
   501 val rotate_prems = permute_prems 0;
   502 
   503 (* permute prems, where the i-th position in the argument list (counting from 0)
   504    gives the position within the original thm to be transferred to position i.
   505    Any remaining trailing positions are left unchanged. *)
   506 val rearrange_prems = let
   507   fun rearr new []      thm = thm
   508   |   rearr new (p::ps) thm = rearr (new+1)
   509      (map (fn q => if new<=q andalso q<p then q+1 else q) ps)
   510      (permute_prems (new+1) (new-p) (permute_prems new (p-new) thm))
   511   in rearr 0 end;
   512 
   513 (*Resolution: exactly one resolvent must be produced.*)
   514 fun tha RSN (i,thb) =
   515   case Seq.chop 2 (biresolution false [(false,tha)] i thb) of
   516       ([th],_) => th
   517     | ([],_)   => raise THM("RSN: no unifiers", i, [tha,thb])
   518     |      _   => raise THM("RSN: multiple unifiers", i, [tha,thb]);
   519 
   520 (*resolution: P==>Q, Q==>R gives P==>R. *)
   521 fun tha RS thb = tha RSN (1,thb);
   522 
   523 (*For joining lists of rules*)
   524 fun thas RLN (i,thbs) =
   525   let val resolve = biresolution false (map (pair false) thas) i
   526       fun resb thb = Seq.list_of (resolve thb) handle THM _ => []
   527   in maps resb thbs end;
   528 
   529 fun thas RL thbs = thas RLN (1,thbs);
   530 
   531 (*Resolve a list of rules against bottom_rl from right to left;
   532   makes proof trees*)
   533 fun rls MRS bottom_rl =
   534   let fun rs_aux i [] = bottom_rl
   535         | rs_aux i (rl::rls) = rl RSN (i, rs_aux (i+1) rls)
   536   in  rs_aux 1 rls  end;
   537 
   538 (*As above, but for rule lists*)
   539 fun rlss MRL bottom_rls =
   540   let fun rs_aux i [] = bottom_rls
   541         | rs_aux i (rls::rlss) = rls RLN (i, rs_aux (i+1) rlss)
   542   in  rs_aux 1 rlss  end;
   543 
   544 (*A version of MRS with more appropriate argument order*)
   545 fun bottom_rl OF rls = rls MRS bottom_rl;
   546 
   547 (*compose Q and [...,Qi,Q(i+1),...]==>R to [...,Q(i+1),...]==>R
   548   with no lifting or renaming!  Q may contain ==> or meta-quants
   549   ALWAYS deletes premise i *)
   550 fun compose(tha,i,thb) =
   551     Seq.list_of (bicompose false (false,tha,0) i thb);
   552 
   553 fun compose_single (tha,i,thb) =
   554   (case compose (tha,i,thb) of
   555     [th] => th
   556   | _ => raise THM ("compose: unique result expected", i, [tha,thb]));
   557 
   558 (*compose Q and [Q1,Q2,...,Qk]==>R to [Q2,...,Qk]==>R getting unique result*)
   559 fun tha COMP thb =
   560     case compose(tha,1,thb) of
   561         [th] => th
   562       | _ =>   raise THM("COMP", 1, [tha,thb]);
   563 
   564 
   565 (** theorem equality **)
   566 
   567 (*True if the two theorems have the same theory.*)
   568 val eq_thm_thy = eq_thy o pairself Thm.theory_of_thm;
   569 
   570 (*True if the two theorems have the same prop field, ignoring hyps, der, etc.*)
   571 val eq_thm_prop = op aconv o pairself Thm.full_prop_of;
   572 
   573 (*Useful "distance" function for BEST_FIRST*)
   574 val size_of_thm = size_of_term o Thm.full_prop_of;
   575 
   576 (*maintain lists of theorems --- preserving canonical order*)
   577 val del_rule = remove eq_thm_prop;
   578 fun add_rule th = cons th o del_rule th;
   579 val merge_rules = Library.merge eq_thm_prop;
   580 
   581 (*pattern equivalence*)
   582 fun equiv_thm ths =
   583   Pattern.equiv (Theory.merge (pairself Thm.theory_of_thm ths)) (pairself Thm.full_prop_of ths);
   584 
   585 
   586 (*** Meta-Rewriting Rules ***)
   587 
   588 fun read_prop s = read_cterm ProtoPure.thy (s, propT);
   589 
   590 fun store_thm name thm = hd (PureThy.smart_store_thms (name, [thm]));
   591 fun store_standard_thm name thm = store_thm name (standard thm);
   592 fun store_thm_open name thm = hd (PureThy.smart_store_thms_open (name, [thm]));
   593 fun store_standard_thm_open name thm = store_thm_open name (standard' thm);
   594 
   595 val reflexive_thm =
   596   let val cx = cert (Var(("x",0),TVar(("'a",0),[])))
   597   in store_standard_thm_open "reflexive" (Thm.reflexive cx) end;
   598 
   599 val symmetric_thm =
   600   let val xy = read_prop "x == y"
   601   in store_standard_thm_open "symmetric" (Thm.implies_intr xy (Thm.symmetric (Thm.assume xy))) end;
   602 
   603 val transitive_thm =
   604   let val xy = read_prop "x == y"
   605       val yz = read_prop "y == z"
   606       val xythm = Thm.assume xy and yzthm = Thm.assume yz
   607   in store_standard_thm_open "transitive" (Thm.implies_intr yz (Thm.transitive xythm yzthm)) end;
   608 
   609 fun symmetric_fun thm = thm RS symmetric_thm;
   610 
   611 fun extensional eq =
   612   let val eq' =
   613     abstract_rule "x" (Thm.dest_arg (fst (dest_equals (cprop_of eq)))) eq
   614   in equal_elim (eta_conversion (cprop_of eq')) eq' end;
   615 
   616 val equals_cong =
   617   store_standard_thm_open "equals_cong" (Thm.reflexive (read_prop "x == y"));
   618 
   619 val imp_cong =
   620   let
   621     val ABC = read_prop "PROP A ==> PROP B == PROP C"
   622     val AB = read_prop "PROP A ==> PROP B"
   623     val AC = read_prop "PROP A ==> PROP C"
   624     val A = read_prop "PROP A"
   625   in
   626     store_standard_thm_open "imp_cong" (implies_intr ABC (equal_intr
   627       (implies_intr AB (implies_intr A
   628         (equal_elim (implies_elim (assume ABC) (assume A))
   629           (implies_elim (assume AB) (assume A)))))
   630       (implies_intr AC (implies_intr A
   631         (equal_elim (symmetric (implies_elim (assume ABC) (assume A)))
   632           (implies_elim (assume AC) (assume A)))))))
   633   end;
   634 
   635 val swap_prems_eq =
   636   let
   637     val ABC = read_prop "PROP A ==> PROP B ==> PROP C"
   638     val BAC = read_prop "PROP B ==> PROP A ==> PROP C"
   639     val A = read_prop "PROP A"
   640     val B = read_prop "PROP B"
   641   in
   642     store_standard_thm_open "swap_prems_eq" (equal_intr
   643       (implies_intr ABC (implies_intr B (implies_intr A
   644         (implies_elim (implies_elim (assume ABC) (assume A)) (assume B)))))
   645       (implies_intr BAC (implies_intr A (implies_intr B
   646         (implies_elim (implies_elim (assume BAC) (assume B)) (assume A))))))
   647   end;
   648 
   649 val imp_cong_rule = combination o combination (reflexive implies);
   650 
   651 local
   652   val dest_eq = dest_equals o cprop_of
   653   val rhs_of = snd o dest_eq
   654 in
   655 fun beta_eta_conversion t =
   656   let val thm = beta_conversion true t
   657   in transitive thm (eta_conversion (rhs_of thm)) end
   658 end;
   659 
   660 fun eta_long_conversion ct = transitive (beta_eta_conversion ct)
   661   (symmetric (beta_eta_conversion (cterm_fun (Pattern.eta_long []) ct)));
   662 
   663 (*Contract all eta-redexes in the theorem, lest they give rise to needless abstractions*)
   664 fun eta_contraction_rule th =
   665   equal_elim (eta_conversion (cprop_of th)) th;
   666 
   667 val abs_def =
   668   let
   669     fun contract_lhs th =
   670       Thm.transitive (Thm.symmetric (beta_eta_conversion (fst (dest_equals (cprop_of th))))) th;
   671     fun abstract cx th = Thm.abstract_rule
   672         (case Thm.term_of cx of Var ((x, _), _) => x | Free (x, _) => x | _ => "x") cx th
   673       handle THM _ => raise THM ("Malformed definitional equation", 0, [th]);
   674   in
   675     contract_lhs
   676     #> `(snd o strip_comb o fst o dest_equals o cprop_of)
   677     #-> fold_rev abstract
   678     #> contract_lhs
   679   end;
   680 
   681 (*rewrite B in !!x1 ... xn. B*)
   682 fun forall_conv 0 cv ct = cv ct
   683   | forall_conv n cv ct =
   684       (case try Thm.dest_comb ct of
   685         NONE => cv ct
   686       | SOME (A, B) =>
   687           (case (term_of A, term_of B) of
   688             (Const ("all", _), Abs (x, _, _)) =>
   689               let val (v, B') = Thm.dest_abs (SOME (gensym "all_")) B in
   690                 Thm.combination (Thm.reflexive A)
   691                   (Thm.abstract_rule x v (forall_conv (n - 1) cv B'))
   692               end
   693           | _ => cv ct));
   694 
   695 (*rewrite B in A1 ==> ... ==> An ==> B*)
   696 fun concl_conv 0 cv ct = cv ct
   697   | concl_conv n cv ct =
   698       (case try dest_implies ct of
   699         NONE => cv ct
   700       | SOME (A, B) => imp_cong_rule (reflexive A) (concl_conv (n - 1) cv B));
   701 
   702 (*rewrite the A's in A1 ==> ... ==> An ==> B*)
   703 fun prems_conv 0 _ = reflexive
   704   | prems_conv n cv =
   705       let
   706         fun conv i ct =
   707           if i = n + 1 then reflexive ct
   708           else
   709             (case try dest_implies ct of
   710               NONE => reflexive ct
   711             | SOME (A, B) => imp_cong_rule (cv i A) (conv (i + 1) B));
   712   in conv 1 end;
   713 
   714 fun goals_conv pred cv = prems_conv ~1 (fn i => if pred i then cv else reflexive);
   715 fun fconv_rule cv th = equal_elim (cv (cprop_of th)) th;
   716 
   717 
   718 (*** Some useful meta-theorems ***)
   719 
   720 (*The rule V/V, obtains assumption solving for eresolve_tac*)
   721 val asm_rl = store_standard_thm_open "asm_rl" (Thm.trivial (read_prop "PROP ?psi"));
   722 val _ = store_thm "_" asm_rl;
   723 
   724 (*Meta-level cut rule: [| V==>W; V |] ==> W *)
   725 val cut_rl =
   726   store_standard_thm_open "cut_rl"
   727     (Thm.trivial (read_prop "PROP ?psi ==> PROP ?theta"));
   728 
   729 (*Generalized elim rule for one conclusion; cut_rl with reversed premises:
   730      [| PROP V;  PROP V ==> PROP W |] ==> PROP W *)
   731 val revcut_rl =
   732   let val V = read_prop "PROP V"
   733       and VW = read_prop "PROP V ==> PROP W";
   734   in
   735     store_standard_thm_open "revcut_rl"
   736       (implies_intr V (implies_intr VW (implies_elim (assume VW) (assume V))))
   737   end;
   738 
   739 (*for deleting an unwanted assumption*)
   740 val thin_rl =
   741   let val V = read_prop "PROP V"
   742       and W = read_prop "PROP W";
   743   in store_standard_thm_open "thin_rl" (implies_intr V (implies_intr W (assume W))) end;
   744 
   745 (* (!!x. PROP ?V) == PROP ?V       Allows removal of redundant parameters*)
   746 val triv_forall_equality =
   747   let val V  = read_prop "PROP V"
   748       and QV = read_prop "!!x::'a. PROP V"
   749       and x  = cert (Free ("x", Term.aT []));
   750   in
   751     store_standard_thm_open "triv_forall_equality"
   752       (equal_intr (implies_intr QV (forall_elim x (assume QV)))
   753         (implies_intr V  (forall_intr x (assume V))))
   754   end;
   755 
   756 (* (PROP ?Phi ==> PROP ?Phi ==> PROP ?Psi) ==>
   757    (PROP ?Phi ==> PROP ?Psi)
   758 *)
   759 val distinct_prems_rl =
   760   let
   761     val AAB = read_prop "PROP Phi ==> PROP Phi ==> PROP Psi"
   762     val A = read_prop "PROP Phi";
   763   in
   764     store_standard_thm_open "distinct_prems_rl"
   765       (implies_intr_list [AAB, A] (implies_elim_list (assume AAB) [assume A, assume A]))
   766   end;
   767 
   768 (* (PROP ?PhiA ==> PROP ?PhiB ==> PROP ?Psi) ==>
   769    (PROP ?PhiB ==> PROP ?PhiA ==> PROP ?Psi)
   770    `thm COMP swap_prems_rl' swaps the first two premises of `thm'
   771 *)
   772 val swap_prems_rl =
   773   let val cmajor = read_prop "PROP PhiA ==> PROP PhiB ==> PROP Psi";
   774       val major = assume cmajor;
   775       val cminor1 = read_prop "PROP PhiA";
   776       val minor1 = assume cminor1;
   777       val cminor2 = read_prop "PROP PhiB";
   778       val minor2 = assume cminor2;
   779   in store_standard_thm_open "swap_prems_rl"
   780        (implies_intr cmajor (implies_intr cminor2 (implies_intr cminor1
   781          (implies_elim (implies_elim major minor1) minor2))))
   782   end;
   783 
   784 (* [| PROP ?phi ==> PROP ?psi; PROP ?psi ==> PROP ?phi |]
   785    ==> PROP ?phi == PROP ?psi
   786    Introduction rule for == as a meta-theorem.
   787 *)
   788 val equal_intr_rule =
   789   let val PQ = read_prop "PROP phi ==> PROP psi"
   790       and QP = read_prop "PROP psi ==> PROP phi"
   791   in
   792     store_standard_thm_open "equal_intr_rule"
   793       (implies_intr PQ (implies_intr QP (equal_intr (assume PQ) (assume QP))))
   794   end;
   795 
   796 (* PROP ?phi == PROP ?psi ==> PROP ?phi ==> PROP ?psi *)
   797 val equal_elim_rule1 =
   798   let val eq = read_prop "PROP phi == PROP psi"
   799       and P = read_prop "PROP phi"
   800   in store_standard_thm_open "equal_elim_rule1"
   801     (Thm.equal_elim (assume eq) (assume P) |> implies_intr_list [eq, P])
   802   end;
   803 
   804 (* PROP ?psi == PROP ?phi ==> PROP ?phi ==> PROP ?psi *)
   805 val equal_elim_rule2 =
   806   store_standard_thm_open "equal_elim_rule2" (symmetric_thm RS equal_elim_rule1);
   807 
   808 (* "[| PROP ?phi; PROP ?phi; PROP ?psi |] ==> PROP ?psi" *)
   809 val remdups_rl =
   810   let val P = read_prop "PROP phi" and Q = read_prop "PROP psi";
   811   in store_standard_thm_open "remdups_rl" (implies_intr_list [P, P, Q] (Thm.assume Q)) end;
   812 
   813 
   814 (*(PROP ?phi ==> (!!x. PROP ?psi(x))) == (!!x. PROP ?phi ==> PROP ?psi(x))
   815   Rewrite rule for HHF normalization.*)
   816 
   817 val norm_hhf_eq =
   818   let
   819     val aT = TFree ("'a", []);
   820     val all = Term.all aT;
   821     val x = Free ("x", aT);
   822     val phi = Free ("phi", propT);
   823     val psi = Free ("psi", aT --> propT);
   824 
   825     val cx = cert x;
   826     val cphi = cert phi;
   827     val lhs = cert (Logic.mk_implies (phi, all $ Abs ("x", aT, psi $ Bound 0)));
   828     val rhs = cert (all $ Abs ("x", aT, Logic.mk_implies (phi, psi $ Bound 0)));
   829   in
   830     Thm.equal_intr
   831       (Thm.implies_elim (Thm.assume lhs) (Thm.assume cphi)
   832         |> Thm.forall_elim cx
   833         |> Thm.implies_intr cphi
   834         |> Thm.forall_intr cx
   835         |> Thm.implies_intr lhs)
   836       (Thm.implies_elim
   837           (Thm.assume rhs |> Thm.forall_elim cx) (Thm.assume cphi)
   838         |> Thm.forall_intr cx
   839         |> Thm.implies_intr cphi
   840         |> Thm.implies_intr rhs)
   841     |> store_standard_thm_open "norm_hhf_eq"
   842   end;
   843 
   844 val norm_hhf_prop = Logic.dest_equals (Thm.prop_of norm_hhf_eq);
   845 
   846 fun is_norm_hhf tm =
   847   let
   848     fun is_norm (Const ("==>", _) $ _ $ (Const ("all", _) $ _)) = false
   849       | is_norm (t $ u) = is_norm t andalso is_norm u
   850       | is_norm (Abs (_, _, t)) = is_norm t
   851       | is_norm _ = true;
   852   in is_norm (Envir.beta_eta_contract tm) end;
   853 
   854 fun norm_hhf thy t =
   855   if is_norm_hhf t then t
   856   else Pattern.rewrite_term thy [norm_hhf_prop] [] t;
   857 
   858 fun norm_hhf_cterm ct =
   859   if is_norm_hhf (Thm.term_of ct) then ct
   860   else cterm_fun (Pattern.rewrite_term (Thm.theory_of_cterm ct) [norm_hhf_prop] []) ct;
   861 
   862 
   863 
   864 (*** Instantiate theorem th, reading instantiations in theory thy ****)
   865 
   866 (*Version that normalizes the result: Thm.instantiate no longer does that*)
   867 fun instantiate instpair th = Thm.instantiate instpair th  COMP   asm_rl;
   868 
   869 fun read_instantiate_sg' thy sinsts th =
   870     let val ts = types_sorts th;
   871         val used = add_used th [];
   872     in  instantiate (read_insts thy ts ts used sinsts) th  end;
   873 
   874 fun read_instantiate_sg thy sinsts th =
   875   read_instantiate_sg' thy (map (apfst Syntax.read_indexname) sinsts) th;
   876 
   877 (*Instantiate theorem th, reading instantiations under theory of th*)
   878 fun read_instantiate sinsts th =
   879     read_instantiate_sg (Thm.theory_of_thm th) sinsts th;
   880 
   881 fun read_instantiate' sinsts th =
   882     read_instantiate_sg' (Thm.theory_of_thm th) sinsts th;
   883 
   884 
   885 (*Left-to-right replacements: tpairs = [...,(vi,ti),...].
   886   Instantiates distinct Vars by terms, inferring type instantiations. *)
   887 local
   888   fun add_types ((ct,cu), (thy,tye,maxidx)) =
   889     let val {thy=thyt, t=t, T= T, maxidx=maxt,...} = rep_cterm ct
   890         and {thy=thyu, t=u, T= U, maxidx=maxu,...} = rep_cterm cu;
   891         val maxi = Int.max(maxidx, Int.max(maxt, maxu));
   892         val thy' = Theory.merge(thy, Theory.merge(thyt, thyu))
   893         val (tye',maxi') = Sign.typ_unify thy' (T, U) (tye, maxi)
   894           handle Type.TUNIFY => raise TYPE("Ill-typed instantiation", [T,U], [t,u])
   895     in  (thy', tye', maxi')  end;
   896 in
   897 fun cterm_instantiate ctpairs0 th =
   898   let val (thy,tye,_) = foldr add_types (Thm.theory_of_thm th, Vartab.empty, 0) ctpairs0
   899       fun instT(ct,cu) =
   900         let val inst = cterm_of thy o Envir.subst_TVars tye o term_of
   901         in (inst ct, inst cu) end
   902       fun ctyp2 (ixn, (S, T)) = (ctyp_of thy (TVar (ixn, S)), ctyp_of thy T)
   903   in  instantiate (map ctyp2 (Vartab.dest tye), map instT ctpairs0) th  end
   904   handle TERM _ =>
   905            raise THM("cterm_instantiate: incompatible theories",0,[th])
   906        | TYPE (msg, _, _) => raise THM(msg, 0, [th])
   907 end;
   908 
   909 
   910 (* global schematic variables *)
   911 
   912 fun unvarify th =
   913   let
   914     val thy = Thm.theory_of_thm th;
   915     val cert = Thm.cterm_of thy;
   916     val certT = Thm.ctyp_of thy;
   917 
   918     val prop = Thm.full_prop_of th;
   919     val _ = map Logic.unvarify (prop :: Thm.hyps_of th)
   920       handle TERM (msg, _) => raise THM (msg, 0, [th]);
   921 
   922     val instT0 = rev (Term.add_tvars prop []) |> map (fn v as ((a, _), S) => (v, TFree (a, S)));
   923     val instT = map (fn (v, T) => (certT (TVar v), certT T)) instT0;
   924     val inst = rev (Term.add_vars prop []) |> map (fn ((a, i), T) =>
   925       let val T' = TermSubst.instantiateT instT0 T
   926       in (cert (Var ((a, i), T')), cert (Free ((a, T')))) end);
   927   in Thm.instantiate (instT, inst) th end;
   928 
   929 
   930 (** protected propositions and embedded terms **)
   931 
   932 local
   933   val A = cert (Free ("A", propT));
   934   val prop_def = unvarify ProtoPure.prop_def;
   935   val term_def = unvarify ProtoPure.term_def;
   936 in
   937   val protect = Thm.capply (cert Logic.protectC);
   938   val protectI = store_thm "protectI" (PureThy.kind_rule Thm.internalK (standard
   939       (Thm.equal_elim (Thm.symmetric prop_def) (Thm.assume A))));
   940   val protectD = store_thm "protectD" (PureThy.kind_rule Thm.internalK (standard
   941       (Thm.equal_elim prop_def (Thm.assume (protect A)))));
   942   val protect_cong = store_standard_thm_open "protect_cong" (Thm.reflexive (protect A));
   943 
   944   val termI = store_thm "termI" (PureThy.kind_rule Thm.internalK (standard
   945       (Thm.equal_elim (Thm.symmetric term_def) (Thm.forall_intr A (Thm.trivial A)))));
   946 end;
   947 
   948 fun implies_intr_protected asms th =
   949   let val asms' = map protect asms in
   950     implies_elim_list
   951       (implies_intr_list asms th)
   952       (map (fn asm' => Thm.assume asm' RS protectD) asms')
   953     |> implies_intr_list asms'
   954   end;
   955 
   956 fun mk_term ct =
   957   let
   958     val {thy, T, ...} = Thm.rep_cterm ct;
   959     val cert = Thm.cterm_of thy;
   960     val certT = Thm.ctyp_of thy;
   961     val a = certT (TVar (("'a", 0), []));
   962     val x = cert (Var (("x", 0), T));
   963   in Thm.instantiate ([(a, certT T)], [(x, ct)]) termI end;
   964 
   965 fun dest_term th =
   966   let val cprop = strip_imp_concl (Thm.cprop_of th) in
   967     if can Logic.dest_term (Thm.term_of cprop) then
   968       Thm.dest_arg cprop
   969     else raise THM ("dest_term", 0, [th])
   970   end;
   971 
   972 fun cterm_rule f = dest_term o f o mk_term;
   973 fun term_rule thy f t = Thm.term_of (cterm_rule f (Thm.cterm_of thy t));
   974 
   975 
   976 
   977 (** variations on instantiate **)
   978 
   979 (*shorthand for instantiating just one variable in the current theory*)
   980 fun inst x t = read_instantiate_sg (the_context()) [(x,t)];
   981 
   982 
   983 (* instantiate by left-to-right occurrence of variables *)
   984 
   985 fun instantiate' cTs cts thm =
   986   let
   987     fun err msg =
   988       raise TYPE ("instantiate': " ^ msg,
   989         map_filter (Option.map Thm.typ_of) cTs,
   990         map_filter (Option.map Thm.term_of) cts);
   991 
   992     fun inst_of (v, ct) =
   993       (Thm.cterm_of (Thm.theory_of_cterm ct) (Var v), ct)
   994         handle TYPE (msg, _, _) => err msg;
   995 
   996     fun tyinst_of (v, cT) =
   997       (Thm.ctyp_of (Thm.theory_of_ctyp cT) (TVar v), cT)
   998         handle TYPE (msg, _, _) => err msg;
   999 
  1000     fun zip_vars xs ys =
  1001       zip_options xs ys handle Library.UnequalLengths =>
  1002         err "more instantiations than variables in thm";
  1003 
  1004     (*instantiate types first!*)
  1005     val thm' =
  1006       if forall is_none cTs then thm
  1007       else Thm.instantiate
  1008         (map tyinst_of (zip_vars (rev (fold_terms Term.add_tvars thm [])) cTs), []) thm;
  1009     val thm'' =
  1010       if forall is_none cts then thm'
  1011       else Thm.instantiate
  1012         ([], map inst_of (zip_vars (rev (fold_terms Term.add_vars thm' [])) cts)) thm';
  1013     in thm'' end;
  1014 
  1015 
  1016 
  1017 (** renaming of bound variables **)
  1018 
  1019 (* replace bound variables x_i in thm by y_i *)
  1020 (* where vs = [(x_1, y_1), ..., (x_n, y_n)]  *)
  1021 
  1022 fun rename_bvars [] thm = thm
  1023   | rename_bvars vs thm =
  1024     let
  1025       val {thy, prop, ...} = rep_thm thm;
  1026       fun ren (Abs (x, T, t)) = Abs (AList.lookup (op =) vs x |> the_default x, T, ren t)
  1027         | ren (t $ u) = ren t $ ren u
  1028         | ren t = t;
  1029     in equal_elim (reflexive (cterm_of thy (ren prop))) thm end;
  1030 
  1031 
  1032 (* renaming in left-to-right order *)
  1033 
  1034 fun rename_bvars' xs thm =
  1035   let
  1036     val {thy, prop, ...} = rep_thm thm;
  1037     fun rename [] t = ([], t)
  1038       | rename (x' :: xs) (Abs (x, T, t)) =
  1039           let val (xs', t') = rename xs t
  1040           in (xs', Abs (the_default x x', T, t')) end
  1041       | rename xs (t $ u) =
  1042           let
  1043             val (xs', t') = rename xs t;
  1044             val (xs'', u') = rename xs' u
  1045           in (xs'', t' $ u') end
  1046       | rename xs t = (xs, t);
  1047   in case rename xs prop of
  1048       ([], prop') => equal_elim (reflexive (cterm_of thy prop')) thm
  1049     | _ => error "More names than abstractions in theorem"
  1050   end;
  1051 
  1052 
  1053 (* var indexes *)
  1054 
  1055 fun incr_indexes th = Thm.incr_indexes (Thm.maxidx_of th + 1);
  1056 
  1057 fun incr_indexes2 th1 th2 =
  1058   Thm.incr_indexes (Int.max (Thm.maxidx_of th1, Thm.maxidx_of th2) + 1);
  1059 
  1060 
  1061 
  1062 (** multi_resolve **)
  1063 
  1064 local
  1065 
  1066 fun res th i rule =
  1067   Thm.biresolution false [(false, th)] i rule handle THM _ => Seq.empty;
  1068 
  1069 fun multi_res _ [] rule = Seq.single rule
  1070   | multi_res i (th :: ths) rule = Seq.maps (res th i) (multi_res (i + 1) ths rule);
  1071 
  1072 in
  1073 
  1074 val multi_resolve = multi_res 1;
  1075 fun multi_resolves facts rules = Seq.maps (multi_resolve facts) (Seq.of_list rules);
  1076 
  1077 end;
  1078 
  1079 end;
  1080 
  1081 structure BasicDrule: BASIC_DRULE = Drule;
  1082 open BasicDrule;