src/Pure/logic.ML
author nipkow
Wed Mar 04 13:16:05 1998 +0100 (1998-03-04)
changeset 4679 24917efb31b5
parent 4667 6328d427a339
child 4693 2e47ea2c6109
permissions -rw-r--r--
Reorganized simplifier. May now reorient rules.
Moved loop tests from logic to thm.
     1 (*  Title: 	Pure/logic.ML
     2     ID:         $Id$
     3     Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   Cambridge University 1992
     5 
     6 Supporting code for defining the abstract type "thm"
     7 *)
     8 
     9 infix occs;
    10 
    11 signature LOGIC = 
    12 sig
    13   val assum_pairs	: term -> (term*term)list
    14   val auto_rename	: bool ref   
    15   val close_form	: term -> term   
    16   val count_prems	: term * int -> int
    17   val dest_equals	: term -> term * term
    18   val dest_flexpair	: term -> term * term
    19   val dest_implies	: term -> term * term
    20   val dest_inclass	: term -> typ * class
    21   val dest_type		: term -> typ
    22   val flatten_params	: int -> term -> term
    23   val incr_indexes	: typ list * int -> term -> term
    24   val is_equals         : term -> bool
    25   val lift_fns		: term * int -> (term -> term) * (term -> term)
    26   val list_flexpairs	: (term*term)list * term -> term
    27   val list_implies	: term list * term -> term
    28   val list_rename_params: string list * term -> term
    29   val mk_equals		: term * term -> term
    30   val mk_flexpair	: term * term -> term
    31   val mk_implies	: term * term -> term
    32   val mk_inclass	: typ * class -> term
    33   val mk_type		: typ -> term
    34   val occs		: term * term -> bool
    35   val rule_of		: (term*term)list * term list * term -> term
    36   val set_rename_prefix	: string -> unit   
    37   val skip_flexpairs	: term -> term
    38   val strip_assums_concl: term -> term
    39   val strip_assums_hyp	: term -> term list
    40   val strip_flexpairs	: term -> (term*term)list * term
    41   val strip_horn	: term -> (term*term)list * term list * term
    42   val strip_imp_concl	: term -> term
    43   val strip_imp_prems	: term -> term list
    44   val strip_params	: term -> (string * typ) list
    45   val strip_prems	: int * term list * term -> term list * term
    46   val unvarify		: term -> term
    47   val varify		: term -> term
    48 end;
    49 
    50 structure Logic : LOGIC =
    51 struct
    52 
    53 
    54 (*** Abstract syntax operations on the meta-connectives ***)
    55 
    56 (** equality **)
    57 
    58 (*Make an equality.  DOES NOT CHECK TYPE OF u*)
    59 fun mk_equals(t,u) = equals(fastype_of t) $ t $ u;
    60 
    61 fun dest_equals (Const("==",_) $ t $ u)  =  (t,u)
    62   | dest_equals t = raise TERM("dest_equals", [t]);
    63 
    64 fun is_equals (Const ("==", _) $ _ $ _) = true
    65   | is_equals _ = false;
    66 
    67 
    68 (** implies **)
    69 
    70 fun mk_implies(A,B) = implies $ A $ B;
    71 
    72 fun dest_implies (Const("==>",_) $ A $ B)  =  (A,B)
    73   | dest_implies A = raise TERM("dest_implies", [A]);
    74 
    75 (** nested implications **)
    76 
    77 (* [A1,...,An], B  goes to  A1==>...An==>B  *)
    78 fun list_implies ([], B) = B : term
    79   | list_implies (A::AS, B) = implies $ A $ list_implies(AS,B);
    80 
    81 (* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
    82 fun strip_imp_prems (Const("==>", _) $ A $ B) = A :: strip_imp_prems B
    83   | strip_imp_prems _ = [];
    84 
    85 (* A1==>...An==>B  goes to B, where B is not an implication *)
    86 fun strip_imp_concl (Const("==>", _) $ A $ B) = strip_imp_concl B
    87   | strip_imp_concl A = A : term;
    88 
    89 (*Strip and return premises: (i, [], A1==>...Ai==>B)
    90     goes to   ([Ai, A(i-1),...,A1] , B) 	(REVERSED) 
    91   if  i<0 or else i too big then raises  TERM*)
    92 fun strip_prems (0, As, B) = (As, B) 
    93   | strip_prems (i, As, Const("==>", _) $ A $ B) = 
    94 	strip_prems (i-1, A::As, B)
    95   | strip_prems (_, As, A) = raise TERM("strip_prems", A::As);
    96 
    97 (*Count premises -- quicker than (length ostrip_prems) *)
    98 fun count_prems (Const("==>", _) $ A $ B, n) = count_prems (B,n+1)
    99   | count_prems (_,n) = n;
   100 
   101 (** flex-flex constraints **)
   102 
   103 (*Make a constraint.*)
   104 fun mk_flexpair(t,u) = flexpair(fastype_of t) $ t $ u;
   105 
   106 fun dest_flexpair (Const("=?=",_) $ t $ u)  =  (t,u)
   107   | dest_flexpair t = raise TERM("dest_flexpair", [t]);
   108 
   109 (*make flexflex antecedents: ( [(a1,b1),...,(an,bn)] , C )
   110     goes to (a1=?=b1) ==>...(an=?=bn)==>C *)
   111 fun list_flexpairs ([], A) = A
   112   | list_flexpairs ((t,u)::pairs, A) =
   113 	implies $ (mk_flexpair(t,u)) $ list_flexpairs(pairs,A);
   114 
   115 (*Make the object-rule tpairs==>As==>B   *)
   116 fun rule_of (tpairs, As, B) = list_flexpairs(tpairs, list_implies(As, B));
   117 
   118 (*Remove and return flexflex pairs: 
   119     (a1=?=b1)==>...(an=?=bn)==>C  to  ( [(a1,b1),...,(an,bn)] , C )	
   120   [Tail recursive in order to return a pair of results] *)
   121 fun strip_flex_aux (pairs, Const("==>", _) $ (Const("=?=",_)$t$u) $ C) =
   122         strip_flex_aux ((t,u)::pairs, C)
   123   | strip_flex_aux (pairs,C) = (rev pairs, C);
   124 
   125 fun strip_flexpairs A = strip_flex_aux([], A);
   126 
   127 (*Discard flexflex pairs*)
   128 fun skip_flexpairs (Const("==>", _) $ (Const("=?=",_)$_$_) $ C) =
   129 	skip_flexpairs C
   130   | skip_flexpairs C = C;
   131 
   132 (*strip a proof state (Horn clause): 
   133    (a1==b1)==>...(am==bm)==>B1==>...Bn==>C
   134     goes to   ( [(a1,b1),...,(am,bm)] , [B1,...,Bn] , C)    *)
   135 fun strip_horn A =
   136   let val (tpairs,horn) = strip_flexpairs A 
   137   in  (tpairs, strip_imp_prems horn, strip_imp_concl horn)   end;
   138 
   139 (** types as terms **)
   140 
   141 fun mk_type ty = Const ("TYPE", itselfT ty);
   142 
   143 fun dest_type (Const ("TYPE", Type ("itself", [ty]))) = ty
   144   | dest_type t = raise TERM ("dest_type", [t]);
   145 
   146 (** class constraints **)
   147 
   148 fun mk_inclass (ty, c) =
   149   Const (Sign.const_of_class c, itselfT ty --> propT) $ mk_type ty;
   150 
   151 fun dest_inclass (t as Const (c_class, _) $ ty) =
   152       ((dest_type ty, Sign.class_of_const c_class)
   153         handle TERM _ => raise TERM ("dest_inclass", [t]))
   154   | dest_inclass t = raise TERM ("dest_inclass", [t]);
   155 
   156 
   157 (*** Low-level term operations ***)
   158 
   159 (*Does t occur in u?  Or is alpha-convertible to u?
   160   The term t must contain no loose bound variables*)
   161 fun t occs u = exists_subterm (fn s => t aconv s) u;
   162 
   163 (*Close up a formula over all free variables by quantification*)
   164 fun close_form A =
   165   list_all_free (sort_wrt fst (map dest_Free (term_frees A)), A);
   166 
   167 
   168 (*** Specialized operations for resolution... ***)
   169 
   170 (*For all variables in the term, increment indexnames and lift over the Us
   171     result is ?Gidx(B.(lev+n-1),...,B.lev) where lev is abstraction level *)
   172 fun incr_indexes (Us: typ list, inc:int) t = 
   173   let fun incr (Var ((a,i), T), lev) = 
   174 		Unify.combound (Var((a, i+inc), Us---> incr_tvar inc T),
   175 				lev, length Us)
   176 	| incr (Abs (a,T,body), lev) =
   177 		Abs (a, incr_tvar inc T, incr(body,lev+1))
   178 	| incr (Const(a,T),_) = Const(a, incr_tvar inc T)
   179 	| incr (Free(a,T),_) = Free(a, incr_tvar inc T)
   180 	| incr (f$t, lev) = incr(f,lev) $ incr(t,lev)
   181 	| incr (t,lev) = t
   182   in  incr(t,0)  end;
   183 
   184 (*Make lifting functions from subgoal and increment.
   185     lift_abs operates on tpairs (unification constraints)
   186     lift_all operates on propositions     *)
   187 fun lift_fns (B,inc) =
   188   let fun lift_abs (Us, Const("==>", _) $ _ $ B) u = lift_abs (Us,B) u
   189 	| lift_abs (Us, Const("all",_)$Abs(a,T,t)) u =
   190 	      Abs(a, T, lift_abs (T::Us, t) u)
   191 	| lift_abs (Us, _) u = incr_indexes(rev Us, inc) u
   192       fun lift_all (Us, Const("==>", _) $ A $ B) u =
   193 	      implies $ A $ lift_all (Us,B) u
   194 	| lift_all (Us, Const("all",_)$Abs(a,T,t)) u = 
   195 	      all T $ Abs(a, T, lift_all (T::Us,t) u)
   196 	| lift_all (Us, _) u = incr_indexes(rev Us, inc) u;
   197   in  (lift_abs([],B), lift_all([],B))  end;
   198 
   199 (*Strips assumptions in goal, yielding list of hypotheses.   *)
   200 fun strip_assums_hyp (Const("==>", _) $ H $ B) = H :: strip_assums_hyp B
   201   | strip_assums_hyp (Const("all",_)$Abs(a,T,t)) = strip_assums_hyp t
   202   | strip_assums_hyp B = [];
   203 
   204 (*Strips assumptions in goal, yielding conclusion.   *)
   205 fun strip_assums_concl (Const("==>", _) $ H $ B) = strip_assums_concl B
   206   | strip_assums_concl (Const("all",_)$Abs(a,T,t)) = strip_assums_concl t
   207   | strip_assums_concl B = B;
   208 
   209 (*Make a list of all the parameters in a subgoal, even if nested*)
   210 fun strip_params (Const("==>", _) $ H $ B) = strip_params B
   211   | strip_params (Const("all",_)$Abs(a,T,t)) = (a,T) :: strip_params t
   212   | strip_params B = [];
   213 
   214 (*Removes the parameters from a subgoal and renumber bvars in hypotheses,
   215     where j is the total number of parameters (precomputed) 
   216   If n>0 then deletes assumption n. *)
   217 fun remove_params j n A = 
   218     if j=0 andalso n<=0 then A  (*nothing left to do...*)
   219     else case A of
   220         Const("==>", _) $ H $ B => 
   221 	  if n=1 then                           (remove_params j (n-1) B)
   222 	  else implies $ (incr_boundvars j H) $ (remove_params j (n-1) B)
   223       | Const("all",_)$Abs(a,T,t) => remove_params (j-1) n t
   224       | _ => if n>0 then raise TERM("remove_params", [A])
   225              else A;
   226 
   227 (** Auto-renaming of parameters in subgoals **)
   228 
   229 val auto_rename = ref false
   230 and rename_prefix = ref "ka";
   231 
   232 (*rename_prefix is not exported; it is set by this function.*)
   233 fun set_rename_prefix a =
   234     if a<>"" andalso forall is_letter (explode a)
   235     then  (rename_prefix := a;  auto_rename := true)
   236     else  error"rename prefix must be nonempty and consist of letters";
   237 
   238 (*Makes parameters in a goal have distinctive names (not guaranteed unique!)
   239   A name clash could cause the printer to rename bound vars;
   240     then res_inst_tac would not work properly.*)
   241 fun rename_vars (a, []) = []
   242   | rename_vars (a, (_,T)::vars) =
   243         (a,T) :: rename_vars (bump_string a, vars);
   244 
   245 (*Move all parameters to the front of the subgoal, renaming them apart;
   246   if n>0 then deletes assumption n. *)
   247 fun flatten_params n A =
   248     let val params = strip_params A;
   249 	val vars = if !auto_rename 
   250 		   then rename_vars (!rename_prefix, params)
   251 		   else ListPair.zip (variantlist(map #1 params,[]),
   252 				      map #2 params)
   253     in  list_all (vars, remove_params (length vars) n A)
   254     end;
   255 
   256 (*Makes parameters in a goal have the names supplied by the list cs.*)
   257 fun list_rename_params (cs, Const("==>", _) $ A $ B) =
   258       implies $ A $ list_rename_params (cs, B)
   259   | list_rename_params (c::cs, Const("all",_)$Abs(_,T,t)) = 
   260       all T $ Abs(c, T, list_rename_params (cs, t))
   261   | list_rename_params (cs, B) = B;
   262 
   263 (*Strips assumptions in goal yielding  ( [Hn,...,H1], [xm,...,x1], B )
   264   where H1,...,Hn are the hypotheses and x1...xm are the parameters.   *)
   265 fun strip_assums_aux (Hs, params, Const("==>", _) $ H $ B) = 
   266 	strip_assums_aux (H::Hs, params, B)
   267   | strip_assums_aux (Hs, params, Const("all",_)$Abs(a,T,t)) =
   268 	strip_assums_aux (Hs, (a,T)::params, t)
   269   | strip_assums_aux (Hs, params, B) = (Hs, params, B);
   270 
   271 fun strip_assums A = strip_assums_aux ([],[],A);
   272 
   273 
   274 (*Produces disagreement pairs, one for each assumption proof, in order.
   275   A is the first premise of the lifted rule, and thus has the form
   276     H1 ==> ... Hk ==> B   and the pairs are (H1,B),...,(Hk,B) *)
   277 fun assum_pairs A =
   278   let val (Hs, params, B) = strip_assums A
   279       val D = Unify.rlist_abs(params, B)
   280       fun pairrev ([],pairs) = pairs  
   281         | pairrev (H::Hs,pairs) = 
   282 	    pairrev(Hs, (Unify.rlist_abs(params,H), D) :: pairs)
   283   in  pairrev (Hs,[])   (*WAS:  map pair (rev Hs)  *)
   284   end;
   285 
   286 
   287 (*Converts Frees to Vars and TFrees to TVars so that axioms can be written
   288   without (?) everywhere*)
   289 fun varify (Const(a,T)) = Const(a, Type.varifyT T)
   290   | varify (Free(a,T)) = Var((a,0), Type.varifyT T)
   291   | varify (Var(ixn,T)) = Var(ixn, Type.varifyT T)
   292   | varify (Abs (a,T,body)) = Abs (a, Type.varifyT T, varify body)
   293   | varify (f$t) = varify f $ varify t
   294   | varify t = t;
   295 
   296 (*Inverse of varify.  Converts axioms back to their original form.*)
   297 fun unvarify (Const(a,T))    = Const(a, Type.unvarifyT T)
   298   | unvarify (Var((a,0), T)) = Free(a, Type.unvarifyT T)
   299   | unvarify (Var(ixn,T))    = Var(ixn, Type.unvarifyT T)  (*non-0 index!*)
   300   | unvarify (Abs (a,T,body)) = Abs (a, Type.unvarifyT T, unvarify body)
   301   | unvarify (f$t) = unvarify f $ unvarify t
   302   | unvarify t = t;
   303 
   304 end;