Search in sources :

Example 31 with Type

use of com.google.api.expr.v1alpha1.Type in project java-smt by sosy-lab.

the class CVC4FormulaManager method dumpFormula.

@Override
public Appender dumpFormula(Expr f) {
    assert getFormulaCreator().getFormulaType(f) == FormulaType.BooleanType : "Only BooleanFormulas may be dumped";
    return new Appenders.AbstractAppender() {

        @Override
        public void appendTo(Appendable out) throws IOException {
            // get all symbols
            final Map<String, Expr> allVars = new LinkedHashMap<>();
            creator.extractVariablesAndUFs(f, true, allVars::put);
            // print all symbols
            for (Map.Entry<String, Expr> entry : allVars.entrySet()) {
                String name = entry.getKey();
                Expr var = entry.getValue();
                // escaping is stolen from SMTInterpol, lets hope this remains consistent
                out.append("(declare-fun ").append(PrintTerm.quoteIdentifier(name)).append(" (");
                // add function parameters
                Iterable<Type> childrenTypes = Iterables.transform(var, Expr::getType);
                out.append(Joiner.on(" ").join(childrenTypes));
                // and return type
                out.append(") ").append(var.getType().toString()).append(")\n");
            }
            // now add the final assert
            out.append("(assert ");
            // f.toStream() uses LET-expressions and is exactly what we want.
            try (OutputStream stream = new OutputStream() {

                @Override
                public void write(int chr) throws IOException {
                    out.append((char) chr);
                }
            }) {
                f.toStream(stream);
            }
            out.append(')');
        }
    };
}
Also used : Type(edu.stanford.CVC4.Type) FormulaType(org.sosy_lab.java_smt.api.FormulaType) Expr(edu.stanford.CVC4.Expr) OutputStream(java.io.OutputStream) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 32 with Type

use of com.google.api.expr.v1alpha1.Type in project java-smt by sosy-lab.

the class CVC4NativeAPITest method checkLIAUfsUnsat.

@Test
public void checkLIAUfsUnsat() {
    // 0 <= f(x)
    // 0 <= f(y)
    // f(x) + f(y) = x
    // f(x) + f(y) = y
    // f(x) = x + 1
    // f(y) = y - 1
    Expr zero = exprMgr.mkConst(new Rational(0));
    Expr one = exprMgr.mkConst(new Rational(1));
    Type intType = exprMgr.integerType();
    // Type for UFs later
    Type intToInt = exprMgr.mkFunctionType(intType, intType);
    Expr xInt = exprMgr.mkVar("x", intType);
    Expr yInt = exprMgr.mkVar("y", intType);
    // declare UFs
    Expr f = exprMgr.mkVar("f", intToInt);
    // Apply UFs
    Expr fx = exprMgr.mkExpr(Kind.APPLY_UF, f, xInt);
    Expr fy = exprMgr.mkExpr(Kind.APPLY_UF, f, yInt);
    Expr plus = exprMgr.mkExpr(Kind.PLUS, fx, fy);
    // Make some assumptions
    Expr assumptions1 = exprMgr.mkExpr(Kind.AND, exprMgr.mkExpr(Kind.LEQ, zero, fx), exprMgr.mkExpr(Kind.LEQ, zero, fy));
    Expr assumptions2 = exprMgr.mkExpr(Kind.AND, exprMgr.mkExpr(Kind.EQUAL, fx, exprMgr.mkExpr(Kind.PLUS, xInt, one)), exprMgr.mkExpr(Kind.EQUAL, fy, exprMgr.mkExpr(Kind.MINUS, yInt, one)), exprMgr.mkExpr(Kind.EQUAL, plus, xInt), exprMgr.mkExpr(Kind.EQUAL, plus, yInt));
    smtEngine.assertFormula(assumptions1);
    smtEngine.assertFormula(assumptions2);
    Result satCheck = smtEngine.checkSat();
    assertThat(satCheck.isSat()).isEqualTo(Sat.UNSAT);
}
Also used : Type(edu.stanford.CVC4.Type) SortType(edu.stanford.CVC4.SortType) ArrayType(edu.stanford.CVC4.ArrayType) BitVectorType(edu.stanford.CVC4.BitVectorType) CVC4.vectorExpr(edu.stanford.CVC4.vectorExpr) SExpr(edu.stanford.CVC4.SExpr) Expr(edu.stanford.CVC4.Expr) Rational(edu.stanford.CVC4.Rational) Result(edu.stanford.CVC4.Result) Test(org.junit.Test)

Example 33 with Type

use of com.google.api.expr.v1alpha1.Type in project java-smt by sosy-lab.

the class CVC4NativeAPITest method checkBooleanUFDeclaration.

@Test
public void checkBooleanUFDeclaration() {
    Type boolType = exprMgr.booleanType();
    Type intType = exprMgr.integerType();
    // arg is bool, return is int
    Type ufType = exprMgr.mkFunctionType(boolType, intType);
    Expr uf = exprMgr.mkVar("fun_bi", ufType);
    Expr ufTrue = exprMgr.mkExpr(uf, exprMgr.mkConst(true));
    Expr ufFalse = exprMgr.mkExpr(uf, exprMgr.mkConst(false));
    Expr assumptions = exprMgr.mkExpr(Kind.NOT, exprMgr.mkExpr(Kind.EQUAL, ufTrue, ufFalse));
    smtEngine.assertFormula(assumptions);
    Result satCheck = smtEngine.checkSat();
    assertThat(satCheck.isSat()).isEqualTo(Sat.SAT);
}
Also used : Type(edu.stanford.CVC4.Type) SortType(edu.stanford.CVC4.SortType) ArrayType(edu.stanford.CVC4.ArrayType) BitVectorType(edu.stanford.CVC4.BitVectorType) CVC4.vectorExpr(edu.stanford.CVC4.vectorExpr) SExpr(edu.stanford.CVC4.SExpr) Expr(edu.stanford.CVC4.Expr) Result(edu.stanford.CVC4.Result) Test(org.junit.Test)

Example 34 with Type

use of com.google.api.expr.v1alpha1.Type in project java-smt by sosy-lab.

the class CVC4NativeAPITest method checkUnsatCore.

@Test
public void checkUnsatCore() {
    // (a & b) & (not(a OR b))
    // Enable UNSAT Core first!
    smtEngine.setOption("produce-unsat-cores", new SExpr(true));
    Type boolType = exprMgr.booleanType();
    Expr a = exprMgr.mkVar("a", boolType);
    Expr b = exprMgr.mkVar("b", boolType);
    Expr aAndb = exprMgr.mkExpr(Kind.AND, a, b);
    Expr notaOrb = exprMgr.mkExpr(Kind.NOT, exprMgr.mkExpr(Kind.OR, a, b));
    smtEngine.assertFormula(aAndb);
    smtEngine.assertFormula(notaOrb);
    Result satCheck = smtEngine.checkSat();
    assertThat(satCheck.isSat()).isEqualTo(Sat.UNSAT);
    UnsatCore unsatCore = smtEngine.getUnsatCore();
    // UnsatCores are iterable
    for (Expr e : unsatCore) {
        assertThat(e.toString()).isIn(Arrays.asList("(not (or a b))", "(and a b)"));
    }
}
Also used : Type(edu.stanford.CVC4.Type) SortType(edu.stanford.CVC4.SortType) ArrayType(edu.stanford.CVC4.ArrayType) BitVectorType(edu.stanford.CVC4.BitVectorType) UnsatCore(edu.stanford.CVC4.UnsatCore) CVC4.vectorExpr(edu.stanford.CVC4.vectorExpr) SExpr(edu.stanford.CVC4.SExpr) Expr(edu.stanford.CVC4.Expr) SExpr(edu.stanford.CVC4.SExpr) Result(edu.stanford.CVC4.Result) Test(org.junit.Test)

Example 35 with Type

use of com.google.api.expr.v1alpha1.Type in project tomee by apache.

the class DynamicSubclass method visitConstructor.

private static MethodVisitor visitConstructor(final ClassWriter cw, final String proxyClassFileName, final String classFileName, final Constructor<?> constructor) {
    final String descriptor = Type.getConstructorDescriptor(constructor);
    final String[] exceptions = new String[constructor.getExceptionTypes().length];
    for (int i = 0; i < exceptions.length; i++) {
        exceptions[i] = Type.getInternalName(constructor.getExceptionTypes()[i]);
    }
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", descriptor, null, exceptions);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    int index = 1;
    for (final Type type : Type.getArgumentTypes(descriptor)) {
        mv.visitVarInsn(type.getOpcode(ILOAD), index);
        index += size(type);
    }
    mv.visitMethodInsn(INVOKESPECIAL, classFileName, "<init>", descriptor, false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(PUTFIELD, proxyClassFileName, "this$handler", "Ljava/lang/reflect/InvocationHandler;");
    mv.visitInsn(RETURN);
    mv.visitMaxs(2, 1);
    return mv;
}
Also used : Type(org.apache.xbean.asm9.Type) MethodVisitor(org.apache.xbean.asm9.MethodVisitor)

Aggregations

Type (com.google.api.expr.v1alpha1.Type)30 Test (org.junit.Test)16 MapType (com.google.api.expr.v1alpha1.Type.MapType)15 Type (edu.stanford.CVC4.Type)14 Type (com.google.spanner.v1.Type)12 Test (org.junit.jupiter.api.Test)12 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)11 ByteString (com.google.protobuf.ByteString)11 ArrayType (edu.stanford.CVC4.ArrayType)11 BitVectorType (edu.stanford.CVC4.BitVectorType)11 Expr (edu.stanford.CVC4.Expr)11 ArrayList (java.util.ArrayList)11 Type (org.apache.xbean.asm9.Type)10 Expr (com.google.api.expr.v1alpha1.Expr)9 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)9 FieldType (org.projectnessie.cel.common.types.ref.FieldType)8 ListValue (com.google.protobuf.ListValue)7 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)7 CheckerEnv.dynElementType (org.projectnessie.cel.checker.CheckerEnv.dynElementType)7 CheckerEnv.getObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType)7