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

The following examples show how to use kodkod.ast.Formula#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(formula) and returns the cached value, if any. If a replacement
 * has not been cached, visits the formula's children. If nothing changes, the
 * argument is cached and returned, otherwise a replacement formula is cached
 * and returned.
 *
 * @return { e: Expression | e.op = formula.op && #e.children =
 *         #formula.children && all i: [0..formula.children) | e.child(i) =
 *         formula.child(i).accept(delegate) }
 */
@Override
public Formula visit(NaryFormula formula) {
    Formula ret = lookup(formula);
    if (ret != null)
        return ret;

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

    ret = allSame ? formula : Formula.compose(formula.op(), visited);
    return cache(formula, 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(FormulaOperator 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 Formula[] exprs = new Formula[i];
        exprs[0] = r1[0].some();
        Formula binExpr = r1[0].some();
        for (int j = 1; j < i; j++) {
            binExpr = binExpr.compose(op, r1[j % 4].some());
            exprs[j] = r1[j % 4].some();
        }
        Formula nExpr = Formula.compose(op, exprs);
        final Solution sol = solver.solve(binExpr.iff(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(FormulaOperator 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 Formula[] exprs = new Formula[i];
        exprs[0] = r1[0].some();
        Formula binExpr = r1[0].some();
        for (int j = 1; j < i; j++) {
            binExpr = binExpr.compose(op, r1[j % 4].some());
            exprs[j] = r1[j % 4].some();
        }
        Formula nExpr = Formula.compose(op, exprs);
        final Solution sol = solver.solve(binExpr.iff(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(formula) and returns the cached value, if any.  
 * If a replacement has not been cached, visits the formula's 
 * children.  If nothing changes, the argument is cached and
 * returned, otherwise a replacement formula is cached and returned.
 * @return { e: Expression | e.op = formula.op && #e.children = #formula.children && all i: [0..formula.children) | e.child(i) = formula.child(i).accept(this) }
 */
public Formula visit(NaryFormula formula) {
	Formula ret = lookup(formula);
	if (ret!=null) return ret;
	
	final Formula[] visited = new Formula[formula.size()];
	boolean allSame = true;
	for(int i = 0 ; i < visited.length; i++) { 
		final Formula child = formula.child(i);
		visited[i] = child.accept(this);
		allSame = allSame && visited[i]==child;
	}
	
	ret = allSame ? formula : Formula.compose(formula.op(), visited);
	return cache(formula,ret);
}
 
Example 5
Source File: TranslatorTest.java    From kodkod with MIT License 6 votes vote down vote up
private final void testNary(FormulaOperator 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 Formula[] exprs = new Formula[i];
		exprs[0] = r1[0].some();
		Formula binExpr = r1[0].some();
		for(int j = 1; j < i; j++) { 
			binExpr = binExpr.compose(op, r1[j%4].some());
			exprs[j] = r1[j%4].some();
		}
		Formula nExpr = Formula.compose(op, exprs);
		final Solution sol = solver.solve(binExpr.iff(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(FormulaOperator 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 Formula[] exprs = new Formula[i];
		exprs[0] = r1[0].some();
		Formula binExpr = r1[0].some();
		for(int j = 1; j < i; j++) { 
			binExpr = binExpr.compose(op, r1[j%4].some());
			exprs[j] = r1[j%4].some();
		}
		Formula nExpr = Formula.compose(op, exprs);
		final Solution sol = solver.solve(binExpr.iff(nExpr).not(), bounds);
		assertNull(sol.instance());	
	}
	
}
 
Example 7
Source File: AbstractReplacer.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(binFormula) and returns the cached value, if any. If a
 * replacement has not been cached, visits the formula's children. If nothing
 * changes, the argument is cached and returned, otherwise a replacement formula
 * is cached and returned.
 *
 * @return { b: BinaryFormula | b.left = binExpr.left.accept(delegate) &&
 *         b.right = binExpr.right.accept(delegate) && b.op = binExpr.op }
 */
@Override
public Formula visit(BinaryFormula binFormula) {
    Formula ret = lookup(binFormula);
    if (ret != null)
        return ret;

    final Formula left = binFormula.left().accept(delegate);
    final Formula right = binFormula.right().accept(delegate);
    ret = (left == binFormula.left() && right == binFormula.right()) ? binFormula : left.compose(binFormula.op(), right);
    return cache(binFormula, ret);
}
 
Example 8
Source File: Skolemizer.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * If not cached, visits the formula's children with appropriate settings for
 * the negated flag and the skolemDepth parameter.
 *
 * @see kodkod.ast.visitor.AbstractReplacer#visit(kodkod.ast.NaryFormula)
 */
@Override
public final Formula visit(NaryFormula bf) {
    Formula ret = lookup(bf);
    if (ret != null)
        return ret;

    final int oldDepth = skolemDepth;
    final FormulaOperator op = bf.op();

    switch (op) {
        case AND :
            if (negated)
                skolemDepth = -1;
            break;
        case OR :
            if (!negated)
                skolemDepth = -1;
            break;
        default :
            throw new IllegalArgumentException("Unknown nary operator: " + op);
    }

    final Formula[] visited = new Formula[bf.size()];
    boolean allSame = true;
    for (int i = 0; i < visited.length; i++) {
        final Formula child = bf.child(i);
        visited[i] = child.accept(this);
        allSame = allSame && (child == visited[i]);
    }
    ret = allSame ? bf : Formula.compose(op, visited);

    skolemDepth = oldDepth;

    return source(cache(bf, ret), bf);
}
 
Example 9
Source File: AbstractReplacer.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(binFormula) and returns the cached value, if any.  
 * If a replacement has not been cached, visits the formula's 
 * children.  If nothing changes, the argument is cached and
 * returned, otherwise a replacement formula is cached and returned.
 * @return { b: BinaryFormula | b.left = binExpr.left.accept(this) &&
 *                              b.right = binExpr.right.accept(this) && b.op = binExpr.op }
 */
public Formula visit(BinaryFormula binFormula) {
	Formula ret = lookup(binFormula);
	if (ret!=null) return ret;
	
	final Formula left  = binFormula.left().accept(this);
	final Formula right = binFormula.right().accept(this);
	ret = (left==binFormula.left() && right==binFormula.right()) ? 
		  binFormula : left.compose(binFormula.op(), right);     
	return cache(binFormula,ret);
}
 
Example 10
Source File: Skolemizer.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * If not cached, visits the formula's children with appropriate settings
 * for the negated flag and the skolemDepth parameter.
 * @see kodkod.ast.visitor.AbstractReplacer#visit(kodkod.ast.NaryFormula)
 */
public final Formula visit(NaryFormula bf) {
	Formula ret = lookup(bf);
	if (ret!=null) return ret;			
	
	final int oldDepth = skolemDepth;
	final FormulaOperator op = bf.op();
	
	switch(op) { 
	case AND : if (negated)  skolemDepth = -1; break;
	case OR  : if (!negated) skolemDepth = -1; break;
	default  : throw new IllegalArgumentException("Unknown nary operator: " + op);
	}
	
	final Formula[] visited = new Formula[bf.size()];
	boolean allSame = true;
	for(int i = 0; i < visited.length; i++) { 
		final Formula child = bf.child(i);
		visited[i] = child.accept(this);
		allSame = allSame && (child==visited[i]);
	}
	ret = allSame ? bf : Formula.compose(op, visited);
	
	skolemDepth = oldDepth;
	
	return source(cache(bf,ret),bf);
}
 
Example 11
Source File: PartialCannonicalizer.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public Formula visit(BinaryFormula formula) { 
	Formula ret = lookup(formula);
	if (ret!=null) return ret;
	final FormulaOperator op = formula.op();
	if (op==FormulaOperator.AND) {
		final Set<Formula> conjuncts = kodkod.util.nodes.Nodes.roots(formula);
		if (conjuncts.size()>2) { 
			return cache(formula, Formula.and(conjuncts).accept(this));
		}
	}
	
	final Formula left = formula.left().accept(this);
	final Formula right = formula.right().accept(this);
	
	ret = simplify(op, left, right);
	
	if (ret==null) {
	
		final int hash = hash(op, left, right);
		for(Iterator<PartialCannonicalizer.Holder<Formula>> itr = formulas.get(hash); itr.hasNext(); ) {
			final Formula next = itr.next().obj;
			if (next instanceof BinaryFormula) { 
				final BinaryFormula hit = (BinaryFormula) next;
				if (hit.op()==op && hit.left()==left && hit.right()==right) { 
					return cache(formula, hit);
				}
			}
		}

		ret = left==formula.left()&&right==formula.right() ? formula : left.compose(op, right);
		formulas.add(new PartialCannonicalizer.Holder<Formula>(ret, hash));
	}
	
	return cache(formula,ret);
}
 
Example 12
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public Formula visit(BinaryFormula expr) { 
	Formula ret = lookup(expr);
	if (ret!=null) return ret;
	final FormulaOperator op = expr.op();
	final Formula left = expr.left().accept(this);
	final Formula 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 13
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
public Formula visit(NaryFormula formula) { 
	Formula ret = lookup(formula);
	if (ret!=null) return ret;
	
	final FormulaOperator op = formula.op();
	
	final List<Formula> children = simplify(op, visitAll(formula));
	final int size = children.size();
	if (size<2) { 
		return cache(formula, Formula.compose(op, children));
	} else {
		ret = formula.size()==size && allSame(formula,children) ? formula : Formula.compose(op, children);	
		return cache(formula,ret);
	}	
}