I'm unsure about how you're using numbervars/3, which is implemented so as to "...unify the free variables of [a term] with a term $VAR(N)", which can't be used directly to turn the term a(t1,t2,t3) into a('$VAR'(0),'$VAR'(1),'$VAR'(2)) as you suggest, unless t1, t2 and t3 are your place-holders for distinct variables.
The terms '$VAR'(N) that SWI's numbervars/3 generates are indeed terms, and not variables (as described in the manual), so can't be treated as variables.
Note that, if you see this:
?- copy_term(a('$VAR'(0),'$VAR'(1),'$VAR'(3)),Term). Term = a(A, B, D).
What is happening is that the line Term = a(A, B, D). is being written via something like write_term/2 to the console, which can be configured to portray certain terms like '$VAR'(N) as named variables (which can be misleading). Notice that if you try this instead:
?- copy_term(a('$VAR'(0),'$VAR'(1),'$VAR'(3)),Term), write_term(Term, [numbervars(false)]). a($VAR(0), $VAR(1), $VAR(3)) Term = a(A, B, D).
The call to write_term/2 here explicitly disables the numbervars option, and prints out the true bindings of the arguments of the a/3 term, which are indeed $VAR(N) terms. The next line printed to the console (i.e., Term = a(A, B, D)) is doing something akin to enabling the numbervars option instead (perhaps for readability).
If you need to 'variablize' a term, I can suggest something along these lines instead:
% takes a term T, a list of [Term:Variable] replacements R, and makes V: variablize(T, R, V) :- member(R0:V, R), R0 == T, !. variablize([T|Ts], R, [NT|NTs]) :- !, variablize(T, R, NT), variablize(Ts, R, NTs). variablize(T, R, NT) :- compound(T), !, T =.. [F|As], variablize(As, R, NAs), NT =.. [F|NAs]. variablize(T, _, T).
Example:
?- variablize(a(t1,t2,t3), [t1:X, t2:Y, t3:Z], T). T = a(X, Y, Z).
This assumes that you know which sub-elements (e.g., t2) you want replaced, and with which variables. This particular implementation could more accurately be called replace/3, as it will find-and-replace any sub-element occurrence in the input term (even if they are other variables!).