use of kodkod.ast.IntExpression in project org.alloytools.alloy by AlloyTools.
the class OverflowTheoremTest method testCardinality2.
/**
* all s, t : set univ | #(s + t) >= #s && #(s + t) >= #t
*/
@Test
public void testCardinality2() {
Variable s = Variable.unary("s");
Variable t = Variable.unary("t");
IntExpression sutCnt = s.union(t).count();
Decls dcls = s.setOf(Expression.UNIV).and(t.setOf(Expression.UNIV));
Formula f = sutCnt.gte(s.count()).and(sutCnt.gte(t.count())).forAll(dcls);
checkTrue(f);
}
use of kodkod.ast.IntExpression in project org.alloytools.alloy by AlloyTools.
the class IntConstraints method formula.
/**
* Returns the formula a1 <= v1 <= a2 <= v2 .... a1000<=v1000
*
* @return the formula
*/
public final Formula formula() {
final List<Formula> constraints = new ArrayList<Formula>(2000);
final List<IntConstant> constants = new ArrayList<IntConstant>(1001);
for (int i = low; i <= high; i += 10) {
constants.add(IntConstant.constant(i));
}
for (int i = 0; i < 1000; i++) {
// convert to primitive int
IntExpression varExpr = var[i].sum();
// a_i <= v_i
constraints.add(constants.get(i).lte(varExpr));
// v_i <=
constraints.add(varExpr.lte(constants.get(i + 1)));
// a_(i+1)
}
return Formula.and(constraints);
}
use of kodkod.ast.IntExpression 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.IntExpression in project org.alloytools.alloy by AlloyTools.
the class TranslateAlloyToKodkod method visit.
// ==============================================================================================================//
/* ============================ */
/* Evaluates an ExprITE node. */
/* ============================ */
/**
* {@inheritDoc}
*/
@Override
public Object visit(ExprITE x) throws Err {
Formula c = cform(x.cond);
Object l = visitThis(x.left);
if (l instanceof Formula) {
Formula c1 = c.implies((Formula) l);
Formula c2 = c.not().implies(cform(x.right));
return k2pos(c1.and(c2), x);
}
if (l instanceof Expression) {
return c.thenElse((Expression) l, cset(x.right));
}
return c.thenElse((IntExpression) l, cint(x.right));
}
use of kodkod.ast.IntExpression in project org.alloytools.alloy by AlloyTools.
the class AbstractCollector method visit.
/**
* Calls lookup(intExpr) and returns the cached value, if any. If no cached
* value exists, visits each child, caches the union of the children's return
* values and returns it.
*
* @return let x = lookup(intExpr) | x != null => x, cache(intExpr,
* intExpr.child(0).accept(this) + .. +
* intExpr.child(intExpr.size()-1).accept(this))
*/
@Override
public Set<T> visit(NaryIntExpression intExpr) {
Set<T> ret = lookup(intExpr);
if (ret != null)
return ret;
ret = newSet();
for (IntExpression child : intExpr) {
ret.addAll(child.accept(this));
}
return cache(intExpr, ret);
}
Aggregations