Clingo
Loading...
Searching...
No Matches
theory.hh
1#pragma once
2
3#include <clingo/input/literal.hh>
4#include <clingo/input/term.hh>
5
6#include <utility>
7
8namespace CppClingo::Input {
9
12
13class TheoryTermSymbol;
14class TheoryTermVariable;
15class TheoryTermTuple;
16class TheoryTermFunction;
17class TheoryTermUnparsed;
18class UnparsedElement;
19
22 std::variant<TheoryTermSymbol, TheoryTermVariable, TheoryTermTuple, TheoryTermFunction, TheoryTermUnparsed>;
25
29class TheoryTermSymbol : public Expression<TheoryTermSymbol> {
30 public:
32 static constexpr auto attributes() {
33 return std::tuple{a_loc = &TheoryTermSymbol::loc_, a_value = &TheoryTermSymbol::value};
34 }
35
37 explicit TheoryTermSymbol(Location loc, Symbol value) : loc_{std::move(loc)}, value_{value} {}
38
40 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
42 [[nodiscard]] auto value() const -> Symbol const & { return *value_; }
43
44 private:
45 Location loc_;
46 SharedSymbol value_;
47};
48
52class TheoryTermVariable : public Expression<TheoryTermVariable> {
53 public:
55 static constexpr auto attributes() {
56 return std::tuple{a_loc = &TheoryTermVariable::loc_, a_name = &TheoryTermVariable::name,
57 a_anonymous = &TheoryTermVariable::anonymous_};
58 }
59
61 explicit TheoryTermVariable(Location loc, String name, bool is_anonymous = false)
62 : loc_{std::move(loc)}, name_{name}, anonymous_{is_anonymous} {}
63
65 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
67 [[nodiscard]] auto name() const -> String const & { return *name_; }
69 [[nodiscard]] auto anonymous() const -> bool { return anonymous_; }
70
71 private:
72 Location loc_;
73 SharedString name_;
74 bool anonymous_;
75};
76
80class TheoryTermTuple : public RecursiveExpression<TheoryTermTuple> {
81 public:
83 static constexpr auto attributes() {
84 return std::tuple{a_loc = &TheoryTermTuple::loc_, a_type = &TheoryTermTuple::type_,
85 a_elems = &TheoryTermTuple::elems_};
86 }
87
90
92 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
94 [[nodiscard]] auto type() const -> TheoryTermTupleType { return type_; }
96 [[nodiscard]] auto elems() const -> TheoryTermArray const & { return elems_; }
97
98 private:
99 Location loc_;
101 TheoryTermArray elems_;
102};
103
107class TheoryTermFunction : public RecursiveExpression<TheoryTermFunction> {
108 public:
110 static constexpr auto attributes() {
111 return std::tuple{a_loc = &TheoryTermFunction::loc_, a_name = &TheoryTermFunction::name,
112 a_args = &TheoryTermFunction::args_};
113 }
114
119
121 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
123 [[nodiscard]] auto name() const -> String const & { return *name_; }
125 [[nodiscard]] auto args() const -> TheoryTermArray const & { return args_; }
126
127 private:
128 Location loc_;
129 SharedString name_;
130 TheoryTermArray args_;
131};
132
138
145class TheoryTermUnparsed : public RecursiveExpression<TheoryTermUnparsed> {
146 public:
148 static constexpr auto attributes() {
149 return std::tuple{a_loc = &TheoryTermUnparsed::loc_, a_elems = &TheoryTermUnparsed::elems_};
150 }
151
154
156 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
158 [[nodiscard]] auto elems() const -> UnparsedElementArray const & { return elems_; }
159
160 private:
161 Location loc_;
163};
164
166class UnparsedElement : public Expression<UnparsedElement> {
167 public:
169 static constexpr auto attributes() {
170 return std::tuple{a_ops = &UnparsedElement::ops, a_term = &UnparsedElement::term_};
171 }
172
174 UnparsedElement(StringSpan ops, TheoryTerm term) : ops_{ops.begin(), ops.end()}, term_{std::move(term)} {}
176 UnparsedElement(SharedStringArray ops, TheoryTerm term) : ops_{std::move(ops)}, term_{std::move(term)} {}
177
179 [[nodiscard]] auto ops() const -> StringSpan { return as_string_span(ops_); }
181 [[nodiscard]] auto term() const -> TheoryTerm const & { return term_; }
182
183 private:
185 TheoryTerm term_;
186};
187
189class TheoryRGuard : public Expression<TheoryRGuard> {
190 public:
192 static constexpr auto attributes() { return std::tuple{a_op = &TheoryRGuard::op, a_term = &TheoryRGuard::term_}; }
193
195 explicit TheoryRGuard(String op, TheoryTerm term) : op_{op}, term_{std::move(term)} {}
196
198 [[nodiscard]] auto op() const -> String const & { return *op_; }
200 [[nodiscard]] auto term() const -> TheoryTerm const & { return term_; }
201
202 private:
203 SharedString op_;
204 TheoryTerm term_;
205};
206
208class TheoryElement : public Expression<TheoryElement> {
209 public:
211 static constexpr auto attributes() {
212 return std::tuple{a_loc = &TheoryElement::loc_, a_tuple = &TheoryElement::tuple_,
213 a_cond = &TheoryElement::cond_};
214 }
215
218 : loc_{std::move(loc)}, tuple_{std::move(tuple)}, cond_{std::move(cond)} {}
219
221 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
223 [[nodiscard]] auto tuple() const -> TheoryTermArray const & { return tuple_; }
225 [[nodiscard]] auto cond() const -> LitArray const & { return cond_; }
226
227 private:
228 Location loc_;
229 TheoryTermArray tuple_;
230 LitArray cond_;
231};
234
238template <bool HasSign>
239class TheoryAtom : public std::conditional_t<HasSign, Signed, Unsigned>, public Expression<TheoryAtom<HasSign>> {
240 public:
242 using Base = std::conditional_t<HasSign, Signed, Unsigned>;
243
245 static constexpr auto attributes() {
246 if constexpr (HasSign) {
247 return std::tuple{a_loc = &TheoryAtom::loc_, a_sign = &Signed::sign, a_name = &TheoryAtom::name_,
248 a_elems = &TheoryAtom::elems_, a_rhs = &TheoryAtom::rhs_};
249 } else {
250 return std::tuple{a_loc = &TheoryAtom::loc_, a_name = &TheoryAtom::name_, a_elems = &TheoryAtom::elems_,
251 a_rhs = &TheoryAtom::rhs_};
252 }
253 }
254
256 explicit TheoryAtom(Location loc, Term name, TheoryElementArray elems, std::optional<TheoryRGuard> rhs)
257 : loc_{std::move(loc)}, name_{std::move(name)}, elems_{std::move(elems)}, rhs_{std::move(rhs)} {
258 static_assert(!HasSign);
259 }
260
262 explicit TheoryAtom(Location loc, Sign sign, Term name, TheoryElementArray elems, std::optional<TheoryRGuard> rhs)
263 : Signed{sign}, loc_{std::move(loc)}, name_{std::move(name)}, elems_{std::move(elems)}, rhs_{std::move(rhs)} {
264 static_assert(HasSign);
265 }
266
268 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
270 [[nodiscard]] auto name() const -> Term const & { return name_; }
272 [[nodiscard]] auto elems() const -> TheoryElementArray const & { return elems_; }
274 [[nodiscard]] auto rhs() const -> std::optional<TheoryRGuard> const & { return rhs_; }
275
276 private:
277 Location loc_;
278 Term name_;
279 TheoryElementArray elems_;
280 std::optional<TheoryRGuard> rhs_;
281};
282
285
288
290
291// TheoryTermTuple
292
294 : loc_{std::move(loc)}, type_(type), elems_{std::move(elems)} {
295}
296
297inline auto operator==(TheoryTermTuple const &a, TheoryTermTuple const &b) -> bool {
298 return a.equal(b);
299}
300
301inline auto operator<=>(TheoryTermTuple const &a, TheoryTermTuple const &b) -> std::strong_ordering {
302 return a.compare(b);
303}
304
305// TheoryTermFunction
306
308 : loc_{std::move(loc)}, name_(name), args_{std::move(args)} {
309}
310
312 : TheoryTermFunction{std::move(loc), name, {}} {
313}
314
315inline auto operator==(TheoryTermFunction const &a, TheoryTermFunction const &b) -> bool {
316 return a.equal(b);
317}
318
319inline auto operator<=>(TheoryTermFunction const &a, TheoryTermFunction const &b) -> std::strong_ordering {
320 return a.compare(b);
321}
322
323// TheoryTermUnparsed
324
325inline TheoryTermUnparsed::TheoryTermUnparsed(Location loc, UnparsedElementArray elems)
326 : loc_{std::move(loc)}, elems_{std::move(elems)} {
327}
328
329inline auto operator==(TheoryTermUnparsed const &a, TheoryTermUnparsed const &b) -> bool {
330 return a.equal(b);
331}
332
333inline auto operator<=>(TheoryTermUnparsed const &a, TheoryTermUnparsed const &b) -> std::strong_ordering {
334 return a.compare(b);
335}
336
337} // namespace CppClingo::Input
A record that friend declares and defines comparison operators.
Definition attributes.hh:60
A record that friend declares comparison operators.
Definition attributes.hh:49
Simple class with a sign.
Definition literal.hh:15
auto sign() const -> Sign
The sign of the class.
Definition literal.hh:20
A theory atom.
Definition theory.hh:239
auto loc() const -> Location const &
The location of the symbol.
Definition theory.hh:268
static constexpr auto attributes()
The record attributes.
Definition theory.hh:245
auto rhs() const -> std::optional< TheoryRGuard > const &
The optional right guard of the atom.
Definition theory.hh:274
auto elems() const -> TheoryElementArray const &
The elements of the atom.
Definition theory.hh:272
TheoryAtom(Location loc, Sign sign, Term name, TheoryElementArray elems, std::optional< TheoryRGuard > rhs)
Construct a theory atom.
Definition theory.hh:262
std::conditional_t< HasSign, Signed, Unsigned > Base
The parent class.
Definition theory.hh:242
auto name() const -> Term const &
The name of the atom.
Definition theory.hh:270
TheoryAtom(Location loc, Term name, TheoryElementArray elems, std::optional< TheoryRGuard > rhs)
Construct a theory atom.
Definition theory.hh:256
An element of the theory atom.
Definition theory.hh:208
auto tuple() const -> TheoryTermArray const &
The tuple of the theory element.
Definition theory.hh:223
auto cond() const -> LitArray const &
The condition of the theory element.
Definition theory.hh:225
static constexpr auto attributes()
The record attributes.
Definition theory.hh:211
TheoryElement(Location loc, TheoryTermArray tuple, LitArray cond)
Construct a theory element.
Definition theory.hh:217
auto loc() const -> Location const &
The location of the theory element.
Definition theory.hh:221
The right guard of the theory atom.
Definition theory.hh:189
static constexpr auto attributes()
The record attributes.
Definition theory.hh:192
auto op() const -> String const &
The tuple of the theory element.
Definition theory.hh:198
auto term() const -> TheoryTerm const &
The condition of the theory element.
Definition theory.hh:200
TheoryRGuard(String op, TheoryTerm term)
Construct the guard.
Definition theory.hh:195
A theory term function.
Definition theory.hh:107
static constexpr auto attributes()
The record attributes.
Definition theory.hh:110
auto loc() const -> Location const &
The location of the symbol.
Definition theory.hh:121
auto name() const -> String const &
The name of the function.
Definition theory.hh:123
TheoryTermFunction(Location loc, String name)
Construct a function theory term.
Definition theory.hh:311
auto args() const -> TheoryTermArray const &
The arguments of the function.
Definition theory.hh:125
A symbolic theory term.
Definition theory.hh:29
auto loc() const -> Location const &
The location of the symbol.
Definition theory.hh:40
static constexpr auto attributes()
The record attributes.
Definition theory.hh:32
TheoryTermSymbol(Location loc, Symbol value)
Construct a symbolic theory term.
Definition theory.hh:37
auto value() const -> Symbol const &
The symbol.
Definition theory.hh:42
A tuple (set or list) theory term.
Definition theory.hh:80
auto loc() const -> Location const &
The location of the symbol.
Definition theory.hh:92
auto elems() const -> TheoryTermArray const &
The elements of the tuple.
Definition theory.hh:96
auto type() const -> TheoryTermTupleType
The type of the term.
Definition theory.hh:94
static constexpr auto attributes()
The record attributes.
Definition theory.hh:83
TheoryTermTuple(Location loc, TheoryTermTupleType type, TheoryTermArray elems)
Construct a tuple theory term.
Definition theory.hh:293
An unparsed theory term.
Definition theory.hh:145
auto loc() const -> Location const &
The location of the symbol.
Definition theory.hh:156
auto elems() const -> UnparsedElementArray const &
The vector of elements.
Definition theory.hh:158
static constexpr auto attributes()
The record attributes.
Definition theory.hh:148
A variable theory term.
Definition theory.hh:52
auto loc() const -> Location const &
The location of the variable.
Definition theory.hh:65
TheoryTermVariable(Location loc, String name, bool is_anonymous=false)
Construct a variable theory term.
Definition theory.hh:61
static constexpr auto attributes()
The record attributes.
Definition theory.hh:55
auto name() const -> String const &
The name of the variable.
Definition theory.hh:67
auto anonymous() const -> bool
Whether the variable is anonymous.
Definition theory.hh:69
An element having the form of a right guard.
Definition theory.hh:166
auto term() const -> TheoryTerm const &
The term.
Definition theory.hh:181
static constexpr auto attributes()
The record attributes.
Definition theory.hh:169
auto ops() const -> StringSpan
The list of operator names.
Definition theory.hh:179
UnparsedElement(SharedStringArray ops, TheoryTerm term)
Construct the element.
Definition theory.hh:176
UnparsedElement(StringSpan ops, TheoryTerm term)
Construct the element.
Definition theory.hh:174
The Location of an expression in an input source.
Definition location.hh:44
Class managing the lifetime of a String.
Definition symbol.hh:93
Class managing the lifetime of a Symbol.
Definition symbol.hh:306
Reference to a string stored in a symbol store.
Definition symbol.hh:18
Variant-like class to store symbols stored in a symbol store.
Definition symbol.hh:225
auto as_string_span(T const &vec) -> StringSpan
Convert a collection of shared strings into a string span.
Definition symbol.hh:196
std::span< String const > StringSpan
A span of strings.
Definition symbol.hh:87
TheoryTermTupleType
Enumeration of theory term tuple types.
Definition core.hh:165
Sign
Enumeration of signs (default negation).
Definition core.hh:16
constexpr auto a_sign
Definition attributes.hh:37
constexpr auto a_elems
Definition attributes.hh:23
constexpr auto a_cond
Definition attributes.hh:20
constexpr auto a_ops
Definition attributes.hh:33
constexpr auto a_args
Definition attributes.hh:15
constexpr auto a_name
Definition attributes.hh:30
constexpr auto a_anonymous
Definition attributes.hh:14
constexpr auto a_rhs
Definition attributes.hh:36
constexpr auto a_tuple
Definition attributes.hh:42
constexpr auto a_type
Definition attributes.hh:43
constexpr auto a_loc
Definition attributes.hh:29
constexpr auto a_op
Definition attributes.hh:32
constexpr auto a_term
Definition attributes.hh:41
constexpr auto a_value
Definition attributes.hh:44
std::variant< TermVariable, TermSymbol, TermTuple, TermFunction, TermAbs, TermUnary, TermBinary > Term
Variant holding the different term types.
Definition term.hh:45
std::variant< TheoryTermSymbol, TheoryTermVariable, TheoryTermTuple, TheoryTermFunction, TheoryTermUnparsed > TheoryTerm
A variant for the different theory terms.
Definition theory.hh:22