Automatic results showing?
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.
@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..
This is another way which I think is quite nice:
:- op(1200, xfx, all).
:- op(1200, xfx, s5).
:- op(1200, xfx, s10).
all(F,X):-
F =.. [_|T],
findall(T, F,X).
s5(F,X):-
F =.. [_|T],
findnsols(5,T,F,X).
s10(F,X):-
F =.. [_|T],
findnsols(10,T,F,X).
p(1).
p(2).
p(3).
p(4).
p(5).
p(6).
p(7).
nat(0).
nat(s(X)) :- nat(X).
nat_nat_sum(0,X,X).
nat_nat_sum(s(X),Y,s(Z)) :- nat_nat_sum(X,Y,Z).
Q:
?- nat(X),nat(Y),nat_nat_sum(X,Y,Z) s5 Sols.
Sols = [[nat(0), (nat(0), nat_nat_sum(0, 0, 0))], [nat(0), (nat(s(0)), nat_nat_sum(0, s(0), s(0)))], [nat(0), (nat(s(s(0))), nat_nat_sum(0, s(s(0)), s(s(0))))], [nat(0), (nat(s(s(...))), nat_nat_sum(0, s(s(...)), s(s(...))))], [nat(0), (nat(s(...)), nat_nat_sum(0, s(...), s(...)))]] ;
Sols = [[nat(0), (nat(s(s(s(s(s(...)))))), nat_nat_sum(0, s(s(s(s(s(...))))), s(s(s(s(s(...)))))))], [nat(0), (nat(s(s(s(s(...))))), nat_nat_sum(0, s(s(s(s(...)))), s(s(s(s(...))))))], [nat(0), (nat(s(s(s(...)))), nat_nat_sum(0, s(s(s(...))), s(s(s(...)))))], [nat(0), (nat(s(s(...))), nat_nat_sum(0, s(s(...)), s(s(...))))], [nat(0), (nat(s(...)), nat_nat_sum(0, s(...), s(...)))]] ;
Sols = [[nat(0), (nat(s(s(s(s(s(...)))))), nat_nat_sum(0, s(s(s(s(s(...))))), s(s(s(s(s(...)))))))], [nat(0), (nat(s(s(s(s(...))))), nat_nat_sum(0, s(s(s(s(...)))), s(s(s(s(...))))))], [nat(0), (nat(s(s(s(...)))), nat_nat_sum(0, s(s(s(...))), s(s(s(...)))))], [nat(0), (nat(s(s(...))), nat_nat_sum(0, s(s(...)), s(s(...))))], [nat(0), (nat(s(...)), nat_nat_sum(0, s(...), s(...)))]] .
?- p(X) s5 Sols.
Sols = [[1], [2], [3], [4], [5]] ;
Sols = [[6], [7]].
The advantage is that you can just add how many answers you want at the end of a query. You then gets lists of that length as answers, so they are not just written to console but can be used for further queries.