Java Code Examples for kodkod.ast.Formula#TRUE

The following examples show how to use kodkod.ast.Formula#TRUE . 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: ALG195_1.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the part of the conjecture 1 that applies to the given h.
 * @return  the part of the conjecture 1 that applies to the given h.
 */
private final Formula co1h(Relation h) {
	Formula f = Formula.TRUE;
	for(Relation x : e1) {
		for(Relation y: e1) {
			Expression expr0 = (y.join(x.join(op1))).join(h); // h(op1(x,y))
			Expression expr1 = (y.join(h)).join((x.join(h)).join(op2)); // op2(h(x),h(y))
			f = f.and(expr0.eq(expr1));
		}
	}
	return f.and(s2.eq(s1.join(h)));
}
 
Example 2
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 Formula simplify(FormulaOperator op, Formula left, Formula right) { 
	switch(op) { 
	case AND : 
		if (left==right)					{ return left; }
		else if (isTrue(left))				{ return right; }
		else if (isTrue(right))				{ return left; } 	
		else if (isFalse(left) || 
				 isFalse(right) || 
				 areInverses(left, right)) 	{ return Formula.FALSE; }
		break;
	case OR : 
		if (left==right)					{ return left; }
		else if (isFalse(left))				{ return right; }
		else if (isFalse(right))			{ return left; } 
		else if (isTrue(left) || 
				 isTrue(right) || 
				 areInverses(left, right)) 	{ return Formula.TRUE; }
		break;
	case IMPLIES : // !left or right
		if (left==right)					{ return Formula.TRUE; }
		else if (isTrue(left))				{ return right; }
		else if (isFalse(right)) 			{ return left; }
		else if (isFalse(left) || 
				 isTrue(right))				{ return Formula.TRUE; }
		break;
	case IFF : // (left and right) or (!left and !right)
		if (left==right)					{ return Formula.TRUE; }
		else if (isTrue(left))				{ return right; }
		else if (isFalse(left))				{ return right.not().accept(this); } 
		else if (isTrue(right))				{ return left; }
		else if (isFalse(right))			{ return left.not().accept(this); }
		else if (areInverses(left, right))	{ return Formula.FALSE; }
		break;
	default :
		Assertions.UNREACHABLE();
	}
	return null;
}
 
Example 3
Source File: PrettyPrinter.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * @ensures this.tokens' = concat[ this.tokens, node.quantifier,
 *          tokenize[node.decls], "|", tokenize[ node.formula ] ]
 */
@Override
public void visit(QuantifiedFormula node) {
    keyword(node.quantifier());
    node.decls().accept(this);
    if (node.domain() != Formula.TRUE) {
        infix("| domain{");
        indent++;
        newline();
        node.domain().accept(this);
        indent--;
        newline();
        infix("}");
        infix("| body{");
        indent++;
        newline();
        node.body().accept(this);
        indent--;
        newline();
        infix("}");
    } else {
        infix("|");
        indent++;
        newline();
        node.body().accept(this);
        indent--;
    }
}
 
Example 4
Source File: ALG195_1.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * States that op is a latin square over s = e[0] +...+ e[6].
 *
 * @requires e's are unary, s is unary, op is ternary
 */
private static Formula opCoversRange(Relation[] e, Relation s, Relation op) {
    Formula f = Formula.TRUE;
    for (Relation x : e) {
        f = f.and(s.eq(s.join(x.join(op)))).and(s.eq(x.join(s.join(op))));
    }
    return f;
}
 
Example 5
Source File: Viktor.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the equations to be satisfied.
 * @return equations to be satisfied.
 */
public final Formula equations() {
	
	// each b <= cols-1
	Formula f0 = Formula.TRUE;
	final IntConstant colConst = IntConstant.constant(cols-1);
	for(IntExpression bi: b) {
		f0 = f0.and(bi.lte(colConst));
	}
	
	final Variable[] y = new Variable[rows];
	for(int i = 0; i < rows; i++) {
		y[i] = Variable.unary("y"+i);
	}
	
	Decls decls = y[0].oneOf(INTS);
	for(int i = 1; i < rows; i++)
		decls = decls.and(y[i].oneOf(INTS));
	
	Formula f1 = Formula.TRUE;
	final Expression[] combo = new Expression[rows];
	for(int i = 0; i < cols; i++) {
		for(int j = i+1; j < cols; j++) {
			for(int k = j+1; k < cols; k++) {
				Formula f2 = Formula.TRUE;
				for(int m = 0; m < rows; m++) {
					combo[0] = a[m][i];
					combo[1] = a[m][j];
					combo[2] = a[m][k];
					f2 = f2.and(conditionalSum(combo, y, 0, rows-1).eq(b[m]));
				}
				f1 = f1.and(f2.not().forAll(decls));
			}
		}
	}
	return f0.and(f1); 
}
 
Example 6
Source File: ALG195_1.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Parametrization of axioms 12 and 13.
 *
 * @requires e's are unary, op is ternary
 */
Formula ax12and13(Relation[] e, Relation op) {
    Formula f = Formula.TRUE;
    for (Relation r : e) {
        f = f.and(r.join(r.join(op)).eq(r).not());
    }
    return f;
}
 
Example 7
Source File: ALG195_1.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns axioms 16-22.
 *
 * @return axioms 16-22.
 */
public final Formula ax16_22() {
    Formula f = Formula.TRUE;
    for (int i = 0; i < 7; i++) {
        f = f.and(ax16_22(e2[i], h[i]));
    }
    return f;
}
 
Example 8
Source File: ALG195_1.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the part of the conjecture 1 that applies to the given h.
 *
 * @return the part of the conjecture 1 that applies to the given h.
 */
private final Formula co1h(Relation h) {
    Formula f = Formula.TRUE;
    for (Relation x : e1) {
        for (Relation y : e1) {
            Expression expr0 = (y.join(x.join(op1))).join(h); // h(op1(x,y))
            Expression expr1 = (y.join(h)).join((x.join(h)).join(op2)); // op2(h(x),h(y))
            f = f.and(expr0.eq(expr1));
        }
    }
    return f.and(s2.eq(s1.join(h)));
}
 
Example 9
Source File: ALG195_1.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Parametrization of axioms 12 and 13.
 * @requires e's are unary, op is ternary
 */
Formula ax12and13(Relation[] e, Relation op) {
	Formula f = Formula.TRUE;
	for(Relation r : e) {
		f = f.and(r.join(r.join(op)).eq(r).not());
	}
	return f;
}
 
Example 10
Source File: ALG195_1.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Parametrization of axioms 3 and 6.
 * @requires s is unary, op is ternary
 */
private static Formula ax3and6(Relation[] e, Relation op) {
	Formula f = Formula.TRUE;
	for( Relation x : e) {
		for (Relation y: e) {
			Expression expr0 = x.join(y.join(op)); // op(y,x)
			Expression expr1 = y.join(expr0.join(op)); // op(op(y,x),y)
			Expression expr2 = y.join(expr1.join(op)); // op(op(op(y,x),y),y)
			f = f.and(expr2.eq(x));
		}
	}
	return f;
}
 
Example 11
Source File: Viktor.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the equations to be satisfied.
 *
 * @return equations to be satisfied.
 */
public final Formula equations() {

    // each b <= cols-1
    Formula f0 = Formula.TRUE;
    final IntConstant colConst = IntConstant.constant(cols - 1);
    for (IntExpression bi : b) {
        f0 = f0.and(bi.lte(colConst));
    }

    final Variable[] y = new Variable[rows];
    for (int i = 0; i < rows; i++) {
        y[i] = Variable.unary("y" + i);
    }

    Decls decls = y[0].oneOf(INTS);
    for (int i = 1; i < rows; i++)
        decls = decls.and(y[i].oneOf(INTS));

    Formula f1 = Formula.TRUE;
    final Expression[] combo = new Expression[rows];
    for (int i = 0; i < cols; i++) {
        for (int j = i + 1; j < cols; j++) {
            for (int k = j + 1; k < cols; k++) {
                Formula f2 = Formula.TRUE;
                for (int m = 0; m < rows; m++) {
                    combo[0] = a[m][i];
                    combo[1] = a[m][j];
                    combo[2] = a[m][k];
                    f2 = f2.and(conditionalSum(combo, y, 0, rows - 1).eq(b[m]));
                }
                f1 = f1.and(f2.not().forAll(decls));
            }
        }
    }
    return f0.and(f1);
}
 
Example 12
Source File: BoundsComputer.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method that returns the constraint that the sig has exactly "n"
 * elements, or at most "n" elements
 */
private Formula size(Sig sig, int n, boolean exact) {
    Expression a = sol.a2k(sig);
    if (n <= 0)
        return a.no();
    if (n == 1)
        return exact ? a.one() : a.lone();
    Formula f = exact ? Formula.TRUE : null;
    Decls d = null;
    Expression sum = null;
    while (n > 0) {
        n--;
        Variable v = Variable.unary("v" + Integer.toString(TranslateAlloyToKodkod.cnt++));
        kodkod.ast.Decl dd = v.oneOf(a);
        if (d == null)
            d = dd;
        else
            d = dd.and(d);
        if (sum == null)
            sum = v;
        else {
            if (f != null)
                f = v.intersection(sum).no().and(f);
            sum = v.union(sum);
        }
    }
    if (f != null)
        return sum.eq(a).and(f).forSome(d);
    else
        return a.no().or(sum.eq(a).forSome(d));
}
 
Example 13
Source File: TranslateAlloyToKodkod.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public Object visit(ExprConstant x) throws Err {
    switch (x.op) {
        case MIN :
            return IntConstant.constant(min); // TODO
        case MAX :
            return IntConstant.constant(max); // TODO
        case NEXT :
            return A4Solution.KK_NEXT;
        case TRUE :
            return Formula.TRUE;
        case FALSE :
            return Formula.FALSE;
        case EMPTYNESS :
            return Expression.NONE;
        case IDEN :
            return Expression.IDEN.intersection(a2k(UNIV).product(Expression.UNIV));
        case STRING :
            Expression ans = s2k(x.string);
            if (ans == null)
                throw new ErrorFatal(x.pos, "String literal " + x + " does not exist in this instance.\n");
            return ans;
        case NUMBER :
            int n = x.num();
            // [am] const
            // if (n<min) throw new ErrorType(x.pos, "Current bitwidth is
            // set to "+bitwidth+", thus this integer constant "+n+" is
            // smaller than the minimum integer "+min);
            // if (n>max) throw new ErrorType(x.pos, "Current bitwidth is
            // set to "+bitwidth+", thus this integer constant "+n+" is
            // bigger than the maximum integer "+max);
            return IntConstant.constant(n).toExpression();
    }
    throw new ErrorFatal(x.pos, "Unsupported operator (" + x.op + ") encountered during ExprConstant.accept()");
}
 
Example 14
Source File: ALG195_1.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * States that op is a latin square over s = e[0] +...+ e[6].
 * @requires e's are unary, s is unary, op is ternary
 */
private static Formula opCoversRange(Relation[] e, Relation s, Relation op) {
	Formula f = Formula.TRUE;
	for( Relation x : e) {
		f = f.and(s.eq(s.join(x.join(op)))).and(s.eq(x.join(s.join(op))));
	}	
	return f;
}
 
Example 15
Source File: SymmetryBreaker.java    From kodkod with MIT License 4 votes vote down vote up
/**
	 * If possible, breaks symmetry on the given total ordering predicate and returns a formula
	 * f such that the meaning of total with respect to this.bounds is equivalent to the
	 * meaning of f with respect to this.bounds'. If symmetry cannot be broken on the given predicate, returns null.  
	 * 
	 * <p>We break symmetry on the relation constrained by the given predicate iff 
	 * total.first, total.last, and total.ordered have the same upper bound, which, when 
	 * cross-multiplied with itself gives the upper bound of total.relation. Assuming that this is the case, 
	 * we then break symmetry on total.relation, total.first, total.last, and total.ordered using one of the methods
	 * described in {@linkplain #breakMatrixSymmetries(Map, boolean)}; the method used depends
	 * on the value of the "aggressive" flag.
	 * The partition that formed the upper bound of total.ordered is removed from this.symmetries.</p>
	 * 
	 * @return null if symmetry cannot be broken on total; otherwise returns a formula
	 * f such that the meaning of total with respect to this.bounds is equivalent to the
	 * meaning of f with respect to this.bounds' 
	 * @ensures this.symmetries and this.bounds are modified as described in {@linkplain #breakMatrixSymmetries(Map, boolean)}
	 * iff total.first, total.last, and total.ordered have the same upper bound, which, when 
	 * cross-multiplied with itself gives the upper bound of total.relation
	 * 
	 * @see #breakMatrixSymmetries(Map,boolean)
	 */
	private final Formula breakTotalOrder(RelationPredicate.TotalOrdering total, boolean aggressive) {
		final Relation first = total.first(), last = total.last(), ordered = total.ordered(), relation = total.relation();
		final IntSet domain = bounds.upperBound(ordered).indexView();		
	
		if (symmetricColumnPartitions(ordered)!=null && 
			bounds.upperBound(first).indexView().contains(domain.min()) && 
			bounds.upperBound(last).indexView().contains(domain.max())) {
			
			// construct the natural ordering that corresponds to the ordering of the atoms in the universe
			final IntSet ordering = Ints.bestSet(usize*usize);
			int prev = domain.min();
			for(IntIterator atoms = domain.iterator(prev+1, usize); atoms.hasNext(); ) {
				int next = atoms.next();
				ordering.add(prev*usize + next);
				prev = next;
			}
			
			if (ordering.containsAll(bounds.lowerBound(relation).indexView()) &&
				bounds.upperBound(relation).indexView().containsAll(ordering)) {
				
				// remove the ordered partition from the set of symmetric partitions
				removePartition(domain.min());
				
				final TupleFactory f = bounds.universe().factory();
				
				if (aggressive) {
					bounds.boundExactly(first, f.setOf(f.tuple(1, domain.min())));
					bounds.boundExactly(last, f.setOf(f.tuple(1, domain.max())));
					bounds.boundExactly(ordered, bounds.upperBound(total.ordered()));
					bounds.boundExactly(relation, f.setOf(2, ordering));
					
					return Formula.TRUE;
					
				} else {
					final Relation firstConst = Relation.unary("SYM_BREAK_CONST_"+first.name());
					final Relation lastConst = Relation.unary("SYM_BREAK_CONST_"+last.name());
					final Relation ordConst = Relation.unary("SYM_BREAK_CONST_"+ordered.name());
					final Relation relConst = Relation.binary("SYM_BREAK_CONST_"+relation.name());
					bounds.boundExactly(firstConst, f.setOf(f.tuple(1, domain.min())));
					bounds.boundExactly(lastConst, f.setOf(f.tuple(1, domain.max())));
					bounds.boundExactly(ordConst, bounds.upperBound(total.ordered()));
					bounds.boundExactly(relConst, f.setOf(2, ordering));
					
					return Formula.and(first.eq(firstConst), last.eq(lastConst), ordered.eq(ordConst), relation.eq(relConst));
//					return first.eq(firstConst).and(last.eq(lastConst)).and( ordered.eq(ordConst)).and( relation.eq(relConst));
				}

			}
		}
		
		return null;
	}
 
Example 16
Source File: FloatTestBase.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
protected FloatTestBase() {
	this(Formula.TRUE, Collections.<Relation>emptySet());
}
 
Example 17
Source File: SymmetryBreaker.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
/**
 * If possible, breaks symmetry on the given total ordering predicate and
 * returns a formula f such that the meaning of total with respect to
 * this.bounds is equivalent to the meaning of f with respect to this.bounds'.
 * If symmetry cannot be broken on the given predicate, returns null.
 * <p>
 * We break symmetry on the relation constrained by the given predicate iff
 * total.first, total.last, and total.ordered have the same upper bound, which,
 * when cross-multiplied with itself gives the upper bound of total.relation.
 * Assuming that this is the case, we then break symmetry on total.relation,
 * total.first, total.last, and total.ordered using one of the methods described
 * in {@linkplain #breakMatrixSymmetries(Map, boolean)}; the method used depends
 * on the value of the "agressive" flag. The partition that formed the upper
 * bound of total.ordered is removed from this.symmetries.
 * </p>
 *
 * @return null if symmetry cannot be broken on total; otherwise returns a
 *         formula f such that the meaning of total with respect to this.bounds
 *         is equivalent to the meaning of f with respect to this.bounds'
 * @ensures this.symmetries and this.bounds are modified as desribed in
 *          {@linkplain #breakMatrixSymmetries(Map, boolean)} iff total.first,
 *          total.last, and total.ordered have the same upper bound, which, when
 *          cross-multiplied with itself gives the upper bound of total.relation
 * @see #breakMatrixSymmetries(Map,boolean)
 */
private final Formula breakTotalOrder(RelationPredicate.TotalOrdering total, boolean aggressive) {
    final Relation first = total.first(), last = total.last(), ordered = total.ordered(),
                    relation = total.relation();
    final IntSet domain = bounds.upperBound(ordered).indexView();

    if (symmetricColumnPartitions(ordered) != null && bounds.upperBound(first).indexView().contains(domain.min()) && bounds.upperBound(last).indexView().contains(domain.max())) {

        // construct the natural ordering that corresponds to the ordering
        // of the atoms in the universe
        final IntSet ordering = Ints.bestSet(usize * usize);
        int prev = domain.min();
        for (IntIterator atoms = domain.iterator(prev + 1, usize); atoms.hasNext();) {
            int next = atoms.next();
            ordering.add(prev * usize + next);
            prev = next;
        }

        if (ordering.containsAll(bounds.lowerBound(relation).indexView()) && bounds.upperBound(relation).indexView().containsAll(ordering)) {

            // remove the ordered partition from the set of symmetric
            // partitions
            removePartition(domain.min());

            final TupleFactory f = bounds.universe().factory();

            if (aggressive) {
                bounds.boundExactly(first, f.setOf(f.tuple(1, domain.min())));
                bounds.boundExactly(last, f.setOf(f.tuple(1, domain.max())));
                bounds.boundExactly(ordered, bounds.upperBound(total.ordered()));
                bounds.boundExactly(relation, f.setOf(2, ordering));

                return Formula.TRUE;

            } else {
                final Relation firstConst = Relation.unary("SYM_BREAK_CONST_" + first.name());
                final Relation lastConst = Relation.unary("SYM_BREAK_CONST_" + last.name());
                final Relation ordConst = Relation.unary("SYM_BREAK_CONST_" + ordered.name());
                final Relation relConst = Relation.binary("SYM_BREAK_CONST_" + relation.name());
                bounds.boundExactly(firstConst, f.setOf(f.tuple(1, domain.min())));
                bounds.boundExactly(lastConst, f.setOf(f.tuple(1, domain.max())));
                bounds.boundExactly(ordConst, bounds.upperBound(total.ordered()));
                bounds.boundExactly(relConst, f.setOf(2, ordering));

                return Formula.and(first.eq(firstConst), last.eq(lastConst), ordered.eq(ordConst), relation.eq(relConst));
                // return first.eq(firstConst).and(last.eq(lastConst)).and(
                // ordered.eq(ordConst)).and( relation.eq(relConst));
            }

        }
    }

    return null;
}
 
Example 18
Source File: Proc.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
public Some4All(Bounds bounds, QuantifiedFormula qf, Proc body) {
    this(bounds, Formula.TRUE, new QuantProc(qf, qf.body().quantify(qf.quantifier().opposite, qf.decls(), qf.domain()), body));
}
 
Example 19
Source File: Proc.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
public Fixpoint(Bounds bounds, FixFormula qf, Proc formProc, Proc condProc) {
    this(bounds, Formula.TRUE, new QuantProc(qf, qf.formula(), formProc, condProc));
}
 
Example 20
Source File: ALG195.java    From org.alloytools.alloy with Apache License 2.0 2 votes vote down vote up
/**
 * Parametrization of axioms 12 and 13.
 *
 * @requires e's are unary, op is ternary
 */
@Override
Formula ax12and13(Relation[] e, Relation op) {
    return Formula.TRUE;
}