GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/parser/tptp/tptp.h Lines: 5 7 71.4 %
Date: 2021-05-22 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
6579
  bool cnf() const { return d_cnf; }
45
1319
  void setCnf(bool cnf) { d_cnf = cnf; }
46
47
154
  bool fof() const { return d_fof; }
48
1319
  void setFof(bool fof) { d_fof = fof; }
49
50
  void forceLogic(const std::string& logic) override;
51
52
  void addFreeVar(api::Term var);
53
  std::vector<api::Term> getFreeVar();
54
55
  api::Term convertRatToUnsorted(api::Term expr);
56
  /**
57
   * Returns a free constant corresponding to the string str. We ensure that
58
   * these constants are one-to-one with str. We assert that all these free
59
   * constants are pairwise distinct before issuing satisfiability queries.
60
   */
61
  api::Term convertStrToUnsorted(std::string str);
62
63
  // CNF and FOF are unsorted so we define this common type.
64
  // This is also the api::Sort of $i in TFF.
65
  api::Sort d_unsorted;
66
67
  enum Theory {
68
    THEORY_CORE,
69
  };/* enum Theory */
70
71
  enum FormulaRole {
72
    FR_AXIOM,
73
    FR_HYPOTHESIS,
74
    FR_DEFINITION,
75
    FR_ASSUMPTION,
76
    FR_LEMMA,
77
    FR_THEOREM,
78
    FR_CONJECTURE,
79
    FR_NEGATED_CONJECTURE,
80
    FR_UNKNOWN,
81
    FR_PLAIN,
82
    FR_FI_DOMAIN,
83
    FR_FI_FUNCTORS,
84
    FR_FI_PREDICATES,
85
    FR_TYPE,
86
  };/* enum FormulaRole */
87
88
43
  bool hasConjecture() const { return d_hasConjecture; }
89
90
 protected:
91
  Tptp(api::Solver* solver,
92
       SymbolManager* sm,
93
       bool strictMode = false,
94
       bool parseOnly = false);
95
96
 public:
97
  ~Tptp();
98
  /**
99
   * Add theory symbols to the parser state.
100
   *
101
   * @param theory the theory to open (e.g., Core, Ints)
102
   */
103
  void addTheory(Theory theory);
104
105
  /** creates a lambda abstraction around application of given kind
106
   *
107
   * Given a kind k and type argType = t1...tn -> t, creates a lambda
108
   * expression
109
   *  (lambda x1:t1,...,xn:tn . (k x1 ... xn)) : t
110
   */
111
  api::Term mkLambdaWrapper(api::Kind k, api::Sort argType);
112
113
  /** get assertion expression, based on the formula role.
114
  * expr should have Boolean type.
115
  * This returns the expression that should be asserted, given the formula role fr.
116
  * For example, if the role is "conjecture", then the return value is the negation of expr.
117
  */
118
  api::Term getAssertionExpr(FormulaRole fr, api::Term expr);
119
120
  /** get assertion for distinct constants
121
   *
122
   * This returns a node of the form distinct( k1, ..., kn ) where k1, ..., kn
123
   * are the distinct constants introduced by this parser (see
124
   * convertStrToUnsorted) if n>1, or null otherwise.
125
   */
126
  api::Term getAssertionDistinctConstants();
127
128
  /** returns the appropriate AssertCommand, given a role, expression expr to
129
   * assert, and information about the assertion. The assertion expr is
130
   * literally what should be asserted (it is already been processed with
131
   * getAssertionExpr above). This may set a flag in the parser to mark
132
   * that we have asserted a conjecture.
133
   */
134
  Command* makeAssertCommand(FormulaRole fr,
135
                             api::Term expr,
136
                             bool cnf,
137
                             bool inUnsatCore);
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
};/* class Tptp */
213
214
215
namespace tptp {
216
/**
217
 * Just exists to provide the uintptr_t constructor that ANTLR
218
 * requires.
219
 */
220
struct myExpr : public cvc5::api::Term
221
{
222
  myExpr() : cvc5::api::Term() {}
223
  myExpr(void*) : cvc5::api::Term() {}
224
  myExpr(const cvc5::api::Term& e) : cvc5::api::Term(e) {}
225
  myExpr(const myExpr& e) : cvc5::api::Term(e) {}
226
}; /* struct myExpr*/
227
228
enum NonAssoc {
229
  NA_IFF,
230
  NA_IMPLIES,
231
  NA_REVIMPLIES,
232
  NA_REVIFF,
233
  NA_REVOR,
234
  NA_REVAND,
235
};
236
237
}  // namespace tptp
238
239
}  // namespace parser
240
}  // namespace cvc5
241
242
#endif /* CVC5__PARSER__TPTP_INPUT_H */