Convert atom to term give me another value the atom value is a variable

I’m using: SWI-Prolog version 8

I want the code to:

have a response look like this :

T = Call

But what I’m getting is:

_53794

My code looks like this:

term_to_atom(T,'Call'),write(T).

Smells like a bug to me. Can reproduce. Should have probably thrown a syntax_error.

On the other hand, what kind of term is that? You can make something similar using:

?- compound_name_arity(T, 'Call', 0).
T = 'Call'().

And even more importantly, what are you trying to achieve by turning an atom into a compound term?

I would like to use Call as a variable using in another predicate and Call it comes from a user sending it to me. is it possible to use the atom ‘Call’ as a variable

So, what really happens here, the atom 'Call' is taken as it comes; and in Prolog, atoms start with a small letter, and variables start with a capital letter; so, this is correctly parsed as a variable name, and so, the term you get is indeed a free variable. What you are trying to achieve though I do not quite understand.

1 Like

we have a step i have a string in input like this “Call” i sould use it as a varaiable

This is exactly what you get though; you get a variable. Maybe if you show this code in some context it would become more clear what is happening.

yes I agree but month I receive from a json object a string that looks like this “Call” and I have to use it as a variable in a predicate.

Show more code.

Have a look at term_string/3 or the older atom_to_term/3.

1 Like
{
    "var": "Call"
}
prove(Request) :-
   http_read_json(Request, DictIn,[json_object(term)]),
   format(user_output,"DictIn is: ~p~n",DictIn),
   P = [DictIn].json,
   Var =P.var 

Now I want use Var in this predicate or the content of Var is "Call" Prolog don’t accept it as variable I should do transformation term_string(V,Var) don’t work

usage of the variable Var in predicate for example deny a Call

deny(V) //must be iinterpreted by deny(Call)

i wan’t to get value in quotation mark “Call” ex : Call

i transform the “Call” to atom by atom_string after would like to transform to term conversion of Capitalize don’t work

I don’t really get it. Prolog variables have no name. Only read/1 and sister predicates ensure that two variables with the same name become the same variable, i.e., reading a(X,X) becomes a term a/2 with two variables that share. Then there is, mostly for toplevel usage, the read_term/3 variation (also term_string/3) that accepts a variable_names/1 option that gives you access to the name of the variable, so,

 ?- term_string(Term, "a(Call)", [variable_names(Names)]).
 Term = a(_48950),
 Names = ['Call'=_48950].

So, it does makes sense to have a query string and a variable for which you want to have the binding, as in (JSON) { var = "Call", query="deny(Call)" } this could mean run deny/1 and reply with the variable binding of Call. You can achieve that using term_string/3 on the query, solve the query and use the var name to look inside the variable_names list for the value of this variable.

Hope this makes some sense …

2 Likes

thank you that helps me but the number of variables is dymamic I could have more than two

term_string/3
works well

1 Like