pl_search_cpp 2.2
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#include "trail.hpp"
33
34#include <memory>
35#include <stack>
36#include <vector>
37
44namespace pl_search {
45
46
111 class Pred : public std::enable_shared_from_this<Pred> {
112 public:
118
124
129 virtual void set_continuation(PredPtr cont) { continuation = cont; }
130
136
151 virtual void call_init() {}
152
153
154 virtual bool call() { return false; }
155
164 if (continuation == nullptr) return true;
165 continuation->call_init();
166 return continuation->call();
167 }
168
172 virtual ~Pred() = default;
173
177 virtual std::string get_name() { return typeid(*this).name(); }
178
179 protected:
182 };
183
184
185
189 class FailPred : public Pred {
190 public:
197 void set_continuation(PredPtr cont) override {
198 // FailPred doesn not need a continuation, so we ignore this.
199 }
200
205 bool call() override { return false; }
206
207 };
208
212 extern std::shared_ptr<FailPred> fail_pred;
213
214
215
224 class ChoicePred : public Pred {
225 public:
226
232 : Pred(trail) {
233 }
234
240 bool call() override;
241
246 virtual bool has_next() = 0;
247
252 virtual bool make_choice() = 0;
253
260 virtual bool test_choice() { return true; }
261
262 };
263
271 class VarChoicePred : public ChoicePred {
272 public:
279 VarChoicePred(Trail* trail, PVarPtr v, std::vector<TermPtr> choices)
280 : ChoicePred(trail), var(v), choices(choices), index(0) {
281 }
282
286 void call_init() override { index = 0; }
287
292 bool has_next() override { return index < choices.size(); }
293
298 bool make_choice() override {
299 TermPtr t = choices[index++];
300 return unify(var, t, trail) && test_choice();
301 }
302
303 protected:
305 std::vector<TermPtr> choices;
306 size_t index;
307
308 };
309
317 PredPtr conjunction(std::vector<PredPtr> preds);
318
322 class DisjPred : public Pred {
323 public:
333 DisjPred(Trail* trail, std::vector<PredPtr> preds)
334 : Pred(trail), preds(preds) {
335 }
336
342 bool call() override;
343
350 void set_continuation(PredPtr cont) override;
351
352 protected:
353 std::vector<PredPtr> preds;
354
355 };
356
365 class Once : public Pred {
366 public:
373
379 bool call() override;
380
381 protected:
383 };
384
385
394 class NotNot : public Pred {
395 public:
402
409 bool call() override;
410 protected:
412
413 };
414
427 class IfThenElse : public Pred {
428 public:
438
444 void set_continuation(PredPtr cont) override;
445 bool call() override;
446
447 protected:
451 };
457 public:
459
465
466 virtual ~LoopBodyFactory() {};
467
475 virtual bool loop_continues(int depth) { return false; }
476
497 virtual PredPtr make_body_pred(int depth) = 0;
498
499 };
500
521 class Loop : public Pred {
522 public:
524
536
537 bool call() override;
538
539 protected:
540
541 int depth;
542
543 };
544
545 // for testing
546 std::string repr(PredPtr pred);
547
548} // namespace pl_search
549
550#endif // PL_SEARCH_PRED_HPP_
A base class that represents a choice predicate.
Definition pred.hpp:224
bool call() override
Calls the choice predicate.
Definition pred.cpp:56
virtual bool has_next()=0
Checks if there are more choices available.
virtual bool make_choice()=0
Makes a choice from the available options.
ChoicePred(Trail *trail)
Constructs a ChoicePred that tries available choices.
Definition pred.hpp:231
virtual bool test_choice()
Tests the current choice for validity.
Definition pred.hpp:260
Represents a disjunction of predicates.
Definition pred.hpp:322
bool call() override
Calls the disjunction predicate, iterating through available predicates.
Definition pred.cpp:85
DisjPred(Trail *trail, std::vector< PredPtr > preds)
Constructs a DisjPred with the given predicates.
Definition pred.hpp:333
void set_continuation(PredPtr cont) override
Sets the continuation for all predicates in the disjunction.
Definition pred.cpp:98
std::vector< PredPtr > preds
The predicates in the disjunction.
Definition pred.hpp:353
Represents a failure predicate that always fails when called.
Definition pred.hpp:189
bool call() override
call method for FailPred always returns false, indicating failure.
Definition pred.hpp:205
void set_continuation(PredPtr cont) override
Sets the continuation of the predicate.
Definition pred.hpp:197
FailPred(Trail *trail)
Constructs a FailPred with the given trail -it does nothing and always fails when called.
Definition pred.hpp:196
Represents the equivalent of Prolog if-then-else i.e. (G1 -> G2; G3) - similar to (G1,...
Definition pred.hpp:427
bool call() override
Definition pred.cpp:154
void set_continuation(PredPtr cont) override
Sets the continuation for the then and else predicates to the given continuation which will be the co...
Definition pred.cpp:181
PredPtr then_pred
Definition pred.hpp:449
PredPtr if_pred
Definition pred.hpp:448
PredPtr else_pred
Definition pred.hpp:450
LoopBodyFactory is an abstract base class used for generating instances of a predicate class used in ...
Definition pred.hpp:456
virtual ~LoopBodyFactory()
Definition pred.hpp:466
virtual PredPtr make_body_pred(int depth)=0
Creates a new instance of the body predicate for the loop.
Trail * trail
Definition pred.hpp:458
LoopBodyFactory(Trail *trail)
Constructs a LoopBodyFactory with the given trail.
Definition pred.hpp:464
virtual bool loop_continues(int depth)
Determines if the loop should continue based on the current depth.
Definition pred.hpp:475
Represents a loop predicate that iterates over instances of a predicate.
Definition pred.hpp:521
int depth
Definition pred.hpp:541
Loop(Trail *trail, LoopBodyFactoryPtr bf)
Constructs a predicate that loops over (instances of) a predicate while loop_continues() is true.
Definition pred.hpp:532
bool call() override
Calls the Loop predicate.
Definition pred.cpp:200
Loop(Trail *trail, LoopBodyFactoryPtr bf, int depth)
Definition pred.hpp:535
LoopBodyFactoryPtr body_factory
Definition pred.hpp:523
Represents an equivalent of the Prolog call \+\+Pred. The aim is to determine if Pred succeeds withou...
Definition pred.hpp:394
PredPtr pred
Definition pred.hpp:411
NotNot(Trail *trail, PredPtr pred)
Create the equivalent of Prolog's \+\+p call.
Definition pred.hpp:401
bool call() override
Calls the NotNot predicate.
Definition pred.cpp:127
Represents the equivalent of Prolog's once(Pred). The aim is to remove any choicepoints produced by P...
Definition pred.hpp:365
PredPtr pred
Definition pred.hpp:382
bool call() override
Calls the once predicate.
Definition pred.cpp:112
Once(Trail *trail, PredPtr pred)
Constructs a Once predicate with the given trail and predicate.
Definition pred.hpp:372
Base class for Prolog-like predicates.
Definition pred.hpp:111
Pred(Trail *trail)
Default constructor.
Definition pred.hpp:117
bool call_continuation()
Calls the continuation of the predicate - first call_init and then call.
Definition pred.hpp:163
virtual std::string get_name()
For debugging.
Definition pred.hpp:177
PredPtr continuation
The continuation of the predicate.
Definition pred.hpp:180
virtual void call_init()
Initialize call.
Definition pred.hpp:151
virtual ~Pred()=default
Virtual destructor for proper cleanup.
PredPtr last_pred()
Follows the continuation chain to the last predicate.
Definition pred.cpp:48
virtual bool call()
Definition pred.hpp:154
Trail * trail
Pointer to the trail for managing variable bindings.
Definition pred.hpp:181
PredPtr get_continuation()
Gets the continuation of the predicate.
Definition pred.hpp:123
virtual void set_continuation(PredPtr cont)
Sets the continuation of the predicate.
Definition pred.hpp:129
The Trail class is used to manage variable bindings during the execution of Prolog-like predicates.
Definition trail.hpp:64
Represents a choice predicate that makes choices for a variable from a set of terms.
Definition pred.hpp:271
void call_init() override
Reset index so that all choices are considered when the predicate is (re)called.
Definition pred.hpp:286
bool has_next() override
Checks if there are more choices available.
Definition pred.hpp:292
bool make_choice() override
Makes a choice from the available options.
Definition pred.hpp:298
PVarPtr var
The variable for which choices are made.
Definition pred.hpp:304
size_t index
The current index in the choices vector.
Definition pred.hpp:306
VarChoicePred(Trail *trail, PVarPtr v, std::vector< TermPtr > choices)
Constructs a VarChoicePred with the given trail, variable, and choices.
Definition pred.hpp:279
std::vector< TermPtr > choices
The available choices for the variable.
Definition pred.hpp:305
Definition clist.hpp:45
std::shared_ptr< FailPred > fail_pred
A shared pointer to a reusable failure predicate.
Definition pred.cpp:41
std::string repr(PredPtr pred)
Definition pred.cpp:216
bool unify(TermPtr t1, TermPtr t2, Trail *trail)
Unifies two terms, binding variables as necessary.
Definition term.cpp:165
std::shared_ptr< Pred > PredPtr
Typedef for a shared pointer to a Pred object.
Definition typedefs.hpp:34
PredPtr conjunction(std::vector< PredPtr > preds)
Represents a conjunction of predicates by chaining them together via continuations....
Definition pred.cpp:72
std::shared_ptr< LoopBodyFactory > LoopBodyFactoryPtr
Definition typedefs.hpp:51
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 Term class.
Definition of the binding trail.
Definition of common typedefs used in the pl_search library.