Search in sources :

Example 16 with IntExpression

use of kodkod.ast.IntExpression in project org.alloytools.alloy by AlloyTools.

the class IntTest method test2sComplementUnOps.

private final void test2sComplementUnOps(IntExpression[] vals) {
    final Options options = solver.options();
    final IntRange range = options.integers();
    final int min = range.min(), max = range.max();
    final int mask = ~(-1 << options.bitwidth());
    for (int i = min; i <= max; i++) {
        IntExpression vi = vals[i - min];
        testUnOp(IntOperator.NEG, vi, i, -i, mask);
        testUnOp(IntOperator.NOT, vi, i, ~i, mask);
        testUnOp(IntOperator.ABS, vi, i, Math.abs(i), mask);
        testUnOp(IntOperator.SGN, vi, i, i < 0 ? -1 : i > 0 ? 1 : 0, mask);
    }
}
Also used : Options(kodkod.engine.config.Options) IntExpression(kodkod.ast.IntExpression) IntRange(kodkod.util.ints.IntRange)

Example 17 with IntExpression

use of kodkod.ast.IntExpression in project org.alloytools.alloy by AlloyTools.

the class IntTest method testUnOp.

private final void testUnOp(IntOperator op, IntExpression ei, int i, int result, int mask) {
    final IntExpression e = ei.apply(op);
    final Formula f = ei.eq(constant(i)).and(e.eq(constant(result)));
    final Solution s = solve(f);
    if (overflows(ei, i, result)) {
        assertNull(f.toString(), s.instance());
    } else {
        assertNotNull(f.toString(), s.instance());
        final Evaluator eval = new Evaluator(s.instance(), solver.options());
        assertEquals(result & mask, eval.evaluate(e) & mask);
    }
}
Also used : Formula(kodkod.ast.Formula) IntExpression(kodkod.ast.IntExpression) Evaluator(kodkod.engine.Evaluator) Solution(kodkod.engine.Solution)

Example 18 with IntExpression

use of kodkod.ast.IntExpression in project org.alloytools.alloy by AlloyTools.

the class TranslateAlloyToKodkod method visit_qt.

/**
 * Helper method that translates the quantification expression "op vars | sub"
 */
private Object visit_qt(final ExprQt.Op op, final ConstList<Decl> xvars, final Expr sub) throws Err {
    if (op == ExprQt.Op.NO) {
        return visit_qt(ExprQt.Op.ALL, xvars, sub.not());
    }
    if (op == ExprQt.Op.ONE || op == ExprQt.Op.LONE) {
        boolean ok = true;
        for (int i = 0; i < xvars.size(); i++) {
            Expr v = addOne(xvars.get(i).expr).deNOP();
            if (v.type().arity() != 1 || v.mult() != ExprUnary.Op.ONEOF) {
                ok = false;
                break;
            }
        }
        if (op == ExprQt.Op.ONE && ok)
            return ((Expression) visit_qt(ExprQt.Op.COMPREHENSION, xvars, sub)).one();
        if (op == ExprQt.Op.LONE && ok)
            return ((Expression) visit_qt(ExprQt.Op.COMPREHENSION, xvars, sub)).lone();
    }
    if (op == ExprQt.Op.ONE) {
        Formula f1 = (Formula) visit_qt(ExprQt.Op.LONE, xvars, sub);
        Formula f2 = (Formula) visit_qt(ExprQt.Op.SOME, xvars, sub);
        return f1.and(f2);
    }
    if (op == ExprQt.Op.LONE) {
        QuantifiedFormula p1 = (QuantifiedFormula) visit_qt(ExprQt.Op.ALL, xvars, sub);
        QuantifiedFormula p2 = (QuantifiedFormula) visit_qt(ExprQt.Op.ALL, xvars, sub);
        Decls s1 = p1.decls(), s2 = p2.decls(), decls = null;
        Formula f1 = p1.formula(), f2 = p2.formula();
        Formula[] conjuncts = new Formula[s1.size()];
        for (int i = 0; i < conjuncts.length; i++) {
            kodkod.ast.Decl d1 = s1.get(i), d2 = s2.get(i);
            conjuncts[i] = d1.variable().eq(d2.variable());
            if (decls == null)
                decls = d1.and(d2);
            else
                decls = decls.and(d1).and(d2);
        }
        return f1.and(f2).implies(Formula.and(conjuncts)).forAll(decls);
    }
    Decls dd = null;
    List<Formula> guards = new ArrayList<Formula>();
    for (Decl dep : xvars) {
        final Expr dexexpr = addOne(dep.expr);
        final Expression dv = cset(dexexpr);
        for (ExprHasName dex : dep.names) {
            final Variable v = Variable.nary(skolem(dex.label), dex.type().arity());
            final kodkod.ast.Decl newd;
            env.put((ExprVar) dex, v);
            if (dex.type().arity() != 1) {
                guards.add(isIn(v, dexexpr));
                newd = v.setOf(dv);
            } else
                switch(dexexpr.mult()) {
                    case SETOF:
                        newd = v.setOf(dv);
                        break;
                    case SOMEOF:
                        newd = v.someOf(dv);
                        break;
                    case LONEOF:
                        newd = v.loneOf(dv);
                        break;
                    default:
                        newd = v.oneOf(dv);
                }
            if (frame != null)
                frame.kv2typepos(v, dex.type(), dex.pos);
            if (dd == null)
                dd = newd;
            else
                dd = dd.and(newd);
        }
    }
    final Formula ans = (op == ExprQt.Op.SUM) ? null : cform(sub);
    final IntExpression ians = (op != ExprQt.Op.SUM) ? null : cint(sub);
    for (Decl d : xvars) for (ExprHasName v : d.names) env.remove((ExprVar) v);
    if (op == ExprQt.Op.COMPREHENSION)
        // guards.size()==0, since each var
        return ans.comprehension(dd);
    // has to be unary
    if (op == ExprQt.Op.SUM)
        // guards.size()==0, since each var has to be
        return ians.sum(dd);
    // unary
    if (op == ExprQt.Op.SOME) {
        if (guards.size() == 0)
            return ans.forSome(dd);
        guards.add(ans);
        return Formula.and(guards).forSome(dd);
    } else {
        if (guards.size() == 0)
            return ans.forAll(dd);
        return Formula.and(guards).implies(ans).forAll(dd);
    }
}
Also used : ExprToIntCast(kodkod.ast.ExprToIntCast) IntToExprCast(kodkod.ast.IntToExprCast) Variable(kodkod.ast.Variable) Decls(kodkod.ast.Decls) IntExpression(kodkod.ast.IntExpression) ArrayList(java.util.ArrayList) Decl(edu.mit.csail.sdg.ast.Decl) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) Expr(edu.mit.csail.sdg.ast.Expr) Expression(kodkod.ast.Expression) BinaryExpression(kodkod.ast.BinaryExpression) IntExpression(kodkod.ast.IntExpression) ExprHasName(edu.mit.csail.sdg.ast.ExprHasName) QuantifiedFormula(kodkod.ast.QuantifiedFormula)

Example 19 with IntExpression

use of kodkod.ast.IntExpression in project org.alloytools.alloy by AlloyTools.

the class TranslateAlloyToKodkod method alloy2kodkod.

/**
 * Translate the Alloy expression into an equivalent Kodkod Expression or
 * IntExpression or Formula object.
 *
 * @param sol - an existing satisfiable A4Solution object
 * @param expr - this is the Alloy expression we want to translate
 */
public static Object alloy2kodkod(A4Solution sol, Expr expr) throws Err {
    if (expr.ambiguous && !expr.errors.isEmpty())
        expr = expr.resolve(expr.type(), null);
    if (!expr.errors.isEmpty())
        throw expr.errors.pick();
    TranslateAlloyToKodkod tr = new TranslateAlloyToKodkod(sol.getBitwidth(), sol.unrolls(), sol.a2k(), sol.s2k());
    Object ans;
    try {
        ans = tr.visitThis(expr);
    } catch (UnsatisfiedLinkError ex) {
        throw new ErrorFatal("The required JNI library cannot be found: " + ex.toString().trim());
    } catch (CapacityExceededException ex) {
        throw rethrow(ex);
    } catch (HigherOrderDeclException ex) {
        throw new ErrorType("Analysis cannot be performed since it requires higher-order quantification that could not be skolemized.");
    } catch (Throwable ex) {
        if (ex instanceof Err)
            throw (Err) ex;
        throw new ErrorFatal("Unknown exception occurred: " + ex, ex);
    }
    if ((ans instanceof IntExpression) || (ans instanceof Formula) || (ans instanceof Expression))
        return ans;
    throw new ErrorFatal("Unknown internal error encountered in the evaluator.");
}
Also used : QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) CapacityExceededException(kodkod.engine.CapacityExceededException) ErrorFatal(edu.mit.csail.sdg.alloy4.ErrorFatal) ErrorType(edu.mit.csail.sdg.alloy4.ErrorType) Err(edu.mit.csail.sdg.alloy4.Err) Expression(kodkod.ast.Expression) BinaryExpression(kodkod.ast.BinaryExpression) IntExpression(kodkod.ast.IntExpression) IntExpression(kodkod.ast.IntExpression) HigherOrderDeclException(kodkod.engine.fol2sat.HigherOrderDeclException)

Example 20 with IntExpression

use of kodkod.ast.IntExpression in project org.alloytools.alloy by AlloyTools.

the class AbstractReplacer method visit.

/**
 * Calls lookup(intComp) and returns the cached value, if any. If a replacement
 * has not been cached, visits the formula's children. If nothing changes, the
 * argument is cached and returned, otherwise a replacement formula is cached
 * and returned.
 *
 * @return { c: Formula | [[c]] = intComp.left.accept(delegate) op
 *         intComp.right.accept(delegate) }
 */
@Override
public Formula visit(IntComparisonFormula intComp) {
    Formula ret = lookup(intComp);
    if (ret != null)
        return ret;
    final IntExpression left = intComp.left().accept(delegate);
    final IntExpression right = intComp.right().accept(delegate);
    ret = (left == intComp.left() && right == intComp.right()) ? intComp : left.compare(intComp.op(), right);
    return cache(intComp, ret);
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) MultiplicityFormula(kodkod.ast.MultiplicityFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) ConstantFormula(kodkod.ast.ConstantFormula) NotFormula(kodkod.ast.NotFormula) ComparisonFormula(kodkod.ast.ComparisonFormula) NaryFormula(kodkod.ast.NaryFormula) Formula(kodkod.ast.Formula) FixFormula(kodkod.ast.FixFormula) IntComparisonFormula(kodkod.ast.IntComparisonFormula) BinaryIntExpression(kodkod.ast.BinaryIntExpression) IfIntExpression(kodkod.ast.IfIntExpression) UnaryIntExpression(kodkod.ast.UnaryIntExpression) NaryIntExpression(kodkod.ast.NaryIntExpression) IntExpression(kodkod.ast.IntExpression)

Aggregations

IntExpression (kodkod.ast.IntExpression)32 Formula (kodkod.ast.Formula)16 Expression (kodkod.ast.Expression)13 BinaryIntExpression (kodkod.ast.BinaryIntExpression)11 IfIntExpression (kodkod.ast.IfIntExpression)11 NaryIntExpression (kodkod.ast.NaryIntExpression)11 UnaryIntExpression (kodkod.ast.UnaryIntExpression)11 Decls (kodkod.ast.Decls)10 BinaryExpression (kodkod.ast.BinaryExpression)8 Variable (kodkod.ast.Variable)8 Solution (kodkod.engine.Solution)8 QuantifiedFormula (kodkod.ast.QuantifiedFormula)7 Relation (kodkod.ast.Relation)7 TupleFactory (kodkod.instance.TupleFactory)6 TupleSet (kodkod.instance.TupleSet)6 SumExpression (kodkod.ast.SumExpression)5 Solver (kodkod.engine.Solver)5 Bounds (kodkod.instance.Bounds)5 Universe (kodkod.instance.Universe)5 ConstantExpression (kodkod.ast.ConstantExpression)4