GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/parser/tptp/tptp.h Lines: 5 7 71.4 %
Date: 2021-08-14 Branches: 0 0 0.0 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Andrew Reynolds, Francois Bobot, Haniel Barbosa
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 * in the top-level source directory and their institutional affiliations.
9
 * All rights reserved.  See the file COPYING in the top-level source
10
 * directory for licensing information.
11
 * ****************************************************************************
12
 *
13
 * Definition of TPTP parser.
14
 */
15
16
#include "cvc5parser_private.h"
17
#include "parser/antlr_input.h"  // Needs to go first.
18
19
#ifndef CVC5__PARSER__TPTP_H
20
#define CVC5__PARSER__TPTP_H
21
22
#include <unordered_map>
23
#include <unordered_set>
24
25
#include "api/cpp/cvc5.h"
26
#include "parser/parse_op.h"
27
#include "parser/parser.h"
28
#include "util/hash.h"
29
30
namespace cvc5 {
31
32
class Command;
33
34
namespace api {
35
class Solver;
36
}
37
38
namespace parser {
39
40
class Tptp : public Parser {
41
 private:
42
  friend class ParserBuilder;
43
 public:
44
6538
  bool cnf() const { return d_cnf; }
45
1311
  void setCnf(bool cnf) { d_cnf = cnf; }
46
47
154
  bool fof() const { return d_fof; }
48
1311
  void setFof(bool fof) { d_fof = fof; }
49
50
  bool hol() const;
51
  void setHol();
52
53
  void forceLogic(const std::string& logic) override;
54
55
  void addFreeVar(api::Term var);
56
  std::vector<api::Term> getFreeVar();
57
58
  api::Term convertRatToUnsorted(api::Term expr);
59
  /**
60
   * Returns a free constant corresponding to the string str. We ensure that
61
   * these constants are one-to-one with str. We assert that all these free
62
   * constants are pairwise distinct before issuing satisfiability queries.
63
   */
64
  api::Term convertStrToUnsorted(std::string str);
65
66
  // CNF and FOF are unsorted so we define this common type.
67
  // This is also the api::Sort of $i in TFF.
68
  api::Sort d_unsorted;
69
70
  enum Theory {
71
    THEORY_CORE,
72
  };/* enum Theory */
73
74
  enum FormulaRole {
75
    FR_AXIOM,
76
    FR_HYPOTHESIS,
77
    FR_DEFINITION,
78
    FR_ASSUMPTION,
79
    FR_LEMMA,
80
    FR_THEOREM,
81
    FR_CONJECTURE,
82
    FR_NEGATED_CONJECTURE,
83
    FR_UNKNOWN,
84
    FR_PLAIN,
85
    FR_FI_DOMAIN,
86
    FR_FI_FUNCTORS,
87
    FR_FI_PREDICATES,
88
    FR_TYPE,
89
  };/* enum FormulaRole */
90
91
41
  bool hasConjecture() const { return d_hasConjecture; }
92
93
 protected:
94
  Tptp(api::Solver* solver,
95
       SymbolManager* sm,
96
       bool strictMode = false,
97
       bool parseOnly = false);
98
99
 public:
100
  ~Tptp();
101
  /**
102
   * Add theory symbols to the parser state.
103
   *
104
   * @param theory the theory to open (e.g., Core, Ints)
105
   */
106
  void addTheory(Theory theory);
107
108
  /** creates a lambda abstraction around application of given kind
109
   *
110
   * Given a kind k and type argType = t1...tn -> t, creates a lambda
111
   * expression
112
   *  (lambda x1:t1,...,xn:tn . (k x1 ... xn)) : t
113
   */
114
  api::Term mkLambdaWrapper(api::Kind k, api::Sort argType);
115
116
  /** get assertion expression, based on the formula role.
117
  * expr should have Boolean type.
118
  * This returns the expression that should be asserted, given the formula role fr.
119
  * For example, if the role is "conjecture", then the return value is the negation of expr.
120
  */
121
  api::Term getAssertionExpr(FormulaRole fr, api::Term expr);
122
123
  /** get assertion for distinct constants
124
   *
125
   * This returns a node of the form distinct( k1, ..., kn ) where k1, ..., kn
126
   * are the distinct constants introduced by this parser (see
127
   * convertStrToUnsorted) if n>1, or null otherwise.
128
   */
129
  api::Term getAssertionDistinctConstants();
130
131
  /** returns the appropriate AssertCommand, given a role, expression expr to
132
   * assert, and information about the assertion. The assertion expr is
133
   * literally what should be asserted (it is already been processed with
134
   * getAssertionExpr above). This may set a flag in the parser to mark
135
   * that we have asserted a conjecture.
136
   */
137
  Command* makeAssertCommand(FormulaRole fr, api::Term expr, bool cnf);
138
139
  /** Ugly hack because I don't know how to return an expression from a
140
      token */
141
  api::Term d_tmp_expr;
142
143
  /** Push a new stream in the lexer. When EOF is reached the previous stream
144
      is reused */
145
  void includeFile(std::string fileName);
146
147
  /** Check a TPTP let binding for well-formedness. */
148
  void checkLetBinding(const std::vector<api::Term>& bvlist,
149
                       api::Term lhs,
150
                       api::Term rhs,
151
                       bool formula);
152
  /**
153
   * This converts a ParseOp to expression, assuming it is a standalone term.
154
   *
155
   * There are three cases in TPTP: either p already has an expression, in which
156
   * case this function just returns it, or p has just a name or a builtin kind.
157
   */
158
  api::Term parseOpToExpr(ParseOp& p);
159
  /**
160
   * Apply parse operator to list of arguments, and return the resulting
161
   * expression.
162
   *
163
   * args must not be empty (otherwise the above method should have been
164
   * called).
165
   *
166
   * There are three cases in TPTP: either p already has an expression, in which
167
   * case this function just applies it to the arguments, or p has
168
   * just a name or a builtin kind, in which case the respective operator is
169
   * built.
170
   *
171
   * Note that the case of uninterpreted functions in TPTP this need not have
172
   * been previously declared, which leads to a more convoluted processing than
173
   * what is necessary in parsing SMT-LIB.
174
   */
175
  api::Term applyParseOp(ParseOp& p, std::vector<api::Term>& args);
176
  /**
177
   * Make decimal, returns a real corresponding to string ( snum "." sden ),
178
   * negated if pos is false, having exponent exp, negated exponent if posE is
179
   * false.
180
   */
181
  api::Term mkDecimal(
182
      std::string& snum, std::string& sden, bool pos, size_t exp, bool posE);
183
184
 private:
185
  void addArithmeticOperators();
186
187
  // In CNF variable are implicitly binded
188
  // d_freevar collect them
189
  std::vector<api::Term> d_freeVar;
190
  api::Term d_rtu_op;
191
  api::Term d_stu_op;
192
  api::Term d_utr_op;
193
  api::Term d_uts_op;
194
  // The set of expression that already have a bridge
195
  std::unordered_set<api::Term> d_r_converted;
196
  std::unordered_map<std::string, api::Term> d_distinct_objects;
197
198
  std::vector< pANTLR3_INPUT_STREAM > d_in_created;
199
200
  // TPTP directory where to find includes;
201
  // empty if none could be determined
202
  std::string d_tptpDir;
203
204
  // the null expression
205
  api::Term d_nullExpr;
206
207
  // hack to make output SZS ontology-compliant
208
  bool d_hasConjecture;
209
210
  bool d_cnf; // in a cnf formula
211
  bool d_fof; // in an fof formula
212
  bool d_hol;  // in a thf formula
213
};/* class Tptp */
214
215
216
namespace tptp {
217
/**
218
 * Just exists to provide the uintptr_t constructor that ANTLR
219
 * requires.
220
 */
221
struct myExpr : public cvc5::api::Term
222
{
223
  myExpr() : cvc5::api::Term() {}
224
  myExpr(void*) : cvc5::api::Term() {}
225
  myExpr(const cvc5::api::Term& e) : cvc5::api::Term(e) {}
226
  myExpr(const myExpr& e) : cvc5::api::Term(e) {}
227
}; /* struct myExpr*/
228
229
enum NonAssoc {
230
  NA_IFF,
231
  NA_IMPLIES,
232
  NA_REVIMPLIES,
233
  NA_REVIFF,
234
  NA_REVOR,
235
  NA_REVAND,
236
};
237
238
}  // namespace tptp
239
240
}  // namespace parser
241
}  // namespace cvc5
242
243
#endif /* CVC5__PARSER__TPTP_INPUT_H */