pub struct TermUtils;Expand description
Term manipulation utilities
Implementations§
Source§impl TermUtils
impl TermUtils
Sourcepub fn get_all_variables(term: &Term) -> HashSet<String>
pub fn get_all_variables(term: &Term) -> HashSet<String>
Get all variables in a term (including nested ones)
Collects all unique variable names that appear anywhere in the term. This includes variables nested inside compound terms.
Sourcepub fn term_depth(term: &Term) -> usize
pub fn term_depth(term: &Term) -> usize
Count the depth of nested terms
Returns the maximum nesting depth of a term. Simple terms (atoms, variables, numbers) have depth 1. Compound terms have depth 1 + max depth of arguments.
Sourcepub fn term_size(term: &Term) -> usize
pub fn term_size(term: &Term) -> usize
Count the total number of subterms
Returns the total size of a term, counting all nodes in the tree. This includes the term itself and all nested subterms.
Sourcepub fn contains_variable(term: &Term, var_name: &str) -> bool
pub fn contains_variable(term: &Term, var_name: &str) -> bool
Check if a term contains a specific variable
Returns true if the given variable name appears anywhere in the term. This is useful for checking variable dependencies.
Sourcepub fn replace_variable(term: &Term, var_name: &str, replacement: &Term) -> Term
pub fn replace_variable(term: &Term, var_name: &str, replacement: &Term) -> Term
Replace all occurrences of a variable with a term
Creates a new term where all instances of the specified variable are replaced with the given replacement term.
Sourcepub fn is_ground(term: &Term) -> bool
pub fn is_ground(term: &Term) -> bool
Check if a term is ground (contains no variables)
A ground term is fully instantiated with no unbound variables. This is important for certain Prolog operations.
Sourcepub fn list_to_vec(term: &Term) -> Option<Vec<Term>>
pub fn list_to_vec(term: &Term) -> Option<Vec<Term>>
Convert a Prolog list term to a Rust Vec
Prolog lists are represented as nested compound terms with the ‘.’ functor. [1,2,3] is actually .(1, .(2, .(3, []))) This function unpacks that structure into a Rust vector.
Sourcepub fn vec_to_list(elements: Vec<Term>) -> Term
pub fn vec_to_list(elements: Vec<Term>) -> Term
Convert a Rust Vec to a Prolog list term
Builds the nested structure that represents a Prolog list. Uses right-to-left folding to build from the tail up.