18typedef int64_t Z3_SIGNED_INTEGER;
19typedef uint64_t Z3_UNSIGNED_INTEGER;
21Z3ExpressionAdapter::Z3ExpressionAdapter(storm::expressions::ExpressionManager& manager, z3::context& context)
22 :
manager(
manager), context(context), additionalAssertions(), variableToExpressionMapping() {
30 expressionCache.clear();
32 for (z3::expr
const& assertion : additionalAssertions) {
33 result = result && assertion;
35 additionalAssertions.clear();
36 return result.simplify();
42 auto const& variableExpressionPair = variableToExpressionMapping.find(variable);
43 if (variableExpressionPair == variableToExpressionMapping.end()) {
44 return createVariable(variable);
47 return variableExpressionPair->second;
51 auto const& declarationVariablePair = declarationToVariableMapping.find(z3Declaration);
52 STORM_LOG_ASSERT(declarationVariablePair != declarationToVariableMapping.end(),
"Unable to find declaration.");
53 return declarationVariablePair->second;
58 switch (expr.decl().decl_kind()) {
64 return this->translateExpression(expr.arg(0)) == this->translateExpression(expr.arg(1));
65 case Z3_OP_DISTINCT: {
66 unsigned args = expr.num_args();
67 STORM_LOG_THROW(args != 0, storm::exceptions::ExpressionEvaluationException,
68 "Failed to convert Z3 expression. DISTINCT (mutual != ) operator with 0-arity is assumed to be an error.");
73 for (
unsigned arg2 = 2; arg2 < args; ++arg2) {
74 retVal = retVal && (this->translateExpression(expr.arg(0)) != this->translateExpression(expr.arg(arg2)));
76 for (
unsigned arg1 = 1; arg1 < args; ++arg1) {
77 for (
unsigned arg2 = arg1 + 1; arg2 < args; ++arg2) {
78 retVal = retVal && (this->translateExpression(expr.arg(arg1)) != this->translateExpression(expr.arg(arg2)));
86 this->translateExpression(expr.arg(2)));
88 unsigned args = expr.num_args();
89 STORM_LOG_THROW(args != 0, storm::exceptions::ExpressionEvaluationException,
90 "Failed to convert Z3 expression. 0-ary AND is assumed to be an error.");
92 return this->translateExpression(expr.arg(0));
95 for (
unsigned i = 1;
i < args;
i++) {
96 retVal = retVal && this->translateExpression(expr.arg(i));
102 unsigned args = expr.num_args();
103 STORM_LOG_THROW(args != 0, storm::exceptions::ExpressionEvaluationException,
104 "Failed to convert Z3 expression. 0-ary OR is assumed to be an error.");
106 return this->translateExpression(expr.arg(0));
109 for (
unsigned i = 1;
i < args;
i++) {
110 retVal = retVal || this->translateExpression(expr.arg(i));
116 return storm::expressions::iff(this->translateExpression(expr.arg(0)), this->translateExpression(expr.arg(1)));
120 return !this->translateExpression(expr.arg(0));
124 return this->translateExpression(expr.arg(0)) <= this->translateExpression(expr.arg(1));
126 return this->translateExpression(expr.arg(0)) >= this->translateExpression(expr.arg(1));
128 return this->translateExpression(expr.arg(0)) < this->translateExpression(expr.arg(1));
130 return this->translateExpression(expr.arg(0)) > this->translateExpression(expr.arg(1));
134 std::vector<storm::expressions::Expression> operands;
135 operands.reserve(expr.num_args());
136 for (
unsigned i = 0;
i < expr.num_args(); ++
i) {
137 operands.push_back(this->translateExpression(expr.arg(i)));
139 STORM_LOG_THROW(!operands.empty(), storm::exceptions::ExpressionEvaluationException,
140 "Failed to convert Z3 expression. Encountered an addition or multiplication without arguments.");
144 return this->translateExpression(expr.arg(0)) - this->translateExpression(expr.arg(1));
146 return -this->translateExpression(expr.arg(0));
148 return this->translateExpression(expr.arg(0)) / this->translateExpression(expr.arg(1));
152 "Failed to convert Z3 expression. Integer division is not supported.");
155 if (expr.is_int() && expr.is_const()) {
156 Z3_SIGNED_INTEGER value;
157 if (Z3_get_numeral_int64(expr.ctx(), expr, &value)) {
160 STORM_LOG_THROW(
false, storm::exceptions::ExpressionEvaluationException,
161 "Failed to convert Z3 expression. Expression is constant integer and value does not fit into 64-bit integer.");
164 STORM_LOG_ASSERT(expr.is_real() && expr.is_const(),
"Cannot handle numerical expression.");
165 Z3_SIGNED_INTEGER num;
166 Z3_SIGNED_INTEGER den;
167 if (Z3_get_numeral_rational_int64(expr.ctx(), expr, &num, &den)) {
176 STORM_LOG_WARN(
"Interpreting algebraic numbers as rational numbers. This is not an exact computation.");
179 std::string(Z3_get_numeral_string(expr.ctx(), Z3_get_algebraic_number_lower(expr.ctx(), expr, 16)))));
180 case Z3_OP_UNINTERPRETED:
182 STORM_LOG_THROW(expr.is_const(), storm::exceptions::ExpressionEvaluationException,
183 "Failed to convert Z3 expression. Encountered non-constant uninterpreted function.");
184 return manager.getVariable(expr.decl().name().str()).getExpression();
186 STORM_LOG_THROW(
false, storm::exceptions::ExpressionEvaluationException,
187 "Failed to convert Z3 expression. Encountered unhandled Z3_decl_kind " << expr.decl().decl_kind() <<
".");
191 STORM_LOG_THROW(
false, storm::exceptions::ExpressionEvaluationException,
"Failed to convert Z3 expression. Encountered unknown expression type.");
196 auto cacheIt = expressionCache.find(&expression);
197 if (cacheIt != expressionCache.end()) {
198 return cacheIt->second;
201 z3::expr leftResult = boost::any_cast<z3::expr>(expression.
getFirstOperand()->accept(*
this, data));
202 z3::expr rightResult = boost::any_cast<z3::expr>(expression.
getSecondOperand()->accept(*
this, data));
204 z3::expr result(context);
207 result = leftResult && rightResult;
210 result = leftResult || rightResult;
213 result = z3::expr(context, Z3_mk_xor(context, leftResult, rightResult));
216 result = z3::expr(context, Z3_mk_implies(context, leftResult, rightResult));
219 result = z3::expr(context, Z3_mk_iff(context, leftResult, rightResult));
222 STORM_LOG_THROW(
false, storm::exceptions::ExpressionEvaluationException,
223 "Cannot evaluate expression: unknown boolean binary operator '" <<
static_cast<int>(expression.
getOperatorType())
224 <<
"' in expression " << expression <<
".");
227 expressionCache.emplace(&expression, result);
232 auto cacheIt = expressionCache.find(&expression);
233 if (cacheIt != expressionCache.end()) {
234 return cacheIt->second;
237 z3::expr leftResult = boost::any_cast<z3::expr>(expression.
getFirstOperand()->accept(*
this, data));
238 z3::expr rightResult = boost::any_cast<z3::expr>(expression.
getSecondOperand()->accept(*
this, data));
240 z3::expr result(context);
243 result = leftResult + rightResult;
246 result = leftResult - rightResult;
249 result = leftResult * rightResult;
252 if (leftResult.is_int() && rightResult.is_int()) {
253 leftResult = z3::expr(context, Z3_mk_int2real(context, leftResult));
255 result = leftResult / rightResult;
258 result =
ite(leftResult <= rightResult, leftResult, rightResult);
261 result =
ite(leftResult >= rightResult, leftResult, rightResult);
264 result = pw(leftResult, rightResult);
267 STORM_LOG_THROW(
false, storm::exceptions::ExpressionEvaluationException,
268 "Cannot evaluate expression: unknown numerical binary operator '" <<
static_cast<int>(expression.
getOperatorType())
269 <<
"' in expression " << expression <<
".");
272 expressionCache.emplace(&expression, result);
277 auto cacheIt = expressionCache.find(&expression);
278 if (cacheIt != expressionCache.end()) {
279 return cacheIt->second;
282 z3::expr leftResult = boost::any_cast<z3::expr>(expression.
getFirstOperand()->accept(*
this, data));
283 z3::expr rightResult = boost::any_cast<z3::expr>(expression.
getSecondOperand()->accept(*
this, data));
285 z3::expr result(context);
288 result = leftResult == rightResult;
291 result = leftResult != rightResult;
294 result = leftResult < rightResult;
297 result = leftResult <= rightResult;
300 result = leftResult > rightResult;
303 result = leftResult >= rightResult;
306 STORM_LOG_THROW(
false, storm::exceptions::ExpressionEvaluationException,
307 "Cannot evaluate expression: unknown boolean binary operator '" <<
static_cast<int>(expression.
getRelationType())
308 <<
"' in expression " << expression <<
".");
311 expressionCache.emplace(&expression, result);
316 auto cacheIt = expressionCache.find(&expression);
317 if (cacheIt != expressionCache.end()) {
318 return cacheIt->second;
321 z3::expr result = context.bool_val(expression.
getValue());
323 expressionCache.emplace(&expression, result);
328 auto cacheIt = expressionCache.find(&expression);
329 if (cacheIt != expressionCache.end()) {
330 return cacheIt->second;
333 std::stringstream fractionStream;
334 fractionStream << expression.
getValue();
335 z3::expr result = context.real_val(fractionStream.str().c_str());
337 expressionCache.emplace(&expression, result);
342 auto cacheIt = expressionCache.find(&expression);
343 if (cacheIt != expressionCache.end()) {
344 return cacheIt->second;
347 z3::expr result = context.int_val(
static_cast<Z3_SIGNED_INTEGER
>(expression.
getValue()));
349 expressionCache.emplace(&expression, result);
354 auto cacheIt = expressionCache.find(&expression);
355 if (cacheIt != expressionCache.end()) {
356 return cacheIt->second;
359 z3::expr result = boost::any_cast<z3::expr>(expression.
getOperand()->accept(*
this, data));
366 STORM_LOG_THROW(
false, storm::exceptions::ExpressionEvaluationException,
367 "Cannot evaluate expression: unknown boolean binary operator '" <<
static_cast<int>(expression.
getOperatorType())
368 <<
"' in expression " << expression <<
".");
371 expressionCache.emplace(&expression, result);
376 auto cacheIt = expressionCache.find(&expression);
377 if (cacheIt != expressionCache.end()) {
378 return cacheIt->second;
381 z3::expr result = boost::any_cast<z3::expr>(expression.
getOperand()->accept(*
this, data));
389 z3::expr floorVariable = context.int_const(freshAuxiliaryVariable.
getName().c_str());
390 additionalAssertions.push_back(z3::expr(context, Z3_mk_int2real(context, floorVariable)) <= result &&
391 result < (z3::expr(context, Z3_mk_int2real(context, floorVariable)) + 1));
392 result = floorVariable;
397 z3::expr ceilVariable = context.int_const(freshAuxiliaryVariable.
getName().c_str());
398 additionalAssertions.push_back(z3::expr(context, Z3_mk_int2real(context, ceilVariable)) - 1 < result &&
399 result <= z3::expr(context, Z3_mk_int2real(context, ceilVariable)));
400 result = ceilVariable;
404 STORM_LOG_THROW(
false, storm::exceptions::ExpressionEvaluationException,
405 "Cannot evaluate expression: unknown numerical unary operator '" <<
static_cast<int>(expression.
getOperatorType()) <<
"'.");
408 expressionCache.emplace(&expression, result);
413 auto cacheIt = expressionCache.find(&expression);
414 if (cacheIt != expressionCache.end()) {
415 return cacheIt->second;
418 z3::expr conditionResult = boost::any_cast<z3::expr>(expression.
getCondition()->accept(*
this, data));
419 z3::expr thenResult = boost::any_cast<z3::expr>(expression.
getThenExpression()->accept(*
this, data));
420 z3::expr elseResult = boost::any_cast<z3::expr>(expression.
getElseExpression()->accept(*
this, data));
421 z3::expr result = z3::expr(context, Z3_mk_ite(context, conditionResult, thenResult, elseResult));
423 expressionCache.emplace(&expression, result);
428 return this->translateExpression(expression.
getVariable());
432 z3::expr z3Variable(context);
434 z3Variable = context.bool_const(variable.
getName().c_str());
436 z3Variable = context.int_const(variable.
getName().c_str());
440 z3Variable = context.real_const(variable.
getName().c_str());
443 "Encountered variable '" << variable.
getName() <<
"' with unknown type while trying to create solver variables.");
445 variableToExpressionMapping.insert(std::make_pair(variable, z3Variable));
446 declarationToVariableMapping.insert(std::make_pair(z3Variable.decl(), variable));
virtual boost::any accept(ExpressionVisitor &visitor, boost::any const &data) const =0
Accepts the given visitor by calling its visit method.
OperatorType getOperatorType() const
Retrieves the operator associated with the expression.
std::shared_ptr< BaseExpression const > const & getSecondOperand() const
Retrieves the second operand of the expression.
std::shared_ptr< BaseExpression const > const & getFirstOperand() const
Retrieves the first operand of the expression.
OperatorType getOperatorType() const
Retrieves the operator associated with the expression.
RelationType getRelationType() const
Retrieves the relation associated with the expression.
bool getValue() const
Retrieves the value of the boolean literal.
BaseExpression const & getBaseExpression() const
Retrieves the base expression underlying this expression object.
ExpressionManager const & getManager() const
Retrieves the manager responsible for this expression.
std::shared_ptr< BaseExpression const > getElseExpression() const
Retrieves the else expression of the if-then-else expression.
std::shared_ptr< BaseExpression const > getCondition() const
Retrieves the condition expression of the if-then-else expression.
std::shared_ptr< BaseExpression const > getThenExpression() const
Retrieves the then expression of the if-then-else expression.
int_fast64_t getValue() const
Retrieves the value of the integer literal.
storm::RationalNumber getValue() const
Retrieves the value of the double literal.
bool isBooleanType() const
Checks whether this type is a boolean type.
std::size_t getWidth() const
Retrieves the bit width of the type, provided that it is a bitvector type.
bool isIntegerType() const
Checks whether this type is an integral type.
bool isRationalType() const
Checks whether this type is a rational type.
bool isBitVectorType() const
Checks whether this type is a bitvector type.
OperatorType getOperatorType() const
Retrieves the operator associated with this expression.
virtual std::shared_ptr< BaseExpression const > getOperand(uint_fast64_t operandIndex) const override
Retrieves the given operand from the expression.
OperatorType getOperatorType() const
Retrieves the operator associated with this expression.
Variable const & getVariable() const
Retrieves the variable associated with this expression.
ExpressionManager const & getManager() const
Retrieves the manager responsible for this variable.
Type const & getType() const
Retrieves the type of the variable.
std::string const & getName() const
Retrieves the name of the variable.
#define STORM_LOG_WARN(message)
#define STORM_LOG_THROW_UNCONDITIONALLY(exception, message)
#define STORM_LOG_ASSERT(cond, message)
#define STORM_LOG_THROW(cond, exception, message)
Expression ite(Expression const &condition, Expression const &thenExpression, Expression const &elseExpression)
Expression iff(Expression const &first, Expression const &second)
Expression sum(std::vector< storm::expressions::Expression > const &expressions)
Expression product(std::vector< storm::expressions::Expression > const &expressions)
Expression xclusiveor(Expression const &first, Expression const &second)
Expression implies(Expression const &first, Expression const &second)
SettingsManager const & manager()
Retrieves the settings manager.
TargetType convertNumber(SourceType const &number)
The contract of a DD backend.