Clingo
Loading...
Searching...
No Matches
context.hh
1#pragma once
2
3#include <clingo/control/term.hh>
4
5#include <clingo/input/literal.hh>
6#include <clingo/input/program.hh>
7#include <clingo/input/term.hh>
8
9#include <clingo/ground/base.hh>
10#include <clingo/ground/program.hh>
11#include <clingo/ground/script.hh>
12
13#include <clingo/input/rewrite/analyze.hh>
14
15#include <clingo/util/small_vector.hh>
16#include <clingo/util/type_traits.hh>
17
18namespace CppClingo::Control {
19
22
27
30
33
36
40 public:
42 BuildContext(std::pmr::monotonic_buffer_resource &mbr, Logger &log, SymbolStore &store,
43 TheorySigVec const &theory_directives, Ground::Bases &base, Input::Component const &comp,
46 : mbr_{&mbr}, log_{&log}, store_{&store}, theory_directives_{&theory_directives}, base_{&base}, comp_{&comp},
47 def_map_{&def_map}, gcomp_{&gcomp}, var_map_{&var_map}, body_{&body}, states_{&states}, context_{context} {}
48
50 [[nodiscard]] auto index(Input::LitSymbolic const &lit) const -> size_t {
51 auto it = comp_->incomplete.find(&lit.term());
52 if (it != comp_->incomplete.end()) {
53 return static_cast<size_t>(it - comp_->incomplete.begin());
54 }
56 }
58 [[nodiscard]] auto single_pass(Input::Lit const &lit) const -> bool {
59 if (intersects(comp_->type, Input::ComponentType::single_pass)) {
60 return true;
61 }
62 if (auto const *slit = std::get_if<Input::LitSymbolic>(&lit); slit != nullptr) {
63 return slit->sign() != Sign::none || index(*slit) == Ground::stratified_index;
64 }
65 return true;
66 }
67
69 [[nodiscard]] auto single_pass_body() const -> bool {
70 return intersects(comp_->type, Input::ComponentType::single_pass) ||
71 std::ranges::all_of(*body_, [](auto const &lit) { return lit->single_pass(); });
72 }
73
75 [[nodiscard]] auto next_index() -> size_t { return comp_->incomplete.size() + index_++; }
76
78 [[nodiscard]] auto logger() const -> Logger & { return *log_; }
79
81 [[nodiscard]] auto store() const -> SymbolStore & { return *store_; }
82
84 [[nodiscard]] auto context() const -> Ground::ScriptCallback * { return context_; }
85
88 -> std::pair<Ground::UTerm, Ground::ProjectState *> {
89 return base_->add_project(*store_, term, base);
90 }
91
93 [[nodiscard]] auto type() const -> Input::ComponentType { return comp_->type; };
94
96 auto add_base(std::tuple<String, size_t, bool> sig) -> Ground::AtomBase & { return base_->add_base(sig); }
97
99 [[nodiscard]] auto mbr() const -> std::pmr::monotonic_buffer_resource & { return *mbr_; }
100
102 [[nodiscard]] auto def_map() const -> DefMap & { return *def_map_; }
104 [[nodiscard]] auto var_map() const -> VarMap & { return *var_map_; }
105
107 [[nodiscard]] auto gcomp() const -> Ground::Component & { return *gcomp_; }
109 [[nodiscard]] auto body() const -> Ground::ULitVec & { return *body_; }
110
112 template <class T, class... Args> [[nodiscard]] auto state(Args &&...args) -> T & {
113 states_->emplace_back(std::make_unique<T>(std::forward<Args>(args)...));
114 return static_cast<T &>(*states_->back());
115 }
116
118 auto inc_priority() -> size_t { return priority++; }
119
121 [[nodiscard]] auto simple_lit(Input::Lit const &lit) -> Ground::AtomSimple {
122 auto res = Ground::AtomSimple{};
123 with_simple_lit(lit, [&res]([[maybe_unused]] auto sig, auto term, auto &base, auto provides) {
124 res.emplace(std::make_tuple(std::move(term), std::ref(base), std::move(provides)));
125 });
126 return res;
127 }
128
130 [[nodiscard]] auto simple_lit(Input::Term const &term) -> Ground::AtomSimple::value_type {
131 auto res = Ground::AtomSimple{};
132 with_simple_lit(term, [&res]([[maybe_unused]] auto sig, auto term, auto &base, auto provides) {
133 res.emplace(std::make_tuple(std::move(term), std::ref(base), std::move(provides)));
134 });
135 assert(res);
136 return *std::move(res);
137 }
138
140 template <class F> void with_simple_lit(Input::Term const &term, F &&fun) {
141 auto provides = std::vector<size_t>{};
142 auto sig = signature(term);
143 assert(sig);
144 auto &base = add_base(*sig);
145 if (auto it = def_map_->find(&term); it != def_map_->end()) {
146 provides = it->second;
147 }
148 std::invoke(std::forward<F>(fun), *sig, build_term(*var_map_, term), base, std::move(provides));
149 }
150
152 template <class F> void with_simple_lit(Input::Lit const &lit, F &&fun, bool expect_truth = false) {
153 std::visit(
154 [&]<class T>(T const &lit) {
155 if constexpr (Util::matches<T, Input::LitSymbolic>) {
156 assert(lit.sign() == Sign::none);
157 with_simple_lit(lit.term(), std::forward<F>(fun));
158 return;
159 } else if constexpr (Util::matches<T, Input::LitBool>) {
160 if (lit.value() == expect_truth) {
161 return;
162 }
163 }
164 throw std::runtime_error("unexpected literal in rule head");
165 },
166 lit);
167 }
168
170 [[nodiscard]] auto is_theory_directive(TheorySig sig) const -> bool {
171 return std::ranges::binary_search(*theory_directives_, sig);
172 }
173
174 private:
175 std::pmr::monotonic_buffer_resource *mbr_;
176 Logger *log_;
177 SymbolStore *store_;
178 TheorySigVec const *theory_directives_;
179 Ground::Bases *base_;
180 Input::Component const *comp_;
181 DefMap *def_map_;
182 Ground::Component *gcomp_;
184 Ground::ULitVec *body_;
185 Ground::UStateVec *states_;
186 Ground::ScriptCallback *context_;
187 size_t priority = 0;
188 size_t index_ = 0;
189};
190
192
193} // namespace CppClingo::Control
Context object holding necessary data for translating from input to ground representation.
Definition context.hh:39
auto inc_priority() -> size_t
Increment the priority and return its previous value.
Definition context.hh:118
auto simple_lit(Input::Lit const &lit) -> Ground::AtomSimple
Translate the given simple input literal into a simple ground atom.
Definition context.hh:121
auto state(Args &&...args) -> T &
Add a new state object for a body aggregate literal.
Definition context.hh:112
auto next_index() -> size_t
Return a fresh atom index.
Definition context.hh:75
auto def_map() const -> DefMap &
Get the definition map.
Definition context.hh:102
auto single_pass(Input::Lit const &lit) const -> bool
Check if the given input literal is single pass.
Definition context.hh:58
auto store() const -> SymbolStore &
Get the symbol store.
Definition context.hh:81
auto var_map() const -> VarMap &
Get the variable map.
Definition context.hh:104
auto index(Input::LitSymbolic const &lit) const -> size_t
Get the index of the given symbolic literal.
Definition context.hh:50
auto context() const -> Ground::ScriptCallback *
Get the associated context to call scripts.
Definition context.hh:84
auto add_base(std::tuple< String, size_t, bool > sig) -> Ground::AtomBase &
Add an atom base for the given signature.
Definition context.hh:96
auto is_theory_directive(TheorySig sig) const -> bool
Check whether the given signature corresponds to a directive.
Definition context.hh:170
auto add_project(Ground::UTerm const &term, Ground::AtomBase &base) -> std::pair< Ground::UTerm, Ground::ProjectState * >
Add a base for a projection.
Definition context.hh:87
void with_simple_lit(Input::Term const &term, F &&fun)
Translate the given input term (for an atom) with a callback.
Definition context.hh:140
auto gcomp() const -> Ground::Component &
Get the current component.
Definition context.hh:107
auto logger() const -> Logger &
Get the logger.
Definition context.hh:78
auto single_pass_body() const -> bool
Check if the (current) body can be grounded in a single pass.
Definition context.hh:69
auto mbr() const -> std::pmr::monotonic_buffer_resource &
Get the monotonic allocator for the component.
Definition context.hh:99
void with_simple_lit(Input::Lit const &lit, F &&fun, bool expect_truth=false)
Translate the given simple input literal with a callback.
Definition context.hh:152
auto type() const -> Input::ComponentType
Get the component type.
Definition context.hh:93
BuildContext(std::pmr::monotonic_buffer_resource &mbr, Logger &log, SymbolStore &store, TheorySigVec const &theory_directives, Ground::Bases &base, Input::Component const &comp, DefMap &def_map, Ground::Component &gcomp, VarMap &var_map, Ground::ULitVec &body, Ground::UStateVec &states, Ground::ScriptCallback *context)
Construct the build context.
Definition context.hh:42
auto body() const -> Ground::ULitVec &
Get the current statement body.
Definition context.hh:109
auto simple_lit(Input::Term const &term) -> Ground::AtomSimple::value_type
Translate the given input term (for an atom) into a simple ground atom.
Definition context.hh:130
An atom base used to store derivable atoms and associated state.
Definition base.hh:212
The base for all atoms and terms.
Definition base.hh:407
auto add_base(std::tuple< String, size_t, bool > sig) -> Ground::AtomBase &
Add an atom base.
auto add_project(SymbolStore &store, Ground::UTerm const &term, Ground::AtomBase &base) -> std::pair< Ground::UTerm, ProjectState * >
Add a base for a projected atom.
Captures statements depending cyclically on each other.
Definition program.hh:13
Interface to call functions during parsing/grounding.
Definition script.hh:28
Literal representing a symbolic literal.
Definition literal.hh:104
Simple logger to report message to stderr or via a callback.
Definition logger.hh:63
A store for symbols.
Definition symbol.hh:454
auto build_term(Util::unordered_map< String, size_t > const &var_map, Input::Term const &term, bool &has_projection) -> Ground::UTerm
Translates input theory terms to their ground representation.
Util::unordered_map< String, size_t > VarMap
A map from variable names (input) to their indices (ground).
Definition context.hh:32
CppClingo::Ground::BaseMap BaseMap
A map from signatures to atom bases.
Definition context.hh:29
Util::unordered_map< Input::Term const *, std::vector< size_t > > DefMap
A map from terms to the indices defining them.
Definition context.hh:35
CppClingo::Ground::ProjectMap ProjectMap
A map from a terms with projections associated state used during grounding.
Definition context.hh:26
std::tuple< String, size_t > TheorySig
A signature of a theory atom.
Definition core.hh:182
std::vector< TheorySig > TheorySigVec
A vector of theory atom signatures.
Definition core.hh:184
@ none
No sign.
Util::ordered_map< std::tuple< String, size_t, bool >, UAtomBase > BaseMap
A map from signatures to atom bases.
Definition base.hh:404
Util::ordered_map< Ground::UTerm, UProjectState > ProjectMap
A map from a terms with projections associated state used during grounding.
Definition base.hh:401
std::vector< UState > UStateVec
A vector of states.
Definition base.hh:448
std::optional< std::tuple< Ground::UTerm, AtomBase &, std::vector< size_t > > > AtomSimple
Represents a simple head literal which is either represented by a symbol term or #false captured by s...
Definition literal.hh:231
std::vector< ULit > ULitVec
A vector of literals.
Definition literal.hh:31
constexpr auto stratified_index
Marker for stratified literals.
Definition literal.hh:189
std::unique_ptr< Term > UTerm
A unique pointer holding a term.
Definition term.hh:33
std::variant< LitBool, LitComparison, LitSymbolic > Lit
Variant holding the different literal types.
Definition literal.hh:129
@ single_pass
The component can be grounded in one pass.
std::variant< TermVariable, TermSymbol, TermTuple, TermFunction, TermAbs, TermUnary, TermBinary > Term
Variant holding the different term types.
Definition term.hh:45
tsl::hopscotch_map< Key, T, Hash, KeyEqual, Allocator, NeighborhoodSize, StoreHash, GrowthPolicy > unordered_map
Alias for unordered maps.
Definition unordered_map.hh:17
A refined component.
Definition program.hh:98
Util::ordered_map< Term const *, Util::ordered_set< Term const * > > incomplete
This vector captures literals that are not yet complete.
Definition program.hh:104
ComponentType type
The type of the component.
Definition program.hh:106