Clingo
Loading...
Searching...
No Matches
term.hh
1#pragma once
2
3#include <clingo/input/attributes.hh>
4
5#include <clingo/core/fstring.hh>
6#include <clingo/core/location.hh>
7#include <clingo/core/symbol.hh>
8
9#include <clingo/util/hash.hh>
10#include <clingo/util/immutable_array.hh>
11#include <clingo/util/immutable_value.hh>
12#include <clingo/util/optional.hh>
13#include <clingo/util/ordered_set.hh>
14
15#include <utility>
16#include <variant>
17
18namespace CppClingo::Input {
19
22
27
29
30class TermVariable;
31class TermSymbol;
32class TermTuple;
33class TermFunction;
34class TermAbs;
35class TermUnary;
36class TermBinary;
38
40using Sig = std::tuple<String, size_t, bool>;
42using SharedSig = std::tuple<SharedString, size_t, bool>;
45
47using Term =
48 std::variant<TermVariable, TermSymbol, TermTuple, TermFunction, TermAbs, TermUnary, TermBinary, TermFormatString>;
49
52
54class Projection : public Expression<Projection> {
55 public:
57 static constexpr auto attributes() { return std::tuple{a_loc = &Projection::loc_}; }
58
60 explicit Projection(Location loc) : loc_{std::move(loc)} {}
62 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
63
64 private:
65 Location loc_;
66};
67
69using Argument = std::variant<Projection, Term>;
72
74class ArgumentTuple : public RecursiveExpression<ArgumentTuple> {
75 public:
77 static constexpr auto attributes() { return std::tuple{a_elems = &ArgumentTuple::elems_}; }
78
81
83 [[nodiscard]] auto elems() const -> ArgumentArray const & { return elems_; }
84
85 private:
86 ArgumentArray elems_;
87};
88
91
95class TermVariable : public Expression<TermVariable> {
96 public:
98 static constexpr auto attributes() {
99 return std::tuple{a_loc = &TermVariable::loc_, a_name = &TermVariable::name,
100 a_anonymous = &TermVariable::anonymous_};
101 }
102
104 explicit TermVariable(Location loc, String name, bool is_anonymous = false)
105 : loc_{std::move(loc)}, name_{name}, anonymous_{is_anonymous} {}
106
108 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
110 [[nodiscard]] auto name() const -> String const & { return *name_; }
112 [[nodiscard]] auto anonymous() const -> bool { return anonymous_; }
113
114 private:
115 Location loc_;
116 SharedString name_;
117 bool anonymous_;
118};
119
123class TermSymbol : public Expression<TermSymbol> {
124 public:
126 static constexpr auto attributes() { return std::tuple{a_loc = &TermSymbol::loc_, a_value = &TermSymbol::value}; }
127
129 explicit TermSymbol(Location loc, Symbol value) : loc_{std::move(loc)}, value_{value} {}
130
132 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
134 [[nodiscard]] auto value() const -> Symbol const & { return *value_; }
135
136 private:
137 Location loc_;
138 SharedSymbol value_;
139};
140
142class FormatFieldExpression : public RecursiveExpression<FormatFieldExpression> {
143 public:
145 static constexpr auto attributes() {
146 return std::tuple{a_loc = &FormatFieldExpression::loc_, a_lhs = &FormatFieldExpression::lhs_,
147 a_rhs = &FormatFieldExpression::rhs_};
148 }
149
152
154 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
156 [[nodiscard]] auto lhs() const -> Util::immutable_value<Term> const & { return lhs_; }
158 [[nodiscard]] auto rhs() const -> FormatSpec const & { return rhs_; }
160 [[nodiscard]] auto rhs_str() const -> std::string_view {
161 if (rhs_view_ == "i") {
163 out << rhs_;
164 rhs_view_ = out.str();
165 }
166 return rhs_view_;
167 }
168
169 private:
170 Location loc_;
172 FormatSpec rhs_;
173 mutable std::string rhs_view_ = "i";
174};
175
177class FormatFieldLiteral : public Expression<FormatFieldLiteral> {
178 public:
180 explicit FormatFieldLiteral(Location loc, SharedString value) : loc_{std::move(loc)}, value_{std::move(value)} {}
181
183 static constexpr auto attributes() {
184 return std::tuple{a_loc = &FormatFieldLiteral::loc_, a_value = &FormatFieldLiteral::value_};
185 }
186
188 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
190 [[nodiscard]] auto value() const -> String const & { return *value_; }
191
192 private:
193 Location loc_;
194 SharedString value_;
195};
196
198using FormatField = std::variant<FormatFieldLiteral, FormatFieldExpression>;
199
202
204class TermFormatString : public Expression<TermFormatString> {
205 public:
207 static constexpr auto attributes() {
208 return std::tuple{a_loc = &TermFormatString::loc_, a_elems = &TermFormatString::elems};
209 }
210
212 explicit TermFormatString(Location loc, FormatFieldArray elems) : loc_{std::move(loc)}, elems_{std::move(elems)} {}
214 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
216 [[nodiscard]] auto elems() const -> FormatFieldArray const & { return elems_; }
217
218 private:
219 Location loc_;
220 FormatFieldArray elems_;
221};
222
224using TupleElement = std::variant<ArgumentTuple, Term>;
227
231class TermTuple : public RecursiveExpression<TermTuple> {
232 public:
234 static constexpr auto attributes() { return std::tuple{a_loc = &TermTuple::loc_, a_pool = &TermTuple::pool_}; }
235
238
240 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
242 [[nodiscard]] auto pool() const -> TupleElementArray const & { return pool_; }
243
244 private:
245 Location loc_;
246 TupleElementArray pool_;
247};
248
252class TermFunction : public RecursiveExpression<TermFunction> {
253 public:
255 static constexpr auto attributes() {
256 return std::tuple{a_loc = &TermFunction::loc_, a_name = &TermFunction::name, a_pool = &TermFunction::pool_,
257 a_external = &TermFunction::external_};
258 }
259
265
267 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
269 [[nodiscard]] auto name() const -> String const & { return *name_; }
271 [[nodiscard]] auto pool() const -> PoolArray const & { return pool_; }
273 [[nodiscard]] auto external() const -> bool { return external_; }
274
275 private:
276 Location loc_;
277 SharedString name_;
278 PoolArray pool_;
279 bool external_;
280};
281
285class TermAbs : public RecursiveExpression<TermAbs> {
286 public:
288 static constexpr auto attributes() { return std::tuple{a_loc = &TermAbs::loc_, a_pool = &TermAbs::pool_}; }
289
293 explicit TermAbs(Location loc, TermArray pool);
294
296 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
298 [[nodiscard]] auto pool() const -> TermArray const & { return pool_; }
299
301 friend auto operator==(TermAbs const &a, TermAbs const &b) -> bool;
303 friend auto operator<=>(TermAbs const &a, TermAbs const &b) -> std::strong_ordering;
304
305 private:
306 Location loc_;
307 TermArray pool_;
308};
309
311enum class UnaryOperator : uint8_t {
312 minus,
313 negate,
314};
315
319class TermUnary : public RecursiveExpression<TermUnary> {
320 public:
322 static constexpr auto attributes() {
323 return std::tuple{a_loc = &TermUnary::loc_, a_op = &TermUnary::op_, a_rhs = &TermUnary::rhs_};
324 }
325
328
330 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
332 [[nodiscard]] auto op() const -> UnaryOperator { return op_; }
334 [[nodiscard]] auto rhs() const -> Util::immutable_value<Term> const & { return rhs_; }
335
336 private:
337 Location loc_;
338 UnaryOperator op_;
340};
341
343enum class BinaryOperator : uint8_t {
344 and_,
345 div,
346 minus,
347 mod,
348 times,
349 or_,
350 plus,
351 pow,
352 xor_,
353 dots,
354};
355
359class TermBinary : public RecursiveExpression<TermBinary> {
360 public:
362 static constexpr auto attributes() {
363 return std::tuple{a_loc = &TermBinary::loc_, a_lhs = &TermBinary::lhs_, a_op = &TermBinary::op_,
364 a_rhs = &TermBinary::rhs_};
365 }
366
370
372 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
374 [[nodiscard]] auto lhs() const -> Util::immutable_value<Term> const & { return lhs_; }
376 [[nodiscard]] auto rhs() const -> Util::immutable_value<Term> const & { return rhs_; }
378 [[nodiscard]] auto op() const -> BinaryOperator { return op_; }
379
380 private:
381 Location loc_;
384 BinaryOperator op_;
385};
386
388
389// FormatField
390
392 : loc_{std::move(loc)}, lhs_{std::move(lhs)}, rhs_{std::move(rhs)} {
393}
394
395inline auto operator==(FormatFieldExpression const &a, FormatFieldExpression const &b) -> bool {
396 return a.equal(b);
397}
398
399inline auto operator<=>(FormatFieldExpression const &a, FormatFieldExpression const &b) -> std::strong_ordering {
400 return a.compare(b);
401}
402
403// ArgumentTuple
404
405inline ArgumentTuple::ArgumentTuple(ArgumentArray elems) : elems_{std::move(elems)} {
406}
407
408inline auto operator==(ArgumentTuple const &a, ArgumentTuple const &b) -> bool {
409 return a.equal(b);
410}
411
412inline auto operator<=>(ArgumentTuple const &a, ArgumentTuple const &b) -> std::strong_ordering {
413 return a.compare(b);
414}
415
416// TermTuple
417
418inline TermTuple::TermTuple(Location loc, TupleElementArray pool) : loc_{std::move(loc)}, pool_{std::move(pool)} {
419}
420
421inline auto operator==(TermTuple const &a, TermTuple const &b) -> bool {
422 return a.equal(b);
423}
424
425inline auto operator<=>(TermTuple const &a, TermTuple const &b) -> std::strong_ordering {
426 return a.compare(b);
427}
428
429// TermFunction
430
431inline TermFunction::TermFunction(Location loc, String name, PoolArray pool, bool external)
432 : loc_{std::move(loc)}, name_(name), pool_{std::move(pool)}, external_{external} {
433}
434
435inline auto operator==(TermFunction const &a, TermFunction const &b) -> bool {
436 return a.equal(b);
437}
438
439inline auto operator<=>(TermFunction const &a, TermFunction const &b) -> std::strong_ordering {
440 return a.compare(b);
441}
442
443// TermAbs
444
445inline TermAbs::TermAbs(Location loc, TermArray pool) : loc_{std::move(loc)}, pool_{std::move(pool)} {
446}
447
448inline auto operator==(TermAbs const &a, TermAbs const &b) -> bool {
449 return a.equal(b);
450}
451
452inline auto operator<=>(TermAbs const &a, TermAbs const &b) -> std::strong_ordering {
453 return a.compare(b);
454}
455
456// TermUnary
457
459 : loc_{std::move(loc)}, op_{op}, rhs_{std::move(rhs)} {
460}
461
462inline auto operator==(TermUnary const &a, TermUnary const &b) -> bool {
463 return a.equal(b);
464}
465
466inline auto operator<=>(TermUnary const &a, TermUnary const &b) -> std::strong_ordering {
467 return a.compare(b);
468}
469
470// TermBinary
471
474 : loc_{std::move(loc)}, lhs_{std::move(lhs)}, rhs_{std::move(rhs)}, op_{op} {
475}
476
477inline auto operator==(TermBinary const &a, TermBinary const &b) -> bool {
478 return a.equal(b);
479}
480
481inline auto operator<=>(TermBinary const &a, TermBinary const &b) -> std::strong_ordering {
482 return a.compare(b);
483}
484
485} // namespace CppClingo::Input
An argument tuple for a function or directly a tuple.
Definition term.hh:74
auto elems() const -> ArgumentArray const &
The elements of the tuple.
Definition term.hh:83
ArgumentTuple(ArgumentArray elems)
Construct an argument tuple.
Definition term.hh:405
static constexpr auto attributes()
The record attributes.
Definition term.hh:77
A record that friend declares and defines comparison operators.
Definition attributes.hh:60
A format field with a variable and format specification.
Definition term.hh:142
auto loc() const -> Location const &
Get the location of the field.
Definition term.hh:154
auto rhs() const -> FormatSpec const &
Get the specification.
Definition term.hh:158
static constexpr auto attributes()
The record attributes.
Definition term.hh:145
auto rhs_str() const -> std::string_view
Get a string representation of the right-hand-side.
Definition term.hh:160
FormatFieldExpression(Location loc, Util::immutable_value< Term > lhs, FormatSpec rhs)
Construct a format field with a variable and format specification.
Definition term.hh:391
auto lhs() const -> Util::immutable_value< Term > const &
Get the variable and format specification.
Definition term.hh:156
A format field with a plain string value.
Definition term.hh:177
auto value() const -> String const &
Get the string value.
Definition term.hh:190
auto loc() const -> Location const &
Get the location of the literal.
Definition term.hh:188
static constexpr auto attributes()
The record attributes.
Definition term.hh:183
FormatFieldLiteral(Location loc, SharedString value)
Construct a format field with a string value.
Definition term.hh:180
Indicate a projected position.
Definition term.hh:54
static constexpr auto attributes()
The record attributes.
Definition term.hh:57
Projection(Location loc)
Construct a projection indicator.
Definition term.hh:60
auto loc() const -> Location const &
The location of the projected position.
Definition term.hh:62
A record that friend declares comparison operators.
Definition attributes.hh:49
Term representing the absolute function.
Definition term.hh:285
auto loc() const -> Location const &
The location of the function.
Definition term.hh:296
friend auto operator<=>(TermAbs const &a, TermAbs const &b) -> std::strong_ordering
Compare two absolute terms.
Definition term.hh:452
static constexpr auto attributes()
The record attributes.
Definition term.hh:288
friend auto operator==(TermAbs const &a, TermAbs const &b) -> bool
Compare two absolute terms.
Definition term.hh:448
auto pool() const -> TermArray const &
The argument pool of the absolute term.
Definition term.hh:298
TermAbs(Location loc, TermArray pool)
Construct an absolute term.
Definition term.hh:445
Term representing a binary operation.
Definition term.hh:359
TermBinary(Location loc, Util::immutable_value< Term > lhs, BinaryOperator op, Util::immutable_value< Term > rhs)
Construct a term for an binary operation.
Definition term.hh:472
auto op() const -> BinaryOperator
The operation.
Definition term.hh:378
auto rhs() const -> Util::immutable_value< Term > const &
The right-hand-side.
Definition term.hh:376
auto loc() const -> Location const &
The location of the function.
Definition term.hh:372
auto lhs() const -> Util::immutable_value< Term > const &
The left-hand-side.
Definition term.hh:374
static constexpr auto attributes()
The record attributes.
Definition term.hh:362
A format string term.
Definition term.hh:204
auto elems() const -> FormatFieldArray const &
The associated fields.
Definition term.hh:216
TermFormatString(Location loc, FormatFieldArray elems)
Construct a projection indicator.
Definition term.hh:212
auto loc() const -> Location const &
The location of the projected position.
Definition term.hh:214
static constexpr auto attributes()
The record attributes.
Definition term.hh:207
Term representing a symbolic or external function.
Definition term.hh:252
auto external() const -> bool
Whether this is an external function.
Definition term.hh:273
static constexpr auto attributes()
The record attributes.
Definition term.hh:255
auto pool() const -> PoolArray const &
The argument pool of the function.
Definition term.hh:271
auto name() const -> String const &
The name of the function.
Definition term.hh:269
TermFunction(Location loc, String name, PoolArray pool, bool external)
Construct a symbolic function.
Definition term.hh:431
auto loc() const -> Location const &
The location of the function.
Definition term.hh:267
Term representing a symbol.
Definition term.hh:123
static constexpr auto attributes()
The record attributes.
Definition term.hh:126
auto value() const -> Symbol const &
The associated symbol.
Definition term.hh:134
TermSymbol(Location loc, Symbol value)
Construct term with the given symbol.
Definition term.hh:129
auto loc() const -> Location const &
The location of the symbol.
Definition term.hh:132
Term representing a tuple.
Definition term.hh:231
auto loc() const -> Location const &
The location of the tuple.
Definition term.hh:240
auto pool() const -> TupleElementArray const &
The argument pool of the tuple.
Definition term.hh:242
static constexpr auto attributes()
The record attributes.
Definition term.hh:234
TermTuple(Location loc, TupleElementArray pool)
Construct a tuple.
Definition term.hh:418
Term representing an unary operation.
Definition term.hh:319
auto rhs() const -> Util::immutable_value< Term > const &
The right-hand-side.
Definition term.hh:334
static constexpr auto attributes()
The record attributes.
Definition term.hh:322
auto loc() const -> Location const &
The location of the function.
Definition term.hh:330
auto op() const -> UnaryOperator
The operation.
Definition term.hh:332
TermUnary(Location loc, UnaryOperator op, Util::immutable_value< Term > rhs)
Construct a term for an unary operation.
Definition term.hh:458
Term representing a variable.
Definition term.hh:95
static constexpr auto attributes()
The record attributes.
Definition term.hh:98
auto name() const -> String const &
The name of the variable.
Definition term.hh:110
auto loc() const -> Location const &
The location of the variable.
Definition term.hh:108
auto anonymous() const -> bool
Whether the variable is anonymous.
Definition term.hh:112
TermVariable(Location loc, String name, bool is_anonymous=false)
Construct a variable.
Definition term.hh:104
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
Create an output buffer that bears some similarities with C++'s iostreams.
Definition print.hh:24
auto str() const -> std::string
Get a string with the current buffer content.
Definition print.hh:69
An immutable value imlementation.
Definition immutable_value.hh:19
auto location(T const &x) -> Location const &
Get the location of an expression.
Definition location.hh:123
Util::unordered_set< String > StringSet
A set of strings.
Definition symbol.hh:83
std::vector< String > StringVec
A vector of strings.
Definition symbol.hh:85
constexpr auto a_elems
Definition attributes.hh:23
constexpr auto a_lhs
Definition attributes.hh:27
constexpr auto a_external
Definition attributes.hh:24
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_loc
Definition attributes.hh:29
constexpr auto a_op
Definition attributes.hh:32
constexpr auto a_value
Definition attributes.hh:44
constexpr auto a_pool
Definition attributes.hh:34
Util::ordered_set< SharedSig > SharedSigSet
A set of predicate signatures.
Definition term.hh:44
std::tuple< String, size_t, bool > Sig
The signature of a predicate.
Definition term.hh:40
BinaryOperator
Enumeration of available binary operators.
Definition term.hh:343
StringVec VariableVec
A vector of variable names.
Definition term.hh:26
StringSet VariableSet
A set of variable names.
Definition term.hh:24
std::variant< TermVariable, TermSymbol, TermTuple, TermFunction, TermAbs, TermUnary, TermBinary, TermFormatString > Term
Variant holding the different term types.
Definition term.hh:48
std::tuple< SharedString, size_t, bool > SharedSig
The signature of a predicate.
Definition term.hh:42
std::variant< ArgumentTuple, Term > TupleElement
A tuple element.
Definition term.hh:224
std::variant< Projection, Term > Argument
A variant capturing either a term or a position that is to be projected.
Definition term.hh:69
std::variant< FormatFieldLiteral, FormatFieldExpression > FormatField
A format field (either a plain string or a term with format specification).
Definition term.hh:198
UnaryOperator
Enumeration of available unary operators.
Definition term.hh:311
@ pow
The exponentiation arithmetic operation.
@ div
The (integer) divide arithmetic operation.
@ xor_
The XOR bit operation.
@ and_
The AND bit operation.
@ mod
The modulo arithmetic operation.
@ or_
The OR bit operation.
@ plus
The plus arithmetic operation.
@ dots
The interval operator.
@ times
The multiply arithmetic operation.
@ negate
The unary negation sign (~).
@ minus
The unary minus sign (-).
tsl::ordered_set< Key, Hash, KeyEqual, Allocator, ValueTypeContainer, IndexType > ordered_set
Alias for ordered sets.
Definition ordered_set.hh:15
Format specification for a field.
Definition fstring.hh:14