use of kodkod.ast.BinaryFormula 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.BinaryFormula)
*/
@Override
public final Formula visit(BinaryFormula bf) {
Formula ret = lookup(bf);
if (ret != null)
return ret;
final FormulaOperator op = bf.op();
final int oldDepth = skolemDepth;
if (op == IFF || (negated && op == AND) || (!negated && (op == OR || op == IMPLIES))) {
// cannot
// skolemize
// in
// these
// cases
skolemDepth = -1;
}
final Formula left, right;
if (negated && op == IMPLIES) {
// !(a => b) = !(!a || b) = a && !b
negated = !negated;
left = bf.left().accept(this);
negated = !negated;
right = bf.right().accept(this);
} else {
left = bf.left().accept(this);
right = bf.right().accept(this);
}
skolemDepth = oldDepth;
ret = (left == bf.left() && right == bf.right()) ? bf : left.compose(op, right);
return source(cache(bf, ret), bf);
}
use of kodkod.ast.BinaryFormula in project org.alloytools.alloy by AlloyTools.
the class AbstractReplacer method visit.
/**
* Calls lookup(binFormula) 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 { b: BinaryFormula | b.left = binExpr.left.accept(delegate) &&
* b.right = binExpr.right.accept(delegate) && b.op = binExpr.op }
*/
@Override
public Formula visit(BinaryFormula binFormula) {
Formula ret = lookup(binFormula);
if (ret != null)
return ret;
final Formula left = binFormula.left().accept(delegate);
final Formula right = binFormula.right().accept(delegate);
ret = (left == binFormula.left() && right == binFormula.right()) ? binFormula : left.compose(binFormula.op(), right);
return cache(binFormula, ret);
}
use of kodkod.ast.BinaryFormula 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;
}
use of kodkod.ast.BinaryFormula in project org.alloytools.alloy by AlloyTools.
the class Nodes method allConjuncts.
public static List<Formula> allConjuncts(Formula formula, List<Formula> acc) {
List<Formula> ans = acc != null ? acc : new ArrayList<Formula>();
if (formula instanceof BinaryFormula) {
final BinaryFormula bin = (BinaryFormula) formula;
if (bin.op() == FormulaOperator.AND) {
allConjuncts(bin.left(), ans);
allConjuncts(bin.right(), ans);
return ans;
}
}
if (formula instanceof NaryFormula) {
final NaryFormula nf = (NaryFormula) formula;
if (nf.op() == FormulaOperator.AND) {
for (Formula child : nf) {
allConjuncts(child, ans);
}
return ans;
}
}
ans.add(formula);
return ans;
}
Aggregations