7

I have been using the chr library along with the jpl interface. I have a general inquiry though. I send the constraints from SWI Prolog to an instance of a java class from within my CHR program. The thing is if the input constraint is leq(A,B) for example, the names of the variables are gone, and the variable names that appear start with _G. This happens even if I try to print leq(A,B) without using the interface at all. It appears that whenever the variable is processed the name is replaced with a fresh one. My question is whether there is a way to do the mapping back. For example whether there is a way to know that _G123 corresponds to A and so on. Thank you very much.

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
user1220625
  • 75
  • 1
  • 4

1 Answers1

9

(This question has nothing to do with CHR nor is it specific to SWI).

The variable names you use when writing a Prolog program are discarded completely by the Prolog system. The reason is that this information cannot be used to print variables accurately. There might be several independent instances of that variable. So one would need to add some unique identifier to the variable name. Also, maintaining that information at runtime would incur significant overheads.

To see this, consider a predicate mylist/1.

?- [user].
|: mylist([]).
|: mylist([_E|Es]) :- mylist(Es).
|: % user://2 compiled 0.00 sec, 4 clauses
true.

Here, we have used the variable _E for each element of the list. The toplevel now prints all those elements with a unique identifier:

?- mylist(Fs).
Fs = [] ;
Fs = [_G295] ;
Fs = [_G295, _G298] .
Fs = [_G295, _G298, _G301] .

The second answer might be printed as Fs = [_E] instead. But what about the third? It cannot be printed as Fs = [_E,_E] since the elements are different variables. So something like Fs = [_E_295,_E_298] is the best we could get. However, this would imply a lot of extra book keeping.

But there is also another reason, why associating source code variable names with runtime variables would lead to extreme complexities: In different places, that variable might have a different name. Here is an artificial example to illustrate this:

p1([_A,_B]).

p2([_B,_A]).

And the query:

?- p1(L), p2(L).
L = [_G337, _G340].

What names, would you like, these two elements should have? The first element might have the name _A or _B or maybe even better: _A_or_B. Or, even _Ap1_and_Bp2. For whom will this be a benefit?

Note that the variable names mentioned in the query at the toplevel are retained:

?- Fs = [_,F|_], mylist(Fs).
Fs = [_G231, F] ;
Fs = [_G231, F, _G375] ;
Fs = [_G231, F, _G375, _G378] 

So there is a way to get that information. On how to obtain the names of variables in SWI and YAP while reading a term, please refer to this question.

Community
  • 1
  • 1
false
  • 10,264
  • 13
  • 101
  • 209
  • Thank you very much for the reply. I understand that variable names are replaced. The thing is if there is a way to know that variable _G123 maps to A for example. The same way it is done is the question you pointed to but without having to use read_term, because I can not do this as I am using CHR so the input are constraints. Thank you – user1220625 Feb 20 '12 at 17:33
  • Unfortunately the F is not shown during trace. Neither for SWI Prolog nor for GNU Prolog. And GNU Prolog uses _ in the answer and A, B, C, ... so that correspondence with trace is lost in answer. Jekejeke Prolog does it fine during trace. –  Feb 20 '12 at 21:11
  • @user1220625: Short answer: There is no easy way to tell this. Please keep in mind that what you want sounds very easy, but it might be extremely complex both to implement and to use. I will add a more convincing example to my answer. – false Feb 20 '12 at 22:25