pl_search_cpp 1.7
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 {
97 if (value) {
98 return value->dereference()->repr();
99 }
100 return "X" + std::to_string(var_id);
101 }
102
103protected:
104 virtual bool isEqualTo(Term &other) const override {
105 if (PVar *v = dynamic_cast<PVar *>(&other)) {
106
107 return getVarId() == v->getVarId();
108 }
109 return false;
110 }
111
112 Term *deref_term() override;
113
114 int var_id;
115};
116
120/*
121 * This is typically used to store (part of) the
122 * state in a way that can be backtracked over. For example, after
123 * binding a variable and then making some deductions the available choices
124 * for another variable might have been reduced and we can use an UpdatableVar
125 * to store that value as the computation moves forward but on backtracking
126 * the old value will be restored.
127 */
128class UpdatablePVar : public PVar {
129
130public:
132
133 // As the intention is to update the value of the variable, we need to
134 // simply return this pointer so that a following bind will update the value.
135 // If we wanted to do a full dereference for an updatable variable, v, we
136 // would use v->PVar::dereference() or v->getValue()->dereference()
137 TermPtr dereference() override { return shared_from_this(); }
138
139protected:
140 Term *deref_term() override { return this; }
141};
142
143} // namespace pl_search
144
145#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:104
int var_id
The ID of the variable.
Definition pvar.hpp:114
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:128
TermPtr dereference() override
Dereferences the variable to find the actual term it points to.
Definition pvar.hpp:137
Term * deref_term() override
Similar to dereference but returning the "raw" pointer.
Definition pvar.hpp:140
UpdatablePVar(TermPtr t)
Definition pvar.hpp:131
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.