What you are seeing is correct. If you are expecting something different, you probably don’t understand variables in Prolog. I suggest posting your question into ChatGPT. It will explain why the above is correct and will be able to explain how variables work. You can then ask questions with instantaneous followup, rather than waiting hours on this discourse channel to clarify your misunderstanding.
test(X) :-
( var(X)
-> write(X), write(' is a variable')
; write(X), write(' is not a variable')
).
?- test('Azer').
...
?- test(Reza).
...
What will be the learning benefit?
BTW: Now I have trust issues with https://zzzcode.ai/, it was charming
that it suggested me to toy around with the modified test/1, but it then
somehow showed two errors in one place:
Each time a variable is “created” during execution, it gets a unique name. e.g.:
?- test(A).
_12244 is a variable
true.
?- test(A).
_13854 is a variable
true.
You’re getting this because Prolog’s interactive top-level doesn’t print variables that are completely unconstrained. Perhaps this will give help you understand what’s going on (where the A = B is printed by the interactive top-level):
?- A=B,test(A).
_18804 is a variable
A = B.
?- A=B,test(A).
_20718 is a variable
A = B.
In plain Prolog, yes. Logical variables carry no name in the Prolog execution model. They should be considered holes (or placeholders) that can change to a concrete term. In that case, the variable “is gone”. That is pretty different from imperative languages where a variable is some location in memory that can get a value (and after that another values, etc.) and a symbol table associates this memory location to its name (bit over simplified).
The toplevel can do
?- A = X.
A = X.
Which kind of suggests variables have a name. This is still not the case. If is merely that the toplevel keeps a mapping of variables used in the query to its name and uses this table to create pretty output. Old systems tended to write
?- A = X.
X = _334785473
A = _334785473.
What you can do in most modern systems is to use attributes on variables. You can use that to name a variable using e.g. put_attr(A, name, ‘A’). But, after we unify A with 42, the variable is gone and nothing will tell us about A = 42. Unifying a variable with another variable creates a “alias” between them, meaning binding either will bind both. What name should it have? Note this is quite different from imperative languages where a = b means copy the value of b to a. Prolog A = B, if both are variables simply state these have the same value from now on. Internally, one of them is filled with a pointer (reference) to the other. Any operation on variables first dereferences such pointers and then act on the single location where the value of all aliased variables is stored. The network of aliased variables is a directed acyclic graph with a single drain.