Clingo
Loading...
Searching...
No Matches
term.hh
1#pragma once
2
3#include <clingo/input/attributes.hh>
4
5#include <clingo/core/location.hh>
6#include <clingo/core/symbol.hh>
7
8#include <clingo/util/hash.hh>
9#include <clingo/util/immutable_array.hh>
10#include <clingo/util/immutable_value.hh>
11#include <clingo/util/optional.hh>
12#include <clingo/util/ordered_set.hh>
13
14#include <utility>
15#include <variant>
16
17namespace CppClingo::Input {
18
21
26
28
29class TermVariable;
30class TermSymbol;
31class TermTuple;
32class TermFunction;
33class TermAbs;
34class TermUnary;
35class TermBinary;
36
38using Sig = std::tuple<String, size_t, bool>;
40using SharedSig = std::tuple<SharedString, size_t, bool>;
43
45using Term = std::variant<TermVariable, TermSymbol, TermTuple, TermFunction, TermAbs, TermUnary, TermBinary>;
46
49
51class Projection : public Expression<Projection> {
52 public:
54 static constexpr auto attributes() { return std::tuple{a_loc = &Projection::loc_}; }
55
57 explicit Projection(Location loc) : loc_{std::move(loc)} {}
59 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
60
61 private:
62 Location loc_;
63};
64
66using Argument = std::variant<Projection, Term>;
69
71class ArgumentTuple : public RecursiveExpression<ArgumentTuple> {
72 public:
74 static constexpr auto attributes() { return std::tuple{a_elems = &ArgumentTuple::elems_}; }
75
78
80 [[nodiscard]] auto elems() const -> ArgumentArray const & { return elems_; }
81
82 private:
83 ArgumentArray elems_;
84};
85
88
92class TermVariable : public Expression<TermVariable> {
93 public:
95 static constexpr auto attributes() {
96 return std::tuple{a_loc = &TermVariable::loc_, a_name = &TermVariable::name,
97 a_anonymous = &TermVariable::anonymous_};
98 }
99
101 explicit TermVariable(Location loc, String name, bool is_anonymous = false)
102 : loc_{std::move(loc)}, name_{name}, anonymous_{is_anonymous} {}
103
105 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
107 [[nodiscard]] auto name() const -> String const & { return *name_; }
109 [[nodiscard]] auto anonymous() const -> bool { return anonymous_; }
110
111 private:
112 Location loc_;
113 SharedString name_;
114 bool anonymous_;
115};
116
120class TermSymbol : public Expression<TermSymbol> {
121 public:
123 static constexpr auto attributes() { return std::tuple{a_loc = &TermSymbol::loc_, a_value = &TermSymbol::value}; }
124
126 explicit TermSymbol(Location loc, Symbol value) : loc_{std::move(loc)}, value_{value} {}
127
129 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
131 [[nodiscard]] auto value() const -> Symbol const & { return *value_; }
132
133 private:
134 Location loc_;
135 SharedSymbol value_;
136};
137
139using TupleElement = std::variant<ArgumentTuple, Term>;
142
146class TermTuple : public RecursiveExpression<TermTuple> {
147 public:
149 static constexpr auto attributes() { return std::tuple{a_loc = &TermTuple::loc_, a_pool = &TermTuple::pool_}; }
150
153
155 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
157 [[nodiscard]] auto pool() const -> TupleElementArray const & { return pool_; }
158
159 private:
160 Location loc_;
161 TupleElementArray pool_;
162};
163
167class TermFunction : public RecursiveExpression<TermFunction> {
168 public:
170 static constexpr auto attributes() {
171 return std::tuple{a_loc = &TermFunction::loc_, a_name = &TermFunction::name, a_pool = &TermFunction::pool_,
172 a_external = &TermFunction::external_};
173 }
174
180
182 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
184 [[nodiscard]] auto name() const -> String const & { return *name_; }
186 [[nodiscard]] auto pool() const -> PoolArray const & { return pool_; }
188 [[nodiscard]] auto external() const -> bool { return external_; }
189
190 private:
191 Location loc_;
192 SharedString name_;
193 PoolArray pool_;
194 bool external_;
195};
196
200class TermAbs : public RecursiveExpression<TermAbs> {
201 public:
203 static constexpr auto attributes() { return std::tuple{a_loc = &TermAbs::loc_, a_pool = &TermAbs::pool_}; }
204
208 explicit TermAbs(Location loc, TermArray pool);
209
211 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
213 [[nodiscard]] auto pool() const -> TermArray const & { return pool_; }
214
216 friend auto operator==(TermAbs const &a, TermAbs const &b) -> bool;
218 friend auto operator<=>(TermAbs const &a, TermAbs const &b) -> std::strong_ordering;
219
220 private:
221 Location loc_;
222 TermArray pool_;
223};
224
226enum class UnaryOperator : uint8_t {
227 minus,
228 negate,
229};
230
234class TermUnary : public RecursiveExpression<TermUnary> {
235 public:
237 static constexpr auto attributes() {
238 return std::tuple{a_loc = &TermUnary::loc_, a_op = &TermUnary::op_, a_rhs = &TermUnary::rhs_};
239 }
240
243
245 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
247 [[nodiscard]] auto op() const -> UnaryOperator { return op_; }
249 [[nodiscard]] auto rhs() const -> Util::immutable_value<Term> const & { return rhs_; }
250
251 private:
252 Location loc_;
253 UnaryOperator op_;
255};
256
258enum class BinaryOperator : uint8_t {
259 and_,
260 div,
261 minus,
262 mod,
263 times,
264 or_,
265 plus,
266 pow,
267 xor_,
268 dots,
269};
270
274class TermBinary : public RecursiveExpression<TermBinary> {
275 public:
277 static constexpr auto attributes() {
278 return std::tuple{a_loc = &TermBinary::loc_, a_lhs = &TermBinary::lhs_, a_op = &TermBinary::op_,
279 a_rhs = &TermBinary::rhs_};
280 }
281
285
287 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
289 [[nodiscard]] auto lhs() const -> Util::immutable_value<Term> const & { return lhs_; }
291 [[nodiscard]] auto rhs() const -> Util::immutable_value<Term> const & { return rhs_; }
293 [[nodiscard]] auto op() const -> BinaryOperator { return op_; }
294
295 private:
296 Location loc_;
299 BinaryOperator op_;
300};
301
303
304// ArgumentTuple
305
306inline ArgumentTuple::ArgumentTuple(ArgumentArray elems) : elems_{std::move(elems)} {
307}
308
309inline auto operator==(ArgumentTuple const &a, ArgumentTuple const &b) -> bool {
310 return a.equal(b);
311}
312
313inline auto operator<=>(ArgumentTuple const &a, ArgumentTuple const &b) -> std::strong_ordering {
314 return a.compare(b);
315}
316
317// TermTuple
318
319inline TermTuple::TermTuple(Location loc, TupleElementArray pool) : loc_{std::move(loc)}, pool_{std::move(pool)} {
320}
321
322inline auto operator==(TermTuple const &a, TermTuple const &b) -> bool {
323 return a.equal(b);
324}
325
326inline auto operator<=>(TermTuple const &a, TermTuple const &b) -> std::strong_ordering {
327 return a.compare(b);
328}
329
330// TermFunction
331
332inline TermFunction::TermFunction(Location loc, String name, PoolArray pool, bool external)
333 : loc_{std::move(loc)}, name_(name), pool_{std::move(pool)}, external_{external} {
334}
335
336inline auto operator==(TermFunction const &a, TermFunction const &b) -> bool {
337 return a.equal(b);
338}
339
340inline auto operator<=>(TermFunction const &a, TermFunction const &b) -> std::strong_ordering {
341 return a.compare(b);
342}
343
344// TermAbs
345
346inline TermAbs::TermAbs(Location loc, TermArray pool) : loc_{std::move(loc)}, pool_{std::move(pool)} {
347}
348
349inline auto operator==(TermAbs const &a, TermAbs const &b) -> bool {
350 return a.equal(b);
351}
352
353inline auto operator<=>(TermAbs const &a, TermAbs const &b) -> std::strong_ordering {
354 return a.compare(b);
355}
356
357// TermUnary
358
360 : loc_{std::move(loc)}, op_{op}, rhs_{std::move(rhs)} {
361}
362
363inline auto operator==(TermUnary const &a, TermUnary const &b) -> bool {
364 return a.equal(b);
365}
366
367inline auto operator<=>(TermUnary const &a, TermUnary const &b) -> std::strong_ordering {
368 return a.compare(b);
369}
370
371// TermBinary
372
375 : loc_{std::move(loc)}, lhs_{std::move(lhs)}, rhs_{std::move(rhs)}, op_{op} {
376}
377
378inline auto operator==(TermBinary const &a, TermBinary const &b) -> bool {
379 return a.equal(b);
380}
381
382inline auto operator<=>(TermBinary const &a, TermBinary const &b) -> std::strong_ordering {
383 return a.compare(b);
384}
385
386} // namespace CppClingo::Input
An argument tuple for a function or directly a tuple.
Definition term.hh:71
auto elems() const -> ArgumentArray const &
The elements of the tuple.
Definition term.hh:80
ArgumentTuple(ArgumentArray elems)
Construct an argument tuple.
Definition term.hh:306
static constexpr auto attributes()
The record attributes.
Definition term.hh:74
A record that friend declares and defines comparison operators.
Definition attributes.hh:60
Indicate a projected position.
Definition term.hh:51
static constexpr auto attributes()
The record attributes.
Definition term.hh:54
Projection(Location loc)
Construct a projection indicator.
Definition term.hh:57
auto loc() const -> Location const &
The location of the projected position.
Definition term.hh:59
A record that friend declares comparison operators.
Definition attributes.hh:49
Term representing the absolute function.
Definition term.hh:200
auto loc() const -> Location const &
The location of the function.
Definition term.hh:211
friend auto operator<=>(TermAbs const &a, TermAbs const &b) -> std::strong_ordering
Compare two absolute terms.
Definition term.hh:353
static constexpr auto attributes()
The record attributes.
Definition term.hh:203
friend auto operator==(TermAbs const &a, TermAbs const &b) -> bool
Compare two absolute terms.
Definition term.hh:349
auto pool() const -> TermArray const &
The argument pool of the absolute term.
Definition term.hh:213
TermAbs(Location loc, TermArray pool)
Construct an absolute term.
Definition term.hh:346
Term representing a binary operation.
Definition term.hh:274
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:373
auto op() const -> BinaryOperator
The operation.
Definition term.hh:293
auto rhs() const -> Util::immutable_value< Term > const &
The right-hand-side.
Definition term.hh:291
auto loc() const -> Location const &
The location of the function.
Definition term.hh:287
auto lhs() const -> Util::immutable_value< Term > const &
The left-hand-side.
Definition term.hh:289
static constexpr auto attributes()
The record attributes.
Definition term.hh:277
Term representing a symbolic or external function.
Definition term.hh:167
auto external() const -> bool
Whether this is an external function.
Definition term.hh:188
static constexpr auto attributes()
The record attributes.
Definition term.hh:170
auto pool() const -> PoolArray const &
The argument pool of the function.
Definition term.hh:186
auto name() const -> String const &
The name of the function.
Definition term.hh:184
TermFunction(Location loc, String name, PoolArray pool, bool external)
Construct a symbolic function.
Definition term.hh:332
auto loc() const -> Location const &
The location of the function.
Definition term.hh:182
Term representing a symbol.
Definition term.hh:120
static constexpr auto attributes()
The record attributes.
Definition term.hh:123
auto value() const -> Symbol const &
The associated symbol.
Definition term.hh:131
TermSymbol(Location loc, Symbol value)
Construct term with the given symbol.
Definition term.hh:126
auto loc() const -> Location const &
The location of the symbol.
Definition term.hh:129
Term representing a tuple.
Definition term.hh:146
auto loc() const -> Location const &
The location of the tuple.
Definition term.hh:155
auto pool() const -> TupleElementArray const &
The argument pool of the tuple.
Definition term.hh:157
static constexpr auto attributes()
The record attributes.
Definition term.hh:149
TermTuple(Location loc, TupleElementArray pool)
Construct a tuple.
Definition term.hh:319
Term representing an unary operation.
Definition term.hh:234
auto rhs() const -> Util::immutable_value< Term > const &
The right-hand-side.
Definition term.hh:249
static constexpr auto attributes()
The record attributes.
Definition term.hh:237
auto loc() const -> Location const &
The location of the function.
Definition term.hh:245
auto op() const -> UnaryOperator
The operation.
Definition term.hh:247
TermUnary(Location loc, UnaryOperator op, Util::immutable_value< Term > rhs)
Construct a term for an unary operation.
Definition term.hh:359
Term representing a variable.
Definition term.hh:92
static constexpr auto attributes()
The record attributes.
Definition term.hh:95
auto name() const -> String const &
The name of the variable.
Definition term.hh:107
auto loc() const -> Location const &
The location of the variable.
Definition term.hh:105
auto anonymous() const -> bool
Whether the variable is anonymous.
Definition term.hh:109
TermVariable(Location loc, String name, bool is_anonymous=false)
Construct a variable.
Definition term.hh:101
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
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
std::variant< TermVariable, TermSymbol, TermTuple, TermFunction, TermAbs, TermUnary, TermBinary > Term
Variant holding the different term types.
Definition term.hh:45
Util::ordered_set< SharedSig > SharedSigSet
A set of predicate signatures.
Definition term.hh:42
std::tuple< String, size_t, bool > Sig
The signature of a predicate.
Definition term.hh:38
BinaryOperator
Enumeration of available binary operators.
Definition term.hh:258
StringVec VariableVec
A vector of variable names.
Definition term.hh:25
StringSet VariableSet
A set of variable names.
Definition term.hh:23
std::tuple< SharedString, size_t, bool > SharedSig
The signature of a predicate.
Definition term.hh:40
std::variant< ArgumentTuple, Term > TupleElement
A tuple element.
Definition term.hh:139
std::variant< Projection, Term > Argument
A variant capturing either a term or a position that is to be projected.
Definition term.hh:66
UnaryOperator
Enumeration of available unary operators.
Definition term.hh:226
@ 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