Represents a loop predicate that iterates over instances of a predicate.
Loop is a predicate that loops over (instances of) a predicate. It is similar the following recursive Prolog code template:
loop(State1, Depth) :- loop_continues(State1, Depth), !, body_call(State1, State2, Depth), NextDepth is Depth + 1, loop(State2, NextDepth).
loop(_, _).
The constructor takes a pointer to a subclass of LoopBodyFactory that is used to generate body predicates to call and to test if the loop should continue. It is essentially the body_call(State1, State2, Depth), NextDepth is Depth + 1, loop(State2, NextDepth) part of the Prolog code template. The continuation of the loop predicate is initially the predicate to call after the loop body. When the loop body predicate is called a new body predicate is generated as well as a new loop predicate. The continuation for the body predicate is set to the new loop predicate and the continuation for the new loop predicate is set to the original continuation.
| bool pl_search::Loop::call |
( |
| ) |
|
|
overridevirtual |
Calls the Loop predicate.
- Returns
- True if the loop terminates and the continuation succeeds or if the "recursive" call to the loop predicate succeeds, false otherwise.
Loop is similar to the following recursive Prolog code template:
loop(State1, Depth) :- % recursive case - loop continues loop_continues(State1, Depth), !, body_call(State1, State2, Depth), NextDepth is Depth + 1, loop(State2, NextDepth). loop(_, _). % base case - loop terminates
Note: loop_continues() must not bind any variables - it should be a pure test of the current state. In the Prolog code above loop_continues is allowed to bind variables that can be used in body_call but this is not allowed in the C++ implementation of Loop. ANy variable bindings needed by the body predicate should be made in the body predicate itself.
Reimplemented from pl_search::Pred.