GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/parser/tptp/tptp.h Lines: 5 7 71.4 %
Date: 2021-08-01 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,
138
                             api::Term expr,
139
                             bool cnf,
140
                             bool inUnsatCore);
141
142
  /** Ugly hack because I don't know how to return an expression from a
143
      token */
144
  api::Term d_tmp_expr;
145
146
  /** Push a new stream in the lexer. When EOF is reached the previous stream
147
      is reused */
148
  void includeFile(std::string fileName);
149
150
  /** Check a TPTP let binding for well-formedness. */
151
  void checkLetBinding(const std::vector<api::Term>& bvlist,
152
                       api::Term lhs,
153
                       api::Term rhs,
154
                       bool formula);
155
  /**
156
   * This converts a ParseOp to expression, assuming it is a standalone term.
157
   *
158
   * There are three cases in TPTP: either p already has an expression, in which
159
   * case this function just returns it, or p has just a name or a builtin kind.
160
   */
161
  api::Term parseOpToExpr(ParseOp& p);
162
  /**
163
   * Apply parse operator to list of arguments, and return the resulting
164
   * expression.
165
   *
166
   * args must not be empty (otherwise the above method should have been
167
   * called).
168
   *
169
   * There are three cases in TPTP: either p already has an expression, in which
170
   * case this function just applies it to the arguments, or p has
171
   * just a name or a builtin kind, in which case the respective operator is
172
   * built.
173
   *
174
   * Note that the case of uninterpreted functions in TPTP this need not have
175
   * been previously declared, which leads to a more convoluted processing than
176
   * what is necessary in parsing SMT-LIB.
177
   */
178
  api::Term applyParseOp(ParseOp& p, std::vector<api::Term>& args);
179
  /**
180
   * Make decimal, returns a real corresponding to string ( snum "." sden ),
181
   * negated if pos is false, having exponent exp, negated exponent if posE is
182
   * false.
183
   */
184
  api::Term mkDecimal(
185
      std::string& snum, std::string& sden, bool pos, size_t exp, bool posE);
186
187
 private:
188
  void addArithmeticOperators();
189
190
  // In CNF variable are implicitly binded
191
  // d_freevar collect them
192
  std::vector<api::Term> d_freeVar;
193
  api::Term d_rtu_op;
194
  api::Term d_stu_op;
195
  api::Term d_utr_op;
196
  api::Term d_uts_op;
197
  // The set of expression that already have a bridge
198
  std::unordered_set<api::Term> d_r_converted;
199
  std::unordered_map<std::string, api::Term> d_distinct_objects;
200
201
  std::vector< pANTLR3_INPUT_STREAM > d_in_created;
202
203
  // TPTP directory where to find includes;
204
  // empty if none could be determined
205
  std::string d_tptpDir;
206
207
  // the null expression
208
  api::Term d_nullExpr;
209
210
  // hack to make output SZS ontology-compliant
211
  bool d_hasConjecture;
212
213
  bool d_cnf; // in a cnf formula
214
  bool d_fof; // in an fof formula
215
  bool d_hol;  // in a thf formula
216
};/* class Tptp */
217
218
219
namespace tptp {
220
/**
221
 * Just exists to provide the uintptr_t constructor that ANTLR
222
 * requires.
223
 */
224
struct myExpr : public cvc5::api::Term
225
{
226
  myExpr() : cvc5::api::Term() {}
227
  myExpr(void*) : cvc5::api::Term() {}
228
  myExpr(const cvc5::api::Term& e) : cvc5::api::Term(e) {}
229
  myExpr(const myExpr& e) : cvc5::api::Term(e) {}
230
}; /* struct myExpr*/
231
232
enum NonAssoc {
233
  NA_IFF,
234
  NA_IMPLIES,
235
  NA_REVIMPLIES,
236
  NA_REVIFF,
237
  NA_REVOR,
238
  NA_REVAND,
239
};
240
241
}  // namespace tptp
242
243
}  // namespace parser
244
}  // namespace cvc5
245
246
#endif /* CVC5__PARSER__TPTP_INPUT_H */