Clingo
Loading...
Searching...
No Matches
program.hh
1#pragma once
2
3#include <clingo/input/statement.hh>
4
5#include <clingo/core/logger.hh>
6
7#include <clingo/util/enum.hh>
8#include <clingo/util/ordered_map.hh>
9#include <clingo/util/ordered_set.hh>
10
11namespace CppClingo::Input {
12
15
19enum class ProjectionMode : uint8_t {
20 disabled = 0,
21 anonymous = 1,
22 pure = 2,
23};
24
32
35
38
49using ProgramPartVec = std::vector<ProgramPart>;
50
53 public:
55 void add(SymbolStore &store, Stm stm);
56
58 void clear();
59
61 [[nodiscard]] auto empty() const -> bool;
62
64 void mark(SymbolCollector &gc) const;
65
67 void join(UnprocessedProgram const &other);
68
70 [[nodiscard]] auto parts() const -> ProgramPartVec const & { return parts_; }
72 [[nodiscard]] auto meta_stms() const -> StmVec const & { return meta_stms_; }
74 private:
75 ProgramPartVec parts_;
76 StmVec meta_stms_;
77};
78
84enum class ComponentType : uint8_t {
85 positive = 1,
86 single_pass = 2,
87};
90
108
110using Components = std::vector<std::vector<Component>>;
111
114 public:
116 virtual ~DependencyBuilder() = default;
118 void param(ProgramParam const &param) { do_param(param); }
120 void meta(std::vector<Stm> const &stms) { do_meta(stms); }
122 void fact(std::vector<Symbol> const &facts) { do_fact(facts); }
124 [[nodiscard]] auto components(Components const &comps) -> bool { return do_components(comps); }
125
126 private:
127 virtual void do_param(ProgramParam const &param) = 0;
128 virtual void do_meta(std::vector<Stm> const &stms) = 0;
129 virtual void do_fact(SymbolVec const &facts) = 0;
130 [[nodiscard]] virtual auto do_components(Components const &comps) -> bool = 0;
131};
132
134class Program {
135 public:
139 Program(RewriteOptions opts) : opts_{opts} {}
144 void join(Logger &log, SymbolStore &store, UnprocessedProgram const &prg);
148 template <class F> void visit_stms(SymbolStore &store, F fun) const {
149 for (auto const &stm : script_stms_) {
150 fun(stm);
151 }
152 if (default_parts_) {
153 fun(*default_parts_);
154 }
155 for (auto const &stm : defined_stms_) {
156 fun(stm);
157 }
158 for (auto const &[id, sym] : const_map_) {
159 fun(Stm{StmConst{sym.first.loc(), sym.first.type(), sym.first.name(),
160 TermSymbol{location(sym.first.value()), *sym.second}}});
161 }
162 for (auto const &stm : thy_stms_) {
163 fun(stm);
164 }
165 for (auto const &stm : meta_stms_) {
166 fun(stm);
167 }
168 for (auto const &[sig, part] : parts_) {
169 auto pum = param_map_(store, part);
170 auto loc = part.part.loc();
171 auto ids = SharedStringArray{pum.begin(), pum.end(), [](auto const &x) { return *x.second; }};
172 fun(StmProgram{loc, *sig.first, std::move(ids)});
173 for (auto const &fact : part.facts) {
174 fun(Stm{StmRule{loc, HdLitSimple{LitSymbolic{loc, Sign::none, TermSymbol{loc, fact}}}, BdLitArray{}}});
175 }
176 for (auto const &stm : part.stms) {
177 if (auto unmapped = unmap_(store, pum, stm); unmapped) {
178 fun(std::move(unmapped).value());
179 } else {
180 fun(stm);
181 }
182 }
183 }
184 }
186 auto meta_stms() -> StmVec const & { return meta_stms_; }
187
189 [[nodiscard]] auto analyze(SymbolStore &store, ProgramParamVec const &params, DependencyBuilder &bld) const -> bool;
190
192 void mark(SymbolCollector &gc) const;
193
195 void check(Logger &log);
196
198 [[nodiscard]] auto theory_directives() const -> TheorySigVec;
199
201 [[nodiscard]] auto const_map() const -> ConstMap const & { return const_map_; };
202
204 void mark_sig(Input::Sig const &sig);
205
207 [[nodiscard]] auto default_parts() -> std::optional<StmParts> & { return default_parts_; }
208
209 private:
213 using Signature = std::pair<SharedString, size_t>;
216
218 [[nodiscard]] static auto param_map_(SymbolStore &store, ProgramPart const &part) -> ParamUnmap;
220 [[nodiscard]] static auto unmap_(SymbolStore &store, ParamUnmap const &pum, Stm const &stm) -> std::optional<Stm>;
221
223 RewriteOptions opts_;
225 StmVec meta_stms_;
227 std::vector<StmScript> script_stms_;
229 std::vector<StmDefined> defined_stms_;
231 std::vector<StmTheory> thy_stms_;
233 std::optional<StmParts> default_parts_;
235 PartMap parts_;
237 ConstMap const_map_;
239 SharedSigSet provide_;
243 size_t depend_offset_ = 0;
244};
245
247
248} // namespace CppClingo::Input
Interface to process a rewritten and analyzed input program.
Definition program.hh:113
auto components(Components const &comps) -> bool
Add components.
Definition program.hh:124
void param(ProgramParam const &param)
Add parts to ground.
Definition program.hh:118
void fact(std::vector< Symbol > const &facts)
Add facts.
Definition program.hh:122
virtual ~DependencyBuilder()=default
Default destructor.
void meta(std::vector< Stm > const &stms)
Add meta statements.
Definition program.hh:120
A single literal in a rule head.
Definition head_literal.hh:15
Literal representing a symbolic literal.
Definition literal.hh:104
A program consisting of parts.
Definition program.hh:134
void check(Logger &log)
Check program and emit diagnostics.
Program(RewriteOptions opts)
Initialize a program with a rewrite level.
Definition program.hh:139
void mark(SymbolCollector &gc) const
Mark symbols occurring in the program.
auto analyze(SymbolStore &store, ProgramParamVec const &params, DependencyBuilder &bld) const -> bool
Prepare the statements in a program for grounding.
void join(Logger &log, SymbolStore &store, UnprocessedProgram const &prg)
Join with the given unprocessed program.
void mark_sig(Input::Sig const &sig)
Mark the given signature as provided.
auto const_map() const -> ConstMap const &
Get the constants in the program.
Definition program.hh:201
auto theory_directives() const -> TheorySigVec
Get a sorted vector of all signatures of theory directives in the program.
auto default_parts() -> std::optional< StmParts > &
Get the default parts.
Definition program.hh:207
auto meta_stms() -> StmVec const &
Get the meta statements in the program.
Definition program.hh:186
void visit_stms(SymbolStore &store, F fun) const
Visit all the statements in the program.
Definition program.hh:148
A const statement.
Definition statement.hh:728
auto loc() const -> Location const &
The location of the statement.
Definition statement.hh:741
A program statement.
Definition statement.hh:690
A rule.
Definition statement.hh:16
Term representing a symbol.
Definition term.hh:120
Program grouping unprocessed statements.
Definition program.hh:52
void add(SymbolStore &store, Stm stm)
Add a statement.
void mark(SymbolCollector &gc) const
Mark symbols occurring in the program.
void clear()
Reset the program to its initial state removing all added statements.
auto meta_stms() const -> StmVec const &
Meta statements.
Definition program.hh:72
auto empty() const -> bool
Check if the program is empty.
void join(UnprocessedProgram const &other)
Join with another unprocessed program.
auto parts() const -> ProgramPartVec const &
Unprocessed statements.
Definition program.hh:70
Simple logger to report message to stderr or via a callback.
Definition logger.hh:63
Helper class to mark owned symbols.
Definition symbol.hh:429
A store for symbols.
Definition symbol.hh:454
auto begin() const noexcept -> const_iterator
Get an iterator pointing to the beginning of the array.
Definition immutable_array.hh:117
std::vector< Symbol > SymbolVec
A vector of symbols.
Definition symbol.hh:220
std::vector< TheorySig > TheorySigVec
A vector of theory atom signatures.
Definition core.hh:184
@ none
No sign.
@ sig
Check if term is a signature.
ComponentType
The type of a component.
Definition program.hh:84
std::vector< ProgramPart > ProgramPartVec
Statements grouped by parts.
Definition program.hh:49
std::vector< std::vector< Component > > Components
The list of components in groundable order.
Definition program.hh:110
Util::ordered_map< SharedString, SharedString > ParamUnmap
Map from parameters to their replacements.
Definition program.hh:37
Util::ordered_map< SharedString, std::pair< StmConst, SharedSymbol > > ConstMap
Map from identifiers to constants.
Definition program.hh:34
ProjectionMode
Enumeration to select variables to project.
Definition program.hh:19
@ positive
The component does not contain a negative cycle.
@ single_pass
The component can be grounded in one pass.
@ disabled
Disable projection.
@ anonymous
Only project anonymous variables.
@ pure
Project pure variables.
std::vector< ProgramParam > ProgramParamVec
A list of program params.
Definition statement.hh:761
std::variant< StmRule, StmTheory, StmOptimize, StmWeakConstraint, StmShow, StmShowNothing, StmShowSig, StmProject, StmProjectSig, StmDefined, StmExternal, StmEdge, StmHeuristic, StmScript, StmInclude, StmProgram, StmConst, StmParts, StmComment > Stm
Variant of available statements.
Definition statement.hh:828
std::vector< Stm > StmVec
A vector of statements.
Definition statement.hh:830
std::pair< SharedString, std::vector< SharedSymbol > > ProgramParam
Concrete symbols for a program statement.
Definition statement.hh:759
Util::ordered_set< SharedSig > SharedSigSet
A set of predicate signatures.
Definition term.hh:42
std::tuple< String, size_t, bool > Sig
The signature of a predicate.
Definition term.hh:38
tsl::hopscotch_set< Key, Hash, KeyEqual, Allocator, NeighborhoodSize, StoreHash, GrowthPolicy > unordered_set
Alias for unordered sets.
Definition unordered_set.hh:16
tsl::ordered_map< Key, T, Hash, KeyEqual, Allocator, ValueTypeContainer, IndexType > ordered_map
Alias for ordered maps.
Definition ordered_map.hh:16
#define CLINGO_ENABLE_BITSET_ENUM(E,...)
Opt-in macro for enabling bit operations for a given enum type.
Definition enum.hh:18
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
std::vector< Stm const * > stms
The statements in the component.
Definition program.hh:100
Util::unordered_set< std::tuple< String, size_t, bool > > depend
The literals a component depends on.
Definition program.hh:102
A program part.
Definition program.hh:40
StmVec stms
The statements in the program part.
Definition program.hh:44
SymbolVec facts
The facts in the program part.
Definition program.hh:46
StmProgram part
The (first) program part statement that introduced the part.
Definition program.hh:42
Options to configure rewriting.
Definition program.hh:26
ProjectionMode project_mode
The projection mode.
Definition program.hh:28
bool project_anonymous
Whether to project anonymous variables in negative literals.
Definition program.hh:30