Connect++ 0.7.0
A fast, readable connection prover for first-order logic.
Loading...
Searching...
No Matches
Matrix.hpp
1/*
2
3Copyright © 2023-26 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#ifndef MATRIX_HPP
26#define MATRIX_HPP
27
28#include<iostream>
29#include<fstream>
30#include<filesystem>
31#include<iomanip>
32#include<string>
33#include<vector>
34#include<tuple>
35#include<algorithm>
36#include<random>
37
38#include "InferenceItem.hpp"
39#include "Clause.hpp"
40#include "ClauseComparisons.hpp"
41#include "Unifier.hpp"
42#include "Reorder.hpp"
43
44using std::pair;
45using std::tuple;
46using std::vector;
47using std::ostream;
48using std::endl;
49using std::setw;
50using std::cout;
51using std::filesystem::path;
52
59using MatrixPairType = pair<ClauseNum, LitNum>;
65using LiteralClausePairType = pair<Literal,Clause>;
74class Matrix {
75private:
79 vector<Clause> clauses;
85 vector<bool> positive_clauses;
91 vector<bool> negative_clauses;
95 vector<bool> ground_clauses;
101 vector<string> clause_roles;
108 vector<string> clause_names;
113 uint32_t num_equals;
118 vector<Clause> clauses_copy;
123 vector<string> roles_copy;
128 vector<string> names_copy;
140 vector<vector<MatrixPairType>> index;
145 vector<vector<LiteralClausePairType>> literal_clause_index;
149 static std::mt19937 d;
154 clauses_copy.clear();
156 roles_copy.clear();
158 names_copy.clear();
160 }
168 void rebuild_index(vector<Clause>&, vector<string>&, vector<string>&);
176 template<class Comparison>
177 void sort_clauses(Comparison comparison) {
178 /*
179 * Combine the clauses with their roles.
180 */
181 vector<ClauseRoleName> cr;
182 size_t s = clauses.size();
183 for (size_t i = 0; i < s; i++) {
184 cr.push_back(ClauseRoleName(clauses[i], pair<string, string>(clause_roles[i], clause_names[i])));
185 }
186 /*
187 * Sort them according to the relevant ordering.
188 */
189 std::sort(cr.begin(), cr.end(), comparison);
190 /*
191 * Split them again and re-build the index.
192 */
193 clauses.clear();
194 positive_clauses.clear();
195 negative_clauses.clear();
196 clause_roles.clear();
197 clause_names.clear();
198 size_t s2 = index.size();
199 index.clear();
200 index = vector<vector<MatrixPairType> >(s2, vector<MatrixPairType>());
201 for (size_t i = 0; i < s; i++) {
202 add_clause(std::get<0>(cr[i]), std::get<1>(cr[i]).first, std::get<1>(cr[i]).second);
203 }
204 }
205public:
211 : clauses()
212 , index()
217 , clause_roles()
218 , clause_names()
219 , num_equals(0)
220 , clauses_copy()
221 , roles_copy()
222 , names_copy()
223 , copy_saved(false)
224 {}
231 Matrix(size_t);
237 Matrix(const Matrix&) = delete;
238 Matrix(const Matrix&&) = delete;
239 Matrix& operator=(const Matrix&) = delete;
240 Matrix& operator=(const Matrix&&) = delete;
244 ClauseNum get_num_clauses() const { return clauses.size(); }
248 inline size_t get_index_entry_size(size_t _i) const {
249 return index[_i].size();
250 }
254 inline MatrixPairType get_index_entry(size_t _i, size_t _j) const {
255 return (index[_i])[_j];
256 }
264 const Clause& operator[](size_t i) const { return clauses[i]; }
273 inline const Literal& inspect_literal(size_t i, size_t j) {
274 return (clauses[i])[j];
275 }
284 inline const Literal& inspect_index_literal(size_t i, size_t j) {
285 return (literal_clause_index[i])[j].first;
286 }
292 bool is_positive(size_t i) const { return positive_clauses[i]; }
298 bool is_negative(size_t i) const { return negative_clauses[i]; }
304 bool is_ground(size_t i) const { return ground_clauses[i]; }
310 bool is_conjecture(size_t i) const;
316 inline void set_num_equals(uint32_t n) { num_equals = n; }
325 pair<bool,size_t> find_start() const;
329 void set_num_preds(size_t);
333 string get_name(size_t _i) const {
334 return clause_names[_i];
335 }
345 void add_clause(Clause&, string = "", string="");
354 void deterministic_reorder(size_t);
358 void random_reorder();
372 /*
373 * Iteration on a Matrix is just iteration over the Clauses.
374 */
375 vector<Clause>::const_iterator cbegin() const { return clauses.cbegin(); }
376 vector<Clause>::const_iterator cend() const { return clauses.cend(); }
377 vector<Clause>::iterator begin() { return clauses.begin(); }
378 vector<Clause>::iterator end() { return clauses.end(); }
382 string to_string() const;
388 string make_LaTeX(bool = false) const;
396 void write_to_prolog_file(const path&) const;
400 void show_tptp() const;
423 void get_literal_clause_pair(LitNum, size_t, Literal&, Clause&) const;
433
434 friend ostream& operator<<(ostream&, const Matrix&);
435};
436
437
438#endif
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
Representation of the Matrix within a proof.
Definition Matrix.hpp:74
string to_string() const
Make a string representation.
Definition Matrix.cpp:252
void sort_clauses_by_increasing_size()
Self-explanatory.
Definition Matrix.hpp:411
vector< Clause > clauses
The Matrix itself is just a set of Clauses.
Definition Matrix.hpp:79
void deterministic_reorder(size_t)
Deterministic reorder of the clauses.
Definition Matrix.cpp:139
void move_equals_to_start()
Self-explanatory.
Definition Matrix.cpp:222
const Literal & inspect_index_literal(size_t i, size_t j)
Straightforward get method.
Definition Matrix.hpp:284
ClauseNum get_num_clauses() const
Straightforward get method.
Definition Matrix.hpp:244
vector< string > clause_names
Keep track of the names of the clauses.
Definition Matrix.hpp:108
string make_LaTeX(bool=false) const
Make a usable LaTeX representation.
Definition Matrix.cpp:277
vector< string > clause_roles
Keep track of which clauses are conjectures etc.
Definition Matrix.hpp:101
void make_clauses_copy()
Store a copy of the clauses.
Definition Matrix.hpp:153
Matrix()
Use this constructor if you really want an empty Matrix.
Definition Matrix.hpp:210
void get_literal_clause_pair(LitNum, size_t, Literal &, Clause &) const
Get a literal and clause from the index.
Definition Matrix.cpp:321
string get_name(size_t _i) const
Retrieve the name of one of the clauses in the matrix.
Definition Matrix.hpp:333
void add_clause(Clause &, string="", string="")
Add a Clause to the Matrix and update the index accordingly.
Definition Matrix.cpp:99
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:113
vector< vector< MatrixPairType > > index
We want to be able to quickly identify where in each clause a particular literal lives.
Definition Matrix.hpp:140
bool is_negative(size_t i) const
Is a particular Clause negative?
Definition Matrix.hpp:298
bool copy_saved
Remember whether you've saved a copy.
Definition Matrix.hpp:132
static std::mt19937 d
Randomness for random reordering.
Definition Matrix.hpp:149
vector< string > names_copy
It makes sense to keep a copy of the names if your schedule reorders multiple times.
Definition Matrix.hpp:128
bool is_conjecture(size_t i) const
Is a particular Clause a conjecture?
Definition Matrix.cpp:51
void make_copy_with_new_unique_vars(size_t, Clause &, VariableIndex &, TermIndex &) const
Make a new copy of a clause in the matrix.
Definition Matrix.cpp:327
vector< string > roles_copy
It makes sense to keep a copy of the roles if your schedule reorders multiple times.
Definition Matrix.hpp:123
void write_to_prolog_file(const path &) const
Write to a file that can be read by Prolog.
Definition Matrix.cpp:292
void random_reorder_literals()
Randomly reorder the literals in each clause in the matrix.
Definition Matrix.cpp:203
void set_num_equals(uint32_t n)
Straightforward set method.
Definition Matrix.hpp:316
const Clause & operator[](size_t i) const
Straightforward get method.
Definition Matrix.hpp:264
Matrix(const Matrix &)=delete
You're never going to need to copy a Matrix.
bool is_positive(size_t i) const
Is a particular Clause positive?
Definition Matrix.hpp:292
MatrixPairType get_index_entry(size_t _i, size_t _j) const
The ExtensionEnumerator needs to know this.
Definition Matrix.hpp:254
size_t get_index_entry_size(size_t _i) const
The ExtensionEnumerator needs to know this.
Definition Matrix.hpp:248
void rebuild_index(vector< Clause > &, vector< string > &, vector< string > &)
Rebuild the index after, for example, reordering.
Definition Matrix.cpp:118
void show_tptp() const
Output in TPTP compatible format.
Definition Matrix.cpp:309
const Literal & inspect_literal(size_t i, size_t j)
Straightforward get method.
Definition Matrix.hpp:273
vector< Clause > clauses_copy
It makes sense to keep a copy of the clauses if your schedule reorders multiple times.
Definition Matrix.hpp:118
void sort_clauses(Comparison comparison)
Template method for general sorting of clauses, which sorts roles and names at the same time.
Definition Matrix.hpp:177
void set_num_preds(size_t)
Make an empty index.
Definition Matrix.cpp:46
vector< vector< LiteralClausePairType > > literal_clause_index
This corresponds closely to index, but has the actual Literal, and the Clause after the Literal's rem...
Definition Matrix.hpp:145
pair< bool, size_t > find_start() const
Use a simple heuristic to find a good start clause.
Definition Matrix.cpp:56
void random_reorder()
Randomly reorder the matrix.
Definition Matrix.cpp:173
vector< bool > negative_clauses
Keep track of which clauses are negative.
Definition Matrix.hpp:91
vector< bool > ground_clauses
We need to know this to control backtracking.
Definition Matrix.hpp:95
vector< bool > positive_clauses
Keep track of which clauses are positive.
Definition Matrix.hpp:85
bool is_ground(size_t i) const
Is a particular Clause ground?
Definition Matrix.hpp:304
Look after terms, using hash consing to avoid storing copies of terms.
Definition TermIndex.hpp:54
Storage of named variables, and management of new, anonymous and unique variables.
Provide a function object to compare clauses by size.