I am studying Prolog using Ivan Bratko book: Programming for Artificial Intelligence and I am finding some difficulties to implement the final part of an exercise proposed
There will be loops in space search graph, then you can switch to some form of bound search. The easier I know it's bounded depth search:
?- length(Situation,_), solve([[c,a,b],[],[]], Situation).
Situation = [[[c, a, b], [], []], [[a, b], [c], []], [[b], [a], [c]], [[], [b, c], [a]], [[], [a, b|...], []]] .
length/2 enumerates unbound lists of growing length. So we get a result.
Note that this will still loop if there are no solutions from initial state to goal/1.
If this is bad, I think solve/2 will need a service solve2/2 predicate, that will get the path, to enable the usual \+ memberchk(NewState, Visited)
after the nondeterministic s/2 call. Then it will be (untested)
solve(N, SearchPath) :- solve2([N], SearchPath).
solve2([N|Visited], [N|Visited]) :- goal(N).
solve2([N|Visited], Path) :-
s(N,N1),
\+ memberchk(N1, Visited),
solve2([N1,N|Visited], Path).