src/Pure/term.ML
changeset 949 83c588d6fee9
parent 728 9a973c3ba350
child 1029 27808dabf4af
equal deleted inserted replaced
948:3647161d15d3 949:83c588d6fee9
   187 (*Number of atoms and abstractions in a term*)
   187 (*Number of atoms and abstractions in a term*)
   188 fun size_of_term (Abs (_,_,body)) = 1 + size_of_term body
   188 fun size_of_term (Abs (_,_,body)) = 1 + size_of_term body
   189   | size_of_term (f$t) = size_of_term f  +  size_of_term t
   189   | size_of_term (f$t) = size_of_term f  +  size_of_term t
   190   | size_of_term _ = 1;
   190   | size_of_term _ = 1;
   191 
   191 
   192  
   192 fun map_type_tvar f (Type(a,Ts)) = Type(a, map (map_type_tvar f) Ts)
       
   193   | map_type_tvar f (T as TFree _) = T
       
   194   | map_type_tvar f (TVar x) = f x;
       
   195 
       
   196 fun map_type_tfree f (Type(a,Ts)) = Type(a, map (map_type_tfree f) Ts)
       
   197   | map_type_tfree f (TFree x) = f x
       
   198   | map_type_tfree f (T as TVar _) = T;
       
   199 
   193 (* apply a function to all types in a term *)
   200 (* apply a function to all types in a term *)
   194 fun map_term_types f =
   201 fun map_term_types f =
   195 let fun map(Const(a,T)) = Const(a, f T)
   202 let fun map(Const(a,T)) = Const(a, f T)
   196       | map(Free(a,T)) = Free(a, f T)
   203       | map(Free(a,T)) = Free(a, f T)
   197       | map(Var(v,T)) = Var(v, f T)
   204       | map(Var(v,T)) = Var(v, f T)
   430   | maxidx_of_term (Abs (_,T,body)) = max[maxidx_of_term body, maxidx_of_typ T]
   437   | maxidx_of_term (Abs (_,T,body)) = max[maxidx_of_term body, maxidx_of_typ T]
   431   | maxidx_of_term (f$t) = max [maxidx_of_term f,  maxidx_of_term t];
   438   | maxidx_of_term (f$t) = max [maxidx_of_term f,  maxidx_of_term t];
   432 
   439 
   433 
   440 
   434 (* Increment the index of all Poly's in T by k *)
   441 (* Increment the index of all Poly's in T by k *)
   435 fun incr_tvar k (Type(a,Ts)) = Type(a, map (incr_tvar k) Ts)
   442 fun incr_tvar k = map_type_tvar (fn ((a,i),S) => TVar((a,i+k),S));
   436   | incr_tvar k (T as TFree _) = T
       
   437   | incr_tvar k (TVar((a,i),rs)) = TVar((a,i+k),rs);
       
   438 
   443 
   439 
   444 
   440 (**** Syntax-related declarations ****)
   445 (**** Syntax-related declarations ****)
   441 
   446 
   442 
   447 
   498 
   503 
   499 fun add_typ_tfrees(Type(_,Ts),fs) = foldr add_typ_tfrees (Ts,fs)
   504 fun add_typ_tfrees(Type(_,Ts),fs) = foldr add_typ_tfrees (Ts,fs)
   500   | add_typ_tfrees(TFree(f),fs) = f ins fs
   505   | add_typ_tfrees(TFree(f),fs) = f ins fs
   501   | add_typ_tfrees(TVar(_),fs) = fs;
   506   | add_typ_tfrees(TVar(_),fs) = fs;
   502 
   507 
       
   508 fun add_typ_varnames(Type(_,Ts),nms) = foldr add_typ_varnames (Ts,nms)
       
   509   | add_typ_varnames(TFree(nm,_),nms) = nm ins nms
       
   510   | add_typ_varnames(TVar((nm,_),_),nms) = nm ins nms;
       
   511 
   503 (*Accumulates the TVars in a term, suppressing duplicates. *)
   512 (*Accumulates the TVars in a term, suppressing duplicates. *)
   504 val add_term_tvars = it_term_types add_typ_tvars;
   513 val add_term_tvars = it_term_types add_typ_tvars;
   505 val add_term_tvar_ixns = (map #1) o (it_term_types add_typ_tvars);
       
   506 
   514 
   507 (*Accumulates the TFrees in a term, suppressing duplicates. *)
   515 (*Accumulates the TFrees in a term, suppressing duplicates. *)
   508 val add_term_tfrees = it_term_types add_typ_tfrees;
   516 val add_term_tfrees = it_term_types add_typ_tfrees;
   509 val add_term_tfree_names = it_term_types add_typ_tfree_names;
   517 val add_term_tfree_names = it_term_types add_typ_tfree_names;
       
   518 
       
   519 val add_term_tvarnames = it_term_types add_typ_varnames;
   510 
   520 
   511 (*Non-list versions*)
   521 (*Non-list versions*)
   512 fun typ_tfrees T = add_typ_tfrees(T,[]);
   522 fun typ_tfrees T = add_typ_tfrees(T,[]);
   513 fun typ_tvars T = add_typ_tvars(T,[]);
   523 fun typ_tvars T = add_typ_tvars(T,[]);
   514 fun term_tfrees t = add_term_tfrees(t,[]);
   524 fun term_tfrees t = add_term_tfrees(t,[]);
   515 fun term_tvars t = add_term_tvars(t,[]);
   525 fun term_tvars t = add_term_tvars(t,[]);
       
   526 
       
   527 (*special code to enforce left-to-right collection of TVar-indexnames*)
       
   528 
       
   529 fun add_typ_ixns(ixns,Type(_,Ts)) = foldl add_typ_ixns (ixns,Ts)
       
   530   | add_typ_ixns(ixns,TVar(ixn,_)) = if ixn mem ixns then ixns else ixns@[ixn]
       
   531   | add_typ_ixns(ixns,TFree(_)) = ixns;
       
   532 
       
   533 fun add_term_tvar_ixns(Const(_,T),ixns) = add_typ_ixns(ixns,T)
       
   534   | add_term_tvar_ixns(Free(_,T),ixns) = add_typ_ixns(ixns,T)
       
   535   | add_term_tvar_ixns(Var(_,T),ixns) = add_typ_ixns(ixns,T)
       
   536   | add_term_tvar_ixns(Bound _,ixns) = ixns
       
   537   | add_term_tvar_ixns(Abs(_,T,t),ixns) =
       
   538       add_term_tvar_ixns(t,add_typ_ixns(ixns,T))
       
   539   | add_term_tvar_ixns(f$t,ixns) =
       
   540       add_term_tvar_ixns(t,add_term_tvar_ixns(f,ixns));
   516 
   541 
   517 (** Frees and Vars **)
   542 (** Frees and Vars **)
   518 
   543 
   519 (*a partial ordering (not reflexive) for atomic terms*)
   544 (*a partial ordering (not reflexive) for atomic terms*)
   520 fun atless (Const (a,_), Const (b,_))  =  a<b
   545 fun atless (Const (a,_), Const (b,_))  =  a<b