Converting Terms to Atoms preserving variable names in YAP prolog?

There are two issues involved. How to get the variable name X into the system, and how to get a term with such a variable into the atom The X you type in is read by the toplevel which converts it to a regular variable which does not have a name associated. Let see that in YAP:?

- read(Term). |: X+3*Y+X. Term = _A+3*_B+_A The : is YAP's prompt for input.

And we have entered X+3*Y+X However, the variable Term contains A and B (names chosen by the top level) in place of X and Y So the information is lost and cannot be restored once it is read by read/1 You have to access that information differently with the more general built-in for reading read_term/2,3 and the option variable_names/1? - read_term(T,variable_names(Eqs)). |: X+3*Y+X.

Eqs = 'X'=_A,'Y'=_B, T = _A+3*_B+_A So the read option variable_names/1 gives you the information to restore the variable names. For each variable read by read_term/2 there is an structure Name = Variable where Name is an atom representing the variable name. Above X is the name capital X.So much for the reading Now for the writing.

We cannot write the term directly but have to accompany it with the list Eqs Let's call the new predicate term_to_atom(Term, Eqs, Atom) In both YAP and SWI there is with_output_to(Output, Goal) which writes the output of Goal to different destinations like atom(A) So you can now use write_term/2 to write the term as you please. An example:? - with_output_to(atom(A),write_term('a b'+X,quoted(true))).

A = '\'a b\'+_131284 The variable _131284 looks very ugly. To get variables associated with their names for printing we could - in an ideal not yet existing world - implement term_to_atom/3 as follows: term_to_atom(T, Eqs, Atom) :- with_output_to(atom(A), write_term(T,variable_names(Eqs),quoted(true)) ) And use it like so:? - read_term(T,variable_names(Eqs)), term_to_atom(T, Eqs, Atom).

|: X+3*Y+X. Atom = 'X+3*Y+X', Eqs = 'X'=_A,'Y'=_B, T = _A+3*_B+_A Unfortunately, things have not advanced that far. But for the moment we can only approximate the ideal option variable_names/1 To print terms with our own variable names, variables have to be substituted in YAP by $VAR'(Codes) where Codes is a list of character codes.

This does not do exactly the same, but it is very close. This goes into a file: :- use_module(library(apply)). :- use_module(library(lambda)).

Write_eqs_term(T, Eqs) :- \+ \+ ( maplist(\Eq^( Eq = (N='$VAR'(Chs)), atom_codes(N,Chs)), Eqs), write_term(T,numbervars(true),quoted(true)) ). Term_to_atom(T, Eqs, A) :- with_output_to(atom(A), write_eqs_term(T, Eqs) ) For SWI, you would have to replace atom_codes(N,Chs) by N = Ch and install library(lambda) first.It's pre-installed in YAP In case you worry why it does not the same as a separate write option: term_to_atom/3 as defined above interferes with constraints and does not correctly render $VAR'/1.

There are two issues involved. How to get the variable name X into the system, and how to get a term with such a variable into the atom. The X you type in is read by the toplevel which converts it to a regular variable which does not have a name associated.

Let see that in YAP:? - read(Term). |: X+3*Y+X.

Term = _A+3*_B+_A The |: is YAP's prompt for input. And we have entered X+3*Y+X. However, the variable Term contains _A and _B (names chosen by the top level) in place of X and Y.So the information is lost and cannot be restored once it is read by read/1.

You have to access that information differently with the more general built-in for reading read_term/2,3 and the option variable_names/1.?- read_term(T,variable_names(Eqs)). |: X+3*Y+X. Eqs = 'X'=_A,'Y'=_B, T = _A+3*_B+_A So the read option variable_names/1 gives you the information to restore the variable names.

For each variable read by read_term/2 there is an structure Name = Variable where Name is an atom representing the variable name. Above, 'X' is the name capital X. So much for the reading.

Now for the writing. We cannot write the term directly but have to accompany it with the list Eqs. Let's call the new predicate term_to_atom(Term, Eqs, Atom).

In both YAP and SWI there is with_output_to(Output, Goal) which writes the output of Goal to different destinations like atom(A). So you can now use write_term/2 to write the term as you please. An example:?

- with_output_to(atom(A),write_term('a b'+X,quoted(true))). A = '\'a b\'+_131284'. The variable _131284 looks very ugly.

To get variables associated with their names for printing we could - in an ideal not yet existing world - implement term_to_atom/3 as follows: term_to_atom(T, Eqs, Atom) :- with_output_to(atom(A), write_term(T,variable_names(Eqs),quoted(true)) ). And use it like so:? - read_term(T,variable_names(Eqs)), term_to_atom(T, Eqs, Atom).

|: X+3*Y+X. Atom = 'X+3*Y+X', Eqs = 'X'=_A,'Y'=_B, T = _A+3*_B+_A Unfortunately, things have not advanced that far. But for the moment we can only approximate the ideal option variable_names/1.

To print terms with our own variable names, variables have to be substituted in YAP by '$VAR'(Codes) where Codes is a list of character codes. This does not do exactly the same, but it is very close. This goes into a file: :- use_module(library(apply)).

:- use_module(library(lambda)). Write_eqs_term(T, Eqs) :- \+ \+ ( maplist(\Eq^( Eq = (N='$VAR'(Chs)), atom_codes(N,Chs)), Eqs), write_term(T,numbervars(true),quoted(true)) ). Term_to_atom(T, Eqs, A) :- with_output_to(atom(A), write_eqs_term(T, Eqs) ).

For SWI, you would have to replace atom_codes(N,Chs) by N = Ch. And install library(lambda) first. It's pre-installed in YAP.

In case you worry why it does not the same as a separate write option: term_to_atom/3 as defined above interferes with constraints and does not correctly render '$VAR'/1.

Thanks for the answer. I am a bit puzzled, could you be so kind of giving me an example?. I understood from your answer that it is not term_to_atom/2 what I should use, but still I have not gotten how to do it with with_output_to/2 (sorry, I am far from being a Prolog expert :) – Sergio Nov 3 at 23:41 Hop that's good enough.

– false Nov 4 at 2:11.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions