forked from sosy-lab/java-smt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormulaClassifier.java
334 lines (302 loc) · 10 KB
/
FormulaClassifier.java
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
// This file is part of JavaSMT,
// an API wrapper for a collection of SMT solvers:
// https://github.com/sosy-lab/java-smt
//
// SPDX-FileCopyrightText: 2020 Dirk Beyer <https://www.sosy-lab.org>
//
// SPDX-License-Identifier: Unlicense OR Apache-2.0 OR MIT
package org.sosy_lab.java_smt.example;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.logging.Level;
import org.sosy_lab.common.ShutdownNotifier;
import org.sosy_lab.common.configuration.Configuration;
import org.sosy_lab.common.configuration.InvalidConfigurationException;
import org.sosy_lab.common.log.BasicLogManager;
import org.sosy_lab.common.log.LogManager;
import org.sosy_lab.java_smt.SolverContextFactory;
import org.sosy_lab.java_smt.SolverContextFactory.Solvers;
import org.sosy_lab.java_smt.api.BooleanFormula;
import org.sosy_lab.java_smt.api.Formula;
import org.sosy_lab.java_smt.api.FormulaManager;
import org.sosy_lab.java_smt.api.FormulaType;
import org.sosy_lab.java_smt.api.FunctionDeclaration;
import org.sosy_lab.java_smt.api.FunctionDeclarationKind;
import org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier;
import org.sosy_lab.java_smt.api.SolverContext;
import org.sosy_lab.java_smt.api.SolverException;
import org.sosy_lab.java_smt.api.visitors.DefaultBooleanFormulaVisitor;
import org.sosy_lab.java_smt.api.visitors.FormulaVisitor;
import org.sosy_lab.java_smt.api.visitors.TraversalProcess;
/**
* This program parses user-given formulas and prints out the (minimal) matching theory for them.
*
* <p>Warning: This is a prototype and not intended for larger usage.
*/
@SuppressWarnings("unused")
public class FormulaClassifier {
private final FormulaManager mgr;
private final SolverContext context;
private final Classifier v = new Classifier();
private int levelLinearArithmetic = 0;
public static void main(String... args)
throws InvalidConfigurationException, SolverException, InterruptedException, IOException {
if (args.length == 0) {
help();
}
Solvers solver = Solvers.MATHSAT5;
Path path = null;
for (String arg : args) {
if (arg.startsWith("-solver=")) {
solver = Solvers.valueOf(arg.substring(8));
} else if (path == null) {
path = Path.of(arg);
} else {
help();
}
}
if (path == null) {
help();
}
Configuration config = Configuration.defaultConfiguration();
LogManager logger = BasicLogManager.create(config);
ShutdownNotifier notifier = ShutdownNotifier.createDummy();
// we need a solver that supports all theories, at least for parsing.
try (SolverContext context =
SolverContextFactory.createSolverContext(config, logger, notifier, solver)) {
List<BooleanFormula> formulas = new ArrayList<>();
// read all formulas from the file
List<String> definitions = new ArrayList<>();
for (String line : Files.readAllLines(path)) {
// we assume a line-based content
if (Iterables.any(
ImmutableList.of(";", "(push ", "(pop ", "(reset", "(set-logic"), line::startsWith)) {
continue;
} else if (line.startsWith("(assert ")) {
BooleanFormula bf =
context.getFormulaManager().parse(Joiner.on("").join(definitions) + line);
formulas.add(bf);
} else {
// it is a definition
definitions.add(line);
}
}
// classify the formulas
FormulaClassifier fc = new FormulaClassifier(context);
formulas.forEach(fc::visit);
System.out.println(fc + ", checked formulas: " + formulas.size());
} catch (InvalidConfigurationException | UnsatisfiedLinkError e) {
// on some machines we support only some solvers,
// thus we can ignore these errors.
logger.logUserException(Level.INFO, e, "Solver " + solver + " is not available.");
} catch (UnsupportedOperationException e) {
logger.logUserException(Level.INFO, e, e.getMessage());
}
}
private static void help() {
throw new AssertionError("run $> TOOL [-solver=SOLVER] PATH");
}
public FormulaClassifier(SolverContext pContext) {
context = pContext;
mgr = context.getFormulaManager();
}
public void visit(BooleanFormula f) {
// first split formula into atoms to avoid repeated analysis of common subtrees.
AtomCollector atomCollector = new AtomCollector();
mgr.getBooleanFormulaManager().visitRecursively(f, atomCollector);
if (atomCollector.hasQuantifiers) {
v.hasQuantifiers = true;
}
// then analyze each part
for (BooleanFormula part : atomCollector.atoms) {
int levelLA = mgr.visit(part, v);
levelLinearArithmetic = Math.max(levelLA, levelLinearArithmetic);
}
}
@Override
public String toString() {
// build logic string
StringBuilder logic = new StringBuilder();
if (!v.hasQuantifiers) {
logic.append("QF_");
}
if (v.hasArrays) {
logic.append("A");
}
if (v.hasUFs) {
logic.append("UF");
}
if (v.hasBVs) {
logic.append("BV");
}
if (v.nonLinearArithmetic || v.linearArithmetic) {
if (v.hasInts && v.hasReals) {
if (v.nonLinearArithmetic) {
logic.append("N");
} else if (v.linearArithmetic) {
logic.append("L");
}
logic.append("IRA");
} else if (v.hasInts) {
if (v.nonLinearArithmetic) {
logic.append("N");
} else if (v.linearArithmetic) {
logic.append("L");
}
logic.append("IA");
} else if (v.hasReals) {
if (v.nonLinearArithmetic) {
logic.append("N");
} else if (v.linearArithmetic) {
logic.append("L");
}
logic.append("RA");
}
}
if (v.hasFloats) {
// TODO forthcoming, see http://smtlib.cs.uiowa.edu/logics.shtml
logic.append("FP");
}
return logic.toString();
}
private static class AtomCollector extends DefaultBooleanFormulaVisitor<TraversalProcess> {
private final Collection<BooleanFormula> atoms = new LinkedHashSet<>();
boolean hasQuantifiers = false;
@Override
protected TraversalProcess visitDefault() {
return TraversalProcess.CONTINUE;
}
@Override
public TraversalProcess visitAtom(
BooleanFormula atom, FunctionDeclaration<BooleanFormula> funcDecl) {
atoms.add(atom);
return TraversalProcess.CONTINUE;
}
@Override
public TraversalProcess visitQuantifier(
Quantifier quantifier,
BooleanFormula quantifiedAST,
List<Formula> boundVars,
BooleanFormula body) {
hasQuantifiers = true;
return visitDefault();
}
}
private class Classifier implements FormulaVisitor<Integer> {
boolean hasUFs = false;
boolean hasQuantifiers = false;
boolean hasFloats = false;
boolean hasInts = false;
boolean hasReals = false;
boolean hasBVs = false;
boolean hasArrays = false;
boolean linearArithmetic = false;
boolean nonLinearArithmetic = false;
void checkType(Formula f) {
FormulaType<Formula> type = mgr.getFormulaType(f);
if (type.isIntegerType()) {
hasInts = true;
}
if (type.isRationalType()) {
hasReals = true;
}
if (type.isFloatingPointType()) {
hasFloats = true;
}
if (type.isBitvectorType()) {
hasBVs = true;
}
if (type.isArrayType()) {
hasArrays = true;
}
}
@Override
public Integer visitFreeVariable(Formula pF, String pName) {
checkType(pF);
return 1;
}
@Override
public Integer visitBoundVariable(Formula pF, int pDeBruijnIdx) {
checkType(pF);
return 1;
}
@Override
public Integer visitConstant(Formula pF, Object pValue) {
checkType(pF);
return 0;
}
@Override
public Integer visitFunction(
Formula pF, List<Formula> args, FunctionDeclaration<?> pFunctionDeclaration) {
if (pFunctionDeclaration.getKind() == FunctionDeclarationKind.UF) {
hasUFs = true;
}
checkType(pF);
int numNonConstantArgs = 0;
int allArgLevel = 0;
for (Formula arg : args) {
int argLevel = mgr.visit(arg, this);
if (argLevel >= 1) {
numNonConstantArgs++;
}
allArgLevel = Math.max(allArgLevel, argLevel);
}
switch (pFunctionDeclaration.getKind()) {
case MUL:
case BV_MUL:
case DIV:
case BV_UDIV:
case BV_SDIV:
case MODULO:
case BV_UREM:
case BV_SREM:
if (numNonConstantArgs >= 2) {
nonLinearArithmetic = true;
return allArgLevel + 1;
}
// $FALL-THROUGH$
default:
if (pFunctionDeclaration.getType().isBooleanType()) {
if (EnumSet.of(
FunctionDeclarationKind.LT,
FunctionDeclarationKind.LTE,
FunctionDeclarationKind.GT,
FunctionDeclarationKind.GTE)
.contains(pFunctionDeclaration.getKind())) {
for (Formula arg : args) {
FormulaType<Formula> type = mgr.getFormulaType(arg);
if (type.isIntegerType() || type.isRationalType()) {
linearArithmetic = true;
}
}
}
return 0;
} else {
if (pFunctionDeclaration.getKind() != FunctionDeclarationKind.UF) {
linearArithmetic = true;
}
return allArgLevel;
}
}
}
@Override
public Integer visitQuantifier(
BooleanFormula pF,
Quantifier pQuantifier,
List<Formula> pBoundVariables,
BooleanFormula pBody) {
hasQuantifiers = true;
checkType(pF);
return mgr.visit(pBody, this);
}
}
}