3#include <clingo/ground/matcher.hh>
4#include <clingo/ground/statement.hh>
6#include <clingo/util/enumerate.hh>
8namespace CppClingo::Ground {
37 premise_is_fact_{static_cast<uint8_t>(premise_is_fact)} {}
51 return premise_is_fact_ != 0 &&
61 [[nodiscard]]
auto has_offset() const ->
bool {
return offset_ > 0; }
67 [[nodiscard]]
auto offset() const ->
size_t {
return offset_ - 1; }
70 [[nodiscard]]
auto premise() const ->
size_t {
return premise_; }
72 [[nodiscard]]
auto conclusion() const -> std::optional<
size_t> {
73 return conclusion_ != invalid_offset ? std::make_optional(conclusion_) : std::nullopt;
77 size_t conclusion_ = invalid_offset;
79 uint64_t offset_ : 61 = 1;
81 uint64_t premise_is_fact_ : 1;
96 void add_elem(
size_t index) { elems_.emplace_back(index); }
110 [[nodiscard]]
auto is_false() const ->
bool {
return false_ != 0; }
112 [[nodiscard]]
auto has_offset() const ->
bool {
return offset_ > 0; }
118 [[nodiscard]]
auto offset() const ->
size_t {
return offset_ - 1; }
123 [[nodiscard]]
auto uid() const -> std::optional<
size_t> {
124 return uid_ != invalid_offset ? std::make_optional(uid_) : std::nullopt;
128 assert(uid_ == invalid_offset || uid_ ==
uid);
132 [[nodiscard]]
auto elems() const -> std::span<
size_t const> {
return elems_; }
135 std::vector<size_t> elems_;
136 uint64_t elems_propagated_ : 61 = 0;
137 uint64_t propagated_ : 1 = 0;
138 uint64_t enqueued_ : 1 = 0;
139 uint64_t false_ : 1 = 0;
140 size_t uid_ = invalid_offset;
156 [[nodiscard]]
auto index(
Key const &key)
const ->
size_t {
157 return std::distance(atoms_->begin(), atoms_->find(key));
160 [[nodiscard]]
auto size() const ->
size_t {
return atoms_->size(); }
163 [[nodiscard]]
auto nth(
size_t i)
const -> MapAtomCondLit::const_iterator {
return atoms_->nth(i); }
165 [[nodiscard]]
auto nth(
size_t i) -> MapAtomCondLit::iterator {
return atoms_->nth(i); }
185 void add(MapElemCondLit::iterator it) {
188 it.value().set_offset(base_.size());
189 base_.emplace_back(std::distance(elems_->begin(), it));
193 [[nodiscard]]
auto index(
Key const &key)
const ->
size_t {
194 if (
auto it = elems_->find(key); it != elems_->end() && it->second.has_offset()) {
195 return it->second.offset();
200 [[nodiscard]]
auto size() const ->
size_t {
return base_.size(); }
203 [[nodiscard]]
auto nth(
size_t i)
const -> MapElemCondLit::const_iterator {
return elems_->nth(base_[i]); }
205 [[nodiscard]]
auto nth(
size_t i) -> MapElemCondLit::iterator {
return elems_->nth(base_[i]); }
209 std::vector<size_t> base_;
223 void add(MapAtomCondLit::iterator it) {
224 it.value().set_offset(base_.size());
225 base_.emplace_back(std::distance(atoms_->begin(), it));
229 [[nodiscard]]
auto index(
Key const &key)
const ->
size_t {
230 if (
auto it = atoms_->find(key); it != atoms_->end() && it->second.has_offset()) {
231 return it->second.offset();
236 [[nodiscard]]
auto size() const ->
size_t {
return base_.size(); }
239 [[nodiscard]]
auto nth(
size_t i)
const -> MapAtomCondLit::const_iterator {
return atoms_->nth(base_[i]); }
241 [[nodiscard]]
auto nth(
size_t i) -> MapAtomCondLit::iterator {
return atoms_->nth(base_[i]); }
245 std::vector<size_t> base_;
253 bool has_conclusion,
bool sp_premise,
bool domain)
254 : local_{std::move(local)}, global_{std::move(global)}, mbr_{&mbr},
255 atoms_{0, Util::array_hash{global_.size()}, Util::array_equal_to{global_.size()}},
256 elems_{0, Util::array_hash{local_.size() + 1}, Util::array_equal_to{local_.size() + 1}}, base_empty_{atoms_},
257 base_premise_{elems_}, base_lit_{atoms_}, index_{
index}, has_conclusion_{has_conclusion},
258 sp_premise_{sp_premise}, domain_{
domain} {
259 temp_syms_.reserve(std::max(global_.size(), local_.size() + 1));
275 [[nodiscard]] auto
index() const ->
size_t;
292 [[nodiscard]] auto
domain() const ->
bool;
341 std::vector<
Symbol> mutable temp_syms_;
342 std::pmr::monotonic_buffer_resource *mbr_;
343 Symbol *syms_atom_ =
nullptr;
346 std::vector<
size_t> propagate_;
351 bool has_conclusion_;
365 ? std::max(state_->vars_global().size(), state_->vars_local().size() + 1)
366 : state_->vars_global().size());
383 friend auto operator<<(std::ostream &out,
MatchCondLit const &m) -> std::ostream &;
393 std::vector<
Symbol> mutable eval_;
406 [[nodiscard]]
auto do_domain() const ->
bool override;
407 [[nodiscard]] auto do_single_pass() const ->
bool override;
408 [[nodiscard]] auto do_matcher(std::pmr::monotonic_buffer_resource &mbr,
MatcherType type,
409 std::vector<
bool> const &bound)
410 -> std::pair<
UMatcher, std::optional<
size_t>> override;
411 [[nodiscard]] auto do_score(std::vector<
bool> const &bound) const ->
double override;
412 void do_print(std::ostream &out) const override;
414 [[nodiscard]] auto do_copy() const ->
ULit override;
415 [[nodiscard]] auto do_hash() const ->
size_t override;
416 [[nodiscard]] auto do_equal_to(
Lit const &other) const ->
bool override;
417 [[nodiscard]] auto do_compare_to(
Lit const &other) const -> std::weak_ordering override;
432 [[nodiscard]]
auto do_domain() const ->
bool override;
433 [[nodiscard]] auto do_single_pass() const ->
bool override;
434 [[nodiscard]] auto do_matcher(std::pmr::monotonic_buffer_resource &mbr,
MatcherType type,
435 std::vector<
bool> const &bound)
436 -> std::pair<
UMatcher, std::optional<
size_t>> override;
437 [[nodiscard]] auto do_score(std::vector<
bool> const &bound) const ->
double override;
438 void do_print(std::ostream &out) const override;
440 [[nodiscard]] auto do_copy() const ->
ULit override;
441 [[nodiscard]] auto do_hash() const ->
size_t override;
442 [[nodiscard]] auto do_equal_to(
Lit const &other) const ->
bool override;
443 [[nodiscard]] auto do_compare_to(
Lit const &other) const -> std::weak_ordering override;
446 void do_init(
size_t gen) override;
447 [[nodiscard]] auto do_report(
EvalContext const &ctx) ->
bool override;
449 [[nodiscard]] auto do_priority() const ->
size_t override;
450 void do_print_head(std::ostream &out) const override;
470 : state_{&base}, body_{std::move(body)}, prio_{prio}, index_{index}, type_{type} {}
474 void do_print(std::ostream &out)
const override;
475 [[nodiscard]]
auto do_body() const ->
ULitVec const & override;
476 [[nodiscard]] auto do_important() const ->
VariableSet override;
479 void do_print_head(std::ostream &out) const override;
480 void do_init(
size_t gen) override;
481 [[nodiscard]] auto do_report(
EvalContext const &ctx) ->
bool override;
483 [[nodiscard]] auto do_priority() const ->
size_t override;
A base for not yet propagated conditional literals.
Definition condlit.hh:147
auto nth(size_t i) const -> MapAtomCondLit::const_iterator
Get the n-th atom in the base.
Definition condlit.hh:163
auto size() const -> size_t
Get the number of atoms in the base.
Definition condlit.hh:160
auto index(Key const &key) const -> size_t
Map a key to its index in the base.
Definition condlit.hh:156
auto nth(size_t i) -> MapAtomCondLit::iterator
Get the n-th atom in the base.
Definition condlit.hh:165
BaseCondLitEmpty(MapAtomCondLit &atoms)
Construct the base.
Definition condlit.hh:153
A base for premises of conditional literals.
Definition condlit.hh:172
auto size() const -> size_t
Get the number of atoms in the base.
Definition condlit.hh:200
void add(MapElemCondLit::iterator it)
Add a blocked element to the base.
Definition condlit.hh:185
auto nth(size_t i) const -> MapElemCondLit::const_iterator
Get the n-th atom in the base.
Definition condlit.hh:203
auto nth(size_t i) -> MapElemCondLit::iterator
Get the n-th atom in the base.
Definition condlit.hh:205
Symbol const * Key
Key to identify atoms.
Definition condlit.hh:175
BaseCondLitPremise(MapElemCondLit &elems)
Construct the base.
Definition condlit.hh:182
auto index(Key const &key) const -> size_t
Map a key to its index in the base.
Definition condlit.hh:193
A base for conditional literals.
Definition condlit.hh:213
void add(MapAtomCondLit::iterator it)
Add a propagated atom to the base.
Definition condlit.hh:223
BaseCondLit(MapAtomCondLit &atoms)
Construct the base.
Definition condlit.hh:220
auto nth(size_t i) -> MapAtomCondLit::iterator
Get the n-th atom in the base.
Definition condlit.hh:241
auto index(Key const &key) const -> size_t
Map a key to its index in the base.
Definition condlit.hh:229
auto size() const -> size_t
Get the number of atoms in the base.
Definition condlit.hh:236
auto nth(size_t i) const -> MapAtomCondLit::const_iterator
Get the n-th atom in the base.
Definition condlit.hh:239
The base implementation of an atom base.
Definition base.hh:148
Symbol const * Key
The key identifies an atom and is usually associated with further state.
Definition base.hh:151
Context object to capture state used during instantiation.
Definition instantiator.hh:35
Callbacks to notify statements during instantiations.
Definition instantiator.hh:107
Helper literal to ground stratified conditional literals.
Definition condlit.hh:424
LitCondLitStrat(StateCondLit &state, ULitVec premise)
Construct the literal.
Definition condlit.hh:427
Helper literals to ground conditional literals.
Definition condlit.hh:399
LitCondLit(LitCondLitType type, StateCondLit &state, size_t index)
Construct the literal.
Definition condlit.hh:402
The base class for groundable literals.
Definition literal.hh:34
A term like object used to match conditional literals and their elements.
Definition condlit.hh:357
Symbol const * Key
The key to match against.
Definition condlit.hh:360
MatchCondLit(StateCondLit &state, LitCondLitType type)
Construct the matcher.
Definition condlit.hh:363
auto vars() const -> VariableSet
Get the variables of the matcher.
A queue to process instantiators.
Definition instantiator.hh:206
Capture (the state of) a conditional literal.
Definition condlit.hh:92
auto is_fact(MapElemCondLit const &elems) const -> bool
Check if all elements of the atom are facts.
void set_offset(size_t offset)
Mark the atom as derived setting its derived index.
Definition condlit.hh:114
auto elems() const -> std::span< size_t const >
Get the elements of the conditional literal.
Definition condlit.hh:132
auto is_false() const -> bool
Check if the atom has been marked false.
Definition condlit.hh:110
auto uid() const -> std::optional< size_t >
Get the unique id of the atom.
Definition condlit.hh:123
auto enqueue(MapElemCondLit const &elems) -> bool
Enqueue the atom for grounding.
void uid(size_t uid)
Set the unique id of the atom.
Definition condlit.hh:127
void add_elem(size_t index)
Add an element with the given index to the atom.
Definition condlit.hh:96
auto has_offset() const -> bool
Check if the atom has been derived.
Definition condlit.hh:112
auto propagate(MapElemCondLit const &elems) -> bool
Propagate a previously enqueued atom.
auto offset() const -> size_t
The index of the atom in the vector of derived atoms.
Definition condlit.hh:118
State to capture a set of conditional literals.
Definition condlit.hh:249
auto add_premise(EvalContext const &ctx, ULitVec const &premise) -> bool
Add a new cond lit element with the given premise.
auto add_empty(Assignment const &ass) -> std::pair< MapAtomCondLit::iterator, bool >
Add a new cond lit atom.
void add_conclusion(Assignment const &ass, MapAtomCondLit::iterator it, size_t conclusion, bool fact)
Add a conclusion to an element.
auto vars(bool all) const -> VariableSet
Get the variables occurring in the conditional literal.
auto atom_index(Assignment const &ass) -> std::optional< size_t >
Find an atom given an assignment and return its index.
void vars(VariableSet &res, bool all) const
Get the variables occurring in the conditional literal.
auto vars_local() const -> VariableVec const &
Get the local variables of the literal.
auto propagate() -> bool
Propagate enqueued conditional literals whose elements are not blocked.
auto elem_index(MapElemCondLit::const_iterator it) const -> size_t
Turn an iterator into an element index.
auto base_premise() -> BaseCondLitPremise &
Get the base containing all premises of conditional literals.
auto elem_find(Assignment const &ass, MapAtomCondLit::iterator it) -> MapElemCondLit::iterator
Get an iterator to an element given an assignment.
auto base_empty() -> BaseCondLitEmpty &
Get the base containing all conditional literals encountered during grounding.
auto atom_is_fact(MapAtomCondLit::iterator it) -> bool
Check if the given atom is a fact.
auto atom_nth(size_t index) -> MapAtomCondLit::iterator
Turn an atom index into an iterator.
auto index() const -> size_t
Get the update index of the conditional literal.
auto atom_find(Assignment const &ass) -> MapAtomCondLit::iterator
Find an atom given an assignment and return an iterator to it.
auto base_lit() -> BaseCondLit &
Get the base containing all conditional literals that have been derived.
auto domain() const -> bool
Return true if all contained literals are domain.
StateCondLit(std::pmr::monotonic_buffer_resource &mbr, VariableVec local, VariableVec global, size_t index, bool has_conclusion, bool sp_premise, bool domain)
Construct an empty state.
Definition condlit.hh:252
void output(Logger &log, SymbolStore &store, OutputStm &out) override
Output the now complete conditional literal.
auto vars_global() const -> VariableVec const &
Get the global variables of the literal.
Class to store state for grounding.
Definition base.hh:438
Helper statement to ground conditional literals.
Definition condlit.hh:466
StmCondLit(StmCondLitType type, StateCondLit &base, ULitVec body, size_t prio, size_t index)
Construct the statement.
Definition condlit.hh:469
Base class for groundable statements.
Definition statement.hh:17
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:90
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< std::optional< Symbol > > Assignment
Assignment mapping variables to symbols.
Definition symbol.hh:222
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.
@ unknown
Indicates that the atom has not yet been derived by a rule.
@ fact
Indicates that the atom is derived by a fact.
Util::ordered_map< Symbol const *, StateAtomCondLit, Util::array_hash, Util::array_equal_to > MapAtomCondLit
A map from the global variables to a conditional literal.
Definition condlit.hh:144
LitCondLitType
The type of the literals involved in grounding conditional literals.
Definition condlit.hh:14
Util::ordered_map< Symbol const *, StateCondLitElem, Util::array_hash, Util::array_equal_to > MapElemCondLit
A map from an atom + local variables to an element of a conditional literal.
Definition condlit.hh:87
auto operator<<(std::ostream &out, LitCondLitType type) -> std::ostream &
Print the type of a conditional literal grounding literal.
StmCondLitType
Type of the helper statement to ground conditional literals.
Definition condlit.hh:457
TruthConclusion
The 4-valued truth value of a conclusion.
Definition condlit.hh:24
@ empty
Literals encountered during grounding.
@ premise
Premises encountered during grounding.
@ lit
Conditional literals derived during grounding.
@ conclusion
Gather conclusions of conditional literals.
@ derived
There is rule that can derived the conclusion.
@ true_
The conclusion is true.
@ unknown
There is no rule yet that could derive the conclusion.
@ false_
The conclusion is false.
MatcherType
Enumeration of matcher types.
Definition instantiator.hh:48
std::unique_ptr< Matcher > UMatcher
A unique pointer to a matcher.
Definition instantiator.hh:100
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
@ all
Get all variables in a literal.
tsl::ordered_map< Key, T, Hash, KeyEqual, Allocator, ValueTypeContainer, IndexType > ordered_map
Alias for ordered maps.
Definition ordered_map.hh:16
Capture (the state of) an element of a conditional literal.
Definition condlit.hh:32
auto premise() const -> size_t
Get the premise of the element.
Definition condlit.hh:70
auto offset() const -> size_t
The index of the element in the vector of derived elements.
Definition condlit.hh:67
auto is_blocked() const -> bool
Check if the element is blocked.
Definition condlit.hh:50
auto is_fact() const -> bool
Check if the element is true, i.e., its conclusion is true.
Definition condlit.hh:45
auto is_false() const
Check if an element is false.
Definition condlit.hh:57
void set_conclusion(size_t conclusion, bool fact)
Mark a previously unknown conclusion either as derived or fact.
Definition condlit.hh:39
void set_offset(size_t offset)
Set the index of the element.
Definition condlit.hh:63
StateCondLitElem(size_t premise, bool premise_is_fact, bool has_conclusion)
Initialize the element.
Definition condlit.hh:35
auto has_offset() const -> bool
Check if the index of the element has already been set.
Definition condlit.hh:61
auto conclusion() const -> std::optional< size_t >
Get the conclusion index of the literal if there is one.
Definition condlit.hh:72