cprover
mathematical_expr.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: API to expression classes for 'mathematical' expressions
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #ifndef CPROVER_UTIL_MATHEMATICAL_EXPR_H
10 #define CPROVER_UTIL_MATHEMATICAL_EXPR_H
11 
14 
15 #include "mathematical_types.h"
16 #include "std_expr.h"
17 
20 class transt : public ternary_exprt
21 {
22 public:
24  const irep_idt &_id,
25  const exprt &_op0,
26  const exprt &_op1,
27  const exprt &_op2,
28  const typet &_type)
29  : ternary_exprt(_id, _op0, _op1, _op2, _type)
30  {
31  }
32 
34  {
35  return op0();
36  }
38  {
39  return op1();
40  }
42  {
43  return op2();
44  }
45 
46  const exprt &invar() const
47  {
48  return op0();
49  }
50  const exprt &init() const
51  {
52  return op1();
53  }
54  const exprt &trans() const
55  {
56  return op2();
57  }
58 };
59 
60 template <>
61 inline bool can_cast_expr<transt>(const exprt &base)
62 {
63  return base.id() == ID_trans;
64 }
65 
66 inline void validate_expr(const transt &value)
67 {
68  validate_operands(value, 3, "Transition systems must have three operands");
69 }
70 
75 inline const transt &to_trans_expr(const exprt &expr)
76 {
77  PRECONDITION(expr.id() == ID_trans);
78  const transt &ret = static_cast<const transt &>(expr);
79  validate_expr(ret);
80  return ret;
81 }
82 
84 inline transt &to_trans_expr(exprt &expr)
85 {
86  PRECONDITION(expr.id() == ID_trans);
87  transt &ret = static_cast<transt &>(expr);
88  validate_expr(ret);
89  return ret;
90 }
91 
93 class power_exprt : public binary_exprt
94 {
95 public:
96  power_exprt(const exprt &_base, const exprt &_exp)
97  : binary_exprt(_base, ID_power, _exp)
98  {
99  }
100 };
101 
102 template <>
103 inline bool can_cast_expr<power_exprt>(const exprt &base)
104 {
105  return base.id() == ID_power;
106 }
107 
108 inline void validate_expr(const power_exprt &value)
109 {
110  validate_operands(value, 2, "Power must have two operands");
111 }
112 
119 inline const power_exprt &to_power_expr(const exprt &expr)
120 {
121  PRECONDITION(expr.id() == ID_power);
122  const power_exprt &ret = static_cast<const power_exprt &>(expr);
123  validate_expr(ret);
124  return ret;
125 }
126 
129 {
130  PRECONDITION(expr.id() == ID_power);
131  power_exprt &ret = static_cast<power_exprt &>(expr);
132  validate_expr(ret);
133  return ret;
134 }
135 
138 {
139 public:
140  factorial_power_exprt(const exprt &_base, const exprt &_exp)
141  : binary_exprt(_base, ID_factorial_power, _exp)
142  {
143  }
144 };
145 
146 template <>
148 {
149  return base.id() == ID_factorial_power;
150 }
151 
152 inline void validate_expr(const factorial_power_exprt &value)
153 {
154  validate_operands(value, 2, "Factorial power must have two operands");
155 }
156 
164 {
165  PRECONDITION(expr.id() == ID_factorial_power);
166  const factorial_power_exprt &ret =
167  static_cast<const factorial_power_exprt &>(expr);
168  validate_expr(ret);
169  return ret;
170 }
171 
174 {
175  PRECONDITION(expr.id() == ID_factorial_power);
176  factorial_power_exprt &ret = static_cast<factorial_power_exprt &>(expr);
177  validate_expr(ret);
178  return ret;
179 }
180 
182 {
183 public:
185  : multi_ary_exprt(ID_tuple, std::move(operands), typet())
186  {
187  }
188 };
189 
192 {
193 public:
195 
198  function_application_exprt(const exprt &_function, argumentst _arguments);
199 
200  exprt &function()
201  {
202  return op0();
203  }
204 
205  const exprt &function() const
206  {
207  return op0();
208  }
209 
212 
214  {
215  return op1().operands();
216  }
217 
218  const argumentst &arguments() const
219  {
220  return op1().operands();
221  }
222 };
223 
224 template <>
226 {
227  return base.id() == ID_function_application;
228 }
229 
230 inline void validate_expr(const function_application_exprt &value)
231 {
232  validate_operands(value, 2, "Function application must have two operands");
233 }
234 
241 inline const function_application_exprt &
243 {
244  PRECONDITION(expr.id() == ID_function_application);
245  const function_application_exprt &ret =
246  static_cast<const function_application_exprt &>(expr);
247  validate_expr(ret);
248  return ret;
249 }
250 
253 {
254  PRECONDITION(expr.id() == ID_function_application);
256  static_cast<function_application_exprt &>(expr);
257  validate_expr(ret);
258  return ret;
259 }
260 
263 {
264 public:
267  : binding_exprt(_id, {std::move(_symbol)}, std::move(_where), bool_typet())
268  {
269  }
270 
272  quantifier_exprt(irep_idt _id, const variablest &_variables, exprt _where)
273  : binding_exprt(_id, _variables, std::move(_where), bool_typet())
274  {
275  }
276 
277  // for the special case of one variable
279  {
280  auto &variables = this->variables();
281  PRECONDITION(variables.size() == 1);
282  return variables.front();
283  }
284 
285  // for the special case of one variable
286  const symbol_exprt &symbol() const
287  {
288  auto &variables = this->variables();
289  PRECONDITION(variables.size() == 1);
290  return variables.front();
291  }
292 };
293 
294 template <>
295 inline bool can_cast_expr<quantifier_exprt>(const exprt &base)
296 {
297  return base.id() == ID_forall || base.id() == ID_exists;
298 }
299 
300 inline void validate_expr(const quantifier_exprt &value)
301 {
302  validate_operands(value, 2, "quantifier expressions must have two operands");
303  for(auto &op : value.variables())
305  op.id() == ID_symbol, "quantified variable shall be a symbol");
306 }
307 
314 inline const quantifier_exprt &to_quantifier_expr(const exprt &expr)
315 {
317  const quantifier_exprt &ret = static_cast<const quantifier_exprt &>(expr);
318  validate_expr(ret);
319  return ret;
320 }
321 
324 {
326  quantifier_exprt &ret = static_cast<quantifier_exprt &>(expr);
327  validate_expr(ret);
328  return ret;
329 }
330 
333 {
334 public:
335  forall_exprt(const symbol_exprt &_symbol, const exprt &_where)
336  : quantifier_exprt(ID_forall, _symbol, _where)
337  {
338  }
339 };
340 
341 template <>
342 inline bool can_cast_expr<forall_exprt>(const exprt &base)
343 {
344  return base.id() == ID_forall;
345 }
346 
347 inline void validate_expr(const forall_exprt &value)
348 {
349  validate_expr(static_cast<const quantifier_exprt &>(value));
350 }
351 
352 inline const forall_exprt &to_forall_expr(const exprt &expr)
353 {
354  PRECONDITION(expr.id() == ID_forall);
355  const forall_exprt &ret = static_cast<const forall_exprt &>(expr);
356  validate_expr(static_cast<const quantifier_exprt &>(ret));
357  return ret;
358 }
359 
361 {
362  PRECONDITION(expr.id() == ID_forall);
363  forall_exprt &ret = static_cast<forall_exprt &>(expr);
364  validate_expr(static_cast<const quantifier_exprt &>(ret));
365  return ret;
366 }
367 
370 {
371 public:
372  exists_exprt(const symbol_exprt &_symbol, const exprt &_where)
373  : quantifier_exprt(ID_exists, _symbol, _where)
374  {
375  }
376 };
377 
378 template <>
379 inline bool can_cast_expr<exists_exprt>(const exprt &base)
380 {
381  return base.id() == ID_exists;
382 }
383 
384 inline void validate_expr(const exists_exprt &value)
385 {
386  validate_expr(static_cast<const quantifier_exprt &>(value));
387 }
388 
389 inline const exists_exprt &to_exists_expr(const exprt &expr)
390 {
391  PRECONDITION(expr.id() == ID_exists);
392  const exists_exprt &ret = static_cast<const exists_exprt &>(expr);
393  validate_expr(static_cast<const quantifier_exprt &>(ret));
394  return ret;
395 }
396 
398 {
399  PRECONDITION(expr.id() == ID_exists);
400  exists_exprt &ret = static_cast<exists_exprt &>(expr);
401  validate_expr(static_cast<const quantifier_exprt &>(ret));
402  return ret;
403 }
404 
407 {
408 public:
409  lambda_exprt(const variablest &, const exprt &_where);
410 
412  {
413  return static_cast<mathematical_function_typet &>(binding_exprt::type());
414  }
415 
417  {
418  return static_cast<const mathematical_function_typet &>(
420  }
421 
422  // apply the function to the given arguments
423  exprt application(const operandst &arguments) const
424  {
425  return instantiate(arguments);
426  }
427 };
428 
429 template <>
430 inline bool can_cast_expr<lambda_exprt>(const exprt &base)
431 {
432  return base.id() == ID_lambda;
433 }
434 
435 inline void validate_expr(const lambda_exprt &value)
436 {
437  validate_operands(value, 2, "lambda must have two operands");
438 }
439 
446 inline const lambda_exprt &to_lambda_expr(const exprt &expr)
447 {
448  PRECONDITION(expr.id() == ID_lambda);
449  DATA_INVARIANT(expr.operands().size() == 2, "lambda must have two operands");
451  expr.type().id() == ID_mathematical_function,
452  "lambda must have right type");
453  return static_cast<const lambda_exprt &>(expr);
454 }
455 
458 {
459  PRECONDITION(expr.id() == ID_lambda);
460  DATA_INVARIANT(expr.operands().size() == 2, "lambda must have two operands");
462  expr.type().id() == ID_mathematical_function,
463  "lambda must have right type");
464  return static_cast<lambda_exprt &>(expr);
465 }
466 
467 #endif // CPROVER_UTIL_MATHEMATICAL_EXPR_H
function_application_exprt::arguments
const argumentst & arguments() const
Definition: mathematical_expr.h:218
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:37
function_application_exprt::arguments
argumentst & arguments()
Definition: mathematical_expr.h:213
can_cast_expr< factorial_power_exprt >
bool can_cast_expr< factorial_power_exprt >(const exprt &base)
Definition: mathematical_expr.h:147
can_cast_expr< quantifier_exprt >
bool can_cast_expr< quantifier_exprt >(const exprt &base)
Definition: mathematical_expr.h:295
factorial_power_exprt
Falling factorial power.
Definition: mathematical_expr.h:138
multi_ary_exprt
A base class for multi-ary expressions Associativity is not specified.
Definition: std_expr.h:824
to_lambda_expr
const lambda_exprt & to_lambda_expr(const exprt &expr)
Cast an exprt to a lambda_exprt.
Definition: mathematical_expr.h:446
forall_exprt
A forall expression.
Definition: mathematical_expr.h:333
ternary_exprt::op2
exprt & op2()
Definition: expr.h:105
typet
The type of an expression, extends irept.
Definition: type.h:28
validate_expr
void validate_expr(const transt &value)
Definition: mathematical_expr.h:66
ternary_exprt
An expression with three operands.
Definition: std_expr.h:53
power_exprt
Exponentiation.
Definition: mathematical_expr.h:94
lambda_exprt::type
const mathematical_function_typet & type() const
Definition: mathematical_expr.h:416
transt::invar
exprt & invar()
Definition: mathematical_expr.h:33
validate_operands
void validate_operands(const exprt &value, exprt::operandst::size_type number, const char *message, bool allow_more=false)
Definition: expr_cast.h:250
to_factorial_power_expr
const factorial_power_exprt & to_factorial_power_expr(const exprt &expr)
Cast an exprt to a factorial_power_exprt.
Definition: mathematical_expr.h:163
exists_exprt
An exists expression.
Definition: mathematical_expr.h:370
ternary_exprt::op1
exprt & op1()
Definition: expr.h:102
exprt
Base class for all expressions.
Definition: expr.h:54
binary_exprt
A base class for binary expressions.
Definition: std_expr.h:550
bool_typet
The Boolean type.
Definition: std_types.h:36
quantifier_exprt
A base class for quantifier expressions.
Definition: mathematical_expr.h:263
can_cast_expr< forall_exprt >
bool can_cast_expr< forall_exprt >(const exprt &base)
Definition: mathematical_expr.h:342
quantifier_exprt::symbol
const symbol_exprt & symbol() const
Definition: mathematical_expr.h:286
symbol_exprt
Expression to hold a symbol (variable)
Definition: std_expr.h:80
power_exprt::power_exprt
power_exprt(const exprt &_base, const exprt &_exp)
Definition: mathematical_expr.h:96
function_application_exprt::argumentst
exprt::operandst argumentst
Definition: mathematical_expr.h:194
transt
Transition system, consisting of state invariant, initial state predicate, and transition predicate.
Definition: mathematical_expr.h:21
forall_exprt::forall_exprt
forall_exprt(const symbol_exprt &_symbol, const exprt &_where)
Definition: mathematical_expr.h:335
binding_exprt::variables
variablest & variables()
Definition: std_expr.h:2867
mathematical_types.h
Mathematical types.
function_application_exprt::function_type
const mathematical_function_typet & function_type() const
This helper method provides the type of the expression returned by function.
Definition: mathematical_expr.cpp:28
transt::transt
transt(const irep_idt &_id, const exprt &_op0, const exprt &_op1, const exprt &_op2, const typet &_type)
Definition: mathematical_expr.h:23
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:82
DATA_INVARIANT
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:510
quantifier_exprt::quantifier_exprt
quantifier_exprt(irep_idt _id, const variablest &_variables, exprt _where)
constructor for multiple variables
Definition: mathematical_expr.h:272
can_cast_expr< exists_exprt >
bool can_cast_expr< exists_exprt >(const exprt &base)
Definition: mathematical_expr.h:379
transt::init
const exprt & init() const
Definition: mathematical_expr.h:50
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
quantifier_exprt::symbol
symbol_exprt & symbol()
Definition: mathematical_expr.h:278
ternary_exprt::op0
exprt & op0()
Definition: expr.h:99
to_trans_expr
const transt & to_trans_expr(const exprt &expr)
Cast an exprt to a transt expr must be known to be transt.
Definition: mathematical_expr.h:75
function_application_exprt
Application of (mathematical) function.
Definition: mathematical_expr.h:192
binding_exprt::variablest
std::vector< symbol_exprt > variablest
Definition: std_expr.h:2848
transt::init
exprt & init()
Definition: mathematical_expr.h:37
irept::id
const irep_idt & id() const
Definition: irep.h:407
binding_exprt
A base class for variable bindings (quantifiers, let, lambda)
Definition: std_expr.h:2846
exprt::operandst
std::vector< exprt > operandst
Definition: expr.h:56
tuple_exprt::tuple_exprt
tuple_exprt(exprt::operandst operands)
Definition: mathematical_expr.h:184
can_cast_expr< power_exprt >
bool can_cast_expr< power_exprt >(const exprt &base)
Definition: mathematical_expr.h:103
transt::invar
const exprt & invar() const
Definition: mathematical_expr.h:46
factorial_power_exprt::factorial_power_exprt
factorial_power_exprt(const exprt &_base, const exprt &_exp)
Definition: mathematical_expr.h:140
to_exists_expr
const exists_exprt & to_exists_expr(const exprt &expr)
Definition: mathematical_expr.h:389
function_application_exprt::function_application_exprt
function_application_exprt(const exprt &_function, argumentst _arguments)
Definition: mathematical_expr.cpp:14
to_quantifier_expr
const quantifier_exprt & to_quantifier_expr(const exprt &expr)
Cast an exprt to a quantifier_exprt.
Definition: mathematical_expr.h:314
can_cast_expr< lambda_exprt >
bool can_cast_expr< lambda_exprt >(const exprt &base)
Definition: mathematical_expr.h:430
can_cast_expr< function_application_exprt >
bool can_cast_expr< function_application_exprt >(const exprt &base)
Definition: mathematical_expr.h:225
lambda_exprt::type
mathematical_function_typet & type()
Definition: mathematical_expr.h:411
lambda_exprt
A (mathematical) lambda expression.
Definition: mathematical_expr.h:407
can_cast_expr< transt >
bool can_cast_expr< transt >(const exprt &base)
Definition: mathematical_expr.h:61
binding_exprt::instantiate
exprt instantiate(const exprt::operandst &) const
substitute free occurrences of the variables in where() by the given values
Definition: std_expr.cpp:234
to_factorial_expr
factorial_power_exprt & to_factorial_expr(exprt &expr)
Cast an exprt to a factorial_power_exprt.
Definition: mathematical_expr.h:173
transt::trans
exprt & trans()
Definition: mathematical_expr.h:41
transt::trans
const exprt & trans() const
Definition: mathematical_expr.h:54
tuple_exprt
Definition: mathematical_expr.h:182
exprt::operands
operandst & operands()
Definition: expr.h:92
to_power_expr
const power_exprt & to_power_expr(const exprt &expr)
Cast an exprt to a power_exprt.
Definition: mathematical_expr.h:119
binary_exprt::op1
exprt & op1()
Definition: expr.h:102
std_expr.h
API to expression classes.
lambda_exprt::application
exprt application(const operandst &arguments) const
Definition: mathematical_expr.h:423
lambda_exprt::lambda_exprt
lambda_exprt(const variablest &, const exprt &_where)
Definition: mathematical_expr.cpp:46
mathematical_function_typet
A type for mathematical functions (do not confuse with functions/methods in code)
Definition: mathematical_types.h:59
to_function_application_expr
const function_application_exprt & to_function_application_expr(const exprt &expr)
Cast an exprt to a function_application_exprt.
Definition: mathematical_expr.h:242
exists_exprt::exists_exprt
exists_exprt(const symbol_exprt &_symbol, const exprt &_where)
Definition: mathematical_expr.h:372
to_forall_expr
const forall_exprt & to_forall_expr(const exprt &expr)
Definition: mathematical_expr.h:352
binary_exprt::op0
exprt & op0()
Definition: expr.h:99
quantifier_exprt::quantifier_exprt
quantifier_exprt(irep_idt _id, symbol_exprt _symbol, exprt _where)
constructor for single variable
Definition: mathematical_expr.h:266