forked from martinjonas/Q3B
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariableOrderer.cpp
230 lines (196 loc) · 5.77 KB
/
VariableOrderer.cpp
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
#include "VariableOrderer.h"
#include <algorithm>
using namespace std;
using namespace z3;
VariableOrderer::VariableOrderer(const std::set<var> &vars, z3::context &ctx) : vars(vars)
{
this->context = &ctx;
this->unionFind = new UF(vars.size());
int i = 0;
for (auto const &v : vars)
{
varIndices[v.first] = i;
i++;
}
}
void VariableOrderer::MergeByExpression(const z3::expr &e, std::vector<std::string> boundVars)
{
auto item = processedMergedSubformulaCache.find((Z3_ast)e);
if (item != processedMergedSubformulaCache.end() && item->second == boundVars)
{
return;
}
assert(e.is_bool());
if (e.is_app())
{
//cout << "APP: " << e << endl;
func_decl f = e.decl();
unsigned num = e.num_args();
string functionName = f.name().str();
//cout << "fun: " << functionName << endl;
if (functionName == "not")
{
MergeByExpression(e.arg(0), boundVars);
}
else if (functionName == "and")
{
for (unsigned int i = 0; i < num; i++)
{
MergeByExpression(e.arg(i), boundVars);
}
}
else if (functionName == "or")
{
for (unsigned int i = 0; i < num; i++)
{
MergeByExpression(e.arg(i), boundVars);
}
}
else if (functionName == "iff")
{
MergeByExpression(e.arg(0), boundVars);
MergeByExpression(e.arg(1), boundVars);
}
else if (functionName == "if")
{
MergeByExpression(e.arg(0), boundVars);
MergeByExpression(e.arg(1), boundVars);
MergeByExpression(e.arg(2), boundVars);
}
else
{
std::set<std::string> vars;
for (unsigned int i = 0; i < num; i++)
{
set<std::string> currentVars = GetVars(e.arg(i), boundVars);
vars.insert(currentVars.begin(), currentVars.end());
}
if (vars.size() > 0)
{
for (auto const &v : vars)
{
MarkDependent(*vars.begin(), v);
}
}
//cout << "function " << f.name().str() << endl;
}
}
else if(e.is_quantifier())
{
Z3_ast ast = (Z3_ast)e;
int boundVariables = Z3_get_quantifier_num_bound(*context, ast);
for (int i = 0; i < boundVariables; i++)
{
Z3_symbol z3_symbol = Z3_get_quantifier_bound_name(*context, ast, i);
symbol current_symbol(*context, z3_symbol);
string c = current_symbol.str();
boundVars.push_back(c);
}
MergeByExpression(e.body(), boundVars);
//bdd bodyBdd = getBDDFromExpr(e.body(), boundVars);
}
processedMergedSubformulaCache.insert({(Z3_ast)e, boundVars});
}
void VariableOrderer::MergeAll()
{
for (unsigned int i = 1; i < vars.size(); i++)
{
unionFind->merge(0, i);
}
}
set<string> VariableOrderer::GetVars(const expr &e, std::vector<std::string> boundVars)
{
set<string> vars;
auto item = processedVarsCache.find((Z3_ast)e);
if (item != processedVarsCache.end() && (item->second).second == boundVars)
{
return (item->second).first;
}
if (e.is_var())
{
Z3_ast ast = (Z3_ast)e;
int deBruijnIndex = Z3_get_index_value(*context, ast);
vars.insert(boundVars[boundVars.size() - deBruijnIndex - 1]);
return vars;
}
if (e.is_app())
{
func_decl f = e.decl();
unsigned num = e.num_args();
if (num != 0)
{
for (unsigned i = 0; i < num; i++)
{
set<string> currentVars = GetVars(e.arg(i), boundVars);
vars.insert(currentVars.begin(), currentVars.end());
processedVarsCache.insert({(Z3_ast)e.arg(i), {vars, boundVars}});
}
}
else if (f.name() != NULL)
{
z3::sort s = f.range();
if (s.is_bv() && !e.is_numeral())
{
vars.insert(f.name().str());
}
}
return vars;
}
else if(e.is_quantifier())
{
Z3_ast ast = (Z3_ast)e;
int boundVariables = Z3_get_quantifier_num_bound(*context, ast);
for (int i = 0; i < boundVariables; i++)
{
Z3_symbol z3_symbol = Z3_get_quantifier_bound_name(*context, ast, i);
symbol current_symbol(*context, z3_symbol);
string c = current_symbol.str();
boundVars.push_back(c);
}
return GetVars(e.body(), boundVars);
//bdd bodyBdd = getBDDFromExpr(e.body(), boundVars);
}
return vars;
}
void VariableOrderer::MarkDependent(const string &x, const string &y)
{
int index1 = varIndices[x];
int index2 = varIndices[y];
unionFind->merge(index1, index2);
//cout << "merging " << x << " " << y << endl;
}
void VariableOrderer::OrderFor(const z3::expr &expr)
{
MergeByExpression(expr, vector<string>());
}
list<list<var>> VariableOrderer::GetOrdered() const
{
vector<pair<var, int>> pairList;
for (auto const &v : vars)
{
auto varIndex = varIndices.find(v.first);
auto p = pair<var, int>(v, unionFind->find(varIndex->second));
pairList.push_back(p);
}
std::sort(pairList.begin(), pairList.end(), [](const std::pair<var,int> &left, const std::pair<var,int> &right) {
return left.second < right.second;
});
list<list<var>> orderedVarGroups;
list<var> currentGroup;
int lastGroupIndex = 0;
for (auto const &p : pairList)
{
if (p.second != lastGroupIndex)
{
if (currentGroup.size() > 0)
{
orderedVarGroups.push_back(currentGroup);
currentGroup.clear();
}
lastGroupIndex = p.second;
}
currentGroup.push_back(p.first);
}
orderedVarGroups.push_back(currentGroup);
return orderedVarGroups;
}