Clingo
Loading...
Searching...
No Matches
backend.hh
1#pragma once
2
3#include <clingo/core/core.hh>
4#include <clingo/core/symbol.hh>
5
6#include <clingo/util/small_vector.hh>
7
8namespace CppClingo {
9
12
16using prg_id_t = uint32_t;
18using PrgIdSpan = std::span<prg_id_t const>;
20using PrgIdVec = std::vector<prg_id_t>;
22using prg_sid_t = std::make_signed_t<prg_id_t>;
27using prg_lit_t = int32_t;
31using prg_atom_t = uint32_t;
33using prg_weight_t = int32_t;
35using prg_sum_t = int64_t;
37using PrgLitSpan = std::span<prg_lit_t const>;
39using PrgLitVec = std::vector<prg_lit_t>;
41using WeightedPrgLitSpan = std::span<std::pair<prg_lit_t, prg_weight_t> const>;
43using WeightedPrgLitVec = std::vector<std::pair<prg_lit_t, prg_weight_t>>;
44
46static constexpr auto prg_lit_max = std::numeric_limits<prg_lit_t>::max();
48static constexpr auto prg_lit_min = -prg_lit_max;
49
55 public:
57 virtual ~ProgramBackend() = default;
58
65 void preamble(unsigned major, unsigned minor, unsigned revision, bool incremental) {
66 do_preamble(major, minor, revision, incremental);
67 }
68
74 void begin_step() { do_begin_step(); }
75
79 void end_ground() { do_end_ground(); }
80
87 void end_step() { do_end_step(); }
88
94 auto next_lit() -> prg_lit_t { return do_next_lit(); }
95
99 auto fact_lit() -> std::optional<prg_lit_t> { return do_fact_lit(); }
100
109 void bd_aggr(PrgLitSpan head, WeightedPrgLitSpan body, int32_t bound, bool choice) {
110 assert(std::ranges::all_of(head, [](auto const &x) { return x > 0; }));
111 do_bd_aggr(head, body, bound, choice);
112 }
113
121 void rule(PrgLitSpan head, PrgLitSpan body, bool choice) {
122 assert(std::ranges::all_of(head, [](auto const &x) { return x > 0; }));
123 do_rule(head, body, choice);
124 }
125
130 void show_term(Symbol sym, PrgLitSpan body) { do_show_term(sym, body); }
131
138 void show_term(Symbol sym, prg_id_t id) { do_show_term(sym, id); }
139
144 void show_term(prg_id_t id, PrgLitSpan body) { do_show_term(id, body); }
145
152 void show_atom(Symbol sym, prg_lit_t lit) { do_show_atom(sym, lit); }
153
159 void edge(prg_id_t u, prg_id_t v, PrgLitSpan body) { do_edge(u, v, body); }
160
170 void heuristic(prg_lit_t atom, int32_t weight, int32_t prio, HeuristicType type, PrgLitSpan body) {
171 assert(atom > 0);
172 do_heuristic(atom, weight, prio, type, body);
173 }
180 void external(prg_lit_t atom, ExternalType type) {
181 assert(atom > 0);
182 do_external(atom, type);
183 }
184
190 void project(PrgLitSpan atoms) {
191 assert(std::ranges::all_of(atoms, [](auto const &x) { return x > 0; }));
192 do_project(atoms);
193 }
194
198 void assume(PrgLitSpan literals) { do_assume(literals); }
199
204 void minimize(prg_weight_t priority, WeightedPrgLitSpan body) { do_minimize(priority, body); }
205
206 private:
207 virtual void do_preamble(unsigned major, unsigned minor, unsigned revision, bool incremental) = 0;
208 virtual void do_begin_step() = 0;
209 virtual void do_end_ground() = 0;
210 virtual void do_end_step() = 0;
211 virtual auto do_next_lit() -> prg_lit_t = 0;
212 virtual auto do_fact_lit() -> std::optional<prg_lit_t> = 0;
213
214 virtual void do_rule(PrgLitSpan head, PrgLitSpan body, bool choice) = 0;
215 virtual void do_bd_aggr(PrgLitSpan head, WeightedPrgLitSpan body, int32_t bound, bool choice) = 0;
216 virtual void do_show_term(Symbol sym, PrgLitSpan body) = 0;
217 virtual void do_show_term(Symbol sym, prg_id_t id) = 0;
218 virtual void do_show_term(prg_id_t id, PrgLitSpan body) = 0;
219 virtual void do_show_atom(Symbol sym, prg_lit_t lit) = 0;
220 virtual void do_edge(prg_id_t u, prg_id_t v, PrgLitSpan body) = 0;
221 virtual void do_heuristic(prg_lit_t atom, prg_weight_t weight, prg_weight_t prio, HeuristicType type,
222 PrgLitSpan body) = 0;
223 virtual void do_external(prg_lit_t atom, ExternalType type) = 0;
224 virtual void do_project(PrgLitSpan atoms) = 0;
225 virtual void do_assume(PrgLitSpan literals) = 0;
226 virtual void do_minimize(prg_weight_t priority, WeightedPrgLitSpan body) = 0;
227};
229using UProgramBackend = std::unique_ptr<ProgramBackend>;
230
233 public:
235 virtual ~TheoryBackend() = default;
236
243 void num(prg_id_t id, prg_weight_t num) { do_num(id, num); }
250 void str(prg_id_t id, std::string_view str) { do_str(id, str); }
259 void fun(prg_id_t id, prg_id_t name, PrgIdSpan args) { do_fun(id, name, args); }
260
268 void tup(prg_id_t id, TheoryTermTupleType type, PrgIdSpan args) { do_tup(id, type, args); }
269
277 void elem(prg_id_t id, PrgIdSpan terms, PrgLitSpan cond) { do_elem(id, terms, cond); }
278
285 void atom(prg_lit_t atom_or_zero, prg_id_t name, PrgIdSpan elems,
286 std::optional<std::pair<prg_id_t, prg_id_t>> guard) {
287 assert(atom_or_zero >= 0);
288 do_atom(atom_or_zero, name, elems, guard);
289 }
290
291 private:
292 virtual void do_num(prg_id_t id, prg_weight_t num) = 0;
293 virtual void do_str(prg_id_t id, std::string_view str) = 0;
294 virtual void do_fun(prg_id_t id, prg_id_t name, PrgIdSpan args) = 0;
295 virtual void do_tup(prg_id_t id, TheoryTermTupleType type, PrgIdSpan args) = 0;
296 virtual void do_elem(prg_id_t id, PrgIdSpan terms, PrgLitSpan cond) = 0;
297 virtual void do_atom(prg_lit_t atom_or_zero, prg_id_t name, PrgIdSpan elems,
298 std::optional<std::pair<prg_id_t, prg_id_t>> guard) = 0;
299};
301using UTheoryBackend = std::unique_ptr<TheoryBackend>;
302
304
305} // namespace CppClingo
Abstract class connecting grounder and solver.
Definition backend.hh:54
auto fact_lit() -> std::optional< prg_lit_t >
Get a factual literal if one existis.
Definition backend.hh:99
void assume(PrgLitSpan literals)
Assume the given literals.
Definition backend.hh:198
void minimize(prg_weight_t priority, WeightedPrgLitSpan body)
Minimize the given weighted literals.
Definition backend.hh:204
void show_term(prg_id_t id, PrgLitSpan body)
Show the symbol with the given id if the given condition is true.
Definition backend.hh:144
void show_atom(Symbol sym, prg_lit_t lit)
Show the atom with the given symbol and program literal.
Definition backend.hh:152
void rule(PrgLitSpan head, PrgLitSpan body, bool choice)
Add a disjunctive or choice rule.
Definition backend.hh:121
void bd_aggr(PrgLitSpan head, WeightedPrgLitSpan body, int32_t bound, bool choice)
Define a weight constraint.
Definition backend.hh:109
void show_term(Symbol sym, prg_id_t id)
Associate the given symbol with an id.
Definition backend.hh:138
void begin_step()
Start a new step.
Definition backend.hh:74
void preamble(unsigned major, unsigned minor, unsigned revision, bool incremental)
Initialize the backend.
Definition backend.hh:65
auto next_lit() -> prg_lit_t
Return a fresh (positive) literal.
Definition backend.hh:94
void edge(prg_id_t u, prg_id_t v, PrgLitSpan body)
Add an edge for acyclicity checking.
Definition backend.hh:159
void show_term(Symbol sym, PrgLitSpan body)
Show the given symbol if the given condition is true.
Definition backend.hh:130
void external(prg_lit_t atom, ExternalType type)
Declare the given atom as external.
Definition backend.hh:180
virtual ~ProgramBackend()=default
Destroy the backend.
void heuristic(prg_lit_t atom, int32_t weight, int32_t prio, HeuristicType type, PrgLitSpan body)
Add a heuristic directive.
Definition backend.hh:170
void end_ground()
Finalize the current grounding step.
Definition backend.hh:79
void project(PrgLitSpan atoms)
Project the given atoms.
Definition backend.hh:190
void end_step()
Finalize the current step.
Definition backend.hh:87
Variant-like class to store symbols stored in a symbol store.
Definition symbol.hh:225
Abstract class connecting grounder and theory data.
Definition backend.hh:232
void str(prg_id_t id, std::string_view str)
Add a theory string.
Definition backend.hh:250
void fun(prg_id_t id, prg_id_t name, PrgIdSpan args)
Add a theory function.
Definition backend.hh:259
void tup(prg_id_t id, TheoryTermTupleType type, PrgIdSpan args)
Add a theory tuple.
Definition backend.hh:268
void elem(prg_id_t id, PrgIdSpan terms, PrgLitSpan cond)
Add a theory element.
Definition backend.hh:277
virtual ~TheoryBackend()=default
Destroy the backend.
void atom(prg_lit_t atom_or_zero, prg_id_t name, PrgIdSpan elems, std::optional< std::pair< prg_id_t, prg_id_t > > guard)
Add a theory atom.
Definition backend.hh:285
void num(prg_id_t id, prg_weight_t num)
Add a theory number.
Definition backend.hh:243
int32_t prg_lit_t
A program literal.
Definition backend.hh:27
std::make_signed_t< prg_id_t > prg_sid_t
An signed version of id_t.
Definition backend.hh:22
std::span< prg_lit_t const > PrgLitSpan
A span of program literals.
Definition backend.hh:37
int32_t prg_weight_t
A weight used in weight and minimize constraints.
Definition backend.hh:33
std::vector< std::pair< prg_lit_t, prg_weight_t > > WeightedPrgLitVec
A vector of program literals.
Definition backend.hh:43
int64_t prg_sum_t
Type to represent sums of weights.
Definition backend.hh:35
uint32_t prg_atom_t
A program atom.
Definition backend.hh:31
uint32_t prg_id_t
An id to refer to elements of a logic program.
Definition backend.hh:16
std::span< prg_id_t const > PrgIdSpan
A span of ids.
Definition backend.hh:18
std::unique_ptr< TheoryBackend > UTheoryBackend
A unique pointer for a theory backend.
Definition backend.hh:301
std::vector< prg_id_t > PrgIdVec
A vector of ids.
Definition backend.hh:20
std::vector< prg_lit_t > PrgLitVec
A vector of literals.
Definition backend.hh:39
std::unique_ptr< ProgramBackend > UProgramBackend
A unique pointer for a program backend.
Definition backend.hh:229
std::span< std::pair< prg_lit_t, prg_weight_t > const > WeightedPrgLitSpan
A span of program literals.
Definition backend.hh:41
TheoryTermTupleType
Enumeration of theory term tuple types.
Definition core.hh:165
HeuristicType
Available heuristic types.
Definition core.hh:172
ExternalType
Available external types.
Definition core.hh:177