Connect++ 0.5.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>;
60
73class Matrix {
74private:
78 vector<Clause> clauses;
84 vector<bool> positive_clauses;
90 vector<bool> negative_clauses;
96 vector<string> clause_roles;
101 uint32_t num_equals;
106 vector<Clause> clauses_copy;
111 vector<string> roles_copy;
123 vector<vector<MatrixPairType>> index;
127 static std::mt19937 d;
147 void find_extensions(Unifier&, vector<InferenceItem>&, const Literal&,
148 LitNum, VariableIndex&, TermIndex&);
153 clauses_copy.clear();
155 roles_copy.clear();
157 }
165 template<class Comparison>
166 void sort_clauses(Comparison comparison) {
167 /*
168 * Combine the clauses with their roles.
169 */
170 vector<ClauseAndRole> cr;
171 size_t s = clauses.size();
172 for (size_t i = 0; i < s; i++) {
173 cr.push_back(ClauseAndRole(clauses[i], clause_roles[i]));
174 }
175 /*
176 * Sort them according to the relevant ordering.
177 */
178 std::sort(cr.begin(), cr.end(), comparison);
179 /*
180 * Split them again and re-build the index.
181 */
182 clauses.clear();
183 positive_clauses.clear();
184 negative_clauses.clear();
185 clause_roles.clear();
186 size_t s2 = index.size();
187 index.clear();
188 index = vector<vector<MatrixPairType> >(s2, vector<MatrixPairType>());
189 for (size_t i = 0; i < s; i++) {
190 add_clause(cr[i].first, cr[i].second);
191 }
192 }
193public:
199 : clauses()
200 , index()
203 , clause_roles()
204 , clauses_copy()
205 , roles_copy()
206 , copy_saved(false)
207 {}
214 Matrix(size_t);
220 Matrix(const Matrix&) = delete;
221 Matrix(const Matrix&&) = delete;
222 Matrix& operator=(const Matrix&) = delete;
223 Matrix& operator=(const Matrix&&) = delete;
227 ClauseNum get_num_clauses() const { return clauses.size(); }
235 const Clause& operator[](size_t i) const { return clauses[i]; }
241 bool is_positive(size_t i) const { return positive_clauses[i]; }
247 bool is_negative(size_t i) const { return negative_clauses[i]; }
253 bool is_conjecture(size_t i) const;
259 inline void set_num_equals(uint32_t n) { num_equals = n; }
268 pair<bool,size_t> find_start() const;
272 void set_num_preds(size_t);
281 void add_clause(Clause&, string = "");
296 void find_limited_extensions(Unifier&, vector<InferenceItem>&, Clause&,
312 void find_all_extensions(Unifier&, vector<InferenceItem>&, Clause&,
322 void deterministic_reorder(size_t);
326 void random_reorder();
340 /*
341 * Iteration on a Matrix is just iteration over the Clauses.
342 */
343 vector<Clause>::const_iterator cbegin() const { return clauses.cbegin(); }
344 vector<Clause>::const_iterator cend() const { return clauses.cend(); }
345 vector<Clause>::iterator begin() { return clauses.begin(); }
346 vector<Clause>::iterator end() { return clauses.end(); }
350 string to_string() const;
356 string make_LaTeX(bool = false) const;
364 void write_to_prolog_file(const path&) const;
368 void show_tptp() const;
380 ClauseLengthCompare comparison;
381 sort_clauses<ClauseLengthCompare>(comparison);
382 }
383
384 friend ostream& operator<<(ostream&, const Matrix&);
385};
386
387
388#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:73
string to_string() const
Make a string representation.
Definition Matrix.cpp:247
void sort_clauses_by_increasing_size()
Self-explanatory.
Definition Matrix.hpp:379
vector< Clause > clauses
The Matrix itself is just a set of Clauses.
Definition Matrix.hpp:78
void deterministic_reorder(size_t)
Deterministic reorder of the clauses.
Definition Matrix.cpp:105
void move_equals_to_start()
Self-explanatory.
Definition Matrix.cpp:183
ClauseNum get_num_clauses() const
Straightforward get method.
Definition Matrix.hpp:227
string make_LaTeX(bool=false) const
Make a usable LaTeX representation.
Definition Matrix.cpp:270
vector< string > clause_roles
Keep track of which clauses are conjectures etc.
Definition Matrix.hpp:96
void make_clauses_copy()
Store a copy of the clauses.
Definition Matrix.hpp:152
void find_extensions(Unifier &, vector< InferenceItem > &, const Literal &, LitNum, VariableIndex &, TermIndex &)
Find all possible extensions, given a Literal.
Definition Matrix.cpp:207
Matrix()
Use this constructor if you really want an empty Matrix.
Definition Matrix.hpp:198
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:101
vector< vector< MatrixPairType > > index
We want to be able to quickly identify where in each clause a particular literal lives.
Definition Matrix.hpp:123
void add_clause(Clause &, string="")
Add a Clause to the Matrix and update the index accordingly.
Definition Matrix.cpp:91
bool is_negative(size_t i) const
Is a particular Clause negative?.
Definition Matrix.hpp:247
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.
Definition Matrix.cpp:225
bool copy_saved
Remember whether you've saved a copy.
Definition Matrix.hpp:115
static std::mt19937 d
Randomness for random reordering.
Definition Matrix.hpp:127
void find_all_extensions(Unifier &, vector< InferenceItem > &, Clause &, VariableIndex &, TermIndex &)
Find all possible extensions given a Clause, considering all Literals in the Clause.
Definition Matrix.cpp:235
bool is_conjecture(size_t i) const
Is a particular Clause a conjecture?
Definition Matrix.cpp:46
vector< string > roles_copy
It makes sense to keep a copy of the roles if your schedule reorders multiple times.
Definition Matrix.hpp:111
void write_to_prolog_file(const path &) const
Write to a file that can be read by Prolog.
Definition Matrix.cpp:283
void random_reorder_literals()
Randomly reorder the literals in each clause in the matrix.
Definition Matrix.cpp:162
void set_num_equals(uint32_t n)
Straightforward set method.
Definition Matrix.hpp:259
const Clause & operator[](size_t i) const
Straightforward get method.
Definition Matrix.hpp:235
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:241
void show_tptp() const
Output in TPTP compatible format.
Definition Matrix.cpp:300
vector< Clause > clauses_copy
It makes sense to keep a copy of the clauses if your schedule reorders multiple times.
Definition Matrix.hpp:106
void sort_clauses(Comparison comparison)
Template method for general sorting of clauses, which sorts roles at the same time.
Definition Matrix.hpp:166
void set_num_preds(size_t)
Make an empty index.
Definition Matrix.cpp:42
pair< bool, size_t > find_start() const
Use a simple heuristic to find a good start clause.
Definition Matrix.cpp:51
void random_reorder()
Randomly reorder the matrix.
Definition Matrix.cpp:136
vector< bool > negative_clauses
Keep track of which clauses are negative.
Definition Matrix.hpp:90
vector< bool > positive_clauses
Keep track of which clauses are positive.
Definition Matrix.hpp:84
Look after terms, (ideally) using hash consing to avoid storing copies of terms.
Definition TermIndex.hpp:75
Wrap up various applications of unificiation into a single class: all the unification you need to do ...
Definition Unifier.hpp:83
Storage of named variables, and management of new, anonymous and unique variables.
Provide a function object to compare clauses by size.