Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable Names in SWI Prolog

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.

like image 846
user1220625 Avatar asked Feb 20 '12 09:02

user1220625


People also ask

What are variables in Prolog?

A variable in Prolog is a string of letters, digits, and underscores ( _ ) beginning either with a capital letter or with an underscore. Examples: X , Sister , _ , _thing , _y47 , First_name , Z2 The variable _ is used as a "don't-care" variable, when we don't mind what value the variable has.

How is a variable represented in Prolog?

In Prolog, predicate names and bound variables are expressed as a sequence of alphanumeric characters beginning with an alphabetic. Variables are represented as a string of alphanumeric characters beginning (the first character, at least) with an uppercase alphabetic. Thus: likes(X, susie).

What are variables atoms and terms in Prolog?

Prolog is dynamically typed. It has a single data type, the term, which has several subtypes: atoms, numbers, variables and compound terms. An atom is a general-purpose name with no inherent meaning. It is composed of a sequence of characters that is parsed by the Prolog reader as a single unit.

How are strings defined in SWI-Prolog?

As of SWI-Prolog version 7, text enclosed in double quotes (e.g., "Hello world" ) is read as objects of the type string. A string is a compact representation of a character sequence that lives on the global (term) stack. Strings represent sequences of Unicode characters including the character code 0 (zero).


1 Answers

(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.

like image 132
false Avatar answered Nov 15 '22 15:11

false