Search in sources :

Example 1 with BooleanFormula

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);
}
Also used : IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) ArrayList(java.util.ArrayList) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula)

Example 2 with BooleanFormula

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());
    }
}
Also used : Path(java.nio.file.Path) ShutdownNotifier(org.sosy_lab.common.ShutdownNotifier) Configuration(org.sosy_lab.common.configuration.Configuration) ArrayList(java.util.ArrayList) InvalidConfigurationException(org.sosy_lab.common.configuration.InvalidConfigurationException) SolverContext(org.sosy_lab.java_smt.api.SolverContext) Solvers(org.sosy_lab.java_smt.SolverContextFactory.Solvers) LogManager(org.sosy_lab.common.log.LogManager) BasicLogManager(org.sosy_lab.common.log.BasicLogManager) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula)

Example 3 with BooleanFormula

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;
}
Also used : IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) ArrayList(java.util.ArrayList) Model(org.sosy_lab.java_smt.api.Model) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula)

Example 4 with BooleanFormula

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;
}
Also used : BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) ValueAssignment(org.sosy_lab.java_smt.api.Model.ValueAssignment) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula)

Example 5 with BooleanFormula

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;
}
Also used : ValueAssignment(org.sosy_lab.java_smt.api.Model.ValueAssignment) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula)

Aggregations

BooleanFormula (org.sosy_lab.java_smt.api.BooleanFormula)358 Test (org.junit.Test)268 IntegerFormula (org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula)116 BooleanFormulaManager (org.sosy_lab.java_smt.api.BooleanFormulaManager)48 BitvectorFormula (org.sosy_lab.java_smt.api.BitvectorFormula)42 ArrayList (java.util.ArrayList)38 Formula (org.sosy_lab.java_smt.api.Formula)32 StringFormula (org.sosy_lab.java_smt.api.StringFormula)29 FloatingPointFormula (org.sosy_lab.java_smt.api.FloatingPointFormula)25 ProverEnvironment (org.sosy_lab.java_smt.api.ProverEnvironment)24 Model (org.sosy_lab.java_smt.api.Model)23 Tuple (com.dat3m.dartagnan.wmm.utils.Tuple)20 List (java.util.List)18 SolverException (org.sosy_lab.java_smt.api.SolverException)17 ImmutableList (com.google.common.collect.ImmutableList)16 BasicProverEnvironment (org.sosy_lab.java_smt.api.BasicProverEnvironment)15 SolverContext (org.sosy_lab.java_smt.api.SolverContext)15 Event (com.dat3m.dartagnan.program.event.core.Event)14 RegexFormula (org.sosy_lab.java_smt.api.RegexFormula)14 BooleanFormulaTransformationVisitor (org.sosy_lab.java_smt.api.visitors.BooleanFormulaTransformationVisitor)14