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_id_max = std::numeric_limits<prg_id_t>::max() - 1;
48static constexpr auto prg_lit_max = std::numeric_limits<prg_lit_t>::max();
50static constexpr auto prg_lit_min = -prg_lit_max;
51
57 public:
59 virtual ~ProgramBackend() = default;
60
67 void preamble(unsigned major, unsigned minor, unsigned revision, bool incremental) {
68 do_preamble(major, minor, revision, incremental);
69 }
70
76 void begin_step() { do_begin_step(); }
77
81 void end_ground() { do_end_ground(); }
82
89 void end_step() { do_end_step(); }
90
96 auto next_lit() -> prg_lit_t { return do_next_lit(); }
97
101 auto fact_lit() -> std::optional<prg_lit_t> { return do_fact_lit(); }
102
111 void bd_aggr(PrgLitSpan head, WeightedPrgLitSpan body, int32_t bound, bool choice) {
112 assert(std::ranges::all_of(head, [](auto const &x) { return x > 0; }));
113 do_bd_aggr(head, body, bound, choice);
114 }
115
123 void rule(PrgLitSpan head, PrgLitSpan body, bool choice) {
124 assert(std::ranges::all_of(head, [](auto const &x) { return x > 0; }));
125 do_rule(head, body, choice);
126 }
127
132 void show_term(Symbol sym, PrgLitSpan body) { do_show_term(sym, body); }
133
140 void show_term(Symbol sym, prg_id_t id) { do_show_term(sym, id); }
141
146 void show_term(prg_id_t id, PrgLitSpan body) { do_show_term(id, body); }
147
154 void show_atom(Symbol sym, prg_lit_t lit) { do_show_atom(sym, lit); }
155
161 void edge(prg_id_t u, prg_id_t v, PrgLitSpan body) { do_edge(u, v, body); }
162
172 void heuristic(prg_lit_t atom, int32_t weight, int32_t prio, HeuristicType type, PrgLitSpan body) {
173 assert(atom > 0);
174 do_heuristic(atom, weight, prio, type, body);
175 }
182 void external(prg_lit_t atom, ExternalType type) {
183 assert(atom > 0);
184 do_external(atom, type);
185 }
186
192 void project(PrgLitSpan atoms) {
193 assert(std::ranges::all_of(atoms, [](auto const &x) { return x > 0; }));
194 do_project(atoms);
195 }
196
200 void assume(PrgLitSpan literals) { do_assume(literals); }
201
206 void minimize(prg_weight_t priority, WeightedPrgLitSpan body) { do_minimize(priority, body); }
207
208 private:
209 virtual void do_preamble(unsigned major, unsigned minor, unsigned revision, bool incremental) = 0;
210 virtual void do_begin_step() = 0;
211 virtual void do_end_ground() = 0;
212 virtual void do_end_step() = 0;
213 virtual auto do_next_lit() -> prg_lit_t = 0;
214 virtual auto do_fact_lit() -> std::optional<prg_lit_t> = 0;
215
216 virtual void do_rule(PrgLitSpan head, PrgLitSpan body, bool choice) = 0;
217 virtual void do_bd_aggr(PrgLitSpan head, WeightedPrgLitSpan body, int32_t bound, bool choice) = 0;
218 virtual void do_show_term(Symbol sym, PrgLitSpan body) = 0;
219 virtual void do_show_term(Symbol sym, prg_id_t id) = 0;
220 virtual void do_show_term(prg_id_t id, PrgLitSpan body) = 0;
221 virtual void do_show_atom(Symbol sym, prg_lit_t lit) = 0;
222 virtual void do_edge(prg_id_t u, prg_id_t v, PrgLitSpan body) = 0;
223 virtual void do_heuristic(prg_lit_t atom, prg_weight_t weight, prg_weight_t prio, HeuristicType type,
224 PrgLitSpan body) = 0;
225 virtual void do_external(prg_lit_t atom, ExternalType type) = 0;
226 virtual void do_project(PrgLitSpan atoms) = 0;
227 virtual void do_assume(PrgLitSpan literals) = 0;
228 virtual void do_minimize(prg_weight_t priority, WeightedPrgLitSpan body) = 0;
229};
231using UProgramBackend = std::unique_ptr<ProgramBackend>;
232
235 public:
237 virtual ~TheoryBackend() = default;
238
245 void num(prg_id_t id, prg_weight_t num) { do_num(id, num); }
252 void str(prg_id_t id, std::string_view str) { do_str(id, str); }
261 void fun(prg_id_t id, prg_id_t name, PrgIdSpan args) { do_fun(id, name, args); }
262
270 void tup(prg_id_t id, TheoryTermTupleType type, PrgIdSpan args) { do_tup(id, type, args); }
271
279 void elem(prg_id_t id, PrgIdSpan terms, PrgLitSpan cond) { do_elem(id, terms, cond); }
280
287 void atom(prg_lit_t atom_or_zero, prg_id_t name, PrgIdSpan elems,
288 std::optional<std::pair<prg_id_t, prg_id_t>> guard) {
289 assert(atom_or_zero >= 0);
290 do_atom(atom_or_zero, name, elems, guard);
291 }
292
293 private:
294 virtual void do_num(prg_id_t id, prg_weight_t num) = 0;
295 virtual void do_str(prg_id_t id, std::string_view str) = 0;
296 virtual void do_fun(prg_id_t id, prg_id_t name, PrgIdSpan args) = 0;
297 virtual void do_tup(prg_id_t id, TheoryTermTupleType type, PrgIdSpan args) = 0;
298 virtual void do_elem(prg_id_t id, PrgIdSpan terms, PrgLitSpan cond) = 0;
299 virtual void do_atom(prg_lit_t atom_or_zero, prg_id_t name, PrgIdSpan elems,
300 std::optional<std::pair<prg_id_t, prg_id_t>> guard) = 0;
301};
303using UTheoryBackend = std::unique_ptr<TheoryBackend>;
304
306
307} // namespace CppClingo
Abstract class connecting grounder and solver.
Definition backend.hh:56
auto fact_lit() -> std::optional< prg_lit_t >
Get a factual literal if one existis.
Definition backend.hh:101
void assume(PrgLitSpan literals)
Assume the given literals.
Definition backend.hh:200
void minimize(prg_weight_t priority, WeightedPrgLitSpan body)
Minimize the given weighted literals.
Definition backend.hh:206
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:146
void show_atom(Symbol sym, prg_lit_t lit)
Show the atom with the given symbol and program literal.
Definition backend.hh:154
void rule(PrgLitSpan head, PrgLitSpan body, bool choice)
Add a disjunctive or choice rule.
Definition backend.hh:123
void bd_aggr(PrgLitSpan head, WeightedPrgLitSpan body, int32_t bound, bool choice)
Define a weight constraint.
Definition backend.hh:111
void show_term(Symbol sym, prg_id_t id)
Associate the given symbol with an id.
Definition backend.hh:140
void begin_step()
Start a new step.
Definition backend.hh:76
void preamble(unsigned major, unsigned minor, unsigned revision, bool incremental)
Initialize the backend.
Definition backend.hh:67
auto next_lit() -> prg_lit_t
Return a fresh (positive) literal.
Definition backend.hh:96
void edge(prg_id_t u, prg_id_t v, PrgLitSpan body)
Add an edge for acyclicity checking.
Definition backend.hh:161
void show_term(Symbol sym, PrgLitSpan body)
Show the given symbol if the given condition is true.
Definition backend.hh:132
void external(prg_lit_t atom, ExternalType type)
Declare the given atom as external.
Definition backend.hh:182
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:172
void end_ground()
Finalize the current grounding step.
Definition backend.hh:81
void project(PrgLitSpan atoms)
Project the given atoms.
Definition backend.hh:192
void end_step()
Finalize the current step.
Definition backend.hh:89
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:234
void str(prg_id_t id, std::string_view str)
Add a theory string.
Definition backend.hh:252
void fun(prg_id_t id, prg_id_t name, PrgIdSpan args)
Add a theory function.
Definition backend.hh:261
void tup(prg_id_t id, TheoryTermTupleType type, PrgIdSpan args)
Add a theory tuple.
Definition backend.hh:270
void elem(prg_id_t id, PrgIdSpan terms, PrgLitSpan cond)
Add a theory element.
Definition backend.hh:279
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:287
void num(prg_id_t id, prg_weight_t num)
Add a theory number.
Definition backend.hh:245
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:303
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:231
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