Search in sources :

Example 1 with Quantifier

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

the class VariableNamesTest method testNameInQuantification.

@Test
public void testNameInQuantification() {
    requireQuantifiers();
    requireIntegers();
    IntegerFormula var = createVariableWith(imgr::makeVariable, getVarname());
    IntegerFormula zero = imgr.makeNumber(0);
    BooleanFormula eq = imgr.equal(var, zero);
    BooleanFormula exists = qmgr.exists(var, eq);
    BooleanFormula query = bmgr.and(bmgr.not(eq), exists);
    // (var != 0) & (EX var: (var == 0))
    assertThat(mgr.extractVariablesAndUFs(eq)).hasSize(1);
    assertThat(mgr.extractVariablesAndUFs(eq)).containsEntry(getVarname(), var);
    assertThat(mgr.extractVariablesAndUFs(query)).hasSize(1);
    assertThat(mgr.extractVariablesAndUFs(query)).containsEntry(getVarname(), var);
    assertThat(mgr.extractVariablesAndUFs(exists)).isEmpty();
    mgr.visit(query, new DefaultFormulaVisitor<Void>() {

        @Override
        public Void visitQuantifier(BooleanFormula pF, Quantifier pQuantifier, List<Formula> pBoundVariables, BooleanFormula pBody) {
            if (solverToUse() != Solvers.PRINCESS) {
                // TODO Princess does not (yet) return quantified variables.
                assertThat(pBoundVariables).hasSize(1);
            }
            for (Formula f : pBoundVariables) {
                Map<String, Formula> map = mgr.extractVariables(f);
                assertThat(map).hasSize(1);
                assertThat(map).containsEntry(getVarname(), f);
            }
            return null;
        }

        @Override
        protected Void visitDefault(Formula pF) {
            return null;
        }
    });
}
Also used : BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) FloatingPointFormula(org.sosy_lab.java_smt.api.FloatingPointFormula) Formula(org.sosy_lab.java_smt.api.Formula) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) Quantifier(org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier) Map(java.util.Map) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) Test(org.junit.Test)

Example 2 with Quantifier

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

the class VariableNamesTest method testNameInNestedQuantification.

@Test
public void testNameInNestedQuantification() {
    requireQuantifiers();
    requireIntegers();
    IntegerFormula var1 = createVariableWith(imgr::makeVariable, getVarname() + 1);
    IntegerFormula var2 = createVariableWith(imgr::makeVariable, getVarname() + 2);
    IntegerFormula var3 = createVariableWith(imgr::makeVariable, getVarname() + 3);
    IntegerFormula var4 = createVariableWith(imgr::makeVariable, getVarname() + 4);
    IntegerFormula zero = imgr.makeNumber(0);
    // (v1 == 0) & (EX v2: ((v2 == v1) & (EX v3: ((v3 == v2) & (EX v4: (v4 == v3))))
    BooleanFormula eq01 = imgr.equal(zero, var1);
    BooleanFormula eq12 = imgr.equal(var1, var2);
    BooleanFormula eq23 = imgr.equal(var2, var3);
    BooleanFormula eq34 = imgr.equal(var3, var4);
    BooleanFormula exists4 = qmgr.exists(var4, eq34);
    BooleanFormula exists3 = qmgr.exists(var3, bmgr.and(eq23, exists4));
    BooleanFormula exists2 = qmgr.exists(var2, bmgr.and(eq12, exists3));
    BooleanFormula query = bmgr.and(eq01, exists2);
    assertThat(mgr.extractVariablesAndUFs(eq01)).hasSize(1);
    assertThat(mgr.extractVariablesAndUFs(eq01)).containsEntry(getVarname() + 1, var1);
    assertThat(mgr.extractVariablesAndUFs(eq12)).hasSize(2);
    assertThat(mgr.extractVariablesAndUFs(eq12)).containsEntry(getVarname() + 1, var1);
    assertThat(mgr.extractVariablesAndUFs(eq12)).containsEntry(getVarname() + 2, var2);
    assertThat(mgr.extractVariablesAndUFs(eq23)).hasSize(2);
    assertThat(mgr.extractVariablesAndUFs(eq23)).containsEntry(getVarname() + 2, var2);
    assertThat(mgr.extractVariablesAndUFs(eq23)).containsEntry(getVarname() + 3, var3);
    assertThat(mgr.extractVariablesAndUFs(eq34)).hasSize(2);
    assertThat(mgr.extractVariablesAndUFs(eq34)).containsEntry(getVarname() + 3, var3);
    assertThat(mgr.extractVariablesAndUFs(eq34)).containsEntry(getVarname() + 4, var4);
    assertThat(mgr.extractVariablesAndUFs(query)).hasSize(1);
    assertThat(mgr.extractVariablesAndUFs(query)).containsEntry(getVarname() + 1, var1);
    assertThat(mgr.extractVariablesAndUFs(exists2)).hasSize(1);
    assertThat(mgr.extractVariablesAndUFs(exists2)).containsEntry(getVarname() + 1, var1);
    assertThat(mgr.extractVariablesAndUFs(exists3)).hasSize(1);
    assertThat(mgr.extractVariablesAndUFs(exists3)).containsEntry(getVarname() + 2, var2);
    assertThat(mgr.extractVariablesAndUFs(exists4)).hasSize(1);
    assertThat(mgr.extractVariablesAndUFs(exists4)).containsEntry(getVarname() + 3, var3);
    mgr.visit(query, new DefaultFormulaVisitor<Void>() {

        int depth = 1;

        @Override
        public Void visitQuantifier(BooleanFormula pF, Quantifier pQuantifier, List<Formula> pBoundVariables, BooleanFormula pBody) {
            if (solverToUse() != Solvers.PRINCESS) {
                // TODO Princess does not return quantified variables.
                assertThat(pBoundVariables).hasSize(1);
            }
            for (Formula f : pBoundVariables) {
                Map<String, Formula> map = mgr.extractVariables(f);
                assertThat(map).hasSize(1);
                assertThat(map).containsEntry(getVarname() + depth, f);
            }
            depth++;
            return null;
        }

        @Override
        protected Void visitDefault(Formula pF) {
            return null;
        }
    });
}
Also used : BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) FloatingPointFormula(org.sosy_lab.java_smt.api.FloatingPointFormula) Formula(org.sosy_lab.java_smt.api.Formula) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) Quantifier(org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier) Map(java.util.Map) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) Test(org.junit.Test)

Example 3 with Quantifier

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

the class UfElimination method isQuantified.

private boolean isQuantified(Formula f) {
    AtomicBoolean result = new AtomicBoolean();
    fmgr.visitRecursively(f, new DefaultFormulaVisitor<>() {

        @Override
        protected TraversalProcess visitDefault(Formula pF) {
            return TraversalProcess.CONTINUE;
        }

        @Override
        public TraversalProcess visitQuantifier(BooleanFormula pF, Quantifier pQ, List<Formula> pBoundVariables, BooleanFormula pBody) {
            result.set(true);
            return TraversalProcess.ABORT;
        }
    });
    return result.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) NumeralFormula(org.sosy_lab.java_smt.api.NumeralFormula) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) StringFormula(org.sosy_lab.java_smt.api.StringFormula) ArrayFormula(org.sosy_lab.java_smt.api.ArrayFormula) FloatingPointFormula(org.sosy_lab.java_smt.api.FloatingPointFormula) BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) Formula(org.sosy_lab.java_smt.api.Formula) Quantifier(org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) TraversalProcess(org.sosy_lab.java_smt.api.visitors.TraversalProcess)

Example 4 with Quantifier

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

the class QuantifierManagerTest method testIntrospectionForall.

@Test
public void testIntrospectionForall() {
    requireIntegers();
    BooleanFormula forall = qmgr.forall(ImmutableList.of(x), a_at_x_eq_0);
    final AtomicBoolean isQuantifier = new AtomicBoolean(false);
    final AtomicBoolean isForall = new AtomicBoolean(false);
    final AtomicInteger numBound = new AtomicInteger(0);
    // Test introspection with visitors.
    mgr.visit(forall, new DefaultFormulaVisitor<Void>() {

        @Override
        protected Void visitDefault(Formula f) {
            return null;
        }

        @Override
        public Void visitQuantifier(BooleanFormula f, Quantifier quantifier, List<Formula> boundVariables, BooleanFormula body) {
            isForall.set(quantifier == Quantifier.FORALL);
            isQuantifier.set(true);
            numBound.set(boundVariables.size());
            return null;
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) StringFormula(org.sosy_lab.java_smt.api.StringFormula) ArrayFormula(org.sosy_lab.java_smt.api.ArrayFormula) BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) Formula(org.sosy_lab.java_smt.api.Formula) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Quantifier(org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) Test(org.junit.Test)

Example 5 with Quantifier

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

the class Z3FormulaCreator method visit.

@Override
public <R> R visit(FormulaVisitor<R> visitor, final Formula formula, final Long f) {
    switch(Z3_ast_kind.fromInt(Native.getAstKind(environment, f))) {
        case Z3_NUMERAL_AST:
            return visitor.visitConstant(formula, convertValue(f));
        case Z3_APP_AST:
            int arity = Native.getAppNumArgs(environment, f);
            int declKind = Native.getDeclKind(environment, Native.getAppDecl(environment, f));
            if (arity == 0) {
                // constants
                Object value = Z3_CONSTANTS.get(declKind);
                if (value != null) {
                    return visitor.visitConstant(formula, value);
                } else if (declKind == Z3_decl_kind.Z3_OP_FPA_NUM.toInt() || Native.getSortKind(environment, Native.getSort(environment, f)) == Z3_sort_kind.Z3_ROUNDING_MODE_SORT.toInt()) {
                    return visitor.visitConstant(formula, convertValue(f));
                } else {
                    // TODO: assert that.
                    return visitor.visitFreeVariable(formula, getAppName(f));
                }
            }
            ImmutableList.Builder<Formula> args = ImmutableList.builder();
            ImmutableList.Builder<FormulaType<?>> argTypes = ImmutableList.builder();
            for (int i = 0; i < arity; i++) {
                long arg = Native.getAppArg(environment, f, i);
                FormulaType<?> argumentType = getFormulaType(arg);
                args.add(encapsulate(argumentType, arg));
                argTypes.add(argumentType);
            }
            return visitor.visitFunction(formula, args.build(), FunctionDeclarationImpl.of(getAppName(f), getDeclarationKind(f), argTypes.build(), getFormulaType(f), Native.getAppDecl(environment, f)));
        case Z3_VAR_AST:
            int deBruijnIdx = Native.getIndexValue(environment, f);
            return visitor.visitBoundVariable(formula, deBruijnIdx);
        case Z3_QUANTIFIER_AST:
            BooleanFormula body = encapsulateBoolean(Native.getQuantifierBody(environment, f));
            Quantifier q = Native.isQuantifierForall(environment, f) ? Quantifier.FORALL : Quantifier.EXISTS;
            return visitor.visitQuantifier((BooleanFormula) formula, q, getBoundVars(f), body);
        case Z3_SORT_AST:
        case Z3_FUNC_DECL_AST:
        case Z3_UNKNOWN_AST:
        default:
            throw new UnsupportedOperationException("Input should be a formula AST, " + "got unexpected type instead");
    }
}
Also used : BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) Z3FloatingPointFormula(org.sosy_lab.java_smt.solvers.z3.Z3Formula.Z3FloatingPointFormula) RegexFormula(org.sosy_lab.java_smt.api.RegexFormula) StringFormula(org.sosy_lab.java_smt.api.StringFormula) Z3StringFormula(org.sosy_lab.java_smt.solvers.z3.Z3Formula.Z3StringFormula) FloatingPointFormula(org.sosy_lab.java_smt.api.FloatingPointFormula) Z3BooleanFormula(org.sosy_lab.java_smt.solvers.z3.Z3Formula.Z3BooleanFormula) BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) Z3RationalFormula(org.sosy_lab.java_smt.solvers.z3.Z3Formula.Z3RationalFormula) Z3FloatingPointRoundingModeFormula(org.sosy_lab.java_smt.solvers.z3.Z3Formula.Z3FloatingPointRoundingModeFormula) Z3RegexFormula(org.sosy_lab.java_smt.solvers.z3.Z3Formula.Z3RegexFormula) ArrayFormula(org.sosy_lab.java_smt.api.ArrayFormula) Z3IntegerFormula(org.sosy_lab.java_smt.solvers.z3.Z3Formula.Z3IntegerFormula) Z3BitvectorFormula(org.sosy_lab.java_smt.solvers.z3.Z3Formula.Z3BitvectorFormula) Formula(org.sosy_lab.java_smt.api.Formula) Z3ArrayFormula(org.sosy_lab.java_smt.solvers.z3.Z3Formula.Z3ArrayFormula) ImmutableList(com.google.common.collect.ImmutableList) Quantifier(org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier) FormulaType(org.sosy_lab.java_smt.api.FormulaType) ArrayFormulaType(org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) Z3BooleanFormula(org.sosy_lab.java_smt.solvers.z3.Z3Formula.Z3BooleanFormula)

Aggregations

BooleanFormula (org.sosy_lab.java_smt.api.BooleanFormula)7 Formula (org.sosy_lab.java_smt.api.Formula)7 Quantifier (org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier)7 ArrayFormula (org.sosy_lab.java_smt.api.ArrayFormula)5 BitvectorFormula (org.sosy_lab.java_smt.api.BitvectorFormula)5 FloatingPointFormula (org.sosy_lab.java_smt.api.FloatingPointFormula)5 IntegerFormula (org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula)5 StringFormula (org.sosy_lab.java_smt.api.StringFormula)5 Test (org.junit.Test)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 FormulaType (org.sosy_lab.java_smt.api.FormulaType)2 ArrayFormulaType (org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType)2 RegexFormula (org.sosy_lab.java_smt.api.RegexFormula)2 ArrayType (edu.stanford.CVC4.ArrayType)1 BitVectorType (edu.stanford.CVC4.BitVectorType)1 Expr (edu.stanford.CVC4.Expr)1 FunctionType (edu.stanford.CVC4.FunctionType)1