use of org.sosy_lab.java_smt.api.BooleanFormula in project java-smt by sosy-lab.
the class Interpolation method interpolateProgramTrace.
/**
* Example for encoding a program path and computing interpolants along the path. Taken from <a
* href="http://satsmt2014.forsyte.at/files/2014/01/interpolation_philipp.pdf">slides</a> from
* Philipp Rümmer.
*
* <p>Example trace of a program:
*
* <pre>
* i=0
* k=j
* assume (i<50)
* i++
* k++
* assume (i>=50)
* assume (j==0)
* assume (k<50)
* </pre>
*/
private static <T> void interpolateProgramTrace(InterpolatingProverEnvironment<T> prover, IntegerFormulaManager imgr, LogManager logger) throws InterruptedException, SolverException {
// create some variables.
// primed variable needed for 'self-assignments', alternatively use SSA-indices.
IntegerFormula i = imgr.makeVariable("i");
IntegerFormula i1 = imgr.makeVariable("i'");
IntegerFormula j = imgr.makeVariable("j");
IntegerFormula k = imgr.makeVariable("k");
IntegerFormula k1 = imgr.makeVariable("k'");
IntegerFormula zero = imgr.makeNumber(0);
IntegerFormula one = imgr.makeNumber(1);
IntegerFormula fifty = imgr.makeNumber(50);
// create and assert some formulas.
List<BooleanFormula> programTrace = ImmutableList.of(imgr.equal(i, zero), imgr.equal(k, j), imgr.lessThan(i, fifty), imgr.equal(i1, imgr.add(i, one)), imgr.equal(k1, imgr.add(k, one)), imgr.greaterOrEquals(i1, fifty), imgr.equal(j, zero), imgr.lessThan(k1, fifty));
// assert all formulas in the prover
List<T> handles = new ArrayList<>();
for (BooleanFormula step : programTrace) {
handles.add(prover.addConstraint(step));
}
// check for satisfiability
boolean unsat = prover.isUnsat();
Preconditions.checkState(unsat, "the example for interpolation should be UNSAT");
// get a sequence of interpolants for the program trace.
List<BooleanFormula> itps = prover.getSeqInterpolants0(handles);
logger.log(Level.INFO, "Interpolants for the program trace are:", itps);
}
use of org.sosy_lab.java_smt.api.BooleanFormula in project java-smt by sosy-lab.
the class PrettyPrinter method main.
public static void main(String... args) throws InvalidConfigurationException, SolverException, InterruptedException, IOException {
if (args.length == 0) {
help();
}
Solvers solver = Solvers.MATHSAT5;
Type type = Type.TEXT;
Path path = null;
for (String arg : args) {
if (arg.startsWith("-solver=")) {
solver = Solvers.valueOf(arg.substring(8));
} else if (arg.startsWith("-type=")) {
type = Type.valueOf(arg.substring(6).toUpperCase());
} 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
org.sosy_lab.java_smt.utils.PrettyPrinter pp = SolverUtils.prettyPrinter(context.getFormulaManager());
for (BooleanFormula formula : formulas) {
System.out.println(formulaToString(formula, pp, type));
}
} 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());
}
}
use of org.sosy_lab.java_smt.api.BooleanFormula in project java-smt by sosy-lab.
the class AllSatExample method allSatIntegers.
/**
* For integer formulas, we can implement the allsat-loop and collect all models when iterating.
*/
private List<List<ValueAssignment>> allSatIntegers() throws InterruptedException, SolverException {
bfmgr = context.getFormulaManager().getBooleanFormulaManager();
ifmgr = context.getFormulaManager().getIntegerFormulaManager();
// formula ((1 <= a <= 10) && (1 <= b <= 10) && (a >= 2 * b)) with 25 models
IntegerFormula a = ifmgr.makeVariable("a");
IntegerFormula b = ifmgr.makeVariable("b");
prover.addConstraint(ifmgr.lessOrEquals(num(1), a));
prover.addConstraint(ifmgr.lessOrEquals(a, num(10)));
prover.addConstraint(ifmgr.lessOrEquals(num(1), b));
prover.addConstraint(ifmgr.lessOrEquals(b, num(10)));
prover.addConstraint(ifmgr.greaterOrEquals(a, ifmgr.multiply(num(2), b)));
List<List<ValueAssignment>> models = new ArrayList<>();
// loop over all possible models for "1<=a<=10 AND 1<=b<=10 AND a==2*b"
while (!prover.isUnsat()) {
models.add(prover.getModelAssignments());
try (Model model = prover.getModel()) {
// convert model into formula, assuming we know all symbols and know they are integers
BooleanFormula modelAsFormula = bfmgr.and(ifmgr.equal(a, num(model.evaluate(a))), ifmgr.equal(b, num(model.evaluate(b))));
// prevent next model from using the same assignment as a previous model
prover.addConstraint(bfmgr.not(modelAsFormula));
}
}
return models;
}
use of org.sosy_lab.java_smt.api.BooleanFormula in project java-smt by sosy-lab.
the class AllSatExample method allSatBitvectors.
/**
* For bitvector formulas, we can implement the allsat-loop and collect all models when iterating.
*/
private List<List<ValueAssignment>> allSatBitvectors() throws InterruptedException, SolverException {
bfmgr = context.getFormulaManager().getBooleanFormulaManager();
bvfmgr = context.getFormulaManager().getBitvectorFormulaManager();
// formula ((1 <= a <= 3) && (0 == b) && (p == q)) with 6 models
final int bitsize = 4;
BitvectorFormula a = bvfmgr.makeVariable(bitsize, "c");
BitvectorFormula b = bvfmgr.makeVariable(bitsize, "d");
BooleanFormula p = bfmgr.makeVariable("r");
BooleanFormula q = bfmgr.makeVariable("s");
prover.addConstraint(bvfmgr.lessOrEquals(bv(bitsize, 1), a, true));
prover.addConstraint(bvfmgr.equal(bv(bitsize, 0), b));
prover.addConstraint(bvfmgr.lessOrEquals(a, bv(bitsize, 3), true));
prover.addConstraint(bfmgr.equivalence(p, q));
List<List<ValueAssignment>> models = new ArrayList<>();
// loop over all possible models for "1<=a<=3 AND p=q"
while (!prover.isUnsat()) {
final ImmutableList<ValueAssignment> modelAssignments = prover.getModelAssignments();
models.add(modelAssignments);
final List<BooleanFormula> modelAssignmentsAsFormulas = new ArrayList<>();
for (ValueAssignment va : modelAssignments) {
modelAssignmentsAsFormulas.add(va.getAssignmentAsFormula());
}
// prevent next model from using the same assignment as a previous model
prover.addConstraint(bfmgr.not(bfmgr.and(modelAssignmentsAsFormulas)));
}
return models;
}
use of org.sosy_lab.java_smt.api.BooleanFormula in project java-smt by sosy-lab.
the class AllSatExample method allSatIntegers2.
/**
* For integer formulas, we can implement the allsat-loop and collect all models when iterating.
*/
private List<List<ValueAssignment>> allSatIntegers2() throws InterruptedException, SolverException {
bfmgr = context.getFormulaManager().getBooleanFormulaManager();
ifmgr = context.getFormulaManager().getIntegerFormulaManager();
// formula ((1 <= a <= 3) && (0 == b) && (p == q)) with 6 models
IntegerFormula a = ifmgr.makeVariable("a");
IntegerFormula b = ifmgr.makeVariable("b");
BooleanFormula p = bfmgr.makeVariable("p");
BooleanFormula q = bfmgr.makeVariable("q");
prover.addConstraint(ifmgr.lessOrEquals(num(1), a));
prover.addConstraint(ifmgr.equal(num(0), b));
prover.addConstraint(ifmgr.lessOrEquals(a, num(3)));
prover.addConstraint(bfmgr.equivalence(p, q));
List<List<ValueAssignment>> models = new ArrayList<>();
// loop over all possible models for "1<=a<=3 AND p=q"
while (!prover.isUnsat()) {
final ImmutableList<ValueAssignment> modelAssignments = prover.getModelAssignments();
models.add(modelAssignments);
final List<BooleanFormula> modelAssignmentsAsFormulas = new ArrayList<>();
for (ValueAssignment va : modelAssignments) {
modelAssignmentsAsFormulas.add(va.getAssignmentAsFormula());
}
// prevent next model from using the same assignment as a previous model
prover.addConstraint(bfmgr.not(bfmgr.and(modelAssignmentsAsFormulas)));
}
return models;
}
Aggregations