kodkod.ast.operator.IntOperator Java Examples

The following examples show how to use kodkod.ast.operator.IntOperator. 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: 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 #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 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 #4
Source File: PartialCannonicalizer.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public IntExpression visit(UnaryIntExpression expr) { 
	IntExpression ret = lookup(expr);
	if (ret!=null) return ret;
	
	final IntOperator op = expr.op();
	final IntExpression child = expr.intExpr().accept(this);
	
	ret = simplify(op, child);
	if (ret==null) {
		final int hash = hash(op, child);
		for(Iterator<PartialCannonicalizer.Holder<IntExpression>> itr = intExprs.get(hash); itr.hasNext(); ) {
			final IntExpression next = itr.next().obj;
			if (next.getClass()==UnaryIntExpression.class) { 
				if (((UnaryIntExpression)next).intExpr()==child)
					return cache(expr, next);
			}
		}
		ret = child==expr.intExpr() ? expr : child.apply(op);
		intExprs.add(new PartialCannonicalizer.Holder<IntExpression>(ret, hash));
	}
	return cache(expr,ret);
}
 
Example #5
Source File: OverflowNumTest.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
private int exeJava(IntOperator op, int i, int j) {
    switch (op) {
        case PLUS :
            return i + j;
        case MINUS :
            return i - j;
        case MULTIPLY :
            return i * j;
        case DIVIDE :
            return i / j;
        case MODULO :
            return i % j;
        default :
            throw new RuntimeException("unknown op: " + op);
    }
}
 
Example #6
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @requires op.nary
 * @return simplification of the given operand list
 **/
final List<IntExpression> simplify(IntOperator op, List<IntExpression> children) { 
	final IntExpression zero = constant(0);
	switch(op) { 
	case PLUS : case OR : 
		for(Iterator<IntExpression> itr = children.iterator(); itr.hasNext(); ) { 
			if (itr.next()==zero)
				itr.remove();
		}
		break;
	case MULTIPLY : case AND :
		for(IntExpression child : children) { 
			if (child==zero)
				return Collections.singletonList(zero);
		}
		break;
	default : 
		Assertions.UNREACHABLE();
	}
	return children;
}
 
Example #7
Source File: IntTest.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
private boolean overflows(IntOperator op, IntExpression ei, IntExpression ej, int i, int j, int realResult) {
    if (!solver.options().noOverflow())
        return false;
    int bw = solver.options().bitwidth();
    if (ei.toString().contains("-#") && outOfBounds(-i, bw))
        return true;
    if (ej.toString().contains("-#") && outOfBounds(-j, bw))
        return true;
    if (op == SHA || op == SHR)
        return false;
    if (op == SHL) {
        if (i == 0 || j == 0)
            return false;
        if (j < 0 || j >= solver.options().bitwidth())
            return true;
    }
    if (outOfBounds(realResult, bw))
        return true;
    return false;
}
 
Example #8
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @return true if the given int expression needs to be parenthesized if a 
 * child of a binary int expression with the given operator */
private boolean parenthesize(IntOperator op, IntExpression child) { 
	return child instanceof SumExpression ||
		   child instanceof IfIntExpression || 
		   child instanceof NaryIntExpression ||
	       (child instanceof BinaryIntExpression && 
	        (!associative(op) || ((BinaryIntExpression)child).op()!=op));
}
 
Example #9
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @return true if the given operator is assocative */
private boolean associative(IntOperator op) { 
	switch(op) { 
	case DIVIDE : case MODULO : case SHA : case SHL : case SHR : return false;
	default : return true;
	}
}
 
Example #10
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @effects appends the given op and child to this.tokens; the child is 
 * parenthesized if it's not an instance of unary int expression or int constant. **/
public void visit(UnaryIntExpression node)  { 
	if (displayed(node)) return;
	final boolean oldTop = notTop();
	final IntExpression child = node.intExpr();
	final IntOperator op = node.op();
	final boolean parens = 
		(op==IntOperator.ABS) || (op==IntOperator.SGN) || 
		parenthesize(child);
	append(node.op());
	visitChild(child, parens);
	top = oldTop;
}
 
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: IntTest.java    From kodkod with MIT License 5 votes vote down vote up
private final void test2sComplementUnOps(IntExpression[] vals) {
	final Options options = solver.options();
	
	final IntRange range = options.integers();
	final int min = range.min(), max = range.max();
	final int mask = ~(-1 << options.bitwidth());
	
	for(int i = min; i <= max; i++) {
		IntExpression vi = vals[i-min];
		testUnOp(IntOperator.NEG, vi, i, -i, mask);
		testUnOp(IntOperator.NOT, vi, i, ~i, mask);
		testUnOp(IntOperator.ABS, vi, i, Math.abs(i), mask);
		testUnOp(IntOperator.SGN, vi, i, i < 0 ? -1 : i > 0 ? 1 : 0, mask);
	}		
}
 
Example #13
Source File: IntTest.java    From kodkod with MIT License 5 votes vote down vote up
private final void testUnOp(IntOperator op, IntExpression ei, int i, int result, int mask) {
	final IntExpression e = ei.apply(op);
	final Formula f = ei.eq(constant(i)).and(e.eq(constant(result)));
	final Solution s = solve(f);

	assertNotNull(s.instance());
	final Evaluator eval = new Evaluator(s.instance(), solver.options());
	assertEquals(result & mask, eval.evaluate(e) & mask);
	
}
 
Example #14
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 #15
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @effects appends the tokenization of the given node to this.tokens */
public void visit(BinaryIntExpression node) {
	if (displayed(node)) return;
	final boolean oldTop = notTop();
	final IntOperator op = node.op();
	visitChild(node.left(), parenthesize(op, node.left()));
	infix(op);
	visitChild(node.right(), parenthesize(op, node.right()));
	top = oldTop;
}
 
Example #16
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @effects appends the tokenization of the given node to this.tokens */
public void visit(NaryIntExpression node) {
	if (displayed(node)) return;
	final boolean oldTop = notTop();
	final IntOperator op = node.op();
	visitChild(node.child(0), parenthesize(op, node.child(0)));
	for(int i = 1, size = node.size(); i < size; i++) {
		infix(op);
		visitChild(node.child(i), parenthesize(op, node.child(i)));
	}
	top = oldTop;
}
 
Example #17
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @return a simplification of op child, if possible, or null otherwise. */
final IntExpression simplify(IntOperator op, IntExpression child) { 
	if (child==constant(0)) {
		switch(op) {
		case ABS : case SGN : case NEG : return child;
		case NOT : return constant(-1);
		default : Assertions.UNREACHABLE();
		}
	}
	return null;
}
 
Example #18
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @return a simplification of left op right, if possible, or null otherwise. */
final IntExpression simplify(IntOperator op, IntExpression left, IntExpression right) { 
	final boolean lzero = left==constant(0), rzero = right==constant(0);
	
	switch(op) { 
	case PLUS : case OR : case XOR : 
		if (lzero)		{ return right; }
		else if (rzero)	{ return left; }
		break;
	case MULTIPLY : case AND : 
		if (lzero)		{ return left; }
		else if (rzero) { return right; }
		break;
	case MINUS : 
		if (lzero)		{ return right.negate().accept(this); }
		else if (rzero) { return left; }
		break;
	case SHA : case SHL : case SHR : 
		if (lzero || rzero)	{ return left; }
		break;
	case DIVIDE : case MODULO : 
		if (lzero) 		{ return left; }
		break;
	default :
		Assertions.UNREACHABLE();
	}
	
	return null;
}
 
Example #19
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public IntExpression visit(UnaryIntExpression expr) { 
	IntExpression ret = lookup(expr);
	if (ret!=null) return ret;
	
	final IntOperator op = expr.op();
	final IntExpression child = expr.intExpr().accept(this);
	
	ret = simplify(op, child);
	if (ret==null) {
		ret = child==expr.intExpr() ? expr : child.apply(op);
	}
	return cache(expr,ret);
}
 
Example #20
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);
}
 
Example #21
Source File: IntExpression.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the composition of the given int expressions using the given operator. 
 * @requires intExprs.length = 2 => op.binary(), intExprs.length > 2 => op.nary()
 * @return intExprs.size() = 1 => intExprs.iterator().next() else {e: IntExpression | e.children = intExprs.toArray() and e.op = this }
 */
public static IntExpression compose(IntOperator op, Collection<? extends IntExpression> intExprs) { 
	switch(intExprs.size()) { 
	case 0 : 	throw new IllegalArgumentException("Expected at least one argument: " + intExprs);
	case 1 : 	return intExprs.iterator().next();
	case 2 :
		final Iterator<? extends IntExpression> itr = intExprs.iterator();
		return new BinaryIntExpression(itr.next(), op, itr.next());
	default : 			
		return new NaryIntExpression(op, intExprs.toArray(new IntExpression[intExprs.size()]));
	}
}
 
Example #22
Source File: IntExpression.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the composition of the given int expressions using the given operator. 
 * @requires intExprs.length = 2 => op.binary(), intExprs.length > 2 => op.nary()
 * @return intExprs.length=1 => intExprs[0] else {e: IntExpression | e.children = intExprs and e.op = this }
 */
public static IntExpression compose(IntOperator op, IntExpression...intExprs) { 
	switch(intExprs.length) { 
	case 0 : 	throw new IllegalArgumentException("Expected at least one argument: " + Arrays.toString(intExprs));
	case 1 : 	return intExprs[0];
	case 2 : 	return new BinaryIntExpression(intExprs[0], op, intExprs[1]);
	default :
		return new NaryIntExpression(op, Containers.copy(intExprs, new IntExpression[intExprs.length]));
	}
}
 
Example #23
Source File: IntExpression.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the composition of the given int expressions using the given
 * operator.
 *
 * @requires intExprs.length = 2 => op.binary(), intExprs.length > 2 =>
 *           op.nary()
 * @return intExprs.length=1 => intExprs[0] else {e: IntExpression | e.children
 *         = intExprs and e.op = this }
 */
public static IntExpression compose(IntOperator op, IntExpression... intExprs) {
    switch (intExprs.length) {
        case 0 :
            throw new IllegalArgumentException("Expected at least one argument: " + Arrays.toString(intExprs));
        case 1 :
            return intExprs[0];
        case 2 :
            return new BinaryIntExpression(intExprs[0], op, intExprs[1]);
        default :
            return new NaryIntExpression(op, Containers.copy(intExprs, new IntExpression[intExprs.length]));
    }
}
 
Example #24
Source File: PrettyPrinter.java    From kodkod with MIT License 5 votes vote down vote up
/** @ensures appends the tokenization of the given node to this.tokens */
public void visit(NaryIntExpression node) {
	final IntOperator op = node.op();
	visitChild(node.child(0), parenthesize(op, node.child(0)));
	for(int i = 1, size = node.size(); i < size; i++) {
		infix(op);
		visitChild(node.child(i), parenthesize(op, node.child(i)));
	}
}
 
Example #25
Source File: PrettyPrinter.java    From kodkod with MIT License 5 votes vote down vote up
/** @ensures appends the tokenization of the given node to this.tokens */
public void visit(BinaryIntExpression node) {
	final IntOperator op = node.op();
	visitChild(node.left(), parenthesize(op, node.left()));
	infix(op);
	visitChild(node.right(), parenthesize(op, node.right()));
}
 
Example #26
Source File: PrettyPrinter.java    From kodkod with MIT License 5 votes vote down vote up
/** @return true if the given int expression needs to be parenthesized if a 
 * child of a binary int expression with the given operator */
private boolean parenthesize(IntOperator op, IntExpression child) { 
	return child instanceof SumExpression ||
		   child instanceof IfIntExpression || 
		   child instanceof NaryIntExpression ||
	       (child instanceof BinaryIntExpression && 
	        (!associative(op) || ((BinaryIntExpression)child).op()!=op));
}
 
Example #27
Source File: PrettyPrinter.java    From kodkod with MIT License 5 votes vote down vote up
/** @return true if the given operator is associative */
private boolean associative(IntOperator op) { 
	switch(op) { 
	case DIVIDE : case MODULO : case SHA : case SHL : case SHR : return false;
	default : return true;
	}
}
 
Example #28
Source File: PrettyPrinter.java    From kodkod with MIT License 5 votes vote down vote up
/** @ensures appends the given op and child to this.tokens; the child is 
 * parenthesized if it's not an instance of unary int expression or int constant. **/
public void visit(UnaryIntExpression node)  { 
	final IntExpression child = node.intExpr();
	final IntOperator op = node.op();
	final boolean parens = 
		(op==IntOperator.ABS) || (op==IntOperator.SGN) || 
		parenthesize(child);
	append(node.op());
	visitChild(child, parens);
}
 
Example #29
Source File: BinaryIntExpression.java    From kodkod with MIT License 5 votes vote down vote up
/**  
 * Constructs a new binary int formula: left op right
 * 
 * @ensures this.left' = left && this.right' = right && this.op' = op
 * @throws NullPointerException  left = null || right = null || op = null
 */
public BinaryIntExpression(final IntExpression left, final IntOperator op, final IntExpression right) {
	if (!op.binary()) throw new IllegalArgumentException("Not a binary operator: " + op);
	this.left = left;
	this.right = right;
	this.op = op;
}
 
Example #30
Source File: NaryIntExpression.java    From kodkod with MIT License 5 votes vote down vote up
/**  
 * Constructs a new composite IntExpression: op(children)
 * @requires children array is not modified while in use by this composite IntExpression
 * @requires some op.op[children]
 * @ensures this.children' = children && this.op' = op
 */
NaryIntExpression(IntOperator op, IntExpression[] children) { 
	assert children.length>2;
	if (!op.nary()) 
		throw new IllegalArgumentException("Cannot construct an nary int expression using the non-nary operator " + op);
	this.op = op;
	this.children = children;
}