Clingo
Loading...
Searching...
No Matches
output.hh
1#pragma once
2
3#include <clingo/core/backend.hh>
4#include <clingo/core/core.hh>
5#include <clingo/core/symbol.hh>
6
7#include <clingo/util/small_vector.hh>
8
9namespace CppClingo {
10
13
15using IndexSpan = std::span<size_t const>;
17using IndexVec = std::vector<size_t>;
18
21 public:
23 enum class AtomType : uint8_t {
24 head,
25 body,
26 directive,
27 };
28
30 using OptGuard = std::optional<std::pair<String, size_t>>;
32 virtual ~OutputTheory() = default;
34 auto str(String val) -> size_t { return do_str(val); }
36 auto num(Number const &num) -> size_t { return do_num(num); }
38 auto fun(String name, IndexSpan args) -> size_t { return do_fun(name, args); }
40 auto tup(TheoryTermTupleType type, IndexSpan args) -> size_t { return do_tup(type, args); }
42 auto elem(IndexSpan tuple, size_t cond) -> size_t { return do_elem(tuple, cond); }
44 void atom(AtomType type, size_t atom_uid, Symbol name, IndexSpan elems, OptGuard guard = std::nullopt) {
45 do_atom(type, atom_uid, name, elems, guard);
46 }
48 auto sym(Symbol sym) -> size_t { return do_sym(sym); }
49
50 private:
51 virtual auto do_str(String val) -> size_t = 0;
52 virtual auto do_num(Number const &val) -> size_t = 0;
53 virtual auto do_fun(String name, std::span<size_t const> args) -> size_t = 0;
54 virtual auto do_tup(TheoryTermTupleType type, std::span<size_t const> args) -> size_t = 0;
55 virtual auto do_sym(Symbol sym) -> size_t = 0;
56 virtual auto do_elem(IndexSpan tuple, size_t cond) -> size_t = 0;
57 virtual void do_atom(AtomType type, size_t atom_uid, Symbol name, IndexSpan elems, OptGuard guard) = 0;
58};
59
61class OutputLit {
62 public:
64 virtual ~OutputLit() = default;
66 void lit(Sign sign, Symbol sym, size_t uid) { do_lit(sign, sym, uid); }
68 void boolean(bool value) { do_boolean(value); }
72 auto cond_lit(std::optional<size_t> uid) -> size_t { return do_cond_lit(uid); }
77 auto bd_aggr(Sign sign, std::optional<size_t> uid) -> size_t { return do_bd_aggr(sign, uid); }
79 auto bd_theory(Sign sign, std::optional<size_t> uid) -> size_t { return do_bd_theory(sign, uid); }
80
81 private:
82 virtual void do_lit(Sign sign, Symbol sym, size_t uid) = 0;
83 virtual void do_boolean(bool value) = 0;
84 virtual auto do_cond_lit(std::optional<size_t> uid) -> size_t = 0;
85 virtual auto do_bd_aggr(Sign sign, std::optional<size_t> uid) -> size_t = 0;
86 virtual auto do_bd_theory(Sign sign, std::optional<size_t> uid) -> size_t = 0;
87};
88
90class OutputStm {
91 public:
95 using BdElem = std::pair<SymbolSpan, IndexSpan>;
97 using BdElemSpan = std::span<BdElem const>;
102 using HdElem = std::pair<SymbolSpan, std::span<std::tuple<Symbol, size_t, size_t> const>>;
104 using HdElemSpan = std::span<HdElem const>;
106 using Guard = std::pair<Relation, Symbol>;
108 using GuardSpan = std::span<Guard const>;
113 using CondLit = std::pair<std::optional<size_t>, size_t>;
115 using CondLitSpan = std::span<CondLit const>;
117 using DisjElem = std::tuple<Symbol, size_t, IndexSpan>;
119 using DisjElemSpan = std::span<DisjElem const>;
120
122 virtual ~OutputStm() = default;
123
127 auto uid(bool fact = false) -> size_t { return do_uid(fact); }
128
130 void fact(Symbol sym, size_t uid) { do_fact(sym, uid); }
131
133 void project_atom(size_t p_atom, size_t atom) { do_project_atom(p_atom, atom); }
134
136 auto body() -> OutputLit & { return do_body(); }
140 void rule(std::optional<std::tuple<Symbol, size_t, bool>> head) { do_rule(head); }
142 void external(Symbol atom, size_t uid, ExternalType type) { do_external(atom, uid, type); }
144 void project(Symbol atom, size_t uid) { do_project(atom, uid); }
146 auto aggr_rule(std::optional<size_t> uid) -> size_t { return do_aggr_rule(uid); }
148 auto theory_rule(std::optional<size_t> uid) -> size_t { return do_theory_rule(uid); }
150 auto disjunctive_rule(std::optional<size_t> uid) -> size_t { return do_disjunctive_rule(uid); }
154 void weak_constraint(Number const &weight, Number const *prio, SymbolSpan terms) {
155 do_weak_constraint(weight, prio, terms);
156 }
160 void heuristic(Symbol atom, size_t uid, Number const &weight, Number const *prio, HeuristicType type) {
161 do_heuristic(atom, uid, weight, prio, type);
162 }
166 void edge(Symbol src, Symbol dst) { do_edge(src, dst); }
167
169 void show_atom(Symbol atom, size_t uid) { do_show_atom(atom, uid); }
173 void show_term(Symbol term) { do_show_term(term); }
174
176 auto cond() -> OutputLit & { return do_cond(); }
178 auto cond_id() -> size_t { return do_cond_id(); }
179
181 void bd_aggr(size_t uid, AggregateFunction fun, BdElemSpan elems, GuardSpan guards) {
182 do_bd_aggr(uid, fun, elems, guards);
183 }
185 void hd_aggr(size_t uid, AggregateFunction fun, HdElemSpan elems, GuardSpan guards) {
186 do_hd_aggr(uid, fun, elems, guards);
187 }
189 void disjunction(size_t uid, DisjElemSpan elems) { do_disjunction(uid, elems); }
190
192 void cond_lit(size_t uid, CondLitSpan elems) { do_cond_lit(uid, elems); }
193
195 auto theory() -> OutputTheory & { return do_theory(); }
196
200 void flush() { do_flush(); }
202 void classical_negation(size_t atom_a, size_t atom_b) { do_classical_negation(atom_a, atom_b); }
204 void end_step() { do_end_step(); }
205
207 void mark(SymbolCollector &gc) { do_mark(gc); }
208
210 void simplify(std::function<TruthValue(prg_lit_t)> const &pred) { do_simplify(pred); }
211
212 private:
213 virtual auto do_uid(bool fact) -> size_t = 0;
214
215 virtual void do_fact(Symbol sym, size_t uid) = 0;
216
217 virtual void do_project_atom(size_t p_atom, size_t atom) = 0;
218
219 virtual auto do_body() -> OutputLit & = 0;
220 virtual void do_rule(std::optional<std::tuple<Symbol, size_t, bool>> head) = 0;
221 virtual void do_external(Symbol atom, size_t uid, ExternalType type) = 0;
222 virtual void do_project(Symbol atom, size_t uid) = 0;
223 virtual auto do_aggr_rule(std::optional<size_t> uid) -> size_t = 0;
224 virtual auto do_theory_rule(std::optional<size_t> uid) -> size_t = 0;
225 virtual auto do_disjunctive_rule(std::optional<size_t> uid) -> size_t = 0;
226 virtual void do_weak_constraint(Number const &weight, Number const *prio, SymbolSpan terms) = 0;
227 virtual void do_heuristic(Symbol atom, size_t uid, Number const &weight, Number const *prio,
228 HeuristicType type) = 0;
229 virtual void do_edge(Symbol src, Symbol dst) = 0;
230
231 virtual void do_show_atom(Symbol atom, size_t uid) = 0;
232 virtual void do_show_term(Symbol term) = 0;
233
234 virtual auto do_cond() -> OutputLit & = 0;
235 virtual auto do_cond_id() -> size_t = 0;
236
237 virtual void do_cond_lit(size_t uid, CondLitSpan elems) = 0;
238 virtual void do_bd_aggr(size_t uid, AggregateFunction fun, BdElemSpan elems, GuardSpan guards) = 0;
239 virtual void do_hd_aggr(size_t uid, AggregateFunction fun, HdElemSpan elems, GuardSpan guards) = 0;
240 virtual void do_disjunction(size_t uid, DisjElemSpan elems) = 0;
241
242 virtual auto do_theory() -> OutputTheory & = 0;
243
244 virtual void do_flush() = 0;
245 virtual void do_classical_negation(size_t atom_a, size_t atom_b) = 0;
246 virtual void do_end_step() = 0;
247
248 virtual void do_mark(SymbolCollector &gc) = 0;
249 virtual void do_simplify(std::function<TruthValue(prg_lit_t)> const &pred) = 0;
250};
251
253using UOutputStm = std::unique_ptr<OutputStm>;
254
256
257} // namespace CppClingo
An arbitrary precision integer.
Definition number.hh:27
Interface to output literals.
Definition output.hh:61
virtual ~OutputLit()=default
Destroy the output.
void lit(Sign sign, Symbol sym, size_t uid)
Output the given symbolic literal.
Definition output.hh:66
auto cond_lit(std::optional< size_t > uid) -> size_t
Output the given conditional literal.
Definition output.hh:72
auto bd_aggr(Sign sign, std::optional< size_t > uid) -> size_t
Delayed output of a body aggregate.
Definition output.hh:77
auto bd_theory(Sign sign, std::optional< size_t > uid) -> size_t
Delayed output of a theory atom.
Definition output.hh:79
void boolean(bool value)
Output the given boolean constant.
Definition output.hh:68
Interface to output statements.
Definition output.hh:90
void simplify(std::function< TruthValue(prg_lit_t)> const &pred)
Simplify stored state in the output.
Definition output.hh:210
auto uid(bool fact=false) -> size_t
Generate a new unique id for a literal.
Definition output.hh:127
auto theory_rule(std::optional< size_t > uid) -> size_t
Output a theory atom rule.
Definition output.hh:148
std::pair< SymbolSpan, std::span< std::tuple< Symbol, size_t, size_t > const > > HdElem
A head aggregate element.
Definition output.hh:102
void end_step()
End the current (incremental) grounding step.
Definition output.hh:204
void disjunction(size_t uid, DisjElemSpan elems)
Complete a delayed disjunction.
Definition output.hh:189
auto aggr_rule(std::optional< size_t > uid) -> size_t
Output a head aggregate rule.
Definition output.hh:146
std::pair< std::optional< size_t >, size_t > CondLit
A conditional literal.
Definition output.hh:113
void show_atom(Symbol atom, size_t uid)
Output the given atom.
Definition output.hh:169
void project(Symbol atom, size_t uid)
Output the given external.
Definition output.hh:144
void weak_constraint(Number const &weight, Number const *prio, SymbolSpan terms)
Output the given weak constraint.
Definition output.hh:154
std::span< DisjElem const > DisjElemSpan
A span of disjunction elements.
Definition output.hh:119
void edge(Symbol src, Symbol dst)
Output the given edge statement.
Definition output.hh:166
void bd_aggr(size_t uid, AggregateFunction fun, BdElemSpan elems, GuardSpan guards)
Complete a delayed body aggregate.
Definition output.hh:181
void external(Symbol atom, size_t uid, ExternalType type)
Output the given external.
Definition output.hh:142
virtual ~OutputStm()=default
Destroy the output.
std::pair< Relation, Symbol > Guard
A rhs guard.
Definition output.hh:106
auto cond_id() -> size_t
Commit a condition of simple literals returning its id.
Definition output.hh:178
std::span< HdElem const > HdElemSpan
A span of body aggregate elements.
Definition output.hh:104
auto cond() -> OutputLit &
Return an output for a condition.
Definition output.hh:176
void project_atom(size_t p_atom, size_t atom)
Adds a projection rule for the given atom.
Definition output.hh:133
std::pair< SymbolSpan, IndexSpan > BdElem
A body aggregate element.
Definition output.hh:95
void hd_aggr(size_t uid, AggregateFunction fun, HdElemSpan elems, GuardSpan guards)
Complete a delayed head aggregate.
Definition output.hh:185
void rule(std::optional< std::tuple< Symbol, size_t, bool > > head)
Output the given rule.
Definition output.hh:140
auto theory() -> OutputTheory &
Return a theory output.
Definition output.hh:195
auto body() -> OutputLit &
Get an output for body literals.
Definition output.hh:136
std::span< Guard const > GuardSpan
The guards of an aggregate.
Definition output.hh:108
auto disjunctive_rule(std::optional< size_t > uid) -> size_t
Output a disjunctive rule.
Definition output.hh:150
void classical_negation(size_t atom_a, size_t atom_b)
Handle classical negation of two atoms.
Definition output.hh:202
std::tuple< Symbol, size_t, IndexSpan > DisjElem
A disjunction element.
Definition output.hh:117
void cond_lit(size_t uid, CondLitSpan elems)
Complete a delayed conditional literal.
Definition output.hh:192
void fact(Symbol sym, size_t uid)
Output the given fact.
Definition output.hh:130
void heuristic(Symbol atom, size_t uid, Number const &weight, Number const *prio, HeuristicType type)
Output the given heuristic statement.
Definition output.hh:160
std::span< BdElem const > BdElemSpan
A span of body aggregate elements.
Definition output.hh:97
void flush()
Flush all delayed rule assuming they are completely defined.
Definition output.hh:200
void show_term(Symbol term)
Output the given term.
Definition output.hh:173
void mark(SymbolCollector &gc)
Mark owned symbols.
Definition output.hh:207
std::span< CondLit const > CondLitSpan
A span of conditional literals.
Definition output.hh:115
Interface to output literals.
Definition output.hh:20
auto str(String val) -> size_t
Output the given symbolic literal.
Definition output.hh:34
auto fun(String name, IndexSpan args) -> size_t
Output the given symbolic literal.
Definition output.hh:38
virtual ~OutputTheory()=default
Destroy the output.
std::optional< std::pair< String, size_t > > OptGuard
An optional guard of string and term indices.
Definition output.hh:30
auto num(Number const &num) -> size_t
Output the given symbolic literal.
Definition output.hh:36
void atom(AtomType type, size_t atom_uid, Symbol name, IndexSpan elems, OptGuard guard=std::nullopt)
Output the given atom.
Definition output.hh:44
auto elem(IndexSpan tuple, size_t cond) -> size_t
Output the given element.
Definition output.hh:42
AtomType
The type of a theory atom.
Definition output.hh:23
@ body
The atom occurs in the body.
@ head
The atom occurs in the head.
@ directive
The atom is a body.
auto tup(TheoryTermTupleType type, IndexSpan args) -> size_t
Output the given tuple.
Definition output.hh:40
auto sym(Symbol sym) -> size_t
Output the given symbol.
Definition output.hh:48
Reference to a string stored in a symbol store.
Definition symbol.hh:18
Helper class to mark owned symbols.
Definition symbol.hh:429
Variant-like class to store symbols stored in a symbol store.
Definition symbol.hh:225
int32_t prg_lit_t
A program literal.
Definition backend.hh:27
std::unique_ptr< OutputStm > UOutputStm
Unique pointer for statement output.
Definition output.hh:253
std::span< size_t const > IndexSpan
A span of indices.
Definition output.hh:15
std::vector< size_t > IndexVec
A vector of indices.
Definition output.hh:17
std::span< Symbol const > SymbolSpan
A span of symbols.
Definition symbol.hh:218
TheoryTermTupleType
Enumeration of theory term tuple types.
Definition core.hh:165
HeuristicType
Available heuristic types.
Definition core.hh:172
ExternalType
Available external types.
Definition core.hh:177
Sign
Enumeration of signs (default negation).
Definition core.hh:16
TruthValue
Truth values for expressions.
Definition core.hh:80
AggregateFunction
Enumeration of aggregate functions.
Definition core.hh:87