dReal4
driver.h
1 #include <istream>
2 #include <string>
3 #include <vector>
4 
5 #include "dreal/smt2/location.hh"
6 #include "dreal/smt2/scanner.h"
7 #include "dreal/smt2/sort.h"
8 #include "dreal/smt2/term.h"
9 #include "dreal/solver/context.h"
10 #include "dreal/util/scoped_unordered_map.h"
11 
12 namespace dreal {
13 
15  public:
16  FunctionDefinition(std::vector<Variable> parameters, Sort return_type,
17  Term body);
18 
19  Term operator()(const std::vector<Term>& arguments) const;
20 
21  private:
22  std::vector<Variable> parameters_;
23  Sort return_type_;
24  Term body_;
25 };
26 
27 /** The Smt2Driver class brings together all components. It creates an
28  * instance of the Parser and Scanner classes and connects them. Then
29  * the input stream is fed into the scanner object and the parser gets
30  * it's token sequence. Furthermore the driver object is available in
31  * the grammar rules as a parameter. Therefore the driver class
32  * contains a reference to the structure into which the parsed data is
33  * saved. */
34 class Smt2Driver {
35  public:
36  /// construct a new parser driver context
37  Smt2Driver() = default;
38  explicit Smt2Driver(Context context);
39 
40  /** Invoke the scanner and parser for a stream.
41  * @param in input stream
42  * @param sname stream name for error messages
43  * @return true if successfully parsed
44  */
45  bool parse_stream(std::istream& in,
46  const std::string& sname = "stream input");
47 
48  /** Invoke the scanner and parser on an input string.
49  * @param input input string
50  * @param sname stream name for error messages
51  * @return true if successfully parsed
52  */
53  bool parse_string(const std::string& input,
54  const std::string& sname = "string stream");
55 
56  /** Invoke the scanner and parser on a file. Use parse_stream with a
57  * std::ifstream if detection of file reading errors is required.
58  * @param filename input file name
59  * @return true if successfully parsed
60  */
61  bool parse_file(const std::string& filename);
62 
63  // To demonstrate pure handling of parse errors, instead of
64  // simply dumping them on the standard error output, we will pass
65  // them to the driver using the following two member functions.
66 
67  /** Error handling with associated line number. This can be modified to
68  * output the error e.g. to a dialog box. */
69  static void error(const location& l, const std::string& m);
70 
71  /** General error handling. This can be modified to output the error
72  * e.g. to a dialog box. */
73  static void error(const std::string& m);
74 
75  /// Calls context_.CheckSat() and print proper output messages to cout.
76  void CheckSat();
77 
78  /// Register a variable with name @p name and sort @p s in the scope. Note
79  /// that it does not declare the variable in the context.
80  Variable RegisterVariable(const std::string& name, Sort sort);
81 
82  /// Declare a variable with name @p name and sort @p sort.
83  Variable DeclareVariable(const std::string& name, Sort sort);
84 
85  /// Declare a variable with name @p name and sort @p sort which is bounded by
86  /// an interval `[lb, ub]`.
87  void DeclareVariable(const std::string& name, Sort sort, const Term& lb,
88  const Term& ub);
89 
90  /// Declare a new variable with label @p name that is globally unique and
91  /// cannot occur in an SMT-LIBv2 file.
92  Variable DeclareLocalVariable(const std::string& name, Sort sort);
93 
94  /// Handles define-fun.
95  void DefineFun(const std::string& name,
96  const std::vector<Variable>& parameters, Sort return_type,
97  const Term& body);
98 
99  /// Returns a representation of a model computed by the solver in
100  /// response to an invocation of the check-sat.
101  void GetModel() const;
102 
103  /// `GetValue([t1, t2, ..., tn])` returns a list of values [v1, v2,
104  /// ..., vn] where v_i is equivalent to t_i in the current model.
105  void GetValue(const std::vector<Term>& term_list) const;
106 
107  /// Handles `(get-option key)`.
108  void GetOption(const std::string& key) const;
109 
110  /// Returns a variable associated with a name @p name.
111  ///
112  /// @throws if no variable is associated with @p name.
113  const Variable& lookup_variable(const std::string& name);
114 
115  void PushScope() {
116  scope_.push();
117  function_definition_map_.push();
118  }
119 
120  void PopScope() {
121  function_definition_map_.pop();
122  scope_.pop();
123  }
124 
125  Term LookupFunction(const std::string& name,
126  const std::vector<Term>& arguments);
127 
128  static Variable ParseVariableSort(const std::string& name, Sort s);
129 
130  std::string MakeUniqueName(const std::string& name);
131 
132  bool trace_scanning() const { return trace_scanning_; }
133  void set_trace_scanning(bool b) { trace_scanning_ = b; }
134 
135  bool trace_parsing() const { return trace_parsing_; }
136  void set_trace_parsing(bool b) { trace_parsing_ = b; }
137 
138  Context& mutable_context() { return context_; }
139 
140  std::string& mutable_streamname() { return streamname_; }
141 
142  /** Pointer to the current scanenr instance, this is used to connect the
143  * parser to the scanner. It is used in the yylex macro. */
144  Smt2Scanner* scanner{nullptr};
145 
146  /// Eliminate Boolean variables in @p vars, `[b_1, ... b_n]`, from
147  /// @p f by constructing `f[b ↦ true] ∧ f[b ↦ false]`. This is used
148  /// in handling `forall` terms.
149  static Formula EliminateBooleanVariables(const Variables& vars,
150  const Formula& f);
151 
152  private:
153  /// enable debug output in the flex scanner
154  bool trace_scanning_{false};
155 
156  /// enable debug output in the bison parser
157  bool trace_parsing_{false};
158 
159  /** Scoped map from a string to a corresponding Variable. */
161 
162  /** Scoped map from a string to a corresponding Variable. */
164 
165  /// Sequential value concatenated to names to make them unique.
166  int64_t nextUniqueId_{};
167 
168  /// stream name (file or input stream) used for error messages.
169  std::string streamname_;
170 
171  /** The context filled during parsing of the expressions. */
172  Context context_;
173 };
174 
175 } // namespace dreal
Context class that holds a set of constraints and provide Assert/Push/Pop/CheckSat functionalities...
Definition: context.h:23
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
The Smt2Driver class brings together all components.
Definition: driver.h:34
Represents a symbolic variable.
Definition: symbolic_variable.h:16
Backtrackable map.
Definition: scoped_unordered_map.h:19
Definition: term.h:10
Definition: driver.h:14
Smt2Scanner is a derived class to add some extra function to the scanner class.
Definition: scanner.h:38
Represents a set of variables.
Definition: symbolic_variables.h:25
Represents a symbolic form of a first-order logic formula.
Definition: symbolic_formula.h:101