Connect++ 0.1
A fast, readable connection prover for first-order logic.
Loading...
Searching...
No Matches
TPTPParser.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
48#ifndef TPTPPARSER_HPP
49#define TPTPPARSER_HPP
50
51#include <iostream>
52#include <fstream>
53
54#include <boost/spirit/include/qi.hpp>
55
56#include "Exceptions.hpp"
57#include "PredicateIndex.hpp"
58#include "FunctionIndex.hpp"
59#include "Matrix.hpp"
60#include "FOF.hpp"
61#include "vic_strings.hpp"
62
63using std::string;
64using std::vector;
65using std::set;
66using std::ifstream;
67using std::cout;
68namespace fs = std::filesystem;
69
70using namespace boost::spirit;
71
72//---------------------------------------------------------------------
73//---------------------------------------------------------------------
74//---------------------------------------------------------------------
75namespace tptp_parser {
79 using Iter = string::const_iterator;
90 boost::variant<
91 string,
92 boost::recursive_wrapper<fof_plain_term_struct>
93 >;
104 string functor;
105 std::vector<fof_arguments_struct> args;
106 };
112 string connective;
114 };
118 using fof_atomic_formula_type = boost::variant<
121 >;
126 string name;
127 string role;
128 std::vector<fof_atomic_formula_type> f;
129 };
130} // tptp_parser namespace ends.
131//---------------------------------------------------------------------
132//---------------------------------------------------------------------
133//---------------------------------------------------------------------
141BOOST_FUSION_ADAPT_STRUCT(
143 (string, functor)
144 (std::vector<tptp_parser::fof_arguments_struct>, args)
145)
153BOOST_FUSION_ADAPT_STRUCT(
156 (string, connective)
158)
159//---------------------------------------------------------------------
160//---------------------------------------------------------------------
161//---------------------------------------------------------------------
162//---------------------------------------------------------------------
163// Back to the tptp_parser namespace.
164//---------------------------------------------------------------------
165namespace tptp_parser {
176 struct fof_unitary_formula_struct;
177 struct fof_negation_struct;
178 struct fof_quantifier_struct;
179 struct fof_binary_struct;
180 struct fof_andor_struct;
184 using fof_formula_type = boost::variant<
185 fof_plain_term_struct,
186 infix_struct,
187 boost::recursive_wrapper<fof_unitary_formula_struct>,
188 boost::recursive_wrapper<fof_negation_struct>,
189 boost::recursive_wrapper<fof_quantifier_struct>,
190 boost::recursive_wrapper<fof_binary_struct>,
191 boost::recursive_wrapper<fof_andor_struct>
192 >;
196 struct fof_unitary_formula_struct {
197 fof_formula_type arg;
198 };
202 struct fof_negation_struct {
203 string type;
204 fof_formula_type arg;
205 };
209 struct fof_quantifier_struct {
210 string type;
211 std::vector<string> vars;
212 fof_formula_type arg;
213 };
217 struct fof_binary_struct {
218 fof_formula_type lhs;
219 string type;
220 fof_formula_type rhs;
221 };
225 struct fof_andor_struct {
226 fof_formula_type lhs;
227 string type;
228 vector<fof_formula_type> rhs;
229 };
230} // tptp_parser namespace ends.
231//---------------------------------------------------------------------
232//---------------------------------------------------------------------
233//---------------------------------------------------------------------
241BOOST_FUSION_ADAPT_STRUCT(
242 tptp_parser::fof_unitary_formula_struct,
243 (tptp_parser::fof_formula_type, arg)
244)
252BOOST_FUSION_ADAPT_STRUCT(
253 tptp_parser::fof_negation_struct,
254 (string, type)
255 (tptp_parser::fof_formula_type, arg)
256)
264BOOST_FUSION_ADAPT_STRUCT(
265 tptp_parser::fof_quantifier_struct,
266 (string, type)
267 (std::vector<string>, vars)
268 (tptp_parser::fof_formula_type, arg)
269)
277BOOST_FUSION_ADAPT_STRUCT(
278 tptp_parser::fof_binary_struct,
279 (tptp_parser::fof_formula_type, lhs)
280 (string, type)
281 (tptp_parser::fof_formula_type, rhs)
282)
290BOOST_FUSION_ADAPT_STRUCT(
291 tptp_parser::fof_andor_struct,
292 (tptp_parser::fof_formula_type, lhs)
293 (string, type)
294 (std::vector<tptp_parser::fof_formula_type>, rhs)
295)
296//---------------------------------------------------------------------
297//---------------------------------------------------------------------
298//---------------------------------------------------------------------
299//---------------------------------------------------------------------
300// Back to the tptp_parser namespace.
301//---------------------------------------------------------------------
302namespace tptp_parser {
309 struct show_fof_plain_term {
310 void operator()(const fof_plain_term_struct&);
311 };
318 struct show_fof_arguments : boost::static_visitor<> {
319 void operator()(const string& s) {
320 cout << s;
321 }
322 void operator()(const fof_plain_term_struct& t) {
323 show_fof_plain_term spt;
324 spt(t);
325 }
326 };
333 struct show_fof_atomic_formula : boost::static_visitor<> {
334 void operator()(const fof_plain_term_struct& s) {
335 show_fof_plain_term st;
336 st(s);
337 }
338 void operator()(const infix_struct& s) {
339 show_fof_arguments sa;
340 boost::apply_visitor(sa, s.left);
341 cout << " " << s.connective << " ";
342 boost::apply_visitor(sa, s.right);
343 }
344 };
351 struct show_clause {
352 void operator()(const vector<fof_atomic_formula_type>& v) const {
353 show_fof_atomic_formula st;
354 cout << "Clause = {" << endl;
355 for (fof_atomic_formula_type f : v) {
356 cout << " ";
357 boost::apply_visitor(st, f);
358 cout << endl;
359 }
360 cout << '}' << endl;
361 }
362 };
369 struct show_fof_formula_type : boost::static_visitor<> {
370 void operator()(const fof_plain_term_struct& t) {
371 show_fof_atomic_formula saf;
372 saf(t);
373 }
374 void operator()(const infix_struct& s) {
375 show_fof_atomic_formula saf;
376 saf(s);
377 }
378 void operator()(const fof_unitary_formula_struct& f) {
380 boost::apply_visitor(sf, f.arg);
381 }
382 void operator()(const fof_negation_struct& f) {
383 cout << f.type << "(";
385 boost::apply_visitor(sf, f.arg);
386 cout << ")";
387 }
388 void operator()(const fof_quantifier_struct& f) {
389 cout << f.type << " [";
390 for (const string& s : f.vars)
391 cout << s << " ";
392 cout << "] (";
394 boost::apply_visitor(sf, f.arg);
395 cout << ")";
396 }
397 void operator()(const fof_binary_struct& f) {
399 boost::apply_visitor(sf, f.lhs);
400 cout << " " << f.type << " ";
401 boost::apply_visitor(sf, f.rhs);
402 }
403 void operator()(const fof_andor_struct& f) {
405 boost::apply_visitor(sf, f.lhs);
406 for (const fof_formula_type& g : f.rhs) {
407 cout << " " << f.type << " ";
408 boost::apply_visitor(sf, g);
409 }
410 }
411 };
419 void operator()(const fof_formula_type& f) const {
421 boost::apply_visitor(sf, f);
422 cout << endl;
423 }
424};
425//---------------------------------------------------------------------
426//---------------------------------------------------------------------
427//---------------------------------------------------------------------
434 void operator()(const vector<string>&, qi::unused_type, qi::unused_type) const;
435};
442 void operator()(const string&, qi::unused_type, qi::unused_type) const;
443};
450 void operator()(const string&, qi::unused_type, qi::unused_type) const;
451};
458 void operator()(const string&, qi::unused_type, qi::unused_type) const;
459};
460//---------------------------------------------------------------------
461//---------------------------------------------------------------------
462//---------------------------------------------------------------------
468struct to_lit {
469 void operator()(const fof_atomic_formula_type&, qi::unused_type,qi::unused_type) const;
470};
476struct show_lit {
477 void operator()(const fof_atomic_formula_type&, qi::unused_type, qi::unused_type) const;
478};
485 void operator()(qi::unused_type, qi::unused_type, qi::unused_type) const;
486};
493 void operator()(qi::unused_type, qi::unused_type, qi::unused_type) const;
494};
501 void operator()(qi::unused_type, qi::unused_type, qi::unused_type) const;
502};
509 void operator()(qi::unused_type, qi::unused_type, qi::unused_type) const;
510};
511//---------------------------------------------------------------------
512//---------------------------------------------------------------------
513//---------------------------------------------------------------------
523struct convert_fof_atomic_formula : boost::static_visitor<Literal> {
524 Literal operator()(const fof_plain_term_struct&) const;
525 Literal operator()(const infix_struct&) const;
526};
536struct convert_fof_arguments_struct : boost::static_visitor<Term*> {
537 Term* operator()(const string&) const;
538 Term* operator()(const fof_plain_term_struct&) const;
539};
540//---------------------------------------------------------------------
541//---------------------------------------------------------------------
542//---------------------------------------------------------------------
547 void operator()(const string&,
548 qi::unused_type,
549 qi::unused_type) const;
550};
555 void operator()(const string&,
556 qi::unused_type,
557 qi::unused_type) const;
558};
559//---------------------------------------------------------------------
560//---------------------------------------------------------------------
561//---------------------------------------------------------------------
566 void operator()(const string&,
567 qi::unused_type,
568 qi::unused_type) const;
569};
574 void operator()(const string&,
575 qi::unused_type,
576 qi::unused_type) const;
577};
578//---------------------------------------------------------------------
579//---------------------------------------------------------------------
580//---------------------------------------------------------------------
586struct convert_fof_formula : boost::static_visitor<FOF> {
587 FOF operator()(const fof_plain_term_struct&) const;
588 FOF operator()(const infix_struct&) const;
589 FOF operator()(const fof_unitary_formula_struct&) const;
590 FOF operator()(const fof_negation_struct&) const;
591 FOF operator()(const fof_quantifier_struct&) const;
592 FOF operator()(const fof_binary_struct&) const;
593 FOF operator()(const fof_andor_struct&) const;
594};
601 void operator()(const fof_formula_type&, qi::unused_type, qi::unused_type) const;
602};
609 void operator()(qi::unused_type, qi::unused_type, qi::unused_type) const;
610};
611//---------------------------------------------------------------------
621void read_tptp_from_file_simple(const string&, string&);
622//---------------------------------------------------------------------
633using FileIncludeItem = pair<string, vector<string>>;
634
635void recursive_tptp_include(const FileIncludeItem&);
636//---------------------------------------------------------------------
637//---------------------------------------------------------------------
638//---------------------------------------------------------------------
651private:
657 string file_contents;
661 vector<string> comment_blocks;
668 VariableIndex* vip;
675 FunctionIndex* fip;
682 TermIndex* tip;
689 PredicateIndex* pip;
694 Matrix* matrix;
704 void read_tptp_from_file(const string&);
705public:
709 TPTPParser() = delete;
715 TermIndex*,
717 Matrix*);
733 bool parse_tptp_from_file(const string&);
739 void show_file_includes();
747 void clear();
753 vector<string> get_defined_items();
759 vector<string> get_system_items();
763 bool equality_used();
767 bool conjecture_present() const;
771 size_t number_of_fof_formulas() const;
775 string get_problem_status();
781 Predicate* get_equals_predicate() const;
782};
783
784};
785
786#endif
Representation of first order formulas.
Definition FOF.hpp:57
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
Representation of the Matrix within a proof.
Definition Matrix.hpp:70
Basic representation of predicates: here just names, ids and arities.
Definition Predicate.hpp:51
Management of Predicate objects.
Definition PredicateIndex.hpp:58
Wrap up everything the TPTP parser needs to do inside a single class.
Definition TPTPParser.hpp:650
TPTPParser()=delete
You definitely don't want this constructor.
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
Storage of named variables, and management of new, anonymous variables.
Definition VariableIndex.hpp:61
The tptp_parser namespace contains a lot of stuff that's essentially just global.
Definition TPTPParser.cpp:71
boost::variant< string, boost::recursive_wrapper< fof_plain_term_struct > > fof_arguments_struct
Variant - Term arguments can be Variables or other Terms.
Definition TPTPParser.hpp:93
string::const_iterator Iter
Iterator type for parser rules and grammars.
Definition TPTPParser.hpp:79
boost::variant< fof_plain_term_struct, infix_struct > fof_atomic_formula_type
An atomic formula can be infix as well.
Definition TPTPParser.hpp:121
One of several basic semantic actions.
Definition TPTPParser.hpp:441
One of several basic semantic actions.
Definition TPTPParser.hpp:449
One of several basic semantic actions.
Definition TPTPParser.hpp:433
One of several basic semantic actions.
Definition TPTPParser.hpp:457
Collection of more complex semantic actions.
Definition TPTPParser.hpp:508
More complex semantic action for FOF formulas.
Definition TPTPParser.hpp:608
Collection of more complex semantic actions.
Definition TPTPParser.hpp:500
More complex semantic actions, now functions etc to make a literal.
Definition TPTPParser.hpp:536
More complex semantic actions, now functions etc to make a literal.
Definition TPTPParser.hpp:523
More complex semantic action for FOF formulas.
Definition TPTPParser.hpp:586
More complex semantic action for FOF formulas.
Definition TPTPParser.hpp:600
Semantic action for cnf_formula.
Definition TPTPParser.hpp:546
Semantic action for cnf_formula.
Definition TPTPParser.hpp:554
Semantic action for fof_formula.
Definition TPTPParser.hpp:565
Semantic action for fof_formula.
Definition TPTPParser.hpp:573
Collection of more complex semantic actions.
Definition TPTPParser.hpp:484
You have to make fof_plain_term_struct etc something that boost::fusion can use.
Definition TPTPParser.hpp:369
Display FOL formulas as you parse.
Definition TPTPParser.hpp:418
Collection of more complex semantic actions.
Definition TPTPParser.hpp:476
Collection of more complex semantic actions.
Definition TPTPParser.hpp:468
Parser's representation of a Clause.
Definition TPTPParser.hpp:125
Recursive data structure for Terms.
Definition TPTPParser.hpp:103
Structure for fof_defined_infix_formula and fof_infix_unary.
Definition TPTPParser.hpp:110
Collection of more complex semantic actions.
Definition TPTPParser.hpp:492