Clingo
Loading...
Searching...
No Matches
rewrite_context.hh
1#pragma once
2
3#include <clingo/input/program.hh>
4
5#include <clingo/util/ordered_set.hh>
6#include <clingo/util/unordered_map.hh>
7
8namespace CppClingo::Input {
9
12
14enum class Arity : uint8_t {
15 unary = 0,
16 binary = 1,
17};
18
20enum class Associativity : uint8_t {
21 left = 0,
22 right = 1,
23 non_assoc = 2,
24};
25
28 public:
30 void add(Logger &log, TheoryOpDefinition const &def);
31
33 void check_operator(Logger &log, String op, Arity arity, Location const &loc) const;
34
36 auto parse(Logger &log, TheoryTermUnparsed const &term) const -> TheoryTerm;
37
39 [[nodiscard]] auto has_error() const { return has_error_; }
40
41 private:
42 using Table = Util::unordered_map<std::pair<SharedString, Arity>, std::pair<int, Associativity>>;
43 using Stack = std::vector<std::pair<SharedString, Arity>>;
44 using Terms = std::vector<TheoryTerm>;
45
47 auto priority_and_associativity_(String op) const -> std::pair<int, Associativity>;
48
50 auto priority_(String op, Arity arity) const -> int;
51
55 auto check_(String op) const -> bool;
56
58 void reduce_() const;
59
60 Table table_;
61 mutable Terms terms_;
62 mutable Stack stack_;
63 mutable bool has_error_ = false;
64};
65
68 public:
70 void add_theory(Logger &log, StmTheory const &stm);
71
73 template <bool has_sign>
74 auto parse(Logger &log, TheoryAtom<has_sign> const &atom, bool fact) const -> std::optional<TheoryAtom<has_sign>>;
75
77 [[nodiscard]] auto has_error() const -> bool;
78
79 private:
80 using ParserIndex = size_t;
81 using GuardTable = std::pair<SharedStringSet, ParserIndex>;
82 using AtomTable = Util::unordered_map<std::pair<SharedString, size_t>,
83 std::tuple<TheoryAtomType, ParserIndex, std::optional<GuardTable>>>;
84
85 std::vector<TheoryTermParser> term_parsers_;
86 AtomTable atom_table_;
87 mutable bool has_error_ = false;
88};
89
91using AuxTermVec = std::vector<std::pair<Term, Term>>;
92
94using ParamMap = Util::ordered_set<SharedString>;
95
98 public:
100 struct pop_ {
102 void operator()(RewriteContext *ctx) const {
103 if (ctx != nullptr) {
104 ctx->pop();
105 ctx = nullptr;
106 }
107 }
108 };
110 using Guard = std::unique_ptr<RewriteContext, pop_>;
112 RewriteContext(Logger &log, SymbolStore &store, RewriteOptions const &opts, TheoryAtomParser const &parser,
113 ParamMap const &param_map, ConstMap const &const_map)
114 : log_{log}, opts_{opts}, parser_{parser}, const_map_{const_map}, param_map_{param_map}, gen_{store, {}, "_A"} {
115 }
117 RewriteContext(RewriteContext &&) noexcept = delete;
119 void init(StringSet names, char const *prefix) {
120 aux_.clear();
121 gen_.init(std::move(names), prefix);
122 }
124 [[nodiscard]] auto logger() const -> Logger & { return log_; }
126 [[nodiscard]] auto options() const -> RewriteOptions const & { return opts_; }
128 [[nodiscard]] auto parser() const -> TheoryAtomParser const & { return parser_; }
130 [[nodiscard]] auto store() const -> SymbolStore & { return gen_.store(); }
132 [[nodiscard]] auto gen() -> NameGen & { return gen_; }
136 [[nodiscard]] auto is_param(String name) const -> std::optional<size_t> {
137 if (auto it = param_map_.find(name); it != param_map_.end()) {
138 return std::distance(param_map_.begin(), it);
139 }
140 return std::nullopt;
141 }
145 [[nodiscard]] auto is_const(String name) const -> std::optional<Symbol> {
146 if (auto it = const_map_.find(name); it != const_map_.end()) {
147 assert(!is_param(name));
148 return *it->second.second;
149 }
150 return std::nullopt;
151 }
153 [[nodiscard]] auto has_params() const -> bool { return !const_map_.empty() || !param_map_.empty(); }
155 [[nodiscard]] auto aux() -> AuxTermVec & {
156 assert(!aux_.empty());
157 return aux_.back();
158 }
160 void pop() {
161 assert(!aux_.empty());
162 aux_.pop_back();
163 }
165 [[nodiscard]] auto push() -> Guard {
166 aux_.emplace_back();
167 return Guard{this};
168 }
169
174 void set_error() { has_error_ = true; }
175
177 [[nodiscard]] auto has_error() const -> bool { return has_error_; }
178
179 private:
180 Logger &log_;
181 RewriteOptions const &opts_;
182 TheoryAtomParser const &parser_;
183 ConstMap const &const_map_;
184 ParamMap const &param_map_;
185 NameGen gen_;
186 std::vector<AuxTermVec> aux_;
187 bool has_error_ = false;
188};
189
191
192} // namespace CppClingo::Input
Helper to pass arguments to rewrite functions.
Definition rewrite_context.hh:97
auto store() const -> SymbolStore &
Get the symbol store.
Definition rewrite_context.hh:130
auto has_params() const -> bool
Check if there is at least one parameter (from a program or const statement).
Definition rewrite_context.hh:153
auto gen() -> NameGen &
Get the name generator.
Definition rewrite_context.hh:132
std::unique_ptr< RewriteContext, pop_ > Guard
Helper to pop auxiliary variable assignments.
Definition rewrite_context.hh:110
auto options() const -> RewriteOptions const &
Get the logger.
Definition rewrite_context.hh:126
void pop()
Pop the last variable term map pushed.
Definition rewrite_context.hh:160
void set_error()
Mark that there was an error during rewriting.
Definition rewrite_context.hh:174
auto has_error() const -> bool
Check if there has been an error during rewriting.
Definition rewrite_context.hh:177
auto parser() const -> TheoryAtomParser const &
Get the parser.
Definition rewrite_context.hh:128
auto logger() const -> Logger &
Get the logger.
Definition rewrite_context.hh:124
auto aux() -> AuxTermVec &
Get the variable term map.
Definition rewrite_context.hh:155
auto push() -> Guard
Push a fresh variable term map.
Definition rewrite_context.hh:165
auto is_const(String name) const -> std::optional< Symbol >
Check if the given identifier is a parameter defined by a constant.
Definition rewrite_context.hh:145
auto is_param(String name) const -> std::optional< size_t >
Check if the given identifier is a parameter defined by a program directive.
Definition rewrite_context.hh:136
RewriteContext(Logger &log, SymbolStore &store, RewriteOptions const &opts, TheoryAtomParser const &parser, ParamMap const &param_map, ConstMap const &const_map)
Construct a rewrite context.
Definition rewrite_context.hh:112
RewriteContext(RewriteContext &&) noexcept=delete
Delete move/copy constructor.
A theory definition.
Definition statement.hh:193
A parser for theory atoms.
Definition rewrite_context.hh:67
auto has_error() const -> bool
Check if there was a parse error.
void add_theory(Logger &log, StmTheory const &stm)
Add a theory statement to the theory atom parser.
auto parse(Logger &log, TheoryAtom< has_sign > const &atom, bool fact) const -> std::optional< TheoryAtom< has_sign > >
Parse the given theory atom.
A theory atom.
Definition theory.hh:239
A theory operator definition.
Definition statement.hh:52
A parser for theory terms.
Definition rewrite_context.hh:27
auto has_error() const
Check if there was a parse error.
Definition rewrite_context.hh:39
void add(Logger &log, TheoryOpDefinition const &def)
Add a theory operator definition to the term parser.
void check_operator(Logger &log, String op, Arity arity, Location const &loc) const
Check if the given operator is in the parse table raising a runtime error if absent.
auto parse(Logger &log, TheoryTermUnparsed const &term) const -> TheoryTerm
Parses the given unparsed term, replacing it by nested theory functions.
An unparsed theory term.
Definition theory.hh:145
The Location of an expression in an input source.
Definition location.hh:44
Simple logger to report message to stderr or via a callback.
Definition logger.hh:63
Generator for auxiliary names.
Definition symbol.hh:585
Class managing the lifetime of a String.
Definition symbol.hh:93
Reference to a string stored in a symbol store.
Definition symbol.hh:18
A store for symbols.
Definition symbol.hh:454
Util::unordered_set< String > StringSet
A set of strings.
Definition symbol.hh:83
Util::unordered_set< SharedString > SharedStringSet
A set of strings.
Definition symbol.hh:181
@ atom
Check if term is an atom.
Util::ordered_map< SharedString, std::pair< StmConst, SharedSymbol > > ConstMap
Map from identifiers to constants.
Definition program.hh:34
Util::ordered_set< SharedString > ParamMap
Map from identifiers to constants.
Definition rewrite_context.hh:94
Arity
Enum for unary/binary operator arities.
Definition rewrite_context.hh:14
Associativity
Enum for operator associativities.
Definition rewrite_context.hh:20
std::vector< std::pair< Term, Term > > AuxTermVec
A vector of term pairs where the second has been substituted by the first in some other term.
Definition rewrite_context.hh:91
TheoryAtomType
Enumeration of theory atom types.
Definition statement.hh:117
@ unary
An unary theory operator.
std::variant< TermVariable, TermSymbol, TermTuple, TermFunction, TermAbs, TermUnary, TermBinary > Term
Variant holding the different term types.
Definition term.hh:45
std::variant< TheoryTermSymbol, TheoryTermVariable, TheoryTermTuple, TheoryTermFunction, TheoryTermUnparsed > TheoryTerm
A variant for the different theory terms.
Definition theory.hh:22
tsl::hopscotch_map< Key, T, Hash, KeyEqual, Allocator, NeighborhoodSize, StoreHash, GrowthPolicy > unordered_map
Alias for unordered maps.
Definition unordered_map.hh:17
Helper to pop auxiliary variable assignments.
Definition rewrite_context.hh:100
void operator()(RewriteContext *ctx) const
Pop the last variable term map pushed.
Definition rewrite_context.hh:102
Options to configure rewriting.
Definition program.hh:26