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