src/Pure/drule.ML
author berghofe
Tue Oct 26 16:33:09 2004 +0200 (2004-10-26)
changeset 15262 49f70168f4c0
parent 15001 fb2141a9f8c0
child 15442 3b75e1b22ff1
permissions -rw-r--r--
Added function strip_type (for ctyps).
     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 strip_imp_prems   : cterm -> cterm list
    18   val strip_imp_concl   : cterm -> cterm
    19   val cprems_of         : thm -> cterm list
    20   val read_insts        :
    21           Sign.sg -> (indexname -> typ option) * (indexname -> sort option)
    22                   -> (indexname -> typ option) * (indexname -> sort option)
    23                   -> string list -> (string*string)list
    24                   -> (indexname*ctyp)list * (cterm*cterm)list
    25   val types_sorts: thm -> (indexname-> typ option) * (indexname-> sort option)
    26   val strip_shyps_warning : thm -> thm
    27   val forall_intr_list  : cterm list -> thm -> thm
    28   val forall_intr_frees : thm -> thm
    29   val forall_intr_vars  : thm -> thm
    30   val forall_elim_list  : cterm list -> thm -> thm
    31   val forall_elim_var   : int -> thm -> thm
    32   val forall_elim_vars  : int -> thm -> thm
    33   val gen_all           : thm -> thm
    34   val freeze_thaw       : thm -> thm * (thm -> thm)
    35   val implies_elim_list : thm -> thm list -> thm
    36   val implies_intr_list : cterm list -> thm -> thm
    37   val instantiate       :
    38     (indexname * ctyp) list * (cterm * cterm) list -> thm -> thm
    39   val zero_var_indexes  : thm -> thm
    40   val standard          : thm -> thm
    41   val standard'         : thm -> thm
    42   val rotate_prems      : int -> thm -> thm
    43   val rearrange_prems   : int list -> thm -> thm
    44   val assume_ax         : theory -> string -> thm
    45   val RSN               : thm * (int * thm) -> thm
    46   val RS                : thm * thm -> thm
    47   val RLN               : thm list * (int * thm list) -> thm list
    48   val RL                : thm list * thm list -> thm list
    49   val MRS               : thm list * thm -> thm
    50   val MRL               : thm list list * thm list -> thm list
    51   val OF                : thm * thm list -> thm
    52   val compose           : thm * int * thm -> thm list
    53   val COMP              : thm * thm -> thm
    54   val read_instantiate_sg: Sign.sg -> (string*string)list -> thm -> thm
    55   val read_instantiate  : (string*string)list -> thm -> thm
    56   val cterm_instantiate : (cterm*cterm)list -> thm -> thm
    57   val eq_thm_sg         : thm * thm -> bool
    58   val eq_thm_prop	: thm * thm -> bool
    59   val weak_eq_thm       : thm * thm -> bool
    60   val size_of_thm       : thm -> int
    61   val reflexive_thm     : thm
    62   val symmetric_thm     : thm
    63   val transitive_thm    : thm
    64   val symmetric_fun     : thm -> thm
    65   val extensional       : thm -> thm
    66   val imp_cong          : thm
    67   val swap_prems_eq     : thm
    68   val equal_abs_elim    : cterm  -> thm -> thm
    69   val equal_abs_elim_list: cterm list -> thm -> thm
    70   val asm_rl            : thm
    71   val cut_rl            : thm
    72   val revcut_rl         : thm
    73   val thin_rl           : thm
    74   val triv_forall_equality: thm
    75   val swap_prems_rl     : thm
    76   val equal_intr_rule   : thm
    77   val equal_elim_rule1  : thm
    78   val inst              : string -> string -> thm -> thm
    79   val instantiate'      : ctyp option list -> cterm option list -> thm -> thm
    80   val incr_indexes_wrt  : int list -> ctyp list -> cterm list -> thm list -> thm -> thm
    81 end;
    82 
    83 signature DRULE =
    84 sig
    85   include BASIC_DRULE
    86   val strip_comb: cterm -> cterm * cterm list
    87   val strip_type: ctyp -> ctyp list * ctyp
    88   val rule_attribute: ('a -> thm -> thm) -> 'a attribute
    89   val tag_rule: tag -> thm -> thm
    90   val untag_rule: string -> thm -> thm
    91   val tag: tag -> 'a attribute
    92   val untag: string -> 'a attribute
    93   val get_kind: thm -> string
    94   val kind: string -> 'a attribute
    95   val theoremK: string
    96   val lemmaK: string
    97   val corollaryK: string
    98   val internalK: string
    99   val kind_internal: 'a attribute
   100   val has_internal: tag list -> bool
   101   val impose_hyps: cterm list -> thm -> thm
   102   val satisfy_hyps: thm list -> thm -> thm
   103   val close_derivation: thm -> thm
   104   val local_standard: 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 add_rules: thm list -> thm list -> thm list
   109   val del_rules: thm list -> thm list -> thm list
   110   val merge_rules: thm list * thm list -> thm list
   111   val imp_cong'         : thm -> thm -> thm
   112   val beta_eta_conversion: cterm -> thm
   113   val goals_conv        : (int -> bool) -> (cterm -> thm) -> cterm -> thm
   114   val forall_conv       : (cterm -> thm) -> cterm -> thm
   115   val fconv_rule        : (cterm -> thm) -> thm -> thm
   116   val norm_hhf_eq: thm
   117   val is_norm_hhf: term -> bool
   118   val norm_hhf: Sign.sg -> term -> term
   119   val triv_goal: thm
   120   val rev_triv_goal: thm
   121   val implies_intr_goals: cterm list -> thm -> thm
   122   val freeze_all: thm -> thm
   123   val mk_triv_goal: cterm -> thm
   124   val tvars_of_terms: term list -> (indexname * sort) list
   125   val vars_of_terms: term list -> (indexname * typ) list
   126   val tvars_of: thm -> (indexname * sort) list
   127   val vars_of: thm -> (indexname * typ) list
   128   val rename_bvars: (string * string) list -> thm -> thm
   129   val rename_bvars': string option list -> thm -> thm
   130   val unvarifyT: thm -> thm
   131   val unvarify: thm -> thm
   132   val tvars_intr_list: string list -> thm -> thm * (string * indexname) list
   133   val remdups_rl: thm
   134   val conj_intr: thm -> thm -> thm
   135   val conj_intr_list: thm list -> thm
   136   val conj_elim: thm -> thm * thm
   137   val conj_elim_list: thm -> thm list
   138   val conj_elim_precise: int -> thm -> thm list
   139   val conj_intr_thm: thm
   140   val abs_def: thm -> thm
   141 end;
   142 
   143 structure Drule: DRULE =
   144 struct
   145 
   146 
   147 (** some cterm->cterm operations: much faster than calling cterm_of! **)
   148 
   149 (** SAME NAMES as in structure Logic: use compound identifiers! **)
   150 
   151 (*dest_implies for cterms. Note T=prop below*)
   152 fun dest_implies ct =
   153     case term_of ct of
   154         (Const("==>", _) $ _ $ _) =>
   155             let val (ct1,ct2) = Thm.dest_comb ct
   156             in  (#2 (Thm.dest_comb ct1), ct2)  end
   157       | _ => raise TERM ("dest_implies", [term_of ct]) ;
   158 
   159 fun dest_equals ct =
   160     case term_of ct of
   161         (Const("==", _) $ _ $ _) =>
   162             let val (ct1,ct2) = Thm.dest_comb ct
   163             in  (#2 (Thm.dest_comb ct1), ct2)  end
   164       | _ => raise TERM ("dest_equals", [term_of ct]) ;
   165 
   166 
   167 (* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
   168 fun strip_imp_prems ct =
   169     let val (cA,cB) = dest_implies ct
   170     in  cA :: strip_imp_prems cB  end
   171     handle TERM _ => [];
   172 
   173 (* A1==>...An==>B  goes to B, where B is not an implication *)
   174 fun strip_imp_concl ct =
   175     case term_of ct of (Const("==>", _) $ _ $ _) =>
   176         strip_imp_concl (#2 (Thm.dest_comb ct))
   177   | _ => ct;
   178 
   179 (*The premises of a theorem, as a cterm list*)
   180 val cprems_of = strip_imp_prems o cprop_of;
   181 
   182 val proto_sign = Theory.sign_of ProtoPure.thy;
   183 
   184 val implies = cterm_of proto_sign Term.implies;
   185 
   186 (*cterm version of mk_implies*)
   187 fun mk_implies(A,B) = Thm.capply (Thm.capply implies A) B;
   188 
   189 (*cterm version of list_implies: [A1,...,An], B  goes to [|A1;==>;An|]==>B *)
   190 fun list_implies([], B) = B
   191   | list_implies(A::AS, B) = mk_implies (A, list_implies(AS,B));
   192 
   193 (*cterm version of strip_comb: maps  f(t1,...,tn)  to  (f, [t1,...,tn]) *)
   194 fun strip_comb ct = 
   195   let
   196     fun stripc (p as (ct, cts)) =
   197       let val (ct1, ct2) = Thm.dest_comb ct
   198       in stripc (ct1, ct2 :: cts) end handle CTERM _ => p
   199   in stripc (ct, []) end;
   200 
   201 (* cterm version of strip_type: maps  [T1,...,Tn]--->T  to   ([T1,T2,...,Tn], T) *)
   202 fun strip_type cT = (case Thm.typ_of cT of
   203     Type ("fun", _) =>
   204       let
   205         val [cT1, cT2] = Thm.dest_ctyp cT;
   206         val (cTs, cT') = strip_type cT2
   207       in (cT1 :: cTs, cT') end
   208   | _ => ([], cT));
   209 
   210 
   211 (** reading of instantiations **)
   212 
   213 fun absent ixn =
   214   error("No such variable in term: " ^ Syntax.string_of_vname ixn);
   215 
   216 fun inst_failure ixn =
   217   error("Instantiation of " ^ Syntax.string_of_vname ixn ^ " fails");
   218 
   219 fun read_insts sign (rtypes,rsorts) (types,sorts) used insts =
   220 let
   221     fun split([],tvs,vs) = (tvs,vs)
   222       | split((sv,st)::l,tvs,vs) = (case Symbol.explode sv of
   223                   "'"::cs => split(l,(Syntax.indexname cs,st)::tvs,vs)
   224                 | cs => split(l,tvs,(Syntax.indexname cs,st)::vs));
   225     val (tvs,vs) = split(insts,[],[]);
   226     fun readT((a,i),st) =
   227         let val ixn = ("'" ^ a,i);
   228             val S = case rsorts ixn of Some S => S | None => absent ixn;
   229             val T = Sign.read_typ (sign,sorts) st;
   230         in if Sign.typ_instance sign (T, TVar(ixn,S)) then (ixn,T)
   231            else inst_failure ixn
   232         end
   233     val tye = map readT tvs;
   234     fun mkty(ixn,st) = (case rtypes ixn of
   235                           Some T => (ixn,(st,typ_subst_TVars tye T))
   236                         | None => absent ixn);
   237     val ixnsTs = map mkty vs;
   238     val ixns = map fst ixnsTs
   239     and sTs  = map snd ixnsTs
   240     val (cts,tye2) = read_def_cterms(sign,types,sorts) used false sTs;
   241     fun mkcVar(ixn,T) =
   242         let val U = typ_subst_TVars tye2 T
   243         in cterm_of sign (Var(ixn,U)) end
   244     val ixnTs = ListPair.zip(ixns, map snd sTs)
   245 in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) (tye2 @ tye),
   246     ListPair.zip(map mkcVar ixnTs,cts))
   247 end;
   248 
   249 
   250 (*** Find the type (sort) associated with a (T)Var or (T)Free in a term
   251      Used for establishing default types (of variables) and sorts (of
   252      type variables) when reading another term.
   253      Index -1 indicates that a (T)Free rather than a (T)Var is wanted.
   254 ***)
   255 
   256 fun types_sorts thm =
   257     let val {prop,hyps,...} = rep_thm thm;
   258         val big = list_comb(prop,hyps); (* bogus term! *)
   259         val vars = map dest_Var (term_vars big);
   260         val frees = map dest_Free (term_frees big);
   261         val tvars = term_tvars big;
   262         val tfrees = term_tfrees big;
   263         fun typ(a,i) = if i<0 then assoc(frees,a) else assoc(vars,(a,i));
   264         fun sort(a,i) = if i<0 then assoc(tfrees,a) else assoc(tvars,(a,i));
   265     in (typ,sort) end;
   266 
   267 
   268 
   269 (** basic attributes **)
   270 
   271 (* dependent rules *)
   272 
   273 fun rule_attribute f (x, thm) = (x, (f x thm));
   274 
   275 
   276 (* add / delete tags *)
   277 
   278 fun map_tags f thm =
   279   Thm.put_name_tags (Thm.name_of_thm thm, f (#2 (Thm.get_name_tags thm))) thm;
   280 
   281 fun tag_rule tg = map_tags (fn tgs => if tg mem tgs then tgs else tgs @ [tg]);
   282 fun untag_rule s = map_tags (filter_out (equal s o #1));
   283 
   284 fun tag tg x = rule_attribute (K (tag_rule tg)) x;
   285 fun untag s x = rule_attribute (K (untag_rule s)) x;
   286 
   287 fun simple_tag name x = tag (name, []) x;
   288 
   289 
   290 (* theorem kinds *)
   291 
   292 val theoremK = "theorem";
   293 val lemmaK = "lemma";
   294 val corollaryK = "corollary";
   295 val internalK = "internal";
   296 
   297 fun get_kind thm =
   298   (case Library.assoc (#2 (Thm.get_name_tags thm), "kind") of
   299     Some (k :: _) => k
   300   | _ => "unknown");
   301 
   302 fun kind_rule k = tag_rule ("kind", [k]) o untag_rule "kind";
   303 fun kind k x = if k = "" then x else rule_attribute (K (kind_rule k)) x;
   304 fun kind_internal x = kind internalK x;
   305 fun has_internal tags = exists (equal internalK o fst) tags;
   306 
   307 
   308 
   309 (** Standardization of rules **)
   310 
   311 (*Strip extraneous shyps as far as possible*)
   312 fun strip_shyps_warning thm =
   313   let
   314     val str_of_sort = Pretty.str_of o Sign.pretty_sort (Thm.sign_of_thm thm);
   315     val thm' = Thm.strip_shyps thm;
   316     val xshyps = Thm.extra_shyps thm';
   317   in
   318     if null xshyps then ()
   319     else warning ("Pending sort hypotheses: " ^ commas (map str_of_sort xshyps));
   320     thm'
   321   end;
   322 
   323 (*Generalization over a list of variables, IGNORING bad ones*)
   324 fun forall_intr_list [] th = th
   325   | forall_intr_list (y::ys) th =
   326         let val gth = forall_intr_list ys th
   327         in  forall_intr y gth   handle THM _ =>  gth  end;
   328 
   329 (*Generalization over all suitable Free variables*)
   330 fun forall_intr_frees th =
   331     let val {prop,sign,...} = rep_thm th
   332     in  forall_intr_list
   333          (map (cterm_of sign) (sort (make_ord atless) (term_frees prop)))
   334          th
   335     end;
   336 
   337 val forall_elim_var = PureThy.forall_elim_var;
   338 val forall_elim_vars = PureThy.forall_elim_vars;
   339 
   340 fun gen_all thm =
   341   let
   342     val {sign, prop, maxidx, ...} = Thm.rep_thm thm;
   343     fun elim (th, (x, T)) = Thm.forall_elim (Thm.cterm_of sign (Var ((x, maxidx + 1), T))) th;
   344     val vs = Term.strip_all_vars prop;
   345   in foldl elim (thm, Term.variantlist (map #1 vs, []) ~~ map #2 vs) end;
   346 
   347 (*Specialization over a list of cterms*)
   348 fun forall_elim_list cts th = foldr (uncurry forall_elim) (rev cts, th);
   349 
   350 (* maps A1,...,An |- B   to   [| A1;...;An |] ==> B  *)
   351 fun implies_intr_list cAs th = foldr (uncurry implies_intr) (cAs,th);
   352 
   353 (* maps [| A1;...;An |] ==> B and [A1,...,An]   to   B *)
   354 fun implies_elim_list impth ths = foldl (uncurry implies_elim) (impth,ths);
   355 
   356 (* maps |- B to A1,...,An |- B *)
   357 fun impose_hyps chyps th =
   358   let val chyps' = gen_rems (op aconv o apfst Thm.term_of) (chyps, #hyps (Thm.rep_thm th))
   359   in implies_elim_list (implies_intr_list chyps' th) (map Thm.assume chyps') end;
   360 
   361 (* maps A1,...,An and A1,...,An |- B to |- B *)
   362 fun satisfy_hyps ths th =
   363   implies_elim_list (implies_intr_list (map (#prop o Thm.crep_thm) ths) th) ths;
   364 
   365 (*Reset Var indexes to zero, renaming to preserve distinctness*)
   366 fun zero_var_indexes th =
   367     let val {prop,sign,...} = rep_thm th;
   368         val vars = term_vars prop
   369         val bs = foldl add_new_id ([], map (fn Var((a,_),_)=>a) vars)
   370         val inrs = add_term_tvars(prop,[]);
   371         val nms' = rev(foldl add_new_id ([], map (#1 o #1) inrs));
   372         val tye = ListPair.map (fn ((v,rs),a) => (v, TVar((a,0),rs)))
   373                      (inrs, nms')
   374         val ctye = map (fn (v,T) => (v,ctyp_of sign T)) tye;
   375         fun varpairs([],[]) = []
   376           | varpairs((var as Var(v,T)) :: vars, b::bs) =
   377                 let val T' = typ_subst_TVars tye T
   378                 in (cterm_of sign (Var(v,T')),
   379                     cterm_of sign (Var((b,0),T'))) :: varpairs(vars,bs)
   380                 end
   381           | varpairs _ = raise TERM("varpairs", []);
   382     in Thm.instantiate (ctye, varpairs(vars,rev bs)) th end;
   383 
   384 
   385 (** Standard form of object-rule: no hypotheses, flexflex constraints,
   386     Frees, or outer quantifiers; all generality expressed by Vars of index 0.**)
   387 
   388 (*Squash a theorem's flexflex constraints provided it can be done uniquely.
   389   This step can lose information.*)
   390 fun flexflex_unique th =
   391     case Seq.chop (2, flexflex_rule th) of
   392       ([th],_) => th
   393     | ([],_)   => raise THM("flexflex_unique: impossible constraints", 0, [th])
   394     |      _   => raise THM("flexflex_unique: multiple unifiers", 0, [th]);
   395 
   396 fun close_derivation thm =
   397   if Thm.get_name_tags thm = ("", []) then Thm.name_thm ("", thm)
   398   else thm;
   399 
   400 fun standard' th =
   401   let val {maxidx,...} = rep_thm th in
   402     th
   403     |> implies_intr_hyps
   404     |> forall_intr_frees |> forall_elim_vars (maxidx + 1)
   405     |> strip_shyps_warning
   406     |> zero_var_indexes |> Thm.varifyT |> Thm.compress
   407   end;
   408 
   409 val standard = close_derivation o standard' o flexflex_unique;
   410 
   411 fun local_standard th =
   412   th |> strip_shyps |> zero_var_indexes
   413   |> Thm.compress |> close_derivation;
   414 
   415 
   416 (*Convert all Vars in a theorem to Frees.  Also return a function for
   417   reversing that operation.  DOES NOT WORK FOR TYPE VARIABLES.
   418   Similar code in type/freeze_thaw*)
   419 fun freeze_thaw th =
   420  let val fth = freezeT th
   421      val {prop, tpairs, sign, ...} = rep_thm fth
   422  in
   423    case foldr add_term_vars (prop :: Thm.terms_of_tpairs tpairs, []) of
   424        [] => (fth, fn x => x)
   425      | vars =>
   426          let fun newName (Var(ix,_), (pairs,used)) =
   427                    let val v = variant used (string_of_indexname ix)
   428                    in  ((ix,v)::pairs, v::used)  end;
   429              val (alist, _) = foldr newName (vars, ([], foldr add_term_names
   430                (prop :: Thm.terms_of_tpairs tpairs, [])))
   431              fun mk_inst (Var(v,T)) =
   432                  (cterm_of sign (Var(v,T)),
   433                   cterm_of sign (Free(the (assoc(alist,v)), T)))
   434              val insts = map mk_inst vars
   435              fun thaw th' =
   436                  th' |> forall_intr_list (map #2 insts)
   437                      |> forall_elim_list (map #1 insts)
   438          in  (Thm.instantiate ([],insts) fth, thaw)  end
   439  end;
   440 
   441 
   442 (*Rotates a rule's premises to the left by k*)
   443 val rotate_prems = permute_prems 0;
   444 
   445 (* permute prems, where the i-th position in the argument list (counting from 0)
   446    gives the position within the original thm to be transferred to position i.
   447    Any remaining trailing positions are left unchanged. *)
   448 val rearrange_prems = let
   449   fun rearr new []      thm = thm
   450   |   rearr new (p::ps) thm = rearr (new+1)
   451      (map (fn q => if new<=q andalso q<p then q+1 else q) ps)
   452      (permute_prems (new+1) (new-p) (permute_prems new (p-new) thm))
   453   in rearr 0 end;
   454 
   455 (*Assume a new formula, read following the same conventions as axioms.
   456   Generalizes over Free variables,
   457   creates the assumption, and then strips quantifiers.
   458   Example is [| ALL x:?A. ?P(x) |] ==> [| ?P(?a) |]
   459              [ !(A,P,a)[| ALL x:A. P(x) |] ==> [| P(a) |] ]    *)
   460 fun assume_ax thy sP =
   461     let val sign = Theory.sign_of thy
   462         val prop = Logic.close_form (term_of (read_cterm sign (sP, propT)))
   463     in forall_elim_vars 0 (assume (cterm_of sign prop))  end;
   464 
   465 (*Resolution: exactly one resolvent must be produced.*)
   466 fun tha RSN (i,thb) =
   467   case Seq.chop (2, biresolution false [(false,tha)] i thb) of
   468       ([th],_) => th
   469     | ([],_)   => raise THM("RSN: no unifiers", i, [tha,thb])
   470     |      _   => raise THM("RSN: multiple unifiers", i, [tha,thb]);
   471 
   472 (*resolution: P==>Q, Q==>R gives P==>R. *)
   473 fun tha RS thb = tha RSN (1,thb);
   474 
   475 (*For joining lists of rules*)
   476 fun thas RLN (i,thbs) =
   477   let val resolve = biresolution false (map (pair false) thas) i
   478       fun resb thb = Seq.list_of (resolve thb) handle THM _ => []
   479   in  List.concat (map resb thbs)  end;
   480 
   481 fun thas RL thbs = thas RLN (1,thbs);
   482 
   483 (*Resolve a list of rules against bottom_rl from right to left;
   484   makes proof trees*)
   485 fun rls MRS bottom_rl =
   486   let fun rs_aux i [] = bottom_rl
   487         | rs_aux i (rl::rls) = rl RSN (i, rs_aux (i+1) rls)
   488   in  rs_aux 1 rls  end;
   489 
   490 (*As above, but for rule lists*)
   491 fun rlss MRL bottom_rls =
   492   let fun rs_aux i [] = bottom_rls
   493         | rs_aux i (rls::rlss) = rls RLN (i, rs_aux (i+1) rlss)
   494   in  rs_aux 1 rlss  end;
   495 
   496 (*A version of MRS with more appropriate argument order*)
   497 fun bottom_rl OF rls = rls MRS bottom_rl;
   498 
   499 (*compose Q and [...,Qi,Q(i+1),...]==>R to [...,Q(i+1),...]==>R
   500   with no lifting or renaming!  Q may contain ==> or meta-quants
   501   ALWAYS deletes premise i *)
   502 fun compose(tha,i,thb) =
   503     Seq.list_of (bicompose false (false,tha,0) i thb);
   504 
   505 fun compose_single (tha,i,thb) =
   506   (case compose (tha,i,thb) of
   507     [th] => th
   508   | _ => raise THM ("compose: unique result expected", i, [tha,thb]));
   509 
   510 (*compose Q and [Q1,Q2,...,Qk]==>R to [Q2,...,Qk]==>R getting unique result*)
   511 fun tha COMP thb =
   512     case compose(tha,1,thb) of
   513         [th] => th
   514       | _ =>   raise THM("COMP", 1, [tha,thb]);
   515 
   516 
   517 (** theorem equality **)
   518 
   519 (*True if the two theorems have the same signature.*)
   520 val eq_thm_sg = Sign.eq_sg o pairself Thm.sign_of_thm;
   521 
   522 (*True if the two theorems have the same prop field, ignoring hyps, der, etc.*)
   523 val eq_thm_prop = op aconv o pairself Thm.prop_of;
   524 
   525 (*Useful "distance" function for BEST_FIRST*)
   526 val size_of_thm = size_of_term o prop_of;
   527 
   528 (*maintain lists of theorems --- preserving canonical order*)
   529 fun del_rules rs rules = Library.gen_rems eq_thm_prop (rules, rs);
   530 fun add_rules rs rules = rs @ del_rules rs rules;
   531 val del_rule = del_rules o single;
   532 val add_rule = add_rules o single;
   533 fun merge_rules (rules1, rules2) = gen_merge_lists' eq_thm_prop rules1 rules2;
   534 
   535 (** Mark Staples's weaker version of eq_thm: ignores variable renaming and
   536     (some) type variable renaming **)
   537 
   538  (* Can't use term_vars, because it sorts the resulting list of variable names.
   539     We instead need the unique list noramlised by the order of appearance
   540     in the term. *)
   541 fun term_vars' (t as Var(v,T)) = [t]
   542   | term_vars' (Abs(_,_,b)) = term_vars' b
   543   | term_vars' (f$a) = (term_vars' f) @ (term_vars' a)
   544   | term_vars' _ = [];
   545 
   546 fun forall_intr_vars th =
   547   let val {prop,sign,...} = rep_thm th;
   548       val vars = distinct (term_vars' prop);
   549   in forall_intr_list (map (cterm_of sign) vars) th end;
   550 
   551 val weak_eq_thm = Thm.eq_thm o pairself (forall_intr_vars o freezeT);
   552 
   553 
   554 (*** Meta-Rewriting Rules ***)
   555 
   556 fun read_prop s = read_cterm proto_sign (s, propT);
   557 
   558 fun store_thm name thm = hd (PureThy.smart_store_thms (name, [thm]));
   559 fun store_standard_thm name thm = store_thm name (standard thm);
   560 fun store_thm_open name thm = hd (PureThy.smart_store_thms_open (name, [thm]));
   561 fun store_standard_thm_open name thm = store_thm_open name (standard' thm);
   562 
   563 val reflexive_thm =
   564   let val cx = cterm_of proto_sign (Var(("x",0),TVar(("'a",0),[])))
   565   in store_standard_thm_open "reflexive" (Thm.reflexive cx) end;
   566 
   567 val symmetric_thm =
   568   let val xy = read_prop "x == y"
   569   in store_standard_thm_open "symmetric" (Thm.implies_intr_hyps (Thm.symmetric (Thm.assume xy))) end;
   570 
   571 val transitive_thm =
   572   let val xy = read_prop "x == y"
   573       val yz = read_prop "y == z"
   574       val xythm = Thm.assume xy and yzthm = Thm.assume yz
   575   in store_standard_thm_open "transitive" (Thm.implies_intr yz (Thm.transitive xythm yzthm)) end;
   576 
   577 fun symmetric_fun thm = thm RS symmetric_thm;
   578 
   579 fun extensional eq =
   580   let val eq' =
   581     abstract_rule "x" (snd (Thm.dest_comb (fst (dest_equals (cprop_of eq))))) eq
   582   in equal_elim (eta_conversion (cprop_of eq')) eq' end;
   583 
   584 val imp_cong =
   585   let
   586     val ABC = read_prop "PROP A ==> PROP B == PROP C"
   587     val AB = read_prop "PROP A ==> PROP B"
   588     val AC = read_prop "PROP A ==> PROP C"
   589     val A = read_prop "PROP A"
   590   in
   591     store_standard_thm_open "imp_cong" (implies_intr ABC (equal_intr
   592       (implies_intr AB (implies_intr A
   593         (equal_elim (implies_elim (assume ABC) (assume A))
   594           (implies_elim (assume AB) (assume A)))))
   595       (implies_intr AC (implies_intr A
   596         (equal_elim (symmetric (implies_elim (assume ABC) (assume A)))
   597           (implies_elim (assume AC) (assume A)))))))
   598   end;
   599 
   600 val swap_prems_eq =
   601   let
   602     val ABC = read_prop "PROP A ==> PROP B ==> PROP C"
   603     val BAC = read_prop "PROP B ==> PROP A ==> PROP C"
   604     val A = read_prop "PROP A"
   605     val B = read_prop "PROP B"
   606   in
   607     store_standard_thm_open "swap_prems_eq" (equal_intr
   608       (implies_intr ABC (implies_intr B (implies_intr A
   609         (implies_elim (implies_elim (assume ABC) (assume A)) (assume B)))))
   610       (implies_intr BAC (implies_intr A (implies_intr B
   611         (implies_elim (implies_elim (assume BAC) (assume B)) (assume A))))))
   612   end;
   613 
   614 val imp_cong' = combination o combination (reflexive implies)
   615 
   616 fun abs_def thm =
   617   let
   618     val (_, cvs) = strip_comb (fst (dest_equals (cprop_of thm)));
   619     val thm' = foldr (fn (ct, thm) => Thm.abstract_rule
   620       (case term_of ct of Var ((a, _), _) => a | Free (a, _) => a | _ => "x")
   621         ct thm) (cvs, thm)
   622   in transitive
   623     (symmetric (eta_conversion (fst (dest_equals (cprop_of thm'))))) thm'
   624   end;
   625 
   626 
   627 local
   628   val dest_eq = dest_equals o cprop_of
   629   val rhs_of = snd o dest_eq
   630 in
   631 fun beta_eta_conversion t =
   632   let val thm = beta_conversion true t
   633   in transitive thm (eta_conversion (rhs_of thm)) end
   634 end;
   635 
   636 (*In [A1,...,An]==>B, rewrite the selected A's only -- for rewrite_goals_tac*)
   637 fun goals_conv pred cv =
   638   let fun gconv i ct =
   639         let val (A,B) = dest_implies ct
   640         in imp_cong' (if pred i then cv A else reflexive A) (gconv (i+1) B) end
   641         handle TERM _ => reflexive ct
   642   in gconv 1 end
   643 
   644 (* Rewrite A in !!x1,...,xn. A *)
   645 fun forall_conv cv ct =
   646   let val p as (ct1, ct2) = Thm.dest_comb ct
   647   in (case pairself term_of p of
   648       (Const ("all", _), Abs (s, _, _)) =>
   649          let val (v, ct') = Thm.dest_abs (Some "@") ct2;
   650          in Thm.combination (Thm.reflexive ct1)
   651            (Thm.abstract_rule s v (forall_conv cv ct'))
   652          end
   653     | _ => cv ct)
   654   end handle TERM _ => cv ct;
   655 
   656 (*Use a conversion to transform a theorem*)
   657 fun fconv_rule cv th = equal_elim (cv (cprop_of th)) th;
   658 
   659 (*** Some useful meta-theorems ***)
   660 
   661 (*The rule V/V, obtains assumption solving for eresolve_tac*)
   662 val asm_rl = store_standard_thm_open "asm_rl" (Thm.trivial (read_prop "PROP ?psi"));
   663 val _ = store_thm "_" asm_rl;
   664 
   665 (*Meta-level cut rule: [| V==>W; V |] ==> W *)
   666 val cut_rl =
   667   store_standard_thm_open "cut_rl"
   668     (Thm.trivial (read_prop "PROP ?psi ==> PROP ?theta"));
   669 
   670 (*Generalized elim rule for one conclusion; cut_rl with reversed premises:
   671      [| PROP V;  PROP V ==> PROP W |] ==> PROP W *)
   672 val revcut_rl =
   673   let val V = read_prop "PROP V"
   674       and VW = read_prop "PROP V ==> PROP W";
   675   in
   676     store_standard_thm_open "revcut_rl"
   677       (implies_intr V (implies_intr VW (implies_elim (assume VW) (assume V))))
   678   end;
   679 
   680 (*for deleting an unwanted assumption*)
   681 val thin_rl =
   682   let val V = read_prop "PROP V"
   683       and W = read_prop "PROP W";
   684   in store_standard_thm_open "thin_rl" (implies_intr V (implies_intr W (assume W))) end;
   685 
   686 (* (!!x. PROP ?V) == PROP ?V       Allows removal of redundant parameters*)
   687 val triv_forall_equality =
   688   let val V  = read_prop "PROP V"
   689       and QV = read_prop "!!x::'a. PROP V"
   690       and x  = read_cterm proto_sign ("x", TypeInfer.logicT);
   691   in
   692     store_standard_thm_open "triv_forall_equality"
   693       (equal_intr (implies_intr QV (forall_elim x (assume QV)))
   694         (implies_intr V  (forall_intr x (assume V))))
   695   end;
   696 
   697 (* (PROP ?PhiA ==> PROP ?PhiB ==> PROP ?Psi) ==>
   698    (PROP ?PhiB ==> PROP ?PhiA ==> PROP ?Psi)
   699    `thm COMP swap_prems_rl' swaps the first two premises of `thm'
   700 *)
   701 val swap_prems_rl =
   702   let val cmajor = read_prop "PROP PhiA ==> PROP PhiB ==> PROP Psi";
   703       val major = assume cmajor;
   704       val cminor1 = read_prop "PROP PhiA";
   705       val minor1 = assume cminor1;
   706       val cminor2 = read_prop "PROP PhiB";
   707       val minor2 = assume cminor2;
   708   in store_standard_thm_open "swap_prems_rl"
   709        (implies_intr cmajor (implies_intr cminor2 (implies_intr cminor1
   710          (implies_elim (implies_elim major minor1) minor2))))
   711   end;
   712 
   713 (* [| PROP ?phi ==> PROP ?psi; PROP ?psi ==> PROP ?phi |]
   714    ==> PROP ?phi == PROP ?psi
   715    Introduction rule for == as a meta-theorem.
   716 *)
   717 val equal_intr_rule =
   718   let val PQ = read_prop "PROP phi ==> PROP psi"
   719       and QP = read_prop "PROP psi ==> PROP phi"
   720   in
   721     store_standard_thm_open "equal_intr_rule"
   722       (implies_intr PQ (implies_intr QP (equal_intr (assume PQ) (assume QP))))
   723   end;
   724 
   725 (* [| PROP ?phi == PROP ?psi; PROP ?phi |] ==> PROP ?psi *)
   726 val equal_elim_rule1 =
   727   let val eq = read_prop "PROP phi == PROP psi"
   728       and P = read_prop "PROP phi"
   729   in store_standard_thm_open "equal_elim_rule1"
   730     (Thm.equal_elim (assume eq) (assume P) |> implies_intr_list [eq, P])
   731   end;
   732 
   733 (* "[| PROP ?phi; PROP ?phi; PROP ?psi |] ==> PROP ?psi" *)
   734 
   735 val remdups_rl =
   736   let val P = read_prop "PROP phi" and Q = read_prop "PROP psi";
   737   in store_standard_thm_open "remdups_rl" (implies_intr_list [P, P, Q] (Thm.assume Q)) end;
   738 
   739 
   740 (*(PROP ?phi ==> (!!x. PROP ?psi(x))) == (!!x. PROP ?phi ==> PROP ?psi(x))
   741   Rewrite rule for HHF normalization.*)
   742 
   743 val norm_hhf_eq =
   744   let
   745     val cert = Thm.cterm_of proto_sign;
   746     val aT = TFree ("'a", []);
   747     val all = Term.all aT;
   748     val x = Free ("x", aT);
   749     val phi = Free ("phi", propT);
   750     val psi = Free ("psi", aT --> propT);
   751 
   752     val cx = cert x;
   753     val cphi = cert phi;
   754     val lhs = cert (Logic.mk_implies (phi, all $ Abs ("x", aT, psi $ Bound 0)));
   755     val rhs = cert (all $ Abs ("x", aT, Logic.mk_implies (phi, psi $ Bound 0)));
   756   in
   757     Thm.equal_intr
   758       (Thm.implies_elim (Thm.assume lhs) (Thm.assume cphi)
   759         |> Thm.forall_elim cx
   760         |> Thm.implies_intr cphi
   761         |> Thm.forall_intr cx
   762         |> Thm.implies_intr lhs)
   763       (Thm.implies_elim
   764           (Thm.assume rhs |> Thm.forall_elim cx) (Thm.assume cphi)
   765         |> Thm.forall_intr cx
   766         |> Thm.implies_intr cphi
   767         |> Thm.implies_intr rhs)
   768     |> store_standard_thm_open "norm_hhf_eq"
   769   end;
   770 
   771 fun is_norm_hhf tm =
   772   let
   773     fun is_norm (Const ("==>", _) $ _ $ (Const ("all", _) $ _)) = false
   774       | is_norm (t $ u) = is_norm t andalso is_norm u
   775       | is_norm (Abs (_, _, t)) = is_norm t
   776       | is_norm _ = true;
   777   in is_norm (Pattern.beta_eta_contract tm) end;
   778 
   779 fun norm_hhf sg t =
   780   if is_norm_hhf t then t
   781   else Pattern.rewrite_term (Sign.tsig_of sg) [Logic.dest_equals (prop_of norm_hhf_eq)] [] t;
   782 
   783 
   784 (*** Instantiate theorem th, reading instantiations under signature sg ****)
   785 
   786 (*Version that normalizes the result: Thm.instantiate no longer does that*)
   787 fun instantiate instpair th = Thm.instantiate instpair th  COMP   asm_rl;
   788 
   789 fun read_instantiate_sg sg sinsts th =
   790     let val ts = types_sorts th;
   791         val used = add_term_tvarnames (prop_of th, []);
   792     in  instantiate (read_insts sg ts ts used sinsts) th  end;
   793 
   794 (*Instantiate theorem th, reading instantiations under theory of th*)
   795 fun read_instantiate sinsts th =
   796     read_instantiate_sg (Thm.sign_of_thm th) sinsts th;
   797 
   798 
   799 (*Left-to-right replacements: tpairs = [...,(vi,ti),...].
   800   Instantiates distinct Vars by terms, inferring type instantiations. *)
   801 local
   802   fun add_types ((ct,cu), (sign,tye,maxidx)) =
   803     let val {sign=signt, t=t, T= T, maxidx=maxt,...} = rep_cterm ct
   804         and {sign=signu, t=u, T= U, maxidx=maxu,...} = rep_cterm cu;
   805         val maxi = Int.max(maxidx, Int.max(maxt, maxu));
   806         val sign' = Sign.merge(sign, Sign.merge(signt, signu))
   807         val (tye',maxi') = Type.unify (Sign.tsig_of sign') (tye, maxi) (T, U)
   808           handle Type.TUNIFY => raise TYPE("Ill-typed instantiation", [T,U], [t,u])
   809     in  (sign', tye', maxi')  end;
   810 in
   811 fun cterm_instantiate ctpairs0 th =
   812   let val (sign,tye,_) = foldr add_types (ctpairs0, (Thm.sign_of_thm th, Vartab.empty, 0))
   813       fun instT(ct,cu) = 
   814         let val inst = cterm_of sign o subst_TVars_Vartab tye o term_of
   815         in (inst ct, inst cu) end
   816       fun ctyp2 (ix,T) = (ix, ctyp_of sign T)
   817   in  instantiate (map ctyp2 (Vartab.dest tye), map instT ctpairs0) th  end
   818   handle TERM _ =>
   819            raise THM("cterm_instantiate: incompatible signatures",0,[th])
   820        | TYPE (msg, _, _) => raise THM(msg, 0, [th])
   821 end;
   822 
   823 
   824 (** Derived rules mainly for METAHYPS **)
   825 
   826 (*Given the term "a", takes (%x.t)==(%x.u) to t[a/x]==u[a/x]*)
   827 fun equal_abs_elim ca eqth =
   828   let val {sign=signa, t=a, ...} = rep_cterm ca
   829       and combth = combination eqth (reflexive ca)
   830       val {sign,prop,...} = rep_thm eqth
   831       val (abst,absu) = Logic.dest_equals prop
   832       val cterm = cterm_of (Sign.merge (sign,signa))
   833   in  transitive (symmetric (beta_conversion false (cterm (abst$a))))
   834            (transitive combth (beta_conversion false (cterm (absu$a))))
   835   end
   836   handle THM _ => raise THM("equal_abs_elim", 0, [eqth]);
   837 
   838 (*Calling equal_abs_elim with multiple terms*)
   839 fun equal_abs_elim_list cts th = foldr (uncurry equal_abs_elim) (rev cts, th);
   840 
   841 
   842 (*** Goal (PROP A) <==> PROP A ***)
   843 
   844 local
   845   val cert = Thm.cterm_of proto_sign;
   846   val A = Free ("A", propT);
   847   val G = Logic.mk_goal A;
   848   val (G_def, _) = freeze_thaw ProtoPure.Goal_def;
   849 in
   850   val triv_goal = store_thm "triv_goal" (kind_rule internalK (standard
   851       (Thm.equal_elim (Thm.symmetric G_def) (Thm.assume (cert A)))));
   852   val rev_triv_goal = store_thm "rev_triv_goal" (kind_rule internalK (standard
   853       (Thm.equal_elim G_def (Thm.assume (cert G)))));
   854 end;
   855 
   856 val mk_cgoal = Thm.capply (Thm.cterm_of proto_sign Logic.goal_const);
   857 fun assume_goal ct = Thm.assume (mk_cgoal ct) RS rev_triv_goal;
   858 
   859 fun implies_intr_goals cprops thm =
   860   implies_elim_list (implies_intr_list cprops thm) (map assume_goal cprops)
   861   |> implies_intr_list (map mk_cgoal cprops);
   862 
   863 
   864 
   865 (** variations on instantiate **)
   866 
   867 (*shorthand for instantiating just one variable in the current theory*)
   868 fun inst x t = read_instantiate_sg (sign_of (the_context())) [(x,t)];
   869 
   870 
   871 (* collect vars in left-to-right order *)
   872 
   873 fun tvars_of_terms ts = rev (foldl Term.add_tvars ([], ts));
   874 fun vars_of_terms ts = rev (foldl Term.add_vars ([], ts));
   875 
   876 fun tvars_of thm = tvars_of_terms [prop_of thm];
   877 fun vars_of thm = vars_of_terms [prop_of thm];
   878 
   879 
   880 (* instantiate by left-to-right occurrence of variables *)
   881 
   882 fun instantiate' cTs cts thm =
   883   let
   884     fun err msg =
   885       raise TYPE ("instantiate': " ^ msg,
   886         mapfilter (apsome Thm.typ_of) cTs,
   887         mapfilter (apsome Thm.term_of) cts);
   888 
   889     fun inst_of (v, ct) =
   890       (Thm.cterm_of (#sign (Thm.rep_cterm ct)) (Var v), ct)
   891         handle TYPE (msg, _, _) => err msg;
   892 
   893     fun zip_vars _ [] = []
   894       | zip_vars (_ :: vs) (None :: opt_ts) = zip_vars vs opt_ts
   895       | zip_vars (v :: vs) (Some t :: opt_ts) = (v, t) :: zip_vars vs opt_ts
   896       | zip_vars [] _ = err "more instantiations than variables in thm";
   897 
   898     (*instantiate types first!*)
   899     val thm' =
   900       if forall is_none cTs then thm
   901       else Thm.instantiate (zip_vars (map fst (tvars_of thm)) cTs, []) thm;
   902     in
   903       if forall is_none cts then thm'
   904       else Thm.instantiate ([], map inst_of (zip_vars (vars_of thm') cts)) thm'
   905     end;
   906 
   907 
   908 
   909 (** renaming of bound variables **)
   910 
   911 (* replace bound variables x_i in thm by y_i *)
   912 (* where vs = [(x_1, y_1), ..., (x_n, y_n)]  *)
   913 
   914 fun rename_bvars [] thm = thm
   915   | rename_bvars vs thm =
   916     let
   917       val {sign, prop, ...} = rep_thm thm;
   918       fun ren (Abs (x, T, t)) = Abs (if_none (assoc (vs, x)) x, T, ren t)
   919         | ren (t $ u) = ren t $ ren u
   920         | ren t = t;
   921     in equal_elim (reflexive (cterm_of sign (ren prop))) thm end;
   922 
   923 
   924 (* renaming in left-to-right order *)
   925 
   926 fun rename_bvars' xs thm =
   927   let
   928     val {sign, prop, ...} = rep_thm thm;
   929     fun rename [] t = ([], t)
   930       | rename (x' :: xs) (Abs (x, T, t)) =
   931           let val (xs', t') = rename xs t
   932           in (xs', Abs (if_none x' x, T, t')) end
   933       | rename xs (t $ u) =
   934           let
   935             val (xs', t') = rename xs t;
   936             val (xs'', u') = rename xs' u
   937           in (xs'', t' $ u') end
   938       | rename xs t = (xs, t);
   939   in case rename xs prop of
   940       ([], prop') => equal_elim (reflexive (cterm_of sign prop')) thm
   941     | _ => error "More names than abstractions in theorem"
   942   end;
   943 
   944 
   945 
   946 (* unvarify(T) *)
   947 
   948 (*assume thm in standard form, i.e. no frees, 0 var indexes*)
   949 
   950 fun unvarifyT thm =
   951   let
   952     val cT = Thm.ctyp_of (Thm.sign_of_thm thm);
   953     val tfrees = map (fn ((x, _), S) => Some (cT (TFree (x, S)))) (tvars_of thm);
   954   in instantiate' tfrees [] thm end;
   955 
   956 fun unvarify raw_thm =
   957   let
   958     val thm = unvarifyT raw_thm;
   959     val ct = Thm.cterm_of (Thm.sign_of_thm thm);
   960     val frees = map (fn ((x, _), T) => Some (ct (Free (x, T)))) (vars_of thm);
   961   in instantiate' [] frees thm end;
   962 
   963 
   964 (* tvars_intr_list *)
   965 
   966 fun tfrees_of thm =
   967   let val {hyps, prop, ...} = Thm.rep_thm thm
   968   in foldr Term.add_term_tfree_names (prop :: hyps, []) end;
   969 
   970 fun tvars_intr_list tfrees thm =
   971   Thm.varifyT' (tfrees_of thm \\ tfrees) thm;
   972 
   973 
   974 (* increment var indexes *)
   975 
   976 fun incr_indexes_wrt is cTs cts thms =
   977   let
   978     val maxidx =
   979       foldl Int.max (~1, is @
   980         map (maxidx_of_typ o #T o Thm.rep_ctyp) cTs @
   981         map (#maxidx o Thm.rep_cterm) cts @
   982         map (#maxidx o Thm.rep_thm) thms);
   983   in Thm.incr_indexes (maxidx + 1) end;
   984 
   985 
   986 (* freeze_all *)
   987 
   988 (*freeze all (T)Vars; assumes thm in standard form*)
   989 
   990 fun freeze_all_TVars thm =
   991   (case tvars_of thm of
   992     [] => thm
   993   | tvars =>
   994       let val cert = Thm.ctyp_of (Thm.sign_of_thm thm)
   995       in instantiate' (map (fn ((x, _), S) => Some (cert (TFree (x, S)))) tvars) [] thm end);
   996 
   997 fun freeze_all_Vars thm =
   998   (case vars_of thm of
   999     [] => thm
  1000   | vars =>
  1001       let val cert = Thm.cterm_of (Thm.sign_of_thm thm)
  1002       in instantiate' [] (map (fn ((x, _), T) => Some (cert (Free (x, T)))) vars) thm end);
  1003 
  1004 val freeze_all = freeze_all_Vars o freeze_all_TVars;
  1005 
  1006 
  1007 (* mk_triv_goal *)
  1008 
  1009 (*make an initial proof state, "PROP A ==> (PROP A)" *)
  1010 fun mk_triv_goal ct = instantiate' [] [Some ct] triv_goal;
  1011 
  1012 
  1013 
  1014 (** meta-level conjunction **)
  1015 
  1016 local
  1017   val A = read_prop "PROP A";
  1018   val B = read_prop "PROP B";
  1019   val C = read_prop "PROP C";
  1020   val ABC = read_prop "PROP A ==> PROP B ==> PROP C";
  1021 
  1022   val proj1 =
  1023     forall_intr_list [A, B] (implies_intr_list [A, B] (Thm.assume A))
  1024     |> forall_elim_vars 0;
  1025 
  1026   val proj2 =
  1027     forall_intr_list [A, B] (implies_intr_list [A, B] (Thm.assume B))
  1028     |> forall_elim_vars 0;
  1029 
  1030   val conj_intr_rule =
  1031     forall_intr_list [A, B] (implies_intr_list [A, B]
  1032       (Thm.forall_intr C (Thm.implies_intr ABC
  1033         (implies_elim_list (Thm.assume ABC) [Thm.assume A, Thm.assume B]))))
  1034     |> forall_elim_vars 0;
  1035 
  1036   val incr = incr_indexes_wrt [] [] [];
  1037 in
  1038 
  1039 fun conj_intr tha thb = thb COMP (tha COMP incr [tha, thb] conj_intr_rule);
  1040 
  1041 fun conj_intr_list [] = asm_rl
  1042   | conj_intr_list ths = foldr1 (uncurry conj_intr) ths;
  1043 
  1044 fun conj_elim th =
  1045   let val th' = forall_elim_var (#maxidx (Thm.rep_thm th) + 1) th
  1046   in (incr [th'] proj1 COMP th', incr [th'] proj2 COMP th') end;
  1047 
  1048 fun conj_elim_list th =
  1049   let val (th1, th2) = conj_elim th
  1050   in conj_elim_list th1 @ conj_elim_list th2 end handle THM _ => [th];
  1051 
  1052 fun conj_elim_precise 0 _ = []
  1053   | conj_elim_precise 1 th = [th]
  1054   | conj_elim_precise n th =
  1055       let val (th1, th2) = conj_elim th
  1056       in th1 :: conj_elim_precise (n - 1) th2 end;
  1057 
  1058 val conj_intr_thm = store_standard_thm_open "conjunctionI"
  1059   (implies_intr_list [A, B] (conj_intr (Thm.assume A) (Thm.assume B)));
  1060 
  1061 end;
  1062 
  1063 end;
  1064 
  1065 structure BasicDrule: BASIC_DRULE = Drule;
  1066 open BasicDrule;