Connect++ 0.4.0
A fast, readable connection prover for first-order logic.
Loading...
Searching...
No Matches
VariableIndex.cpp
1/*
2
3Copyright © 2023-24 Sean Holden. All rights reserved.
4
5*/
6/*
7
8This file is part of Connect++.
9
10Connect++ is free software: you can redistribute it and/or modify it
11under the terms of the GNU General Public License as published by the
12Free Software Foundation, either version 3 of the License, or (at your
13option) any later version.
14
15Connect++ is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18more details.
19
20You should have received a copy of the GNU General Public License along
21with Connect++. If not, see <https://www.gnu.org/licenses/>.
22
23*/
24
25#include "VariableIndex.hpp"
26
27//----------------------------------------------------------------------
28VariableIndex::VariableIndex()
29: vars()
30, name_index()
31, next_index(0)
32, next_unique_index(0)
33, first_anon_variable(0)
34, highest_anon_variable(0)
35, all_names_added(false)
36, backtrack_points()
37{}
38//----------------------------------------------------------------------
39VariableIndex::~VariableIndex() {
40 for (size_t i = 0; i < vars.size(); i++)
41 delete vars[i];
42}
43//----------------------------------------------------------------------
45 Variable* new_var = nullptr;
46 if (!all_names_added) {
47 auto i = name_index.find(name);
48 if (i == name_index.end()) {
49 new_var = new Variable(next_index++, name);
50 vars.push_back(new_var);
51 name_index.insert(MapType(name, new_var));
52 }
53 else {
54 new_var = i->second;
55 }
56 }
57 else {
58 cerr << "You can't add any more named variables." << endl;
59 }
60 return new_var;
61}
62//----------------------------------------------------------------------
67//----------------------------------------------------------------------
69 for (size_t i = 0; i < vars.size(); i++)
70 vars[i]->remove_substitution();
71}
72//----------------------------------------------------------------------
74 if (!all_names_added) {
76 }
77 Variable* new_var = nullptr;
78 // You can recycle an anonymous variable that was used
79 // earlier then freed up under backtracking.
81 new_var = vars[next_index++];
82 new_var->remove_substitution();
83 }
84 // You need to actually generate a new anonymous variable.
85 else {
87 string name("_");
88 name += to_string(next_index);
89 new_var = new Variable(next_index++, name);
90 vars.push_back(new_var);
91 }
92 return new_var;
93}
94//----------------------------------------------------------------------
96 string name(params::unique_var_prefix);
97 name += to_string(next_unique_index);
99 return add_named_var(name);
100}
101//----------------------------------------------------------------------
102Variable* VariableIndex::find_variable(const string& var_name) {
103 Variable* var = nullptr;
104 istringstream st(var_name);
105 char c;
106 st >> c;
107 if (c == '_') {
108 ID id;
109 st >> id;
110 if (id < next_index)
111 var = vars[id];
112 else
113 cerr << "That anonymous variable doesn't exist" << endl;
114 }
115 else {
116 auto i = name_index.find(var_name);
117 if (i != name_index.end())
118 var = i->second;
119 else
120 cerr << "That named variable doesn't exist" << endl;
121 }
122 return var;
123}
124//----------------------------------------------------------------------
128//----------------------------------------------------------------------
133//----------------------------------------------------------------------
135 if (backtrack_points.size() > 0) {
137 backtrack_points.pop_back();
138 }
139 else
140 cerr << "There's nowhere left to backtrack to." << endl;
141}
142//----------------------------------------------------------------------
144 while (backtrack_points.back() != id) {
145 backtrack_points.pop_back();
146 }
147 next_index = id;
148 backtrack_points.pop_back();
149}
150//----------------------------------------------------------------------
152 if (all_names_added) {
153 backtrack_points.clear();
155 }
156}
157//----------------------------------------------------------------------
158ostream& operator<<(ostream& out, const VariableIndex& vi) {
159 out << "Variable Index" << endl;
160 out << "--------------" << endl;
161 ID id = 0;
162 auto i = vi.backtrack_points.begin();
163 for (Variable* v : vi.vars) {
164 if (id == vi.next_index)
165 break;
166 out << *v << " ";
167 if (i != vi.backtrack_points.end()) {
168 if (id == *i) {
169 out << " <- Backtrack point";
170 i++;
171 }
172 }
173 out << endl;
174 id++;
175 }
176 out << "Highest anon id: " << vi.highest_anon_variable;
177 return out;
178}
Basic representation of variables.
Definition Variable.hpp:58
void remove_substitution()
Self-explanatory, but be careful!
Definition Variable.hpp:128
Storage of named variables, and management of new, anonymous variables.
Variable * add_unique_var()
Add a unique variable when converting to CNF.
void reset()
Get rid of all the markers used for backtracking.
ID next_unique_index
When converting to CNF you need new, unique variables, but they need to be treated separately from an...
vector< ID > backtrack_points
Backtrack points stored as indices in the vars vector.
void clear_substitutions()
Undo all substitutions made, to anything, ever.
Variable * add_named_var(const string &)
Add a variable with the specified name to the index.
ID next_index
Keep track of automatically generated Variable indices.
ID add_named_backtrack_point()
Same as add_backtrack_point, but return an identifier allowing multiple levels of backtracking using ...
ID first_anon_variable
Set after you've added all the named variables for the problem.
void set_all_names_added()
Call this to indicate that only anonymous variables can now be created.
ID highest_anon_variable
Keep track of how many anonymous Variables you currently have available..
void backtrack_to_named_point(ID)
Backtrack, possibly multiple times, to the specified point.
vector< Variable * > vars
All the variables made, named and anonymous.
Variable * add_anon_var()
Add an anonymous variable.
void backtrack()
Backtrack to the last marker.
void add_backtrack_point()
Add a backtrack point.
unordered_map< string, Variable * > name_index
Fast lookup of Variables by name.
bool all_names_added
Set after you've added all the named variables for the problem.
Variable * find_variable(const string &)
Look up a variable by name.