Clingo
Loading...
Searching...
No Matches
aggregate.hh
1#pragma once
2
3#include <clingo/input/literal.hh>
4
5#include <optional>
6#include <utility>
7
8namespace CppClingo::Input {
9
12
14using LGuard = std::optional<std::pair<Term, Relation>>;
16using RGuard = std::optional<std::pair<Relation, Term>>;
17
19inline auto reduct_is_nonmonotone(LGuard const &lhs, AggregateFunction fun, RGuard const &rhs) -> bool {
20 if (!lhs.has_value() && !rhs.has_value()) {
21 return false;
22 }
23 if (lhs.has_value() && lhs->second == Relation::not_equal) {
24 return true;
25 }
26 if (rhs.has_value() && rhs->first == Relation::not_equal) {
27 return true;
28 }
29 return fun == AggregateFunction::sum;
30}
31
33inline auto reduct_is_monotone(LGuard const &lhs, AggregateFunction fun, RGuard const &rhs) -> bool {
34 if (fun != AggregateFunction::sum) {
35 auto check = [](auto rel, bool less) {
36 return less ? (rel == Relation::less || rel == Relation::less_equal)
37 : (rel == Relation::greater || rel == Relation::greater_equal);
38 };
39 bool less = fun != AggregateFunction::min;
40 return (!lhs || check(lhs->second, less)) && (!rhs || check(rhs->first, !less));
41 }
42 return false;
43}
44
46class SetAggregateElement : public Expression<SetAggregateElement> {
47 public:
49 static constexpr auto attributes() {
50 return std::tuple{a_loc = &SetAggregateElement::loc_, a_lit = &SetAggregateElement::lit_,
51 a_cond = &SetAggregateElement::cond_};
52 }
53
56 : loc_{std::move(loc)}, lit_{std::move(lit)}, cond_{std::move(cond)} {}
58 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
60 [[nodiscard]] auto lit() const -> Lit const & { return lit_; }
62 [[nodiscard]] auto cond() const -> LitArray const & { return cond_; }
63
64 private:
65 Location loc_;
66 Lit lit_;
67 LitArray cond_;
68};
69
72
76template <bool HasSign>
77class SetAggregate : public std::conditional_t<HasSign, Signed, Unsigned>, public Expression<SetAggregate<HasSign>> {
78 public:
80 using Base = std::conditional_t<HasSign, Signed, Unsigned>;
81
83 static constexpr auto attributes() {
84 if constexpr (HasSign) {
85 return std::tuple{a_loc = &SetAggregate::loc_, a_sign = &Signed::sign, a_lhs = &SetAggregate::lhs_,
86 a_elems = &SetAggregate::elems_, a_rhs = &SetAggregate::rhs_};
87 } else {
88 return std::tuple{a_loc = &SetAggregate::loc_, a_lhs = &SetAggregate::lhs_, a_elems = &SetAggregate::elems_,
89 a_rhs = &SetAggregate::rhs_};
90 }
91 }
92
95 : loc_{std::move(loc)}, elems_{std::move(elems)}, lhs_(std::move(lhs)), rhs_(std::move(rhs)) {
96 static_assert(!HasSign);
97 }
98
101 : Signed{sign}, loc_{std::move(loc)}, elems_{std::move(elems)}, lhs_(std::move(lhs)), rhs_(std::move(rhs)) {
102 static_assert(HasSign);
103 }
104
106 [[nodiscard]] auto loc() const -> Location const & { return loc_; }
108 [[nodiscard]] auto elems() const -> SetAggregateElementArray const & { return elems_; }
110 [[nodiscard]] auto lhs() const -> LGuard const & { return lhs_; }
112 [[nodiscard]] auto rhs() const -> RGuard const & { return rhs_; }
113
114 private:
115 Location loc_;
117 LGuard lhs_;
118 RGuard rhs_;
119};
120
123
126
128
129} // namespace CppClingo::Input
A record that friend declares and defines comparison operators.
Definition attributes.hh:60
An element of a set aggregate.
Definition aggregate.hh:46
static constexpr auto attributes()
The record attributes.
Definition aggregate.hh:49
auto lit() const -> Lit const &
The literal.
Definition aggregate.hh:60
auto loc() const -> Location const &
The location of the element.
Definition aggregate.hh:58
SetAggregateElement(Location loc, Lit lit, LitArray cond)
Construct a set aggregate element.
Definition aggregate.hh:55
auto cond() const -> LitArray const &
The condition.
Definition aggregate.hh:62
A set aggregate.
Definition aggregate.hh:77
SetAggregate(Location loc, LGuard lhs, SetAggregateElementArray elems, RGuard rhs)
Construct a set aggregate.
Definition aggregate.hh:94
auto loc() const -> Location const &
The location of the aggregate.
Definition aggregate.hh:106
auto elems() const -> SetAggregateElementArray const &
The elements of the set aggregate.
Definition aggregate.hh:108
auto rhs() const -> RGuard const &
The optional left guard of the aggregate.
Definition aggregate.hh:112
static constexpr auto attributes()
The record attributes.
Definition aggregate.hh:83
auto lhs() const -> LGuard const &
The optional right guard of the aggregate.
Definition aggregate.hh:110
std::conditional_t< HasSign, Signed, Unsigned > Base
The base class.
Definition aggregate.hh:80
SetAggregate(Location loc, Sign sign, LGuard lhs, SetAggregateElementArray elems, RGuard rhs)
Construct a set aggregate.
Definition aggregate.hh:100
Simple class with a sign.
Definition literal.hh:15
auto sign() const -> Sign
The sign of the class.
Definition literal.hh:20
The Location of an expression in an input source.
Definition location.hh:44
Sign
Enumeration of signs (default negation).
Definition core.hh:16
AggregateFunction
Enumeration of aggregate functions.
Definition core.hh:87
@ greater_equal
The greater than or equal to symbol (>=).
@ less_equal
The less than or equal to symbol (<=).
@ greater
The greater than symbol (>).
@ not_equal
The not equal to symbol (!=).
@ less
The less than symbol (<).
@ sum
The #count function.
@ min
The #sum+ function.
std::optional< std::pair< Term, Relation > > LGuard
Definition aggregate.hh:14
auto reduct_is_nonmonotone(LGuard const &lhs, AggregateFunction fun, RGuard const &rhs) -> bool
Check whether the given aggregate function/guard combination can result in a nonmonotone aggregate.
Definition aggregate.hh:19
auto reduct_is_monotone(LGuard const &lhs, AggregateFunction fun, RGuard const &rhs) -> bool
Check whether the given aggregate function/guard combination is monotone.
Definition aggregate.hh:33
std::optional< std::pair< Relation, Term > > RGuard
An optional right guard of an aggregate.
Definition aggregate.hh:16
constexpr auto a_sign
Definition attributes.hh:37
constexpr auto a_elems
Definition attributes.hh:23
constexpr auto a_cond
Definition attributes.hh:20
constexpr auto a_lhs
Definition attributes.hh:27
constexpr auto a_lit
Definition attributes.hh:28
constexpr auto a_rhs
Definition attributes.hh:36
constexpr auto a_loc
Definition attributes.hh:29
std::variant< LitBool, LitComparison, LitSymbolic > Lit
Variant holding the different literal types.
Definition literal.hh:129