Counting number of seen states
As a general rule of thumb, you should avoid using the global&nbsp;database for such purposes.

For example, in your case, after the predicate completes, counted_states/1 is still asserted, and will influence later invocations of the predicate.

This certainly is not intended, no?

There is a straight-forward solution for this:


First, describe what it means that that a state is reachable from a particular state.
Then, use findall/3 to collect all such reachable states into a list of&nbsp;results.
On that list, simply use length/2 to obtain the number of reachable states.


This works without modifying the global database in any way.