Clingo
Loading...
Searching...
No Matches
literal.hh
1#pragma once
2
3#include <clingo/control/context.hh>
4#include <clingo/control/term.hh>
5
6#include <clingo/input/literal.hh>
7#include <clingo/input/print.hh>
8
9#include <clingo/ground/literal.hh>
10
11#include <clingo/input/rewrite/analyze.hh>
12
13namespace CppClingo::Control {
14
17
18namespace Detail {
19
23template <class F, bool stratify = false> class BuilderLit {
24 public:
26 BuilderLit(BuildContext &ctx, F cb) : cb_{std::move(cb)}, ctx_{&ctx} {}
30 void operator()(Input::LitBool const &lit) const { cb_(std::make_unique<Ground::LitBool>(lit.value())); }
36 void operator()(Input::LitComparison const &lit) const {
37 if (Input::is_interval(lit.rhs().front().second)) {
38 auto lhs = build_term(ctx_->var_map(), lit.lhs());
39 auto const &rng = std::get<Input::TermBinary>(lit.rhs().front().second);
40 auto lower = build_term(ctx_->var_map(), *rng.lhs());
41 auto upper = build_term(ctx_->var_map(), *rng.rhs());
42 cb_(std::make_unique<Ground::LitInterval>(std::move(lhs), std::move(lower), std::move(upper)));
43 } else if (Input::is_external(lit.rhs().front().second)) {
44 if (ctx_->context() == nullptr) {
45 CLINGO_REPORT_LOC(ctx_->logger(), error, lit.loc()) << "script context unavailable";
46 throw std::runtime_error("script context unavailable");
47 }
48 auto lhs = build_term(ctx_->var_map(), lit.lhs());
49 auto const &rng = std::get<Input::TermFunction>(lit.rhs().front().second);
50 auto args = CppClingo::Util::to_vec(rng.pool().front().elems(), [this](auto const &elem) {
51 return build_term(ctx_->var_map(), std::get<Input::Term>(elem));
52 });
53 cb_(std::make_unique<Ground::LitExternal>(*ctx_->context(), lit.loc(), rng.name(), std::move(lhs),
54 std::move(args)));
55 } else {
56 auto add_cmp = [this](auto const &lhs, auto rel, auto const &rhs) {
57 auto l = build_term(ctx_->var_map(), lhs);
58 auto r = build_term(ctx_->var_map(), rhs);
59 cb_(std::make_unique<Ground::LitComparison>(std::move(l), rel, std::move(r)));
60 };
61 auto const &lhs = lit.lhs();
62 auto const &rhs = lit.rhs().front().second;
63 auto rel = lit.rhs().front().first;
64 add_cmp(lhs, rel, rhs);
65 if (rel == Relation::equal && Input::is_matchable(rhs) && !Input::is_symbol(rhs)) {
66 add_cmp(rhs, rel, lhs);
67 }
68 }
69 }
71 void operator()(Input::LitSymbolic const &lit) const {
72 auto has_projection = false;
73 auto term = build_term(ctx_->var_map(), lit.term(), has_projection);
74 auto idx = stratify && lit.sign() == Sign::none ? Ground::stratified_index : ctx_->index(lit);
75 // NOLINTNEXTLINE(bugprone-unchecked-optional-access)
76 auto &base = ctx_->add_base(Input::signature(lit.term()).value());
77 if (has_projection) {
78 auto [p_term, state] = ctx_->add_project(term, base);
79 cb_(std::make_unique<Ground::LitProject>(*state, lit.sign(), std::move(term), std::move(p_term), idx,
80 ctx_->gcomp().domain()));
81 } else {
82 cb_(std::make_unique<Ground::LitSymbolic>(base, lit.sign(), std::move(term), idx, ctx_->gcomp().domain()));
83 }
84 }
85
86 private:
87 F cb_;
88 BuildContext *ctx_;
89};
90
91} // namespace Detail
92
96template <class F> void build_lit(BuildContext &ctx, Input::Lit const &lit, F &&fun) {
97 std::visit(Detail::BuilderLit{ctx, std::forward<F>(fun)}, lit);
98}
99
103template <class F> void build_stratified_lit(BuildContext &ctx, Input::Lit const &lit, F &&fun) {
104 std::visit(Detail::BuilderLit<std::decay_t<F>, true>{ctx, std::forward<F>(fun)}, lit);
105}
106
108
109} // namespace CppClingo::Control
Context object holding necessary data for translating from input to ground representation.
Definition context.hh:39
void build_stratified_lit(BuildContext &ctx, Input::Lit const &lit, F &&fun)
Translate input literals to their ground representation.
Definition literal.hh:103
auto build_term(Util::unordered_map< String, size_t > const &var_map, Input::Term const &term, bool &has_projection) -> Ground::UTerm
Translates input theory terms to their ground representation.
void build_lit(BuildContext &ctx, Input::Lit const &lit, F &&fun)
Translate input literals to their ground representation.
Definition literal.hh:96
#define CLINGO_REPORT_LOC(p, id, loc)
Report messages of the given type and location.
Definition logger.hh:223
@ equal
The equal to symbol (=).
@ none
No sign.
@ lit
Conditional literals derived during grounding.
constexpr auto stratified_index
Marker for stratified literals.
Definition literal.hh:189
auto is_interval(Term const &term) -> bool
Returns true if the term has form t..u.
auto is_symbol(Term const &term) -> bool
Check if the term is a symbol.
Definition analyze.hh:98
auto signature(Term const &term) -> std::optional< std::tuple< String, size_t, bool > >
Get the signature of a term.
auto is_external(Term const &term) -> bool
Returns true if the term has form @f(...).
auto is_matchable(Term const &term) -> bool
Check if a term is matchable.
std::variant< LitBool, LitComparison, LitSymbolic > Lit
Variant holding the different literal types.
Definition literal.hh:129
auto to_vec(It begin, Sent end, Pred &&pred, Args &&...args)
Use a transformation function to build a vector from a range.
Definition algorithm.hh:48