Home > Archive > Prolog > September 2007 > copy_term/2 in Eclipse
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
copy_term/2 in Eclipse
|
|
| Jens Claßen 2007-08-30, 8:09 pm |
| Hi,
I want to use the copy_term/2 predicate in order to generate the copy of
a term where its variables are replaced by new ones. I am using the
latest version (5.10) of Eclipse.
However when I for instance try the second example from here:
http://eclipse.crosscoreop.com/ecli...opy_term-2.html
the result is the following:
> [eclipse 1]: copy_term(s(X,a,Y,X), C).
>
> X = X
> Y = Y
> C = s(X, a, Y, X)
> Yes (0.00s cpu)
That is, C is not a copy with new variables, but it is just unified with
the orginial term. Does anyone have an idea of how to fix this (or what
may be the problem)? Any hint would be welcome.
Regard
Jens
| |
| bart demoen 2007-08-30, 8:09 pm |
| On Thu, 30 Aug 2007 19:25:51 +0200, Jens Claßen wrote:
[color=darkred]
It is weird. I also get:
[eclipse 3]: A = f(X), copy_term(A,B), X = 1.
A = f(1)
B = f(X)
X = 1
Yes (0.00s cpu)
and
[eclipse 4]: copy_term(f(X),Z), Z = f(A), A == X.
No (0.00s cpu)
so it might be something (wrong) with the toplevel printing routines
and not necessarily with copy_term itself. Joachim will enligthen us, I'm
sure :-)
Cheers
Bart Demoen
| |
| Jens Claßen 2007-08-31, 8:56 am |
| > so it might be something (wrong) with the toplevel printing routines
> and not necessarily with copy_term itself. Joachim will enligthen us, I'm
> sure :-)
Thank you very much, that was the right hint! The predicate and thus my
program works fine; the problem was only in the way the toplevel and
debugger printing routines displayed the variables. To obtain the
"right" output, one has to do the following:
[eclipse 1]: set_flag(variable_names,off).
Yes (0.00s cpu)
Then the output is the following:
[eclipse 2]: copy_term(s(X,a,Y,X), C).
X = _67
Y = _76
C = s(_183, a, _185, _183)
Yes (0.00s cpu)
and
[eclipse 3]: A = f(X), copy_term(A,B), X = 1.
A = f(1)
X = 1
B = f(_249)
Yes (0.00s cpu)
and
[eclipse 4]: copy_term(f(X),Z), Z = f(A), A == X.
No (0.00s cpu)
[There was no change in the last example, but I think there actually
should not be any. The query fails because of "A==X"; A and X are
distinct variables.]
But again, turning variable names off is *not* necessary for the
predicate to work properly; it is just needed in order for the toplevel
output to not replace the newly generated variables (like _183) by their
source's name (like X).
Regards
Jens
| |
| Kish Shen 2007-08-31, 8:10 pm |
| Hi,
Jens Claßen wrote:
> Hi,
>
> I want to use the copy_term/2 predicate in order to generate the copy of
> a term where its variables are replaced by new ones. I am using the
> latest version (5.10) of Eclipse.
>
> However when I for instance try the second example from here:
>
> http://eclipse.crosscoreop.com/ecli...opy_term-2.html
>
> the result is the following:
>
>
> That is, C is not a copy with new variables, but it is just unified with
> the orginial term. Does anyone have an idea of how to fix this (or what
> may be the problem)? Any hint would be welcome.
>
> Regard
> Jens
This is quite a common question we get about ECLiPSe...
No, there is no bug -- you are by a feature of ECLiPSe, which
tries to use source variable names whenever possible, even for different
instances of the source variables. This is meant to help debugging, but
can be confusing at times. It looks like copy_term copies the source
variable names as well, but the actual variables with the same names are
different variables:
[eclipse 8]: copy_term(s(X,a,Y,X),C), X = 3.
X = 3
Y = Y
C = s(X, a, Y, X)
Yes (0.00s cpu)
You can reduce this duplication of variable names by changing the print
mode for ECLiPSe, e.g.
[eclipse 1]: get_flag(output_options,Old), set_flag(output_options,
[variables(full)|Old]).
Old = [depth(20), attributes(pretty), quoted(true), portrayed(true)]
Yes (0.00s cpu)
[eclipse 2]: copy_term(s(X,a,Y,X), C).
X = X_67
Y = Y_76
C = s(X_187, a, Y_188, X_187)
Yes (0.00s cpu)
This appends a number to the variable name that is different for
different instances.
In general, if a Prolog system uses source name in the answer, it is
possible for the same variable name to refer to different variables,
e.g. in SWI-Prolog, I can do the following:
?- copy_term(a(X,Y,X), C), _G317 = 1.
C = a(_G316, _G317, _G316 ),
_G317 = 1
The two _G317 are obviously different variables. Admittedly you have to
try harder in other Prologs...
Cheers,
Kish
| |
| Jens Claßen 2007-09-03, 4:19 am |
| Thanks a lot for the explanation!
Regards
Jens
|
|
|
|
|