Prolog 'Out of Local Stack'

165 Views Asked by At

I am developing a program that solves a more complicated version of the infamous puzzle 'The farmer, the fox, the goose, and the grain', which has eight components instead of four. I've already determined the solution; additionally, I've written out just the necessary states to complete the problem, like this:

move([w,w,w,w,w,w,w,w],[e,w,w,e,w,w,w,w]).
move([e,w,w,e,w,w,w,w],[w,w,w,e,w,w,w,w]).

etc.

My goal now is to have this program follow those states, chaining from one to the next, until it reaches the ultimate goal of [e,e,e,e,e,e,e,e]. To accomplish this, I have defined predicates as such:

solution([e,e,e,e,e,e,e,e],[]).
solution(Start,End) :-
    move(Start,NextConfig),
    solution(NextConfig,End).

My query is solution([w,w,w,w,w,w,w,w],[e,e,e,e,e,e,e,e]). However, this results in apparently infinite recursion. What am I missing?

1

There are 1 best solutions below

1
false On

To avoid cycles, try closure0/3

solution(S) :-
   closure0(move, S,[e,e,e,e,e,e,e,e]).