pl_search_cpp 1.4
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
69class Engine {
70public:
71 Engine(){};
72
79 bool unify(TermPtr t1, TermPtr t2);
80
87 bool execute(PredPtr p, bool unbind);
88
92 friend class Cut;
93 friend void Pred::wrap_with_once();
94 friend class DisjPred;
95 friend class IfThenElse;
96
97 // for testing
98 friend class ::EngineTest;
99
100private:
101 std::stack<trail_entry *> trail_stack;
102 std::stack<env_entry *> env_stack;
103
108 void trail(PVarPtr v);
109
113 void backtrack();
114
125 bool call_predicate(PredPtr p);
126
137 bool retry_predicate(PredPtr p);
138
147 return p->apply_choice() && call_predicate(p->get_continuation());
148 }
149
154 void push(PredPtr p);
155
159 void pop_pred_call();
160
165 void cut_to_choice_point(int env_index);
166
170 void clear_stacks();
171};
172
173} // namespace pl_search
174
175#endif // PL_SEARCH_ENGINE_HPP_
Represents a Prolog like cut. When called it pops env_stack thus removing choicepoints.
Definition pred.hpp:235
Represents a disjunction of predicates.
Definition pred.hpp:193
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:170
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:226
bool execute(PredPtr p, bool unbind)
Executes a predicate.
Definition engine.cpp:241
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:146
void trail(PVarPtr v)
Trails a variable.
Definition engine.cpp:49
void pop_pred_call()
Pops the top predicate call from the environment stack.
Definition engine.cpp:204
bool retry_predicate(PredPtr p)
Retries the call on a predicate.
Definition engine.cpp:191
void backtrack()
Performs backtracking.
Definition engine.cpp:59
void push(PredPtr p)
Pushes a predicate onto the environment stack.
Definition engine.cpp:153
Engine()
Definition engine.hpp:71
std::stack< trail_entry * > trail_stack
Stack for trail entries.
Definition engine.hpp:101
std::stack< env_entry * > env_stack
Stack for environment entries.
Definition engine.hpp:102
void cut_to_choice_point(int env_index)
Cuts the environment stack to the choice point at the given index.
Definition engine.cpp:215
Represents the equivalent of Prolog if-then-else i.e. (G1 -> G2; G3) - similar to (G1,...
Definition pred.hpp:305
void wrap_with_once()
Wraps the predicate with a once.
Definition pred.cpp:55
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:33
std::shared_ptr< PVar > PVarPtr
Definition typedefs.hpp:44
std::shared_ptr< Term > TermPtr
Typedefs for shared pointers to Terms and subclasses.
Definition typedefs.hpp:43
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.