Clingo
Loading...
Searching...
No Matches
instantiator.hh
1#pragma once
2
3#include <clingo/ground/profile.hh>
4
5#include <clingo/core/logger.hh>
6#include <clingo/core/output.hh>
7#include <clingo/core/symbol.hh>
8
9#include <memory>
10#include <utility>
11#include <vector>
12
13namespace CppClingo::Ground {
14
17
18class Instantiator;
19
22 public:
26 [[nodiscard]] auto log() const -> Logger & { return *log_; }
28 [[nodiscard]] auto store() const -> SymbolStore & { return *store_; }
30 [[nodiscard]] auto out() const -> OutputStm & { return *out_; }
31
32 private:
33 Logger *log_;
34 SymbolStore *store_;
35 OutputStm *out_;
36};
37
40 public:
45 [[nodiscard]] auto ass() const -> Assignment & { return *ass_; }
46
47 private:
48 Assignment *ass_;
49};
50
52enum class MatcherType : uint8_t {
53 new_atoms,
54 old_atoms,
56};
57
59inline auto operator<<(std::ostream &out, MatcherType type) -> std::ostream & {
60 switch (type) {
62 out << "a";
63 break;
64 }
66 out << "o";
67 break;
68 }
69 case MatcherType::new_atoms: {
70 out << "n";
71 break;
72 }
73 }
74 return out;
75}
76
78class Matcher {
79 public:
81 virtual ~Matcher() = default;
83 void init(InstantiationContext const &ctx, size_t gen) { do_init(ctx, gen); }
85 void match(EvalContext const &ctx) { do_match(ctx); }
90 [[nodiscard]] auto next(EvalContext const &ctx) -> bool { return do_next(ctx); }
92 void print(std::ostream &out) const { do_print(out); }
94 [[nodiscard]] auto type() const -> MatcherType { return do_type(); }
95
96 private:
97 virtual void do_init(InstantiationContext const &ctx, size_t gen) = 0;
98 virtual void do_match(EvalContext const &ctx) = 0;
99 [[nodiscard]] virtual auto do_next(EvalContext const &ctx) -> bool = 0;
100 virtual void do_print(std::ostream &out) const = 0;
101 [[nodiscard]] virtual auto do_type() const -> MatcherType { return MatcherType::all_atoms; }
102};
104using UMatcher = std::unique_ptr<Matcher>;
106using UMatcherVec = std::vector<UMatcher>;
107
108class Queue;
109
112 public:
114 virtual ~InstanceCallback() = default;
116 void init(size_t gen) { do_init(gen); }
118 [[nodiscard]] auto report(EvalContext const &ctx) -> bool { return do_report(ctx); }
120 void propagate(SymbolStore &store, OutputStm &out, Queue &queue) { do_propagate(store, out, queue); }
122 [[nodiscard]] auto priority() const -> size_t { return do_priority(); }
124 void print_head(std::ostream &out) const { do_print_head(out); }
126 [[nodiscard]] auto is_important(size_t index) const -> bool { return do_is_important(index); }
130 [[nodiscard]] auto profile_node() const -> ProfileNodeInternal * { return do_profile_node(); }
131
132 private:
133 virtual void do_init(size_t gen) = 0;
134 [[nodiscard]] virtual auto do_report(EvalContext const &ctx) -> bool = 0;
135 virtual void do_propagate(SymbolStore &store, OutputStm &out, Queue &queue) = 0;
136 [[nodiscard]] virtual auto do_priority() const -> size_t = 0;
137 virtual void do_print_head(std::ostream &out) const = 0;
138 [[nodiscard]] virtual auto do_is_important([[maybe_unused]] size_t index) const -> bool { return true; }
139 [[nodiscard]] virtual auto do_profile_node() const -> ProfileNodeInternal * = 0;
140};
141
144 public:
146 using DependVec = std::vector<size_t>;
148 Instantiator(InstanceCallback &icb, size_t vars, size_t n) : icb_{&icb}, stats_{profile_node_(icb)}, ass_{vars} {
149 matchers_.reserve(n + 1);
150 }
154 void init(InstantiationContext const &ctx, size_t gen);
160 void add(UMatcher matcher, DependVec depends);
165 void finalize(DependVec depends);
170 [[nodiscard]] auto enqueue() -> bool;
174 [[nodiscard]] auto instantiate(Logger &log, SymbolStore &store, OutputStm &out) -> bool;
176 void propagate(SymbolStore &store, OutputStm &out, Queue &queue);
178 void print(std::ostream &out) const;
180 [[nodiscard]] auto priority() const { return icb_->priority(); }
181
183 friend auto operator<<(std::ostream &out, Instantiator const &inst) -> std::ostream & {
184 inst.print(out);
185 return out;
186 }
187
188 private:
189 static auto profile_node_(InstanceCallback const &icb) -> ProfileStats * {
190 auto *node = icb.profile_node();
191 return node != nullptr ? &node->add_child(std::make_unique<ProfileData>()).step_ : nullptr;
192 }
193 class BackjumpMatcher {
194 public:
195 BackjumpMatcher(UMatcher matcher, DependVec depend)
196 : matcher_{std::move(matcher)}, depend_{std::move(depend)} {}
197 void init(InstantiationContext const &ctx, size_t gen);
198 void match(EvalContext const &ctx);
199 auto next(EvalContext const &ctx) -> bool;
200 auto first(EvalContext const &ctx) -> bool;
201 void print(std::ostream &out, size_t index) const;
202 [[nodiscard]] auto depend() const -> DependVec const &;
203 [[nodiscard]] auto backjumpable() const -> bool;
204 void block();
205
206 private:
207 UMatcher matcher_;
208 std::vector<size_t> depend_;
209 bool backjumpable_ = true;
210 };
211 InstanceCallback *icb_;
212 ProfileStats *stats_;
213 Assignment ass_;
214 std::vector<BackjumpMatcher> matchers_;
215 bool enqueued_ = false;
216};
217
219using InstantiatorVec = std::vector<Instantiator>;
220
222class Queue {
223 public:
225 Queue() = default;
227 void insert(Instantiator inst, std::optional<size_t> index);
229 void propagate(size_t index);
231 [[nodiscard]] auto process(Logger &log, SymbolStore &store, OutputStm &out) -> bool;
233 auto release() -> std::vector<Instantiator> { return std::move(insts_); }
234
235 private:
237 void enter_(size_t i);
238
239 std::vector<Instantiator> insts_;
240 std::vector<std::vector<size_t>> update_;
241 std::vector<std::vector<Instantiator *>> queues_;
242 size_t size_ = 0;
243 size_t max_prio_ = 0;
244};
245
247
248} // namespace CppClingo::Ground
Context object to capture state used during instantiation.
Definition instantiator.hh:39
EvalContext(Logger &log, SymbolStore &store, OutputStm &out, Assignment &ass)
Construct an instantiation state.
Definition instantiator.hh:42
auto ass() const -> Assignment &
Get the assignment.
Definition instantiator.hh:45
Callbacks to notify statements during instantiations.
Definition instantiator.hh:111
auto report(EvalContext const &ctx) -> bool
Report an assignment giving rise to an instance for a statement.
Definition instantiator.hh:118
void propagate(SymbolStore &store, OutputStm &out, Queue &queue)
Notify a statement that instantiation has finished.
Definition instantiator.hh:120
void init(size_t gen)
Notify a statement that instantiation starts.
Definition instantiator.hh:116
auto priority() const -> size_t
The priority of the callback.
Definition instantiator.hh:122
auto profile_node() const -> ProfileNodeInternal *
Get a node to store profiling data for this statement.
Definition instantiator.hh:130
void print_head(std::ostream &out) const
Print representation for debugging.
Definition instantiator.hh:124
auto is_important(size_t index) const -> bool
Check if the literal with the given index is important.
Definition instantiator.hh:126
virtual ~InstanceCallback()=default
Destroy the callback.
Context object to capture state used during instantiation.
Definition instantiator.hh:21
auto out() const -> OutputStm &
Get the output.
Definition instantiator.hh:30
InstantiationContext(Logger &log, SymbolStore &store, OutputStm &out)
Construct an instantiation state.
Definition instantiator.hh:24
auto store() const -> SymbolStore &
Get the store.
Definition instantiator.hh:28
auto log() const -> Logger &
Get the logger.
Definition instantiator.hh:26
An instantiator implementing the basic grounding algorithm.
Definition instantiator.hh:143
std::vector< size_t > DependVec
A vector of Matcher indices.
Definition instantiator.hh:146
void print(std::ostream &out) const
Print the instantiator.
void propagate(SymbolStore &store, OutputStm &out, Queue &queue)
Add instantiators that need grounding to queue.
friend auto operator<<(std::ostream &out, Instantiator const &inst) -> std::ostream &
Print the instantiator.
Definition instantiator.hh:183
auto priority() const
The priority of the instantiator.
Definition instantiator.hh:180
void add(UMatcher matcher, DependVec depends)
Finalize the instantiator.
auto instantiate(Logger &log, SymbolStore &store, OutputStm &out) -> bool
Find all assignments for the added matchers.
auto enqueue() -> bool
Mark enqueued for instantiation.
Instantiator(InstanceCallback &icb, size_t vars, size_t n)
Construct an instantiator with the given instance callback and number of variables.
Definition instantiator.hh:148
void finalize(DependVec depends)
Finalize the instantiator.
void init(InstantiationContext const &ctx, size_t gen)
Prepare the instantiator for the first grounding step (with generation 0).
A matcher to match expressions.
Definition instantiator.hh:78
void match(EvalContext const &ctx)
Initialize matching.
Definition instantiator.hh:85
virtual ~Matcher()=default
Destroy the matcher.
auto type() const -> MatcherType
Get the type of the matcher.
Definition instantiator.hh:94
void print(std::ostream &out) const
Print the matcher to the given stream.
Definition instantiator.hh:92
void init(InstantiationContext const &ctx, size_t gen)
Notify that instantiation starts.
Definition instantiator.hh:83
auto next(EvalContext const &ctx) -> bool
Obtain the next match.
Definition instantiator.hh:90
Profile node that can hold children.
Definition profile.hh:155
A queue to process instantiators.
Definition instantiator.hh:222
Queue()=default
Construct an empty queue.
auto process(Logger &log, SymbolStore &store, OutputStm &out) -> bool
Process previously enqueued instantiators.
auto release() -> std::vector< Instantiator >
Release the contained instantiators.
Definition instantiator.hh:233
void propagate(size_t index)
Propagate instantiators with the given index.
void insert(Instantiator inst, std::optional< size_t > index)
Register an instantiator with the queue.
Simple logger to report message to stderr or via a callback.
Definition logger.hh:63
Interface to output statements.
Definition output.hh:90
A store for symbols.
Definition symbol.hh:454
std::vector< std::optional< Symbol > > Assignment
Assignment mapping variables to symbols.
Definition symbol.hh:222
auto operator<<(std::ostream &out, LitCondLitType type) -> std::ostream &
Print the type of a conditional literal grounding literal.
std::vector< UMatcher > UMatcherVec
A vector of matchers.
Definition instantiator.hh:106
std::vector< Instantiator > InstantiatorVec
A vector of instantiators.
Definition instantiator.hh:219
MatcherType
Enumeration of matcher types.
Definition instantiator.hh:52
std::unique_ptr< Matcher > UMatcher
A unique pointer to a matcher.
Definition instantiator.hh:104
@ all_atoms
Indicates a matcher for previously added atoms.
@ old_atoms
Indicates a matcher for freshly added atoms.
The profiling data.
Definition profile.hh:63