A better name which clarifies a bit what you want would be
occurs_in(Var, Term)
Many approaches can use the same techniques as for var_in_vars/2
.
This works in any ISO conforming system. It could be quite efficient, too ; that is, being (in the best case) independent of the size of Term
.
occurs_in(Var, Term) :-
\+ subsumes_term(Var, Term).
The next is good as long as you do not have any constraints:
occurs_in(Var, Term) :-
\+ unify_with_occurs_check(Var, Term).
If you insist on using contains_term/2
— (the name term_subterm/2
would be much more appropriate):
occurs_in(Var, Term) :-
once( ( contains_term(Term, Subterm), Var == Subterm ) ).
Or, to take into account Prolog superstitions:
occurs_in(Var, Term) :-
\+ \+ ( contains_term(Term, Subterm), Var == Subterm ).