Use of Meta-Logical Predicates

This example illustrates the use of the meta-logical predicates var/1, arg/3, and functor/3 (see Meta Logic). The goal variables(Term, L, []) instantiates variable L to a list of all the variable occurrences in Term. E.g.:

     | ?- variables(d(U*V, X, DU*V+U*DV), L, []).
     
     L = [U,V,X,DU,V,U,DV]
     
     variables(X, [X|L0], L) :- var(X), !, L = L0.
     variables(T, L0, L) :-
     %       nonvar(T),
             functor(T, _, A),
             variables(0, A, T, L0, L).
     
     variables(A, A, _, L0, L) :- !, L = L0.
     variables(A0, A, T, L0, L) :-
     %       A0<A,
             A1 is A0+1,
             arg(A1, T, X),
             variables(X, L0, L1),
             variables(A1, A, T, L1, L).