pl_search_cpp 1.4
Loading...
Searching...
No Matches
pred.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// Aproximating Prolog predicates in C++ using a continuation-passing style
26
27#ifndef PL_SEARCH_PRED_HPP_
28#define PL_SEARCH_PRED_HPP_
29
30#include "term.hpp"
31#include "typedefs.hpp"
32
33#include <memory>
34#include <stack>
35#include <vector>
36
42namespace pl_search {
43
44class ChoiceIterator;
45
54class Pred : public std::enable_shared_from_this<Pred> {
55public:
59 Pred(Engine *eng) : engine(eng), continuation(nullptr) {}
60
64 virtual void initialize_call() {};
65
70 virtual bool apply_choice() { return false; }
71
76 virtual bool more_choices() { return false; }
77
83
88 virtual void set_continuation(PredPtr cont) { continuation = cont; }
89
95
100 bool is_non_det() { return true; }
101
105 void wrap_with_once();
106
110 virtual ~Pred() = default;
111
115 std::string get_name() { return typeid(this).name(); }
116
117protected:
120};
121
122typedef std::shared_ptr<ChoiceIterator> ChoiceIteratorPtr;
123
127class ChoicePred : public Pred {
128public:
130
138
139 void initialize_call() override {}
140 bool apply_choice() override;
141 bool more_choices() override;
142};
143
147class SemiDetPred : public Pred {
148public:
153 SemiDetPred(Engine *eng) : Pred(eng) {}
154
159 bool more_choices() override { return false; }
160
161 bool is_non_det() { return false; }
162};
163
167class DetPred : public SemiDetPred {
168public:
173 DetPred(Engine *eng) : SemiDetPred(eng) {}
174
179 bool apply_choice() override { return true; }
180};
181
188PredPtr conjunction(std::vector<PredPtr> preds);
189
193class DisjPred : public Pred {
194public:
203 DisjPred(Engine *eng, std::vector<PredPtr> preds)
204 : Pred(eng), preds(preds), index(0) {}
205
206 void initialize_call() override {
208 for (auto it = preds.begin(); it != preds.end(); it++) {
209 (*it)->set_continuation(continuation);
210 }
211 };
212
213 bool apply_choice() override {
215 PredPtr choice = preds[index++];
220 continuation = choice;
221 return true;
222 }
223
224 bool more_choices() override { return index < preds.size(); }
225
226protected:
227 std::vector<PredPtr> preds;
228 int index;
229};
230
235class Cut : public DetPred {
236public:
243 Cut(Engine *eng, int index) : DetPred(eng), env_index(index) {}
244
245 void initialize_call() override {}
246
247 bool apply_choice() override;
248
249 void set_cut_point(int cut_point) { env_index = cut_point; }
250
251protected:
253};
254
259class NotNotEnd : public SemiDetPred {
260public:
268 NotNotEnd(Engine *eng, bool *succ) : SemiDetPred(eng), succeeded(succ) {}
269
270 void initialize_call() override {};
271 bool apply_choice() override;
272
273protected:
275};
276
281class NotNot : public Pred {
282public:
288 NotNot(Engine *eng, PredPtr p) : Pred(eng), pred(p) {}
289
290 void initialize_call() override;
291 bool apply_choice() override;
292 bool more_choices() override;
293
294protected:
299};
300
305class IfThenElse : public Pred {
306public:
316
317 void initialize_call() override;
318 bool apply_choice() override;
319 bool more_choices() override;
320 void set_continuation(PredPtr cont);
321
322protected:
327 std::shared_ptr<Cut> cut_pred;
329};
335public:
337
339
340 virtual ~LoopBodyFactory(){};
341
342 virtual bool loop_continues() = 0;
343
344 virtual PredPtr make_body_pred() = 0;
345};
346
351class Loop : public DetPred {
352public:
354
363
364 void initialize_call() override;
365 bool apply_choice() override;
366 void set_continuation(PredPtr cont);
367
368protected:
370};
371
372// for testing
373std::string repr(PredPtr pred);
374
375} // namespace pl_search
376
377#endif // PL_SEARCH_PRED_HPP_
Represents a choice predicate.
Definition pred.hpp:127
ChoicePred(Engine *eng, ChoiceIteratorPtr ch)
Constructs a ChoicePred with the given choice iterator.
Definition pred.hpp:136
ChoiceIteratorPtr choice_iterator
Pointer to the choice iterator.
Definition pred.hpp:129
bool apply_choice() override
Applies a choice for the choice predicate.
Definition pred.cpp:65
bool more_choices() override
Checks if there are more choices for the choice predicate.
Definition pred.cpp:71
void initialize_call() override
Initializes the predicate call.
Definition pred.hpp:139
Represents a Prolog like cut. When called it pops env_stack thus removing choicepoints.
Definition pred.hpp:235
Cut(Engine *eng, int index)
Constructs a Cut predicate.
Definition pred.hpp:243
void set_cut_point(int cut_point)
Definition pred.hpp:249
void initialize_call() override
Initializes the predicate call.
Definition pred.hpp:245
int env_index
Definition pred.hpp:252
bool apply_choice() override
Applies a choice for the cut predicate.
Definition pred.cpp:94
Represents a deterministic predicate.
Definition pred.hpp:167
DetPred(Engine *eng)
Represents a deterministic predicate.
Definition pred.hpp:173
bool apply_choice() override
Noop - all the work is done in initialize_call.
Definition pred.hpp:179
Represents a disjunction of predicates.
Definition pred.hpp:193
bool apply_choice() override
Applies a choice.
Definition pred.hpp:213
void initialize_call() override
Initializes the predicate call.
Definition pred.hpp:206
bool more_choices() override
Checks if there are more choices.
Definition pred.hpp:224
int index
index of the current predicate choice
Definition pred.hpp:228
DisjPred(Engine *eng, std::vector< PredPtr > preds)
Constructs a DisjPred with the given predicates.
Definition pred.hpp:203
std::vector< PredPtr > preds
The predicates in the disjunction.
Definition pred.hpp:227
The Engine class manages the execution of predicates and backtracking.
Definition engine.hpp:69
Represents the equivalent of Prolog if-then-else i.e. (G1 -> G2; G3) - similar to (G1,...
Definition pred.hpp:305
void set_continuation(PredPtr cont)
Sets the continuation of the predicate.
Definition pred.cpp:192
PredPtr then_pred
Definition pred.hpp:324
PredPtr if_then_pred
Definition pred.hpp:326
bool more_choices() override
Checks if there are more choices.
Definition pred.cpp:188
bool apply_choice() override
Applies a choice.
Definition pred.cpp:176
std::shared_ptr< Cut > cut_pred
Definition pred.hpp:327
int choice_number
Definition pred.hpp:328
PredPtr if_pred
Definition pred.hpp:323
void initialize_call() override
Initializes the predicate call.
Definition pred.cpp:168
PredPtr else_pred
Definition pred.hpp:325
LoopBodyFactory is an abstract base class used for generating instances of a predicate class used in ...
Definition pred.hpp:334
virtual ~LoopBodyFactory()
Definition pred.hpp:340
virtual PredPtr make_body_pred()=0
virtual bool loop_continues()=0
LoopBodyFactory(Engine *eng)
Definition pred.hpp:338
Engine * engine
Definition pred.hpp:336
Constructs a predicate that loops over (instances of) a predicate while some condition (loop_continue...
Definition pred.hpp:351
void initialize_call() override
Initializes the predicate call.
Definition pred.cpp:197
PredPtr saved_continuation
Definition pred.hpp:369
LoopBodyFactory * body_factory
Definition pred.hpp:353
Loop(Engine *eng, LoopBodyFactory *bf)
Constructs a predicate that loops over (instances of) a predicate while some condition (loop_continue...
Definition pred.hpp:362
void set_continuation(PredPtr cont)
Sets the continuation for the loop predicate.
Definition pred.cpp:203
bool apply_choice() override
Applies a choice for the loop predicate.
Definition pred.cpp:219
Intended for internal use by NotNot which injects this call directly after the predicate supplied to ...
Definition pred.hpp:259
bool * succeeded
Definition pred.hpp:274
NotNotEnd(Engine *eng, bool *succ)
NotNot::initialize_call uses this to "terminate" the call supplied to NotNot.
Definition pred.hpp:268
bool apply_choice() override
Applies a choice for the not-not-end predicate.
Definition pred.cpp:103
void initialize_call() override
Initializes the predicate call.
Definition pred.hpp:270
Represents an equivalent of the Prolog call \+\+Call. The aim is to determine if Call succeeds withou...
Definition pred.hpp:281
NotNot(Engine *eng, PredPtr p)
Create the equivalent of Prolog's \+\+p call.
Definition pred.hpp:288
PredPtr pred
Definition pred.hpp:295
bool more_choices() override
Checks if there are more choices for the not-not predicate.
Definition pred.cpp:143
bool apply_choice() override
Applies a choice for the not-not predicate.
Definition pred.cpp:125
bool another_choice
Definition pred.hpp:297
bool succeeded
Definition pred.hpp:296
PredPtr saved_continuation
Definition pred.hpp:298
void initialize_call() override
Initializes the call for the not-not predicate.
Definition pred.cpp:111
Abstract base class for Prolog-like predicates.
Definition pred.hpp:54
virtual bool more_choices()
Checks if there are more choices.
Definition pred.hpp:76
Pred(Engine *eng)
Default constructor.
Definition pred.hpp:59
Engine * engine
Definition pred.hpp:119
PredPtr continuation
The continuation of the predicate.
Definition pred.hpp:118
std::string get_name()
For debugging.
Definition pred.hpp:115
virtual void initialize_call()
Initializes the predicate call.
Definition pred.hpp:64
virtual ~Pred()=default
Virtual destructor for proper cleanup.
PredPtr last_pred()
Follows the continuation chain to the last predicate.
Definition pred.cpp:47
virtual bool apply_choice()
Applies a choice.
Definition pred.hpp:70
PredPtr get_continuation()
Gets the continuation of the predicate.
Definition pred.hpp:82
void wrap_with_once()
Wraps the predicate with a once.
Definition pred.cpp:55
virtual void set_continuation(PredPtr cont)
Sets the continuation of the predicate.
Definition pred.hpp:88
bool is_non_det()
Determines if the predicate is non-deterministic.
Definition pred.hpp:100
Represents a semi-deterministic predicate.
Definition pred.hpp:147
bool is_non_det()
Definition pred.hpp:161
bool more_choices() override
Applies a choice.
Definition pred.hpp:159
SemiDetPred(Engine *eng)
Represents a semi-deterministic predicate.
Definition pred.hpp:153
Definition choice_iterator.hpp:16
std::shared_ptr< ChoiceIterator > ChoiceIteratorPtr
Definition pred.hpp:122
std::string repr(PredPtr pred)
Definition pred.cpp:231
std::shared_ptr< Pred > PredPtr
Typedef for a shared pointer to a Pred object.
Definition typedefs.hpp:33
PredPtr conjunction(std::vector< PredPtr > preds)
Represents a conjunction of predicates by chaining them together via continuations.
Definition pred.cpp:78
Definition of the Term class.
Definition of common typedefs used in the pl_search library.