Connect++ 0.1
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;
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;
95 static PredicateIndex* pred_index;
96 //---------------------------------------------------------------------------
97 // Simple methods for Variables.
98 //---------------------------------------------------------------------------
108 bool has_free_variable(Variable*);
118 void replace_variable(Variable*, Variable*);
119 //---------------------------------------------------------------------------
120 // Skolemization.
121 //---------------------------------------------------------------------------
130 Term* make_skolem_function(const vector<Term*>&);
142 void replace_variable_with_term(Term*, Variable*);
155 void skolemize_main(vector<Term*>);
156 //---------------------------------------------------------------------------
157 // Miniscoping.
158 //---------------------------------------------------------------------------
171 void miniscope_split(Variable*, vector<FOF>&, vector<FOF>&);
175 void miniscope_all();
176public:
177 //---------------------------------------------------------------------------
178 // Constructors.
179 //---------------------------------------------------------------------------
183 FOF() = delete;
187 FOF(FOFType t)
188 : type(t), pred(), sub_formulas(), var(nullptr) {}
194 FOF(const Literal&);
202 FOF(FOFType, const vector<FOF>&, Variable*);
203 //---------------------------------------------------------------------------
204 // Straightforward methods for getting, setting etc.
205 //---------------------------------------------------------------------------
209 static void set_indexes(std::tuple<VariableIndex*,
212 TermIndex*> is) {
213 var_index = std::get<0>(is);
214 fun_index = std::get<1>(is);
215 pred_index = std::get<2>(is);
216 term_index = std::get<3>(is);
217 }
221 void show_indexes() const {
222 cout << var_index << " " << fun_index
223 << " " << pred_index
224 << " " << term_index
225 << endl;
226 }
230 FOFType fof_type() { return type; }
234 void add_formula(const FOF& f) { sub_formulas.push_back(f); }
238 void clear() {
239 type = FOFType::Empty;
240 pred.clear();
241 sub_formulas.clear();
242 var = nullptr;
243 }
244 //---------------------------------------------------------------------------
245 // Straightforward methods for making FOFs.
246 //---------------------------------------------------------------------------
250 static FOF make_literal(const Literal& lit) {
251 FOF result(lit);
252 return result;
253 }
259 static FOF make_neg(const FOF& f) {
260 vector<FOF> fs;
261 fs.push_back(f);
262 FOF result(FOFType::Neg, fs, nullptr);
263 return result;
264 }
268 static FOF make_and(const vector<FOF>& fs) {
269 FOF result(FOFType::And, fs, nullptr);
270 return result;
271 }
275 static FOF make_or(const vector<FOF>& fs) {
276 FOF result(FOFType::Or, fs, nullptr);
277 return result;
278 }
282 static FOF make_imp(const FOF& lhs, const FOF& rhs) {
283 vector<FOF> fs;
284 fs.push_back(lhs);
285 fs.push_back(rhs);
286 FOF result(FOFType::Imp, fs, nullptr);
287 return result;
288 }
292 static FOF make_iff(const FOF& lhs, const FOF& rhs) {
293 vector<FOF> fs;
294 fs.push_back(lhs);
295 fs.push_back(rhs);
296 FOF result(FOFType::Iff, fs, nullptr);
297 return result;
298 }
302 static FOF make_forall(const FOF& f, Variable* v) {
303 vector<FOF> fs;
304 fs.push_back(f);
305 FOF result(FOFType::A, fs, v);
306 return result;
307 }
311 static FOF make_exists(const FOF& f, Variable* v) {
312 vector<FOF> fs;
313 fs.push_back(f);
314 FOF result(FOFType::E, fs, v);
315 return result;
316 }
317 //---------------------------------------------------------------------------
318 // Basic tests.
319 //---------------------------------------------------------------------------
325 bool is_literal() const;
334 bool is_clause() const;
335 //---------------------------------------------------------------------------
336 // Standard simplifications.
337 //---------------------------------------------------------------------------
342 void remove_negation();
347 void negate();
351 void remove_iff();
355 void remove_imp();
367 void push_negs();
373 void convert_to_nnf();
380 void miniscope();
389 void convert_to_cnf_clauses(vector<Clause>&);
397 void skolemize();
410 void to_Literal(Literal&) const;
415 void convert_to_clauses(vector<Clause>&) const;
423 size_t find_and() const;
428 bool distribute_or_once();
436 void distribute_or();
440 string to_string () const;
445 static SimplificationResult simplify_cnf(vector<Clause>&);
446
447 friend ostream& operator<<(ostream&, const FOF&);
448};
449
450
451#endif
Representation of first order formulas.
Definition FOF.hpp:57
void skolemize()
Skolemize the given formula.
Definition FOF.cpp:640
void remove_iff()
Replace <-> throughout using ->.
Definition FOF.cpp:272
static FOF make_or(const vector< FOF > &fs)
Directly make a disjunction.
Definition FOF.hpp:275
void convert_to_nnf()
Convert to Negation Normal Form.
Definition FOF.cpp:407
void clear()
Make an FOFType::Empty.
Definition FOF.hpp:238
void add_formula(const FOF &f)
Add a subformula.
Definition FOF.hpp:234
void convert_to_cnf_clauses(vector< Clause > &)
Convert to Conjunctive Normal Form.
Definition FOF.cpp:584
FOFType fof_type()
Basic get method.
Definition FOF.hpp:230
void show_indexes() const
Show the indices.
Definition FOF.hpp:221
void make_unique_bound_variables()
Replace all bound variables with unique new ones.
Definition FOF.cpp:317
static SimplificationResult simplify_cnf(vector< Clause > &)
Simplify the clauses in a CNF using the method in Clause.
Definition FOF.cpp:861
void remove_universal_quantifiers()
Self-explanatory.
Definition FOF.cpp:645
bool distribute_or_once()
Find an OR that can be distributed and deal with it. Indicate if there isn't one.
Definition FOF.cpp:726
static FOF make_and(const vector< FOF > &fs)
Directly make a conjunction.
Definition FOF.hpp:268
void miniscope()
Apply the rules for miniscoping.
Definition FOF.cpp:417
static FOF make_iff(const FOF &lhs, const FOF &rhs)
Directly make an iff.
Definition FOF.hpp:292
void push_negs()
Push all negations in.
Definition FOF.cpp:342
static FOF make_literal(const Literal &lit)
Directly make a Literal.
Definition FOF.hpp:250
static void set_indexes(std::tuple< VariableIndex *, FunctionIndex *, PredicateIndex *, TermIndex * > is)
Set up pointer to the variable index etc.
Definition FOF.hpp:209
static FOF make_neg(const FOF &f)
Directly negate a FOF.
Definition FOF.hpp:259
void to_Literal(Literal &) const
Assuming the FOF is a literal, convert it to something of type Literal.
Definition FOF.cpp:673
static FOF make_exists(const FOF &f, Variable *v)
Directly make an existentially quantified FOF.
Definition FOF.hpp:311
void remove_negation()
Remove the leading negation from an arbitrary FOF, if it has one.
Definition FOF.cpp:243
static FOF make_forall(const FOF &f, Variable *v)
Directly make a universally quantified FOF.
Definition FOF.hpp:302
void convert_to_clauses(vector< Clause > &) const
Assuming you have a CNF, convert it to a collection of Clauses.
Definition FOF.cpp:683
void remove_imp()
Replace A->B throughout with neg A v B.
Definition FOF.cpp:299
size_t find_and() const
Does the collection of subformulas contain an AND? If so, find it.
Definition FOF.cpp:716
FOF(FOFType t)
You probably don't want this constructor.
Definition FOF.hpp:187
FOF()=delete
You don't want this constructor.
static FOF make_imp(const FOF &lhs, const FOF &rhs)
Directly make an implication.
Definition FOF.hpp:282
void distribute_or()
Definition FOF.cpp:779
bool is_literal() const
Check if an FOF is a literal.
Definition FOF.cpp:221
string to_string() const
Convert a FOF into a nice-looking string.
Definition FOF.cpp:783
void negate()
Negate an FOF, applying obvious simplifications if it's already negated or an implication.
Definition FOF.cpp:250
bool is_clause() const
Check if something is a clause.
Definition FOF.cpp:226
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
Management of Predicate objects.
Definition PredicateIndex.hpp:58
General representation of terms.
Definition Term.hpp:62
Look after terms, (ideally) using hash consing to avoid storing copies of terms.
Definition TermIndex.hpp:75
Basic representation of variables.
Definition Variable.hpp:58
Storage of named variables, and management of new, anonymous variables.
Definition VariableIndex.hpp:61