Search in sources :

Example 1 with BitvectorFormula

use of org.sosy_lab.java_smt.api.BitvectorFormula 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 2 with BitvectorFormula

use of org.sosy_lab.java_smt.api.BitvectorFormula in project java-smt by sosy-lab.

the class CVC4FormulaCreator method getFormulaType.

@SuppressWarnings("unchecked")
@Override
public <T extends Formula> FormulaType<T> getFormulaType(T pFormula) {
    Type t = extractInfo(pFormula).getType();
    if (pFormula instanceof BitvectorFormula) {
        checkArgument(t.isBitVector(), "BitvectorFormula with actual type %s: %s", t, pFormula);
        return (FormulaType<T>) getFormulaType(extractInfo(pFormula));
    } else if (pFormula instanceof FloatingPointFormula) {
        checkArgument(t.isFloatingPoint(), "FloatingPointFormula with actual type %s: %s", t, pFormula);
        edu.stanford.CVC4.FloatingPointType fpType = new edu.stanford.CVC4.FloatingPointType(t);
        return (FormulaType<T>) FormulaType.getFloatingPointType((int) fpType.getExponentSize(), // without sign bit
        (int) fpType.getSignificandSize() - 1);
    } else if (pFormula instanceof ArrayFormula<?, ?>) {
        FormulaType<T> arrayIndexType = getArrayFormulaIndexType((ArrayFormula<T, T>) pFormula);
        FormulaType<T> arrayElementType = getArrayFormulaElementType((ArrayFormula<T, T>) pFormula);
        return (FormulaType<T>) FormulaType.getArrayType(arrayIndexType, arrayElementType);
    }
    return super.getFormulaType(pFormula);
}
Also used : BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) CVC4BitvectorFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4BitvectorFormula) FloatingPointType(org.sosy_lab.java_smt.api.FormulaType.FloatingPointType) CVC4.vectorType(edu.stanford.CVC4.vectorType) Type(edu.stanford.CVC4.Type) BitVectorType(edu.stanford.CVC4.BitVectorType) FormulaType(org.sosy_lab.java_smt.api.FormulaType) ArrayFormulaType(org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType) ArrayType(edu.stanford.CVC4.ArrayType) FunctionType(edu.stanford.CVC4.FunctionType) FloatingPointFormula(org.sosy_lab.java_smt.api.FloatingPointFormula) CVC4FloatingPointFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4FloatingPointFormula) ArrayFormula(org.sosy_lab.java_smt.api.ArrayFormula) CVC4ArrayFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4ArrayFormula) FormulaType(org.sosy_lab.java_smt.api.FormulaType) ArrayFormulaType(org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType) FloatingPointType(org.sosy_lab.java_smt.api.FormulaType.FloatingPointType)

Example 3 with BitvectorFormula

use of org.sosy_lab.java_smt.api.BitvectorFormula in project java-smt by sosy-lab.

the class ArrayFormulaManagerTest method testBvIndexBvValue.

/*
   *  Test whether or not Bitvector Arrays are possible with bv index
   */
@Test
public void testBvIndexBvValue() throws SolverException, InterruptedException {
    requireBitvectors();
    // (arr2 = store(arr1, 0100, 0010)) & !(select(arr2, 0100) = 0010)
    BitvectorFormula num2 = bvmgr.makeBitvector(4, 2);
    BitvectorFormula num4 = bvmgr.makeBitvector(4, 4);
    ArrayFormula<BitvectorFormula, BitvectorFormula> arr1 = amgr.makeArray("arr1", getBitvectorTypeWithSize(4), getBitvectorTypeWithSize(4));
    ArrayFormula<BitvectorFormula, BitvectorFormula> arr2 = amgr.makeArray("arr2", getBitvectorTypeWithSize(4), getBitvectorTypeWithSize(4));
    BooleanFormula query = bmgr.and(amgr.equivalence(arr2, amgr.store(arr1, num4, num2)), bmgr.not(bvmgr.equal(num2, amgr.select(arr2, num4))));
    assertThatFormula(query).isUnsatisfiable();
}
Also used : BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) Test(org.junit.Test)

Example 4 with BitvectorFormula

use of org.sosy_lab.java_smt.api.BitvectorFormula in project java-smt by sosy-lab.

the class BitvectorFormulaManagerTest method bvBvArray.

@Test
public void bvBvArray() {
    requireArrays();
    BitvectorFormula bv = bvmgr.makeBitvector(4, 3);
    ArrayFormula<BitvectorFormula, BitvectorFormula> arr = amgr.makeArray("arr", bvType4, bvType4);
    BitvectorFormula index = bvmgr.makeBitvector(4, 2);
    arr = amgr.store(arr, index, bv);
    assertThat(mgr.getFormulaType(arr)).isEqualTo(FormulaType.getArrayType(bvType4, bvType4));
}
Also used : BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) Test(org.junit.Test)

Example 5 with BitvectorFormula

use of org.sosy_lab.java_smt.api.BitvectorFormula in project java-smt by sosy-lab.

the class BitvectorFormulaManagerTest method bvExtractTooLargeNumStartAltSigned.

@Test
public void bvExtractTooLargeNumStartAltSigned() {
    // Use bv > 1 because of Boolector
    BitvectorFormula bv = bvmgr.makeBitvector(4, 4);
    assertThrows(IllegalArgumentException.class, () -> bvmgr.extract(bv, 3, 4));
}
Also used : BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) Test(org.junit.Test)

Aggregations

BitvectorFormula (org.sosy_lab.java_smt.api.BitvectorFormula)61 Test (org.junit.Test)54 BooleanFormula (org.sosy_lab.java_smt.api.BooleanFormula)23 IntegerFormula (org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula)12 FloatingPointFormula (org.sosy_lab.java_smt.api.FloatingPointFormula)7 Model (org.sosy_lab.java_smt.api.Model)6 ProverEnvironment (org.sosy_lab.java_smt.api.ProverEnvironment)6 BasicProverEnvironment (org.sosy_lab.java_smt.api.BasicProverEnvironment)5 ArrayFormula (org.sosy_lab.java_smt.api.ArrayFormula)4 BitvectorType (org.sosy_lab.java_smt.api.FormulaType.BitvectorType)4 ValueAssignment (org.sosy_lab.java_smt.api.Model.ValueAssignment)4 StringFormula (org.sosy_lab.java_smt.api.StringFormula)3 BooleanFormulaTransformationVisitor (org.sosy_lab.java_smt.api.visitors.BooleanFormulaTransformationVisitor)3 FormulaTransformationVisitor (org.sosy_lab.java_smt.api.visitors.FormulaTransformationVisitor)3 FormulaType (org.sosy_lab.java_smt.api.FormulaType)2 ArrayFormulaType (org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType)2 FloatingPointType (org.sosy_lab.java_smt.api.FormulaType.FloatingPointType)2 ITerm (ap.parser.ITerm)1 Sort (ap.types.Sort)1 ImmutableList (com.google.common.collect.ImmutableList)1