Use this. Note that the numbersvars(true)
option of the write family just tell these predicates to interpret '$VAR'(N)
as A, B, … You have to number the variables yourself. Note that if you need to print multiple terms with possible sharing variables you should numbervars them together, for example by passing a list to numbervars/4.
Finally, the double negation is to make sure that the bindings are undone before proceeding and thus the term is not modified by the operation.
\+ \+ ( numbervars(Term, 0, _, [singletons(true)]),
format('~W', [Term, [numbervars(true)]])
)