Clingo
Loading...
Searching...
No Matches
sort.hh
1#pragma once
2
3#include <clingo/ground/literal.hh>
4#include <clingo/ground/matcher.hh>
5#include <clingo/ground/statement.hh>
6
7#include <clingo/util/small_vector.hh>
8
9#include <map>
10#include <memory_resource>
11
12namespace CppClingo::Ground {
13
16
19 public:
21 struct ValueState {
22 bool fact;
24 };
26 using Values = std::map<Symbol, ValueState>;
31 using Pairs = std::vector<Symbol>;
34
36 AtomSortAggr() = default;
37
39 void accumulate(Symbol sym, bool fact);
40
42 [[nodiscard]] auto is_fact(Symbol sym) const -> bool;
44 [[nodiscard]] auto todo_values(SymbolStore &store) -> std::span<Symbol const>;
45
47 void add_elem(size_t idx);
49 [[nodiscard]] auto elems() const -> std::span<size_t const>;
51 [[nodiscard]] auto enqueue() -> bool;
53 void dequeue();
55 [[nodiscard]] auto todo_elems() -> std::span<size_t const>;
56
57 private:
58 [[nodiscard]] auto num_values_() const -> size_t;
59
60 Elems elems_;
61 Values values_;
62 Pairs pairs_;
63 ptrdiff_t propagated_pairs_ = 0;
64 ptrdiff_t propagated_elems_ = 0;
65 bool dirty_ = false;
66 bool fact_ = true;
67 bool enqueued_ = false;
68};
69
74class BaseSortAggr : public BaseImpl<std::pair<size_t, Symbol>, BaseSortAggr> {
75 public:
77 using BaseImpl::Key;
82
84 BaseSortAggr(size_t size, bool domain_elems, bool single_pass_elems)
85 : atoms_{0, size, size}, domain_elems_{domain_elems}, single_pass_elems_{single_pass_elems} {}
86
91 [[nodiscard]] auto is_fact(Key sym) const -> bool;
95 auto add(size_t idx, Symbol val) -> bool;
96
98 [[nodiscard]] auto size() const -> size_t;
99
103 [[nodiscard]] auto index(Key sym) const -> size_t;
105 [[nodiscard]] auto nth(size_t i) const -> AtomSet::const_iterator;
107 auto nth(size_t i) -> AtomSet::iterator;
108
110 [[nodiscard]] auto atoms() -> AtomMap &;
112 [[nodiscard]] auto derived() -> AtomSet &;
113
115 [[nodiscard]] auto domain_elems() const -> bool;
117 [[nodiscard]] auto single_pass_elems() const -> bool;
118
119 private:
120 AtomMap atoms_;
121 AtomSet derived_;
122 bool domain_elems_;
123 bool single_pass_elems_;
124};
125
126class StmSortAggrElem;
127
129class StateSortAggr : public State {
130 public:
131 class AtomKey;
135 using ElementKey = std::pair<size_t, Symbol>;
136
143
145 StateSortAggr(std::pmr::monotonic_buffer_resource &mbr, VariableVec global, UTerm term, size_t index,
146 bool domain_elems, bool single_pass_elems)
147 : base_{global.size(), domain_elems, single_pass_elems}, global_{std::move(global)}, term_{std::move(term)},
148 mbr_{&mbr}, index_{index} {}
149
153 [[nodiscard]] auto global() const -> VariableVec const &;
155 [[nodiscard]] auto symbols() -> SymbolVec &;
157 [[nodiscard]] auto term() const -> Term const &;
161 [[nodiscard]] auto domain_elems() const -> bool;
166 [[nodiscard]] auto single_pass_elems() const -> bool;
168 [[nodiscard]] auto index() const -> size_t;
169
171 auto propagate(SymbolStore &store) -> bool;
172
177 auto insert_atom(EvalContext const &ctx) -> std::pair<AtomMap::iterator, bool>;
178
180 void insert_elem(EvalContext const &ctx, AtomMap::iterator it, StmSortAggrElem &elem);
181
183 auto atom_index(AtomMap::iterator it) -> size_t;
184
186 void print(std::ostream &out, bool print_index);
187
189 [[nodiscard]] auto base() -> BaseSortAggr &;
190
192 void output(Logger &log, SymbolStore &store, OutputStm &out) override;
193
194 private:
196 void enqueue_(AtomMap::iterator it);
197
198 BaseSortAggr base_;
199 ElementMap tuples_;
200 VariableVec global_;
201 SymbolVec symbols_;
202 UTerm term_;
203 std::vector<size_t> queue_;
204 std::pmr::monotonic_buffer_resource *mbr_;
205 AtomKey *atom_key_ = nullptr;
206 size_t index_;
207};
208
211 public:
214
216 MatchSortAggr(StateSortAggr &state) : state_{&state} { eval_.reserve(state_->global().size()); }
217
219 [[nodiscard]] auto vars() const -> VariableSet;
220
222 [[nodiscard]] auto signature(VariableSet const &bound, [[maybe_unused]] VariableSet const &bind) const
223 -> VariableVec;
224
226 [[nodiscard]] auto match(EvalContext const &ctx, Key key) const -> bool;
227
229 [[nodiscard]] auto eval(EvalContext const &ctx) const -> std::optional<Key>;
230
232 friend auto operator<<(std::ostream &out, MatchSortAggr const &m) -> std::ostream &;
233
235 [[nodiscard]] auto state() const -> StateSortAggr &;
236
237 private:
238 std::vector<Symbol> mutable eval_;
239 StateSortAggr *state_;
240};
241
243class LitSortAggr : public Lit, private MatchSortAggr {
244 public:
247
248 private:
249 void do_vars(VariableSet &vars, VarSelectMode mode) const override;
250
251 [[nodiscard]] auto do_domain() const -> bool override;
252
254 [[nodiscard]] auto do_single_pass() const -> bool override;
255
256 [[nodiscard]] auto do_matcher(std::pmr::monotonic_buffer_resource &mbr, MatcherType type,
257 std::vector<bool> const &bound)
258 -> std::pair<UMatcher, std::optional<size_t>> override;
259
260 [[nodiscard]] auto do_score([[maybe_unused]] std::vector<bool> const &bound, [[maybe_unused]] double estimate) const
261 -> double override;
262 [[nodiscard]] auto do_domain_size(EstimateSelector sel) const -> std::optional<double> override;
263
264 void do_print(std::ostream &out) const override;
265
266 auto do_output([[maybe_unused]] EvalContext const &ctx, OutputLit &out) const -> bool override;
267
268 [[nodiscard]] auto do_copy() const -> ULit override;
269
270 [[nodiscard]] auto do_hash() const -> size_t override;
271
272 [[nodiscard]] auto do_equal_to(Lit const &other) const -> bool override;
273
274 [[nodiscard]] auto do_compare_to(Lit const &other) const -> std::weak_ordering override;
275
276 size_t offset_ = invalid_offset;
277};
278
280class StmSortAggrElem : public Stm {
281 public:
287 StmSortAggrElem(StateSortAggr &state, Location loc_weight, UTermVec tuple, ULitVec body, size_t num_cond,
288 size_t priority, ProfileNodeInternal *node)
289 : state_{&state}, loc_weight_{std::move(loc_weight)}, tuple_{std::move(tuple)}, body_{std::move(body)},
290 node_{node}, num_cond_{num_cond}, priority_{priority} {}
291
294 : state_{other.state_}, loc_weight_{other.loc_weight_}, tuple_{copy_uvec(other.tuple_)},
295 body_{copy_uvec(other.body_)}, node_{other.node_}, num_cond_{other.num_cond_}, priority_{other.priority_} {};
297 StmSortAggrElem(StmSortAggrElem &&other) noexcept = default;
299 auto operator=(StmSortAggrElem const &other) -> StmSortAggrElem & = default;
301 auto operator=(StmSortAggrElem &&other) noexcept -> StmSortAggrElem & = default;
302
303 private:
304 friend class StateSortAggr;
305
306 [[nodiscard]] auto do_body() const -> ULitVec const & override;
307 [[nodiscard]] auto do_important() const -> VariableSet override;
308 [[nodiscard]] auto do_is_important(size_t index) const -> bool override;
309 void do_init([[maybe_unused]] size_t gen) override;
310 [[nodiscard]] auto do_report(EvalContext const &ctx) -> bool override;
311 void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) override;
312 [[nodiscard]] auto do_priority() const -> size_t override;
313 void do_print_head(std::ostream &out) const override;
314 void do_print(std::ostream &out) const override;
315 [[nodiscard]] auto do_profile_node() const -> ProfileNodeInternal * override { return node_; }
316
317 auto get_cond_(EvalContext const &ctx) -> std::pair<size_t, bool>;
318
319 StateSortAggr *state_;
320 Location loc_weight_;
321 UTermVec tuple_;
322 ULitVec body_;
323 ProfileNodeInternal *node_;
324 size_t num_cond_;
325 size_t priority_;
326};
327
328static_assert(std::is_nothrow_move_constructible_v<StmSortAggrElem>);
329static_assert(std::is_nothrow_move_assignable_v<StmSortAggrElem>);
330static_assert(std::is_copy_assignable_v<StmSortAggrElem>);
331
333class LitSortAggrStrat : public Lit, private MatchSortAggr {
334 public:
336 LitSortAggrStrat(StateSortAggr &state, std::vector<StmSortAggrElem> elems)
337 : MatchSortAggr{state}, elems_{std::move(elems)} {}
338
339 private:
340 void do_vars(VariableSet &vars, VarSelectMode mode) const override;
341
342 [[nodiscard]] auto do_domain() const -> bool override;
343
344 [[nodiscard]] auto do_single_pass() const -> bool override;
345
346 [[nodiscard]] auto do_matcher(std::pmr::monotonic_buffer_resource &mbr, MatcherType type,
347 std::vector<bool> const &bound)
348 -> std::pair<UMatcher, std::optional<size_t>> override;
349
350 [[nodiscard]] auto do_score([[maybe_unused]] std::vector<bool> const &bound, [[maybe_unused]] double estimate) const
351 -> double override;
352
353 [[nodiscard]] auto do_domain_size(EstimateSelector sel) const -> std::optional<double> override;
354
355 void do_print(std::ostream &out) const override;
356
357 auto do_output([[maybe_unused]] EvalContext const &ctx, OutputLit &out) const -> bool override;
358
359 [[nodiscard]] auto do_copy() const -> ULit override;
360
361 [[nodiscard]] auto do_hash() const -> size_t override;
362
363 [[nodiscard]] auto do_equal_to(Lit const &other) const -> bool override;
364
365 [[nodiscard]] auto do_compare_to(Lit const &other) const -> std::weak_ordering override;
366
367 std::vector<StmSortAggrElem> elems_;
368 size_t offset_ = invalid_offset;
369};
370
372
373} // namespace CppClingo::Ground
Extensible ground representation of sort aggregates.
Definition sort.hh:18
auto todo_elems() -> std::span< size_t const >
Get the elements that have to be propagated.
auto elems() const -> std::span< size_t const >
Get the aggregate elements.
AtomSortAggr()=default
Initialize an empty aggregate.
auto todo_values(SymbolStore &store) -> std::span< Symbol const >
Get the values to propagate.
void accumulate(Symbol sym, bool fact)
Accumulate a symbol.
auto is_fact(Symbol sym) const -> bool
Check if the given symbol tuple is a fact.
auto enqueue() -> bool
Enqueue aggregate to propagate its elements.
std::map< Symbol, ValueState > Values
The set of symbols accumulated by the sort aggregate atom.
Definition sort.hh:26
std::vector< Symbol > Pairs
The set of symbol pairs derived from the values.
Definition sort.hh:31
void dequeue()
Dequeue an aggregate whose elements have been propagated.
void add_elem(size_t idx)
Add a new element.
The base implementation of an atom base.
Definition base.hh:148
KeyType Key
The key identifies an atom and is usually associated with further state.
Definition base.hh:151
auto contains(Key const &sym, MatcherType type) const -> std::optional< size_t >
Check if the base contains the given atom with in the given generation.
Definition base.hh:160
The base capturing derived sort aggregate atoms.
Definition sort.hh:74
auto size() const -> size_t
Get the number of derived atoms.
auto add(size_t idx, Symbol val) -> bool
Add an atom to the base.
auto is_fact(Key sym) const -> bool
Check if the given atom is a fact.
KeyType Key
The key identifies an atom and is usually associated with further state.
Definition base.hh:151
Util::ordered_map< Symbol const *, AtomSortAggr, Util::array_hash, Util::array_equal_to > AtomMap
Map containing the atoms.
Definition sort.hh:79
BaseSortAggr(size_t size, bool domain_elems, bool single_pass_elems)
Construct an empty base.
Definition sort.hh:84
Util::ordered_map< Key, size_t > AtomSet
Map containing the derived atoms and their values.
Definition sort.hh:81
Context object to capture state used during instantiation.
Definition instantiator.hh:41
Literal representing a stratified sort aggregate.
Definition sort.hh:333
LitSortAggrStrat(StateSortAggr &state, std::vector< StmSortAggrElem > elems)
Construct a sort aggregate literal.
Definition sort.hh:336
Literal representing a sort aggregate.
Definition sort.hh:243
LitSortAggr(StateSortAggr &state)
Construct a sort aggregate literal.
Definition sort.hh:246
The base class for groundable literals.
Definition literal.hh:34
A term like object to match sort aggregates.
Definition sort.hh:210
auto vars() const -> VariableSet
Get the variables of the matcher.
BaseSortAggr::Key Key
The key to match against.
Definition sort.hh:213
MatchSortAggr(StateSortAggr &state)
Construct the matcher.
Definition sort.hh:216
Profile node that can hold children.
Definition profile.hh:156
A queue to process instantiators.
Definition instantiator.hh:225
State storing all necessary information to ground sort aggregates.
Definition sort.hh:129
StateSortAggr(std::pmr::monotonic_buffer_resource &mbr, VariableVec global, UTerm term, size_t index, bool domain_elems, bool single_pass_elems)
Initialize an aggregate state.
Definition sort.hh:145
std::pair< size_t, Symbol > ElementKey
Keys for aggregate elements storing their aggregate atom index and symbol pair.
Definition sort.hh:135
Util::ordered_map< ElementKey, Util::small_vector< size_t > > ElementMap
A map from tuples to their conditions.
Definition sort.hh:142
BaseSortAggr::AtomMap AtomMap
A map from global variables to the aggregate representation.
Definition sort.hh:138
auto global() const -> VariableVec const &
Get the global variables in the aggregate.
Class to store state for grounding.
Definition base.hh:451
Gather sort aggregate elements.
Definition sort.hh:280
StmSortAggrElem(StmSortAggrElem const &other)
Copy constructor.
Definition sort.hh:293
auto operator=(StmSortAggrElem &&other) noexcept -> StmSortAggrElem &=default
Move assignment.
auto operator=(StmSortAggrElem const &other) -> StmSortAggrElem &=default
Copy assignment.
StmSortAggrElem(StmSortAggrElem &&other) noexcept=default
Move constructor.
StmSortAggrElem(StateSortAggr &state, Location loc_weight, UTermVec tuple, ULitVec body, size_t num_cond, size_t priority, ProfileNodeInternal *node)
Construct the statement.
Definition sort.hh:287
Base class for groundable statements.
Definition statement.hh:17
Term interface.
Definition term.hh:68
The Location of an expression in an input source.
Definition location.hh:44
Simple logger to report message to stderr or via a callback.
Definition logger.hh:63
Interface to output literals.
Definition output.hh:61
Interface to output statements.
Definition output.hh:82
A store for symbols.
Definition symbol.hh:454
Variant-like class to store symbols stored in a symbol store.
Definition symbol.hh:225
std::vector< Symbol > SymbolVec
A vector of symbols.
Definition symbol.hh:220
EstimateSelector
Selector for which estimates to consider.
Definition core.hh:204
VariableSet::values_container_type VariableVec
A vector of variables.
Definition base.hh:21
Util::ordered_set< size_t > VariableSet
A set of variables.
Definition base.hh:19
@ derived
Indicates that the atom is derived by some rule but not a fact.
@ fact
Indicates that the atom is derived by a fact.
MatcherType
Enumeration of matcher types.
Definition instantiator.hh:54
std::unique_ptr< Matcher > UMatcher
A unique pointer to a matcher.
Definition instantiator.hh:106
std::unique_ptr< Lit > ULit
A unique pointer holding a literal.
Definition literal.hh:29
VarSelectMode
Available variable selection modes.
Definition literal.hh:21
std::vector< ULit > ULitVec
A vector of literals.
Definition literal.hh:31
auto copy_uvec(std::vector< std::unique_ptr< T > > const &vec)
Helper to copy vectors with copyable elements.
Definition term.hh:45
std::unique_ptr< Term > UTerm
A unique pointer holding a term.
Definition term.hh:34
std::vector< UTerm > UTermVec
A vector of terms.
Definition term.hh:36
tsl::ordered_map< Key, T, Hash, KeyEqual, Allocator, ValueTypeContainer, IndexType > ordered_map
Alias for ordered maps.
Definition ordered_map.hh:16
The state of a value provided by some element.
Definition sort.hh:21
bool propagated
whether the value has been propagated
Definition sort.hh:23
bool fact
value is provided by a fact
Definition sort.hh:22