Alphabetical order between two free variables using term_to_atom/2

I’m using: SWI-Prolog version 8.3.11 Running on SWISH

I want the code to: get names of two free variables, in two terms, as atom or string, and know their alphabetical order.

But what I’m getting is: the alphabetical order isn’t respected in my tests

My code looks like this:

?-term_to_atom(B,AtomA),term_to_atom(D,AtomB),AtomA @< AtomB.

How about using `term_string/3’, with which you can get names of variables, though I am not sure this helps you.

?- term_string(T, 'f(A)', [variable_names(Vnames)]).
T = f(_7810),
Vnames = ['A'=_7810].

Thank you for the answer; there’s big difference between the use of ‘term_string/3’ and ‘term_to_atom/2’ in terms of “results”? I want to obtain an atom(or string) from a var in a term; i think (but i’m new in Prolog) that the type of output i need (atom or string) not change, so it’s the same if i use ‘term_string/3’ or ‘term_to_atom/2’; this not affect the analysis of order, right? On SWI-Prolog version 8.1.22 i don’t have such problems, the query in my post give different result on SWISH.

I dont know definition of the result Y of term_to_atom(X, Y) when X is unboud. So, such difference is not so strange for me, but I believe some readers are able to answer precisely to your question.

The real problem is that Prolog variables do not have names :slight_smile: So, term_to_atom(A, X) produces something like X = _2422 or some other weird number. When you do the comparison of these two mostly meaningless numbers you get a meaningless result.

One of the few things that give variables names in normal Prolog is read_term/3 with the option variable_names(Names).

In fact, you can also get access to the query variable names in SWISH using

?- b_getval('$variable_names', Names).

Still, as a beginner it is better to get used to Prolog variables as things without a name.

2 Likes