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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/*
* 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/>.
*/
#include "atp/frontend/tptp_parser.h"
#include "atp/core/clause.h"
#include "atp/core/literal.h"
#include "atp/core/symbol_table.h"
#include "atp/core/term_bank.h"
#include "atp/core/types.h"
#include <cctype>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <ios>
#include <iterator>
#include <stdexcept>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
namespace atp {
FormulaRole parseRole(std::string_view role_str) {
if (role_str == "axiom") {
return FormulaRole::kAxiom;
}
if (role_str == "hypothesis") {
return FormulaRole::kHypothesis;
}
if (role_str == "definition") {
return FormulaRole::kDefinition;
}
if (role_str == "assumption") {
return FormulaRole::kAssumption;
}
if (role_str == "lemma") {
return FormulaRole::kLemma;
}
if (role_str == "theorem") {
return FormulaRole::kTheorem;
}
if (role_str == "conjecture") {
return FormulaRole::kConjecture;
}
if (role_str == "negated_conjecture") {
return FormulaRole::kNegatedConjecture;
}
if (role_str == "plain") {
return FormulaRole::kPlain;
}
return FormulaRole::kPlain;
}
enum class TokenType : std::uint8_t { kWord, kLParen, kRParen, kOr, kNot, kDot, kComma, kEOF, kUnkown };
struct Token {
TokenType type_{};
std::string_view value_;
size_t line_{};
size_t column_{};
};
class TptpLexer {
public:
explicit TptpLexer(std::string_view input) : source_(input) {}
Token nextToken() {
skipWhitespaceAndComments();
if (pos_ >= source_.length()) {
return {.type_ = TokenType::kEOF, .value_ = "", .line_ = line_, .column_ = column_};
}
size_t const start_pos = pos_;
[[maybe_unused]] size_t const start_col = column_;
char const c = source_[pos_];
if ((std::isalpha(static_cast<unsigned char>(c)) != 0) || c == '_') {
while (
pos_ < source_.size() &&
((std::isalnum(static_cast<unsigned char>(source_[pos_])) != 0) || source_[pos_] == '_')) {
advanceChar();
}
return {.type_ = TokenType::kWord,
.value_ = source_.substr(start_pos, pos_ - start_pos),
.line_ = line_,
.column_ = column_};
}
advanceChar();
switch (c) {
case '(':
return {.type_ = TokenType::kLParen, .value_ = "(", .line_ = line_, .column_ = column_};
case ')':
return {.type_ = TokenType::kRParen, .value_ = ")", .line_ = line_, .column_ = column_};
case ',':
return {.type_ = TokenType::kComma, .value_ = ",", .line_ = line_, .column_ = column_};
case '.':
return {.type_ = TokenType::kDot, .value_ = ".", .line_ = line_, .column_ = column_};
case '~':
return {.type_ = TokenType::kNot, .value_ = "~", .line_ = line_, .column_ = column_};
case '|':
return {.type_ = TokenType::kOr, .value_ = "|", .line_ = line_, .column_ = column_};
default:
return {.type_ = TokenType::kUnkown,
.value_ = source_.substr(start_pos, 1),
.line_ = line_,
.column_ = column_};
}
}
private:
std::string_view source_;
size_t pos_ = 0;
size_t line_ = 1;
size_t column_ = 1;
void advanceChar() {
if (pos_ < source_.size()) {
if (source_[pos_] == '\n') {
line_++;
column_ = 1;
} else {
column_++;
}
pos_++;
}
}
void skipWhitespaceAndComments() {
while (pos_ < source_.size()) {
char const c = source_[pos_];
if (std::isspace(static_cast<unsigned char>(c)) != 0) {
advanceChar();
} else if (c == '%') {
while (pos_ < source_.size() && source_[pos_] != '\n') {
advanceChar();
}
} else {
break;
}
}
}
};
class TptpParser {
public:
explicit TptpParser(std::string_view input, TermBank& bank, SymbolTable& symbols)
: lexer_(input), bank_(bank), symbols_(symbols) {
advance();
}
void parseProblem(ParsedProblem& problem) {
while (current_token_.type_ != TokenType::kEOF) {
if (current_token_.type_ == TokenType::kWord && current_token_.value_ == "cnf") {
advance();
expect(TokenType::kLParen);
AnnotatedFormula af;
af.is_cnf_ = true;
af.name_ = std::string(current_token_.value_);
expect(TokenType::kWord);
expect(TokenType::kComma);
af.role_ = parseRole(current_token_.value_);
expect(TokenType::kWord);
expect(TokenType::kComma);
Clause clause;
clause.literals_.push_back(parseLiteral());
while (current_token_.type_ == TokenType::kOr) {
advance();
clause.literals_.push_back(parseLiteral());
}
af.clauses_.push_back(std::move(clause));
expect(TokenType::kRParen);
expect(TokenType::kDot);
problem.formulas_.push_back(std::move(af));
variable_cache_.clear();
} else if (current_token_.value_ == "fof") {
throw std::runtime_error("FOF parsing not implemented. Line " +
std::to_string(current_token_.line_));
} else {
throw std::runtime_error("Unknown declaration: '" +
std::string(current_token_.value_) + "'");
}
}
}
private:
TptpLexer lexer_;
TermBank& bank_;
SymbolTable& symbols_;
Token current_token_{};
std::vector<Token> tokens_;
std::unordered_map<std::string_view, SymbolId> variable_cache_;
void advance() { current_token_ = lexer_.nextToken(); }
void expect(TokenType type) {
if (current_token_.type_ != type) {
throw std::runtime_error("Parse error at line " + std::to_string(current_token_.line_) +
", col " + std::to_string(current_token_.column_) +
": unexpected token '" + std::string(current_token_.value_) +
"'");
}
advance();
}
TermId parseTerm() {
if (current_token_.type_ != TokenType::kWord) {
throw std::runtime_error("Expected variable or function name at line " +
std::to_string(current_token_.line_) + ", col " +
std::to_string(current_token_.column_) +
": unexpected token '" + std::string(current_token_.value_) +
"'");
}
std::string_view const name = current_token_.value_;
bool const is_variable = std::isupper(static_cast<unsigned char>(name[0])) != 0;
advance();
if (is_variable) {
// Check cache first — same variable name in one clause → same TermId
auto it = variable_cache_.find(name);
if (it != variable_cache_.end()) {
return it->second;
}
SymbolId const sym = symbols_.intern(name, SymbolKind::kVariable);
TermId const var_id = bank_.makeVar(sym);
variable_cache_[name] = var_id;
return var_id;
}
// Check if this is a function/predicate (has args) or a constant (no parens)
if (current_token_.type_ == TokenType::kLParen) {
advance(); // consume '('
std::vector<TermId> args;
if (current_token_.type_ != TokenType::kRParen) {
args.push_back(parseTerm());
while (current_token_.type_ == TokenType::kComma) {
advance();
args.push_back(parseTerm());
}
}
expect(TokenType::kRParen);
const SymbolId sym = symbols_.intern(name, SymbolKind::kFunction,
static_cast<uint16_t>(args.size()));
return bank_.makeTerm(sym, args);
}
// Constant: no parentheses (e.g., a, b, h0, s0)
const SymbolId sym = symbols_.intern(name, SymbolKind::kConstant, 0);
return bank_.makeTerm(sym, {});
}
Literal parseLiteral() {
bool is_positive = true;
if (current_token_.type_ == TokenType::kNot) {
is_positive = false;
advance();
}
TermId const term = parseTerm();
return Literal{.atom_ = term, .is_positive_ = is_positive};
}
};
ParsedProblem parseTptpString(const std::string& input, TermBank& bank, SymbolTable& symbols) {
ParsedProblem problem;
TptpParser parser(input, bank, symbols);
parser.parseProblem(problem);
return problem;
}
ParsedProblem parseTptpFile(const std::string& filename, TermBank& bank, SymbolTable& symbols) {
std::ifstream file(filename, std::ios::binary);
if (!file.is_open()) {
throw std::runtime_error("Failed to open file: " + filename);
}
std::string const content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
ParsedProblem problem = parseTptpString(content, bank, symbols);
problem.source_file_ = filename;
return problem;
}
std::vector<Clause> prepareForProving(ParsedProblem& problem, TermBank& /*bank*/,
SymbolTable& /*symbols*/) {
std::vector<Clause> final_clauses;
for (auto& af : problem.formulas_) {
if (af.is_cnf_) {
for (auto& clause : af.clauses_) {
clause.rule_ = InferenceRule::kInput;
clause.parent1_ = kInvalidId;
clause.parent2_ = kInvalidId;
final_clauses.push_back(std::move(clause));
}
}
}
return final_clauses;
}
} // namespace atp
|