Connect++ 0.6.0
A fast, readable connection prover for first-order logic.
Loading...
Searching...
No Matches
Matrix.hpp
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#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;
106 uint32_t num_equals;
111 vector<Clause> clauses_copy;
116 vector<string> roles_copy;
128 vector<vector<MatrixPairType>> index;
133 vector<vector<LiteralClausePairType>> literal_clause_index;
137 static std::mt19937 d;
142 clauses_copy.clear();
144 roles_copy.clear();
146 }
153 void rebuild_index(vector<Clause>&, vector<string>&);
161 template<class Comparison>
162 void sort_clauses(Comparison comparison) {
163 /*
164 * Combine the clauses with their roles.
165 */
166 vector<ClauseAndRole> cr;
167 size_t s = clauses.size();
168 for (size_t i = 0; i < s; i++) {
169 cr.push_back(ClauseAndRole(clauses[i], clause_roles[i]));
170 }
171 /*
172 * Sort them according to the relevant ordering.
173 */
174 std::sort(cr.begin(), cr.end(), comparison);
175 /*
176 * Split them again and re-build the index.
177 */
178 clauses.clear();
179 positive_clauses.clear();
180 negative_clauses.clear();
181 clause_roles.clear();
182 size_t s2 = index.size();
183 index.clear();
184 index = vector<vector<MatrixPairType> >(s2, vector<MatrixPairType>());
185 for (size_t i = 0; i < s; i++) {
186 add_clause(cr[i].first, cr[i].second);
187 }
188 }
189public:
195 : clauses()
196 , index()
201 , clause_roles()
202 , clauses_copy()
203 , roles_copy()
204 , copy_saved(false)
205 {}
212 Matrix(size_t);
218 Matrix(const Matrix&) = delete;
219 Matrix(const Matrix&&) = delete;
220 Matrix& operator=(const Matrix&) = delete;
221 Matrix& operator=(const Matrix&&) = delete;
225 ClauseNum get_num_clauses() const { return clauses.size(); }
229 inline size_t get_index_entry_size(size_t _i) const {
230 return index[_i].size();
231 }
235 inline MatrixPairType get_index_entry(size_t _i, size_t _j) const {
236 return (index[_i])[_j];
237 }
245 const Clause& operator[](size_t i) const { return clauses[i]; }
254 inline const Literal& inspect_literal(size_t i, size_t j) {
255 return (clauses[i])[j];
256 }
265 inline const Literal& inspect_index_literal(size_t i, size_t j) {
266 return (literal_clause_index[i])[j].first;
267 }
273 bool is_positive(size_t i) const { return positive_clauses[i]; }
279 bool is_negative(size_t i) const { return negative_clauses[i]; }
285 bool is_ground(size_t i) const { return ground_clauses[i]; }
291 bool is_conjecture(size_t i) const;
297 inline void set_num_equals(uint32_t n) { num_equals = n; }
306 pair<bool,size_t> find_start() const;
310 void set_num_preds(size_t);
319 void add_clause(Clause&, string = "");
328 void deterministic_reorder(size_t);
332 void random_reorder();
346 /*
347 * Iteration on a Matrix is just iteration over the Clauses.
348 */
349 vector<Clause>::const_iterator cbegin() const { return clauses.cbegin(); }
350 vector<Clause>::const_iterator cend() const { return clauses.cend(); }
351 vector<Clause>::iterator begin() { return clauses.begin(); }
352 vector<Clause>::iterator end() { return clauses.end(); }
356 string to_string() const;
362 string make_LaTeX(bool = false) const;
370 void write_to_prolog_file(const path&) const;
374 void show_tptp() const;
386 ClauseLengthCompare comparison;
387 sort_clauses<ClauseLengthCompare>(comparison);
388 }
397 void get_literal_clause_pair(LitNum, size_t, Literal&, Clause&) const;
398
399 friend ostream& operator<<(ostream&, const Matrix&);
400};
401
402
403#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:236
void rebuild_index(vector< Clause > &, vector< string > &)
Rebuild the index after, for example, reordering.
Definition Matrix.cpp:115
void sort_clauses_by_increasing_size()
Self-explanatory.
Definition Matrix.hpp:385
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:133
void move_equals_to_start()
Self-explanatory.
Definition Matrix.cpp:208
const Literal & inspect_index_literal(size_t i, size_t j)
Straightforward get method.
Definition Matrix.hpp:265
ClauseNum get_num_clauses() const
Straightforward get method.
Definition Matrix.hpp:225
string make_LaTeX(bool=false) const
Make a usable LaTeX representation.
Definition Matrix.cpp:259
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:141
Matrix()
Use this constructor if you really want an empty Matrix.
Definition Matrix.hpp:194
void get_literal_clause_pair(LitNum, size_t, Literal &, Clause &) const
Get a literal and clause from the index.
Definition Matrix.cpp:301
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:106
vector< vector< MatrixPairType > > index
We want to be able to quickly identify where in each clause a particular literal lives.
Definition Matrix.hpp:128
void add_clause(Clause &, string="")
Add a Clause to the Matrix and update the index accordingly.
Definition Matrix.cpp:97
bool is_negative(size_t i) const
Is a particular Clause negative?
Definition Matrix.hpp:279
bool copy_saved
Remember whether you've saved a copy.
Definition Matrix.hpp:120
static std::mt19937 d
Randomness for random reordering.
Definition Matrix.hpp:137
bool is_conjecture(size_t i) const
Is a particular Clause a conjecture?
Definition Matrix.cpp:49
vector< string > roles_copy
It makes sense to keep a copy of the roles if your schedule reorders multiple times.
Definition Matrix.hpp:116
void write_to_prolog_file(const path &) const
Write to a file that can be read by Prolog.
Definition Matrix.cpp:272
void random_reorder_literals()
Randomly reorder the literals in each clause in the matrix.
Definition Matrix.cpp:190
void set_num_equals(uint32_t n)
Straightforward set method.
Definition Matrix.hpp:297
const Clause & operator[](size_t i) const
Straightforward get method.
Definition Matrix.hpp:245
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:273
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
void show_tptp() const
Output in TPTP compatible format.
Definition Matrix.cpp:289
const Literal & inspect_literal(size_t i, size_t j)
Straightforward get method.
Definition Matrix.hpp:254
vector< Clause > clauses_copy
It makes sense to keep a copy of the clauses if your schedule reorders multiple times.
Definition Matrix.hpp:111
void sort_clauses(Comparison comparison)
Template method for general sorting of clauses, which sorts roles at the same time.
Definition Matrix.hpp:162
void set_num_preds(size_t)
Make an empty index.
Definition Matrix.cpp:44
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:133
pair< bool, size_t > find_start() const
Use a simple heuristic to find a good start clause.
Definition Matrix.cpp:54
void random_reorder()
Randomly reorder the matrix.
Definition Matrix.cpp:164
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:285
Provide a function object to compare clauses by size.