Connect++ 0.6.0
A fast, readable connection prover for first-order logic.
Loading...
Searching...
No Matches
StackItem.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 "StackItem.hpp"
26
27//----------------------------------------------------------------------
28ostream& operator<<(ostream& out, const StackItemType& si) {
29 switch (si) {
30 case StackItemType::Start:
31 out << "Start";
32 break;
33 case StackItemType::Axiom:
34 out << "Axiom";
35 break;
36 case StackItemType::Reduction:
37 out << "Reduction";
38 break;
39 case StackItemType::LeftBranch:
40 out << "Left Branch";
41 break;
42 case StackItemType::RightBranch:
43 out << "Right Branch";
44 break;
45 default:
46 break;
47 }
48 return out;
49}
50//----------------------------------------------------------------------
70//----------------------------------------------------------------------
75//----------------------------------------------------------------------
77 // Default constructor makes InferenceItemType::None.
79 return;
80 UnificationOutcome outcome = UnificationOutcome::ConflictFail;
81 while (outcome != UnificationOutcome::Succeed &&
85 continue;
86 }
87 outcome = _u(reduction_neg_lit, lit2);
88 if (outcome == UnificationOutcome::Succeed) {
89 reduction_next_result = InferenceItem(InferenceItemType::Reduction, reduction_lit, reduction_ind,
91 _u.backtrack();
92 return;
93 }
94 _u.backtrack();
95 }
97}
98//----------------------------------------------------------------------
100 // Default constructor makes InferenceItemType::None.
101 InferenceItem result;
103 return result;
104 result = reduction_next_result;
106 return result;
107}
108//----------------------------------------------------------------------
109void StackItem::all_reductions(vector<InferenceItem>& reductions,
110 Unifier& _u) {
112 reductions.push_back(reduction_next_result);
114 }
115}
116//----------------------------------------------------------------------
123//----------------------------------------------------------------------
126 return;
127 }
128 UnificationOutcome outcome = UnificationOutcome::ConflictFail;
129 while (outcome != UnificationOutcome::Succeed &&
132 outcome = _u(extension_neg_lit, _m.inspect_index_literal(extension_i, new_index));
133 if (outcome == UnificationOutcome::Succeed) {
134 const MatrixPairType p = _m.get_index_entry(extension_i, new_index);
135 extension_next_result = InferenceItem(InferenceItemType::Extension,
137 p.first, p.second, extension_i, new_index);
138 _u.backtrack();
139 return;
140 }
141 _u.backtrack();
142 }
144}
145//----------------------------------------------------------------------
147 // Default constructor makes InferenceItemType::None.
148 InferenceItem result;
150 return result;
151 result = extension_next_result;
153 return result;
154}
155//----------------------------------------------------------------------
156void StackItem::all_extensions(vector<InferenceItem>& extensions,
157 Matrix& _m, Unifier& _u) {
159 extensions.push_back(extension_next_result);
161 }
162}
163//----------------------------------------------------------------------
168//----------------------------------------------------------------------
170 /*
171 * Implement the leanCop style restriction on backtracking.
172 */
173 actions.clear();
176}
177//----------------------------------------------------------------------
181//----------------------------------------------------------------------
185//----------------------------------------------------------------------
189//----------------------------------------------------------------------
191 InferenceItem result;
192 if (actions.size() > 0) {
193 result = actions.back();
194 actions.pop_back();
195 return result;
196 }
197 if (!reduction_is_empty()) {
198 return next_reduction(_u);
199 }
200 if (!extension_is_empty()) {
201 return next_extension(_m, _u);
202 }
203 return result;
204}
205//----------------------------------------------------------------------
207 string result;
208 result += "Stack item: ";
209 switch (item_type) {
210 case StackItemType::Start:
211 result += "Start";
212 break;
213 case StackItemType::Axiom:
214 result += "Axiom";
215 break;
216 case StackItemType::Reduction:
217 result += "Reduction";
218 break;
219 case StackItemType::LeftBranch:
220 result += "Left Branch";
221 break;
222 case StackItemType::RightBranch:
223 result += "Right Branch";
224 break;
225 default:
226 break;
227 }
228 result += " with ";
229 result += to_string(actions.size()) += " options\n";
230 result += "C = ";
231 result += c.to_string() += "\n";
232 result += "P = ";
233 result += p.to_string() += "\n";
234 result += "L = ";
235 result += l.to_string() += "\n";
236 result += "Sub = ";
237 result += sub.to_string() += "\n";
238 result += "Depth: ";
239 result += std::to_string(depth);
240 return result;
241}
242//----------------------------------------------------------------------
243ostream& operator<<(ostream& out, const StackItem& si) {
244 out << si.to_string_unsubbed() << endl;
245 for (const InferenceItem& i : si.actions) {
246 out << i << endl;
247 }
248 return out;
249}
Literal get_literal(LitNum) const
Get and return the specified Literal.
Definition Clause.cpp:200
string to_string(bool=false) const
Convert to a string.
Definition Clause.cpp:244
string to_string(bool=false) const
Convert to a string.
Definition Lemmata.cpp:82
Basic representation of literals, bundling together (pointers to) a Predicate, a collection of argume...
Definition Literal.hpp:50
bool is_compatible_with(const Literal *) const
Literals can only be unified if the polarity and actual predicate are the same.
Definition Literal.cpp:74
size_t get_pred_as_index() const
Turn a Literal into an array index.
Definition Literal.hpp:126
void invert()
Basic manipulation - entirely self-explanatory.
Definition Literal.hpp:107
Representation of the Matrix within a proof.
Definition Matrix.hpp:74
const Literal & inspect_index_literal(size_t i, size_t j)
Straightforward get method.
Definition Matrix.hpp:265
MatrixPairType get_index_entry(size_t _i, size_t _j) const
The ExtensionEnumerator needs to know this.
Definition Matrix.hpp:235
size_t get_index_entry_size(size_t _i) const
The ExtensionEnumerator needs to know this.
Definition Matrix.hpp:229
string to_string(bool=false) const
Convert the path to a string.
uint32_t length() const
Straightforward get method.
const Literal & examine_literal(size_t i) const
Access individual literals.
string to_string(bool=false) const
Make a string representation.
Wrap up various applications of unificiation into a single class: all the unification you need to do ...
Definition Unifier.hpp:89
void backtrack()
Apply the backtracking process to the substitution that has been constructed.
Definition Unifier.cpp:195
Substitution get_substitution() const
Trivial get methods for the result.
Definition Unifier.hpp:122
Full representation of inferences, beyond just the name.
Stack items: each contains its own material for generating possible next inferences.
Definition StackItem.hpp:56
size_t reduction_current_path_index
Which possible reduction will we consider next?
size_t extension_current_index_entry
Which possible extension will we consider next?
void add_all_reductions_to_actions(Unifier &)
You may really want to generate all the reductions at once.
bool extension_no_more_results
Make it easy to know when there are no more possibilities.
InferenceItem get_next_inference(Matrix &, Unifier &)
Get the next inference to try.
void restrict_backtrack()
Adjust the collection of actions to limit backtracking.
SimplePath p
Each node in the proof tree is a tuple of clause, matrix, path, lemmata: this is the path.
Definition StackItem.hpp:70
size_t extension_index_entry_size
Size of matrix index entry.
void all_extensions(vector< InferenceItem > &, Matrix &, Unifier &)
Generate all possible extensions in one go, should you need to.
InferenceItem extension_next_result
We always want to know the next available result, and especially need to try to find it so that we ac...
vector< InferenceItem > actions
Actions available to try.
Definition StackItem.hpp:96
string to_string_unsubbed() const
Make a string representation.
Literal reduction_neg_lit
Negation of literal of interest.
size_t extension_i
Index computed for neg_lit telling us where to find the information we need in the matrix index.
Literal reduction_lit
Literal of interest.
bool extension_backtrack_restricted
Setting this to true stops any further possibilities from being generated.
Substitution sub
Some nodes in the proof tree have an associated substitution.
Definition StackItem.hpp:84
uint32_t depth
How deep in the proof tree are we?
InferenceItem next_extension(Matrix &, Unifier &)
Find the next possible extension or indicate there is none. The state of the object keeps track of wh...
bool reduction_no_more_results
Make it easy to know when there are no more possibilities.
void extension_restrict_backtrack()
Indicate that we don't want any further possibilities.
Literal extension_lit
Literal of interest.
bool no_more_inferences()
Is there anything left to do?
void add_all_extensions_to_actions(Matrix &, Unifier &)
You may really want to generate all the extensions at once.
InferenceItem reduction_next_result
We always want to know the next available result, and especially need to try to find it so that we ac...
void initialize(Matrix &, Unifier &)
Call this after the constructor but before you want to use the enumeration of possible actions.
bool reduction_is_empty() const
Have we exhaused all possible reductions?
bool extension_is_empty() const
Have we exhaused all possible extensions?
void extension_initialize(Matrix &, Unifier &)
Make sure you do this!
bool reduction_backtrack_restricted
Setting this to true stops any further possibilities from being generated.
LitNum reduction_ind
Index of literal of interest.
void reduction_initialize(Unifier &)
Make sure you do this!
Definition StackItem.cpp:71
void extension_move_to_next_result(Matrix &, Unifier &)
Before you do anything else, set up the first result.
Literal extension_neg_lit
Negation of literal of interest.
Lemmata l
Each node in the proof tree is a tuple of clause, matrix, path, lemmata: this is the lemmata.
Definition StackItem.hpp:79
InferenceItem next_reduction(Unifier &)
Find the next possible reduction or indicate there is none. The state of the object keeps track of wh...
Definition StackItem.cpp:99
Clause c
Each node in the proof tree is a tuple of clause, matrix, path, lemmata: this is the clause.
Definition StackItem.hpp:65
void reduction_move_to_next_result(Unifier &)
Before you do anything else, set up the first result.
Definition StackItem.cpp:76
StackItemType item_type
What type of StackItem is this?
Definition StackItem.hpp:60
LitNum extension_ind
Index of literal of interest.
void all_reductions(vector< InferenceItem > &, Unifier &)
Generate all possible reductions in one go, should you need to.
void reduction_restrict_backtrack()
Indicate that we don't want any further possibilities.
void stack_item_setup()
Basic setup collected in a method to avoid repeating it in the various constructors.
Definition StackItem.cpp:51
size_t path_length
Length of current path.
Definition StackItem.hpp:74