use of kodkod.ast.IntToExprCast in project org.alloytools.alloy by AlloyTools.
the class TranslateAlloyToKodkod method visit.
/* =============================== */
/* Evaluates an ExprBinary node. */
/* =============================== */
/**
* {@inheritDoc}
*/
@Override
public Object visit(ExprBinary x) throws Err {
Expr a = x.left, b = x.right;
Expression s, s2, eL, eR;
IntExpression i;
Formula f;
Object objL, objR;
switch(x.op) {
case IMPLIES:
f = cform(a).not().or(cform(b));
return k2pos(f, x);
case IN:
return k2pos(isIn(cset(a), b), x);
case NOT_IN:
return k2pos(isIn(cset(a), b).not(), x);
case LT:
i = cint(a);
f = i.lt(cint(b));
return k2pos(f, x);
case LTE:
i = cint(a);
f = i.lte(cint(b));
return k2pos(f, x);
case GT:
i = cint(a);
f = i.gt(cint(b));
return k2pos(f, x);
case GTE:
i = cint(a);
f = i.gte(cint(b));
return k2pos(f, x);
case NOT_LT:
i = cint(a);
f = i.lt(cint(b)).not();
return k2pos(f, x);
case NOT_LTE:
i = cint(a);
f = i.lte(cint(b)).not();
return k2pos(f, x);
case NOT_GT:
i = cint(a);
f = i.gt(cint(b)).not();
return k2pos(f, x);
case NOT_GTE:
i = cint(a);
f = i.gte(cint(b)).not();
return k2pos(f, x);
case AND:
f = cform(a);
f = f.and(cform(b));
return k2pos(f, x);
case OR:
f = cform(a);
f = f.or(cform(b));
return k2pos(f, x);
case IFF:
f = cform(a);
f = f.iff(cform(b));
return k2pos(f, x);
case PLUSPLUS:
s = cset(a);
return s.override(cset(b));
case MUL:
i = cint(a);
return i.multiply(cint(b));
case DIV:
i = cint(a);
return i.divide(cint(b));
case REM:
i = cint(a);
return i.modulo(cint(b));
case SHL:
i = cint(a);
return i.shl(cint(b));
case SHR:
i = cint(a);
return i.shr(cint(b));
case SHA:
i = cint(a);
return i.sha(cint(b));
case PLUS:
return cset(a).union(cset(b));
// s = (Expression)obj; return s.union(cset(b));
case IPLUS:
return cint(a).plus(cint(b));
case MINUS:
// exception)
if (a instanceof ExprConstant && ((ExprConstant) a).op == ExprConstant.Op.NUMBER && ((ExprConstant) a).num() == 0)
if (b instanceof ExprConstant && ((ExprConstant) b).op == ExprConstant.Op.NUMBER && ((ExprConstant) b).num() == max + 1)
return IntConstant.constant(min);
return cset(a).difference(cset(b));
// s=(Expression)obj; return s.difference(cset(b));
case IMINUS:
return cint(a).minus(cint(b));
case INTERSECT:
s = cset(a);
return s.intersection(cset(b));
case ANY_ARROW_SOME:
case ANY_ARROW_ONE:
case ANY_ARROW_LONE:
case SOME_ARROW_ANY:
case SOME_ARROW_SOME:
case SOME_ARROW_ONE:
case SOME_ARROW_LONE:
case ONE_ARROW_ANY:
case ONE_ARROW_SOME:
case ONE_ARROW_ONE:
case ONE_ARROW_LONE:
case LONE_ARROW_ANY:
case LONE_ARROW_SOME:
case LONE_ARROW_ONE:
case LONE_ARROW_LONE:
case ISSEQ_ARROW_LONE:
case ARROW:
s = cset(a);
return s.product(cset(b));
case JOIN:
a = a.deNOP();
s = cset(a);
s2 = cset(b);
if (a instanceof Sig && ((Sig) a).isOne != null && s2 instanceof BinaryExpression) {
BinaryExpression bin = (BinaryExpression) s2;
if (bin.op() == ExprOperator.PRODUCT && bin.left() == s)
return bin.right();
}
return s.join(s2);
case EQUALS:
objL = visitThis(a);
objR = visitThis(b);
eL = toSet(a, objL);
eR = toSet(b, objR);
if (eL instanceof IntToExprCast && eR instanceof IntToExprCast)
f = ((IntToExprCast) eL).intExpr().eq(((IntToExprCast) eR).intExpr());
else
f = eL.eq(eR);
return k2pos(f, x);
case NOT_EQUALS:
objL = visitThis(a);
objR = visitThis(b);
eL = toSet(a, objL);
eR = toSet(b, objR);
if (eL instanceof IntToExprCast && eR instanceof IntToExprCast)
f = ((IntToExprCast) eL).intExpr().eq(((IntToExprCast) eR).intExpr()).not();
else
f = eL.eq(eR).not();
return k2pos(f, x);
case DOMAIN:
s = cset(a);
s2 = cset(b);
for (int j = s2.arity(); j > 1; j--) s = s.product(Expression.UNIV);
return s.intersection(s2);
case RANGE:
s = cset(a);
s2 = cset(b);
for (int j = s.arity(); j > 1; j--) s2 = Expression.UNIV.product(s2);
return s.intersection(s2);
}
throw new ErrorFatal(x.pos, "Unsupported operator (" + x.op + ") encountered during ExprBinary.accept()");
}
use of kodkod.ast.IntToExprCast 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.IntToExprCast 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);
}
Aggregations