BOOL all_args_vars(Term t);This Boolean routine checks if all argumets of Term t are VARIABLEs. (It is true also if t is a VARIABLE.)
int arg_position(Term parent, Term child);If the given terms are in a parent-child relatioship, return the argument position (index) of the child. Otherwise, return -1.
int biggest_variable(Term t);This routine returns the greatest variable index of any variable int the given term t. If t is ground, -1 is returned.
Term build_binary_term(int sn, Term a1, Term a2);Build and return a binary term with SYMNUM sn, first term a1, and second term a2.
WARNING: if sn is not a binary symbol, bad things will happen!
Term build_unary_term(int sn, Term a);Build and return a unary term with SYMNUM sn and argument term a.
WARNING: if sn is not a unary symbol, bad things will happen!
char * term_symbol(Term t);Return the print string associated with the given nonvariable term. If the term is a variable, return NULL.
Plist constants_in_term(Term t, Plist p);Given a term t, return a plist containing the set of constants in t.
Term copy_term(Term t);This routine copies a term. Only the symbols and structure are copied---any extra fields such as bits or u are NOT copied.
Term entirely_new_term(Term t);The purpose of this routine is to get the term ready for FPA indexing.
Call it like this: x->t = entirely_new_term(x->t);
void fprint_term(FILE *fp, Term t);This routine prints (to FILE *fp) a term. A newline is NOT printed.
void fprint_term_mem(FILE *fp, BOOL heading);This routine prints (to FILE *fp) memory usage statistics for Terms. The Boolean argument heading tells whether to print a heading on the table.
void free_term(Term p);This routine frees a term node only. If you wish to recursively free all of the subterms as well, call zap_term(t) instead.
Term get_rigid_term(char *sym, int arity);This routine allocates and returns a term node with the given symbol and arity. If you already have a similar term node, say t, (containing the symbol and arity you need) call get_rigid_term_like(t) instead.
Term get_rigid_term_dangerously(int symnum, int arity);This routine can be used to allocate a term node if all you have is the symbol ID and arity. If the arity is not correct for the symbol ID, terrible things will happen!
If you have a similar term, use get_rigid_term_like() instead. If you can afford the time to access the symbol table, use sn_to_str() and get_rigid_term() instead.
Term get_rigid_term_like(Term t);This routine allocates and returns a term node with the same symbol and arity as the given Term t.
Term get_variable_term(int var_num);This routine returns a term of type VARIABLE. The index of the variable is set to var_num, which should be an integer >= 0.
int greatest_symnum_in_term(Term t);This function returns the greatest SYMNUM (of a CONSTANT or COMPLEX term) in the given Term t. If the term is a VARIABLE, return -1.
int greatest_variable(Term t);This routine returns the greatest variable index in a term. If the term is ground, -1 is returned.
BOOL ground_term(Term t);This function checks if a term is ground, that is, has no variables.
BOOL is_term(Term t, char *str, int arity);Does term t have the the given symbol and arity?
Plist multiset_of_variables(Term t);
Plist multiset_of_vars(Term t, Plist vars);
Term nat_to_term(int n);This routine takes a nonnegative integer and returns a constant Term with the string representation of the integer as the constant symbol.
int natural_constant(Term t);This routine takes a term, and if the term represents an nonnegative integer, that integer is returned; otherwise, -1 is returned.
Term new_term_top(Term t1);Replace the top node of a term with a new node. The new node has an address greater than any other term currently in use. The container pointer and private_flags are copied.
If TERM_ID_FIELD is defined, the new node gets a term ID greater than any other currently in use.
The purpose of this routine is to get the term ready for FPA indexing.
Call it like this: x->t = new_term_top(x->t);
int occurrences(Term t, Term target);This function returns the number of occurrences of Term target in Term t. The checks are made with term_ident().
BOOL occurs_in(Term t1, Term t2);This function checks if Term t2 is identical to a subterm of Term t1, including the case term_ident(t1,t2). All identity checks are done with term_ident(), so extra fields such as bits or u are not checked.
void p_term(Term t);This routine prints a term, followed by '\n' and fflush, to stdout. If you don't want the newline, use fprint_term() instead. If you want the term put into a string, use sprint_term() instead.
void p_term_mem(void);This routine prints memory usage statistics for Terms to stdout.
BOOL plist_member_term(Plist lst, Term t);This function checks if a term is a member of a Plist. It assumes the Plist contains terms, and term_ident is used.
Term renum_vars_recurse(Term t, int vmap[], int max_vars);This routine renumbers the variables of a term. It is assumed that vmap has been filled with -1 on the initial call and that the size of vmap is at least max_vars.
This returns a Term instead of being void, in case the given term is itself a variable. (Recall that variables may be shared, so we can't just change a variable's index.
Term set_vars_recurse(Term t, char *vnames[], int max_vars);This routine sets the variables of a term. It is assumed that vnames has been filled with NULL on the initial call and that the size of vnames is at least max_vars.
This returns a Term instead of being void, in case the given term is itself becomes a variable.
void sprint_term(String_buf sb, Term t);This (recursive) routine appends the string representation of a term to a String_buf. A newline is not included.
Term subst_term(Term t, Term target, Term replacement);In term t, replace all occurrences of Term target with copies of Term replacement. Free all of the replaced terms;
Term subst_var_term(Term t, int symnum, int varnum);In Term t, replace all CONSTANT terms containing SYMNUM symnum with a variable containing VARNUM varnum. Free the replaced constants and return the result.
int symbol_count(Term t);This routine returns the total number of symbols (i.e., the number of nodes) in the given term t.
Ilist symbols_in_term(Term t, Ilist g);This routine collects the set of nonvariable symbols in a term. An Ilist of symbol IDs (symnums) is returned
BOOL term_ident(Term t1, Term t2);This function checks if two terms are identical. Only the structure and symbols are checked---any extra fields such as bits or u are NOT checked.
void term_set_variables(Term t, int max_vars);This routine traverses a term and changes the constants that should be variables, into variables. On input, the term should have no variables. The new variables are numbered 0, 1, 2 ... according the the first occurrence, reading from the left.
A fatal error occurs if there are more than max_vars variables.
If you are dealing with clauses, use clause_set_variables() instead.
BOOL term_to_int(Term t, int *result);Given a term, see if it represents an integer. If so, set *result to the integer and return TRUE. If not, return FALSE.
The term representation of a negative integer is the function symbol "-" applied to a nonnegative integer.
void upward_term_links(Term t, void *p);In the given Term t, make the "container" field of t and each subterm, except possibly variables, point to (void *) p. (We're experimenting with shared variables.)
BOOL variables_subset(Term t1, Term t2);
void zap_term(Term t);This routine frees a term t and all of its subterms. You should not refer to t after calling zap_term(t).
void zap_variables(void);
#define MAX_VARS 100 /* max number of (distinct) variables per term */ #define MAX_VNUM 1000 /* maximum variable ID, for array of vars */ #define MAX_VAR SHRT_MAX /* max var ID that fits in sym field of term */ #define MAX_SYM (-SHRT_MIN) /* max ID of any rigid symbol */ #define MAX_ARITY UCHAR_MAX /* max arity of any term (don't make this big) */ #define FLAGS_TYPE unsigned char /* for private_flags field of Term */ typedef struct term * Term; /* Term is a pointer to a term struct */ struct term { short private_symbol; /* const/func/pred/var symbol ID */ unsigned char arity; /* number of auguments */ FLAGS_TYPE private_flags; /* for marking terms in various ways */ Term *args; /* array (of size arity) of pointers to args */ void *container; /* an object that contains the term */ #ifdef TERM_ID_FIELD unsigned id; /* unique ID */ #endif #ifdef TERM_VP_FIELD void *vp; /* auxiliary pointer */ #endif }; /* to check type of term */ #define VARIABLE(t) ((t)->private_symbol >= 0) #define CONSTANT(t) ((t)->private_symbol < 0 && (t)->arity == 0) #define COMPLEX(t) ((t)->private_symbol < 0 && (t)->arity > 0) /* to get symbol ID from a CONSTANT or COMPLEX term */ #define SYMNUM(t) (-((t)->private_symbol)) /* to get the variable number of a VARIABLE term */ #define VARNUM(t) ((t)->private_symbol) /* to get the arity of a term (VARIABLE terms have arity 0) */ #define ARITY(t) ((t)->arity) /* to get the i-th argument of a term (make sure i is in [0..arity-1]) */ #define ARG(t,i) ((t)->args[i])
No
There are three types of term, and the Boolean macros VARIABLE(t), CONSTANT(t), and COMPLEX(t) should be used to find out what type a term is. If you have a CONSTANT or COMPLEX term t, you can get its symbol id with SYMNUM(t) (from which you can get other information about the symbol such as the print string and any special properties). If you have a variable t, you can get its index with VARNUM(t), which is a signed integral type in the range [0..MAX_VAR]. Warning: MAX_VAR is a big number---a higher-level unification package will typically have a much smaller MAX_VARS defined for array sizes, because it does array indexing with VARNUM(t).
The macro ARITY(t) gets the arity of a term (constants and variables have arity 0), and ARG(t,i) gets the i-th argument (counting from 0) of a term. When using ARG(t,i), make sure that i is in range [0..ARITY(t)-1], because ARG does not check.
Here is an example of recursing through a term.
int symbol_count(Term t) { int count = 0; int i; for (i = 0; i < ARITY(t); i++) count += symbol_count(ARG(t,i)); return count+1; }
These activities are projects of the Mathematics and Computer Science Division of Argonne National Laboratory.