Hide variables in a Prolog request

I am working on a task with a prologue that includes an airport database (these are the cities of airports and flight links, which include airport tax and length of time), and the first question includes queries. We have to write a Prolog request to answer the question, I already know how to answer the question, the problem is that Prolog displays more information than I want. I wrote:

flight(X,_,Y,_,N), N > 180. 

Basically, everything he does is a list of all cities (X is the source flight, Y is the destination field, and N is the duration). I want X and Y, but I do not want N. The only way I can get this to work is to wrap this request in a rule and just show it X and Y, but since we do not have to write the rules, I'm not sure how to get around this. I do not want the explicit answer to be just a hint or something.

+7
source share
5 answers

Whenever possible, I prefer to use expressions to compress IO

 ?- forall((flight(X,_,Y,_,N), N > 180), writeln((x=X,y=Y))). 

for example with another generator

 ?- forall((member(X,"12"),member(Y,"ab")),writeln((x=X,y=Y))). x=49,y=97 x=49,y=98 x=50,y=97 x=50,y=98 true. 

Of course, for the right indentation tables, format / 2 would be much better ...

change is possible, I misunderstood the question, assuming that you have already considered

 query(X,Y) :- flight(X,_,Y,_,N), N > 180. 
+2
source

Jekejeke Prolog offers a unique solution to this problem. This allows the operator (^) / 2 to appear in top-level queries. Therefore, if you have:

 ?- [user]. flight(a,1,b,2,100). flight(c,3,d,4,200). ^D 

You will usually get the following answer, and N can annoy you:

 ?- flight(X,_,Y,_,N), N > 180. X = c, Y = d, N = 200 

Now you can hide N as follows:

 ?- N^(flight(X,_,Y,_,N), N>180). X = c, Y = d 

The use of (^) / 2 for this purpose is connected with the role (^) / 2 already found in the sum / 3 and the set of predicates / 3.

Bye

+2
source

For a one-time request like this, I often use a simple print statement, for example:

 flight(X,_,Y,_,N), N > 180, print(('X' = X, 'Y' = Y)), nl, fail. 

The disadvantages of this are:

  • The result can be ugly if you did not put it in the formatting. Here I am building the term ,/2 (with =/2 subterms), which should appear similarly to the Prolog release for regular target solutions.

  • Worse, the target actually fails (to prevent it from subsequently printing all of the many false bindings, such as those for N ), so it can be difficult to use for something more.

But this is the fastest way that I know to print a set of solutions for a goal entered on the command line and hide all working variables.

+1
source

Using library(lambda) , you can declare those variables that should remain visible.

 ?- {X,Y}+\ ( flight(X,_,Y,_,N), N > 180 ). X = c, Y = d. 
+1
source

Does the following provide an acceptable solution? findall finds all unifications of a given term that satisfy a given purpose. Therefore, you can format your answers as you wish.

 findall([X, Y], (flight(X, _, Y, _, N), N > 180), Solutions). 
-one
source

All Articles