use of kodkod.engine.bool.Int in project org.alloytools.alloy by AlloyTools.
the class FOL2BoolTranslator method visit.
/**
* Calls lookup(intExpr) and returns the cached value, if any. If a translation
* has not been cached, translates the expression, calls cache(...) on it and
* returns it.
*
* @return let t = lookup(intExpr) | some t => t, cache(intExpr,
* translate(intExpr))
*/
@Override
public final Int visit(SumExpression intExpr) {
final Int ret = lookup(intExpr);
if (ret != null)
return ret;
final List<Int> values = new ArrayList<Int>();
sum(intExpr.decls(), intExpr.intExpr(), 0, BooleanConstant.TRUE, values);
for (int sums = values.size(); sums > 1; sums -= sums / 2) {
final int max = sums - 1;
for (int i = 0; i < max; i += 2) {
values.set(i / 2, values.get(i).plus(values.get(i + 1)));
}
if (max % 2 == 0) {
// even max => odd number of entries
values.set(max / 2, values.get(max));
}
}
if (values.isEmpty()) {
return cache(intExpr, interpreter.factory().integer(0));
} else {
Int sumValue = values.get(0);
return cache(intExpr, sumValue);
}
}
use of kodkod.engine.bool.Int in project org.alloytools.alloy by AlloyTools.
the class FOL2BoolTranslator method visit.
/**
* Calls lookup(intExpr) and returns the cached value, if any. If a translation
* has not been cached, translates the expression, calls cache(...) on it and
* returns it.
*
* @return let t = lookup(intExpr) | some t => t, cache(intExpr,
* intExpr.left.accept(this) intExpr.op intExpr.right.accept(this))
*/
@Override
public final Int visit(NaryIntExpression intExpr) {
Int ret = lookup(intExpr);
if (ret != null)
return ret;
final Int first = intExpr.child(0).accept(this);
final Int[] rest = new Int[intExpr.size() - 1];
for (int i = 0; i < rest.length; i++) {
rest[i] = intExpr.child(i + 1).accept(this);
}
switch(intExpr.op()) {
case PLUS:
ret = first.plus(rest);
break;
case MULTIPLY:
ret = first.multiply(rest);
break;
case AND:
ret = first.and(rest);
break;
case OR:
ret = first.or(rest);
break;
default:
throw new IllegalArgumentException("Unknown nary operator: " + intExpr.op());
}
return cache(intExpr, ret);
}
Aggregations