dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
Driver.cpp
1
7#include "dlinear/parser/vnnlib/Driver.h"
8
9#include <iostream>
10#include <sstream>
11
12#include "dlinear/symbolic/ExpressionEvaluator.h"
13#include "dlinear/symbolic/PrefixPrinter.h"
14#include "dlinear/util/exception.h"
15#include "dlinear/util/logging.h"
16
17namespace dlinear::vnnlib {
18
19VnnlibDriver::VnnlibDriver(Context &context) : Driver{context, "VnnlibDriver"} {
20 for (const Variable &var : context_.box().variables()) scope_variables_.insert(var.get_name(), var);
21}
22
23bool VnnlibDriver::ParseStreamCore(std::istream &in) {
27
28 VnnlibParser parser(*this);
29 parser.set_debug_level(debug_parsing_);
30 const bool res = parser.parse() == 0;
31 scanner_ = nullptr;
32 return res;
33}
34
35void VnnlibDriver::error(const location &l, const std::string &m) { std::cerr << l << " : " << m << std::endl; }
36
38 Formula ret{f};
39 for (const Variable &b : vars) {
40 if (b.get_type() == Variable::Type::BOOLEAN) {
41 ret = ret.Substitute(b, Formula::True()) && ret.Substitute(b, Formula::False());
42 }
43 }
44 return ret;
45}
46
47void VnnlibDriver::DefineFun(const std::string &name, const std::vector<Variable> &parameters, Sort return_type,
48 const Term &body) {
49 FunctionDefinition func{parameters, return_type, body};
50 scope_functions_.insert(name, func);
51}
52
53Variable VnnlibDriver::RegisterVariable(const std::string &name, const Sort sort) {
54 auto it = scope_variables_.find(name);
55 if (it != scope_variables_.cend()) return it->second;
56 const Variable v{name, SortToType(sort)};
57 scope_variables_.insert(v.get_name(), v);
58 return v;
59}
60
61Variable VnnlibDriver::DeclareVariable(const std::string &name, const Sort sort) {
62 Variable v{RegisterVariable(name, sort)};
64 return v;
65}
66
67void VnnlibDriver::DeclareVariable(const std::string &name, const Sort sort, const Term &lb, const Term &ub) {
68 const Variable v{RegisterVariable(name, sort)};
70}
71
72std::string VnnlibDriver::MakeUniqueName(const std::string &name) {
73 std::stringstream oss;
74 // The \ character ensures that the name cannot occur in an SMT-LIBv2 file.
75 oss << "L" << nextUniqueId_++ << "\\" << name;
76 return oss.str();
77}
78
79Variable VnnlibDriver::DeclareLocalVariable(const std::string &name, const Sort sort) {
80 const Variable v{MakeUniqueName(name), SortToType(sort)};
81 scope_variables_.insert(name, v); // v is not inserted under its own name.
82 context_.DeclareVariable(v, false /* This local variable is not a model variable. */);
83 return v;
84}
85
86void VnnlibDriver::GetValue(const std::vector<Term> &term_list) const {
87 const Box &box{context_.model()};
88 if (!context_.config().silent()) std::cout << "(\n" << std::endl;
89 for (const auto &term : term_list) {
90 std::string term_str;
91 std::string value_str;
92 std::stringstream ss;
93 PrefixPrinter pp{ss};
94
95 switch (term.type()) {
96 case Term::Type::EXPRESSION: {
97 const Expression &e{term.expression()};
98 const ExpressionEvaluator evaluator{e, context_.config()};
99 pp.Print(e);
100 term_str = ss.str();
101 const Interval iv{ExpressionEvaluator(term.expression(), context_.config())(box)};
102 value_str = (std::stringstream{} << iv).str();
103 break;
104 }
105 case Term::Type::FORMULA: {
106 const Formula &f{term.formula()};
107 pp.Print(f);
108 term_str = ss.str();
109 if (is_variable(f)) {
110 value_str = box[get_variable(f)].ub() == 1 ? "true" : "false";
111 } else {
112 DLINEAR_RUNTIME_ERROR_FMT("get-value does not handle a compound formula {}.", term_str);
113 }
114 break;
115 }
116 }
117 if (!context_.config().silent()) std::cout << "\t(" << term_str << " " << value_str << " )\n";
118 }
119 if (!context_.config().silent()) std::cout << ")" << std::endl;
120}
121
122std::variant<const Expression *, const Variable *> VnnlibDriver::LookupDefinedName(const std::string &name) const {
123 const auto it = scope_variables_.find(name);
124 if (it != scope_variables_.cend()) return {&it->second};
125 const auto it2 = scope_constants_.find(name);
126 if (it2 != scope_constants_.cend()) return {&it2->second};
127 DLINEAR_OUT_OF_RANGE_FMT("{} is an undeclared name.", name);
128}
129
130const Expression &VnnlibDriver::LookupConstant(const std::string &name) const {
131 const auto it = scope_constants_.find(name);
132 if (it == scope_constants_.cend()) DLINEAR_OUT_OF_RANGE_FMT("{} is an undeclared constant.", name);
133 return it->second;
134}
135
136const Variable &VnnlibDriver::LookupVariable(const std::string &name) const {
137 const auto it = scope_variables_.find(name);
138 if (it == scope_variables_.cend()) DLINEAR_OUT_OF_RANGE_FMT("{} is an undeclared variable.", name);
139 return it->second;
140}
141
142Term VnnlibDriver::LookupFunction(const std::string &name, const std::vector<Term> &arguments) const {
143 const auto it = scope_functions_.find(name);
144 if (it == scope_functions_.end()) DLINEAR_OUT_OF_RANGE_FMT("Function {} is not defined.", name);
145 return it->second(arguments);
146}
147
148void VnnlibDriver::DefineLocalConstant(const std::string &name, const Expression &value) {
149 DLINEAR_ASSERT(is_constant(value), "Value must be a constant expression.");
150 scope_constants_.insert(name, value);
151}
152
153} // namespace dlinear::vnnlib
Collection of variables with associated intervals.
Definition Box.h:31
const std::vector< Variable > & variables() const
Get read-only access to the variables of the box.
Definition Box.cpp:113
const bool & silent() const
Get read-write access to the silent parameter of the configuration.
Definition Config.h:204
Context class that holds the set of constraints and provide Assert/Push/Pop/CheckSat functionalities.
Definition Context.h:31
const Config & config() const
Get read-only access to the configuration of the context.
Definition Context.cpp:65
void DeclareVariable(const Variable &v, bool is_model_variable=true)
Declare a variable v.
Definition Context.cpp:31
const Box & model() const
Get a representation of a model computed by the solver in response to the last invocation of the chec...
Definition Context.cpp:67
const Box & box() const
Get the current active box from the top of the stack of boxes.
Definition Context.cpp:66
The Driver is the base class for all the parsers.
Definition Driver.h:26
bool debug_scanning_
Enable debug output in the flex scanner.
Definition Driver.h:168
bool debug_parsing_
Enable debug output in the bison parser.
Definition Driver.h:170
Context & context_
The context filled during parsing of the expressions.
Definition Driver.h:166
Evaluate an expression with a given box.
Print expressions and formulas in prefix-form.
Represents a symbolic form of an expression.
Represents a symbolic form of a first-order logic formula.
Formula Substitute(const Variable &var, const Expression &e) const
Returns a copy of this formula replacing all occurrences of var with e.
Represents a symbolic variable.
@ BOOLEAN
A BOOLEAN variable takes a bool value.
Represents a set of variables.
A new function defined by the user in the VNNLIB language using the define-fun command.
Generic term that can be either an expression or a formula.
Definition Term.h:34
const Expression & expression() const
Get read-only access to the expression of the term.
Definition Term.cpp:36
static Formula EliminateBooleanVariables(const Variables &vars, const Formula &f)
Eliminate Boolean variables vars from f.
Definition Driver.cpp:37
ScopedUnorderedMap< std::string, Expression > scope_constants_
Scoped map from a name to a constant.
Definition Driver.h:145
Variable DeclareLocalVariable(const std::string &name, Sort sort)
Declare a new variable with label name that is globally unique and cannot occur in an SMT-LIBv2 file.
Definition Driver.cpp:79
const Expression & LookupConstant(const std::string &name) const
Lookup a constant expression associated with a name name.
Definition Driver.cpp:130
VnnlibScanner * scanner()
Pointer to the current scanner instance, this is used to connect the parser to the scanner.
Definition Driver.h:140
int64_t nextUniqueId_
Sequential value concatenated to names to make them unique.
Definition Driver.h:149
bool ParseStreamCore(std::istream &in) override
Parse the stream.
Definition Driver.cpp:23
void DefineFun(const std::string &name, const std::vector< Variable > &parameters, Sort return_type, const Term &body)
Called by the define-fun command in the SMT-2 file.
Definition Driver.cpp:47
const Variable & LookupVariable(const std::string &name) const
Lookup a variable associated with a name name.
Definition Driver.cpp:136
Term LookupFunction(const std::string &name, const std::vector< Term > &arguments) const
Lookup a function with name name and run it with arguments, producing a new term.
Definition Driver.cpp:142
Variable DeclareVariable(const std::string &name, Sort sort)
Declare a variable with name name and sort sort.
Definition Driver.cpp:61
static void error(const location &l, const std::string &m)
Error handling with associated line number.
Definition Driver.cpp:35
ScopedUnorderedMap< std::string, Variable > scope_variables_
Scoped map from a name to a Variable.
Definition Driver.h:146
std::variant< const Expression *, const Variable * > LookupDefinedName(const std::string &name) const
Lookup a variable or constant expression associated with a name name.
Definition Driver.cpp:122
void DefineLocalConstant(const std::string &name, const Expression &value)
Define a constant within the current scope.
Definition Driver.cpp:148
VnnlibScanner * scanner_
The scanner producing the tokens for the parser.
Definition Driver.h:143
ScopedUnorderedMap< std::string, FunctionDefinition > scope_functions_
Scoped map from a name to a Function.
Definition Driver.h:147
Variable RegisterVariable(const std::string &name, Sort sort)
Register a variable with name name and sort s in the scope.
Definition Driver.cpp:53
VnnlibDriver(Context &context)
construct a new parser driver context
Definition Driver.cpp:19
void GetValue(const std::vector< Term > &term_list) const
Get the value of all the terms in term_list.
Definition Driver.cpp:86
VnnlibScanner is a derived class to add some extra function to the scanner class.
Definition scanner.h:41
void set_debug(bool b)
Enable debug output (via arg_yyout) if compiled into the scanner.
Namespace for the VNNLIB parser of the dlinear library.
Definition Driver.cpp:17
Sort
Sort of a term.
Definition Sort.h:18
Variable::Type SortToType(Sort sort)
Convert a sort to a variable type.
Definition Sort.cpp:43