Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
Expression.h
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <map>
5#include <memory>
6#include <set>
7#include <unordered_map>
8#include <vector>
9
10#include "storm-config.h"
14
15namespace storm {
16namespace expressions {
17// Foward-declare expression manager class.
19class Variable;
22
24 public:
25 friend class ExpressionManager;
26 friend class Variable;
27 template<typename MapType>
28 friend class SubstitutionVisitor;
29
30 friend Expression operator+(Expression const& first, Expression const& second);
31 friend Expression operator+(Expression const& first, int64_t second);
32 friend Expression operator+(int64_t first, Expression const& second);
33 friend Expression operator-(Expression const& first, Expression const& second);
34 friend Expression operator-(Expression const& first, int64_t second);
35 friend Expression operator-(int64_t first, Expression const& second);
36 friend Expression operator-(Expression const& first);
37 friend Expression operator*(Expression const& first, Expression const& second);
38 friend Expression operator/(Expression const& first, Expression const& second);
39 friend Expression operator%(Expression const& first, Expression const& second);
40 friend Expression operator&&(Expression const& first, Expression const& second);
41 friend Expression operator||(Expression const& first, Expression const& second);
42 friend Expression operator!(Expression const& first);
43 friend Expression operator==(Expression const& first, Expression const& second);
44 friend Expression operator!=(Expression const& first, Expression const& second);
45 friend Expression operator>(Expression const& first, Expression const& second);
46 friend Expression operator>=(Expression const& first, Expression const& second);
47 friend Expression operator<(Expression const& first, Expression const& second);
48 friend Expression operator<=(Expression const& first, Expression const& second);
49 friend Expression operator>(Expression const& first, int64_t second);
50 friend Expression operator>=(Expression const& first, int64_t second);
51 friend Expression operator<(Expression const& first, int64_t second);
52 friend Expression operator<=(Expression const& first, int64_t second);
53 friend Expression ite(Expression const& condition, Expression const& thenExpression, Expression const& elseExpression);
54 friend Expression implies(Expression const& first, Expression const& second);
55 friend Expression iff(Expression const& first, Expression const& second);
56 friend Expression xclusiveor(Expression const& first, Expression const& second);
57 friend Expression pow(Expression const& base, Expression const& exponent, bool allowIntegerType);
58 friend Expression abs(Expression const& first);
59 friend Expression truncate(Expression const& first);
60 friend Expression sign(Expression const& first);
61 friend Expression floor(Expression const& first);
62 friend Expression ceil(Expression const& first);
63 friend Expression round(Expression const& first);
64 friend Expression minimum(Expression const& first, Expression const& second);
65 friend Expression maximum(Expression const& first, Expression const& second);
66
67 Expression() = default;
69
75 Expression(Variable const& variable);
76
82 Expression(std::shared_ptr<BaseExpression const> const& expressionPtr);
83
84 // Instantiate constructors and assignments with their default implementations.
85 Expression(Expression const& other) = default;
86 Expression& operator=(Expression const& other) = default;
87 Expression(Expression&&) = default;
89
93 Expression changeManager(ExpressionManager const& newExpressionManager) const;
94
104 Expression substitute(std::map<Variable, Expression> const& variableToExpressionMap) const;
105
120 Expression substitute(std::unordered_map<Variable, Expression> const& variableToExpressionMap) const;
121
129 bool evaluateAsBool(Valuation const* valuation = nullptr) const;
130
138 int_fast64_t evaluateAsInt(Valuation const* valuation = nullptr) const;
139
147 double evaluateAsDouble(Valuation const* valuation = nullptr) const;
148
156 storm::RationalNumber evaluateAsRational() const;
157
163 Expression simplify() const;
164
171
179
185 bool isFunctionApplication() const;
186
192 uint_fast64_t getArity() const;
193
200 Expression getOperand(uint_fast64_t operandIndex) const;
201
208 std::string const& getIdentifier() const;
209
215 bool containsVariables() const;
216
222 bool isLiteral() const;
223
229 bool isVariable() const;
230
236 bool isTrue() const;
237
243 bool isFalse() const;
244
251 bool areSame(storm::expressions::Expression const& other) const;
252
259 bool isRelationalExpression() const;
260
266 bool isLinear() const;
267
273 std::set<storm::expressions::Variable> getVariables() const;
274
281 void gatherVariables(std::set<storm::expressions::Variable>& variables) const;
282
289 bool containsVariable(std::set<storm::expressions::Variable> const& variables) const;
290
298 bool containsVariableInITEGuard(std::set<storm::expressions::Variable> const& variables) const;
299
306 BaseExpression const& getBaseExpression() const;
307
313 std::shared_ptr<BaseExpression const> const& getBaseExpressionPointer() const;
314
320 ExpressionManager const& getManager() const;
321
327 Type const& getType() const;
328
334 bool hasNumericalType() const;
335
341 bool hasRationalType() const;
342
348 bool hasBooleanType() const;
349
355 bool hasIntegerType() const;
356
362 bool hasBitVectorType() const;
363
369 boost::any accept(ExpressionVisitor& visitor, boost::any const& data) const;
370
376 std::string toString() const;
377
381 bool isInitialized() const;
382
387
391 bool hasCompiledExpression() const;
392
396 void setCompiledExpression(std::shared_ptr<CompiledExpression> const& compiledExpression) const;
397
402
403 friend std::ostream& operator<<(std::ostream& stream, Expression const& expression);
404
405 private:
406 // A pointer to the underlying base expression.
407 std::shared_ptr<BaseExpression const> expressionPtr;
408
409 // A pointer to an associated compiled expression object (if any).
410 mutable std::shared_ptr<CompiledExpression> compiledExpression;
411};
412
413// Provide operator overloads to conveniently construct new expressions from other expressions.
414Expression operator+(Expression const& first, Expression const& second);
415Expression operator+(Expression const& first, int64_t second);
416Expression operator+(int64_t first, Expression const& second);
417Expression operator-(Expression const& first, Expression const& second);
418Expression operator-(Expression const& first, int64_t second);
419Expression operator-(int64_t first, Expression const& second);
420Expression operator-(Expression const& first);
421Expression operator*(Expression const& first, Expression const& second);
422Expression operator/(Expression const& first, Expression const& second);
423Expression operator&&(Expression const& first, Expression const& second);
424Expression operator||(Expression const& first, Expression const& second);
425Expression operator!(Expression const& first);
426Expression operator==(Expression const& first, Expression const& second);
427Expression operator!=(Expression const& first, Expression const& second);
428Expression operator>(Expression const& first, Expression const& second);
429Expression operator>=(Expression const& first, Expression const& second);
430Expression operator<(Expression const& first, Expression const& second);
431Expression operator<=(Expression const& first, Expression const& second);
432Expression operator>(Expression const& first, int64_t second);
433Expression operator>=(Expression const& first, int64_t second);
434Expression operator<(Expression const& first, int64_t second);
435Expression operator<=(Expression const& first, int64_t second);
436Expression ite(Expression const& condition, Expression const& thenExpression, Expression const& elseExpression);
437Expression implies(Expression const& first, Expression const& second);
438Expression iff(Expression const& first, Expression const& second);
439Expression xclusiveor(Expression const& first, Expression const& second);
440
449Expression pow(Expression const& base, Expression const& exponent, bool allowIntegerType = false);
450Expression abs(Expression const& first);
451Expression truncate(Expression const& first);
452Expression sign(Expression const& first);
453Expression floor(Expression const& first);
454Expression ceil(Expression const& first);
455Expression round(Expression const& first);
456Expression modulo(Expression const& first, Expression const& second);
457Expression logarithm(Expression const& first, Expression const& second);
458Expression cos(Expression const& first);
459Expression sin(Expression const& first);
460Expression minimum(Expression const& first, Expression const& second);
461Expression maximum(Expression const& first, Expression const& second);
462Expression atLeastOneOf(std::vector<storm::expressions::Expression> const& expressions);
463Expression atMostOneOf(std::vector<storm::expressions::Expression> const& expressions);
464Expression exactlyOneOf(std::vector<storm::expressions::Expression> const& expressions);
465Expression disjunction(std::vector<storm::expressions::Expression> const& expressions);
466Expression conjunction(std::vector<storm::expressions::Expression> const& expressions);
467Expression sum(std::vector<storm::expressions::Expression> const& expressions);
468Expression product(std::vector<storm::expressions::Expression> const& expressions);
469Expression apply(std::vector<storm::expressions::Expression> const& expressions,
470 std::function<Expression(Expression const&, Expression const&)> const& function);
471Expression applyAssociative(std::vector<storm::expressions::Expression> const& expressions,
472 std::function<Expression(Expression const&, Expression const&)> const& function);
473Expression makeBinaryRelationExpression(Expression const& lhs, Expression const& rhs, RelationType const& reltype);
474
476 bool operator()(Expression const& lhs, Expression const& rhs) const {
477 return std::less<BaseExpression const*>()(lhs.getBaseExpressionPointer().get(), rhs.getBaseExpressionPointer().get());
478 }
479};
480} // namespace expressions
481} // namespace storm
482
483namespace std {
484#ifndef STORM_WORKAROUND_LLVM_EXPRESSION_LESS_BUG
485template<>
486struct less<storm::expressions::Expression> {
490};
491#endif
492
493template<>
494struct hash<storm::expressions::Expression> {
496 return reinterpret_cast<size_t>(e.getBaseExpressionPointer().get());
497 }
498};
499
500template<>
501struct equal_to<storm::expressions::Expression> {
503 return e1.areSame(e2);
504 }
505};
506} // namespace std
507
508namespace storm {
509namespace expressions {
510#ifdef STORM_WORKAROUND_LLVM_EXPRESSION_LESS_BUG
512#else
513using ExpressionComparator = std::less<Expression>;
514#endif
515using ExpressionSet = std::set<Expression, ExpressionComparator>;
516template<typename Value>
517using ExpressionMap = std::map<Expression, Value, ExpressionComparator>;
518} // namespace expressions
519} // namespace storm
The base class of all expression classes.
friend Expression truncate(Expression const &first)
Expression simplify() const
Simplifies the expression according to some basic rules.
friend Expression operator==(Expression const &first, Expression const &second)
friend Expression maximum(Expression const &first, Expression const &second)
friend Expression implies(Expression const &first, Expression const &second)
int_fast64_t evaluateAsInt(Valuation const *valuation=nullptr) const
Evaluates the expression under the valuation of variables given by the valuation and returns the resu...
bool isVariable() const
Retrieves whether the expression is a variable.
OperatorType getOperator() const
Retrieves the operator of a function application.
bool hasNumericalType() const
Retrieves whether the expression has a numerical return type, i.e., integer or double.
bool evaluateAsBool(Valuation const *valuation=nullptr) const
Evaluates the expression under the valuation of variables given by the valuation and returns the resu...
bool isLinear() const
Retrieves whether this expression is a linear expression.
friend Expression operator||(Expression const &first, Expression const &second)
Expression reduceNesting() const
Tries to flatten the syntax tree of the expression, e.g., 1 + (2 + (3 + 4)) becomes (1 + 2) + (3 + 4)...
double evaluateAsDouble(Valuation const *valuation=nullptr) const
Evaluates the expression under the valuation of variables given by the valuation and returns the resu...
storm::RationalNumber evaluateAsRational() const
Evaluates the expression and returns the resulting rational number.
bool hasBooleanType() const
Retrieves whether the expression has a boolean return type.
friend Expression operator>(Expression const &first, Expression const &second)
Expression getOperand(uint_fast64_t operandIndex) const
Retrieves the given operand from the expression.
std::string const & getIdentifier() const
Retrieves the identifier associated with this expression.
bool containsVariableInITEGuard(std::set< storm::expressions::Variable > const &variables) const
Retrieves whether the expression contains any of the given variables in the 'if' part of any sub-IfTh...
friend Expression minimum(Expression const &first, Expression const &second)
Expression(Expression &&)=default
void setCompiledExpression(std::shared_ptr< CompiledExpression > const &compiledExpression) const
Associates the given compiled expression with this expression object.
bool isFalse() const
Checks if the expression is equal to the boolean literal false.
Expression & operator=(Expression const &other)=default
friend Expression pow(Expression const &base, Expression const &exponent, bool allowIntegerType)
The type of the resulting expression is.
bool containsVariables() const
Retrieves whether the expression contains a variable.
friend Expression operator+(Expression const &first, Expression const &second)
friend Expression ite(Expression const &condition, Expression const &thenExpression, Expression const &elseExpression)
friend Expression operator/(Expression const &first, Expression const &second)
boost::any accept(ExpressionVisitor &visitor, boost::any const &data) const
Accepts the given visitor.
std::set< storm::expressions::Variable > getVariables() const
Retrieves the set of all variables that appear in the expression.
friend Expression xclusiveor(Expression const &first, Expression const &second)
bool isRelationalExpression() const
Retrieves whether this expression is a relation expression, i.e., an expression that has a relation (...
Expression changeManager(ExpressionManager const &newExpressionManager) const
Converts the expression to an expression over the variables of the provided expression manager.
friend Expression operator<(Expression const &first, Expression const &second)
std::shared_ptr< BaseExpression const > const & getBaseExpressionPointer() const
Retrieves a pointer to the base expression underlying this expression object.
friend Expression operator&&(Expression const &first, Expression const &second)
bool hasIntegerType() const
Retrieves whether the expression has an integral return type.
BaseExpression const & getBaseExpression() const
Retrieves the base expression underlying this expression object.
friend Expression operator!=(Expression const &first, Expression const &second)
friend Expression floor(Expression const &first)
friend Expression operator<=(Expression const &first, Expression const &second)
bool isFunctionApplication() const
Checks if the expression is a function application (of any sort).
bool isLiteral() const
Retrieves whether the expression is a literal.
std::string toString() const
Converts the expression into a string.
friend Expression operator!(Expression const &first)
Type const & getType() const
Retrieves the type of the expression.
bool areSame(storm::expressions::Expression const &other) const
Checks whether the two expressions are the same.
friend Expression operator%(Expression const &first, Expression const &second)
ExpressionManager const & getManager() const
Retrieves the manager responsible for this expression.
friend Expression iff(Expression const &first, Expression const &second)
Expression substitute(std::map< Variable, Expression > const &variableToExpressionMap) const
Substitutes all occurrences of the variables according to the given map.
bool isSyntacticallyEqual(storm::expressions::Expression const &other) const
Checks whether the two expressions are syntatically the same.
friend Expression operator>=(Expression const &first, Expression const &second)
bool hasRationalType() const
Retrieves whether the expression has a rational return type.
bool isTrue() const
Checks if the expression is equal to the boolean literal true.
friend Expression operator-(Expression const &first, Expression const &second)
friend Expression ceil(Expression const &first)
void gatherVariables(std::set< storm::expressions::Variable > &variables) const
Retrieves the set of all variables that appear in the expression.
Expression(Expression const &other)=default
bool containsVariable(std::set< storm::expressions::Variable > const &variables) const
Retrieves whether the expression contains any of the given variables.
bool hasCompiledExpression() const
Retrieves whether this expression object has an associated compiled expression.
friend Expression round(Expression const &first)
friend Expression sign(Expression const &first)
bool hasBitVectorType() const
Retrieves whether the expression has an integral return type.
friend std::ostream & operator<<(std::ostream &stream, Expression const &expression)
Expression & operator=(Expression &&)=default
CompiledExpression const & getCompiledExpression() const
Retrieves the associated compiled expression object (if there is any).
uint_fast64_t getArity() const
Retrieves the arity of the expression.
bool isInitialized() const
Checks whether the object encapsulates a base-expression.
friend Expression abs(Expression const &first)
Expression substituteNonStandardPredicates() const
Eliminate nonstandard predicates from the expression.
friend Expression operator*(Expression const &first, Expression const &second)
This class is responsible for managing a set of typed variables and all expressions using these varia...
The base class of all valuations of variables.
Definition Valuation.h:15
Expression maximum(Expression const &first, Expression const &second)
Expression operator*(Expression const &first, Expression const &second)
Expression atLeastOneOf(std::vector< Expression > const &expressions)
Expression round(Expression const &first)
Expression makeBinaryRelationExpression(Expression const &first, Expression const &second, RelationType const &reltype)
std::less< Expression > ExpressionComparator
Definition Expression.h:513
Expression ceil(Expression const &first)
Expression operator!=(Expression const &first, Expression const &second)
std::map< Expression, Value, ExpressionComparator > ExpressionMap
Definition Expression.h:517
Expression ite(Expression const &condition, Expression const &thenExpression, Expression const &elseExpression)
Expression operator<(Expression const &first, Expression const &second)
Expression apply(std::vector< storm::expressions::Expression > const &expressions, std::function< Expression(Expression const &, Expression const &)> const &function)
Expression iff(Expression const &first, Expression const &second)
Expression abs(Expression const &first)
Expression sum(std::vector< storm::expressions::Expression > const &expressions)
std::set< Expression, ExpressionComparator > ExpressionSet
Definition Expression.h:515
Expression conjunction(std::vector< storm::expressions::Expression > const &expressions)
Expression operator!(Expression const &first)
Expression operator-(Expression const &first, Expression const &second)
Expression product(std::vector< storm::expressions::Expression > const &expressions)
Expression applyAssociative(std::vector< storm::expressions::Expression > const &expressions, std::function< Expression(Expression const &, Expression const &)> const &function)
Expression exactlyOneOf(std::vector< Expression > const &expressions)
Expression operator==(Expression const &first, Expression const &second)
Expression operator>=(Expression const &first, Expression const &second)
Expression atMostOneOf(std::vector< Expression > const &expressions)
Expression pow(Expression const &base, Expression const &exponent, bool allowIntegerType)
The type of the resulting expression is.
Expression operator/(Expression const &first, Expression const &second)
Expression minimum(Expression const &first, Expression const &second)
Expression disjunction(std::vector< storm::expressions::Expression > const &expressions)
Expression floor(Expression const &first)
Expression sin(Expression const &first)
Expression sign(Expression const &first)
Expression truncate(Expression const &first)
RelationType
An enum type specifying the different relations applicable.
Expression operator&&(Expression const &first, Expression const &second)
Expression operator||(Expression const &first, Expression const &second)
Expression xclusiveor(Expression const &first, Expression const &second)
Expression operator<=(Expression const &first, Expression const &second)
Expression logarithm(Expression const &first, Expression const &second)
Expression operator>(Expression const &first, Expression const &second)
Expression cos(Expression const &first)
Expression implies(Expression const &first, Expression const &second)
Expression modulo(Expression const &first, Expression const &second)
Expression operator+(Expression const &first, Expression const &second)
bool operator()(storm::expressions::Expression const &e1, storm::expressions::Expression const &e2) const
Definition Expression.h:502
size_t operator()(storm::expressions::Expression const &e) const
Definition Expression.h:495
bool operator()(storm::expressions::Expression const &lhs, storm::expressions::Expression const &rhs) const
Definition Expression.h:487
bool operator()(Expression const &lhs, Expression const &rhs) const
Definition Expression.h:476