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 <unordered_map>
7#include <vector>
8
12
13namespace storm {
14namespace expressions {
15// Foward-declare expression manager class.
17class Variable;
20
22 public:
23 friend class ExpressionManager;
24 friend class Variable;
25 template<typename MapType>
26 friend class SubstitutionVisitor;
27
28 friend Expression operator+(Expression const& first, Expression const& second);
29 friend Expression operator+(Expression const& first, int64_t second);
30 friend Expression operator+(int64_t first, Expression const& second);
31 friend Expression operator-(Expression const& first, Expression const& second);
32 friend Expression operator-(Expression const& first, int64_t second);
33 friend Expression operator-(int64_t first, Expression const& second);
34 friend Expression operator-(Expression const& first);
35 friend Expression operator*(Expression const& first, Expression const& second);
36 friend Expression operator/(Expression const& first, Expression const& second);
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);
41 friend Expression operator==(Expression const& first, Expression const& second);
42 friend Expression operator!=(Expression const& first, Expression const& second);
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, int64_t second);
48 friend Expression operator>=(Expression const& first, int64_t second);
49 friend Expression operator<(Expression const& first, int64_t second);
50 friend Expression operator<=(Expression const& first, int64_t second);
51 friend Expression ite(Expression const& condition, Expression const& thenExpression, Expression const& elseExpression);
52 friend Expression implies(Expression const& first, Expression const& second);
53 friend Expression iff(Expression const& first, Expression const& second);
54 friend Expression xclusiveor(Expression const& first, Expression const& second);
55 friend Expression pow(Expression const& base, Expression const& exponent, bool allowIntegerType);
56 friend Expression abs(Expression const& first);
57 friend Expression truncate(Expression const& first);
58 friend Expression sign(Expression const& first);
59 friend Expression floor(Expression const& first);
60 friend Expression ceil(Expression const& first);
61 friend Expression round(Expression const& first);
62 friend Expression minimum(Expression const& first, Expression const& second);
63 friend Expression maximum(Expression const& first, Expression const& second);
64
65 Expression() = default;
67
73 Expression(Variable const& variable);
74
80 Expression(std::shared_ptr<BaseExpression const> const& expressionPtr);
81
82 // Instantiate constructors and assignments with their default implementations.
83 Expression(Expression const& other) = default;
84 Expression& operator=(Expression const& other) = default;
85 Expression(Expression&&) = default;
87
91 Expression changeManager(ExpressionManager const& newExpressionManager) const;
92
102 Expression substitute(std::map<Variable, Expression> const& variableToExpressionMap) const;
103
118 Expression substitute(std::unordered_map<Variable, Expression> const& variableToExpressionMap) const;
119
127 bool evaluateAsBool(Valuation const* valuation = nullptr) const;
128
136 int_fast64_t evaluateAsInt(Valuation const* valuation = nullptr) const;
137
145 double evaluateAsDouble(Valuation const* valuation = nullptr) const;
146
154 storm::RationalNumber evaluateAsRational() const;
155
161 Expression simplify() const;
162
169
177
183 bool isFunctionApplication() const;
184
190 uint_fast64_t getArity() const;
191
198 Expression getOperand(uint_fast64_t operandIndex) const;
199
206 std::string const& getIdentifier() const;
207
213 bool containsVariables() const;
214
220 bool isLiteral() const;
221
227 bool isVariable() const;
228
234 bool isTrue() const;
235
241 bool isFalse() const;
242
249 bool areSame(storm::expressions::Expression const& other) const;
250
257 bool isRelationalExpression() const;
258
264 bool isLinear() const;
265
271 std::set<storm::expressions::Variable> getVariables() const;
272
279 void gatherVariables(std::set<storm::expressions::Variable>& variables) const;
280
287 bool containsVariable(std::set<storm::expressions::Variable> const& variables) const;
288
296 bool containsVariableInITEGuard(std::set<storm::expressions::Variable> const& variables) const;
297
304 BaseExpression const& getBaseExpression() const;
305
311 std::shared_ptr<BaseExpression const> const& getBaseExpressionPointer() const;
312
318 ExpressionManager const& getManager() const;
319
325 Type const& getType() const;
326
332 bool hasNumericalType() const;
333
339 bool hasRationalType() const;
340
346 bool hasBooleanType() const;
347
353 bool hasIntegerType() const;
354
360 bool hasBitVectorType() const;
361
367 boost::any accept(ExpressionVisitor& visitor, boost::any const& data) const;
368
374 std::string toString() const;
375
379 bool isInitialized() const;
380
385
389 bool hasCompiledExpression() const;
390
394 void setCompiledExpression(std::shared_ptr<CompiledExpression> const& compiledExpression) const;
395
400
401 friend std::ostream& operator<<(std::ostream& stream, Expression const& expression);
402
403 private:
404 // A pointer to the underlying base expression.
405 std::shared_ptr<BaseExpression const> expressionPtr;
406
407 // A pointer to an associated compiled expression object (if any).
408 mutable std::shared_ptr<CompiledExpression> compiledExpression;
409};
410
411// Provide operator overloads to conveniently construct new expressions from other expressions.
412Expression operator+(Expression const& first, Expression const& second);
413Expression operator+(Expression const& first, int64_t second);
414Expression operator+(int64_t first, Expression const& second);
415Expression operator-(Expression const& first, Expression const& second);
416Expression operator-(Expression const& first, int64_t second);
417Expression operator-(int64_t first, Expression const& second);
418Expression operator-(Expression const& first);
419Expression operator*(Expression const& first, Expression const& second);
420Expression operator/(Expression const& first, Expression const& second);
421Expression operator&&(Expression const& first, Expression const& second);
422Expression operator||(Expression const& first, Expression const& second);
423Expression operator!(Expression const& first);
424Expression operator==(Expression const& first, Expression const& second);
425Expression operator!=(Expression const& first, Expression const& second);
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, int64_t second);
431Expression operator>=(Expression const& first, int64_t second);
432Expression operator<(Expression const& first, int64_t second);
433Expression operator<=(Expression const& first, int64_t second);
434Expression ite(Expression const& condition, Expression const& thenExpression, Expression const& elseExpression);
435Expression implies(Expression const& first, Expression const& second);
436Expression iff(Expression const& first, Expression const& second);
437Expression xclusiveor(Expression const& first, Expression const& second);
438
447Expression pow(Expression const& base, Expression const& exponent, bool allowIntegerType = false);
448Expression abs(Expression const& first);
449Expression truncate(Expression const& first);
450Expression sign(Expression const& first);
451Expression floor(Expression const& first);
452Expression ceil(Expression const& first);
453Expression round(Expression const& first);
454Expression modulo(Expression const& first, Expression const& second);
455Expression logarithm(Expression const& first, Expression const& second);
456Expression cos(Expression const& first);
457Expression sin(Expression const& first);
458Expression minimum(Expression const& first, Expression const& second);
459Expression maximum(Expression const& first, Expression const& second);
460Expression atLeastOneOf(std::vector<storm::expressions::Expression> const& expressions);
461Expression atMostOneOf(std::vector<storm::expressions::Expression> const& expressions);
462Expression exactlyOneOf(std::vector<storm::expressions::Expression> const& expressions);
463Expression disjunction(std::vector<storm::expressions::Expression> const& expressions);
464Expression conjunction(std::vector<storm::expressions::Expression> const& expressions);
465Expression sum(std::vector<storm::expressions::Expression> const& expressions);
466Expression apply(std::vector<storm::expressions::Expression> const& expressions,
467 std::function<Expression(Expression const&, Expression const&)> const& function);
468Expression applyAssociative(std::vector<storm::expressions::Expression> const& expressions,
469 std::function<Expression(Expression const&, Expression const&)> const& function);
470Expression makeBinaryRelationExpression(Expression const& lhs, Expression const& rhs, RelationType const& reltype);
471} // namespace expressions
472} // namespace storm
473
474namespace std {
475template<>
476struct less<storm::expressions::Expression> {
480};
481
482template<>
483struct hash<storm::expressions::Expression> {
485 return reinterpret_cast<size_t>(e.getBaseExpressionPointer().get());
486 }
487};
488
489template<>
490struct equal_to<storm::expressions::Expression> {
492 return e1.areSame(e2);
493 }
494};
495} // namespace std
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)
Expression ceil(Expression const &first)
Expression operator!=(Expression const &first, Expression const &second)
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)
Expression conjunction(std::vector< storm::expressions::Expression > const &expressions)
Expression operator!(Expression const &first)
Expression operator-(Expression const &first, Expression const &second)
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:491
size_t operator()(storm::expressions::Expression const &e) const
Definition Expression.h:484
bool operator()(storm::expressions::Expression const &lhs, storm::expressions::Expression const &rhs) const
Definition Expression.h:477