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
70 void end() { do_end(); }
71
77 auto next_lit() -> prg_lit_t { return do_next_lit(); }
78
82 auto fact_lit() -> std::optional<prg_lit_t> { return do_fact_lit(); }
83
92 void bd_aggr(PrgLitSpan head, WeightedPrgLitSpan body, int32_t bound, bool choice) {
93 assert(std::ranges::all_of(head, [](auto const &x) { return x > 0; }));
94 do_bd_aggr(head, body, bound, choice);
95 }
96
104 void rule(PrgLitSpan head, PrgLitSpan body, bool choice) {
105 assert(std::ranges::all_of(head, [](auto const &x) { return x > 0; }));
106 do_rule(head, body, choice);
107 }
108
113 void show_term(Symbol sym, PrgLitSpan body) { do_show_term(sym, body); }
114
121 void show_term(Symbol sym, prg_id_t id) { do_show_term(sym, id); }
122
127 void show_term(prg_id_t id, PrgLitSpan body) { do_show_term(id, body); }
128
135 void show_atom(Symbol sym, prg_lit_t lit) { do_show_atom(sym, lit); }
136
142 void edge(prg_id_t u, prg_id_t v, PrgLitSpan body) { do_edge(u, v, body); }
143
153 void heuristic(prg_lit_t atom, int32_t weight, int32_t prio, HeuristicType type, PrgLitSpan body) {
154 assert(atom > 0);
155 do_heuristic(atom, weight, prio, type, body);
156 }
163 void external(prg_lit_t atom, ExternalType type) {
164 assert(atom > 0);
165 do_external(atom, type);
166 }
167
173 void project(PrgLitSpan atoms) {
174 assert(std::ranges::all_of(atoms, [](auto const &x) { return x > 0; }));
175 do_project(atoms);
176 }
177
181 void assume(PrgLitSpan literals) { do_assume(literals); }
182
187 void minimize(prg_weight_t priority, WeightedPrgLitSpan body) { do_minimize(priority, body); }
188
189 private:
190 virtual void do_preamble(unsigned major, unsigned minor, unsigned revision, bool incremental) = 0;
191 virtual void do_end() = 0;
192 virtual auto do_next_lit() -> prg_lit_t = 0;
193 virtual auto do_fact_lit() -> std::optional<prg_lit_t> = 0;
194
195 virtual void do_rule(PrgLitSpan head, PrgLitSpan body, bool choice) = 0;
196 virtual void do_bd_aggr(PrgLitSpan head, WeightedPrgLitSpan body, int32_t bound, bool choice) = 0;
197 virtual void do_show_term(Symbol sym, PrgLitSpan body) = 0;
198 virtual void do_show_term(Symbol sym, prg_id_t id) = 0;
199 virtual void do_show_term(prg_id_t id, PrgLitSpan body) = 0;
200 virtual void do_show_atom(Symbol sym, prg_lit_t lit) = 0;
201 virtual void do_edge(prg_id_t u, prg_id_t v, PrgLitSpan body) = 0;
202 virtual void do_heuristic(prg_lit_t atom, prg_weight_t weight, prg_weight_t prio, HeuristicType type,
203 PrgLitSpan body) = 0;
204 virtual void do_external(prg_lit_t atom, ExternalType type) = 0;
205 virtual void do_project(PrgLitSpan atoms) = 0;
206 virtual void do_assume(PrgLitSpan literals) = 0;
207 virtual void do_minimize(prg_weight_t priority, WeightedPrgLitSpan body) = 0;
208};
210using UProgramBackend = std::unique_ptr<ProgramBackend>;
211
214 public:
216 virtual ~TheoryBackend() = default;
217
224 void num(prg_id_t id, prg_weight_t num) { do_num(id, num); }
231 void str(prg_id_t id, std::string_view str) { do_str(id, str); }
240 void fun(prg_id_t id, prg_id_t name, PrgIdSpan args) { do_fun(id, name, args); }
241
249 void tup(prg_id_t id, TheoryTermTupleType type, PrgIdSpan args) { do_tup(id, type, args); }
250
258 void elem(prg_id_t id, PrgIdSpan terms, PrgLitSpan cond) { do_elem(id, terms, cond); }
259
266 void atom(prg_lit_t atom_or_zero, prg_id_t name, PrgIdSpan elems,
267 std::optional<std::pair<prg_id_t, prg_id_t>> guard) {
268 assert(atom_or_zero >= 0);
269 do_atom(atom_or_zero, name, elems, guard);
270 }
271
273 void end() { do_end(); }
274
275 private:
276 virtual void do_num(prg_id_t id, prg_weight_t num) = 0;
277 virtual void do_str(prg_id_t id, std::string_view str) = 0;
278 virtual void do_fun(prg_id_t id, prg_id_t name, PrgIdSpan args) = 0;
279 virtual void do_tup(prg_id_t id, TheoryTermTupleType type, PrgIdSpan args) = 0;
280 virtual void do_elem(prg_id_t id, PrgIdSpan terms, PrgLitSpan cond) = 0;
281 virtual void do_atom(prg_lit_t atom_or_zero, prg_id_t name, PrgIdSpan elems,
282 std::optional<std::pair<prg_id_t, prg_id_t>> guard) = 0;
283 virtual void do_end() = 0;
284};
286using UTheoryBackend = std::unique_ptr<TheoryBackend>;
287
289
290} // 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:82
void assume(PrgLitSpan literals)
Assume the given literals.
Definition backend.hh:181
void minimize(prg_weight_t priority, WeightedPrgLitSpan body)
Minimize the given weighted literals.
Definition backend.hh:187
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:127
void show_atom(Symbol sym, prg_lit_t lit)
Show the atom with the given symbol and program literal.
Definition backend.hh:135
void rule(PrgLitSpan head, PrgLitSpan body, bool choice)
Add a disjunctive or choice rule.
Definition backend.hh:104
void bd_aggr(PrgLitSpan head, WeightedPrgLitSpan body, int32_t bound, bool choice)
Define a weight constraint.
Definition backend.hh:92
void show_term(Symbol sym, prg_id_t id)
Associate the given symbol with an id.
Definition backend.hh:121
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:77
void edge(prg_id_t u, prg_id_t v, PrgLitSpan body)
Add an edge for acyclicity checking.
Definition backend.hh:142
void show_term(Symbol sym, PrgLitSpan body)
Show the given symbol if the given condition is true.
Definition backend.hh:113
void external(prg_lit_t atom, ExternalType type)
Declare the given atom as external.
Definition backend.hh:163
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:153
void project(PrgLitSpan atoms)
Project the given atoms.
Definition backend.hh:173
void end()
Finalize the current grounding step.
Definition backend.hh:70
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:213
void str(prg_id_t id, std::string_view str)
Add a theory string.
Definition backend.hh:231
void end()
Finalize the theory.
Definition backend.hh:273
void fun(prg_id_t id, prg_id_t name, PrgIdSpan args)
Add a theory function.
Definition backend.hh:240
void tup(prg_id_t id, TheoryTermTupleType type, PrgIdSpan args)
Add a theory tuple.
Definition backend.hh:249
void elem(prg_id_t id, PrgIdSpan terms, PrgLitSpan cond)
Add a theory element.
Definition backend.hh:258
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:266
void num(prg_id_t id, prg_weight_t num)
Add a theory number.
Definition backend.hh:224
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:286
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:210
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