Reputation: 43
Basically I've defined a predicate to find all 'next possible states' from a current state, however I have to use the built-in bagof predicate to unify the list of all these states, however when I call it in the command window I get the error 'out of global stack' so I've tried tracing it and it just calls the next states recursively non-stop, so it can't possible get a 'bag' of all these solutions if there is an infinite number of solutions!
I don't think 'limiting' the list of possible current states to a certain number of elements is an option, so what other way is there of calling bagof without the out of global stack error?
Sorry if this doesn't make much sense!
Upvotes: 0
Views: 708
Reputation: 60034
I think that you should use call_with_depth_limit/3, but it's difficult to detail any hint without knowing how your state machine is structured. Say you have a state(Curr, Next)
with recursion, that should be the culprit for the loop, then you could get the list of 'Next' with something like
'next possible states'(Curr, ListNext) :- findall(Next, call_with_depth_limit(state(Curr, Next),2,_), ListNext).
Note that findall is simpler than bagof to use when you are not really interested in quantifying variables usage...
Upvotes: 1
Reputation: 2162
I bet you've got cycles in the graph. So you're going back and forth an arbitrary number of times.
It's like asking for all the routes from my house to the local store. I can walk around my block an arbitrary number of times before I go the last half block. So ther's an infinite number.
The solution to this is to forget bagof. Use recursion, but as you descend, keep a list of places you've been with you, and don't go back to them.
Upvotes: 1
Reputation: 1514
Each branch of the search tree needs to terminate at some point or else you will indeed get this error. It would be useful to see the code to see where the infinite loop is located.
Probably you created a recursive structure somewhere. For instance bagof would get your same result here:
nextState(State1,State2):-
If you would start with 1 it would find 2, 3, 4, 5, 6... until inifinite but then your Stack will Overflow!
State2 is State1+1.
nextState(State1,State3):-
nextState(State1,State2),
nextState(State2,State3).
Upvotes: 0