Clingo
Loading...
Searching...
No Matches
backend.hh
1#include <clingo/core/backend.hh>
2#include <clingo/core/output.hh>
3
4#include <clingo/util/checked_math.hh>
5#include <clingo/util/enum.hh>
6#include <clingo/util/graph.hh>
7#include <clingo/util/interval_set.hh>
8#include <clingo/util/ordered_map.hh>
9#include <clingo/util/unordered_map.hh>
10
11namespace CppClingo::Output {
12
15
18 public:
23
28 TheoryData(SymbolStore &store, UTheoryBackend backend) : store_{&store}, backend_{std::move(backend)} {}
29
35
41
49 auto fun(prg_id_t name, IdVec args) -> prg_id_t;
55 auto fun(String name, IdVec args) -> prg_id_t { return fun(str(name), std::move(args)); }
56
58 auto fun(String name, PrgIdSpan args) -> prg_id_t { return fun(str(name), IdVec{args.begin(), args.end()}); }
59
66
69 return tup(type, IdVec{args.begin(), args.end()});
70 }
71
77
83 auto elem(IdVec tuple, LitVec cond) -> prg_id_t;
84
86 auto elem(PrgIdSpan tuple, PrgLitSpan cond) -> prg_id_t {
87 return elem(IdVec{tuple.begin(), tuple.end()}, LitVec{cond.begin(), cond.end()});
88 }
89
101 auto atom(std::function<prg_lit_t()> const &atom, prg_id_t name, IdVec elems,
102 std::optional<std::pair<prg_id_t, prg_id_t>> guard) -> prg_lit_t;
103
105 auto atom(std::function<prg_lit_t()> const &atom, Symbol name, IdVec elems,
106 std::optional<std::pair<String, prg_id_t>> guard) -> prg_lit_t;
107
109 auto atom(std::function<prg_lit_t()> const &atom, Symbol name, PrgIdSpan elems,
110 std::optional<std::pair<String, prg_id_t>> guard) -> prg_lit_t {
111 return this->atom(atom, name, IdVec{elems.begin(), elems.end()}, guard);
112 }
113
115 void reset() noexcept;
116
117 private:
118 using StringMap = Util::unordered_map<SharedString, prg_id_t>;
119 using NumMap = Util::unordered_map<prg_weight_t, prg_id_t>;
120 using FunMap = Util::unordered_map<std::pair<prg_id_t, IdVec>, prg_id_t>;
121 using TupMap = Util::unordered_map<std::pair<TheoryTermTupleType, IdVec>, prg_id_t>;
122 using ElemMap = Util::unordered_map<std::pair<IdVec, LitVec>, prg_id_t>;
123 using AtomMap =
124 Util::unordered_map<std::tuple<prg_id_t, IdVec, std::optional<std::pair<prg_id_t, prg_id_t>>>, prg_lit_t>;
125
131 template <class M, class V> auto insert_(M &map, V &&val) -> std::pair<typename M::iterator, bool>;
132
133 SymbolStore *store_;
134 UTheoryBackend backend_;
135 Util::OutputBuffer buf_;
136 StringMap strings_;
137 NumMap nums_;
138 FunMap funs_;
139 TupMap tups_;
140 ElemMap elems_;
141 AtomMap atoms_;
142 prg_id_t ids_ = 0;
143};
144
156
158
159} // namespace CppClingo::Output
Class similar to Potassco::TheoryData but with automatic id generation.
Definition backend.hh:17
auto tup(TheoryTermTupleType type, PrgIdSpan args) -> prg_id_t
Overload for spans.
Definition backend.hh:68
auto atom(std::function< prg_lit_t()> const &atom, Symbol name, IdVec elems, std::optional< std::pair< String, prg_id_t > > guard) -> prg_lit_t
Overload for strings.
auto num(prg_weight_t num) -> prg_id_t
Add a number term.
auto sym(Symbol sym) -> prg_id_t
Convert a symbol into a theory term.
auto fun(prg_id_t name, IdVec args) -> prg_id_t
Add a function term.
auto elem(PrgIdSpan tuple, PrgLitSpan cond) -> prg_id_t
Overload for spans.
Definition backend.hh:86
auto tup(TheoryTermTupleType type, IdVec args) -> prg_id_t
Add a tuple term.
auto atom(std::function< prg_lit_t()> const &atom, prg_id_t name, IdVec elems, std::optional< std::pair< prg_id_t, prg_id_t > > guard) -> prg_lit_t
Add a theory atom.
auto fun(String name, PrgIdSpan args) -> prg_id_t
Overload for strings and spans.
Definition backend.hh:58
TheoryData(SymbolStore &store, UTheoryBackend backend)
Construct a theory data object.
Definition backend.hh:28
void reset() noexcept
Clear the theory data.
auto str(String str) -> prg_id_t
Add a string term.
auto elem(IdVec tuple, LitVec cond) -> prg_id_t
Add a theory element.
auto atom(std::function< prg_lit_t()> const &atom, Symbol name, PrgIdSpan elems, std::optional< std::pair< String, prg_id_t > > guard) -> prg_lit_t
Overload for strings and spans.
Definition backend.hh:109
auto fun(String name, IdVec args) -> prg_id_t
Overload for strings.
Definition backend.hh:55
Abstract class connecting grounder and solver.
Definition backend.hh:54
Class managing the lifetime of a String.
Definition symbol.hh:93
Reference to a string stored in a symbol store.
Definition symbol.hh:18
A store for symbols.
Definition symbol.hh:454
Variant-like class to store symbols stored in a symbol store.
Definition symbol.hh:225
A vector that misuses the begin, end and capacity pointers to store elements.
Definition small_vector.hh:26
auto begin() -> iterator
Get an iterator to the beginning of the vector.
Definition small_vector.hh:135
int32_t prg_lit_t
A program literal.
Definition backend.hh:27
std::unique_ptr< OutputStm > UOutputStm
Unique pointer for statement output.
Definition output.hh:253
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
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
TheoryTermTupleType
Enumeration of theory term tuple types.
Definition core.hh:165
auto make_backend_output(SymbolStore &store, ProgramBackend &backend, TheoryData &theory) -> UOutputStm
Create an output that forwards ground statements to a backend.