Let’s say I have a really complicated term that is any combination of lists, atoms, predicates, etc. Worse, the items have child items themselves of the same nature. So I have a single term, but one that is composed of a completely free form nature of items, with child items containing child items, recursively with (possibly) interleaved lists, atomics, etc.
Is there an SWI-Prolog predicate that can take a desired predicate name, and scan the entire tree of a term and find occurences of the desried predicate anywhere it might exist in the parent term’s tree of items?
I think I can write such a function, but it will be messy and tricky given the interwined nature of the clauses needed to switch between lists, compound terms, and atomics as you descend recursively through the parent term’s structure. I’m hoping there might be something in SWI-Prolog or one of its packages that does this.
For example. Supposed I was looking for a predicate named
my_pred_to_find in a complex term named
Target = parent_term( a, b, c, child_predicate( [1, 2, 3, my_pred_to_find( [dog, nested_cat([fluffy, furry, paw(has_toes)])])])), find_predicate_anywhere(my_pred_to_find, Target, PredFound).
The hypothetical function named
find_predicate_anywhere would bind
ValueOfPredFound = [dog, nested_cat([fluffy, furry, paw(has_toes)])].
Is there such a function/predicate in SWI-Prolog or one of its packages?