Search in sources :

Example 1 with IfExpression

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

the class TranslateKodkodToJava method countHeight.

/**
 * Count the height of the given Kodkod AST tree.
 */
public static int countHeight(Node node) {
    ReturnVisitor<Integer, Integer, Integer, Integer> vis = new ReturnVisitor<Integer, Integer, Integer, Integer>() {

        private int max(int a, int b) {
            return (a >= b) ? a : b;
        }

        private int max(int a, int b, int c) {
            return (a >= b) ? (a >= c ? a : c) : (b >= c ? b : c);
        }

        @Override
        public Integer visit(Relation x) {
            return 1;
        }

        @Override
        public Integer visit(IntConstant x) {
            return 1;
        }

        @Override
        public Integer visit(ConstantFormula x) {
            return 1;
        }

        @Override
        public Integer visit(Variable x) {
            return 1;
        }

        @Override
        public Integer visit(ConstantExpression x) {
            return 1;
        }

        @Override
        public Integer visit(NotFormula x) {
            return 1 + x.formula().accept(this);
        }

        @Override
        public Integer visit(IntToExprCast x) {
            return 1 + x.intExpr().accept(this);
        }

        @Override
        public Integer visit(Decl x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(ExprToIntCast x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(UnaryExpression x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(UnaryIntExpression x) {
            return 1 + x.intExpr().accept(this);
        }

        @Override
        public Integer visit(MultiplicityFormula x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(BinaryExpression x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(ComparisonFormula x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(BinaryFormula x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(BinaryIntExpression x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(IntComparisonFormula x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(IfExpression x) {
            return 1 + max(x.condition().accept(this), x.thenExpr().accept(this), x.elseExpr().accept(this));
        }

        @Override
        public Integer visit(IfIntExpression x) {
            return 1 + max(x.condition().accept(this), x.thenExpr().accept(this), x.elseExpr().accept(this));
        }

        @Override
        public Integer visit(SumExpression x) {
            return 1 + max(x.decls().accept(this), x.intExpr().accept(this));
        }

        @Override
        public Integer visit(QuantifiedFormula x) {
            return 1 + max(x.decls().accept(this), x.formula().accept(this));
        }

        @Override
        public Integer visit(FixFormula x) {
            return 1 + max(x.condition().accept(this), x.formula().accept(this));
        }

        @Override
        public Integer visit(Comprehension x) {
            return 1 + max(x.decls().accept(this), x.formula().accept(this));
        }

        @Override
        public Integer visit(Decls x) {
            int max = 0, n = x.size();
            for (int i = 0; i < n; i++) max = max(max, x.get(i).accept(this));
            return max;
        }

        @Override
        public Integer visit(ProjectExpression x) {
            int max = x.expression().accept(this);
            for (Iterator<IntExpression> t = x.columns(); t.hasNext(); ) {
                max = max(max, t.next().accept(this));
            }
            return max;
        }

        @Override
        public Integer visit(RelationPredicate x) {
            if (x instanceof Function) {
                Function f = ((Function) x);
                return max(f.domain().accept(this), f.range().accept(this));
            }
            return 1;
        }

        @Override
        public Integer visit(NaryExpression x) {
            int max = 0;
            for (int m = 0, n = x.size(), i = 0; i < n; i++) {
                m = x.child(i).accept(this);
                if (i == 0 || max < m)
                    max = m;
            }
            return max + 1;
        }

        @Override
        public Integer visit(NaryIntExpression x) {
            int max = 0;
            for (int m = 0, n = x.size(), i = 0; i < n; i++) {
                m = x.child(i).accept(this);
                if (i == 0 || max < m)
                    max = m;
            }
            return max + 1;
        }

        @Override
        public Integer visit(NaryFormula x) {
            int max = 0;
            for (int m = 0, n = x.size(), i = 0; i < n; i++) {
                m = x.child(i).accept(this);
                if (i == 0 || max < m)
                    max = m;
            }
            return max + 1;
        }
    };
    Object ans = node.accept(vis);
    if (ans instanceof Integer)
        return ((Integer) ans).intValue();
    else
        return 0;
}
Also used : Variable(kodkod.ast.Variable) UnaryIntExpression(kodkod.ast.UnaryIntExpression) Decls(kodkod.ast.Decls) ConstantExpression(kodkod.ast.ConstantExpression) ComparisonFormula(kodkod.ast.ComparisonFormula) IntComparisonFormula(kodkod.ast.IntComparisonFormula) UnaryExpression(kodkod.ast.UnaryExpression) Function(kodkod.ast.RelationPredicate.Function) Relation(kodkod.ast.Relation) BinaryExpression(kodkod.ast.BinaryExpression) IntConstant(kodkod.ast.IntConstant) NaryIntExpression(kodkod.ast.NaryIntExpression) NotFormula(kodkod.ast.NotFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) IfExpression(kodkod.ast.IfExpression) IntToExprCast(kodkod.ast.IntToExprCast) ProjectExpression(kodkod.ast.ProjectExpression) IfIntExpression(kodkod.ast.IfIntExpression) IfIntExpression(kodkod.ast.IfIntExpression) UnaryIntExpression(kodkod.ast.UnaryIntExpression) NaryIntExpression(kodkod.ast.NaryIntExpression) BinaryIntExpression(kodkod.ast.BinaryIntExpression) IntExpression(kodkod.ast.IntExpression) RelationPredicate(kodkod.ast.RelationPredicate) NaryExpression(kodkod.ast.NaryExpression) BinaryIntExpression(kodkod.ast.BinaryIntExpression) Decl(kodkod.ast.Decl) ConstantFormula(kodkod.ast.ConstantFormula) Comprehension(kodkod.ast.Comprehension) NaryFormula(kodkod.ast.NaryFormula) ExprToIntCast(kodkod.ast.ExprToIntCast) MultiplicityFormula(kodkod.ast.MultiplicityFormula) BinaryFormula(kodkod.ast.BinaryFormula) SumExpression(kodkod.ast.SumExpression) FixFormula(kodkod.ast.FixFormula) ReturnVisitor(kodkod.ast.visitor.ReturnVisitor) IntComparisonFormula(kodkod.ast.IntComparisonFormula)

Example 2 with IfExpression

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

the class AbstractReplacer method visit.

/**
 * Calls lookup(ifExpr) and returns the cached value, if any. If a replacement
 * has not been cached, visits the expression's children. If nothing changes,
 * the argument is cached and returned, otherwise a replacement expression is
 * cached and returned.
 *
 * @return { i: IfExpression | i.condition = ifExpr.condition.accept(delegate)
 *         && i.thenExpr = ifExpr.thenExpr.accept(delegate) && i.elseExpr =
 *         ifExpr.elseExpr.accept(delegate) }
 */
@Override
public Expression visit(IfExpression ifExpr) {
    Expression ret = lookup(ifExpr);
    if (ret != null)
        return ret;
    final Formula condition = ifExpr.condition().accept(delegate);
    final Expression thenExpr = ifExpr.thenExpr().accept(delegate);
    final Expression elseExpr = ifExpr.elseExpr().accept(delegate);
    ret = (condition == ifExpr.condition() && thenExpr == ifExpr.thenExpr() && elseExpr == ifExpr.elseExpr()) ? ifExpr : condition.thenElse(thenExpr, elseExpr);
    return cache(ifExpr, 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) ProjectExpression(kodkod.ast.ProjectExpression) BinaryIntExpression(kodkod.ast.BinaryIntExpression) SumExpression(kodkod.ast.SumExpression) IfIntExpression(kodkod.ast.IfIntExpression) BinaryExpression(kodkod.ast.BinaryExpression) ConstantExpression(kodkod.ast.ConstantExpression) UnaryIntExpression(kodkod.ast.UnaryIntExpression) NaryIntExpression(kodkod.ast.NaryIntExpression) IntExpression(kodkod.ast.IntExpression) Expression(kodkod.ast.Expression) UnaryExpression(kodkod.ast.UnaryExpression) NaryExpression(kodkod.ast.NaryExpression) IfExpression(kodkod.ast.IfExpression)

Example 3 with IfExpression

use of kodkod.ast.IfExpression 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)3 IfExpression (kodkod.ast.IfExpression)3 BinaryFormula (kodkod.ast.BinaryFormula)2 BinaryIntExpression (kodkod.ast.BinaryIntExpression)2 ComparisonFormula (kodkod.ast.ComparisonFormula)2 Comprehension (kodkod.ast.Comprehension)2 ConstantExpression (kodkod.ast.ConstantExpression)2 ConstantFormula (kodkod.ast.ConstantFormula)2 FixFormula (kodkod.ast.FixFormula)2 IfIntExpression (kodkod.ast.IfIntExpression)2 IntComparisonFormula (kodkod.ast.IntComparisonFormula)2 IntExpression (kodkod.ast.IntExpression)2 IntToExprCast (kodkod.ast.IntToExprCast)2 MultiplicityFormula (kodkod.ast.MultiplicityFormula)2 NaryExpression (kodkod.ast.NaryExpression)2 NaryFormula (kodkod.ast.NaryFormula)2 NaryIntExpression (kodkod.ast.NaryIntExpression)2 NotFormula (kodkod.ast.NotFormula)2 ProjectExpression (kodkod.ast.ProjectExpression)2 QuantifiedFormula (kodkod.ast.QuantifiedFormula)2