Search in sources :

Example 81 with Type

use of com.google.spanner.v1.Type in project java-smt by sosy-lab.

the class CVC4FormulaCreator method convertValue.

@Override
public Object convertValue(Expr expForType, Expr value) {
    final Type type = expForType.getType();
    final Type valueType = value.getType();
    if (value.getKind() == Kind.BOUND_VARIABLE) {
        // CVC4 does not allow model values for bound vars
        return value.toString();
    } else if (valueType.isBoolean()) {
        return value.getConstBoolean();
    } else if (valueType.isInteger() && type.isInteger()) {
        return new BigInteger(value.getConstRational().toString());
    } else if (valueType.isReal() && type.isReal()) {
        Rational rat = value.getConstRational();
        return org.sosy_lab.common.rationals.Rational.of(new BigInteger(rat.getNumerator().toString()), new BigInteger(rat.getDenominator().toString()));
    } else if (valueType.isBitVector()) {
        Integer bv = value.getConstBitVector().getValue();
        if (bv.fitsSignedLong()) {
            return BigInteger.valueOf(bv.getUnsignedLong());
        } else {
            // default
            return value.toString();
        }
    } else if (valueType.isFloatingPoint()) {
        return parseFloatingPoint(value);
    } else if (valueType.isString()) {
        return value.getConstString().toString();
    } else {
        // String serialization for unknown terms.
        return value.toString();
    }
}
Also used : Integer(edu.stanford.CVC4.Integer) UnsignedInteger(com.google.common.primitives.UnsignedInteger) BigInteger(java.math.BigInteger) FloatingPointType(org.sosy_lab.java_smt.api.FormulaType.FloatingPointType) CVC4.vectorType(edu.stanford.CVC4.vectorType) Type(edu.stanford.CVC4.Type) BitVectorType(edu.stanford.CVC4.BitVectorType) FormulaType(org.sosy_lab.java_smt.api.FormulaType) ArrayFormulaType(org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType) ArrayType(edu.stanford.CVC4.ArrayType) FunctionType(edu.stanford.CVC4.FunctionType) Rational(edu.stanford.CVC4.Rational) BigInteger(java.math.BigInteger)

Example 82 with Type

use of com.google.spanner.v1.Type in project java-smt by sosy-lab.

the class CVC4FormulaCreator method makeBoundCopy.

/**
 * Makes a bound copy of a variable for use in quantifier. Note that all occurrences of the free
 * var have to be substituted by the bound once it exists.
 *
 * @param var Variable you want a bound copy of.
 * @return Bound Variable
 */
public Expr makeBoundCopy(Expr var) {
    Type type = var.getType();
    String name = getName(var);
    Expr boundCopy = exprManager.mkBoundVar(name, type);
    return boundCopy;
}
Also used : FloatingPointType(org.sosy_lab.java_smt.api.FormulaType.FloatingPointType) CVC4.vectorType(edu.stanford.CVC4.vectorType) Type(edu.stanford.CVC4.Type) BitVectorType(edu.stanford.CVC4.BitVectorType) FormulaType(org.sosy_lab.java_smt.api.FormulaType) ArrayFormulaType(org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType) ArrayType(edu.stanford.CVC4.ArrayType) FunctionType(edu.stanford.CVC4.FunctionType) CVC4.vectorExpr(edu.stanford.CVC4.vectorExpr) Expr(edu.stanford.CVC4.Expr)

Example 83 with Type

use of com.google.spanner.v1.Type in project java-smt by sosy-lab.

the class CVC4ArrayFormulaManager method internalMakeArray.

@Override
@SuppressWarnings("MethodTypeParameterName")
protected <TI extends Formula, TE extends Formula> Expr internalMakeArray(String pName, FormulaType<TI> pIndexType, FormulaType<TE> pElementType) {
    final ArrayFormulaType<TI, TE> arrayFormulaType = FormulaType.getArrayType(pIndexType, pElementType);
    final Type cvc4ArrayType = toSolverType(arrayFormulaType);
    return getFormulaCreator().makeVariable(cvc4ArrayType, pName);
}
Also used : Type(edu.stanford.CVC4.Type) FormulaType(org.sosy_lab.java_smt.api.FormulaType) ArrayFormulaType(org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType)

Example 84 with Type

use of com.google.spanner.v1.Type in project java-smt by sosy-lab.

the class CVC4FloatingPointFormulaManager method genericCast.

private Expr genericCast(Expr pNumber, FormulaType<?> pTargetType) {
    Type type = pNumber.getType();
    FormulaType<?> argType = getFormulaCreator().getFormulaType(pNumber);
    Expr castFuncDecl = getFormulaCreator().declareUFImpl("__cast_" + argType + "_to_" + pTargetType, toSolverType(pTargetType), ImmutableList.of(type));
    return exprManager.mkExpr(Kind.APPLY_UF, castFuncDecl, pNumber);
}
Also used : FloatingPointType(org.sosy_lab.java_smt.api.FormulaType.FloatingPointType) Type(edu.stanford.CVC4.Type) FormulaType(org.sosy_lab.java_smt.api.FormulaType) BitvectorType(org.sosy_lab.java_smt.api.FormulaType.BitvectorType) Expr(edu.stanford.CVC4.Expr)

Example 85 with Type

use of com.google.spanner.v1.Type in project java-smt by sosy-lab.

the class CVC4FormulaCreator method visit.

@Override
public <R> R visit(FormulaVisitor<R> visitor, Formula formula, final Expr f) {
    checkState(!f.isNull());
    Type type = f.getType();
    if (f.isConst()) {
        if (type.isBoolean()) {
            return visitor.visitConstant(formula, f.getConstBoolean());
        } else if (type.isInteger() || type.isReal()) {
            return visitor.visitConstant(formula, f.getConstRational());
        } else if (type.isBitVector()) {
            // TODO is this correct?
            return visitor.visitConstant(formula, f.getConstBitVector().getValue());
        } else if (type.isFloatingPoint()) {
            // TODO is this correct?
            return visitor.visitConstant(formula, f.getConstFloatingPoint());
        } else if (type.isRoundingMode()) {
            // TODO is this correct?
            return visitor.visitConstant(formula, f.getConstRoundingMode());
        } else if (type.isString()) {
            return visitor.visitConstant(formula, f.getConstString());
        } else {
            throw new UnsupportedOperationException("Unhandled constant " + f + " with type " + type);
        }
    } else if (f.getKind() == Kind.BOUND_VARIABLE) {
        // BOUND vars are used for all vars that are bound to a quantifier in CVC4.
        // We resubstitute them back to the original free.
        // CVC4 doesn't give you the de-brujin index
        Expr originalVar = variablesCache.get(formula.toString());
        return visitor.visitBoundVariable(encapsulate(originalVar), 0);
    } else if (f.getKind() == Kind.FORALL || f.getKind() == Kind.EXISTS) {
        // QUANTIFIER: replace bound variable with free variable for visitation
        assert f.getNumChildren() == 2;
        Expr body = f.getChildren().get(1);
        List<Formula> freeVars = new ArrayList<>();
        for (Expr boundVar : f.getChild(0)) {
            // unpack grand-children of f.
            String name = getName(boundVar);
            Expr freeVar = Preconditions.checkNotNull(variablesCache.get(name));
            body = body.substitute(boundVar, freeVar);
            freeVars.add(encapsulate(freeVar));
        }
        BooleanFormula fBody = encapsulateBoolean(body);
        Quantifier quant = f.getKind() == Kind.EXISTS ? Quantifier.EXISTS : Quantifier.FORALL;
        return visitor.visitQuantifier((BooleanFormula) formula, quant, freeVars, fBody);
    } else if (f.isVariable()) {
        assert f.getKind() != Kind.BOUND_VARIABLE;
        return visitor.visitFreeVariable(formula, getName(f));
    } else {
        // Expressions like uninterpreted function calls (Kind.APPLY_UF) or operators (e.g. Kind.AND).
        // These are all treated like operators, so we can get the declaration by f.getOperator()!
        List<Formula> args = ImmutableList.copyOf(Iterables.transform(f, this::encapsulate));
        List<FormulaType<?>> argsTypes = new ArrayList<>();
        Expr operator = normalize(f.getOperator());
        if (operator.getType().isFunction()) {
            vectorType argTypes = new FunctionType(operator.getType()).getArgTypes();
            for (int i = 0; i < argTypes.size(); i++) {
                argsTypes.add(getFormulaTypeFromTermType(argTypes.get(i)));
            }
        } else {
            for (Expr arg : f) {
                argsTypes.add(getFormulaType(arg));
            }
        }
        checkState(args.size() == argsTypes.size());
        // additional parameters? We do so for some methods of Princess.
        return visitor.visitFunction(formula, args, FunctionDeclarationImpl.of(getName(f), getDeclarationKind(f), argsTypes, getFormulaType(f), operator));
    }
}
Also used : CVC4.vectorType(edu.stanford.CVC4.vectorType) FunctionType(edu.stanford.CVC4.FunctionType) ArrayList(java.util.ArrayList) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) RegexFormula(org.sosy_lab.java_smt.api.RegexFormula) StringFormula(org.sosy_lab.java_smt.api.StringFormula) FloatingPointFormula(org.sosy_lab.java_smt.api.FloatingPointFormula) BitvectorFormula(org.sosy_lab.java_smt.api.BitvectorFormula) CVC4BooleanFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4BooleanFormula) CVC4StringFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4StringFormula) CVC4FloatingPointFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4FloatingPointFormula) ArrayFormula(org.sosy_lab.java_smt.api.ArrayFormula) CVC4IntegerFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4IntegerFormula) CVC4FloatingPointRoundingModeFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4FloatingPointRoundingModeFormula) CVC4RationalFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4RationalFormula) CVC4RegexFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4RegexFormula) CVC4BitvectorFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4BitvectorFormula) Formula(org.sosy_lab.java_smt.api.Formula) CVC4ArrayFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4ArrayFormula) FloatingPointType(org.sosy_lab.java_smt.api.FormulaType.FloatingPointType) CVC4.vectorType(edu.stanford.CVC4.vectorType) Type(edu.stanford.CVC4.Type) BitVectorType(edu.stanford.CVC4.BitVectorType) FormulaType(org.sosy_lab.java_smt.api.FormulaType) ArrayFormulaType(org.sosy_lab.java_smt.api.FormulaType.ArrayFormulaType) ArrayType(edu.stanford.CVC4.ArrayType) FunctionType(edu.stanford.CVC4.FunctionType) CVC4.vectorExpr(edu.stanford.CVC4.vectorExpr) Expr(edu.stanford.CVC4.Expr) Quantifier(org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) BooleanFormula(org.sosy_lab.java_smt.api.BooleanFormula) CVC4BooleanFormula(org.sosy_lab.java_smt.solvers.cvc4.CVC4Formula.CVC4BooleanFormula)

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