Connect++ 0.3.0
A fast, readable connection prover for first-order logic.
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
Matrix Class Reference

Representation of the Matrix within a proof. More...

#include <Matrix.hpp>

Collaboration diagram for Matrix:
Collaboration graph
[legend]

Public Member Functions

 Matrix ()
 Use this constructor if you really want an empty Matrix.
 
 Matrix (size_t)
 Use this constructor when you know how many predicates there are.
 
 Matrix (const Matrix &)=delete
 You're never going to need to copy a Matrix.
 
 Matrix (const Matrix &&)=delete
 
Matrixoperator= (const Matrix &)=delete
 
Matrixoperator= (const Matrix &&)=delete
 
ClauseNum get_num_clauses () const
 Straightforward get method.
 
const Clauseoperator[] (size_t i) const
 Straightforward get method.
 
bool is_positive (size_t i) const
 Is a particular Clause positive?.
 
bool is_negative (size_t i) const
 Is a particular Clause negative?.
 
bool is_conjecture (size_t i) const
 Is a particular Clause a conjecture?
 
void set_num_equals (uint32_t n)
 Straightforward set method.
 
pair< bool, size_t > find_start () const
 Use a simple heuristic to find a good start clause.
 
void set_num_preds (size_t)
 Make an empty index.
 
void add_clause (Clause &, string="")
 Add a Clause to the Matrix and update the index accordingly.
 
void find_limited_extensions (Unifier &, vector< InferenceItem > &, Clause &, VariableIndex &, TermIndex &)
 Find all possible extensions given a Clause, but only consider the first Literal in the Clause.
 
void find_all_extensions (Unifier &, vector< InferenceItem > &, Clause &, VariableIndex &, TermIndex &)
 Find all possible extensions given a Clause, considering all Literals in the Clause.
 
void deterministic_reorder (size_t)
 Deterministic reorder of the clauses.
 
void random_reorder ()
 Randomly reorder the matrix.
 
void move_equals_to_start ()
 Self-explanatory.
 
vector< Clause >::const_iterator cbegin () const
 
vector< Clause >::const_iterator cend () const
 
vector< Clause >::iterator begin ()
 
vector< Clause >::iterator end ()
 
string to_string () const
 Make a string representation.
 
string make_LaTeX (bool=false) const
 Make a usable LaTeX representation.
 
void write_to_prolog_file (const path &) const
 Write to a file that can be read by Prolog.
 
void show_tptp () const
 Output in TPTP compatible format.
 

Private Member Functions

void find_extensions (Unifier &, vector< InferenceItem > &, const Literal &, LitNum, VariableIndex &, TermIndex &)
 Find all possible extensions, given a Literal.
 
void make_clauses_copy ()
 Store a copy of the clauses.
 

Private Attributes

vector< Clauseclauses
 The Matrix itself is just a set of Clauses.
 
vector< bool > positive_clauses
 Keep track of which clauses are positive.
 
vector< bool > negative_clauses
 Keep track of which clauses are negative.
 
vector< string > clause_roles
 Keep track of which clauses are conjectures etc.
 
uint32_t num_equals
 You need to know how many equality axioms there are in case you want to move them around.
 
vector< Clauseclauses_copy
 It makes sense to keep a copy of the clauses if your schedule reorders multiple times.
 
vector< string > roles_copy
 It makes sense to keep a copy of the roles if your schedule reorders multiple times.
 
bool copy_saved
 Remember whether you've saved a copy.
 
vector< vector< MatrixPairType > > index
 We want to be able to quickly identify where in each clause a particular literal lives.
 

Static Private Attributes

static std::mt19937 d
 Randomness for random reordering.
 

Friends

ostream & operator<< (ostream &, const Matrix &)
 

Detailed Description

Representation of the Matrix within a proof.

The Matrix itself never actually changes. However we do want to be able to look things up quickly when finding possible extension steps, so we build an index to help with that.

This class is also the natural place to implement the finding of all extensions, and the start clause heuristics.

Definition at line 72 of file Matrix.hpp.

Constructor & Destructor Documentation

◆ Matrix() [1/3]

Matrix::Matrix ( )
inline

Use this constructor if you really want an empty Matrix.

Definition at line 162 of file Matrix.hpp.

163 : clauses()
164 , index()
167 , clause_roles()
168 , clauses_copy()
169 , roles_copy()
170 , copy_saved(false)
171 {}
vector< Clause > clauses
The Matrix itself is just a set of Clauses.
Definition Matrix.hpp:77
vector< string > clause_roles
Keep track of which clauses are conjectures etc.
Definition Matrix.hpp:95
vector< vector< MatrixPairType > > index
We want to be able to quickly identify where in each clause a particular literal lives.
Definition Matrix.hpp:122
bool copy_saved
Remember whether you've saved a copy.
Definition Matrix.hpp:114
vector< string > roles_copy
It makes sense to keep a copy of the roles if your schedule reorders multiple times.
Definition Matrix.hpp:110
vector< Clause > clauses_copy
It makes sense to keep a copy of the clauses if your schedule reorders multiple times.
Definition Matrix.hpp:105
vector< bool > negative_clauses
Keep track of which clauses are negative.
Definition Matrix.hpp:89
vector< bool > positive_clauses
Keep track of which clauses are positive.
Definition Matrix.hpp:83

◆ Matrix() [2/3]

Matrix::Matrix ( size_t  num_predicates)

Use this constructor when you know how many predicates there are.

Parameters
num_predicatesNumber of predicates.

Definition at line 30 of file Matrix.cpp.

31: clauses()
32, index((num_predicates * 2), vector<MatrixPairType>())
36, num_equals(0)
38, roles_copy()
39, copy_saved(false)
40{}
uint32_t num_equals
You need to know how many equality axioms there are in case you want to move them around.
Definition Matrix.hpp:100

◆ Matrix() [3/3]

Matrix::Matrix ( const Matrix )
delete

You're never going to need to copy a Matrix.

Let the compiler be your friend.

Member Function Documentation

◆ add_clause()

void Matrix::add_clause ( Clause clause,
string  role = "" 
)

Add a Clause to the Matrix and update the index accordingly.

Optionally add a clause role.

Parameters
clauseReference to the Clause.
roleA string denoting the role of the clause.

Definition at line 91 of file Matrix.cpp.

91 {
92 ClauseNum clause_number = clauses.size();
93 LitNum literal_number = 0;
94 for (size_t j = 0; j < clause.size(); j++) {
95 size_t i = clause[j].get_pred_as_index();
96 index[i].push_back(MatrixPairType(clause_number, literal_number));
97 literal_number++;
98 }
99 clauses.push_back(clause);
100 positive_clauses.push_back(clause.is_positive());
101 negative_clauses.push_back(clause.is_negative());
102 clause_roles.push_back(role);
103}
bool is_positive() const
A clause is positive if it has no negated literals.
Definition Clause.cpp:30
size_t size() const
Straightforward get method.
Definition Clause.hpp:78
bool is_negative() const
A clause is negative if it has only negated literals.
Definition Clause.cpp:42

◆ begin()

vector< Clause >::iterator Matrix::begin ( )
inline

Definition at line 304 of file Matrix.hpp.

304{ return clauses.begin(); }

◆ cbegin()

vector< Clause >::const_iterator Matrix::cbegin ( ) const
inline

Definition at line 302 of file Matrix.hpp.

302{ return clauses.cbegin(); }

◆ cend()

vector< Clause >::const_iterator Matrix::cend ( ) const
inline

Definition at line 303 of file Matrix.hpp.

303{ return clauses.cend(); }

◆ deterministic_reorder()

void Matrix::deterministic_reorder ( size_t  n)

Deterministic reorder of the clauses.

Call the reorder algorithm the specified number of times. This assumes you have stored a copy of the original clauses.

Parameters
nNumber of times to call the reorder algorithm

Only store a copy of the clauses the first time you do this.

Definition at line 105 of file Matrix.cpp.

105 {
109 if (!copy_saved) {
110 copy_saved = true;
112 }
113 /*
114 * Find a suitably reordered set of indices.
115 */
116 vector<uint32_t> new_order;
117 size_t s = clauses.size();
118 for (size_t i = 0; i < s; i++)
119 new_order.push_back(i);
120 new_order = deterministic_reorder_n_times<uint32_t>(new_order, n);
121 /*
122 * Clear and rebuild as necessary.
123 */
124 clauses.clear();
125 positive_clauses.clear();
126 negative_clauses.clear();
127 clause_roles.clear();
128 size_t s2 = index.size();
129 index.clear();
130 index = vector<vector<MatrixPairType> >(s2, vector<MatrixPairType>());
131 for (size_t i = 0; i < s; i++) {
132 add_clause(clauses_copy[new_order[i]], roles_copy[new_order[i]]);
133 }
134}
void make_clauses_copy()
Store a copy of the clauses.
Definition Matrix.hpp:151
void add_clause(Clause &, string="")
Add a Clause to the Matrix and update the index accordingly.
Definition Matrix.cpp:91

◆ end()

vector< Clause >::iterator Matrix::end ( )
inline

Definition at line 305 of file Matrix.hpp.

305{ return clauses.end(); }

◆ find_all_extensions()

void Matrix::find_all_extensions ( Unifier u,
vector< InferenceItem > &  result,
Clause c,
VariableIndex var_index,
TermIndex term_index 
)

Find all possible extensions given a Clause, considering all Literals in the Clause.

See the documentation for find_extensions, which does all the heavy lifting.

Parameters
uA reference to a Unifier function object.
resultA reference to a vector of InferenceItems. Start with this empty.
cThe Clause of interest.
var_indexA reference to the VariableIndex.
term_indexA reference to the TermIndex.

Definition at line 222 of file Matrix.cpp.

224 {
225 if (c.size() == 0) {
226 cerr << "You shouldn't be looking for extensions with an empty clause" << endl;
227 return;
228 }
229 for (size_t index = 0; index < c.size(); index++) {
230 find_extensions(u, result, c[index], index, var_index, term_index);
231 }
232}
void find_extensions(Unifier &, vector< InferenceItem > &, const Literal &, LitNum, VariableIndex &, TermIndex &)
Find all possible extensions, given a Literal.
Definition Matrix.cpp:191

◆ find_extensions()

void Matrix::find_extensions ( Unifier u,
vector< InferenceItem > &  result,
const Literal lit,
LitNum  ind,
VariableIndex var_index,
TermIndex term_index 
)
private

Find all possible extensions, given a Literal.

Essentially this involves finding all the Clauses in the Matrix containing a Literal complementary to the one of interest, and selecting each of those Literals that unifies with the one of interest. You can do this by traversing a single row of the index.

It's up to the caller to decide whether to clear the result.

Parameters
uReference to a Unifier function object.
resultReference to a vector of InferenceItems.
litReference to the Literal of interest.
indthe index (position) of the Literal in its Clause.
var_indexReference to the VariableIndex.
term_indexReference to the TermIndex.

Definition at line 191 of file Matrix.cpp.

193 {
194 Literal neg_lit(lit);
195 neg_lit.invert();
196 size_t i = neg_lit.get_pred_as_index();
197 for (const MatrixPairType& p : index[i]) {
198 ClauseNum c_num = p.first;
199 LitNum l_num = p.second;
200 var_index.add_backtrack_point();
201 Clause new_c = (clauses[c_num]).make_copy_with_new_vars(var_index, term_index);
202 UnificationOutcome outcome = u(neg_lit, new_c[l_num]);
203 if (outcome == UnificationOutcome::Succeed) {
204 result.push_back(InferenceItem(InferenceItemType::Extension, lit, ind,
205 c_num, l_num, u.get_substitution()));
206 }
207 var_index.backtrack();
208 u.backtrack();
209 }
210}
Representation of clauses.
Definition Clause.hpp:52
Basic representation of literals, bundling together (pointers to) a Predicate, a collection of argume...
Definition Literal.hpp:50
void backtrack()
Apply the backtracking process to the substitution that has been constructed.
Definition Unifier.cpp:134
Substitution get_substitution() const
Trivial get methods for the result.
Definition Unifier.hpp:116
void backtrack()
Backtrack to the last marker.
void add_backtrack_point()
Add a backtrack point.
Full representation of inferences, beyond just the name.

◆ find_limited_extensions()

void Matrix::find_limited_extensions ( Unifier u,
vector< InferenceItem > &  result,
Clause c,
VariableIndex var_index,
TermIndex term_index 
)

Find all possible extensions given a Clause, but only consider the first Literal in the Clause.

See the documentation for find_extensions, which does all the heavy lifting.

Parameters
uA reference to a Unifier function object.
resultA reference to a vector of InferenceItems. Start with this empty.
cThe Clause of interest.
var_indexA reference to the VariableIndex.
term_indexA reference to the TermIndex.

Definition at line 212 of file Matrix.cpp.

214 {
215 if (c.size() == 0) {
216 cerr << "You shouldn't be looking for extensions with an empty clause" << endl;
217 return;
218 }
219 find_extensions(u, result, c[0], 0, var_index, term_index);
220}

◆ find_start()

pair< bool, size_t > Matrix::find_start ( ) const

Use a simple heuristic to find a good start clause.

Find a start clause that is positive/negative according to the –negative flag, and if possible also a conjecture clause. Also, indicate if there is no positive/negative clause, which means the problem is trivial.

Definition at line 51 of file Matrix.cpp.

51 {
52 bool found_positive = false;
53 bool found_negative = false;
54 size_t first_positive = 0;
55 size_t first_negative = 0;
56 bool found_candidate = false;
57 size_t candidate = 0;
58 size_t i = 0;
59 for (bool positive : positive_clauses) {
60 if (positive_clauses[i]) {
61 if (!found_positive) {
62 first_positive = i;
63 found_positive = true;
64 }
65 }
66 if (negative_clauses[i]) {
67 if (!found_negative) {
68 first_negative = i;
69 found_negative = true;
70 }
71 }
72 if (is_conjecture(i) && !found_candidate) {
73 if ((params::positive_representation && positive_clauses[i]) ||
74 (!params::positive_representation && negative_clauses[i])) {
75 found_candidate = true;
76 candidate = i;
77 }
78 }
79 i++;
80 }
81 if (!(found_positive && found_negative))
82 return pair<bool,size_t>(false, 0);
83 if (found_candidate)
84 return pair<bool, size_t>(true, candidate);
85 else if (params::positive_representation)
86 return pair<bool, size_t>(true, first_positive);
87 else
88 return pair<bool, size_t>(true, first_negative);
89}
bool is_conjecture(size_t i) const
Is a particular Clause a conjecture?
Definition Matrix.cpp:46

◆ get_num_clauses()

ClauseNum Matrix::get_num_clauses ( ) const
inline

Straightforward get method.

Definition at line 191 of file Matrix.hpp.

191{ return clauses.size(); }

◆ is_conjecture()

bool Matrix::is_conjecture ( size_t  i) const

Is a particular Clause a conjecture?

Parameters
iIndex of Clause to check.

Definition at line 46 of file Matrix.cpp.

46 {
47 return clause_roles[i] == "negated_conjecture" ||
48 clause_roles[i] == "conjecture";
49}

◆ is_negative()

bool Matrix::is_negative ( size_t  i) const
inline

Is a particular Clause negative?.

Parameters
iIndex of Clause to check.

Definition at line 211 of file Matrix.hpp.

211{ return negative_clauses[i]; }

◆ is_positive()

bool Matrix::is_positive ( size_t  i) const
inline

Is a particular Clause positive?.

Parameters
iIndex of Clause to check.

Definition at line 205 of file Matrix.hpp.

205{ return positive_clauses[i]; }

◆ make_clauses_copy()

void Matrix::make_clauses_copy ( )
inlineprivate

Store a copy of the clauses.

Definition at line 151 of file Matrix.hpp.

151 {
152 clauses_copy.clear();
154 roles_copy.clear();
156 }

◆ make_LaTeX()

string Matrix::make_LaTeX ( bool  subbed = false) const

Make a usable LaTeX representation.

Parameters
subbedImplement substitutions if true.

Definition at line 257 of file Matrix.cpp.

257 {
258 string s ("\\[\n\\begin{split}\n");
259 s += "\\textcolor{magenta}{M} = ";
260 for (const Clause& c : clauses) {
261 s += "&\\,";
262 s += c.make_LaTeX(subbed);
263 s += "\\\\";
264 }
265 s += "\n\\end{split}\n\\]\n";
266
267 return s;
268}

◆ move_equals_to_start()

void Matrix::move_equals_to_start ( )

Self-explanatory.

BUT only do this to the matrix built at the start of the process, as it assumes the equality axioms are at the end of the matrix.

Definition at line 167 of file Matrix.cpp.

167 {
168 if (num_equals == 0) {
169 cerr << "Why are you trying to move equality axioms - there aren't any?" << endl;
170 return;
171 }
173 clauses.clear();
174 positive_clauses.clear();
175 negative_clauses.clear();
176 clause_roles.clear();
177 size_t s2 = index.size();
178 index.clear();
179 index = vector<vector<MatrixPairType> >(s2, vector<MatrixPairType>());
180 size_t n_clauses = clauses_copy.size();
181 for (size_t i = n_clauses - num_equals; i < n_clauses; i++) {
183 }
184 for (size_t i = 0; i < n_clauses - num_equals; i++) {
186 }
187 clauses_copy.clear();
188 roles_copy.clear();
189}

◆ operator[]()

const Clause & Matrix::operator[] ( size_t  i) const
inline

Straightforward get method.

Beware - the parameter is not checked, so behave!

Parameters
iIndex of item to get.

Definition at line 199 of file Matrix.hpp.

199{ return clauses[i]; }

◆ random_reorder()

void Matrix::random_reorder ( )

Randomly reorder the matrix.

Only store a copy of the clauses the first time you do this.

Definition at line 136 of file Matrix.cpp.

136 {
140 if (!copy_saved) {
141 copy_saved = true;
143 }
144 /*
145 * Find a suitably reordered set of indices.
146 */
147 vector<uint32_t> new_order;
148 size_t s = clauses.size();
149 for (size_t i = 0; i < s; i++)
150 new_order.push_back(i);
151 std::shuffle(new_order.begin(), new_order.end(), d);
152 /*
153 * Clear and rebuild as necessary.
154 */
155 clauses.clear();
156 positive_clauses.clear();
157 negative_clauses.clear();
158 clause_roles.clear();
159 size_t s2 = index.size();
160 index.clear();
161 index = vector<vector<MatrixPairType> >(s2, vector<MatrixPairType>());
162 for (size_t i = 0; i < s; i++) {
163 add_clause(clauses_copy[new_order[i]], roles_copy[new_order[i]]);
164 }
165}
static std::mt19937 d
Randomness for random reordering.
Definition Matrix.hpp:126

◆ set_num_equals()

void Matrix::set_num_equals ( uint32_t  n)
inline

Straightforward set method.

Parameters
nNumber of equality axioms.

Definition at line 223 of file Matrix.hpp.

223{ num_equals = n; }

◆ set_num_preds()

void Matrix::set_num_preds ( size_t  num_predicates)

Make an empty index.

Definition at line 42 of file Matrix.cpp.

42 {
43 index = vector<vector<MatrixPairType> >((num_predicates * 2), vector<MatrixPairType>());
44}

◆ show_tptp()

void Matrix::show_tptp ( ) const

Output in TPTP compatible format.

Definition at line 287 of file Matrix.cpp.

287 {
288 size_t matrix_i = 0;
289 for (const Clause& c : clauses) {
290 cout << "cnf(matrix-";
291 cout << std::to_string(matrix_i++);
292 cout << ", plain, ";
293 cout << c.to_tptp_string();
294 cout << ").";
295 cout << std::endl;
296 }
297}

◆ to_string()

string Matrix::to_string ( ) const

Make a string representation.

Definition at line 234 of file Matrix.cpp.

234 {
235 string result;
236 colour_string::ColourString cs(params::use_colours);
237 size_t i = 0;
238 for (const Clause& c : clauses) {
239 if (is_conjecture(i))
240 result += cs("*").orange();
241 else
242 result += " ";
243 if (positive_clauses[i])
244 result += cs("+").orange();
245 else if (negative_clauses[i])
246 result += cs("-").orange();
247 else
248 result += " ";
249 result += " ";
250 i++;
251 result += c.to_string();
252 result += "\n";
253 }
254 return result;
255}
Simple addition of colour to strings and ostreams.

◆ write_to_prolog_file()

void Matrix::write_to_prolog_file ( const path &  path_to_file) const

Write to a file that can be read by Prolog.

This is for proof checking.

Parameters
path_to_fileReference to path object.

Definition at line 270 of file Matrix.cpp.

270 {
271 std::ofstream file(path_to_file);
272 size_t matrix_i = 0;
273 // Nasty hack needed to stop the proof checker from printing
274 // a bunch of warnings when reading the matrix.
275 file << ":- style_check(-singleton)." << std::endl;
276 for (const Clause& c : clauses) {
277 file << "matrix(";
278 file << std::to_string(matrix_i++);
279 file << ", ";
280 file << c.to_prolog_string();
281 file << ").";
282 file << std::endl;
283 }
284 file.close();
285}

Friends And Related Symbol Documentation

◆ operator<<

ostream & operator<< ( ostream &  out,
const Matrix m 
)
friend

Definition at line 299 of file Matrix.cpp.

299 {
300 out << "Clauses in matrix:" << endl;
301 out << "------------------" << endl;
302 size_t i = 0;
303 for (const Clause& c : m.clauses)
304 out << setw(params::output_width) << i++
305 << ": " << c << endl;
306 vector<string> index_lits(m.index.size(), string(""));
307 for (Clause c : m.clauses)
308 for (size_t i = 0; i < c.size(); i++)
309 index_lits[c[i].get_pred_as_index()] = c[i].get_small_lit();
310 i = 0;
311 out << endl << "Index: (Clause, Literal):" << endl;
312 out << "-------------------------" << endl;
313 for (vector<MatrixPairType> v : m.index) {
314 out << setw(params::output_width + 20) << index_lits[i++] << ": ";
315 for (MatrixPairType p : v) {
316 out << "(" << p.first << ", " << p.second << ") ";
317 }
318 out << endl;
319 }
320 return out;
321}
Structure containing all the command line and other options.

Member Data Documentation

◆ clause_roles

vector<string> Matrix::clause_roles
private

Keep track of which clauses are conjectures etc.

This allows start clause heuristics to be applied.

Definition at line 95 of file Matrix.hpp.

◆ clauses

vector<Clause> Matrix::clauses
private

The Matrix itself is just a set of Clauses.

Definition at line 77 of file Matrix.hpp.

◆ clauses_copy

vector<Clause> Matrix::clauses_copy
private

It makes sense to keep a copy of the clauses if your schedule reorders multiple times.

Definition at line 105 of file Matrix.hpp.

◆ copy_saved

bool Matrix::copy_saved
private

Remember whether you've saved a copy.

Definition at line 114 of file Matrix.hpp.

◆ d

std::mt19937 Matrix::d
staticprivate

Randomness for random reordering.

Definition at line 126 of file Matrix.hpp.

◆ index

vector<vector<MatrixPairType> > Matrix::index
private

We want to be able to quickly identify where in each clause a particular literal lives.

Store clause and position as pairs in a row corresponding to the literal.

Definition at line 122 of file Matrix.hpp.

◆ negative_clauses

vector<bool> Matrix::negative_clauses
private

Keep track of which clauses are negative.

This allows start clause heuristics to be applied.

Definition at line 89 of file Matrix.hpp.

◆ num_equals

uint32_t Matrix::num_equals
private

You need to know how many equality axioms there are in case you want to move them around.

Definition at line 100 of file Matrix.hpp.

◆ positive_clauses

vector<bool> Matrix::positive_clauses
private

Keep track of which clauses are positive.

This allows start clause heuristics to be applied.

Definition at line 83 of file Matrix.hpp.

◆ roles_copy

vector<string> Matrix::roles_copy
private

It makes sense to keep a copy of the roles if your schedule reorders multiple times.

Definition at line 110 of file Matrix.hpp.


The documentation for this class was generated from the following files: