dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
GenericExpressionVisitor.h
1
7#pragma once
8
9#include <stdexcept>
10
11#include "dlinear/symbolic/symbolic.h"
12#include "dlinear/util/Config.h"
13#include "dlinear/util/Stats.h"
14
15namespace dlinear {
16
24template <typename Result, typename... Args>
26 public:
28 [[nodiscard]] const IterationStats &stats() const { return stats_; }
30 [[nodiscard]] const Config &config() const { return config_; }
31
32 protected:
33 explicit GenericExpressionVisitor(const Config &config, const std::string &class_name = "GenericExpressionVisitor")
34 : config_{config}, stats_{config.with_timings(), class_name, "Converting"} {}
35 virtual ~GenericExpressionVisitor() = default;
36
37 [[nodiscard]] virtual Result VisitExpression(const Expression &e, Args... args) const {
38 switch (e.get_kind()) {
39 case ExpressionKind::Constant:
40 return VisitConstant(e, std::forward<Args>(args)...);
41 case ExpressionKind::Var:
42 return VisitVariable(e, std::forward<Args>(args)...);
43 case ExpressionKind::Add:
44 return VisitAddition(e, std::forward<Args>(args)...);
45 case ExpressionKind::Mul:
46 return VisitMultiplication(e, std::forward<Args>(args)...);
47 case ExpressionKind::Div:
48 return VisitDivision(e, std::forward<Args>(args)...);
49 case ExpressionKind::Log:
50 return VisitLog(e, std::forward<Args>(args)...);
51 case ExpressionKind::Abs:
52 return VisitAbs(e, std::forward<Args>(args)...);
53 case ExpressionKind::Exp:
54 return VisitExp(e, std::forward<Args>(args)...);
55 case ExpressionKind::Sqrt:
56 return VisitSqrt(e, std::forward<Args>(args)...);
57 case ExpressionKind::Pow:
58 return VisitPow(e, std::forward<Args>(args)...);
59 case ExpressionKind::Sin:
60 return VisitSin(e, std::forward<Args>(args)...);
61 case ExpressionKind::Cos:
62 return VisitCos(e, std::forward<Args>(args)...);
63 case ExpressionKind::Tan:
64 return VisitTan(e, std::forward<Args>(args)...);
65 case ExpressionKind::Asin:
66 return VisitAsin(e, std::forward<Args>(args)...);
67 case ExpressionKind::Acos:
68 return VisitAcos(e, std::forward<Args>(args)...);
69 case ExpressionKind::Atan:
70 return VisitAtan(e, std::forward<Args>(args)...);
71 case ExpressionKind::Atan2:
72 return VisitAtan2(e, std::forward<Args>(args)...);
73 case ExpressionKind::Sinh:
74 return VisitSinh(e, std::forward<Args>(args)...);
75 case ExpressionKind::Cosh:
76 return VisitCosh(e, std::forward<Args>(args)...);
77 case ExpressionKind::Tanh:
78 return VisitTanh(e, std::forward<Args>(args)...);
79 case ExpressionKind::Min:
80 return VisitMin(e, std::forward<Args>(args)...);
81 case ExpressionKind::Max:
82 return VisitMax(e, std::forward<Args>(args)...);
83 case ExpressionKind::IfThenElse:
84 return VisitIfThenElse(e, std::forward<Args>(args)...);
85 case ExpressionKind::Infty:
86 throw std::runtime_error("An infinity is detected while visiting an expression.");
87 case ExpressionKind::NaN:
88 throw std::runtime_error("NaN is detected while visiting an expression.");
89 case ExpressionKind::UninterpretedFunction:
90 return VisitUninterpretedFunction(e, std::forward<Args>(args)...);
91 default:
92 throw std::runtime_error("Unreachable code.");
93 }
94 }
95
96 virtual Result VisitVariable(const Expression &e, Args... args) const = 0;
97 virtual Result VisitConstant(const Expression &e, Args... args) const = 0;
98 virtual Result VisitAddition(const Expression &e, Args... args) const = 0;
99 virtual Result VisitMultiplication(const Expression &e, Args... args) const = 0;
100 virtual Result VisitDivision(const Expression &e, Args... args) const = 0;
101 virtual Result VisitLog(const Expression &e, Args... args) const = 0;
102 virtual Result VisitAbs(const Expression &e, Args... args) const = 0;
103 virtual Result VisitExp(const Expression &e, Args... args) const = 0;
104 virtual Result VisitSqrt(const Expression &e, Args... args) const = 0;
105 virtual Result VisitPow(const Expression &e, Args... args) const = 0;
106 virtual Result VisitSin(const Expression &e, Args... args) const = 0;
107 virtual Result VisitCos(const Expression &e, Args... args) const = 0;
108 virtual Result VisitTan(const Expression &e, Args... args) const = 0;
109 virtual Result VisitAsin(const Expression &e, Args... args) const = 0;
110 virtual Result VisitAcos(const Expression &e, Args... args) const = 0;
111 virtual Result VisitAtan(const Expression &e, Args... args) const = 0;
112 virtual Result VisitAtan2(const Expression &e, Args... args) const = 0;
113 virtual Result VisitSinh(const Expression &e, Args... args) const = 0;
114 virtual Result VisitCosh(const Expression &e, Args... args) const = 0;
115 virtual Result VisitTanh(const Expression &e, Args... args) const = 0;
116 virtual Result VisitMin(const Expression &e, Args... args) const = 0;
117 virtual Result VisitMax(const Expression &e, Args... args) const = 0;
118 virtual Result VisitIfThenElse(const Expression &e, Args... args) const = 0;
119 virtual Result VisitUninterpretedFunction(const Expression &e, Args... args) const = 0;
120
123};
124
125} // namespace dlinear
Simple dataclass used to store the configuration of the program.
Definition Config.h:38
Generic expression visitor implementing the visitor pattern.
const Config & config() const
Get read-only access to the configuration of the GenericExpressionVisitor.
const IterationStats & stats() const
Get read-only access to the statistics of the GenericExpressionVisitor.
Dataclass collecting statistics about some operation or process.
Definition Stats.h:71
Global namespace for the dlinear library.