Search in sources :

Example 1 with Formula

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

the class FormulaTransformationVisitorImpl method visitFunction.

@Override
public Void visitFunction(Formula f, List<Formula> args, FunctionDeclaration<?> functionDeclaration) {
    Preconditions.checkNotNull(f);
    boolean allArgumentsTransformed = true;
    // Construct a new argument list for the function application.
    List<Formula> newArgs = new ArrayList<>(args.size());
    for (Formula c : args) {
        Formula newC = pCache.get(c);
        if (newC != null) {
            newArgs.add(newC);
        } else {
            toProcess.push(c);
            allArgumentsTransformed = false;
        }
    }
    // of the function were already processed.
    if (allArgumentsTransformed) {
        // Create a processed version of the function application.
        if (!toProcess.isEmpty()) {
            toProcess.pop();
        }
        Formula out = delegate.visitFunction(f, newArgs, functionDeclaration);
        Formula prev = pCache.put(f, out);
        assert prev == null;
    }
    return null;
}
Also used : Formula(org.sosy_lab.java_smt.api.Formula) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) ArrayList(java.util.ArrayList)

Example 2 with Formula

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

the class Yices2FormulaCreator method visitFunctionApplication.

private <R> R visitFunctionApplication(FormulaVisitor<R> pVisitor, Formula pFormula, int pF, final int constructor) {
    // Map built-in constructors in negative int to avoid collision with UFs.
    int functionDeclaration = -constructor;
    assert !CONSTANT_AND_VARIABLE_CONSTRUCTORS.contains(constructor) : String.format("Term %s with constructor %d should be handled somewhere else", yices_term_to_string(pF), constructor);
    // filled later, except for some special function applications
    String functionName = null;
    List<Integer> functionArgs = null;
    // filled directly when handling the function application
    final FunctionDeclarationKind functionKind;
    switch(constructor) {
        case YICES_ITE_TERM:
            functionKind = FunctionDeclarationKind.ITE;
            break;
        case YICES_APP_TERM:
            functionKind = FunctionDeclarationKind.UF;
            functionArgs = getArgs(pF);
            functionName = yices_term_to_string(functionArgs.get(0));
            functionDeclaration = functionArgs.get(0);
            functionArgs.remove(0);
            break;
        case YICES_EQ_TERM:
            // Covers all equivalences
            functionKind = FunctionDeclarationKind.EQ;
            break;
        case YICES_NOT_TERM:
            if (isNestedConjunction(pF)) {
                functionKind = FunctionDeclarationKind.AND;
                functionArgs = getNestedConjunctionArgs(pF);
                functionDeclaration = -YICES_AND;
            } else {
                functionKind = FunctionDeclarationKind.NOT;
            }
            break;
        case YICES_OR_TERM:
            functionKind = FunctionDeclarationKind.OR;
            break;
        case YICES_XOR_TERM:
            functionKind = FunctionDeclarationKind.XOR;
            break;
        case YICES_BV_DIV:
            functionKind = FunctionDeclarationKind.BV_UDIV;
            break;
        case YICES_BV_REM:
            functionKind = FunctionDeclarationKind.BV_UREM;
            break;
        case YICES_BV_SDIV:
            functionKind = FunctionDeclarationKind.BV_SDIV;
            break;
        case YICES_BV_SREM:
            functionKind = FunctionDeclarationKind.BV_SREM;
            break;
        case YICES_BV_SHL:
            functionKind = FunctionDeclarationKind.BV_SHL;
            break;
        case YICES_BV_LSHR:
            functionKind = FunctionDeclarationKind.BV_LSHR;
            break;
        case YICES_BV_ASHR:
            functionKind = FunctionDeclarationKind.BV_ASHR;
            break;
        case YICES_BV_GE_ATOM:
            functionKind = FunctionDeclarationKind.BV_UGE;
            break;
        case YICES_BV_SGE_ATOM:
            functionKind = FunctionDeclarationKind.BV_SGE;
            break;
        case YICES_ARITH_GE_ATOM:
            functionKind = FunctionDeclarationKind.GTE;
            break;
        case YICES_FLOOR:
            functionKind = FunctionDeclarationKind.FLOOR;
            break;
        case YICES_RDIV:
            functionKind = FunctionDeclarationKind.DIV;
            break;
        case YICES_IDIV:
            functionKind = FunctionDeclarationKind.DIV;
            break;
        case YICES_SELECT_TERM:
            functionKind = FunctionDeclarationKind.SELECT;
            break;
        case YICES_BV_SUM:
            if (yices_term_num_children(pF) == 1) {
                functionKind = FunctionDeclarationKind.BV_MUL;
                functionArgs = getMultiplyBvSumArgsFromSum(pF);
                functionDeclaration = -YICES_BV_MUL;
            } else {
                functionKind = FunctionDeclarationKind.BV_ADD;
                functionArgs = getBvSumArgs(pF);
            }
            break;
        case YICES_ARITH_SUM:
            if (yices_term_num_children(pF) == 1) {
                functionKind = FunctionDeclarationKind.MUL;
                functionArgs = getMultiplySumArgsFromSum(pF);
                functionDeclaration = -YICES_POWER_PRODUCT;
            } else {
                functionKind = FunctionDeclarationKind.ADD;
                functionArgs = getSumArgs(pF);
            }
            break;
        case YICES_POWER_PRODUCT:
            if (yices_type_is_bitvector(yices_type_of_term(pF))) {
                functionKind = FunctionDeclarationKind.BV_MUL;
                functionArgs = getMultiplyArgs(pF, true);
                functionDeclaration = -YICES_BV_MUL;
            // TODO Product of more then 2 bitvectors ?
            } else {
                functionKind = FunctionDeclarationKind.MUL;
                functionArgs = getMultiplyArgs(pF, false);
            }
            break;
        case YICES_BIT_TERM:
            functionKind = FunctionDeclarationKind.BV_EXTRACT;
            functionArgs = getBitArgs(pF);
            break;
        case YICES_BV_ARRAY:
            functionKind = FunctionDeclarationKind.BV_CONCAT;
            break;
        default:
            functionKind = FunctionDeclarationKind.OTHER;
    }
    if (functionName == null) {
        functionName = functionKind.toString();
    }
    if (functionArgs == null) {
        functionArgs = getArgs(pF);
    }
    final ImmutableList<FormulaType<?>> argTypes = ImmutableList.copyOf(toType(functionArgs));
    Preconditions.checkState(functionArgs.size() == argTypes.size(), "different size of args (%s) and their types (%s) in term %s", functionArgs, argTypes, pFormula);
    final ImmutableList.Builder<Formula> argsBuilder = ImmutableList.builder();
    for (int i = 0; i < functionArgs.size(); i++) {
        argsBuilder.add(encapsulate(argTypes.get(i), functionArgs.get(i)));
    }
    final ImmutableList<Formula> args = argsBuilder.build();
    return pVisitor.visitFunction(pFormula, args, FunctionDeclarationImpl.of(functionName, functionKind, argTypes, getFormulaType(pF), functionDeclaration));
}
Also used : BigInteger(java.math.BigInteger) Yices2RationalFormula(org.sosy_lab.java_smt.solvers.yices2.Yices2Formula.Yices2RationalFormula) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) Yices2IntegerFormula(org.sosy_lab.java_smt.solvers.yices2.Yices2Formula.Yices2IntegerFormula) Yices2BitvectorFormula(org.sosy_lab.java_smt.solvers.yices2.Yices2Formula.Yices2BitvectorFormula) Formula(org.sosy_lab.java_smt.api.Formula) Yices2BooleanFormula(org.sosy_lab.java_smt.solvers.yices2.Yices2Formula.Yices2BooleanFormula) ImmutableList(com.google.common.collect.ImmutableList) FunctionDeclarationKind(org.sosy_lab.java_smt.api.FunctionDeclarationKind) FormulaType(org.sosy_lab.java_smt.api.FormulaType) Yices2NativeApi.yices_term_is_int(org.sosy_lab.java_smt.solvers.yices2.Yices2NativeApi.yices_term_is_int)

Example 3 with Formula

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

the class SolverVisitorTest method testVisitingTrue.

@Test
public void testVisitingTrue() {
    // Check that "true" is correctly treated as a constant.
    BooleanFormula t = bmgr.makeBoolean(true);
    final List<Boolean> containsTrue = new ArrayList<>();
    mgr.visitRecursively(t, new DefaultFormulaVisitor<>() {

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

        @Override
        public TraversalProcess visitConstant(Formula f, Object o) {
            if (f.equals(bmgr.makeBoolean(true))) {
                containsTrue.add(true);
            }
            return TraversalProcess.CONTINUE;
        }
    });
    assertThat(containsTrue).isNotEmpty();
}
Also used : BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) RegexFormula(org.sosy_lab.java_smt.api.RegexFormula) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) StringFormula(org.sosy_lab.java_smt.api.StringFormula) FloatingPointFormula(org.sosy_lab.java_smt.api.FloatingPointFormula) BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) Formula(org.sosy_lab.java_smt.api.Formula) ArrayList(java.util.ArrayList) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) TraversalProcess(org.sosy_lab.java_smt.api.visitors.TraversalProcess) Test(org.junit.Test)

Example 4 with Formula

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

the class SolverVisitorTest method extractionTest1.

@Test
public void extractionTest1() {
    IntegerFormula v = imgr.makeVariable("v");
    BooleanFormula q = fmgr.declareAndCallUF("q", FormulaType.BooleanType, v);
    Map<String, Formula> mapping = mgr.extractVariablesAndUFs(q);
    assertThat(mapping).hasSize(2);
    assertThat(mapping).containsEntry("v", v);
    assertThat(mapping).containsEntry("q", q);
}
Also used : BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) RegexFormula(org.sosy_lab.java_smt.api.RegexFormula) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) StringFormula(org.sosy_lab.java_smt.api.StringFormula) FloatingPointFormula(org.sosy_lab.java_smt.api.FloatingPointFormula) BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) Formula(org.sosy_lab.java_smt.api.Formula) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) Test(org.junit.Test)

Example 5 with Formula

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

the class SolverVisitorTest method extractionDeclarations.

@Test
public void extractionDeclarations() {
    requireIntegers();
    // Create the variables and uf
    IntegerFormula a = imgr.makeVariable("a");
    IntegerFormula b = imgr.makeVariable("b");
    IntegerFormula ab = imgr.add(a, b);
    BooleanFormula uf1 = fmgr.declareAndCallUF("testFunc", FormulaType.BooleanType, a, b, ab);
    BooleanFormula uf2 = fmgr.declareAndCallUF("testFunc", FormulaType.BooleanType, ab, b, a);
    BooleanFormula f = bmgr.and(uf1, uf2);
    final Collection<Formula> usedArgs = new LinkedHashSet<>();
    final List<FunctionDeclaration<?>> usedDecls = new ArrayList<>();
    FormulaVisitor<TraversalProcess> argCollectingVisitor = new DefaultFormulaVisitor<>() {

        @Override
        public TraversalProcess visitFunction(Formula pF, List<Formula> args, FunctionDeclaration<?> pFunctionDeclaration) {
            usedArgs.addAll(args);
            usedDecls.add(pFunctionDeclaration);
            return visitDefault(pF);
        }

        @Override
        protected TraversalProcess visitDefault(Formula pF) {
            return TraversalProcess.CONTINUE;
        }
    };
    mgr.visitRecursively(f, argCollectingVisitor);
    // check general stuff about variables, copied from above
    assertThat(usedArgs).hasSize(5);
    assertThat(usedArgs).containsExactly(uf1, uf2, a, b, ab);
    Map<String, Formula> vars = mgr.extractVariables(f);
    assertThat(vars).hasSize(2);
    assertThat(vars.keySet()).containsExactly("a", "b");
    Map<String, Formula> varsUfs = mgr.extractVariablesAndUFs(f);
    assertThat(varsUfs).hasSize(3);
    assertThat(varsUfs.keySet()).containsExactly("a", "b", "testFunc");
    // check correct traversal order of the functions
    assertThat(usedDecls).hasSize(4);
    assertThat(usedDecls.get(0).getKind()).isEqualTo(FunctionDeclarationKind.AND);
    assertThat(usedDecls.get(1).getName()).isEqualTo("testFunc");
    assertThat(usedDecls.get(2).getKind()).isEqualTo(FunctionDeclarationKind.ADD);
    assertThat(usedDecls.get(3).getName()).isEqualTo("testFunc");
    // check UF-equality. This check went wrong in CVC4 and was fixed.
    assertThat(usedDecls.get(1)).isEqualTo(usedDecls.get(3));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) ArrayList(java.util.ArrayList) TraversalProcess(org.sosy_lab.java_smt.api.visitors.TraversalProcess) DefaultFormulaVisitor(org.sosy_lab.java_smt.api.visitors.DefaultFormulaVisitor) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) RegexFormula(org.sosy_lab.java_smt.api.RegexFormula) IntegerFormula(org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula) StringFormula(org.sosy_lab.java_smt.api.StringFormula) FloatingPointFormula(org.sosy_lab.java_smt.api.FloatingPointFormula) BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) Formula(org.sosy_lab.java_smt.api.Formula) FunctionDeclaration(org.sosy_lab.java_smt.api.FunctionDeclaration) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) Test(org.junit.Test)

Aggregations

Formula (org.sosy_lab.java_smt.api.Formula)51 BooleanFormula (org.sosy_lab.java_smt.api.BooleanFormula)39 Test (org.junit.Test)38 IntegerFormula (org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula)37 BitvectorFormula (org.sosy_lab.java_smt.api.BitvectorFormula)24 FloatingPointFormula (org.sosy_lab.java_smt.api.FloatingPointFormula)22 StringFormula (org.sosy_lab.java_smt.api.StringFormula)20 RegexFormula (org.sosy_lab.java_smt.api.RegexFormula)16 ArrayFormula (org.sosy_lab.java_smt.api.ArrayFormula)12 ArrayList (java.util.ArrayList)10 RationalFormula (org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula)10 ImmutableList (com.google.common.collect.ImmutableList)7 FormulaType (org.sosy_lab.java_smt.api.FormulaType)7 Quantifier (org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier)7 TraversalProcess (org.sosy_lab.java_smt.api.visitors.TraversalProcess)7 List (java.util.List)5 ArrayFormulaType (org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType)5 FunctionDeclaration (org.sosy_lab.java_smt.api.FunctionDeclaration)4 LinkedHashSet (java.util.LinkedHashSet)3 BooleanFormulaTransformationVisitor (org.sosy_lab.java_smt.api.visitors.BooleanFormulaTransformationVisitor)3