Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic results showing?

How can I make SWI-Prolog interpreter to automatically do a semicolon? I have a lot of results (about 300) due to backtracking and I don't want to push semicolon for all of them.

I do NOT want a list of all solutions, I only want not to push semicolon or space so I can leave the program printing backtracked solutions on background.

like image 547
Javier Avatar asked Jan 23 '14 16:01

Javier


2 Answers

You can manually emit solutions (using for example write/1 or format/2), and force backtracking with false to see all solutions. For example:

?- solution(S), writeln(S), false.

In addition, for example in SWI-Prolog, you can simply press SPACE instead of ; for further solutions.

like image 75
mat Avatar answered Oct 13 '22 01:10

mat


@repeat Not sure if this would fit your criteria but could you not develop a meta interpreter ? I am not sure if there is a way to load a mi so that all top level queries go through it?

So something like:

mi1(true).
mi1((A,B)) :-
 mi1(A),
 mi1(B).
mi1(Goal) :-
 Goal \= true,
 Goal \= (_,_),
 Goal =..List,
 maplist(newvar,List,NewList),
 Goal2 =..NewList,
 clause(Goal2, Body),
 List=[_,T],!,
 findnsols(5,I,(Goal =..[_,I],Goal),T),
 mi1(Body).

newvar(V,V2):-
 var(V).
newvar(V,V):-
 nonvar(V).

%test predicates.
natnum1(0).
natnum1(s(X)) :-
 natnum1(X).

 w(w1).
 w(w2).
 w(w3).
 w(w4).
 w(w5).
 w(w6).
 w(w7).
 w(w8).

Queries:

?- mi1(w(X)).
X = [w1, w2, w3, w4, w5] ;
X = [w6, w7, w8] ;
false.

?- mi1(natnum1(X)).
X = [0, s(0), s(s(0)), s(s(s(0))), s(s(s(s(0))))] ;
X = [s(s(s(s(s(0))))), s(s(s(s(s(s(0)))))), s(s(s(s(s(s(s(...))))))), s(s(s(s(s(s(...)))))), s(s(s(s(s(...)))))] ;
X = [s(s(s(s(s(s(s(s(s(...))))))))), s(s(s(s(s(s(s(s(...)))))))), s(s(s(s(s(s(s(...))))))), s(s(s(s(s(s(...)))))), s(s(s(s(s(...)))))] ;
X = [s(s(s(s(s(s(s(s(s(...))))))))), s(s(s(s(s(s(s(s(...)))))))), s(s(s(s(s(s(s(...))))))), s(s(s(s(s(s(...)))))), s(s(s(s(s(...)))))] 
...

Just showing the basic idea.. this only works for 1 arg predicates..

like image 26
user27815 Avatar answered Oct 12 '22 23:10

user27815