dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
Term.cpp
1
6#include "Term.h"
7
8#include <iostream>
9
10#include "dlinear/util/exception.h"
11#include "dlinear/util/logging.h"
12
13namespace dlinear::vnnlib {
14
15Term::Term() : term_{} {}
16Term::Term(Expression e) : term_{e} { DLINEAR_TRACE_FMT("Term::Term({}) - Expression", e); }
17Term::Term(Formula f) : term_{f} { DLINEAR_TRACE_FMT("Term::Term({}) - Formula", f); }
18Term &Term::operator=(const Formula &f) {
19 term_.emplace<Formula>(f);
20 return *this;
21}
22Term &Term::operator=(const Expression &e) {
23 term_.emplace<Expression>(e);
24 return *this;
25}
26Term &Term::operator=(Formula &&f) {
27 term_.emplace<Formula>(f);
28 return *this;
29}
30Term &Term::operator=(Expression &&e) {
31 term_.emplace<Expression>(e);
32 return *this;
33}
34
35Term::Type Term::type() const { return std::holds_alternative<Expression>(term_) ? Type::EXPRESSION : Type::FORMULA; }
36const Expression &Term::expression() const { return std::get<Expression>(term_); }
37Expression &Term::m_expression() { return std::get<Expression>(term_); }
38const Formula &Term::formula() const { return std::get<Formula>(term_); }
39Formula &Term::m_formula() { return std::get<Formula>(term_); }
40
41Term Term::Substitute(const Variable &v, const Term &t) {
42 switch (type()) {
43 case Type::FORMULA: {
44 switch (v.get_type()) {
48 return Term{formula().Substitute(v, t.expression())};
50 return Term{formula().Substitute(v, t.formula())};
51 default:
52 DLINEAR_UNREACHABLE();
53 }
54 }
55 case Type::EXPRESSION: {
56 switch (v.get_type()) {
60 return Term{expression().Substitute(v, t.expression())};
62 return Term{expression().Substitute({}, {{v, t.formula()}})};
63 default:
64 DLINEAR_UNREACHABLE();
65 }
66 }
67 default:
68 DLINEAR_UNREACHABLE();
69 }
70}
71
72void Term::Check(Sort s) const {
73 switch (type()) {
74 case Term::Type::EXPRESSION:
75 if (s == Sort::Int || s == Sort::Real || s == Sort::Binary) return; // OK
76 break;
77 case Term::Type::FORMULA:
78 if (s == Sort::Bool) return; // OK
79 break;
80 default:
81 DLINEAR_RUNTIME_ERROR_FMT("Term {} does not match against sort {}", *this, s);
82 }
83}
84
86 switch (type()) {
87 case Type::FORMULA:
88 if (t == Variable::Type::BOOLEAN) return; // OK
89 break;
90 case Type::EXPRESSION:
92 break;
93 default:
94 DLINEAR_RUNTIME_ERROR_FMT("Term {} does not match against type {}", *this, t);
95 }
96}
97
98std::ostream &operator<<(std::ostream &os, const Term &t) {
99 switch (t.type()) {
100 case Term::Type::EXPRESSION:
101 return os << t.expression();
102 case Term::Type::FORMULA:
103 return os << t.formula();
104 default:
105 DLINEAR_UNREACHABLE();
106 }
107}
108
109} // namespace dlinear::vnnlib
Represents a symbolic form of an expression.
Expression Substitute(const Variable &var, const Expression &e) const
Returns a copy of this expression replacing all occurrences of var with e.
Represents a symbolic form of a first-order logic formula.
Formula Substitute(const Variable &var, const Expression &e) const
Returns a copy of this formula replacing all occurrences of var with e.
Represents a symbolic variable.
Type
Supported types of symbolic variables.
@ INTEGER
An INTEGER variable takes an int value.
@ BINARY
A BINARY variable takes an integer value from {0, 1}.
@ CONTINUOUS
A CONTINUOUS variable takes a mpq_class value.
@ BOOLEAN
A BOOLEAN variable takes a bool value.
Generic term that can be either an expression or a formula.
Definition Term.h:34
std::variant< Expression, Formula > term_
Expression or formula stored by the term.
Definition Term.h:94
Expression & m_expression()
Get read-write access to the expression of the term.
Definition Term.cpp:37
const Formula & formula() const
Get read-only access to the formula of the term.
Definition Term.cpp:38
Term()
Construct a new term object.
Definition Term.cpp:15
Formula & m_formula()
Get read-write access to the formula of the term.
Definition Term.cpp:39
Type type() const
Get read-only access to the type of the term.
Definition Term.cpp:35
void Check(Sort s) const
Check if this term can be matched with s.
Definition Term.cpp:72
Term Substitute(const Variable &v, const Term &t)
Create a new Term with substitutes the variable v in this term with t.
Definition Term.cpp:41
const Expression & expression() const
Get read-only access to the expression of the term.
Definition Term.cpp:36
Namespace for the VNNLIB parser of the dlinear library.
Definition Driver.cpp:17
Sort
Sort of a term.
Definition Sort.h:18
@ Int
Integer sort.
@ Binary
Binary sort.
@ Bool
Boolean sort.