Clingo
Loading...
Searching...
No Matches
analyze.hh
1#pragma once
2
3#include <clingo/input/statement.hh>
4
5#include <clingo/core/logger.hh>
6
7#include <cstring>
8
9namespace CppClingo::Input {
10
13
15enum class TermCheckType : uint8_t {
16 atom,
17 sig,
21};
22
28 bool has_sign = false;
30 Number const *pos_number = nullptr;
33};
34
36[[nodiscard]] auto check_type(Term const &term, TermCheckType type, CheckTypeResult *res = nullptr) -> bool;
37
40 public:
42 [[nodiscard]] auto m() const -> Number const & { return std::get<TermSymbol>(term_m()).value().num(); }
44 [[nodiscard]] auto n() const -> Number const & { return std::get<TermSymbol>(term_n()).value().num(); }
46 [[nodiscard]] auto x() const -> String { return std::get<TermVariable>(term_x()).name(); }
48 [[nodiscard]] auto term_m() const -> Term const & { return *std::get<TermBinary>(term_mx()).lhs(); }
50 [[nodiscard]] auto term_n() const -> Term const & { return mxn_->rhs(); }
52 [[nodiscard]] auto term_x() const -> Term const & { return *std::get<TermBinary>(term_mx()).rhs(); }
54 [[nodiscard]] auto term_mx() const -> Term const & { return mxn_->lhs(); }
56 [[nodiscard]] auto term_mxn() const -> TermBinary const & { return *mxn_; }
57
58 friend auto check_linear(TermBinary const &term) -> std::optional<LinearTerm>;
59
60 private:
61 LinearTerm(TermBinary const &mxn) : mxn_{&mxn} {}
62
63 TermBinary const *mxn_;
64};
65
67[[nodiscard]] auto is_linear(Term const &term) -> bool;
68
70[[nodiscard]] auto is_linear(TermBinary const &term) -> bool;
71
73auto check_linear(TermBinary const &term) -> std::optional<LinearTerm>;
74
76auto check_linear(Term const &term) -> std::optional<LinearTerm>;
77
79[[nodiscard]] auto is_interval(Term const &term) -> bool;
80
82[[nodiscard]] auto is_external(Term const &term) -> bool;
83
85[[nodiscard]] auto is_interval(TermBinary const &term) -> bool;
86
90[[nodiscard]] auto always_numeric(Term const &term) -> bool;
91
95[[nodiscard]] auto never_numeric(Term const &term) -> bool;
96
98[[nodiscard]] inline auto is_symbol(Term const &term) -> bool {
99 return std::holds_alternative<TermSymbol>(term);
100}
101
103[[nodiscard]] inline auto is_variable(Term const &term) -> bool {
104 return std::holds_alternative<TermVariable>(term);
105}
106
108[[nodiscard]] inline auto is_fixed(Lit const &lit) -> std::optional<bool> {
109 if (auto const *blit = std::get_if<LitBool>(&lit); blit != nullptr) {
110 return blit->value() == (blit->sign() != Sign::once);
111 }
112 return std::nullopt;
113}
114
116[[nodiscard]] auto is_atom(Term const &term) -> bool;
117
121[[nodiscard]] auto is_atom(Lit const &lit) -> bool;
122
126[[nodiscard]] auto is_atom(HdLit const &lit) -> bool;
127
131[[nodiscard]] auto is_atom(BdLit const &lit) -> bool;
132
134[[nodiscard]] inline auto is_boolean(Lit const &lit) -> std::optional<bool> {
135 if (auto const *lb = std::get_if<LitBool>(&lit); lb != nullptr) {
136 return lb->value() == (lb->sign() != Sign::once);
137 }
138 return std::nullopt;
139}
140
146[[nodiscard]] auto is_test(BdLit const &lit) -> bool;
147
154[[nodiscard]] auto is_classical(HdLit const &lit) -> bool;
155
160[[nodiscard]] auto is_fact(SymbolStore &store, StmRule const &rule) -> std::optional<Symbol>;
161
166[[nodiscard]] auto is_fact(SymbolStore &store, Stm const &stm) -> std::optional<Symbol>;
167
169[[nodiscard]] auto check_global(Logger &log, VariableSet const &global, Stm const &stm) -> bool;
170
172[[nodiscard]] inline auto is_theory_operator(std::string_view name) -> bool {
173 return (!name.empty() && std::strchr("/!<=>+-*\\?&@|:;~^.", name.front()) != nullptr) || (name == "not");
174}
175
177[[nodiscard]] auto signature(Term const &term) -> std::optional<std::tuple<String, size_t, bool>>;
178
185[[nodiscard]] auto is_matchable(Term const &term) -> bool;
186
188void extract_can_fail(Term const &term, std::vector<Term> &result);
189
191
192} // namespace CppClingo::Input
Helper to access elements of linear terms.
Definition analyze.hh:39
auto term_mx() const -> Term const &
The term for the coefficient times the variable.
Definition analyze.hh:54
auto term_n() const -> Term const &
The term for the constant.
Definition analyze.hh:50
auto m() const -> Number const &
The coefficient.
Definition analyze.hh:42
auto term_x() const -> Term const &
The term for the variable.
Definition analyze.hh:52
auto x() const -> String
The variable name.
Definition analyze.hh:46
friend auto check_linear(TermBinary const &term) -> std::optional< LinearTerm >
Check if the given term is a linear term and return an object to access its elements.
auto n() const -> Number const &
The constant.
Definition analyze.hh:44
auto term_m() const -> Term const &
The term for the coefficient.
Definition analyze.hh:48
auto term_mxn() const -> TermBinary const &
The term for the whole linear term.
Definition analyze.hh:56
A rule.
Definition statement.hh:16
Term representing a binary operation.
Definition term.hh:274
auto rhs() const -> Util::immutable_value< Term > const &
The right-hand-side.
Definition term.hh:291
auto lhs() const -> Util::immutable_value< Term > const &
The left-hand-side.
Definition term.hh:289
Simple logger to report message to stderr or via a callback.
Definition logger.hh:63
An arbitrary precision integer.
Definition number.hh:27
Reference to a string stored in a symbol store.
Definition symbol.hh:18
A store for symbols.
Definition symbol.hh:454
@ once
One sign (not).
auto is_interval(Term const &term) -> bool
Returns true if the term has form t..u.
TermCheckType
Enumeration for Term::check_type().
Definition analyze.hh:15
auto is_theory_operator(std::string_view name) -> bool
Check if the given string is a theory operator.
Definition analyze.hh:172
auto is_test(BdLit const &lit) -> bool
Check if the literal is a test.
auto check_global(Logger &log, VariableSet const &global, Stm const &stm) -> bool
Check that none of the given variables are local in the statement.
auto check_type(Term const &term, TermCheckType type, CheckTypeResult *res=nullptr) -> bool
Query information about the structure of the given term.
auto is_atom(Term const &term) -> bool
Check whether the term can represent an atom.
auto is_variable(Term const &term) -> bool
Check if the term is a variable.
Definition analyze.hh:103
auto is_boolean(Lit const &lit) -> std::optional< bool >
Check if a literal is a boolean constant.
Definition analyze.hh:134
auto check_linear(TermBinary const &term) -> std::optional< LinearTerm >
Check if the given term is a linear term and return an object to access its elements.
auto is_fixed(Lit const &lit) -> std::optional< bool >
Get the truth value of a literal, in case it is a Boolean constant.
Definition analyze.hh:108
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_linear(Term const &term) -> bool
Check if the given term is a linear term.
auto never_numeric(Term const &term) -> bool
Check if the term never evaluates to a number.
auto is_fact(SymbolStore &store, StmRule const &rule) -> std::optional< Symbol >
Check if a rule is a fact.
auto is_external(Term const &term) -> bool
Returns true if the term has form @f(...).
auto is_classical(HdLit const &lit) -> bool
Check if the literal is classical.
auto always_numeric(Term const &term) -> bool
Check if the term always evaluates to a number.
auto is_matchable(Term const &term) -> bool
Check if a term is matchable.
void extract_can_fail(Term const &term, std::vector< Term > &result)
Extract terms whose evaluation can fail.
@ sig
Check if term is a signature.
@ atom
Check if term is an atom.
@ pos_number
Check if term is a positive number.
@ signed_identifier
Check if term is a signed identifier.
@ identifier
Check if term is an identifier.
std::variant< BdLitSimple, BdLitConjunction, BdLitAggregate, BdLitSetAggregate, BdLitTheoryAtom > BdLit
A body literal.
Definition body_literal.hh:116
std::variant< HdLitSimple, HdLitDisjunction, HdLitAggregate, HdLitSetAggregate, HdLitTheoryAtom > HdLit
A head literal.
Definition head_literal.hh:130
std::variant< LitBool, LitComparison, LitSymbolic > Lit
Variant holding the different literal types.
Definition literal.hh:129
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::variant< TermVariable, TermSymbol, TermTuple, TermFunction, TermAbs, TermUnary, TermBinary > Term
Variant holding the different term types.
Definition term.hh:45
StringSet VariableSet
A set of variable names.
Definition term.hh:23
@ global
Visit variables occurring in global scope.
Extract additional information while checking the type of a term.
Definition analyze.hh:26
String identifier
The identifier represented by the term.
Definition analyze.hh:32
Number const * pos_number
The number represented by the term.
Definition analyze.hh:30
bool has_sign
Whether the term is signed.
Definition analyze.hh:28