aboutsummaryrefslogtreecommitdiff
path: root/include/atp/frontend/tptp_parser.h
blob: 075b45e6aa411a01cd3e10119d08bc532d88811a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * richard-atp-engine: A saturation-based automated theorem prover for first-order logic.
 * Copyright (C) 2026 Richard Qin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#pragma once

/// @file tptp_parser.h
/// @brief TPTP format parser — converts .p files to internal representations.
///
/// The parser interns all symbols (predicates, functions, variables) into the
/// SymbolTable and builds terms in the TermBank during parsing. This is the
/// E Prover approach: parse and intern simultaneously, avoiding a separate
/// resolution pass.
///
/// V1: Hand-written recursive descent for TPTP CNF/FOF subset.
///     (ANTLR4 integration deferred — avoids build complexity for V1.)

#include "atp/core/clause.h"
#include "atp/core/symbol_table.h"
#include "atp/core/term_bank.h"
#include "atp/normalize/formula.h"

#include <string>
#include <vector>

namespace atp {

/// Role of a TPTP formula.
/// See TPTP spec: https://www.tptp.org/TPTP/SyntaxBNF.html
enum class FormulaRole : uint8_t {
    kAxiom,                ///< Background truth
    kHypothesis,           ///< Problem-specific assumption
    kDefinition,           ///< Symbol definition (can be unfolded)
    kAssumption,           ///< Assumed for this proof attempt
    kLemma,                ///< Derived intermediate result
    kTheorem,              ///< Previously proved statement (used as axiom)
    kConjecture,           ///< The statement to prove
    kNegatedConjecture,    ///< Already negated conjecture (input was cnf)
    kPlain,                ///< No semantic role specified
};

/// Map a TPTP role string to enum. Returns kPlain for unknown roles.
FormulaRole parseRole(std::string_view role_str);

/// A parsed TPTP formula with its name and role.
struct AnnotatedFormula {
    std::string name;                  ///< Formula name (e.g., "axiom1")
    FormulaRole role;                  ///< Semantic role
    std::unique_ptr<Formula> formula;  ///< The FOL formula (for fof(...))
    std::vector<Clause> clauses;       ///< Direct clauses (for cnf(...))
    bool is_cnf = false;               ///< true if input was cnf(), false if fof()
};

/// A complete parsed TPTP problem.
struct ParsedProblem {
    std::vector<AnnotatedFormula> formulas;

    /// Does any formula contain an equality predicate?
    bool has_equality = false;

    /// Original file path (for error messages).
    std::string source_file;
};

/// Parse a TPTP file.
///
/// For `cnf(...)` entries: clauses are built directly (no clausification needed).
/// For `fof(...)` entries: Formula AST is built (clausification happens later).
///
/// All symbols are interned into `symbols` and terms into `bank` during parsing.
ParsedProblem parseTptpFile(const std::string& filepath,
                            TermBank& bank, SymbolTable& symbols);

/// Parse a TPTP string (for testing).
ParsedProblem parseTptpString(const std::string& input,
                              TermBank& bank, SymbolTable& symbols);

/// Prepare a parsed problem for proving:
///   1. Negate conjecture formulas.
///   2. Clausify all fof() formulas.
///   3. Collect all cnf() clauses directly.
///   4. Assign ClauseIds and set provenance to kInput.
///
/// Returns the final clause set ready for the Prover.
std::vector<Clause> prepareForProving(ParsedProblem& problem,
                                      TermBank& bank, SymbolTable& symbols);

}  // namespace atp