src/Pure/term.ML
changeset 0 a5a9c433f639
child 40 3f9f8395519e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/term.ML	Thu Sep 16 12:20:38 1993 +0200
     1.3 @@ -0,0 +1,549 @@
     1.4 +(*  Title: 	term.ML
     1.5 +    ID:         $Id$
     1.6 +    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
     1.7 +    Copyright   Cambridge University 1992
     1.8 +*)
     1.9 +
    1.10 +
    1.11 +(*Simply typed lambda-calculus: types, terms, and basic operations*)
    1.12 +
    1.13 +
    1.14 +(*Indexnames can be quickly renamed by adding an offset to the integer part,
    1.15 +  for resolution.*)
    1.16 +type indexname = string*int;
    1.17 +
    1.18 +(* Types are classified by classes. *)
    1.19 +type class = string;
    1.20 +type sort  = class list;
    1.21 +
    1.22 +(* The sorts attached to TFrees and TVars specify the sort of that variable *)
    1.23 +datatype typ = Type  of string * typ list
    1.24 +             | TFree of string * sort
    1.25 +	     | TVar  of indexname * sort;
    1.26 +
    1.27 +infixr 5 -->;
    1.28 +fun S --> T = Type("fun",[S,T]);
    1.29 +
    1.30 +(*handy for multiple args: [T1,...,Tn]--->T  gives  T1-->(T2--> ... -->T)*)
    1.31 +infixr --->;
    1.32 +val op ---> = foldr (op -->);
    1.33 +
    1.34 +
    1.35 +(*terms.  Bound variables are indicated by depth number.
    1.36 +  Free variables, (scheme) variables and constants have names.
    1.37 +  An term is "closed" if there every bound variable of level "lev"
    1.38 +  is enclosed by at least "lev" abstractions. 
    1.39 +
    1.40 +  It is possible to create meaningless terms containing loose bound vars
    1.41 +  or type mismatches.  But such terms are not allowed in rules. *)
    1.42 +
    1.43 +
    1.44 +
    1.45 +infix 9 $;  (*application binds tightly!*)
    1.46 +datatype term = 
    1.47 +    Const of string * typ
    1.48 +  | Free  of string * typ 
    1.49 +  | Var   of indexname * typ
    1.50 +  | Bound of int
    1.51 +  | Abs   of string*typ*term
    1.52 +  | op $  of term*term;
    1.53 +
    1.54 +
    1.55 +(*For errors involving type mismatches*)
    1.56 +exception TYPE of string * typ list * term list;
    1.57 +
    1.58 +(*For system errors involving terms*)
    1.59 +exception TERM of string * term list;
    1.60 +
    1.61 +
    1.62 +(*Note variable naming conventions!
    1.63 +    a,b,c: string
    1.64 +    f,g,h: functions (including terms of function type)
    1.65 +    i,j,m,n: int
    1.66 +    t,u: term
    1.67 +    v,w: indexnames
    1.68 +    x,y: any
    1.69 +    A,B,C: term (denoting formulae)
    1.70 +    T,U: typ
    1.71 +*)
    1.72 +
    1.73 +
    1.74 +(** Discriminators **)
    1.75 +
    1.76 +fun is_Const (Const _) = true
    1.77 +  | is_Const _ = false;
    1.78 +
    1.79 +fun is_Free (Free _) = true
    1.80 +  | is_Free _ = false;
    1.81 +
    1.82 +fun is_Var (Var _) = true
    1.83 +  | is_Var _ = false;
    1.84 +
    1.85 +fun is_TVar (TVar _) = true
    1.86 +  | is_TVar _ = false;
    1.87 +
    1.88 +(** Destructors **)
    1.89 +
    1.90 +fun dest_Const (Const x) =  x
    1.91 +  | dest_Const t = raise TERM("dest_Const", [t]);
    1.92 +
    1.93 +fun dest_Free (Free x) =  x
    1.94 +  | dest_Free t = raise TERM("dest_Free", [t]);
    1.95 +
    1.96 +fun dest_Var (Var x) =  x
    1.97 +  | dest_Var t = raise TERM("dest_Var", [t]);
    1.98 +
    1.99 +
   1.100 +(* maps  [T1,...,Tn]--->T  to the list  [T1,T2,...,Tn]*)
   1.101 +fun binder_types (Type("fun",[S,T])) = S :: binder_types T
   1.102 +  | binder_types _   =  [];
   1.103 +
   1.104 +(* maps  [T1,...,Tn]--->T  to T*)
   1.105 +fun body_type (Type("fun",[S,T])) = body_type T
   1.106 +  | body_type T   =  T;
   1.107 +
   1.108 +(* maps  [T1,...,Tn]--->T  to   ([T1,T2,...,Tn], T)  *)
   1.109 +fun strip_type T : typ list * typ =
   1.110 +  (binder_types T, body_type T);
   1.111 +
   1.112 +
   1.113 +(*Compute the type of the term, checking that combinations are well-typed
   1.114 +  Ts = [T0,T1,...] holds types of bound variables 0, 1, ...*)
   1.115 +fun type_of1 (Ts, Const (_,T)) = T
   1.116 +  | type_of1 (Ts, Free  (_,T)) = T
   1.117 +  | type_of1 (Ts, Bound i) = (nth_elem (i,Ts)  
   1.118 +  	handle LIST _ => raise TYPE("type_of: bound variable", [], [Bound i]))
   1.119 +  | type_of1 (Ts, Var (_,T)) = T
   1.120 +  | type_of1 (Ts, Abs (_,T,body)) = T --> type_of1(T::Ts, body)
   1.121 +  | type_of1 (Ts, f$u) = 
   1.122 +      let val U = type_of1(Ts,u)
   1.123 +          and T = type_of1(Ts,f)
   1.124 +      in case T of
   1.125 +	    Type("fun",[T1,T2]) =>
   1.126 +	      if T1=U then T2  else raise TYPE
   1.127 +	         ("type_of: type mismatch in application", [T1,U], [f$u])
   1.128 +	  | _ => raise TYPE ("type_of: Rator must have function type",
   1.129 +	                        [T,U], [f$u])
   1.130 +      end;
   1.131 +
   1.132 +
   1.133 +fun type_of t : typ = type_of1 ([],t);
   1.134 +
   1.135 +(*Determines the type of a term, with minimal checking*)
   1.136 +fun fastype_of(Ts, f$u) = (case fastype_of(Ts,f) of
   1.137 +	Type("fun",[_,T]) => T
   1.138 +	| _ => raise TERM("fastype_of: expected function type", [f$u]))
   1.139 +  | fastype_of(_, Const (_,T)) = T
   1.140 +  | fastype_of(_, Free (_,T)) = T
   1.141 +  | fastype_of(Ts, Bound i) = (nth_elem(i,Ts)
   1.142 +  	 handle LIST _ => raise TERM("fastype_of: Bound", [Bound i]))
   1.143 +  | fastype_of(_, Var (_,T)) = T 
   1.144 +  | fastype_of(Ts, Abs (_,T,u)) = T --> fastype_of(T::Ts, u);
   1.145 +
   1.146 +
   1.147 +(* maps  (x1,...,xn)t   to   t  *)
   1.148 +fun strip_abs_body (Abs(_,_,t))  =  strip_abs_body t  
   1.149 +  | strip_abs_body u  =  u;
   1.150 +
   1.151 +
   1.152 +(* maps  (x1,...,xn)t   to   [x1, ..., xn]  *)
   1.153 +fun strip_abs_vars (Abs(a,T,t))  =  (a,T) :: strip_abs_vars t 
   1.154 +  | strip_abs_vars u  =  [] : (string*typ) list;
   1.155 +
   1.156 +
   1.157 +fun strip_qnt_body qnt =
   1.158 +let fun strip(tm as Const(c,_)$Abs(_,_,t)) = if c=qnt then strip t else tm
   1.159 +      | strip t = t
   1.160 +in strip end;
   1.161 +
   1.162 +fun strip_qnt_vars qnt =
   1.163 +let fun strip(Const(c,_)$Abs(a,T,t)) = if c=qnt then (a,T)::strip t else []
   1.164 +      | strip t  =  [] : (string*typ) list
   1.165 +in strip end;
   1.166 +
   1.167 +
   1.168 +(* maps   (f, [t1,...,tn])  to  f(t1,...,tn) *)
   1.169 +val list_comb : term * term list -> term = foldl (op $);
   1.170 +
   1.171 +
   1.172 +(* maps   f(t1,...,tn)  to  (f, [t1,...,tn]) ; naturally tail-recursive*)
   1.173 +fun strip_comb u : term * term list = 
   1.174 +    let fun stripc (f$t, ts) = stripc (f, t::ts)
   1.175 +        |   stripc  x =  x 
   1.176 +    in  stripc(u,[])  end;
   1.177 +
   1.178 +
   1.179 +(* maps   f(t1,...,tn)  to  f , which is never a combination *)
   1.180 +fun head_of (f$t) = head_of f
   1.181 +  | head_of u = u;
   1.182 +
   1.183 +
   1.184 +(*Number of atoms and abstractions in a term*)
   1.185 +fun size_of_term (Abs (_,_,body)) = 1 + size_of_term body
   1.186 +  | size_of_term (f$t) = size_of_term f  +  size_of_term t
   1.187 +  | size_of_term _ = 1;
   1.188 +
   1.189 + 
   1.190 +(* apply a function to all types in a term *)
   1.191 +fun map_term_types f =
   1.192 +let fun map(Const(a,T)) = Const(a, f T)
   1.193 +      | map(Free(a,T)) = Free(a, f T)
   1.194 +      | map(Var(v,T)) = Var(v, f T)
   1.195 +      | map(t as Bound _)  = t
   1.196 +      | map(Abs(a,T,t)) = Abs(a, f T, map t)
   1.197 +      | map(f$t) = map f $ map t;
   1.198 +in map end;
   1.199 +
   1.200 +(* iterate a function over all types in a term *)
   1.201 +fun it_term_types f =
   1.202 +let fun iter(Const(_,T), a) = f(T,a)
   1.203 +      | iter(Free(_,T), a) = f(T,a)
   1.204 +      | iter(Var(_,T), a) = f(T,a)
   1.205 +      | iter(Abs(_,T,t), a) = iter(t,f(T,a))
   1.206 +      | iter(f$u, a) = iter(f, iter(u, a))
   1.207 +      | iter(Bound _, a) = a
   1.208 +in iter end
   1.209 +
   1.210 +
   1.211 +(** Connectives of higher order logic **)
   1.212 +
   1.213 +val propT : typ = Type("prop",[]);
   1.214 +
   1.215 +val implies = Const("==>", propT-->propT-->propT);
   1.216 +
   1.217 +fun all T = Const("all", (T-->propT)-->propT);
   1.218 +
   1.219 +fun equals T = Const("==", T-->T-->propT);
   1.220 +
   1.221 +fun flexpair T = Const("=?=", T-->T-->propT);
   1.222 +
   1.223 +(* maps  !!x1...xn. t   to   t  *)
   1.224 +fun strip_all_body (Const("all",_)$Abs(_,_,t))  =  strip_all_body t  
   1.225 +  | strip_all_body t  =  t;
   1.226 +
   1.227 +(* maps  !!x1...xn. t   to   [x1, ..., xn]  *)
   1.228 +fun strip_all_vars (Const("all",_)$Abs(a,T,t))  =
   1.229 +		(a,T) :: strip_all_vars t 
   1.230 +  | strip_all_vars t  =  [] : (string*typ) list;
   1.231 +
   1.232 +(*increments a term's non-local bound variables
   1.233 +  required when moving a term within abstractions
   1.234 +     inc is  increment for bound variables
   1.235 +     lev is  level at which a bound variable is considered 'loose'*)
   1.236 +fun incr_bv (inc, lev, u as Bound i) = if i>=lev then Bound(i+inc) else u 
   1.237 +  | incr_bv (inc, lev, Abs(a,T,body)) =
   1.238 +	Abs(a, T, incr_bv(inc,lev+1,body))
   1.239 +  | incr_bv (inc, lev, f$t) = 
   1.240 +      incr_bv(inc,lev,f) $ incr_bv(inc,lev,t)
   1.241 +  | incr_bv (inc, lev, u) = u;
   1.242 +
   1.243 +fun incr_boundvars  0  t = t
   1.244 +  | incr_boundvars inc t = incr_bv(inc,0,t);
   1.245 +
   1.246 +
   1.247 +(*Accumulate all 'loose' bound vars referring to level 'lev' or beyond.
   1.248 +   (Bound 0) is loose at level 0 *)
   1.249 +fun add_loose_bnos (Bound i, lev, js) = 
   1.250 +	if i<lev then js  else  (i-lev) :: js
   1.251 +  | add_loose_bnos (Abs (_,_,t), lev, js) = add_loose_bnos (t, lev+1, js)
   1.252 +  | add_loose_bnos (f$t, lev, js) =
   1.253 +	add_loose_bnos (f, lev, add_loose_bnos (t, lev, js)) 
   1.254 +  | add_loose_bnos (_, _, js) = js;
   1.255 +
   1.256 +fun loose_bnos t = add_loose_bnos (t, 0, []);
   1.257 +
   1.258 +(* loose_bvar(t,k) iff t contains a 'loose' bound variable referring to
   1.259 +   level k or beyond. *)
   1.260 +fun loose_bvar(Bound i,k) = i >= k
   1.261 +  | loose_bvar(f$t, k) = loose_bvar(f,k) orelse loose_bvar(t,k)
   1.262 +  | loose_bvar(Abs(_,_,t),k) = loose_bvar(t,k+1)
   1.263 +  | loose_bvar _ = false;
   1.264 +
   1.265 +
   1.266 +(*Substitute arguments for loose bound variables.
   1.267 +  Beta-reduction of arg(n-1)...arg0 into t replacing (Bound i) with (argi).
   1.268 +  Note that for ((x,y)c)(a,b), the bound vars in c are x=1 and y=0
   1.269 +	and the appropriate call is  subst_bounds([b,a], c) .
   1.270 +  Loose bound variables >=n are reduced by "n" to
   1.271 +     compensate for the disappearance of lambdas.
   1.272 +*)
   1.273 +fun subst_bounds (args: term list, t) : term = 
   1.274 +  let val n = length args;
   1.275 +      fun subst (t as Bound i, lev) =
   1.276 + 	    if i<lev then  t    (*var is locally bound*)
   1.277 +	    else  (case (drop (i-lev,args)) of
   1.278 +		  []     => Bound(i-n)  (*loose: change it*)
   1.279 +	        | arg::_ => incr_boundvars lev arg)
   1.280 +	| subst (Abs(a,T,body), lev) = Abs(a, T,  subst(body,lev+1))
   1.281 +	| subst (f$t, lev) =  subst(f,lev)  $  subst(t,lev)
   1.282 +	| subst (t,lev) = t
   1.283 +  in   case args of [] => t  | _ => subst (t,0)  end;
   1.284 +
   1.285 +(*beta-reduce if possible, else form application*)
   1.286 +fun betapply (Abs(_,_,t), u) = subst_bounds([u],t)
   1.287 +  | betapply (f,u) = f$u;
   1.288 +
   1.289 +(*Tests whether 2 terms are alpha-convertible and have same type.
   1.290 +  Note that constants and Vars may have more than one type.*)
   1.291 +infix aconv;
   1.292 +fun (Const(a,T)) aconv (Const(b,U)) = a=b  andalso  T=U
   1.293 +  | (Free(a,T)) aconv (Free(b,U)) = a=b  andalso  T=U
   1.294 +  | (Var(v,T)) aconv (Var(w,U)) =   v=w  andalso  T=U
   1.295 +  | (Bound i) aconv (Bound j)  =   i=j
   1.296 +  | (Abs(_,T,t)) aconv (Abs(_,U,u)) = t aconv u  andalso  T=U
   1.297 +  | (f$t) aconv (g$u) = (f aconv g) andalso (t aconv u)
   1.298 +  | _ aconv _  =  false;
   1.299 +
   1.300 +(*are two term lists alpha-convertible in corresponding elements?*)
   1.301 +fun aconvs ([],[]) = true
   1.302 +  | aconvs (t::ts, u::us) = t aconv u andalso aconvs(ts,us)
   1.303 +  | aconvs _ = false;
   1.304 +
   1.305 +(*A fast unification filter: true unless the two terms cannot be unified. 
   1.306 +  Terms must be NORMAL.  Treats all Vars as distinct. *)
   1.307 +fun could_unify (t,u) =
   1.308 +  let fun matchrands (f$t, g$u) = could_unify(t,u) andalso  matchrands(f,g)
   1.309 +	| matchrands _ = true
   1.310 +  in case (head_of t , head_of u) of
   1.311 +	(_, Var _) => true
   1.312 +      | (Var _, _) => true
   1.313 +      | (Const(a,_), Const(b,_)) =>  a=b andalso matchrands(t,u)
   1.314 +      | (Free(a,_), Free(b,_)) =>  a=b andalso matchrands(t,u)
   1.315 +      | (Bound i, Bound j) =>  i=j andalso matchrands(t,u)
   1.316 +      | (Abs _, _) =>  true   (*because of possible eta equality*)
   1.317 +      | (_, Abs _) =>  true
   1.318 +      | _ => false
   1.319 +  end;
   1.320 +
   1.321 +(*Substitute new for free occurrences of old in a term*)
   1.322 +fun subst_free [] = (fn t=>t)
   1.323 +  | subst_free pairs =
   1.324 +      let fun substf u = 
   1.325 +	    case gen_assoc (op aconv) (pairs, u) of
   1.326 +		Some u' => u'
   1.327 +	      | None => (case u of Abs(a,T,t) => Abs(a, T, substf t)
   1.328 +				 | t$u' => substf t $ substf u'
   1.329 +				 | _ => u)
   1.330 +      in  substf  end;
   1.331 +
   1.332 +(*a total, irreflexive ordering on index names*)
   1.333 +fun xless ((a,i), (b,j): indexname) = i<j  orelse  (i=j andalso a<b);
   1.334 +
   1.335 +
   1.336 +(*Abstraction of the term "body" over its occurrences of v, 
   1.337 +    which must contain no loose bound variables.
   1.338 +  The resulting term is ready to become the body of an Abs.*)
   1.339 +fun abstract_over (v,body) =
   1.340 +  let fun abst (lev,u) = if (v aconv u) then (Bound lev) else
   1.341 +      (case u of
   1.342 +          Abs(a,T,t) => Abs(a, T, abst(lev+1, t))
   1.343 +	| f$rand => abst(lev,f) $ abst(lev,rand)
   1.344 +	| _ => u)
   1.345 +  in  abst(0,body)  end;
   1.346 +
   1.347 +
   1.348 +(*Form an abstraction over a free variable.*)
   1.349 +fun absfree (a,T,body) = Abs(a, T, abstract_over (Free(a,T), body));
   1.350 +
   1.351 +(*Abstraction over a list of free variables*)
   1.352 +fun list_abs_free ([ ] ,     t) = t
   1.353 +  | list_abs_free ((a,T)::vars, t) = 
   1.354 +      absfree(a, T, list_abs_free(vars,t));
   1.355 +
   1.356 +(*Quantification over a list of free variables*)
   1.357 +fun list_all_free ([], t: term) = t
   1.358 +  | list_all_free ((a,T)::vars, t) = 
   1.359 +        (all T) $ (absfree(a, T, list_all_free(vars,t)));
   1.360 +
   1.361 +(*Quantification over a list of variables (already bound in body) *)
   1.362 +fun list_all ([], t) = t
   1.363 +  | list_all ((a,T)::vars, t) = 
   1.364 +        (all T) $ (Abs(a, T, list_all(vars,t)));
   1.365 +
   1.366 +(*Replace the ATOMIC term ti by ui;    instl = [(t1,u1), ..., (tn,un)]. 
   1.367 +  A simultaneous substitution:  [ (a,b), (b,a) ] swaps a and b.  *)
   1.368 +fun subst_atomic [] t = t : term
   1.369 +  | subst_atomic (instl: (term*term) list) t =
   1.370 +      let fun subst (Abs(a,T,body)) = Abs(a, T, subst body)
   1.371 +	    | subst (f$t') = subst f $ subst t'
   1.372 +	    | subst t = (case assoc(instl,t) of
   1.373 +		           Some u => u  |  None => t)
   1.374 +      in  subst t  end;
   1.375 +
   1.376 +fun typ_subst_TVars iTs T = if null iTs then T else
   1.377 +  let fun subst(Type(a,Ts)) = Type(a, map subst Ts)
   1.378 +	| subst(T as TFree _) = T
   1.379 +	| subst(T as TVar(ixn,_)) =
   1.380 +            (case assoc(iTs,ixn) of None => T | Some(U) => U)
   1.381 +  in subst T end;
   1.382 +
   1.383 +val subst_TVars = map_term_types o typ_subst_TVars;
   1.384 +
   1.385 +fun subst_Vars itms t = if null itms then t else
   1.386 +  let fun subst(v as Var(ixn,_)) =
   1.387 +            (case assoc(itms,ixn) of None => v | Some t => t)
   1.388 +        | subst(Abs(a,T,t)) = Abs(a,T,subst t)
   1.389 +        | subst(f$t) = subst f $ subst t
   1.390 +        | subst(t) = t
   1.391 +  in subst t end;
   1.392 +
   1.393 +fun subst_vars(iTs,itms) = if null iTs then subst_Vars itms else
   1.394 +  let fun subst(Const(a,T)) = Const(a,typ_subst_TVars iTs T)
   1.395 +        | subst(Free(a,T)) = Free(a,typ_subst_TVars iTs T)
   1.396 +        | subst(v as Var(ixn,T)) = (case assoc(itms,ixn) of
   1.397 +            None   => Var(ixn,typ_subst_TVars iTs T)
   1.398 +          | Some t => t)
   1.399 +        | subst(b as Bound _) = b
   1.400 +        | subst(Abs(a,T,t)) = Abs(a,typ_subst_TVars iTs T,subst t)
   1.401 +        | subst(f$t) = subst f $ subst t
   1.402 +  in subst end;
   1.403 +
   1.404 +
   1.405 +(*Computing the maximum index of a typ*)
   1.406 +fun maxidx_of_typ(Type(_,Ts)) =
   1.407 +	if Ts=[] then ~1 else max(map maxidx_of_typ Ts)
   1.408 +  | maxidx_of_typ(TFree _) = ~1
   1.409 +  | maxidx_of_typ(TVar((_,i),_)) = i;
   1.410 +
   1.411 +
   1.412 +(*Computing the maximum index of a term*)
   1.413 +fun maxidx_of_term (Const(_,T)) = maxidx_of_typ T
   1.414 +  | maxidx_of_term (Bound _) = ~1
   1.415 +  | maxidx_of_term (Free(_,T)) = maxidx_of_typ T
   1.416 +  | maxidx_of_term (Var ((_,i), T)) = max[i, maxidx_of_typ T]
   1.417 +  | maxidx_of_term (Abs (_,T,body)) = max[maxidx_of_term body, maxidx_of_typ T]
   1.418 +  | maxidx_of_term (f$t) = max [maxidx_of_term f,  maxidx_of_term t];
   1.419 +
   1.420 +
   1.421 +(* Increment the index of all Poly's in T by k *)
   1.422 +fun incr_tvar k (Type(a,Ts)) = Type(a, map (incr_tvar k) Ts)
   1.423 +  | incr_tvar k (T as TFree _) = T
   1.424 +  | incr_tvar k (TVar((a,i),rs)) = TVar((a,i+k),rs);
   1.425 +
   1.426 +
   1.427 +(**** Syntax-related declarations ****)
   1.428 +
   1.429 +
   1.430 +(*Dummy type for parsing.  Will be replaced during type inference. *)
   1.431 +val dummyT = Type("dummy",[]);
   1.432 +
   1.433 +(*scan a numeral of the given radix, normally 10*)
   1.434 +fun scan_radixint (radix: int, cs) : int * string list =
   1.435 +  let val zero = ord"0"
   1.436 +      val limit = zero+radix
   1.437 +      fun scan (num,[]) = (num,[])
   1.438 +	| scan (num, c::cs) =
   1.439 +	      if  zero <= ord c  andalso  ord c < limit
   1.440 +	      then scan(radix*num + ord c - zero, cs)
   1.441 +	      else (num, c::cs)
   1.442 +  in  scan(0,cs)  end;
   1.443 +
   1.444 +fun scan_int cs = scan_radixint(10,cs);
   1.445 +
   1.446 +
   1.447 +(*** Printing ***)
   1.448 +
   1.449 +
   1.450 +(*Makes a variant of the name c distinct from the names in bs.
   1.451 +  First attaches the suffix "a" and then increments this. *)
   1.452 +fun variant bs c : string =
   1.453 +  let fun vary2 c = if (c mem bs) then  vary2 (bump_string c)  else  c
   1.454 +      fun vary1 c = if (c mem bs) then  vary2 (c ^ "a")  else  c
   1.455 +  in  vary1 (if c="" then "u" else c)  end;
   1.456 +
   1.457 +(*Create variants of the list of names, with priority to the first ones*)
   1.458 +fun variantlist ([], used) = []
   1.459 +  | variantlist(b::bs, used) = 
   1.460 +      let val b' = variant used b
   1.461 +      in  b' :: variantlist (bs, b'::used)  end;
   1.462 +
   1.463 +(** TFrees and TVars **)
   1.464 +
   1.465 +(*maps  (bs,v)  to   v'::bs    this reverses the identifiers bs*)
   1.466 +fun add_new_id (bs, c) : string list =  variant bs c  ::  bs;
   1.467 +
   1.468 +(*Accumulates the names in the term, suppressing duplicates.
   1.469 +  Includes Frees and Consts.  For choosing unambiguous bound var names.*)
   1.470 +fun add_term_names (Const(a,_), bs) = a ins bs
   1.471 +  | add_term_names (Free(a,_), bs) = a ins bs
   1.472 +  | add_term_names (f$u, bs) = add_term_names (f, add_term_names(u, bs))
   1.473 +  | add_term_names (Abs(_,_,t), bs) = add_term_names(t,bs)
   1.474 +  | add_term_names (_, bs) = bs;
   1.475 +
   1.476 +(*Accumulates the TVars in a type, suppressing duplicates. *)
   1.477 +fun add_typ_tvars(Type(_,Ts),vs) = foldr add_typ_tvars (Ts,vs)
   1.478 +  | add_typ_tvars(TFree(_),vs) = vs
   1.479 +  | add_typ_tvars(TVar(v),vs) = v ins vs;
   1.480 +
   1.481 +(*Accumulates the TFrees in a type, suppressing duplicates. *)
   1.482 +fun add_typ_tfree_names(Type(_,Ts),fs) = foldr add_typ_tfree_names (Ts,fs)
   1.483 +  | add_typ_tfree_names(TFree(f,_),fs) = f ins fs
   1.484 +  | add_typ_tfree_names(TVar(_),fs) = fs;
   1.485 +
   1.486 +fun add_typ_tfrees(Type(_,Ts),fs) = foldr add_typ_tfrees (Ts,fs)
   1.487 +  | add_typ_tfrees(TFree(f),fs) = f ins fs
   1.488 +  | add_typ_tfrees(TVar(_),fs) = fs;
   1.489 +
   1.490 +(*Accumulates the TVars in a term, suppressing duplicates. *)
   1.491 +val add_term_tvars = it_term_types add_typ_tvars;
   1.492 +val add_term_tvar_ixns = (map #1) o (it_term_types add_typ_tvars);
   1.493 +
   1.494 +(*Accumulates the TFrees in a term, suppressing duplicates. *)
   1.495 +val add_term_tfrees = it_term_types add_typ_tfrees;
   1.496 +val add_term_tfree_names = it_term_types add_typ_tfree_names;
   1.497 +
   1.498 +(*Non-list versions*)
   1.499 +fun typ_tfrees T = add_typ_tfrees(T,[]);
   1.500 +fun typ_tvars T = add_typ_tvars(T,[]);
   1.501 +fun term_tfrees t = add_term_tfrees(t,[]);
   1.502 +fun term_tvars t = add_term_tvars(t,[]);
   1.503 +
   1.504 +(** Frees and Vars **)
   1.505 +
   1.506 +(*a partial ordering (not reflexive) for atomic terms*)
   1.507 +fun atless (Const (a,_), Const (b,_))  =  a<b
   1.508 +  | atless (Free (a,_), Free (b,_)) =  a<b
   1.509 +  | atless (Var(v,_), Var(w,_))  =  xless(v,w)
   1.510 +  | atless (Bound i, Bound j)  =   i<j
   1.511 +  | atless _  =  false;
   1.512 +
   1.513 +(*insert atomic term into partially sorted list, suppressing duplicates (?)*)
   1.514 +fun insert_aterm (t,us) =
   1.515 +  let fun inserta [] = [t]
   1.516 +        | inserta (us as u::us') = 
   1.517 +	      if atless(t,u) then t::us
   1.518 +	      else if t=u then us (*duplicate*)
   1.519 +	      else u :: inserta(us')
   1.520 +  in  inserta us  end;
   1.521 +
   1.522 +(*Accumulates the Vars in the term, suppressing duplicates*)
   1.523 +fun add_term_vars (t, vars: term list) = case t of
   1.524 +    Var   _ => insert_aterm(t,vars)
   1.525 +  | Abs (_,_,body) => add_term_vars(body,vars)
   1.526 +  | f$t =>  add_term_vars (f, add_term_vars(t, vars))
   1.527 +  | _ => vars;
   1.528 +
   1.529 +fun term_vars t = add_term_vars(t,[]);
   1.530 +
   1.531 +(*Accumulates the Frees in the term, suppressing duplicates*)
   1.532 +fun add_term_frees (t, frees: term list) = case t of
   1.533 +    Free   _ => insert_aterm(t,frees)
   1.534 +  | Abs (_,_,body) => add_term_frees(body,frees)
   1.535 +  | f$t =>  add_term_frees (f, add_term_frees(t, frees))
   1.536 +  | _ => frees;
   1.537 +
   1.538 +fun term_frees t = add_term_frees(t,[]);
   1.539 +
   1.540 +(*Given an abstraction over P, replaces the bound variable by a Free variable
   1.541 +  having a unique name. *)
   1.542 +fun variant_abs (a,T,P) =
   1.543 +  let val b = variant (add_term_names(P,[])) a
   1.544 +  in  (b,  subst_bounds ([Free(b,T)], P))  end;
   1.545 +
   1.546 +(* renames and reverses the strings in vars away from names *)
   1.547 +fun rename_aTs names vars : (string*typ)list =
   1.548 +  let fun rename_aT (vars,(a,T)) =
   1.549 +		(variant (map #1 vars @ names) a, T) :: vars
   1.550 +  in foldl rename_aT ([],vars) end;
   1.551 +
   1.552 +fun rename_wrt_term t = rename_aTs (add_term_names(t,[]));