pl_search_cpp 2.2
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Protected Attributes | List of all members
pl_search::Loop Class Reference

Represents a loop predicate that iterates over instances of a predicate. More...

#include <pred.hpp>

Inheritance diagram for pl_search::Loop:
Inheritance graph
[legend]
Collaboration diagram for pl_search::Loop:
Collaboration graph
[legend]

Public Member Functions

 Loop (Trail *trail, LoopBodyFactoryPtr bf)
 Constructs a predicate that loops over (instances of) a predicate while loop_continues() is true.
 
 Loop (Trail *trail, LoopBodyFactoryPtr bf, int depth)
 
bool call () override
 Calls the Loop predicate.
 
- Public Member Functions inherited from pl_search::Pred
 Pred (Trail *trail)
 Default constructor.
 
PredPtr get_continuation ()
 Gets the continuation of the predicate.
 
virtual void set_continuation (PredPtr cont)
 Sets the continuation of the predicate.
 
PredPtr last_pred ()
 Follows the continuation chain to the last predicate.
 
virtual void call_init ()
 Initialize call.
 
bool call_continuation ()
 Calls the continuation of the predicate - first call_init and then call.
 
virtual ~Pred ()=default
 Virtual destructor for proper cleanup.
 
virtual std::string get_name ()
 For debugging.
 

Public Attributes

LoopBodyFactoryPtr body_factory
 

Protected Attributes

int depth
 
- Protected Attributes inherited from pl_search::Pred
PredPtr continuation
 The continuation of the predicate.
 
Trailtrail
 Pointer to the trail for managing variable bindings.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ Loop() [1/2]

pl_search::Loop::Loop ( Trail trail,
LoopBodyFactoryPtr  bf 
)
inline

Constructs a predicate that loops over (instances of) a predicate while loop_continues() is true.

Parameters
trailPointer to the trail.
bfa pointer to a subclass of LoopBodyFactory that is used to generate body predicates to call and to test if the loop should continue.

◆ Loop() [2/2]

pl_search::Loop::Loop ( Trail trail,
LoopBodyFactoryPtr  bf,
int  depth 
)
inline

Member Function Documentation

◆ call()

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.

Member Data Documentation

◆ body_factory

LoopBodyFactoryPtr pl_search::Loop::body_factory

◆ depth

int pl_search::Loop::depth
protected

The documentation for this class was generated from the following files: