Connect++ 0.1
A fast, readable connection prover for first-order logic.
Loading...
Searching...
No Matches
FOF.hpp
1/*
2
3Copyright © 2023 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;
67 Literal pred;
71 vector<FOF> sub_formulas;
75 Variable* var;
80 static VariableIndex* var_index;
85 static FunctionIndex* fun_index;
90 static TermIndex* term_index;
91 //---------------------------------------------------------------------------
92 // Simple methods for Variables.
93 //---------------------------------------------------------------------------
102 bool has_free_variable(Variable*);
109 void replace_variable(Variable*, Variable*);
118 //---------------------------------------------------------------------------
119 // Skolemization.
120 //---------------------------------------------------------------------------
121 Term* make_skolem_function(const vector<Term*>&);
130 void replace_variable_with_term(Term*, Variable*);
143 void skolemize_main(vector<Term*>);
144 //---------------------------------------------------------------------------
145 // Miniscoping.
146 //---------------------------------------------------------------------------
159 void miniscope_split(Variable*, vector<FOF>&, vector<FOF>&);
163 void miniscope_all();
164public:
165 //---------------------------------------------------------------------------
166 // Constructors.
167 //---------------------------------------------------------------------------
171 FOF() = delete;
175 FOF(FOFType t)
176 : type(t), pred(), sub_formulas(), var(nullptr) {}
182 FOF(const Literal&);
190 FOF(FOFType, const vector<FOF>&, Variable*);
191 //---------------------------------------------------------------------------
192 // Straightforward methods for getting, setting etc.
193 //---------------------------------------------------------------------------
197 static void set_indexes(std::tuple<VariableIndex*, FunctionIndex*, PredicateIndex*, TermIndex*> is) {
198 var_index = std::get<0>(is);
199 fun_index = std::get<1>(is);
200 term_index = std::get<3>(is);
201 }
205 void show_indexes() const {
206 cout << var_index << " " << fun_index << " " << term_index << endl;
207 }
211 FOFType fof_type() { return type; }
215 void add_formula(const FOF& f) { sub_formulas.push_back(f); }
219 void clear() { type = FOFType::Empty, pred.clear(), sub_formulas.clear(), var = nullptr; }
220 //---------------------------------------------------------------------------
221 // Straightforward methods for making FOFs.
222 //---------------------------------------------------------------------------
226 static FOF make_literal(const Literal& lit) {
227 FOF result(lit);
228 return result;
229 }
235 static FOF make_neg(const FOF& f) {
236 vector<FOF> fs;
237 fs.push_back(f);
238 FOF result(FOFType::Neg, fs, nullptr);
239 return result;
240 }
244 static FOF make_and(const vector<FOF>& fs) {
245 FOF result(FOFType::And, fs, nullptr);
246 return result;
247 }
251 static FOF make_or(const vector<FOF>& fs) {
252 FOF result(FOFType::Or, fs, nullptr);
253 return result;
254 }
258 static FOF make_imp(const FOF& lhs, const FOF& rhs) {
259 vector<FOF> fs;
260 fs.push_back(lhs);
261 fs.push_back(rhs);
262 FOF result(FOFType::Imp, fs, nullptr);
263 return result;
264 }
268 static FOF make_iff(const FOF& lhs, const FOF& rhs) {
269 vector<FOF> fs;
270 fs.push_back(lhs);
271 fs.push_back(rhs);
272 FOF result(FOFType::Iff, fs, nullptr);
273 return result;
274 }
278 static FOF make_forall(const FOF& f, Variable* v) {
279 vector<FOF> fs;
280 fs.push_back(f);
281 FOF result(FOFType::A, fs, v);
282 return result;
283 }
287 static FOF make_exists(const FOF& f, Variable* v) {
288 vector<FOF> fs;
289 fs.push_back(f);
290 FOF result(FOFType::E, fs, v);
291 return result;
292 }
293 //---------------------------------------------------------------------------
294 // Basic tests.
295 //---------------------------------------------------------------------------
301 bool is_literal() const;
310 bool is_clause() const;
311 //---------------------------------------------------------------------------
312 // Standard simplifications.
313 //---------------------------------------------------------------------------
318 void remove_negation();
323 void negate();
327 void remove_iff();
331 void remove_imp();
343 void push_negs();
349 void convert_to_nnf();
356 void miniscope();
364 void convert_to_cnf();
372 void skolemize();
385 void to_Literal(Literal&) const;
393 void to_clause(Clause&) const;
402 void to_clauses(vector<Clause>&) const;
406 bool has_and() const;
412 void collect_ors();
421 void distribute_or();
426 void flatten_ands();
433 void flatten();
434
435 string to_string () const;
436
437 friend ostream& operator<<(ostream&, const FOF&);
438};
439
440
441#endif
Representation of clauses.
Definition Clause.hpp:45
Representation of first order formulas.
Definition FOF.hpp:57
void skolemize()
Skolemize the given formula.
Definition FOF.cpp:139
void remove_iff()
Replace <-> throughout using ->.
Definition FOF.cpp:408
static FOF make_or(const vector< FOF > &fs)
Directly make a disjunction.
Definition FOF.hpp:251
void convert_to_nnf()
Convert to Negation Normal Form.
Definition FOF.cpp:543
void clear()
Make an FOFType::Empty.
Definition FOF.hpp:219
void add_formula(const FOF &f)
Add a subformula.
Definition FOF.hpp:215
FOFType fof_type()
Basic get method.
Definition FOF.hpp:211
void show_indexes() const
Show the indices.
Definition FOF.hpp:205
void make_unique_bound_variables()
Replace all bound variables with unique new ones.
Definition FOF.cpp:518
void remove_universal_quantifiers()
Self-explanatory.
Definition FOF.cpp:562
void to_clauses(vector< Clause > &) const
Assuming you have a flattened CNF, convert it to a collection of Clauses.
Definition FOF.cpp:614
static FOF make_and(const vector< FOF > &fs)
Directly make a conjunction.
Definition FOF.hpp:244
void miniscope()
Apply the rules for miniscoping.
Definition FOF.cpp:165
static FOF make_iff(const FOF &lhs, const FOF &rhs)
Directly make an iff.
Definition FOF.hpp:268
void push_negs()
Push all negations in.
Definition FOF.cpp:453
static FOF make_literal(const Literal &lit)
Directly make a Literal.
Definition FOF.hpp:226
static void set_indexes(std::tuple< VariableIndex *, FunctionIndex *, PredicateIndex *, TermIndex * > is)
Set up pointer to the variable index etc.
Definition FOF.hpp:197
static FOF make_neg(const FOF &f)
Directly negate a FOF.
Definition FOF.hpp:235
void to_Literal(Literal &) const
Assuming the FOF is a literal, convert it to something of type Literal.
Definition FOF.cpp:590
static FOF make_exists(const FOF &f, Variable *v)
Directly make an existentially quantified FOF.
Definition FOF.hpp:287
void remove_negation()
Remove the leading negation from an arbitrary FOF, if it has one.
Definition FOF.cpp:379
static FOF make_forall(const FOF &f, Variable *v)
Directly make a universally quantified FOF.
Definition FOF.hpp:278
void remove_imp()
Replace A->B throughout with neg A v B.
Definition FOF.cpp:435
void collect_ors()
Assuming you just have a structure containing ORs, ANDs and literals, re-arrange it to make it easier...
Definition FOF.cpp:654
FOF(FOFType t)
You probably don't want this constructor.
Definition FOF.hpp:175
FOF()=delete
You don't want this constructor.
void flatten()
After distributing ORs, flatten into something directly in CNF form.
Definition FOF.cpp:753
void flatten_ands()
Assuming you have a conjunction of CNFs, get rid of the initial AND.
Definition FOF.cpp:728
static FOF make_imp(const FOF &lhs, const FOF &rhs)
Directly make an implication.
Definition FOF.hpp:258
void convert_to_cnf()
Convert to Conjunctive Normal Form.
Definition FOF.cpp:553
void distribute_or()
Distribute ORs to the maximum extent possible.
Definition FOF.cpp:673
bool has_and() const
Does the collection of subformulas contain an AND?
Definition FOF.cpp:645
bool is_literal() const
Check if an FOF is a literal.
Definition FOF.cpp:357
void negate()
Negate an FOF, applying obvious simplifications if it's already negated or an implication.
Definition FOF.cpp:386
bool is_clause() const
Check if something is a clause.
Definition FOF.cpp:362
void to_clause(Clause &) const
Assuming you have an FOF that's just a clause, convert it to something of Clause type.
Definition FOF.cpp:600
Mechanism for looking after functions.
Definition FunctionIndex.hpp:64
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:115
General representation of terms.
Definition Term.hpp:62
Look after terms, using hash consing to avoid storing copies of terms.
Definition TermIndex-hash.hpp:26
Basic representation of variables.
Definition Variable.hpp:58
Storage of named variables, and management of new, anonymous variables.
Definition VariableIndex.hpp:61