src/Pure/drule.ML
changeset 252 7532f95d7f44
parent 229 4002c4cd450c
child 385 921f87897a76
--- a/src/Pure/drule.ML	Thu Feb 03 13:53:44 1994 +0100
+++ b/src/Pure/drule.ML	Thu Feb 03 13:55:03 1994 +0100
@@ -1,6 +1,6 @@
-(*  Title: 	Pure/drule.ML
+(*  Title:      Pure/drule.ML
     ID:         $Id$
-    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
+    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     Copyright   1993  University of Cambridge
 
 Derived rules and other operations on theorems and theories
@@ -14,14 +14,12 @@
   local open Thm  in
   val asm_rl: thm
   val assume_ax: theory -> string -> thm
-  val cterm_fun: (term -> term) -> (cterm -> cterm)
   val COMP: thm * thm -> thm
   val compose: thm * int * thm -> thm list
   val cterm_instantiate: (cterm*cterm)list -> thm -> thm
   val cut_rl: thm
   val equal_abs_elim: cterm  -> thm -> thm
   val equal_abs_elim_list: cterm list -> thm -> thm
-  val eq_sg: Sign.sg * Sign.sg -> bool
   val eq_thm: thm * thm -> bool
   val eq_thm_sg: thm * thm -> bool
   val flexpair_abs_elim_list: cterm list -> thm -> thm
@@ -36,7 +34,6 @@
   val MRS: thm list * thm -> thm
   val pprint_cterm: cterm -> pprint_args -> unit
   val pprint_ctyp: ctyp -> pprint_args -> unit
-  val pprint_sg: Sign.sg -> pprint_args -> unit
   val pprint_theory: theory -> pprint_args -> unit
   val pprint_thm: thm -> pprint_args -> unit
   val pretty_thm: thm -> Sign.Syntax.Pretty.T
@@ -44,16 +41,14 @@
   val print_ctyp: ctyp -> unit
   val print_goals: int -> thm -> unit
   val print_goals_ref: (int -> thm -> unit) ref
-  val print_sg: Sign.sg -> unit
   val print_theory: theory -> unit
   val print_thm: thm -> unit
   val prth: thm -> thm
   val prthq: thm Sequence.seq -> thm Sequence.seq
   val prths: thm list -> thm list
-  val read_ctyp: Sign.sg -> string -> ctyp
   val read_instantiate: (string*string)list -> thm -> thm
   val read_instantiate_sg: Sign.sg -> (string*string)list -> thm -> thm
-  val read_insts: 
+  val read_insts:
           Sign.sg -> (indexname -> typ option) * (indexname -> sort option)
                   -> (indexname -> typ option) * (indexname -> sort option)
                   -> (string*string)list
@@ -82,7 +77,7 @@
   end
   end;
 
-functor DruleFun (structure Logic: LOGIC and Thm: THM)(* : DRULE *) = (* FIXME *)
+functor DruleFun (structure Logic: LOGIC and Thm: THM): DRULE =
 struct
 structure Thm = Thm;
 structure Sign = Thm.Sign;
@@ -93,12 +88,6 @@
 
 (**** More derived rules and operations on theorems ****)
 
-fun cterm_fun f ct =
- let val {sign,t,...} = rep_cterm ct in cterm_of sign (f t) end;
-
-fun read_ctyp sign = ctyp_of sign o Sign.read_typ(sign, K None);
-
-
 (** reading of instantiations **)
 
 fun indexname cs = case Syntax.scan_varname cs of (v,[]) => v
@@ -136,7 +125,8 @@
 in (map (fn (ixn,T) => (ixn,ctyp_of sign T)) tye', cterms) end;
 
 
-(*** Printing of theorems ***)
+
+(*** Printing of theories, theorems, etc. ***)
 
 (*If false, hypotheses are printed as dots*)
 val show_hyps = ref true;
@@ -144,11 +134,11 @@
 fun pretty_thm th =
 let val {sign, hyps, prop,...} = rep_thm th
     val hsymbs = if null hyps then []
-		 else if !show_hyps then
-		      [Pretty.brk 2,
-		       Pretty.lst("[","]") (map (Sign.pretty_term sign) hyps)]
-		 else Pretty.str" [" :: map (fn _ => Pretty.str".") hyps @
-		      [Pretty.str"]"];
+                 else if !show_hyps then
+                      [Pretty.brk 2,
+                       Pretty.lst("[","]") (map (Sign.pretty_term sign) hyps)]
+                 else Pretty.str" [" :: map (fn _ => Pretty.str".") hyps @
+                      [Pretty.str"]"];
 in Pretty.blk(0, Sign.pretty_term sign prop :: hsymbs) end;
 
 val string_of_thm = Pretty.string_of o pretty_thm;
@@ -163,38 +153,49 @@
 
 (*Print and return a sequence of theorems, separated by blank lines. *)
 fun prthq thseq =
-    (Sequence.prints (fn _ => print_thm) 100000 thseq;
-     thseq);
+  (Sequence.prints (fn _ => print_thm) 100000 thseq; thseq);
 
 (*Print and return a list of theorems, separated by blank lines. *)
 fun prths ths = (print_list_ln print_thm ths; ths);
 
-(*Other printing commands*)
-fun pprint_ctyp cT = 
- let val {sign,T} = rep_ctyp cT in  Sign.pprint_typ sign T  end;
+
+(* other printing commands *)
 
-fun string_of_ctyp cT = 
- let val {sign,T} = rep_ctyp cT in  Sign.string_of_typ sign T  end;
+fun pprint_ctyp cT =
+  let val {sign, T} = rep_ctyp cT in Sign.pprint_typ sign T end;
+
+fun string_of_ctyp cT =
+  let val {sign, T} = rep_ctyp cT in Sign.string_of_typ sign T end;
 
 val print_ctyp = writeln o string_of_ctyp;
 
-fun pprint_cterm ct = 
- let val {sign,t,...} = rep_cterm ct in  Sign.pprint_term sign t  end;
+fun pprint_cterm ct =
+  let val {sign, t, ...} = rep_cterm ct in Sign.pprint_term sign t end;
 
-fun string_of_cterm ct = 
- let val {sign,t,...} = rep_cterm ct in  Sign.string_of_term sign t  end;
+fun string_of_cterm ct =
+  let val {sign, t, ...} = rep_cterm ct in Sign.string_of_term sign t end;
 
 val print_cterm = writeln o string_of_cterm;
 
-fun pretty_sg sg = 
-  Pretty.lst ("{", "}") (map (Pretty.str o !) (#stamps (Sign.rep_sg sg)));
+
+(* print theory *)
+
+val pprint_theory = Sign.pprint_sg o sign_of;
 
-val pprint_sg = Pretty.pprint o pretty_sg;
+fun print_theory thy =
+  let
+    fun prt_thm (name, thm) = Pretty.block
+      [Pretty.str (name ^ ":"), Pretty.brk 1, Pretty.quote (pretty_thm thm)];
 
-val pprint_theory = pprint_sg o sign_of;
+    val sg = sign_of thy;
+    val axioms =        (* FIXME should rather fix axioms_of *)
+      sort (fn ((x, _), (y, _)) => x <= y)
+        (gen_distinct eq_fst (axioms_of thy));
+  in
+    Sign.print_sg sg;
+    Pretty.writeln (Pretty.big_list "axioms:" (map prt_thm axioms))
+  end;
 
-val print_sg = writeln o Pretty.string_of o pretty_sg;
-val print_theory = print_sg o sign_of;
 
 
 (** Print thm A1,...,An/B in "goal style" -- premises as numbered subgoals **)
@@ -205,26 +206,26 @@
 let val {sign, hyps, prop,...} = rep_thm th;
     fun printgoals (_, []) = ()
       | printgoals (n, A::As) =
-	let val prettyn = Pretty.str(" " ^ string_of_int n ^ ". ");
-	    val prettyA = Sign.pretty_term sign A
-	in prettyprints[prettyn,prettyA]; 
-           printgoals (n+1,As) 
+        let val prettyn = Pretty.str(" " ^ string_of_int n ^ ". ");
+            val prettyA = Sign.pretty_term sign A
+        in prettyprints[prettyn,prettyA];
+           printgoals (n+1,As)
         end;
     fun prettypair(t,u) =
         Pretty.blk(0, [Sign.pretty_term sign t, Pretty.str" =?=", Pretty.brk 1,
-		       Sign.pretty_term sign u]);
+                       Sign.pretty_term sign u]);
     fun printff [] = ()
       | printff tpairs =
-	 writeln("\nFlex-flex pairs:\n" ^
-		 Pretty.string_of(Pretty.lst("","") (map prettypair tpairs)))
+         writeln("\nFlex-flex pairs:\n" ^
+                 Pretty.string_of(Pretty.lst("","") (map prettypair tpairs)))
     val (tpairs,As,B) = Logic.strip_horn(prop);
     val ngoals = length As
-in 
+in
    writeln (Sign.string_of_term sign B);
    if ngoals=0 then writeln"No subgoals!"
-   else if ngoals>maxgoals 
+   else if ngoals>maxgoals
         then (printgoals (1, take(maxgoals,As));
-	      writeln("A total of " ^ string_of_int ngoals ^ " subgoals..."))
+              writeln("A total of " ^ string_of_int ngoals ^ " subgoals..."))
         else printgoals (1, As);
    printff tpairs
 end;
@@ -232,7 +233,7 @@
 (*"hook" for user interfaces: allows print_goals to be replaced*)
 val print_goals_ref = ref print_goals;
 
-(*** Find the type (sort) associated with a (T)Var or (T)Free in a term 
+(*** Find the type (sort) associated with a (T)Var or (T)Free in a term
      Used for establishing default types (of variables) and sorts (of
      type variables) when reading another term.
      Index -1 indicates that a (T)Free rather than a (T)Var is wanted.
@@ -240,13 +241,13 @@
 
 fun types_sorts thm =
     let val {prop,hyps,...} = rep_thm thm;
-	val big = list_comb(prop,hyps); (* bogus term! *)
-	val vars = map dest_Var (term_vars big);
-	val frees = map dest_Free (term_frees big);
-	val tvars = term_tvars big;
-	val tfrees = term_tfrees big;
-	fun typ(a,i) = if i<0 then assoc(frees,a) else assoc(vars,(a,i));
-	fun sort(a,i) = if i<0 then assoc(tfrees,a) else assoc(tvars,(a,i));
+        val big = list_comb(prop,hyps); (* bogus term! *)
+        val vars = map dest_Var (term_vars big);
+        val frees = map dest_Free (term_frees big);
+        val tvars = term_tvars big;
+        val tfrees = term_tfrees big;
+        fun typ(a,i) = if i<0 then assoc(frees,a) else assoc(vars,(a,i));
+        fun sort(a,i) = if i<0 then assoc(tfrees,a) else assoc(tvars,(a,i));
     in (typ,sort) end;
 
 (** Standardization of rules **)
@@ -254,31 +255,31 @@
 (*Generalization over a list of variables, IGNORING bad ones*)
 fun forall_intr_list [] th = th
   | forall_intr_list (y::ys) th =
-	let val gth = forall_intr_list ys th
-	in  forall_intr y gth   handle THM _ =>  gth  end;
+        let val gth = forall_intr_list ys th
+        in  forall_intr y gth   handle THM _ =>  gth  end;
 
 (*Generalization over all suitable Free variables*)
 fun forall_intr_frees th =
     let val {prop,sign,...} = rep_thm th
     in  forall_intr_list
-         (map (cterm_of sign) (sort atless (term_frees prop))) 
+         (map (cterm_of sign) (sort atless (term_frees prop)))
          th
     end;
 
 (*Replace outermost quantified variable by Var of given index.
     Could clash with Vars already present.*)
-fun forall_elim_var i th = 
+fun forall_elim_var i th =
     let val {prop,sign,...} = rep_thm th
     in case prop of
-	  Const("all",_) $ Abs(a,T,_) =>
-	      forall_elim (cterm_of sign (Var((a,i), T)))  th
-	| _ => raise THM("forall_elim_var", i, [th])
+          Const("all",_) $ Abs(a,T,_) =>
+              forall_elim (cterm_of sign (Var((a,i), T)))  th
+        | _ => raise THM("forall_elim_var", i, [th])
     end;
 
 (*Repeat forall_elim_var until all outer quantifiers are removed*)
-fun forall_elim_vars i th = 
+fun forall_elim_vars i th =
     forall_elim_vars i (forall_elim_var i th)
-	handle THM _ => th;
+        handle THM _ => th;
 
 (*Specialization over a list of cterms*)
 fun forall_elim_list cts th = foldr (uncurry forall_elim) (rev cts, th);
@@ -290,21 +291,21 @@
 fun implies_elim_list impth ths = foldl (uncurry implies_elim) (impth,ths);
 
 (*Reset Var indexes to zero, renaming to preserve distinctness*)
-fun zero_var_indexes th = 
+fun zero_var_indexes th =
     let val {prop,sign,...} = rep_thm th;
         val vars = term_vars prop
         val bs = foldl add_new_id ([], map (fn Var((a,_),_)=>a) vars)
-	val inrs = add_term_tvars(prop,[]);
-	val nms' = rev(foldl add_new_id ([], map (#1 o #1) inrs));
-	val tye = map (fn ((v,rs),a) => (v, TVar((a,0),rs))) (inrs ~~ nms')
-	val ctye = map (fn (v,T) => (v,ctyp_of sign T)) tye;
-	fun varpairs([],[]) = []
-	  | varpairs((var as Var(v,T)) :: vars, b::bs) =
-		let val T' = typ_subst_TVars tye T
-		in (cterm_of sign (Var(v,T')),
-		    cterm_of sign (Var((b,0),T'))) :: varpairs(vars,bs)
-		end
-	  | varpairs _ = raise TERM("varpairs", []);
+        val inrs = add_term_tvars(prop,[]);
+        val nms' = rev(foldl add_new_id ([], map (#1 o #1) inrs));
+        val tye = map (fn ((v,rs),a) => (v, TVar((a,0),rs))) (inrs ~~ nms')
+        val ctye = map (fn (v,T) => (v,ctyp_of sign T)) tye;
+        fun varpairs([],[]) = []
+          | varpairs((var as Var(v,T)) :: vars, b::bs) =
+                let val T' = typ_subst_TVars tye T
+                in (cterm_of sign (Var(v,T')),
+                    cterm_of sign (Var((b,0),T'))) :: varpairs(vars,bs)
+                end
+          | varpairs _ = raise TERM("varpairs", []);
     in instantiate (ctye, varpairs(vars,rev bs)) th end;
 
 
@@ -312,22 +313,22 @@
     all generality expressed by Vars having index 0.*)
 fun standard th =
     let val {maxidx,...} = rep_thm th
-    in  varifyT (zero_var_indexes (forall_elim_vars(maxidx+1) 
+    in  varifyT (zero_var_indexes (forall_elim_vars(maxidx+1)
                          (forall_intr_frees(implies_intr_hyps th))))
     end;
 
-(*Assume a new formula, read following the same conventions as axioms. 
+(*Assume a new formula, read following the same conventions as axioms.
   Generalizes over Free variables,
   creates the assumption, and then strips quantifiers.
   Example is [| ALL x:?A. ?P(x) |] ==> [| ?P(?a) |]
-	     [ !(A,P,a)[| ALL x:A. P(x) |] ==> [| P(a) |] ]    *)
+             [ !(A,P,a)[| ALL x:A. P(x) |] ==> [| P(a) |] ]    *)
 fun assume_ax thy sP =
     let val sign = sign_of thy
-	val prop = Logic.close_form (term_of (read_cterm sign
-			 (sP, propT)))
+        val prop = Logic.close_form (term_of (read_cterm sign
+                         (sP, propT)))
     in forall_elim_vars 0 (assume (cterm_of sign prop))  end;
 
-(*Resolution: exactly one resolvent must be produced.*) 
+(*Resolution: exactly one resolvent must be produced.*)
 fun tha RSN (i,thb) =
   case Sequence.chop (2, biresolution false [(false,tha)] i thb) of
       ([th],_) => th
@@ -338,7 +339,7 @@
 fun tha RS thb = tha RSN (1,thb);
 
 (*For joining lists of rules*)
-fun thas RLN (i,thbs) = 
+fun thas RLN (i,thbs) =
   let val resolve = biresolution false (map (pair false) thas) i
       fun resb thb = Sequence.list_of_s (resolve thb) handle THM _ => []
   in  flat (map resb thbs)  end;
@@ -347,27 +348,27 @@
 
 (*Resolve a list of rules against bottom_rl from right to left;
   makes proof trees*)
-fun rls MRS bottom_rl = 
+fun rls MRS bottom_rl =
   let fun rs_aux i [] = bottom_rl
-	| rs_aux i (rl::rls) = rl RSN (i, rs_aux (i+1) rls)
+        | rs_aux i (rl::rls) = rl RSN (i, rs_aux (i+1) rls)
   in  rs_aux 1 rls  end;
 
 (*As above, but for rule lists*)
-fun rlss MRL bottom_rls = 
+fun rlss MRL bottom_rls =
   let fun rs_aux i [] = bottom_rls
-	| rs_aux i (rls::rlss) = rls RLN (i, rs_aux (i+1) rlss)
+        | rs_aux i (rls::rlss) = rls RLN (i, rs_aux (i+1) rlss)
   in  rs_aux 1 rlss  end;
 
-(*compose Q and [...,Qi,Q(i+1),...]==>R to [...,Q(i+1),...]==>R 
+(*compose Q and [...,Qi,Q(i+1),...]==>R to [...,Q(i+1),...]==>R
   with no lifting or renaming!  Q may contain ==> or meta-quants
   ALWAYS deletes premise i *)
-fun compose(tha,i,thb) = 
+fun compose(tha,i,thb) =
     Sequence.list_of_s (bicompose false (false,tha,0) i thb);
 
 (*compose Q and [Q1,Q2,...,Qk]==>R to [Q2,...,Qk]==>R getting unique result*)
 fun tha COMP thb =
     case compose(tha,1,thb) of
-        [th] => th  
+        [th] => th
       | _ =>   raise THM("COMP", 1, [tha,thb]);
 
 (*Instantiate theorem th, reading instantiations under signature sg*)
@@ -387,18 +388,18 @@
     let val {sign=signt, t=t, T= T, ...} = rep_cterm ct
         and {sign=signu, t=u, T= U, ...} = rep_cterm cu
         val sign' = Sign.merge(sign, Sign.merge(signt, signu))
-	val tye' = Type.unify (#tsig(Sign.rep_sg sign')) ((T,U), tye)
-	  handle Type.TUNIFY => raise TYPE("add_types", [T,U], [t,u])
+        val tye' = Type.unify (#tsig(Sign.rep_sg sign')) ((T,U), tye)
+          handle Type.TUNIFY => raise TYPE("add_types", [T,U], [t,u])
     in  (sign', tye')  end;
 in
-fun cterm_instantiate ctpairs0 th = 
+fun cterm_instantiate ctpairs0 th =
   let val (sign,tye) = foldr add_types (ctpairs0, (#sign(rep_thm th),[]))
       val tsig = #tsig(Sign.rep_sg sign);
       fun instT(ct,cu) = let val inst = subst_TVars tye
-			 in (cterm_fun inst ct, cterm_fun inst cu) end
+                         in (cterm_fun inst ct, cterm_fun inst cu) end
       fun ctyp2 (ix,T) = (ix, ctyp_of sign T)
   in  instantiate (map ctyp2 tye, map instT ctpairs0) th  end
-  handle TERM _ => 
+  handle TERM _ =>
            raise THM("cterm_instantiate: incompatible signatures",0,[th])
        | TYPE _ => raise THM("cterm_instantiate: types", 0, [th])
 end;
@@ -406,21 +407,18 @@
 
 (** theorem equality test is exported and used by BEST_FIRST **)
 
-(*equality of signatures means exact identity -- by ref equality*)
-fun eq_sg (sg1,sg2) = (#stamps(Sign.rep_sg sg1) = #stamps(Sign.rep_sg sg2));
-
-(*equality of theorems uses equality of signatures and 
+(*equality of theorems uses equality of signatures and
   the a-convertible test for terms*)
-fun eq_thm (th1,th2) = 
+fun eq_thm (th1,th2) =
     let val {sign=sg1, hyps=hyps1, prop=prop1, ...} = rep_thm th1
-	and {sign=sg2, hyps=hyps2, prop=prop2, ...} = rep_thm th2
-    in  eq_sg (sg1,sg2) andalso 
-        aconvs(hyps1,hyps2) andalso 
-        prop1 aconv prop2  
+        and {sign=sg2, hyps=hyps2, prop=prop2, ...} = rep_thm th2
+    in  Sign.eq_sg (sg1,sg2) andalso
+        aconvs(hyps1,hyps2) andalso
+        prop1 aconv prop2
     end;
 
 (*Do the two theorems have the same signature?*)
-fun eq_thm_sg (th1,th2) = eq_sg(#sign(rep_thm th1), #sign(rep_thm th2));
+fun eq_thm_sg (th1,th2) = Sign.eq_sg(#sign(rep_thm th1), #sign(rep_thm th2));
 
 (*Useful "distance" function for BEST_FIRST*)
 val size_of_thm = size_of_term o #prop o rep_thm;
@@ -449,13 +447,13 @@
 
 (*In [A1,...,An]==>B, rewrite the selected A's only -- for rewrite_goals_tac*)
 (*Do not rewrite flex-flex pairs*)
-fun goals_conv pred cv = 
+fun goals_conv pred cv =
   let fun gconv i ct =
         let val (A,B) = Thm.dest_cimplies ct
             val (thA,j) = case term_of A of
                   Const("=?=",_)$_$_ => (reflexive A, i)
                 | _ => (if pred i then cv A else reflexive A, i+1)
-	in  combination (combination refl_cimplies thA) (gconv j B) end
+        in  combination (combination refl_cimplies thA) (gconv j B) end
         handle TERM _ => reflexive ct
   in gconv 1 end;
 
@@ -504,10 +502,10 @@
   fun err th = raise THM("flexpair_inst: ", 0, [th])
   fun flexpair_inst def th =
     let val {prop = Const _ $ t $ u,  sign,...} = rep_thm th
-	val cterm = cterm_of sign
-	fun cvar a = cterm(Var((a,0),alpha))
-	val def' = cterm_instantiate [(cvar"t", cterm t), (cvar"u", cterm u)] 
-		   def
+        val cterm = cterm_of sign
+        fun cvar a = cterm(Var((a,0),alpha))
+        val def' = cterm_instantiate [(cvar"t", cterm t), (cvar"u", cterm u)]
+                   def
     in  equal_elim def' th
     end
     handle THM _ => err th | bind => err th
@@ -517,7 +515,7 @@
 end;
 
 (*Version for flexflex pairs -- this supports lifting.*)
-fun flexpair_abs_elim_list cts = 
+fun flexpair_abs_elim_list cts =
     flexpair_intr o equal_abs_elim_list cts o flexpair_elim;
 
 
@@ -527,17 +525,17 @@
 val asm_rl = trivial(read_cterm Sign.pure ("PROP ?psi",propT));
 
 (*Meta-level cut rule: [| V==>W; V |] ==> W *)
-val cut_rl = trivial(read_cterm Sign.pure 
-	("PROP ?psi ==> PROP ?theta", propT));
+val cut_rl = trivial(read_cterm Sign.pure
+        ("PROP ?psi ==> PROP ?theta", propT));
 
-(*Generalized elim rule for one conclusion; cut_rl with reversed premises: 
+(*Generalized elim rule for one conclusion; cut_rl with reversed premises:
      [| PROP V;  PROP V ==> PROP W |] ==> PROP W *)
 val revcut_rl =
   let val V = read_cterm Sign.pure ("PROP V", propT)
       and VW = read_cterm Sign.pure ("PROP V ==> PROP W", propT);
-  in  standard (implies_intr V 
-		(implies_intr VW
-		 (implies_elim (assume VW) (assume V))))
+  in  standard (implies_intr V
+                (implies_intr VW
+                 (implies_elim (assume VW) (assume V))))
   end;
 
 (* (!!x. PROP ?V) == PROP ?V       Allows removal of redundant parameters*)
@@ -546,8 +544,9 @@
       and QV = read_cterm Sign.pure ("!!x::'a. PROP V", propT)
       and x  = read_cterm Sign.pure ("x", TFree("'a",["logic"]));
   in  standard (equal_intr (implies_intr QV (forall_elim x (assume QV)))
-		           (implies_intr V  (forall_intr x (assume V))))
+                           (implies_intr V  (forall_intr x (assume V))))
   end;
 
 end
 end;
+