Clingo
Loading...
Searching...
No Matches
symbol.hh
1#pragma once
2
3#include <clingo/core.hh>
4
5#include <clingo/symbol.h>
6
7#include <optional>
8#include <ostream>
9#include <span>
10
11namespace Clingo {
12
19
35
36class Symbol;
38using SymbolSpan = std::span<Symbol const>;
40using SymbolList = std::initializer_list<Symbol const>;
42using SymbolVector = std::vector<Symbol>;
43
52[[nodiscard]] inline auto cpp_cast(clingo_symbol_t const *sym) -> Symbol const * {
53 return reinterpret_cast<Symbol const *>(sym); // NOLINT
54}
55
68class Symbol {
69 public:
71 Symbol() = default;
74
78 Symbol(Symbol const &other) noexcept : rep_{other.rep_} { clingo_symbol_acquire(rep_); }
83 auto operator=(Symbol const &other) noexcept -> Symbol & {
86 rep_ = other.rep_;
87 return *this;
88 }
89
93 Symbol(Symbol &&other) noexcept : rep_{std::exchange(other.rep_, 0)} {}
98 auto operator=(Symbol &&other) noexcept -> Symbol & {
99 if (rep_ != other.rep_) {
101 rep_ = std::exchange(other.rep_, 0);
102 }
103 return *this;
104 }
105
112 explicit Symbol(clingo_symbol_t rep, bool acquire) : rep_{rep} {
113 if (acquire) {
115 }
116 }
117
122 [[nodiscard]] friend auto c_cast(Symbol const &sym) -> clingo_symbol_t const & { return sym.rep_; }
127 [[nodiscard]] friend auto c_cast(Symbol const *sym) -> clingo_symbol_t const * {
128 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
129 return reinterpret_cast<clingo_symbol_t const *>(sym);
130 }
131
135 [[nodiscard]] auto number() const -> int { return Detail::call<clingo_symbol_number>(rep_); }
136
140 [[nodiscard]] auto name() const -> std::string_view {
141 auto [data, size] = Detail::call<clingo_symbol_name>(rep_);
142 return {data, size};
143 }
144
148 [[nodiscard]] auto string() const -> std::string_view {
149 auto [data, size] = Detail::call<clingo_symbol_string>(rep_);
150 return {data, size};
151 }
152
156 [[nodiscard]] auto is_positive() const -> bool { return Detail::call<clingo_symbol_is_positive>(rep_); }
157
161 [[nodiscard]] auto is_negative() const -> bool { return !is_positive(); }
162
167 clingo_symbol_t const *res = nullptr;
168 size_t n = 0;
169 Detail::handle_error(clingo_symbol_arguments(rep_, &res, &n));
170 return {cpp_cast(res), n};
171 }
172
176 [[nodiscard]] auto type() const -> SymbolType { return static_cast<SymbolType>(clingo_symbol_type(rep_)); }
177
184 [[nodiscard]] auto to_string() const -> std::string {
185 auto bld = StringBuilder{};
187 return std::string{bld.str()};
188 }
189
195 [[nodiscard]] auto signature() const -> std::optional<std::tuple<std::string_view, size_t, bool>> {
196 if (type() == SymbolType::function) {
197 return std::make_tuple(name(), arguments().size(), is_positive());
198 }
199 return std::nullopt;
200 }
201
208 [[nodiscard]] auto match(std::string_view name, size_t arity, bool positive = true) const -> bool {
209 return type() == SymbolType::function && this->name() == name && arguments().size() == arity &&
210 positive == is_positive();
211 }
212
217 [[nodiscard]] auto match(size_t arity) const -> bool {
218 return type() == SymbolType::tuple && arguments().size() == arity;
219 }
220
226 [[nodiscard]] auto hash() const noexcept -> size_t { return clingo_symbol_hash(rep_); }
227
233 friend auto operator==(Symbol const &a, Symbol const &b) noexcept -> bool {
234 return clingo_symbol_equal(a.rep_, b.rep_);
235 }
236
242 friend auto operator<=>(Symbol const &a, Symbol const &b) noexcept -> std::strong_ordering {
243 return clingo_symbol_compare(a.rep_, b.rep_) <=> 0;
244 }
245
251 friend auto operator<<(std::ostream &out, Symbol const &sym) -> std::ostream & {
252 out << sym.to_string();
253 return out;
254 }
255
256 private:
257 clingo_symbol_t rep_ = 0;
258};
259
264inline auto Number(int num) -> Symbol {
265 return Symbol{clingo_symbol_create_number(num), false};
266}
267
273inline auto Number(Library const &lib, std::string_view str) -> Symbol {
274 return Symbol{Detail::call<clingo_symbol_create_number_str>(c_cast(lib), str.data(), str.size()), false};
275}
276
282inline auto Supremum() -> Symbol {
283 return Symbol{clingo_symbol_create_supremum(), false};
284}
285
291inline auto Infimum() -> Symbol {
292 return Symbol{clingo_symbol_create_infimum(), false};
293}
294
300inline auto String(Library const &lib, std::string_view str) -> Symbol {
301 return Symbol{Detail::call<clingo_symbol_create_string>(c_cast(lib), str.data(), str.size()), false};
302}
303
313inline auto Function(Library const &lib, std::string_view str, SymbolSpan arguments = {}, bool is_positive = true)
314 -> Symbol {
315 return Symbol{Detail::call<clingo_symbol_create_function>(c_cast(lib), str.data(), str.size(),
316 c_cast(arguments.data()), arguments.size(), is_positive),
317 false};
318}
319
329inline auto Function(Library const &lib, std::string_view str, SymbolList arguments, bool is_positive = true)
330 -> Symbol {
331 return Function(lib, str, std::span{arguments.begin(), arguments.end()}, is_positive);
332}
333
339inline auto Tuple(Library const &lib, SymbolSpan arguments = {}) -> Symbol {
340 return Symbol{Detail::call<clingo_symbol_create_tuple>(c_cast(lib), c_cast(arguments.data()), arguments.size()),
341 false};
342}
343
349inline auto Tuple(Library const &lib, SymbolList arguments) -> Symbol {
350 return Tuple(lib, std::span{arguments.begin(), arguments.end()});
351}
352
357inline auto parse_term(Library const &lib, std::string_view str) -> Symbol {
358 return Symbol{Detail::call<clingo_parse_term>(c_cast(lib), str.data(), str.size()), false};
359}
360
362
363} // namespace Clingo
364
365namespace std {
366
367template <> struct hash<Clingo::Symbol> {
368 auto operator()(Clingo::Symbol const &sym) const noexcept -> size_t { return sym.hash(); }
369};
370
371} // namespace std
The main library class for managing global information and logging.
Definition core.hh:471
A string builder for constructing strings.
Definition core.hh:524
Class modeling a symbol in Clingo.
Definition symbol.hh:68
auto is_negative() const -> bool
Check whether a function or number symbol is negative.
Definition symbol.hh:161
~Symbol()
The destructor releases the symbol.
Definition symbol.hh:73
auto hash() const noexcept -> size_t
Compute a hash value for the symbol.
Definition symbol.hh:226
auto type() const -> SymbolType
Get the type of the symbol.
Definition symbol.hh:176
friend auto operator<<(std::ostream &out, Symbol const &sym) -> std::ostream &
Output the symbol to a stream.
Definition symbol.hh:251
auto to_string() const -> std::string
Get a string representation of the symbol.
Definition symbol.hh:184
Symbol()=default
Default constructor, creates a symbol holding number zero.
auto match(std::string_view name, size_t arity, bool positive=true) const -> bool
Match the symbol against a function signature.
Definition symbol.hh:208
auto operator=(Symbol const &other) noexcept -> Symbol &
The copy assignment acquires the symbol and releases the old one.
Definition symbol.hh:83
auto name() const -> std::string_view
Get the name of the symbol if it is a function.
Definition symbol.hh:140
friend auto operator<=>(Symbol const &a, Symbol const &b) noexcept -> std::strong_ordering
Compare two symbols.
Definition symbol.hh:242
friend auto c_cast(Symbol const &sym) -> clingo_symbol_t const &
Cast the symbol to its C API representation.
Definition symbol.hh:122
Symbol(clingo_symbol_t rep, bool acquire)
Construct a symbol from its C API representation.
Definition symbol.hh:112
Symbol(Symbol &&other) noexcept
The move constructor transfers ownership of the symbol.
Definition symbol.hh:93
auto string() const -> std::string_view
Get the string value of the symbol if it is a string.
Definition symbol.hh:148
auto arguments() const -> SymbolSpan
Get the arguments of a function or tuple symbol.
Definition symbol.hh:166
auto signature() const -> std::optional< std::tuple< std::string_view, size_t, bool > >
Get the signature of the symbol if it is a function.
Definition symbol.hh:195
auto operator=(Symbol &&other) noexcept -> Symbol &
The move assignment transfers ownership and releases the old one.
Definition symbol.hh:98
friend auto operator==(Symbol const &a, Symbol const &b) noexcept -> bool
Compare two symbols for equality.
Definition symbol.hh:233
auto is_positive() const -> bool
Check whether a function or number symbol is positive.
Definition symbol.hh:156
Symbol(Symbol const &other) noexcept
The copy constructor acquires the symbol.
Definition symbol.hh:78
auto match(size_t arity) const -> bool
Match the symbol against a tuple signature.
Definition symbol.hh:217
friend auto c_cast(Symbol const *sym) -> clingo_symbol_t const *
Cast the symbol to its C API representation.
Definition symbol.hh:127
auto number() const -> int
Get the numeric value of the symbol if it is a number.
Definition symbol.hh:135
CLINGO_VISIBILITY_DEFAULT int clingo_symbol_compare(clingo_symbol_t a, clingo_symbol_t b)
Check if a symbol is less than another symbol.
CLINGO_VISIBILITY_DEFAULT clingo_symbol_t clingo_symbol_create_supremum(void)
Construct a symbol representing #sup.
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_to_string(clingo_symbol_t symbol, clingo_string_builder_t *builder)
Get the string representation of a symbol.
CLINGO_VISIBILITY_DEFAULT size_t clingo_symbol_hash(clingo_symbol_t symbol)
Calculate a hash code of a symbol.
uint64_t clingo_symbol_t
Type to represent a symbol.
Definition symbol.h:51
CLINGO_VISIBILITY_DEFAULT void clingo_symbol_acquire(clingo_symbol_t symbol)
Acquire ownership of the given symbol.
CLINGO_VISIBILITY_DEFAULT clingo_symbol_type_t clingo_symbol_type(clingo_symbol_t symbol)
Get the type of a symbol.
int clingo_symbol_type_t
Corresponding type to clingo_symbol_type.
Definition symbol.h:45
CLINGO_VISIBILITY_DEFAULT void clingo_symbol_release(clingo_symbol_t symbol)
Release ownership of the given symbol.
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_arguments(clingo_symbol_t symbol, clingo_symbol_t const **arguments, size_t *arguments_size)
Get the arguments of a symbol.
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_equal(clingo_symbol_t a, clingo_symbol_t b)
Check if two symbols are equal.
CLINGO_VISIBILITY_DEFAULT clingo_symbol_t clingo_symbol_create_number(int32_t number)
Construct a symbol representing a number.
CLINGO_VISIBILITY_DEFAULT clingo_symbol_t clingo_symbol_create_infimum(void)
Construct a symbol representing #inf.
@ clingo_symbol_type_number
a numeric symbol, e.g., 1
Definition symbol.h:37
@ clingo_symbol_type_string
a string symbol, e.g., "a"
Definition symbol.h:40
@ clingo_symbol_type_function
a function symbol, e.g., c, -c, or f(1,"a")
Definition symbol.h:42
@ clingo_symbol_type_supremum
the #sup symbol
Definition symbol.h:38
@ clingo_symbol_type_tuple
a tuple symbol, e.g., (1, "a")`
Definition symbol.h:41
@ clingo_symbol_type_infimum
the #inf symbol
Definition symbol.h:39
@ tuple
Theory tuples "(t1,...,tn)".
@ number
a number term, e.g., 42
@ function
a function term, e.g., f(1,2,3)
auto String(Library const &lib, std::string_view str) -> Symbol
Construct a string symbol from a string.
Definition symbol.hh:300
std::vector< Symbol > SymbolVector
A vector of symbols.
Definition symbol.hh:42
auto parse_term(Library const &lib, std::string_view str) -> Symbol
Parse the given string and evaluate it to a symbol.
Definition symbol.hh:357
auto Number(int num) -> Symbol
Construct a numeric symbol from an integer.
Definition symbol.hh:264
std::span< Symbol const > SymbolSpan
A span of symbols, which is a view on a contiguous sequence of symbols.
Definition symbol.hh:38
auto Infimum() -> Symbol
Construct an infimum symbol.
Definition symbol.hh:291
auto Function(Library const &lib, std::string_view str, SymbolSpan arguments={}, bool is_positive=true) -> Symbol
Construct a function symbol from a name and arguments.
Definition symbol.hh:313
auto cpp_cast(clingo_symbol_t const *sym) -> Symbol const *
Cast a C symbol to its C++ representation.
Definition symbol.hh:52
std::initializer_list< Symbol const > SymbolList
A list of symbols.
Definition symbol.hh:40
auto Tuple(Library const &lib, SymbolSpan arguments={}) -> Symbol
Construct a tuple symbol from a list of arguments.
Definition symbol.hh:339
SymbolType
Enumeration of symbol types.
Definition symbol.hh:21
auto Supremum() -> Symbol
Construct a supremum symbol.
Definition symbol.hh:282
@ tuple
The symbol is a tuple.
@ supremum
The symbol is a sentinel for the maximum value.
@ function
The symbol is a function.
@ infimum
The symbol is a sentinel for the minimum value.