Java Code Examples for kodkod.ast.IntExpression#compose()

The following examples show how to use kodkod.ast.IntExpression#compose() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractReplacer.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Calls lookup(intExpr) and returns the cached value, if any. If a replacement
 * has not been cached, visits the intExpr's children. If nothing changes, the
 * argument is cached and returned, otherwise a replacement intExpr is cached
 * and returned.
 *
 * @return { e: IntExpression | e.op = intExpr.op && #e.children =
 *         #intExpr.children && all i: [0..intExpr.children) | e.child(i) =
 *         intExpr.child(i).accept(delegate) }
 */
@Override
public IntExpression visit(NaryIntExpression intExpr) {
    IntExpression ret = lookup(intExpr);
    if (ret != null)
        return ret;

    final IntExpression[] visited = new IntExpression[intExpr.size()];
    boolean allSame = true;
    for (int i = 0; i < visited.length; i++) {
        final IntExpression child = intExpr.child(i);
        visited[i] = child.accept(delegate);
        allSame = allSame && visited[i] == child;
    }

    ret = allSame ? intExpr : IntExpression.compose(intExpr.op(), visited);
    return cache(intExpr, ret);
}
 
Example 2
Source File: TranslatorTest.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
private final void testNary(IntOperator op) {
    bounds.bound(r1[0], factory.range(factory.tuple(1, 0), factory.tuple(1, 3)));
    bounds.bound(r1[1], factory.range(factory.tuple(1, 2), factory.tuple(1, 5)));
    bounds.bound(r1[3], factory.range(factory.tuple(1, 3), factory.tuple(1, 6)));

    for (int i = 2; i <= 5; i++) {
        final IntExpression[] exprs = new IntExpression[i];
        exprs[0] = r1[0].count();
        IntExpression binExpr = r1[0].count();
        for (int j = 1; j < i; j++) {
            binExpr = binExpr.compose(op, r1[j % 4].count());
            exprs[j] = r1[j % 4].count();
        }
        IntExpression nExpr = IntExpression.compose(op, exprs);
        final Solution sol = solver.solve(binExpr.eq(nExpr).not(), bounds);
        assertNull(sol.instance());
    }

}
 
Example 3
Source File: TranslatorTest.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
private final void testNary(IntOperator op) {
    bounds.bound(r1[0], factory.range(factory.tuple(1, 0), factory.tuple(1, 3)));
    bounds.bound(r1[1], factory.range(factory.tuple(1, 2), factory.tuple(1, 5)));
    bounds.bound(r1[3], factory.range(factory.tuple(1, 3), factory.tuple(1, 6)));

    for (int i = 2; i <= 5; i++) {
        final IntExpression[] exprs = new IntExpression[i];
        exprs[0] = r1[0].count();
        IntExpression binExpr = r1[0].count();
        for (int j = 1; j < i; j++) {
            binExpr = binExpr.compose(op, r1[j % 4].count());
            exprs[j] = r1[j % 4].count();
        }
        IntExpression nExpr = IntExpression.compose(op, exprs);
        final Solution sol = solver.solve(binExpr.eq(nExpr).not(), bounds);
        assertNull(sol.instance());
    }

}
 
Example 4
Source File: AbstractReplacer.java    From kodkod with MIT License 6 votes vote down vote up
/** 
* Calls lookup(intExpr) and returns the cached value, if any.  
* If a replacement has not been cached, visits the intExpr's 
* children.  If nothing changes, the argument is cached and
* returned, otherwise a replacement intExpr is cached and returned.
* @return { e: IntExpression | e.op = intExpr.op && #e.children = #intExpr.children && all i: [0..intExpr.children) | e.child(i) = intExpr.child(i).accept(this) }
*/
  public IntExpression visit(NaryIntExpression intExpr) {
IntExpression ret = lookup(intExpr);
if (ret!=null) return ret;
	
final IntExpression[] visited = new IntExpression[intExpr.size()];
boolean allSame = true;
for(int i = 0 ; i < visited.length; i++) { 
	final IntExpression child = intExpr.child(i);
	visited[i] = child.accept(this);
	allSame = allSame && visited[i]==child;
}

ret = allSame ? intExpr : IntExpression.compose(intExpr.op(), visited);
return cache(intExpr,ret);
  }
 
Example 5
Source File: TranslatorTest.java    From kodkod with MIT License 6 votes vote down vote up
private final void testNary(IntOperator op) { 
	bounds.bound(r1[0], factory.range(factory.tuple(1, 0), factory.tuple(1, 3)));
	bounds.bound(r1[1], factory.range(factory.tuple(1, 2), factory.tuple(1, 5)));
	bounds.bound(r1[3], factory.range(factory.tuple(1, 3), factory.tuple(1, 6)));
	
	for(int i = 2; i <= 5; i++) { 
		final IntExpression[] exprs = new IntExpression[i];
		exprs[0] = r1[0].count();
		IntExpression binExpr = r1[0].count();
		for(int j = 1; j < i; j++) { 
			binExpr = binExpr.compose(op, r1[j%4].count());
			exprs[j] = r1[j%4].count();
		}
		IntExpression nExpr = IntExpression.compose(op, exprs);
		final Solution sol = solver.solve(binExpr.eq(nExpr).not(), bounds);
		assertNull(sol.instance());	
	}
	
}
 
Example 6
Source File: TranslatorTest.java    From kodkod with MIT License 6 votes vote down vote up
private final void testNary(IntOperator op) { 
	bounds.bound(r1[0], factory.range(factory.tuple(1, 0), factory.tuple(1, 3)));
	bounds.bound(r1[1], factory.range(factory.tuple(1, 2), factory.tuple(1, 5)));
	bounds.bound(r1[3], factory.range(factory.tuple(1, 3), factory.tuple(1, 6)));
	
	for(int i = 2; i <= 5; i++) { 
		final IntExpression[] exprs = new IntExpression[i];
		exprs[0] = r1[0].count();
		IntExpression binExpr = r1[0].count();
		for(int j = 1; j < i; j++) { 
			binExpr = binExpr.compose(op, r1[j%4].count());
			exprs[j] = r1[j%4].count();
		}
		IntExpression nExpr = IntExpression.compose(op, exprs);
		final Solution sol = solver.solve(binExpr.eq(nExpr).not(), bounds);
		assertNull(sol.instance());	
	}
	
}
 
Example 7
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public IntExpression visit(NaryIntExpression expr) { 
	IntExpression ret = lookup(expr);
	if (ret!=null) return ret;
	
	final IntOperator op = expr.op();
	
	final List<IntExpression> children = simplify(op, visitAll(expr));
	final int size = children.size();
	switch(size) { 
	case 0 : return cache(expr, constant(0));
	case 1 : return cache(expr, children.get(0));
	default :
		ret = expr.size()==size && allSame(expr,children) ? expr : IntExpression.compose(op, children);
		return cache(expr,ret);
	}	
}
 
Example 8
Source File: AbstractReplacer.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(intExpr) and returns the cached value, if any. If a replacement
 * has not been cached, visits the expression's children. If nothing changes,
 * the argument is cached and returned, otherwise a replacement expression is
 * cached and returned.
 *
 * @return { c: IntExpression | [[c]] = intExpr.left.accept(delegate) op
 *         intExpr.right.accept(delegate) }
 */
@Override
public IntExpression visit(BinaryIntExpression intExpr) {
    IntExpression ret = lookup(intExpr);
    if (ret != null)
        return ret;

    final IntExpression left = intExpr.left().accept(delegate);
    final IntExpression right = intExpr.right().accept(delegate);
    ret = (left == intExpr.left() && right == intExpr.right()) ? intExpr : left.compose(intExpr.op(), right);
    return cache(intExpr, ret);
}
 
Example 9
Source File: IntTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
private final void testBinOp(IntOperator op, IntExpression ei, IntExpression ej, int i, int j, int result, int realResult, int mask) {
    final IntExpression e = ei.compose(op, ej);
    final Formula f = ei.eq(constant(i)).and(ej.eq(constant(j))).and(e.eq(constant(result)));
    final Solution s = solve(f);
    Instance inst = s.instance();
    if (overflows(op, ei, ej, i, j, realResult)) {
        assertNull(f.toString(), inst);
    } else {
        assertNotNull(f.toString(), inst);
        final Evaluator eval = new Evaluator(inst, solver.options());
        assertEquals(f.toString(), result & mask, eval.evaluate(e) & mask);
    }
}
 
Example 10
Source File: AbstractReplacer.java    From kodkod with MIT License 5 votes vote down vote up
/** 
* Calls lookup(intExpr) and returns the cached value, if any.  
* If a replacement has not been cached, visits the expression's 
* children.  If nothing changes, the argument is cached and
* returned, otherwise a replacement expression is cached and returned.
* @return { c: IntExpression | [[c]] = intExpr.left.accept(this) op intExpr.right.accept(this) }
*/
  public IntExpression visit(BinaryIntExpression intExpr) {
IntExpression ret = lookup(intExpr);
if (ret!=null) return ret;
	
final IntExpression left  = intExpr.left().accept(this);
final IntExpression right = intExpr.right().accept(this);
ret =  (left==intExpr.left() && right==intExpr.right()) ? 
		intExpr : left.compose(intExpr.op(), right);
return cache(intExpr,ret);
  }
 
Example 11
Source File: IntTest.java    From kodkod with MIT License 5 votes vote down vote up
private final void testBinOp(IntOperator op, IntExpression ei, IntExpression ej, int i, int j, int result, int mask) {
	final IntExpression e = ei.compose(op, ej);
	final Formula f = ei.eq(constant(i)).and(ej.eq(constant(j))).and(e.eq(constant(result)));
	final Solution s = solve(f);
	//if (s.instance()==null)
	//	System.out.println(f + " no solution!");
	assertNotNull(s.instance());
	final Evaluator eval = new Evaluator(s.instance(), solver.options());
	//System.out.println(f + ", expected: " + (result & mask) + ", actual: " + (eval.evaluate(e) & mask));
	assertEquals(result & mask, eval.evaluate(e) & mask);
	
}
 
Example 12
Source File: PartialCannonicalizer.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public IntExpression visit(BinaryIntExpression expr) { 
	IntExpression ret = lookup(expr);
	if (ret!=null) return ret;
	final IntOperator op = expr.op();
	final IntExpression left = expr.left().accept(this);
	final IntExpression right = expr.right().accept(this);
	
	ret = simplify(op, left, right);
	
	if (ret==null) {
	
		final int hash = hash(op, left, right);
		for(Iterator<PartialCannonicalizer.Holder<IntExpression>> itr = intExprs.get(hash); itr.hasNext(); ) {
			final IntExpression next = itr.next().obj;
			if (next instanceof BinaryIntExpression) { 
				final BinaryIntExpression hit = (BinaryIntExpression) next;
				if (hit.op()==op && hit.left()==left && hit.right()==right) { 
					return cache(expr, hit);
				}
			}
		}

		ret = left==expr.left()&&right==expr.right() ? expr : left.compose(op, right);
		intExprs.add(new PartialCannonicalizer.Holder<IntExpression>(ret, hash));
	}
	
	return cache(expr,ret);
}
 
Example 13
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public IntExpression visit(BinaryIntExpression expr) { 
	IntExpression ret = lookup(expr);
	if (ret!=null) return ret;
	final IntOperator op = expr.op();
	final IntExpression left = expr.left().accept(this);
	final IntExpression right = expr.right().accept(this);
	
	ret = simplify(op, left, right);
	
	if (ret==null) {
		ret = left==expr.left()&&right==expr.right() ? expr : left.compose(op, right);
	}
	
	return cache(expr,ret);
}