Search in sources :

Example 31 with Type

use of com.google.spanner.v1.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.spanner.v1.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.spanner.v1.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.spanner.v1.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.spanner.v1.Type in project google-cloud-java by GoogleCloudPlatform.

the class Type method fromProto.

static Type fromProto(com.google.spanner.v1.Type proto) {
    Code type = Code.fromProtoCode(proto.getCode());
    switch(type) {
        case BOOL:
            return bool();
        case INT64:
            return int64();
        case FLOAT64:
            return float64();
        case STRING:
            return string();
        case BYTES:
            return bytes();
        case TIMESTAMP:
            return timestamp();
        case DATE:
            return date();
        case ARRAY:
            checkArgument(proto.hasArrayElementType(), "Missing expected 'array_element_type' field in 'Type' message: %s", proto);
            Type elementType;
            try {
                elementType = fromProto(proto.getArrayElementType());
            } catch (IllegalArgumentException e) {
                throw new IllegalArgumentException("Could not parse 'array_element_type' attribute in 'Type' message: " + proto, e);
            }
            return array(elementType);
        case STRUCT:
            checkArgument(proto.hasStructType(), "Missing expected 'struct_type' field in 'Type' message: %s", proto);
            List<StructField> fields = new ArrayList<>(proto.getStructType().getFieldsCount());
            for (com.google.spanner.v1.StructType.Field field : proto.getStructType().getFieldsList()) {
                checkArgument(field.hasType(), "Missing expected 'type' attribute in 'Field': %s", proto);
                // Names may be empty; for example, the name of the column returned by "SELECT 1".
                String name = Strings.nullToEmpty(field.getName());
                fields.add(StructField.of(name, fromProto(field.getType())));
            }
            return struct(fields);
        default:
            throw new AssertionError("Unimplemented case: " + type);
    }
}
Also used : ArrayList(java.util.ArrayList) TypeCode(com.google.spanner.v1.TypeCode)

Aggregations

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