Connect++ 0.5.0
A fast, readable connection prover for first-order logic.
Loading...
Searching...
No Matches
TermIndex.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 "TermIndex.hpp"
26
27//----------------------------------------------------------------------
28#ifdef HASHCONSTERMS
29
30Term* TermIndex::find(const Term& t) {
31 auto i = index.find(t);
32 if (i != index.end()) {
33 return i->second;
34 }
35 return nullptr;
36}
37
38#else
39
41 for (size_t i = 0; i < term_pointers.size(); i++) {
42 if (*(term_pointers[i]) == t) {
43 return term_pointers[i];
44 }
45 }
46 return nullptr;
47}
48
49#endif
50//----------------------------------------------------------------------
51TermIndex::~TermIndex() {
52 for (size_t i = 0; i < term_pointers.size(); i++)
53 delete term_pointers[i];
54}
55//----------------------------------------------------------------------
57 if (vp->is_subbed()) {
58 cerr << "Don't add substituted variables to the Term Index!" << endl;
59 }
60 Term t(vp);
61 Term* p = find(t);
62 if (p != nullptr) {
63 return p;
64 }
65 p = new Term(vp);
66#ifdef HASHCONSTERMS
67 index.insert(pair<Term, Term*>(t, p));
68#endif
69 term_pointers.push_back(p);
70 return p;
71}
72//----------------------------------------------------------------------
74 const vector<Term*>& args) {
75 for (size_t i = 0; i < args.size(); i++) {
76 if (args[i]->is_subbed()) {
77 cerr << "Don't add substituted functions to the Term Index!" << endl;
78 }
79 }
80 Term t(fp, args);
81 Term* p = find(t);
82 if (p != nullptr) {
83 return p;
84 }
85 p = new Term(fp, args);
86#ifdef HASHCONSTERMS
87 index.insert(pair<Term, Term*>(t, p));
88#endif
89 term_pointers.push_back(p);
90 return p;
91}
92//----------------------------------------------------------------------
94 Variable* old_v,
95 Term* t) {
96 if (t->is_variable()) {
97 if (old_v == t->get_v()) {
98 return add_variable_term(new_v);
99 }
100 else
101 return t;
102 }
103 else {
104 Function* f = t->get_f();
105 vector<Term*> new_args;
106 for (size_t i = 0; i < t->arity(); i++)
107 new_args.push_back(replace_variable_in_term(new_v, old_v, (*t)[i]));
108 return add_function_term(f, new_args);
109 }
110}
111//----------------------------------------------------------------------
112// ONLY use this if the new Term is in the index.
113//----------------------------------------------------------------------
115 Variable* old_v,
116 Term* t) {
117 if (t->is_variable()) {
118 if (old_v == t->get_v()) {
119 return new_t;
120 }
121 else
122 return t;
123 }
124 else {
125 Function* f = t->get_f();
126 vector<Term*> new_args;
127 for (size_t i = 0; i < t->arity(); i++)
128 new_args.push_back(replace_variable_in_term_with_term(new_t, old_v, (*t)[i]));
129 return add_function_term(f, new_args);
130 }
131}
132//----------------------------------------------------------------------
133ostream& operator<<(ostream& out, const TermIndex& t) {
134 out << "Contents of term index:" << endl;
135 out << "-----------------------" << endl;
136 for (Term* p : t.term_pointers)
137 out << p << " : " << *p << endl;
138 return out;
139}
140
141
142/*
143* From here on we have methods for Term that need to use the TermIndex.
144*/
145
146
147//----------------------------------------------------------------------
149 TermIndex& ti,
150 unordered_map<Variable*,Term*>& v_map) const {
151 Term* result = nullptr;
152 if (v != nullptr) {
153 if (v->is_subbed()) {
154 result = v->get_subbed_term()->make_copy_with_new_vars_helper(vi, ti, v_map);
155 cerr << "If you're doing Connection Calculus then something's wrong!" << endl;
156 }
157 else {
158 auto i = v_map.find(v);
159 if (i == v_map.end()) {
160 Variable* new_v = vi.add_anon_var();
161 result = ti.add_variable_term(new_v);
162 v_map.insert(VarMapType(v, result));
163 }
164 else {
165 result = i->second;
166 }
167 }
168 }
169 else {
170 vector<Term*> new_args;
171 for (size_t i = 0; i < args.size(); i++) {
172 new_args.push_back(args[i]->make_copy_with_new_vars_helper(vi, ti, v_map));
173 }
174 result = ti.add_function_term(f, new_args);
175 }
176 return result;
177}
178//----------------------------------------------------------------------
180 TermIndex& ti) const {
181
182 unordered_map<Variable*,Term*> v_map;
183 return make_copy_with_new_vars_helper(vi, ti, v_map);
184}
Basic representation of functions.
Definition Function.hpp:54
General representation of terms.
Definition Term.hpp:62
Term * make_copy_with_new_vars(VariableIndex &, TermIndex &) const
Does as the name suggests.
Variable * get_v() const
Self-explanatory access function.
Definition Term.hpp:106
Term * make_copy_with_new_vars_helper(VariableIndex &, TermIndex &, unordered_map< Variable *, Term * > &) const
Main implementation of make_copy_with_new_vars.
Function * get_f() const
Self-explanatory access function.
Definition Term.hpp:110
bool is_variable() const
Self-explanatory.
Definition Term.hpp:114
Arity arity() const
Self-explanatory access function.
Definition Term.hpp:122
Look after terms, (ideally) using hash consing to avoid storing copies of terms.
Definition TermIndex.hpp:75
Term * find(const Term &)
Find a term in the index.
Definition TermIndex.cpp:40
Term * add_function_term(Function *, const vector< Term * > &)
Self-explanatory: add a Term containing a function to the index.
Definition TermIndex.cpp:73
vector< Term * > term_pointers
Hash table for storing pointers to where Terms are actually stored.
Definition TermIndex.hpp:93
Term * add_variable_term(Variable *)
Self-explanatory: add a Term containing a variable to the index.
Definition TermIndex.cpp:56
Term * replace_variable_in_term_with_term(Term *, Variable *, Term *)
Minor variation on replace_variable_in_term.
Term * replace_variable_in_term(Variable *, Variable *, Term *)
Replace a variable in a term with an alternative, maintaining the structure of the TermIndex.
Definition TermIndex.cpp:93
Basic representation of variables.
Definition Variable.hpp:58
bool is_subbed() const
Self-explanatory.
Definition Variable.hpp:111
Term * get_subbed_term() const
Self-explanatory.
Definition Variable.hpp:116
Storage of named variables, and management of new, anonymous and unique variables.
Variable * add_anon_var()
Add an anonymous variable.