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