1
votes

I have a predicate like:

solve(parts(X), Places) :-
    "iterate Pl from Places to 0",
    tryPutPart(X, Pl),
    fail.

I want to force the backtracking there, because I want all the possible solutions.(Instead I would have found the place Pl recursively in different predicate).

Is it possible to do it somehow? I got the idea to make a list of length Places and looking like [1, 2, 3.....], and then try to non-deterministically putout some Y from it.

The behavior I would like is if I wrote places(0). places(1). places(2). - ... - and so on into code and then wrote it like

:- places(Y), tryPutPart(X, Y).
2
You question is a bit chaotic. I don't understand what you try to achieve. Please try to reformulate it. - Willem Van Onsem

2 Answers

2
votes

You can use the between/3 predicate to check all integers from a given range. For example:

?- between(1, 10, N), N > 3, write(N), nl, fail.
4
5
6
7
8
9
10
false.

See http://www.swi-prolog.org/pldoc/doc_for?object=between/3 for SWI-Prolog documentation on this predicate.

1
votes

For this problem, I used predicate for

for(M,M,N):- M < N.
for(I,M,N):- M < N, M1 is M + 1, for(I,M1,N).

and then you can force backtracking:

solve(parts(X),Places) :- for(Y, 1, TPlaces), tryPutPart(X, Y), fail.