Clingo
Loading...
Searching...
No Matches
theory.hh
1#pragma once
2
3#include <clingo/app.hh>
4#include <clingo/ast.hh>
5
6#include <clingo/theory.h>
7
8#include <iterator>
9#include <variant>
10
11namespace Clingo {
12
26
29 public:
31 struct sentinel {};
33 class iterator {
34 public:
36 using iterator_category = std::input_iterator_tag;
38 using value_type = std::pair<Symbol, std::variant<int, double, Symbol>>;
40 using difference_type = std::ptrdiff_t;
45
49 iterator(TheoryAssignment const &assignment) : assignment_{&assignment} { advance(); }
50
54 iterator() = default;
55
59 auto operator*() const -> reference { return current_; }
60
64 auto operator++() -> iterator & {
65 advance();
66 return *this;
67 }
68
70 auto operator++(int) -> iterator { return std::exchange(*this, ++iterator(*this)); }
71
77 friend auto operator==(iterator const &a, [[maybe_unused]] sentinel const &b) -> bool { return !a.has_value_; }
78
79 private:
80 void advance() {
81 Detail::handle_error(assignment_->theory_->assignment_next(
82 assignment_->theory_->self, assignment_->thread_id_, &init_, &index_, &has_value_));
83 if (has_value_) {
84 current_ = assignment_->at(index_);
85 }
86 }
87
88 mutable value_type current_;
89 TheoryAssignment const *assignment_ = nullptr;
90 size_t index_ = 0;
91 bool init_ = true;
92 bool has_value_ = true;
93 };
94 static_assert(std::input_iterator<iterator>);
95 static_assert(std::sentinel_for<sentinel, iterator>);
96
102 : theory_{theory}, thread_id_{thread_id} {}
103
108 [[nodiscard]] auto lookup(Symbol const &sym) const -> std::optional<size_t> {
109 bool found = false;
110 size_t index = 0;
111 theory_->lookup_symbol(theory_->self, c_cast(sym), &index, &found);
112 return found ? std::optional{index} : std::nullopt;
113 }
114
119 [[nodiscard]] auto at(size_t index) const -> std::pair<Symbol, std::variant<int, double, Symbol>> {
122 bool found = false;
123 Detail::handle_error(theory_->assignment_get_value(theory_->self, thread_id_, index, &symbol, &value, &found));
124 auto sym = Clingo::Symbol{symbol, false};
125 if (!found) {
126 throw std::out_of_range{"invalid index"};
127 }
128 switch (value.type) {
130 return {std::move(sym), value.int_number}; // NOLINT
131 break;
132 }
134 return {std::move(sym), value.double_number}; // NOLINT
135 break;
136 }
138 return {std::move(sym), Symbol{value.symbol, false}}; // NOLINT
139 break;
140 }
141 default: {
142 throw std::logic_error{"invalid symbol type"};
143 }
144 }
145 }
146
150 [[nodiscard]] auto begin() const -> iterator { return {*this}; }
154 [[nodiscard]] static auto end() -> sentinel { return {}; }
155
156 private:
157 clingo_theory_t const *theory_;
158 uint32_t thread_id_;
159};
160
162class Theory {
163 public:
169 create(c_cast(lib), &theory_);
170 }
171
175 friend auto c_cast(Theory const &x) -> clingo_theory_t const * { return &x.theory_; }
176
178 Theory(Theory &&other) = delete;
179
182 if (theory_.destroy != nullptr) {
183 theory_.destroy(theory_.self);
184 }
185 }
186
192 void prepare(Control const &ctl) const { Detail::handle_error(theory_.prepare(theory_.self, c_cast(ctl))); }
193
200 template <class F> void rewrite(AST::Node const &stm, F fun) const {
201 constexpr auto add = [](clingo_ast_t *stm, void *data) -> bool {
202 CLINGO_TRY {
203 auto *fun = static_cast<F *>(data);
204 std::invoke<F &>(*fun, AST::Node{stm, true});
205 }
206 CLINGO_CATCH;
207 };
208 Detail::handle_error(theory_.rewrite_ast(theory_.self, c_cast(stm), +add, static_cast<void *>(&fun)));
209 }
210
216 void rewrite(Library const &lib, Control const &ctl, std::string_view str) const {
217 auto prg = AST::Program{lib};
219 lib, str, [&](AST::Node const &stm) { rewrite(stm, [&](AST::Node const &stm) { prg.add(stm); }); }, &ctl);
220 ctl.join(prg);
221 }
222
228 void rewrite(Library const &lib, Control const &ctl, StringSpan files) const {
229 auto prg = AST::Program{lib};
231 lib, files, [&](AST::Node const &stm) { rewrite(stm, [&](AST::Node const &stm) { prg.add(stm); }); }, &ctl);
232 ctl.join(prg);
233 }
234
239 [[nodiscard]] auto assignment(uint32_t thread_id) const -> TheoryAssignment {
240 return TheoryAssignment{&theory_, thread_id};
241 }
242
248 Detail::handle_error(theory_.on_stats(theory_.self, c_cast(step)));
249 }
250
253 void model(Model model) const { Detail::handle_error(theory_.on_model(theory_.self, c_cast(model))); }
254
261 void register_theory(Control const &ctl) const {
262 Detail::handle_error(theory_.register_theory(theory_.self, c_cast(ctl)));
263 }
264
270 void register_options(Options const &opts) const {
271 Detail::handle_error(theory_.register_options(theory_.self, c_cast(opts)));
272 }
273
277 void validate_options() const { Detail::handle_error(theory_.validate_options(theory_.self)); }
278
287 void configure(std::string_view key, std::string_view value) const {
288 Detail::handle_error(theory_.configure(theory_.self, key.data(), key.size(), value.data(), value.size()));
289 }
290
291 private:
292 clingo_theory_t theory_{};
293};
294
296
297} // namespace Clingo
Node capturing expressions in logic programs.
Definition ast.hh:273
A program to add statements to.
Definition ast.hh:809
The main control class for grounding and solving logic programs.
Definition control.hh:179
The main library class for managing global information and logging.
Definition core.hh:471
Class to provide an mutable view of a model.
Definition solve.hh:256
Object to add command-line options.
Definition app.hh:17
Class modeling a mutable view on a statistics entry.
Definition stats.hh:106
Class modeling a symbol in Clingo.
Definition symbol.hh:68
Iterator type for the theory assignment.
Definition theory.hh:33
std::pair< Symbol, std::variant< int, double, Symbol > > value_type
The value type of the iterator, which is a pair of a symbol and its value.
Definition theory.hh:38
iterator()=default
The default constructor for the iterator.
auto operator++() -> iterator &
Increment the iterator to the next value.
Definition theory.hh:64
auto operator*() const -> reference
Get the current value of the iterator.
Definition theory.hh:59
friend auto operator==(iterator const &a, sentinel const &b) -> bool
Check whether their are still values in the assignment.
Definition theory.hh:77
std::input_iterator_tag iterator_category
The iterator category.
Definition theory.hh:36
value_type & reference
The reference type of the iterator.
Definition theory.hh:44
iterator(TheoryAssignment const &assignment)
Constructor for the iterator.
Definition theory.hh:49
value_type * pointer
The pointer type of the iterator.
Definition theory.hh:42
std::ptrdiff_t difference_type
The difference type of the iterator.
Definition theory.hh:40
auto operator++(int) -> iterator
Increment the iterator to the next value (postfix).
Definition theory.hh:70
Class to represent a theory assignment.
Definition theory.hh:28
auto begin() const -> iterator
Get an iterator pointing to the first element of the theory assignment.
Definition theory.hh:150
auto at(size_t index) const -> std::pair< Symbol, std::variant< int, double, Symbol > >
Get the symbol value pair at the given index in the theory assignment.
Definition theory.hh:119
TheoryAssignment(clingo_theory_t const *theory, uint32_t thread_id)
Construct a theory assignment from its C representation and a thread id.
Definition theory.hh:101
static auto end() -> sentinel
Get sentinel marking the end of the theory assignment.
Definition theory.hh:154
auto lookup(Symbol const &sym) const -> std::optional< size_t >
Lookup a symbol in the theory assignment.
Definition theory.hh:108
Theory class to represent a theory in Clingo.
Definition theory.hh:162
friend auto c_cast(Theory const &x) -> clingo_theory_t const *
Get the underlying C representation of the theory.
Definition theory.hh:175
void model(Model model) const
Incorporate theory symbols into the model.
Definition theory.hh:253
void validate_options() const
Validate previously parsed the theory options.
Definition theory.hh:277
void stats(Stats step, Stats accu) const
Incorporate statistics from the theory into the solver's statistics.
Definition theory.hh:247
void prepare(Control const &ctl) const
Prepare the theory for solving.
Definition theory.hh:192
void configure(std::string_view key, std::string_view value) const
Configure the theory with the given key and value.
Definition theory.hh:287
void register_theory(Control const &ctl) const
Register the theory with the control object.
Definition theory.hh:261
void register_options(Options const &opts) const
Add theory related options to the given options object.
Definition theory.hh:270
void rewrite(Library const &lib, Control const &ctl, std::string_view str) const
Rewrite the given program using the theory's rewrite function.
Definition theory.hh:216
void rewrite(Library const &lib, Control const &ctl, StringSpan files) const
Rewrite the programs in the files using the theory's rewrite function.
Definition theory.hh:228
Theory(Theory &&other)=delete
Disable copy and move operations for the Theory class.
~Theory()
Destructor for the Theory class.
Definition theory.hh:181
void rewrite(AST::Node const &stm, F fun) const
Rewrite the given AST node using the theory's rewrite function.
Definition theory.hh:200
Theory(Library const &lib, bool(*create)(clingo_lib_t *lib, clingo_theory_t *theory))
Constructor for the Theory class.
Definition theory.hh:168
auto assignment(uint32_t thread_id) const -> TheoryAssignment
Get the theory assignment for the given thread id.
Definition theory.hh:239
struct clingo_ast clingo_ast_t
This struct provides a view to nodes in the AST.
Definition ast.h:163
struct clingo_lib clingo_lib_t
A library object storing global information.
Definition core.h:176
uint64_t clingo_symbol_t
Type to represent a symbol.
Definition symbol.h:51
@ clingo_theory_value_type_double
A double value.
Definition theory.h:40
@ clingo_theory_value_type_symbol
A symbol value.
Definition theory.h:42
@ clingo_theory_value_type_int
An integer value.
Definition theory.h:38
auto parse(Library const &lib, std::string_view string, ParseType type=ParseType::statement) -> Node
Parse an expression of the given type.
Definition ast.hh:837
@ tuple
Theory tuples "(t1,...,tn)".
@ symbol
a symbol term, e.g., c
@ value
The configuration entry is a double value.
@ rewrite
Parse and rewrite.
std::span< std::string_view const > StringSpan
A span of string views.
Definition core.hh:423
@ theory
Select symbols added by theory.
Sentinel type for the end of the theory assignment.
Definition theory.hh:31
A struct to hold values assigned by a theory.
Definition theory.h:47
A theory object to extend solving.
Definition theory.h:64
bool(* assignment_get_value)(void *self, uint32_t thread_id, size_t index, clingo_symbol_t *symbol, clingo_theory_value_t *value, bool *has_value)
Get the value assigned to the given index.
Definition theory.h:172
bool(* assignment_next)(void *self, uint32_t thread_id, bool *init, size_t *index, bool *has_value)
Get the next index that has a value.
Definition theory.h:160
bool(* validate_options)(void *self)
Validate the options of the theory.
Definition theory.h:114
bool(* prepare)(void *self, clingo_control_t *control)
Prepare the theory.
Definition theory.h:103
bool(* register_options)(void *self, clingo_options_t *options)
Register the theory's options with the given application options object.
Definition theory.h:109
bool(* register_theory)(void *self, clingo_control_t *control)
Register the theory with the given control object.
Definition theory.h:85
void(* destroy)(void *self)
Destroy the theory.
Definition theory.h:77
bool(* rewrite_ast)(void *self, clingo_ast_t *statement, clingo_theory_ast_callback_t callback, void *data)
Rewrite the given ast for the theory.
Definition theory.h:95
bool(* on_model)(void *self, clingo_model_t *model)
Inform the theory that a model has been found.
Definition theory.h:129
bool(* configure)(void *self, char const *key, size_t key_size, char const *value, size_t value_size)
Configure the theory passing key value pairs.
Definition theory.h:123
bool(* on_stats)(void *self, clingo_stats_t *stats)
Add the theory's statistics to the given maps.
Definition theory.h:135
bool(* lookup_symbol)(void *self, clingo_symbol_t symbol, size_t *index, bool *found)
Get the integer index of a symbol assigned by the theory when a model is found.
Definition theory.h:145
void * self
The userdata for the first value to the callabcks in this struct.
Definition theory.h:176