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(')');
}
};
}
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);
}
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);
}
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)"));
}
}
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);
}
}
Aggregations