src/Pure/logic.ML
author wenzelm
Tue Aug 23 19:31:05 1994 +0200 (1994-08-23)
changeset 573 2fa5ef27bd0a
parent 546 36e40454e03e
child 585 409c9ee7a9f3
permissions -rw-r--r--
removed constant _constrain from Pure sig;
     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 freeze_vars	: term -> term
    24   val incr_indexes	: typ list * int -> term -> term
    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 thaw_vars		: term -> term
    47   val unvarifyT		: typ -> typ  
    48   val unvarify		: term -> term  
    49   val varify		: term -> term  
    50   end;
    51 
    52 functor LogicFun (structure Unify: UNIFY and Net:NET): LOGIC =
    53 struct
    54 
    55 structure Sign = Unify.Sign;
    56 structure Type = Sign.Type;
    57 
    58 (*** Abstract syntax operations on the meta-connectives ***)
    59 
    60 (** equality **)
    61 
    62 (*Make an equality.*)
    63 fun mk_equals(t,u) = equals(fastype_of t) $ t $ u;
    64 
    65 fun dest_equals (Const("==",_) $ t $ u)  =  (t,u)
    66   | dest_equals t = raise TERM("dest_equals", [t]);
    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 = (t aconv u) orelse 
   162       (case u of
   163           Abs(_,_,body) => t occs body
   164 	| f$t' => t occs f  orelse  t occs t'
   165 	| _ => false);
   166 
   167 (*Close up a formula over all free variables by quantification*)
   168 fun close_form A =
   169     list_all_free (map dest_Free (sort atless (term_frees A)),   
   170 		   A);
   171 
   172 
   173 (*Freeze all (T)Vars by turning them into (T)Frees*)
   174 fun freeze_vars(Var(ixn,T)) = Free(Syntax.string_of_vname ixn,
   175                                    Type.freeze_vars T)
   176   | freeze_vars(Const(a,T)) = Const(a,Type.freeze_vars T)
   177   | freeze_vars(Free(a,T))  = Free(a,Type.freeze_vars T)
   178   | freeze_vars(s$t)        = freeze_vars s $ freeze_vars t
   179   | freeze_vars(Abs(a,T,t)) = Abs(a,Type.freeze_vars T,freeze_vars t)
   180   | freeze_vars(b)          = b;
   181 
   182 (*Reverse the effect of freeze_vars*)
   183 fun thaw_vars(Const(a,T)) = Const(a,Type.thaw_vars T)
   184   | thaw_vars(Free(a,T))  =
   185       let val T' = Type.thaw_vars T
   186       in case explode a of
   187 	   "?"::vn => let val (ixn,_) = Syntax.scan_varname vn
   188                       in Var(ixn,T') end
   189 	 | _       => Free(a,T')
   190       end
   191   | thaw_vars(Abs(a,T,t)) = Abs(a,Type.thaw_vars T, thaw_vars t)
   192   | thaw_vars(s$t)        = thaw_vars s $ thaw_vars t
   193   | thaw_vars(b)          = b;
   194 
   195 
   196 (*** Specialized operations for resolution... ***)
   197 
   198 (*For all variables in the term, increment indexnames and lift over the Us
   199     result is ?Gidx(B.(lev+n-1),...,B.lev) where lev is abstraction level *)
   200 fun incr_indexes (Us: typ list, inc:int) t = 
   201   let fun incr (Var ((a,i), T), lev) = 
   202 		Unify.combound (Var((a, i+inc), Us---> incr_tvar inc T),
   203 				lev, length Us)
   204 	| incr (Abs (a,T,body), lev) =
   205 		Abs (a, incr_tvar inc T, incr(body,lev+1))
   206 	| incr (Const(a,T),_) = Const(a, incr_tvar inc T)
   207 	| incr (Free(a,T),_) = Free(a, incr_tvar inc T)
   208 	| incr (f$t, lev) = incr(f,lev) $ incr(t,lev)
   209 	| incr (t,lev) = t
   210   in  incr(t,0)  end;
   211 
   212 (*Make lifting functions from subgoal and increment.
   213     lift_abs operates on tpairs (unification constraints)
   214     lift_all operates on propositions     *)
   215 fun lift_fns (B,inc) =
   216   let fun lift_abs (Us, Const("==>", _) $ _ $ B) u = lift_abs (Us,B) u
   217 	| lift_abs (Us, Const("all",_)$Abs(a,T,t)) u =
   218 	      Abs(a, T, lift_abs (T::Us, t) u)
   219 	| lift_abs (Us, _) u = incr_indexes(rev Us, inc) u
   220       fun lift_all (Us, Const("==>", _) $ A $ B) u =
   221 	      implies $ A $ lift_all (Us,B) u
   222 	| lift_all (Us, Const("all",_)$Abs(a,T,t)) u = 
   223 	      all T $ Abs(a, T, lift_all (T::Us,t) u)
   224 	| lift_all (Us, _) u = incr_indexes(rev Us, inc) u;
   225   in  (lift_abs([],B), lift_all([],B))  end;
   226 
   227 (*Strips assumptions in goal, yielding list of hypotheses.   *)
   228 fun strip_assums_hyp (Const("==>", _) $ H $ B) = H :: strip_assums_hyp B
   229   | strip_assums_hyp (Const("all",_)$Abs(a,T,t)) = strip_assums_hyp t
   230   | strip_assums_hyp B = [];
   231 
   232 (*Strips assumptions in goal, yielding conclusion.   *)
   233 fun strip_assums_concl (Const("==>", _) $ H $ B) = strip_assums_concl B
   234   | strip_assums_concl (Const("all",_)$Abs(a,T,t)) = strip_assums_concl t
   235   | strip_assums_concl B = B;
   236 
   237 (*Make a list of all the parameters in a subgoal, even if nested*)
   238 fun strip_params (Const("==>", _) $ H $ B) = strip_params B
   239   | strip_params (Const("all",_)$Abs(a,T,t)) = (a,T) :: strip_params t
   240   | strip_params B = [];
   241 
   242 (*Removes the parameters from a subgoal and renumber bvars in hypotheses,
   243     where j is the total number of parameters (precomputed) 
   244   If n>0 then deletes assumption n. *)
   245 fun remove_params j n A = 
   246     if j=0 andalso n<=0 then A  (*nothing left to do...*)
   247     else case A of
   248         Const("==>", _) $ H $ B => 
   249 	  if n=1 then                           (remove_params j (n-1) B)
   250 	  else implies $ (incr_boundvars j H) $ (remove_params j (n-1) B)
   251       | Const("all",_)$Abs(a,T,t) => remove_params (j-1) n t
   252       | _ => if n>0 then raise TERM("remove_params", [A])
   253              else A;
   254 
   255 (** Auto-renaming of parameters in subgoals **)
   256 
   257 val auto_rename = ref false
   258 and rename_prefix = ref "ka";
   259 
   260 (*rename_prefix is not exported; it is set by this function.*)
   261 fun set_rename_prefix a =
   262     if a<>"" andalso forall is_letter (explode a)
   263     then  (rename_prefix := a;  auto_rename := true)
   264     else  error"rename prefix must be nonempty and consist of letters";
   265 
   266 (*Makes parameters in a goal have distinctive names (not guaranteed unique!)
   267   A name clash could cause the printer to rename bound vars;
   268     then res_inst_tac would not work properly.*)
   269 fun rename_vars (a, []) = []
   270   | rename_vars (a, (_,T)::vars) =
   271         (a,T) :: rename_vars (bump_string a, vars);
   272 
   273 (*Move all parameters to the front of the subgoal, renaming them apart;
   274   if n>0 then deletes assumption n. *)
   275 fun flatten_params n A =
   276     let val params = strip_params A;
   277 	val vars = if !auto_rename 
   278 		   then rename_vars (!rename_prefix, params)
   279 		   else variantlist(map #1 params,[]) ~~ map #2 params
   280     in  list_all (vars, remove_params (length vars) n A)
   281     end;
   282 
   283 (*Makes parameters in a goal have the names supplied by the list cs.*)
   284 fun list_rename_params (cs, Const("==>", _) $ A $ B) =
   285       implies $ A $ list_rename_params (cs, B)
   286   | list_rename_params (c::cs, Const("all",_)$Abs(_,T,t)) = 
   287       all T $ Abs(c, T, list_rename_params (cs, t))
   288   | list_rename_params (cs, B) = B;
   289 
   290 (*Strips assumptions in goal yielding  ( [Hn,...,H1], [xm,...,x1], B )
   291   where H1,...,Hn are the hypotheses and x1...xm are the parameters.   *)
   292 fun strip_assums_aux (Hs, params, Const("==>", _) $ H $ B) = 
   293 	strip_assums_aux (H::Hs, params, B)
   294   | strip_assums_aux (Hs, params, Const("all",_)$Abs(a,T,t)) =
   295 	strip_assums_aux (Hs, (a,T)::params, t)
   296   | strip_assums_aux (Hs, params, B) = (Hs, params, B);
   297 
   298 fun strip_assums A = strip_assums_aux ([],[],A);
   299 
   300 
   301 (*Produces disagreement pairs, one for each assumption proof, in order.
   302   A is the first premise of the lifted rule, and thus has the form
   303     H1 ==> ... Hk ==> B   and the pairs are (H1,B),...,(Hk,B) *)
   304 fun assum_pairs A =
   305   let val (Hs, params, B) = strip_assums A
   306       val D = Unify.rlist_abs(params, B)
   307       fun pairrev ([],pairs) = pairs  
   308         | pairrev (H::Hs,pairs) = 
   309 	    pairrev(Hs, (Unify.rlist_abs(params,H), D) :: pairs)
   310   in  pairrev (Hs,[])   (*WAS:  map pair (rev Hs)  *)
   311   end;
   312 
   313 
   314 (*Converts Frees to Vars and TFrees to TVars so that axioms can be written
   315   without (?) everywhere*)
   316 fun varify (Const(a,T)) = Const(a, Type.varifyT T)
   317   | varify (Free(a,T)) = Var((a,0), Type.varifyT T)
   318   | varify (Var(ixn,T)) = Var(ixn, Type.varifyT T)
   319   | varify (Abs (a,T,body)) = Abs (a, Type.varifyT T, varify body)
   320   | varify (f$t) = varify f $ varify t
   321   | varify t = t;
   322 
   323 (*Inverse of varifyT.  Move to Pure/type.ML?*)
   324 fun unvarifyT (Type (a, Ts)) = Type (a, map unvarifyT Ts)
   325   | unvarifyT (TVar ((a, 0), S)) = TFree (a, S)
   326   | unvarifyT T = T;
   327 
   328 (*Inverse of varify.  Converts axioms back to their original form.*)
   329 fun unvarify (Const(a,T))    = Const(a, unvarifyT T)
   330   | unvarify (Var((a,0), T)) = Free(a, unvarifyT T)
   331   | unvarify (Var(ixn,T))    = Var(ixn, unvarifyT T)	(*non-zero index!*)
   332   | unvarify (Abs (a,T,body)) = Abs (a, unvarifyT T, unvarify body)
   333   | unvarify (f$t) = unvarify f $ unvarify t
   334   | unvarify t = t;
   335 
   336 end;