forked from Z3Prover/z3
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcounter_automaton.h
351 lines (291 loc) · 14.5 KB
/
counter_automaton.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#ifndef Z3_STR_COUNTER_AUT_H_
#define Z3_STR_COUNTER_AUT_H_
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <memory>
#include <concepts>
#include <compare>
#include <mata/nfa/nfa.hh>
#include <mata/nfa/strings.hh>
#include <mata/nfa/builder.hh>
#include "formula.h"
#include "util.h"
/**
* Exapands to two if statements terminating the function containing the macro early.
*
* Insert two ifs that make the code containing this macro return true if (cmp_expr < 0) and false if (cmp_expr > 0)
* @Note: when cmp_expr == 0, the control flow goes/falls through the inserted branches.
*/
#define INSERT_LEXICOGRAPHIC_BRANCH_ON_CMP_RESULT(cmp_expr) \
{ \
if ((cmp_expr) < 0) return true; \
else if ((cmp_expr) > 0) return false; \
}
namespace smt::noodler::ca {
/**
* @brief Structured alphabet. Performs mapping between mata symbols
* and particular symbols T.
*
* @tparam T Symbol type
*/
template <typename T>
requires std::strict_weak_order<std::less<T>, T const&, T const&>
class StructAlphabet {
private:
std::map<T, mata::Symbol> alph_symb_mata {};
std::map<mata::Symbol, T> alph_mata_symb {};
public:
StructAlphabet() : alph_symb_mata(), alph_mata_symb() { }
mata::Symbol add_symbol(const T& symb) {
// Speculate: if the given @p symb is not in the map, then this will be its handle
mata::Symbol new_symbol = this->alph_symb_mata.size();
// Use .emplace to avoid doing another lookup to store the value if is not present in the map
const auto& [map_bucket, did_emplace_insert_new] = this->alph_symb_mata.emplace(symb, new_symbol);
if (did_emplace_insert_new) {
// Create the reverse mapping
this->alph_mata_symb[new_symbol] = symb;
}
return map_bucket->second; // The bucket's value (.second) is correct regardless of previous existence
}
void insert(const mata::Symbol mata_symb, const T& symb) {
this->alph_symb_mata[symb] = mata_symb;
this->alph_mata_symb[mata_symb] = symb;
}
bool has_mata_symbol_for(const T& symbol) const {
return (this->alph_symb_mata.find(symbol) != this->alph_symb_mata.end());
}
mata::Symbol get_mata_symbol(const T& symb) const {
return this->alph_symb_mata.at(symb);
}
const T& get_symbol(const mata::Symbol symb) const {
return this->alph_mata_symb.at(symb);
}
std::set<T> get_all_symbols() const {
std::set<T> ret;
for (const auto& [key,val] : this->alph_symb_mata) {
ret.insert(key);
}
return ret;
}
};
/**
* @brief Symbols of the form <mark, var, label, symbol>
* <L,var> counts the length of a string assignment of variable var
* <P,var,i> counts the i-th mismatch in variable var (i=label)
* <R,x,i,symb> captures i-th mismatch symbol (i=label)
*/
struct AtomicSymbol {
enum class TagType : uint8_t {
LENGTH = 0, // <L, x>
MISMATCH_POS = 1, // <P, var, mismatch_idx>
REGISTER_STORE = 2, // <R, disequation, side, mismatch_idx, alphabet_symbol>
COPY_PREVIOUS = 3, // <C, var, disequation, side, mismatch_idx>
};
enum class PredicateSide : uint8_t {
LEFT = 0,
RIGHT = 1,
};
TagType type;
BasicTerm var; // variable from string constraint
int predicate_idx; // Negative values mean that we only deal with one predicate
PredicateSide predicate_side;
size_t copy_idx; // What level of the tag automaton is this tag located
mata::Symbol symbol; // Symbol stored during sampling transition (tag with type REGISTER_STORE)
// Lexicographical order on (type, copy_idx, disequation_idx, predicate_side, symbol, var)
bool operator<(const AtomicSymbol& other_symbol) const {
INSERT_LEXICOGRAPHIC_BRANCH_ON_CMP_RESULT(type <=> other_symbol.type);
INSERT_LEXICOGRAPHIC_BRANCH_ON_CMP_RESULT(copy_idx <=> other_symbol.copy_idx);
INSERT_LEXICOGRAPHIC_BRANCH_ON_CMP_RESULT(predicate_idx <=> other_symbol.predicate_idx);
INSERT_LEXICOGRAPHIC_BRANCH_ON_CMP_RESULT(predicate_side <=> other_symbol.predicate_side);
INSERT_LEXICOGRAPHIC_BRANCH_ON_CMP_RESULT(symbol <=> other_symbol.symbol);
return var < other_symbol.var;
}
bool operator==(const AtomicSymbol&) const = default;
bool is_mutating_registers() const {
return (this->type == TagType::REGISTER_STORE || this->type == TagType::COPY_PREVIOUS);
}
std::string to_string() const {
std::string var_escape = var.is_literal() ? "\\\"" + var.get_name().encode() + "\\\"" : var.to_string();
switch (this->type) {
case TagType::LENGTH: {
return "<L, " + var_escape + ">";
}
case TagType::MISMATCH_POS: { // <P, var, copy_idx>
return "<P, " + var_escape + ", " + std::to_string(this->copy_idx) + ">";
}
case TagType::REGISTER_STORE: { // <R, var, disequation, side, mismatch_idx, alphabet_symbol>
std::stringstream string_builder;
string_builder << "<R, "
<< var_escape + ", ";
if (this->predicate_idx >= 0) {
string_builder << this->predicate_idx << "-"
<< (this->predicate_side == PredicateSide::LEFT ? "L" : "R")
<< ", ";
}
string_builder << copy_idx << ", " << this->symbol << ">";
return string_builder.str();
}
case TagType::COPY_PREVIOUS: { // <C, var, disequation, side, mismatch_idx>
std::stringstream string_builder;
string_builder << "<C, " << var_escape << ", "
<< "Pred" << this->predicate_idx << "-"
<< (this->predicate_side == PredicateSide::LEFT ? "L" : "R")
<< ", " << this->copy_idx << ">";
return string_builder.str();
}
default: {
assert(false);
return "??";
}
}
}
static AtomicSymbol create_l_symbol(const BasicTerm& var) {
return AtomicSymbol{TagType::LENGTH, var, 0, PredicateSide::LEFT, 0, 0};
}
static AtomicSymbol create_p_symbol(const BasicTerm& var, size_t copy_idx) {
return AtomicSymbol{TagType::MISMATCH_POS, var, 0, PredicateSide::LEFT, copy_idx, 0};
}
/**
* Create mismatch-sampling symbol for a single disequation. When deciding multiple disequations, use
* create_r_symbol_with_predicate_info.
*/
static AtomicSymbol create_r_symbol(const BasicTerm& var, size_t copy_idx, mata::Symbol symbol) {
return AtomicSymbol{TagType::REGISTER_STORE, var, /* predicate_idx */-1, PredicateSide::LEFT, copy_idx, symbol};
}
static AtomicSymbol create_r_symbol_with_predicate_info(size_t predicate_idx, const BasicTerm& var, PredicateSide side, size_t copy_idx, mata::Symbol symbol) {
AtomicSymbol tag(
TagType::REGISTER_STORE,
var,
predicate_idx,
side,
copy_idx,
symbol
);
return tag;
}
static AtomicSymbol create_c_symbol(const BasicTerm& var, int predicate_idx, PredicateSide side, size_t copy_idx) {
// Create a Copy tag of the form: <C, x, h, k, i -> i+1> with the following semantics
// the mismatch letter for _h_-th disequation and its _k_-th side is in variable _x_
// and it is seen when transitioning from i-th copy. The symbol is the same as the one
// sampled in previous sampling transition.
return AtomicSymbol{TagType::COPY_PREVIOUS, var, predicate_idx, side, copy_idx, 0};
}
private:
// private constructor; the AtomicSymbol should be constructed using functions create*
AtomicSymbol(TagType tag_type, const BasicTerm& var, int predicate_idx, PredicateSide side, size_t copy_idx, mata::Symbol symb )
: type(tag_type), var(var), predicate_idx(predicate_idx), predicate_side(side), copy_idx(copy_idx), symbol(symb) {}
AtomicSymbol(TagType tag_type, int predicate_idx, PredicateSide side, size_t copy_idx, mata::Symbol symb )
: type(tag_type), var(BasicTermType::Variable, "dummy"), predicate_idx(predicate_idx), predicate_side(side), copy_idx(copy_idx), symbol(symb) {}
};
using TagSet = std::set<AtomicSymbol>;
using CounterAlphabet = StructAlphabet<TagSet>;
/**
* Metadata about states of a tag automaton, allowing better debugging enabling testing.
*/
struct TagAutStateMetadata {
std::vector<size_t> var_info; // What variable does the state belong to
std::vector<size_t> levels; // In what copy does the state reside
/**
* Maps states to integers so that two states with the same value are copies of the same state.
*
* Underlying nfa is created by copying one NFA multiple times. This
* field tells if two states are the same because they are a copy of the
* same state.
*/
std::vector<size_t> where_is_state_copied_from;
};
/**
* @brief Tag automaton: an automaton with special symbols consisting of sets of tags present on its transition edges.
*/
struct TagAut {
// underlying nfa
mata::nfa::Nfa nfa;
// Structured alphabet providing bi-directional between sets of tags and their corresponding Mata handles that are actually used by mata
CounterAlphabet alph;
// variable ordering
std::vector<BasicTerm> var_order {};
TagAutStateMetadata metadata;
// Debugging: color states in the tag automaton according to the variable it originates in
std::unordered_map<mata::nfa::State, uint64_t> node_color_map;
size_t num_of_states_in_row;
std::string symbol_to_string(const std::set<AtomicSymbol>& sym) const {
std::string ret;
for(const AtomicSymbol& as : sym) {
ret += as.to_string() + " ";
}
return ret;
}
// For some reason we cannot pull parikh_image.h, remake the Transition alias
typedef std::tuple<mata::nfa::State, mata::Symbol, mata::nfa::State> TransitionTuple;
// taken and modified from Mata
void print_to_dot(std::ostream &output, const std::map<TransitionTuple, BasicTerm>& transition_vars = {}) const {
const char* color_table[] = {
"gray", // DEFAULT
"red",
"green",
"blue",
"yellow",
"purple",
};
const size_t state_cnt = nfa.delta.num_of_states();
// Collect all used states so we do not create notes for needless states
// #Optimize(mhecko): maybe bit_set could be faster
std::unordered_set<mata::nfa::State> used_states;
for (mata::nfa::State source_state = 0; source_state < state_cnt; ++source_state) {
for (const mata::nfa::SymbolPost &move: nfa.delta[source_state]) {
for (mata::nfa::State target_state: move.targets) {
used_states.insert(source_state);
used_states.insert(target_state);
}
}
}
output << "digraph finiteAutomaton {" << std::endl
<< "node [shape=circle];" << std::endl;
for (mata::nfa::State state = 0; state < state_cnt; state++) {
if (!used_states.contains(state)) continue;
bool is_final = nfa.final.contains(state);
auto color_val_it = this->node_color_map.find(state);
uint64_t color_idx = (color_val_it == this->node_color_map.end()) ? 0 : color_val_it->second;
color_idx = color_idx >= sizeof(color_table) ? 0 : color_idx; // Reset if overflow
const char* shape = is_final ? "doublecircle" : "circle";
const char* color = color_table[color_idx];
output << "node_" << state << "[style=filled, fillcolor=" << color << ", shape=" << shape << ", label=" << state << "];\n";
}
for (mata::nfa::State source = 0; source != state_cnt; ++source) {
for (const mata::nfa::SymbolPost &move: nfa.delta[source]) {
for (mata::nfa::State target: move.targets) {
TransitionTuple transition = std::make_tuple(source, move.symbol, target);
auto var_labeling_transition_it = transition_vars.find(transition);
output << "node_" << source << " -> "
<< "node_" << target << " "
<< " [label=\"" << symbol_to_string(alph.get_symbol(move.symbol));
if (var_labeling_transition_it != transition_vars.end()) {
output << " (" << var_labeling_transition_it->second << ")";
}
output << "\"];\n";
}
}
}
output << "node [shape=none, label=\"\"];" << std::endl;
for (mata::nfa::State init_state: nfa.initial) {
output << "i" << init_state << " -> " << "node_" << init_state << ";" << std::endl;
}
output << "}" << std::endl;
}
std::set<ca::AtomicSymbol> gather_used_symbols() {
std::set<AtomicSymbol> atomic_symbols;
for (const auto& trans : this->nfa.delta.transitions()) {
std::set<AtomicSymbol> sms = this->alph.get_symbol(trans.symbol);
atomic_symbols.insert(sms.begin(), sms.end());
}
return atomic_symbols;
}
};
}
namespace std {
std::ostream& operator<<(std::ostream& out_stream, const smt::noodler::ca::TagSet& tag_set);
}
#endif