Connect++ 0.7.0
A fast, readable connection prover for first-order logic.
Loading...
Searching...
No Matches
FOF.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 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#include "TPTPRecords.hpp"
36
37using std::string;
38using std::cout;
39using std::endl;
40
44enum class FOFType {Empty, Atom, Neg, And, Or, Imp, Iff, A, E};
45
58class FOF {
59private:
67 FOFType type;
76 vector<FOF> sub_formulas;
101 //---------------------------------------------------------------------------
102 // Simple methods for Variables.
103 //---------------------------------------------------------------------------
124 //---------------------------------------------------------------------------
125 // Skolemization.
126 //---------------------------------------------------------------------------
135 Term* make_skolem_function(const vector<Term*>&);
160 void skolemize_main(vector<Term*>, FOF*, bool=false);
166 void skolemize_universals_main(vector<Term*>, FOF*, bool=false);
167 //---------------------------------------------------------------------------
168 // Miniscoping.
169 //---------------------------------------------------------------------------
182 void miniscope_split(Variable*, vector<FOF>&, vector<FOF>&);
186 void miniscope_all();
187 //---------------------------------------------------------------------------
188 // Definitional clause translation.
189 //---------------------------------------------------------------------------
195 //---------------------------------------------------------------------------
196 // Distribution of Or/And.
197 //---------------------------------------------------------------------------
202 bool distribute_or_once();
207 bool distribute_and_once();
208public:
209 //---------------------------------------------------------------------------
210 // Constructors.
211 //---------------------------------------------------------------------------
215 FOF() = delete;
219 FOF(FOFType t)
220 : type(t), pred(), sub_formulas(), var(nullptr) {}
226 FOF(const Literal&);
234 FOF(FOFType, const vector<FOF>&, Variable*);
235 //---------------------------------------------------------------------------
236 // Straightforward methods for getting, setting etc.
237 //---------------------------------------------------------------------------
241 static void set_indexes(std::tuple<VariableIndex*,
244 TermIndex*> is) {
245 var_index = std::get<0>(is);
246 fun_index = std::get<1>(is);
247 pred_index = std::get<2>(is);
248 term_index = std::get<3>(is);
249 }
253 static void set_recs_p(TPTPRecords* _p) {
254 recs_p = _p;
255 }
259 void show_indexes() const {
260 cout << var_index << " " << fun_index
261 << " " << pred_index
262 << " " << term_index
263 << endl;
264 }
268 FOFType fof_type() { return type; }
272 void add_formula(const FOF& f) { sub_formulas.push_back(f); }
276 void clear() {
277 type = FOFType::Empty;
278 pred.clear();
279 sub_formulas.clear();
280 var = nullptr;
281 }
282 //---------------------------------------------------------------------------
283 // Straightforward methods for making FOFs.
284 //---------------------------------------------------------------------------
288 static FOF make_literal(const Literal& lit) {
289 FOF result(lit);
290 return result;
291 }
297 static FOF make_neg(const FOF& f) {
298 vector<FOF> fs;
299 fs.push_back(f);
300 FOF result(FOFType::Neg, fs, nullptr);
301 return result;
302 }
306 static FOF make_and(const vector<FOF>& fs) {
307 FOF result(FOFType::And, fs, nullptr);
308 return result;
309 }
313 static FOF make_or(const vector<FOF>& fs) {
314 FOF result(FOFType::Or, fs, nullptr);
315 return result;
316 }
320 static FOF make_imp(const FOF& lhs, const FOF& rhs) {
321 vector<FOF> fs;
322 fs.push_back(lhs);
323 fs.push_back(rhs);
324 FOF result(FOFType::Imp, fs, nullptr);
325 return result;
326 }
330 static FOF make_iff(const FOF& lhs, const FOF& rhs) {
331 vector<FOF> fs;
332 fs.push_back(lhs);
333 fs.push_back(rhs);
334 FOF result(FOFType::Iff, fs, nullptr);
335 return result;
336 }
340 static FOF make_forall(const FOF& f, Variable* v) {
341 vector<FOF> fs;
342 fs.push_back(f);
343 FOF result(FOFType::A, fs, v);
344 return result;
345 }
349 static FOF make_exists(const FOF& f, Variable* v) {
350 vector<FOF> fs;
351 fs.push_back(f);
352 FOF result(FOFType::E, fs, v);
353 return result;
354 }
355 //---------------------------------------------------------------------------
356 // Basic tests.
357 //---------------------------------------------------------------------------
363 bool is_literal() const;
372 bool is_clause() const;
373 //---------------------------------------------------------------------------
374 // Standard simplifications.
375 //---------------------------------------------------------------------------
380 void remove_negation();
385 void negate();
389 void simple_negate();
393 void invert_literal();
397 void remove_iff();
401 void remove_imp();
421 void push_negs();
427 void convert_to_nnf();
434 void miniscope();
446 void convert_to_cnf_clauses(vector<Clause>&, vector<string>&, const string&);
453 void split_sub_formulas(vector<FOF>&, vector<FOF>&);
464 void find_definitional_tuple(FOF&, vector<FOF>&, vector<FOF>&, bool);
479 void definitional_convert_to_cnf_clauses(vector<Clause>&, vector<string>&, const string&);
487 void skolemize();
513 void to_Literal(Literal&) const;
518 void convert_to_clauses(vector<Clause>&) const;
523 void dnf_invert_and_convert_to_clauses(vector<Clause>&) const;
531 size_t find_and() const;
539 size_t find_or() const;
547 void distribute_or();
555 void distribute_and();
559 string to_string() const;
567 string to_tptp_string(bool=false) const;
572 string predicate_to_tptp_string() const;
577 static SimplificationResult simplify_cnf(vector<Clause>&);
582 static SimplificationResult simplify_cnf(vector<Clause>&,
583 vector<string>&,
584 vector<string>&);
592 set<Term*> find_free_variables() const;
593
594 friend ostream& operator<<(ostream&, const FOF&);
595};
596
597
598#endif
Representation of first order formulas.
Definition FOF.hpp:58
void skolemize()
Skolemize the given formula.
Definition FOF.cpp:1093
void remove_iff()
Replace <-> throughout using ->.
Definition FOF.cpp:474
static FOF make_or(const vector< FOF > &fs)
Directly make a disjunction.
Definition FOF.hpp:313
void miniscope_all()
Apply miniscoping to all subformulas.
Definition FOF.cpp:339
size_t find_or() const
Does the collection of subformulas contain an OR? If so, find it.
Definition FOF.cpp:1249
static VariableIndex * var_index
Access to the index: static because all FOFs should share it.
Definition FOF.hpp:85
void convert_to_nnf()
Convert to Negation Normal Form.
Definition FOF.cpp:638
void clear()
Make an FOFType::Empty.
Definition FOF.hpp:276
string to_tptp_string(bool=false) const
Convert a FOF into a nice-looking string in the TPTP format.
Definition FOF.cpp:1345
void add_formula(const FOF &f)
Add a subformula.
Definition FOF.hpp:272
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:1202
set< Term * > find_free_variables() const
Find the free variables in the formula.
Definition FOF.cpp:1511
FOFType fof_type()
Basic get method.
Definition FOF.hpp:268
void show_indexes() const
Show the indices.
Definition FOF.hpp:259
void remove_redundant_quantifiers()
Remove quantifiers if they don't bind anything in their scope.
Definition FOF.cpp:544
void miniscope_split(Variable *, vector< FOF > &, vector< FOF > &)
Helper for miniscope.
Definition FOF.cpp:323
void split_sub_formulas(vector< FOF > &, vector< FOF > &)
Split subformulas in half.
Definition FOF.cpp:892
void make_unique_bound_variables()
Replace all bound variables with unique new ones.
Definition FOF.cpp:519
static SimplificationResult simplify_cnf(vector< Clause > &)
Simplify the clauses in a CNF using the method in Clause.
Definition FOF.cpp:1490
void remove_universal_quantifiers()
Self-explanatory.
Definition FOF.cpp:1103
void skolemize_main(vector< Term * >, FOF *, bool=false)
Main helper function called by FOF::skolemize().
Definition FOF.cpp:225
Variable * var
Associate a Variable with a quantifier.
Definition FOF.hpp:80
bool distribute_or_once()
Find an OR that can be distributed and deal with it. Indicate if there isn't one.
Definition FOF.cpp:97
FOFType type
Used for all FOFs to denote what kind it it.
Definition FOF.hpp:67
static FOF make_and(const vector< FOF > &fs)
Directly make a conjunction.
Definition FOF.hpp:306
void miniscope()
Apply the rules for miniscoping.
Definition FOF.cpp:648
static FOF make_iff(const FOF &lhs, const FOF &rhs)
Directly make an iff.
Definition FOF.hpp:330
void push_negs()
Push all negations in.
Definition FOF.cpp:573
static FOF make_literal(const Literal &lit)
Directly make a Literal.
Definition FOF.hpp:288
static void set_indexes(std::tuple< VariableIndex *, FunctionIndex *, PredicateIndex *, TermIndex * > is)
Set up pointer to the variable index etc.
Definition FOF.hpp:241
bool distribute_and_once()
Find an AND that can be distributed and deal with it. Indicate if there isn't one.
Definition FOF.cpp:150
bool has_free_variable(Variable *)
Does this formula contain the specified variable unbound?
Definition FOF.cpp:36
static FOF make_neg(const FOF &f)
Directly negate a FOF.
Definition FOF.hpp:297
void skolemize_universals()
Skolemize the universal quantifiers in the given formula.
Definition FOF.cpp:1098
void replace_variable(Variable *, Variable *)
Self-explanatory.
Definition FOF.cpp:73
vector< FOF > sub_formulas
Most FOFs will have subformulas.
Definition FOF.hpp:76
void to_Literal(Literal &) const
Assuming the FOF is a literal, convert it to something of type Literal.
Definition FOF.cpp:1159
FOF make_definitional_predicate() const
When doing definitional clause conversion you need to be able to make unique new predicates....
Definition FOF.cpp:87
static FOF make_exists(const FOF &f, Variable *v)
Directly make an existentially quantified FOF.
Definition FOF.hpp:349
void definitional_convert_to_cnf_clauses(vector< Clause > &, vector< string > &, const string &)
Convert to Conjunctive Normal Form using a form of definitional conversion.
Definition FOF.cpp:984
static TermIndex * term_index
Access to the index: static because all FOFs should share it.
Definition FOF.hpp:95
void remove_negation()
Remove the leading negation from an arbitrary FOF, if it has one.
Definition FOF.cpp:424
static PredicateIndex * pred_index
Access to the index: static because all FOFs should share it.
Definition FOF.hpp:100
static FOF make_forall(const FOF &f, Variable *v)
Directly make a universally quantified FOF.
Definition FOF.hpp:340
void convert_to_clauses(vector< Clause > &) const
Assuming you have a CNF, convert it to a collection of Clauses.
Definition FOF.cpp:1169
static void set_recs_p(TPTPRecords *_p)
Set up the pointer allowing TPTP outputs to be added.
Definition FOF.hpp:253
void remove_imp()
Replace A->B throughout with neg A v B.
Definition FOF.cpp:501
size_t find_and() const
Does the collection of subformulas contain an AND? If so, find it.
Definition FOF.cpp:1239
static FunctionIndex * fun_index
Access to the index: static because all FOFs should share it.
Definition FOF.hpp:90
FOF(FOFType t)
You probably don't want this constructor.
Definition FOF.hpp:219
FOF()=delete
You don't want this constructor.
void distribute_and()
Distribute ANDs to the maximum extent possible.
Definition FOF.cpp:1263
Term * make_skolem_function(const vector< Term * > &)
Make a new Skolem function.
Definition FOF.cpp:205
static FOF make_imp(const FOF &lhs, const FOF &rhs)
Directly make an implication.
Definition FOF.hpp:320
void distribute_or()
Distribute ORs to the maximum extent possible.
Definition FOF.cpp:1259
static TPTPRecords * recs_p
Allow FOF to add TPTP outputs.
Definition FOF.hpp:63
void convert_to_cnf_clauses(vector< Clause > &, vector< string > &, const string &)
Convert to Conjunctive Normal Form.
Definition FOF.cpp:817
void find_definitional_tuple(FOF &, vector< FOF > &, vector< FOF > &, bool)
Heavy-lifting for conversion to definitional clause form.
Definition FOF.cpp:916
void simple_negate()
Negate an FOF without applying any simplifications.
Definition FOF.cpp:453
bool is_literal() const
Check if an FOF is a literal.
Definition FOF.cpp:402
void skolemize_universals_main(vector< Term * >, FOF *, bool=false)
The dual of skolemize_main.
Definition FOF.cpp:273
string to_string() const
Convert a FOF into a nice-looking string.
Definition FOF.cpp:1267
void negate()
Negate an FOF, applying obvious simplifications if it's already negated or an implication.
Definition FOF.cpp:431
string predicate_to_tptp_string() const
Assuming this is an Atom, return the predicate name as a string.
Definition FOF.cpp:1438
void remove_existential_quantifiers()
Self-explanatory.
Definition FOF.cpp:1131
void invert_literal()
Self-explanatory!
Definition FOF.cpp:462
bool is_clause() const
Check if something is a clause.
Definition FOF.cpp:407
Literal pred
FOFType::Atom can store one of these to represent a Literal.
Definition FOF.hpp:72
void replace_variable_with_term(Term *, Variable *)
Replace a Variable with a Term.
Definition FOF.cpp:211
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.
Class collecting a bunch of TPTPRecord items for later formatting.
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.