pl_search_cpp 1.4
Loading...
Searching...
No Matches
pvar.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_PVAR_HPP
26#define PL_SEARCH_PVAR_HPP
27
28#include "term.hpp"
29#include "typedefs.hpp"
30
36namespace pl_search {
37
44class PVar : public Term {
45
46public:
47 static int id;
48
50
54 PVar() : value(nullptr), var_id(id++) {}
55
60 TermPtr dereference() override;
61
69 virtual bool bind(const TermPtr &t);
70
75 void reset(const TermPtr &t);
76
81 bool is_var() override;
82
88 bool isLessThan(Term &other) const override;
89
94 int getVarId() const { return var_id; }
95
96 std::string repr() const override { return "X" + std::to_string(var_id); }
97
98protected:
99 virtual bool isEqualTo(Term &other) const override {
100 if (PVar *v = dynamic_cast<PVar *>(&other)) {
101
102 return getVarId() == v->getVarId();
103 }
104 return false;
105 }
106
107 Term *deref_term() override;
108
109 int var_id;
110};
111
115/*
116 * This is typically used to store (part of) the
117 * state in a way that can be backtracked over. For example, after
118 * binding a variable and then making some deductions the available choices
119 * for another variable might have been reduced and we can use an UpdatableVar
120 * to store that value as the computation moves forward but on backtracking
121 * the old value will be restored.
122 */
123class UpdatablePVar : public PVar {
124
125public:
127
128 // As the intention is to update the value of the variable, we need to
129 // simply return this pointer so that a following bind will update the value.
130 // If we wanted to do a full dereference for an updatable variable, v, we
131 // would use v->PVar::dereference() or v->getValue()->dereference()
132 TermPtr dereference() override { return shared_from_this(); }
133
134protected:
135 Term *deref_term() override { return this; }
136};
137
138} // namespace pl_search
139
140#endif // PL_SEARCH_PVAR_HPP
Represents a Prolog variable.
Definition pvar.hpp:44
virtual bool isEqualTo(Term &other) const override
Checks if the term is equal to another term.
Definition pvar.hpp:99
int var_id
The ID of the variable.
Definition pvar.hpp:109
TermPtr dereference() override
Dereferences the variable to find the actual term it points to.
Definition pvar.cpp:42
TermPtr value
The value of the variable.
Definition pvar.hpp:49
static int id
Static member to generate unique IDs for variables.
Definition pvar.hpp:47
int getVarId() const
Returns the variable ID.
Definition pvar.hpp:94
PVar()
Constructs a PVar.
Definition pvar.hpp:54
Term * deref_term() override
Similar to dereference but returning the "raw" pointer.
Definition pvar.cpp:60
void reset(const TermPtr &t)
Resets the variable to point at the supplied term.
Definition pvar.cpp:110
std::string repr() const override
Returns a string representation of the term.
Definition pvar.hpp:96
bool is_var() override
Checks if the term is a variable.
Definition pvar.cpp:82
virtual bool bind(const TermPtr &t)
Binds the variable to a term.
Definition pvar.cpp:95
bool isLessThan(Term &other) const override
Checks if the variable is less than another term.
Definition pvar.cpp:117
Abstract base class for terms that approximate Prolog terms.
Definition term.hpp:50
UpdatableVar implements what some Prologs call updatable assignment.
Definition pvar.hpp:123
TermPtr dereference() override
Dereferences the variable to find the actual term it points to.
Definition pvar.hpp:132
Term * deref_term() override
Similar to dereference but returning the "raw" pointer.
Definition pvar.hpp:135
UpdatablePVar(TermPtr t)
Definition pvar.hpp:126
Definition choice_iterator.hpp:16
std::shared_ptr< Term > TermPtr
Typedefs for shared pointers to Terms and subclasses.
Definition typedefs.hpp:43
Definition of the Term class.
Definition of common typedefs used in the pl_search library.