Search in sources :

Example 1 with NaryFormula

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

the class Nodes method roots.

/**
 * Returns the roots of the given formula. In other words, breaks up the given
 * formula into its conjunctive components, {f0, ..., fk}, such that, for all
 * 0<=i<=k, f<sub>i</sub> is not a conjunction and [[f0 && ... && fk]] <=>
 * [[formula]].
 *
 * @return subformulas, {f0, ..., fk}, of the given formula such that, for all
 *         0<=i<=k, f<sub>i</sub> is not a conjuction and [[f0 && ... && fk]]
 *         <=> [[formula]].
 */
public static Set<Formula> roots(Formula formula) {
    final List<Formula> formulas = new LinkedList<Formula>();
    formulas.add(formula);
    final ListIterator<Formula> itr = formulas.listIterator();
    while (itr.hasNext()) {
        final Formula f = itr.next();
        if (f instanceof BinaryFormula) {
            final BinaryFormula bin = (BinaryFormula) f;
            if (bin.op() == FormulaOperator.AND) {
                itr.remove();
                itr.add(bin.left());
                itr.add(bin.right());
                itr.previous();
                itr.previous();
            }
        } else if (f instanceof NaryFormula) {
            final NaryFormula nf = (NaryFormula) f;
            if (nf.op() == FormulaOperator.AND) {
                itr.remove();
                for (Formula child : nf) {
                    itr.add(child);
                }
                for (int i = nf.size(); i > 0; i--) {
                    itr.previous();
                }
            }
        }
    }
    return new LinkedHashSet<Formula>(formulas);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BinaryFormula(kodkod.ast.BinaryFormula) Formula(kodkod.ast.Formula) NaryFormula(kodkod.ast.NaryFormula) BinaryFormula(kodkod.ast.BinaryFormula) NaryFormula(kodkod.ast.NaryFormula) LinkedList(java.util.LinkedList)

Example 2 with NaryFormula

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

the class FOL2BoolTranslator method visit.

/**
 * Calls lookup(formula) and returns the cached value, if any. If a translation
 * has not been cached, translates the formula, calls cache(...) on it and
 * returns it.
 *
 * @return let t = lookup(formula) | some t => t, cache(formula,
 *         formula.op(formula.left.accept(this), formula.right.accept(this))
 */
@Override
public final BooleanValue visit(NaryFormula formula) {
    final BooleanValue ret = lookup(formula);
    if (ret != null)
        return ret;
    final FormulaOperator op = formula.op();
    final Operator.Nary boolOp;
    switch(op) {
        case AND:
            boolOp = Operator.AND;
            break;
        case OR:
            boolOp = Operator.OR;
            break;
        default:
            throw new IllegalArgumentException("Unknown nary operator: " + op);
    }
    final BooleanAccumulator acc = BooleanAccumulator.treeGate(boolOp);
    final BooleanValue shortCircuit = boolOp.shortCircuit();
    for (Formula child : formula) {
        if (acc.add(child.accept(this)) == shortCircuit)
            break;
    }
    return cache(formula, interpreter.factory().accumulate(acc));
}
Also used : FormulaOperator(kodkod.ast.operator.FormulaOperator) ExprCompOperator(kodkod.ast.operator.ExprCompOperator) ExprOperator(kodkod.ast.operator.ExprOperator) Operator(kodkod.engine.bool.Operator) ConstantFormula(kodkod.ast.ConstantFormula) ComparisonFormula(kodkod.ast.ComparisonFormula) NaryFormula(kodkod.ast.NaryFormula) BinaryFormula(kodkod.ast.BinaryFormula) MultiplicityFormula(kodkod.ast.MultiplicityFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) NotFormula(kodkod.ast.NotFormula) Formula(kodkod.ast.Formula) FixFormula(kodkod.ast.FixFormula) IntComparisonFormula(kodkod.ast.IntComparisonFormula) FormulaOperator(kodkod.ast.operator.FormulaOperator) BooleanValue(kodkod.engine.bool.BooleanValue) BooleanAccumulator(kodkod.engine.bool.BooleanAccumulator)

Example 3 with NaryFormula

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

the class Skolemizer method visit.

/**
 * If not cached, visits the formula's children with appropriate settings for
 * the negated flag and the skolemDepth parameter.
 *
 * @see kodkod.ast.visitor.AbstractReplacer#visit(kodkod.ast.NaryFormula)
 */
@Override
public final Formula visit(NaryFormula bf) {
    Formula ret = lookup(bf);
    if (ret != null)
        return ret;
    final int oldDepth = skolemDepth;
    final FormulaOperator op = bf.op();
    switch(op) {
        case AND:
            if (negated)
                skolemDepth = -1;
            break;
        case OR:
            if (!negated)
                skolemDepth = -1;
            break;
        default:
            throw new IllegalArgumentException("Unknown nary operator: " + op);
    }
    final Formula[] visited = new Formula[bf.size()];
    boolean allSame = true;
    for (int i = 0; i < visited.length; i++) {
        final Formula child = bf.child(i);
        visited[i] = child.accept(this);
        allSame = allSame && (child == visited[i]);
    }
    ret = allSame ? bf : Formula.compose(op, visited);
    skolemDepth = oldDepth;
    return source(cache(bf, ret), bf);
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) MultiplicityFormula(kodkod.ast.MultiplicityFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) NotFormula(kodkod.ast.NotFormula) ComparisonFormula(kodkod.ast.ComparisonFormula) NaryFormula(kodkod.ast.NaryFormula) Formula(kodkod.ast.Formula) IntComparisonFormula(kodkod.ast.IntComparisonFormula) FormulaOperator(kodkod.ast.operator.FormulaOperator)

Example 4 with NaryFormula

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

the class AbstractReplacer method visit.

/**
 * Calls lookup(formula) 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 { e: Expression | e.op = formula.op && #e.children =
 *         #formula.children && all i: [0..formula.children) | e.child(i) =
 *         formula.child(i).accept(delegate) }
 */
@Override
public Formula visit(NaryFormula formula) {
    Formula ret = lookup(formula);
    if (ret != null)
        return ret;
    final Formula[] visited = new Formula[formula.size()];
    boolean allSame = true;
    for (int i = 0; i < visited.length; i++) {
        final Formula child = formula.child(i);
        visited[i] = child.accept(delegate);
        allSame = allSame && visited[i] == child;
    }
    ret = allSame ? formula : Formula.compose(formula.op(), visited);
    return cache(formula, 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)

Example 5 with NaryFormula

use of kodkod.ast.NaryFormula 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)

Aggregations

BinaryFormula (kodkod.ast.BinaryFormula)10 NaryFormula (kodkod.ast.NaryFormula)10 Formula (kodkod.ast.Formula)9 NotFormula (kodkod.ast.NotFormula)8 QuantifiedFormula (kodkod.ast.QuantifiedFormula)8 ComparisonFormula (kodkod.ast.ComparisonFormula)7 IntComparisonFormula (kodkod.ast.IntComparisonFormula)7 MultiplicityFormula (kodkod.ast.MultiplicityFormula)7 ConstantFormula (kodkod.ast.ConstantFormula)6 FixFormula (kodkod.ast.FixFormula)6 FormulaOperator (kodkod.ast.operator.FormulaOperator)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 BinaryExpression (kodkod.ast.BinaryExpression)1 BinaryIntExpression (kodkod.ast.BinaryIntExpression)1 Comprehension (kodkod.ast.Comprehension)1 ConstantExpression (kodkod.ast.ConstantExpression)1 Decl (kodkod.ast.Decl)1