Connect++ 0.6.0
A fast, readable connection prover for first-order logic.
Loading...
Searching...
No Matches
FOF.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 FOF_HPP
26#define FOF_HPP
27
28#include<iostream>
29#include<string>
30
31#include "PredicateIndex.hpp"
32#include "FunctionIndex.hpp"
33#include "Literal.hpp"
34#include "Clause.hpp"
35
36using std::string;
37using std::cout;
38using std::endl;
39
43enum class FOFType {Empty, Atom, Neg, And, Or, Imp, Iff, A, E};
44
57class FOF {
58private:
62 FOFType type;
71 vector<FOF> sub_formulas;
96 //---------------------------------------------------------------------------
97 // Simple methods for Variables.
98 //---------------------------------------------------------------------------
119 //---------------------------------------------------------------------------
120 // Skolemization.
121 //---------------------------------------------------------------------------
130 Term* make_skolem_function(const vector<Term*>&);
155 void skolemize_main(vector<Term*>);
161 void skolemize_universals_main(vector<Term*>);
162 //---------------------------------------------------------------------------
163 // Miniscoping.
164 //---------------------------------------------------------------------------
177 void miniscope_split(Variable*, vector<FOF>&, vector<FOF>&);
181 void miniscope_all();
182 //---------------------------------------------------------------------------
183 // Definitional clause translation.
184 //---------------------------------------------------------------------------
190 //---------------------------------------------------------------------------
191 // Distribution of Or/And.
192 //---------------------------------------------------------------------------
197 bool distribute_or_once();
202 bool distribute_and_once();
203public:
204 //---------------------------------------------------------------------------
205 // Constructors.
206 //---------------------------------------------------------------------------
210 FOF() = delete;
214 FOF(FOFType t)
215 : type(t), pred(), sub_formulas(), var(nullptr) {}
221 FOF(const Literal&);
229 FOF(FOFType, const vector<FOF>&, Variable*);
230 //---------------------------------------------------------------------------
231 // Straightforward methods for getting, setting etc.
232 //---------------------------------------------------------------------------
236 static void set_indexes(std::tuple<VariableIndex*,
239 TermIndex*> is) {
240 var_index = std::get<0>(is);
241 fun_index = std::get<1>(is);
242 pred_index = std::get<2>(is);
243 term_index = std::get<3>(is);
244 }
248 void show_indexes() const {
249 cout << var_index << " " << fun_index
250 << " " << pred_index
251 << " " << term_index
252 << endl;
253 }
257 FOFType fof_type() { return type; }
261 void add_formula(const FOF& f) { sub_formulas.push_back(f); }
265 void clear() {
266 type = FOFType::Empty;
267 pred.clear();
268 sub_formulas.clear();
269 var = nullptr;
270 }
271 //---------------------------------------------------------------------------
272 // Straightforward methods for making FOFs.
273 //---------------------------------------------------------------------------
277 static FOF make_literal(const Literal& lit) {
278 FOF result(lit);
279 return result;
280 }
286 static FOF make_neg(const FOF& f) {
287 vector<FOF> fs;
288 fs.push_back(f);
289 FOF result(FOFType::Neg, fs, nullptr);
290 return result;
291 }
295 static FOF make_and(const vector<FOF>& fs) {
296 FOF result(FOFType::And, fs, nullptr);
297 return result;
298 }
302 static FOF make_or(const vector<FOF>& fs) {
303 FOF result(FOFType::Or, fs, nullptr);
304 return result;
305 }
309 static FOF make_imp(const FOF& lhs, const FOF& rhs) {
310 vector<FOF> fs;
311 fs.push_back(lhs);
312 fs.push_back(rhs);
313 FOF result(FOFType::Imp, fs, nullptr);
314 return result;
315 }
319 static FOF make_iff(const FOF& lhs, const FOF& rhs) {
320 vector<FOF> fs;
321 fs.push_back(lhs);
322 fs.push_back(rhs);
323 FOF result(FOFType::Iff, fs, nullptr);
324 return result;
325 }
329 static FOF make_forall(const FOF& f, Variable* v) {
330 vector<FOF> fs;
331 fs.push_back(f);
332 FOF result(FOFType::A, fs, v);
333 return result;
334 }
338 static FOF make_exists(const FOF& f, Variable* v) {
339 vector<FOF> fs;
340 fs.push_back(f);
341 FOF result(FOFType::E, fs, v);
342 return result;
343 }
344 //---------------------------------------------------------------------------
345 // Basic tests.
346 //---------------------------------------------------------------------------
352 bool is_literal() const;
361 bool is_clause() const;
362 //---------------------------------------------------------------------------
363 // Standard simplifications.
364 //---------------------------------------------------------------------------
369 void remove_negation();
374 void negate();
378 void simple_negate();
382 void invert_literal();
386 void remove_iff();
390 void remove_imp();
410 void push_negs();
416 void convert_to_nnf();
423 void miniscope();
435 void convert_to_cnf_clauses(vector<Clause>&);
442 void split_sub_formulas(vector<FOF>&, vector<FOF>&);
452 void find_definitional_tuple(FOF&, vector<FOF>&, bool);
467 void definitional_convert_to_cnf_clauses(vector<Clause>&);
475 void skolemize();
501 void to_Literal(Literal&) const;
506 void convert_to_clauses(vector<Clause>&) const;
511 void dnf_invert_and_convert_to_clauses(vector<Clause>&) const;
519 size_t find_and() const;
527 size_t find_or() const;
535 void distribute_or();
543 void distribute_and();
547 string to_string() const;
552 static SimplificationResult simplify_cnf(vector<Clause>&);
557 static SimplificationResult simplify_cnf(vector<Clause>&, vector<string>&);
565 set<Term*> find_free_variables() const;
566
567 friend ostream& operator<<(ostream&, const FOF&);
568};
569
570
571#endif
Representation of first order formulas.
Definition FOF.hpp:57
void skolemize()
Skolemize the given formula.
Definition FOF.cpp:986
void remove_iff()
Replace <-> throughout using ->.
Definition FOF.cpp:441
static FOF make_or(const vector< FOF > &fs)
Directly make a disjunction.
Definition FOF.hpp:302
void miniscope_all()
Apply miniscoping to all subformulas.
Definition FOF.cpp:306
size_t find_or() const
Does the collection of subformulas contain an OR? If so, find it.
Definition FOF.cpp:1142
static VariableIndex * var_index
Access to the index: static because all FOFs should share it.
Definition FOF.hpp:80
void convert_to_nnf()
Convert to Negation Normal Form.
Definition FOF.cpp:605
void clear()
Make an FOFType::Empty.
Definition FOF.hpp:265
void add_formula(const FOF &f)
Add a subformula.
Definition FOF.hpp:261
void dnf_invert_and_convert_to_clauses(vector< Clause > &) const
Assuming you have a DNF, invert it and convert it to a collection of CNF clauses.
Definition FOF.cpp:1095
set< Term * > find_free_variables() const
Find the free variables in the formula.
Definition FOF.cpp:1289
void convert_to_cnf_clauses(vector< Clause > &)
Convert to Conjunctive Normal Form.
Definition FOF.cpp:784
FOFType fof_type()
Basic get method.
Definition FOF.hpp:257
void show_indexes() const
Show the indices.
Definition FOF.hpp:248
void remove_redundant_quantifiers()
Remove quantifiers if they don't bind anything in their scope.
Definition FOF.cpp:511
void miniscope_split(Variable *, vector< FOF > &, vector< FOF > &)
Helper for miniscope.
Definition FOF.cpp:290
void split_sub_formulas(vector< FOF > &, vector< FOF > &)
Split subformulas in half.
Definition FOF.cpp:845
void make_unique_bound_variables()
Replace all bound variables with unique new ones.
Definition FOF.cpp:486
static SimplificationResult simplify_cnf(vector< Clause > &)
Simplify the clauses in a CNF using the method in Clause.
Definition FOF.cpp:1268
void skolemize_main(vector< Term * >)
Main helper function called by FOF::skolemize().
Definition FOF.cpp:224
void remove_universal_quantifiers()
Self-explanatory.
Definition FOF.cpp:996
Variable * var
Associate a Variable with a quantifier.
Definition FOF.hpp:75
bool distribute_or_once()
Find an OR that can be distributed and deal with it. Indicate if there isn't one.
Definition FOF.cpp:96
FOFType type
Used for all FOFs to denote what kind it it.
Definition FOF.hpp:62
static FOF make_and(const vector< FOF > &fs)
Directly make a conjunction.
Definition FOF.hpp:295
void miniscope()
Apply the rules for miniscoping.
Definition FOF.cpp:615
static FOF make_iff(const FOF &lhs, const FOF &rhs)
Directly make an iff.
Definition FOF.hpp:319
void push_negs()
Push all negations in.
Definition FOF.cpp:540
static FOF make_literal(const Literal &lit)
Directly make a Literal.
Definition FOF.hpp:277
static void set_indexes(std::tuple< VariableIndex *, FunctionIndex *, PredicateIndex *, TermIndex * > is)
Set up pointer to the variable index etc.
Definition FOF.hpp:236
bool distribute_and_once()
Find an AND that can be distributed and deal with it. Indicate if there isn't one.
Definition FOF.cpp:149
bool has_free_variable(Variable *)
Does this formula contain the specified variable unbound?
Definition FOF.cpp:35
static FOF make_neg(const FOF &f)
Directly negate a FOF.
Definition FOF.hpp:286
void definitional_convert_to_cnf_clauses(vector< Clause > &)
Convert to Conjunctive Normal Form using a form of definitional conversion.
Definition FOF.cpp:935
void skolemize_universals()
Skolemize the universal quantifiers in the given formula.
Definition FOF.cpp:991
void replace_variable(Variable *, Variable *)
Self-explanatory.
Definition FOF.cpp:72
vector< FOF > sub_formulas
Most FOFs will have subformulas.
Definition FOF.hpp:71
void to_Literal(Literal &) const
Assuming the FOF is a literal, convert it to something of type Literal.
Definition FOF.cpp:1052
FOF make_definitional_predicate() const
When doing definitional clause conversion you need to be able to make unique new predicates....
Definition FOF.cpp:86
static FOF make_exists(const FOF &f, Variable *v)
Directly make an existentially quantified FOF.
Definition FOF.hpp:338
static TermIndex * term_index
Access to the index: static because all FOFs should share it.
Definition FOF.hpp:90
void remove_negation()
Remove the leading negation from an arbitrary FOF, if it has one.
Definition FOF.cpp:391
static PredicateIndex * pred_index
Access to the index: static because all FOFs should share it.
Definition FOF.hpp:95
static FOF make_forall(const FOF &f, Variable *v)
Directly make a universally quantified FOF.
Definition FOF.hpp:329
void convert_to_clauses(vector< Clause > &) const
Assuming you have a CNF, convert it to a collection of Clauses.
Definition FOF.cpp:1062
void skolemize_universals_main(vector< Term * >)
The dual of skolemize_main.
Definition FOF.cpp:256
void remove_imp()
Replace A->B throughout with neg A v B.
Definition FOF.cpp:468
size_t find_and() const
Does the collection of subformulas contain an AND? If so, find it.
Definition FOF.cpp:1132
void find_definitional_tuple(FOF &, vector< FOF > &, bool)
Heavy-lifting for conversion to definitional clause form.
Definition FOF.cpp:869
static FunctionIndex * fun_index
Access to the index: static because all FOFs should share it.
Definition FOF.hpp:85
FOF(FOFType t)
You probably don't want this constructor.
Definition FOF.hpp:214
FOF()=delete
You don't want this constructor.
void distribute_and()
Distribute ANDs to the maximum extent possible.
Definition FOF.cpp:1156
Term * make_skolem_function(const vector< Term * > &)
Make a new Skolem function.
Definition FOF.cpp:204
static FOF make_imp(const FOF &lhs, const FOF &rhs)
Directly make an implication.
Definition FOF.hpp:309
void distribute_or()
Distribute ORs to the maximum extent possible.
Definition FOF.cpp:1152
void simple_negate()
Negate an FOF without applying any simplifications.
Definition FOF.cpp:420
bool is_literal() const
Check if an FOF is a literal.
Definition FOF.cpp:369
string to_string() const
Convert a FOF into a nice-looking string.
Definition FOF.cpp:1160
void negate()
Negate an FOF, applying obvious simplifications if it's already negated or an implication.
Definition FOF.cpp:398
void remove_existential_quantifiers()
Self-explanatory.
Definition FOF.cpp:1024
void invert_literal()
Self-explanatory!
Definition FOF.cpp:429
bool is_clause() const
Check if something is a clause.
Definition FOF.cpp:374
Literal pred
FOFType::Atom can store one of these to represent a Literal.
Definition FOF.hpp:67
void replace_variable_with_term(Term *, Variable *)
Replace a Variable with a Term.
Definition FOF.cpp:210
Mechanism for looking after functions.
Basic representation of literals, bundling together (pointers to) a Predicate, a collection of argume...
Definition Literal.hpp:50
void clear()
Basic manipulation - entirely self-explanatory.
Definition Literal.hpp:119
Management of Predicate objects.
General representation of terms.
Definition Term.hpp:62
Look after terms, using hash consing to avoid storing copies of terms.
Definition TermIndex.hpp:54
Basic representation of variables.
Definition Variable.hpp:58
Storage of named variables, and management of new, anonymous and unique variables.