pl_search_cpp 1.8
Loading...
Searching...
No Matches
engine.hpp
Go to the documentation of this file.
1/*
2MIT License
3
4Copyright (c) 2025 [Peter Robinson]
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#ifndef PL_SEARCH_ENGINE_HPP_
26#define PL_SEARCH_ENGINE_HPP_
27
28#include "clist.hpp"
29#include "pred.hpp"
30#include "pvar.hpp"
31#include "term.hpp"
32#include "typedefs.hpp"
33
34#include <memory>
35#include <stack>
36
42class EngineTest;
43
44namespace pl_search {
45
53
61
69 class Engine {
70 public:
71 Engine() { push(nullptr); }
78 bool unify(TermPtr t1, TermPtr t2);
79
86 bool execute(PredPtr p, bool unbind);
87
91 friend class Cut;
92 friend class Once;
93 friend class OnceEnd;
94 friend class NotNotEnd;
95 friend class NotNot;
96 friend class DisjPred;
97 friend class IfThenElse;
98
99 // for testing
100 friend class ::EngineTest;
101 void print_env_stack() const;
102 int stack_size() const { return env_stack.size(); }
103 private:
104 std::stack<std::shared_ptr<trail_entry>> trail_stack;
105 std::stack<std::shared_ptr<env_entry>> env_stack;
106
111 void trail(PVarPtr v);
112
116 void backtrack();
117
128 bool call_predicate(PredPtr p);
129
140 bool retry_predicate(PredPtr p);
141
150 return p->apply_choice() && call_predicate(p->get_continuation());
151 }
152
157 void push(PredPtr p);
158
162 void pop_pred_call();
163
168 void cut_to_choice_point(int env_index);
169
174 void pop_to_pred(PredPtr p);
175
179 void clear_stacks();
180
181
182 };
183
184} // namespace pl_search
185
186#endif // PL_SEARCH_ENGINE_HPP_
Represents a disjunction of predicates.
Definition pred.hpp:215
The Engine class manages the execution of predicates and backtracking.
Definition engine.hpp:69
bool call_predicate(PredPtr p)
Call a predicate.
Definition engine.cpp:172
bool unify(TermPtr t1, TermPtr t2)
Unifies two terms.
Definition engine.cpp:76
void clear_stacks()
Clears the environment and trail stacks.
Definition engine.cpp:248
bool execute(PredPtr p, bool unbind)
Executes a predicate.
Definition engine.cpp:268
bool make_choice_and_continue(PredPtr p)
Makes the current choice, checks it, and continues execution (using the predicates continuation) if t...
Definition engine.hpp:149
int stack_size() const
Definition engine.hpp:102
std::stack< std::shared_ptr< env_entry > > env_stack
Stack for environment entries.
Definition engine.hpp:105
void trail(PVarPtr v)
Trails a variable.
Definition engine.cpp:49
std::stack< std::shared_ptr< trail_entry > > trail_stack
Stack for trail entries.
Definition engine.hpp:104
void pop_pred_call()
Pops the top predicate call from the environment stack.
Definition engine.cpp:215
bool retry_predicate(PredPtr p)
Retries the call on a predicate.
Definition engine.cpp:202
void backtrack()
Performs backtracking.
Definition engine.cpp:59
void push(PredPtr p)
Pushes a predicate onto the environment stack.
Definition engine.cpp:155
Engine()
Initialize the environment stack with a dummy entry.
Definition engine.hpp:71
friend class Cut
Definition engine.hpp:91
void print_env_stack() const
Definition engine.cpp:285
void cut_to_choice_point(int env_index)
Cuts the environment stack to the choice point at the given index.
Definition engine.cpp:239
void pop_to_pred(PredPtr p)
Pops predicate calls from the environment stack until the specified predicate is reached.
Definition engine.cpp:226
Represents the equivalent of Prolog if-then-else i.e. (G1 -> G2; G3) - similar to (G1,...
Definition pred.hpp:350
Intended for internal use by NotNot which injects this call directly after the predicate supplied to ...
Definition pred.hpp:302
Represents an equivalent of the Prolog call \+\+Call. The aim is to determine if Call succeeds withou...
Definition pred.hpp:328
Definition pred.hpp:255
Represents the equivalent of Prolog's once(Call). The aim is to remove any choicepoints produced by C...
Definition pred.hpp:280
Definition of the CList class.
Definition choice_iterator.hpp:16
std::shared_ptr< Pred > PredPtr
Typedef for a shared pointer to a Pred object.
Definition typedefs.hpp:34
std::shared_ptr< PVar > PVarPtr
Definition typedefs.hpp:45
std::shared_ptr< Term > TermPtr
Typedefs for shared pointers to Terms and subclasses.
Definition typedefs.hpp:44
Definition of the Pred class and its derived classes.
Definition of the PVar class.
Represents an environment entry for predicates.
Definition engine.hpp:57
int trail_index
The index in the trail stack.
Definition engine.hpp:59
PredPtr pred
The predicate being executed.
Definition engine.hpp:58
Represents a trail entry for backtracking.
Definition engine.hpp:49
PVarPtr var
The variable being trailed.
Definition engine.hpp:50
TermPtr value
The value of the variable.
Definition engine.hpp:51
Definition of the Term class.
Definition of common typedefs used in the pl_search library.