Re: [eclipse-users] term_string

From: Joachim Schimpf (Independent Contractor) <"Joachim>
Date: Wed, 15 Aug 2007 11:27:49 +0100
Malcolm Ryan wrote:
> The docs for term_string(Term, String) say:
> "If String is instantiated, its contents are parsed, and if the whole  
> string can be parsed as one Prolog term it is unified with Term."
> But this doesn't seem to work if String represents a variable.
> Eg:
> [eclipse 19]: term_string(X, String), term_string(Y, String), Y = a.
> X = X
> String = "_65"
> Y = a
> Yes (0.00s cpu)
> I would have expected X and Y to have been unified.

The point to keep in mind is that variables do not have "global names".

Variable names are only used locally in the _textual_representation_ of
a single term to indicate identity or difference.  E.g. "f(X,X)"
is a textual representation for a 2-argument structure where both arguments
are the same variable, while in "f(X,Y)" they are two different variables.
If you parse two terms separately (e.g. via two invocations of term_string/2,
or in the case of two separate clauses in a source file), then the two
textual representations both have their own variable-name-space, so you
get the _same_ result for

?- term_string(T1, "f(X)"), term_string(T2, "f(Y)"), T1 \== T2.


?- term_string(T1, "f(X)"), term_string(T2, "f(X)"), T1 \== T2.

I am aware that some confusion is caused by an ECLiPSe feature that is
meant to help debugging, but sometimes achieves the opposite: the
retention and printing of these (non-unique) source names.  That's why
in the second example, the toplevel by default prints f(X) for both
T1 and T2, even though these X's are different...
You can change these print-settings in tkeclipse via
Tools->GlobalSettings->OutputMode->variables by selecting the
"_123" or "X_123" mode, or by calling one of
?- get_flag(output_options,Old), set_flag(output_options,[variables(raw)|Old]).
?- get_flag(output_options,Old), set_flag(output_options,[variables(full)|Old]).
from the toplevel or your program.  This will then guarantee to print
different names for different variables.

Note that this still doesn't give you a means to _access_ arbitary
variables within your runtime data structures "by name".  As in any
other programming language, if you want to get at your data, you have
to organise it in an appropriate data structure, e.g. by putting
your variables into an array, list or hash table.

-- Joachim
Received on Wed Aug 15 2007 - 11:28:02 CEST

This archive was generated by hypermail 2.3.0 : Sat Aug 24 2019 - 06:15:04 CEST