Search in sources :

Example 6 with BinaryExpression

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

the class BookExamples method trial.

/**
 * This tries a particular solution against the formula.
 */
private static Solution trial(A4Reporter rep, TupleFactory fac, Solver solver, Iterable<Sig> sigs, Formula f, A4Solution frame, Object[] t) {
    try {
        frame.kr2typeCLEAR();
        Bounds b = null;
        TupleSet ts = null;
        for (int i = 1; i < t.length; i++) {
            Object x = t[i];
            if (x == null)
                return null;
            if (x instanceof String && ((String) x).length() > 0) {
                // This
                // means
                // it's
                // a
                // unary
                // Tuple
                // containing
                // the
                // given
                // atom
                Tuple xx = fac.tuple((String) x);
                if (ts == null)
                    ts = fac.noneOf(1);
                ts.add(xx);
                continue;
            }
            if (x instanceof Tuple) {
                // This means it's a Tuple
                Tuple xx = (Tuple) x;
                if (ts == null)
                    ts = fac.noneOf(xx.arity());
                ts.add(xx);
                continue;
            }
            if (x instanceof String) {
                // The empty string means the sig
                // name follows here
                i++;
                if (i >= t.length - 1 || !(t[i] instanceof String) || !(t[i + 1] instanceof String))
                    return null;
                String sigName = (String) (t[i]);
                i++;
                String fieldName = (String) (t[i]);
                Sig first = hasSig(sigs, sigName);
                if (first == null)
                    return null;
                Expression expr = null;
                if (fieldName.length() == 0) {
                    expr = frame.a2k(first);
                } else {
                    for (Field field : first.getFields()) if (field.label.equals(fieldName)) {
                        expr = frame.a2k(field);
                        while (expr instanceof BinaryExpression) expr = ((BinaryExpression) expr).right();
                        break;
                    }
                }
                if (!(expr instanceof Relation))
                    return null;
                if (b == null)
                    // We delay the expansive
                    b = frame.getBounds();
                // really find a possible match
                if (ts == null)
                    ts = fac.noneOf(expr.arity());
                if (!ts.containsAll(b.lowerBound((Relation) expr)))
                    // Sanity check
                    return null;
                if (!b.upperBound((Relation) expr).containsAll(ts))
                    // Sanity check
                    return null;
                b.boundExactly((Relation) expr, ts);
                ts = null;
                continue;
            }
        }
        SATFactory sat = solver.options().solver();
        Solution sol;
        try {
            solver.options().setSolver(SATFactory.DefaultSAT4J);
            sol = solver.solve(f, b);
        } finally {
            solver.options().setSolver(sat);
        }
        if (sol == null || (sol.outcome() != SATISFIABLE && sol.outcome() != TRIVIALLY_SATISFIABLE))
            return null;
        if (rep != null)
            rep.debug("Comment: " + t[0] + "\n");
        return sol;
    } catch (Throwable ex) {
        return null;
    }
}
Also used : TupleSet(kodkod.instance.TupleSet) Bounds(kodkod.instance.Bounds) Sig(edu.mit.csail.sdg.ast.Sig) Field(edu.mit.csail.sdg.ast.Sig.Field) Relation(kodkod.ast.Relation) SATFactory(kodkod.engine.satlab.SATFactory) BinaryExpression(kodkod.ast.BinaryExpression) Expression(kodkod.ast.Expression) BinaryExpression(kodkod.ast.BinaryExpression) Tuple(kodkod.instance.Tuple) Solution(kodkod.engine.Solution)

Example 7 with BinaryExpression

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

the class FOL2BoolTranslator method approximate.

/**
 * Translates the given annotated expression into a boolean matrix that is a
 * least sound upper bound on the expression's value, given the leaf and
 * variable bindings in the the provided interpreter and environment.
 *
 * @requires interpreter.relations = AnnotatedNode.relations(annotated)
 * @return a boolean matrix that is a least sound upper bound on the
 *         expression's value
 * @throws HigherOrderDeclException annotated.node contains a higher order
 *             declaration
 * @throws UnboundLeafException annotated.node refers to a variable that neither
 *             declared nor bound in env
 */
static final BooleanMatrix approximate(AnnotatedNode<Expression> annotated, LeafInterpreter interpreter, Environment<BooleanMatrix, Expression> env) {
    final FOL2BoolTranslator approximator = new FOL2BoolTranslator(new FOL2BoolCache(annotated), interpreter, env) {

        @Override
        public final BooleanMatrix visit(BinaryExpression binExpr) {
            final BooleanMatrix ret = lookup(binExpr);
            if (ret != null)
                return ret;
            switch(binExpr.op()) {
                case DIFFERENCE:
                    return cache(binExpr, binExpr.left().accept(this));
                case OVERRIDE:
                    return cache(binExpr, binExpr.left().accept(this).or(binExpr.right().accept(this)));
                default:
                    return super.visit(binExpr);
            }
        }

        @Override
        public final BooleanMatrix visit(Comprehension cexpr) {
            final BooleanMatrix ret = lookup(cexpr);
            return ret != null ? ret : cache(cexpr, super.visit((Comprehension) Formula.TRUE.comprehension(cexpr.decls())));
        }

        @Override
        public BooleanMatrix visit(IfExpression ifExpr) {
            final BooleanMatrix ret = lookup(ifExpr);
            return ret != null ? ret : cache(ifExpr, ifExpr.thenExpr().accept(this).or(ifExpr.elseExpr().accept(this)));
        }

        @Override
        public BooleanMatrix visit(IntToExprCast castExpr) {
            BooleanMatrix ret = lookup(castExpr);
            if (ret != null)
                return ret;
            switch(castExpr.op()) {
                case INTCAST:
                    return cache(castExpr, Expression.INTS.accept(this));
                case BITSETCAST:
                    final BooleanFactory factory = super.interpreter.factory();
                    ret = factory.matrix(Dimensions.square(super.interpreter.universe().size(), 1));
                    final IntSet ints = super.interpreter.ints();
                    final int msb = factory.bitwidth() - 1;
                    // handle all bits but the sign bit
                    for (int i = 0; i < msb; i++) {
                        int pow2 = 1 << i;
                        if (ints.contains(pow2)) {
                            ret.set(super.interpreter.interpret(pow2), BooleanConstant.TRUE);
                        }
                    }
                    // handle the sign bit
                    if (ints.contains(-1 << msb)) {
                        ret.set(super.interpreter.interpret(-1 << msb), BooleanConstant.TRUE);
                    }
                    return cache(castExpr, ret);
                default:
                    throw new IllegalArgumentException("Unknown operator: " + castExpr.op());
            }
        }
    };
    return annotated.node().accept(approximator);
}
Also used : IfExpression(kodkod.ast.IfExpression) BinaryExpression(kodkod.ast.BinaryExpression) IntToExprCast(kodkod.ast.IntToExprCast) IntSet(kodkod.util.ints.IntSet) BooleanMatrix(kodkod.engine.bool.BooleanMatrix) Comprehension(kodkod.ast.Comprehension) BooleanFactory(kodkod.engine.bool.BooleanFactory)

Aggregations

BinaryExpression (kodkod.ast.BinaryExpression)7 Relation (kodkod.ast.Relation)4 Expression (kodkod.ast.Expression)3 IfExpression (kodkod.ast.IfExpression)3 IntExpression (kodkod.ast.IntExpression)3 IntToExprCast (kodkod.ast.IntToExprCast)3 ErrorFatal (edu.mit.csail.sdg.alloy4.ErrorFatal)2 Sig (edu.mit.csail.sdg.ast.Sig)2 BinaryIntExpression (kodkod.ast.BinaryIntExpression)2 Comprehension (kodkod.ast.Comprehension)2 ConstantExpression (kodkod.ast.ConstantExpression)2 IfIntExpression (kodkod.ast.IfIntExpression)2 NaryExpression (kodkod.ast.NaryExpression)2 NaryIntExpression (kodkod.ast.NaryIntExpression)2 ProjectExpression (kodkod.ast.ProjectExpression)2 QuantifiedFormula (kodkod.ast.QuantifiedFormula)2 SumExpression (kodkod.ast.SumExpression)2 UnaryExpression (kodkod.ast.UnaryExpression)2 UnaryIntExpression (kodkod.ast.UnaryIntExpression)2 TupleSet (kodkod.instance.TupleSet)2