Java Code Examples for kodkod.ast.Formula#thenElse()

The following examples show how to use kodkod.ast.Formula#thenElse() . 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: PartialCannonicalizer.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public Expression visit(IfExpression expr) { 
	Expression ret = lookup(expr);
	if (ret!=null) return ret;
	
	final Formula cond = expr.condition().accept(this);
	final Expression thenExpr = expr.thenExpr().accept(this);
	final Expression elseExpr = expr.elseExpr().accept(this);
	
	ret = simplify(cond,thenExpr,elseExpr);
	
	if (ret==null) { 
		final int hash = hash(IfExpression.class, cond, thenExpr, elseExpr);
		for(Iterator<PartialCannonicalizer.Holder<Expression>> itr = exprs.get(hash); itr.hasNext(); ) {
			final Expression next = itr.next().obj;
			if (next.getClass()==IfExpression.class) { 
				final IfExpression hit = (IfExpression) next;
				if (hit.condition()==cond && hit.thenExpr()==thenExpr && hit.elseExpr()==elseExpr) { 
					return cache(expr, hit);
				}
			}
		}
		ret = cond==expr.condition()&&thenExpr==expr.thenExpr()&&elseExpr==expr.elseExpr() ? expr : cond.thenElse(thenExpr, elseExpr);
		exprs.add(new PartialCannonicalizer.Holder<Expression>(ret, hash));
	}
	return cache(expr,ret);
}
 
Example 2
Source File: PartialCannonicalizer.java    From quetzal with Eclipse Public License 2.0 6 votes vote down vote up
public IntExpression visit(IfIntExpression expr) { 
	IntExpression ret = lookup(expr);
	if (ret!=null) return ret;
	
	final Formula cond = expr.condition().accept(this);
	final IntExpression thenExpr = expr.thenExpr().accept(this);
	final IntExpression elseExpr = expr.elseExpr().accept(this);
	
	ret = simplify(cond,thenExpr,elseExpr);
	
	if (ret==null) { 
		final int hash = hash(IfIntExpression.class, cond, thenExpr, elseExpr);
		for(Iterator<PartialCannonicalizer.Holder<IntExpression>> itr = intExprs.get(hash); itr.hasNext(); ) {
			final IntExpression next = itr.next().obj;
			if (next.getClass()==IfIntExpression.class) { 
				final IfIntExpression hit = (IfIntExpression) next;
				if (hit.condition()==cond && hit.thenExpr()==thenExpr && hit.elseExpr()==elseExpr) { 
					return cache(expr, hit);
				}
			}
		}
		ret = cond==expr.condition()&&thenExpr==expr.thenExpr()&&elseExpr==expr.elseExpr() ? expr : cond.thenElse(thenExpr, elseExpr);
		intExprs.add(new PartialCannonicalizer.Holder<IntExpression>(ret, hash));
	}
	return cache(expr,ret);
}
 
Example 3
Source File: AbstractReplacer.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(ifExpr) 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 { i: IfExpression | i.condition = ifExpr.condition.accept(delegate)
 *         && i.thenExpr = ifExpr.thenExpr.accept(delegate) && i.elseExpr =
 *         ifExpr.elseExpr.accept(delegate) }
 */
@Override
public Expression visit(IfExpression ifExpr) {
    Expression ret = lookup(ifExpr);
    if (ret != null)
        return ret;

    final Formula condition = ifExpr.condition().accept(delegate);
    final Expression thenExpr = ifExpr.thenExpr().accept(delegate);
    final Expression elseExpr = ifExpr.elseExpr().accept(delegate);
    ret = (condition == ifExpr.condition() && thenExpr == ifExpr.thenExpr() && elseExpr == ifExpr.elseExpr()) ? ifExpr : condition.thenElse(thenExpr, elseExpr);
    return cache(ifExpr, ret);
}
 
Example 4
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 { i: IfIntExpression | i.condition =
 *         intExpr.condition.accept(delegate) && i.thenExpr =
 *         intExpr.thenExpr.accept(delegate) && i.elseExpr =
 *         intExpr.elseExpr.accept(delegate) }
 */
@Override
public IntExpression visit(IfIntExpression intExpr) {
    IntExpression ret = lookup(intExpr);
    if (ret != null)
        return ret;

    final Formula condition = intExpr.condition().accept(delegate);
    final IntExpression thenExpr = intExpr.thenExpr().accept(delegate);
    final IntExpression elseExpr = intExpr.elseExpr().accept(delegate);
    ret = (condition == intExpr.condition() && thenExpr == intExpr.thenExpr() && elseExpr == intExpr.elseExpr()) ? intExpr : condition.thenElse(thenExpr, elseExpr);
    return cache(intExpr, ret);
}
 
Example 5
Source File: TranslateAlloyToKodkod.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/** {@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));
}
 
Example 6
Source File: AbstractReplacer.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(ifExpr) 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 { i: IfExpression | i.condition = ifExpr.condition.accept(this) &&
 *                             i.thenExpr = ifExpr.thenExpr.accept(this) &&
 *                             i.elseExpr = ifExpr.elseExpr.accept(this) }
 */
public Expression visit(IfExpression ifExpr) {
	Expression ret = lookup(ifExpr);
	if (ret!=null) return ret;

	final Formula condition = ifExpr.condition().accept(this);
	final Expression thenExpr = ifExpr.thenExpr().accept(this);
	final Expression elseExpr = ifExpr.elseExpr().accept(this);
	ret = (condition==ifExpr.condition() && thenExpr==ifExpr.thenExpr() &&
		   elseExpr==ifExpr.elseExpr()) ? 
	      ifExpr : condition.thenElse(thenExpr, elseExpr);
	return cache(ifExpr,ret);
}
 
Example 7
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 { i: IfIntExpression | i.condition = intExpr.condition.accept(this) &&
 *                             i.thenExpr = intExpr.thenExpr.accept(this) &&
 *                             i.elseExpr = intExpr.elseExpr.accept(this) }
 */
public IntExpression visit(IfIntExpression intExpr) {
	IntExpression ret = lookup(intExpr);
	if (ret!=null) return ret;

	final Formula condition = intExpr.condition().accept(this);
	final IntExpression thenExpr = intExpr.thenExpr().accept(this);
	final IntExpression elseExpr = intExpr.elseExpr().accept(this);
	ret = (condition==intExpr.condition() && thenExpr==intExpr.thenExpr() &&
		   elseExpr==intExpr.elseExpr()) ? 
	      intExpr : condition.thenElse(thenExpr, elseExpr);
	return cache(intExpr,ret);
}
 
Example 8
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public Expression visit(IfExpression expr) { 
	Expression ret = lookup(expr);
	if (ret!=null) return ret;
	
	final Formula cond = expr.condition().accept(this);
	final Expression thenExpr = expr.thenExpr().accept(this);
	final Expression elseExpr = expr.elseExpr().accept(this);
	
	ret = simplify(cond,thenExpr,elseExpr);
	
	if (ret==null) { 
		ret = cond==expr.condition()&&thenExpr==expr.thenExpr()&&elseExpr==expr.elseExpr() ? expr : cond.thenElse(thenExpr, elseExpr);
	}
	return cache(expr,ret);
}
 
Example 9
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public IntExpression visit(IfIntExpression expr) { 
	IntExpression ret = lookup(expr);
	if (ret!=null) return ret;
	
	final Formula cond = expr.condition().accept(this);
	final IntExpression thenExpr = expr.thenExpr().accept(this);
	final IntExpression elseExpr = expr.elseExpr().accept(this);
	
	ret = simplify(cond,thenExpr,elseExpr);
	
	if (ret==null) { 
		ret = cond==expr.condition()&&thenExpr==expr.thenExpr()&&elseExpr==expr.elseExpr() ? expr : cond.thenElse(thenExpr, elseExpr);
	}
	return cache(expr,ret);
}
 
Example 10
Source File: TranslatorTest.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
public final void testFlattening() {
    final List<String> atoms = new ArrayList<String>(9);
    atoms.add("-1");
    atoms.add("0");
    atoms.add("1");
    atoms.add("null");
    for (int i = 0; i < 5; i++) {
        atoms.add("m" + i);
    }
    final Universe u = new Universe(atoms);
    final TupleFactory t = u.factory();

    final Relation[] m = new Relation[5];
    for (int i = 0; i < 5; i++) {
        m[i] = Relation.unary("m" + i);
    }

    final Relation univ = Relation.unary("univ"), none = Relation.unary("none"), iden = Relation.binary("inden"),
                    mutant = Relation.unary("mutant"), inc = Relation.binary("inc"), add = Relation.ternary("add"),
                    i0 = Relation.unary("i0"), zero = Relation.unary("0"), one = Relation.unary("1"),
                    ints = Relation.unary("int");

    final Bounds b = new Bounds(u);

    b.boundExactly(univ, t.allOf(1));
    b.boundExactly(none, t.noneOf(1));
    TupleSet idenSet = t.noneOf(2);
    for (String atom : atoms) {
        idenSet.add(t.tuple(atom, atom));
    }
    b.boundExactly(iden, idenSet);

    b.bound(mutant, t.range(t.tuple("m0"), t.tuple("m4")));
    for (int i = 0; i < 5; i++) {
        b.boundExactly(m[i], t.setOf("m" + i));
    }

    b.bound(i0, t.range(t.tuple("-1"), t.tuple("1")));
    b.boundExactly(zero, t.setOf(t.tuple("0")));
    b.boundExactly(one, t.setOf(t.tuple("1")));
    b.boundExactly(ints, t.allOf(1));
    b.boundExactly(inc, t.setOf(t.tuple("-1", "0"), t.tuple("0", "1")));

    // [1, 1, -1], [1, -1, 0], [1, 0, 1], [-1, 1, 0], [-1, -1, 1],
    // [-1, 0, -1], [0, 1, 1], [0, -1, -1], [0, 0, 0]]
    b.boundExactly(add, t.setOf(t.tuple("1", "1", "-1"), t.tuple("1", "-1", "0"), t.tuple("1", "0", "1"), t.tuple("-1", "1", "0"), t.tuple("-1", "-1", "1"), t.tuple("-1", "0", "-1"), t.tuple("0", "1", "1"), t.tuple("0", "-1", "-1"), t.tuple("0", "0", "0")));

    /*
     * ((one i0 && one mutant) && !((if (i0 in (0 . ^~inc)) then ((add . 0) . i0)
     * else i0) = (if !((if (mutant in m4) then (if (i0 in 0) then 1 else 0) else
     * (if (mutant in m3) then (if ((i0 = 0) || (i0 in (0 . ^~inc))) then 1 else 0)
     * else (if (mutant in m2) then (if (i0 in (0 . ^inc)) then 1 else 0) else (if
     * (mutant in m1) then (if ((i0 = 0) || (i0 in (0 . ^inc))) then 1 else 0) else
     * (if (mutant in m0) then (if !(i0 in 0) then 1 else 0) else (if (i0 in (0 .
     * ^~inc)) then 1 else 0)))))) in 0) then ((add . 0) . i0) else i0)))
     */

    final Formula[] cm = new Formula[5];
    for (int i = 0; i < 5; i++) {
        cm[i] = mutant.in(m[i]);
    }

    // (i0 in (0 . ^~inc))
    final Formula fs0 = i0.in(zero.join(inc.transpose().closure()));
    // (i0 in (0 . ^inc))
    final Formula fs1 = i0.in(zero.join(inc.closure()));
    // (i0 = 0)
    final Formula fs2 = i0.eq(zero);

    final Expression em0 = cm[0].thenElse(i0.in(zero).not().thenElse(one, zero), fs0.thenElse(one, zero));
    final Expression em1 = cm[1].thenElse((fs2.or(fs1)).thenElse(one, zero), em0);
    final Expression em2 = cm[2].thenElse(fs1.thenElse(one, zero), em1);
    final Expression em3 = cm[3].thenElse(fs2.or(fs0).thenElse(one, zero), em2);
    final Expression em4 = cm[4].thenElse(i0.in(zero).thenElse(one, zero), em3);

    final Expression es1 = add.join(zero).join(i0);
    final Expression expr2 = em4.in(zero).not().thenElse(es1, i0);
    final Expression expr1 = fs0.thenElse(es1, i0);

    final Formula f = i0.one().and(mutant.one()).and(expr1.eq(expr2).not());

    final Instance instance = solver.solve(f, b).instance();
    assertNotNull(instance);

    // System.out.println(instance);

}
 
Example 11
Source File: ExpressionUtil.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
private static Formula unsignedOverflow(IntExpression l, IntExpression r) {
	Formula carryFromLSB = lsb(l).and(lsb(r));
	IntExpression shiftedSum = l.shr(one).plus(r.shr(one));
	shiftedSum = carryFromLSB.thenElse(shiftedSum.plus(one), shiftedSum);
	return shiftedSum.and(one.shl(IntConstant.constant(31))).eq(zero).not();
}