2
votes

I have the next code in a file called "testing.pl":

fact(1).
fact(2).
fact(3).

funcA(X) :- funcB(X).

funcB(X) :- fact(X).

testing :- funcA(_X).

Then, in SWI-Prolog interpreter I query funcA(X). and the output is:

X = 1 ;
X = 2 ;
X = 3.

But, whenever I query testing. the output is:

true ;
true ;
true.

So, my questions is:

How can I use the conclusion of a rule as a premise of another rule (funcA(X) at the right of testing), but having the same effect as if I query that premise (funcA(X))?

In the example above, I would like to write testing. at some point of my "testing.pl" file and to get the funcA(X) to do the same like when I query with the interpreter, so funcB(X) will check for all values that X can take from fact(N) and return it.

My desire result would be to write testing. and to get on screen:

X = 1 ;
X = 2 ;
X = 3.

Thanks.

1
If you want testing. to result in X = 1, ... then you need to have X be an argument to testing. So, testing(X) :- funcA(X). (which doesn't seem to add any purpose). Alternatively, and less preferably, you could write the results: testing :- funcA(X), write('X = '), write(X), nl. - lurker

1 Answers

3
votes

You can manually print anything on the terminal, using for example predicates like portray_clause/1, format/2 etc.

The only additional piece you need is a way to force backtracking over all answers. One way to do it is using false/0.

So, in your case, you can write for example:

testing :-
        funcA(X),
        format("X = ~q ;~n", [X]),
        false.

And now invoking testing/0 yields, without any further interaction:

?- testing.
X = 1 ;
X = 2 ;
X = 3 ;

In addition, the predicates now fails, so you also get false/0 if you use it interactively (from the toplevel), but not if you invoke testing from the shell via swipl -g testing ....

Also take a look at the important variable_names/1 option that is available to use the intended variable names.

I lave tailoring this output to your exact use case as an exercise. Ideally, the output should be a proper Prolog term, so that you can actually test it easily by reading it with read/1 and comparing it to a reference result.