Java Code Examples for kodkod.ast.Formula#FALSE

The following examples show how to use kodkod.ast.Formula#FALSE . 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: TranslateAlloyToKodkod.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method that merge a list of conjuncts or disjoints while minimizing
 * the AST depth (external caller should use i==1)
 */
private Formula getSingleFormula(boolean isConjunct, int i, List<Expr> formulas) throws Err {
    // We actually build a "binary heap" where node X's two children are
    // node 2X and node 2X+1
    int n = formulas.size();
    if (n == 0)
        return isConjunct ? Formula.TRUE : Formula.FALSE;
    Formula me = cform(formulas.get(i - 1)), other;
    int child1 = i + i, child2 = child1 + 1;
    if (child1 < i || child1 > n)
        return me;
    other = getSingleFormula(isConjunct, child1, formulas);
    if (isConjunct)
        me = me.and(other);
    else
        me = me.or(other);
    if (child2 < 1 || child2 > n)
        return me;
    other = getSingleFormula(isConjunct, child2, formulas);
    if (isConjunct)
        me = me.and(other);
    else
        me = me.or(other);
    return me;
}
 
Example 2
Source File: ALG195_1.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns conjecture 1.
 *
 * @return co1
 */
public final Formula co1() {

    Formula f = Formula.FALSE;
    for (int i = 0; i < 7; i++) {
        f = f.or(co1h(h[i]));
    }

    return f;
}
 
Example 3
Source File: DNACuts.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the cutChainLength constraint. (Similar to CutChainsAtMost6BasesLong
 * fact, but with the cut chain length as specified during construction.)
 *
 * @return the cutChainLength constraint
 */
public Formula cutChainLength() {
    Formula ret = Formula.FALSE;
    final Variable c = Variable.unary("c");
    for (int i = 0; i < neighbor.length; i++) {
        ret = ret.or(c.join(neighbor[i]).in(JoinLink));
    }
    return ret.forAll(c.oneOf(CutLink));
}
 
Example 4
Source File: A4Solution.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Add the given formula to the list of Kodkod formulas, and associate it with
 * the given Pos object (pos can be null if unknown).
 */
void addFormula(Formula newFormula, Pos pos) throws Err {
    if (solved)
        throw new ErrorFatal("Cannot add an additional formula since solve() has completed.");
    if (formulas.size() > 0 && formulas.get(0) == Formula.FALSE)
        return; // If one formula is false, we don't need the others
    if (newFormula == Formula.FALSE)
        formulas.clear(); // If one formula is false, we don't need the
                         // others
    formulas.add(newFormula);
    if (pos != null && pos != Pos.UNKNOWN)
        k2pos(newFormula, pos);
}
 
Example 5
Source File: A4Solution.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Add the given formula to the list of Kodkod formulas, and associate it with
 * the given Expr object (expr can be null if unknown)
 */
void addFormula(Formula newFormula, Expr expr) throws Err {
    if (solved)
        throw new ErrorFatal("Cannot add an additional formula since solve() has completed.");
    if (formulas.size() > 0 && formulas.get(0) == Formula.FALSE)
        return; // If one formula is false, we don't need the others
    if (newFormula == Formula.FALSE)
        formulas.clear(); // If one formula is false, we don't need the
                         // others
    formulas.add(newFormula);
    if (expr != null)
        k2pos(newFormula, expr);
}
 
Example 6
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 7
Source File: ALG195_1.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns conjecture 1.
 * @return co1
 */
public final Formula co1() {
	
	Formula f = Formula.FALSE;
	for(int i = 0; i < 7; i++) {	
		f = f.or(co1h(h[i]));
	}
	
	return f;
}
 
Example 8
Source File: DNACuts.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns the cutChainLength constraint.  (Similar to 
 * CutChainsAtMost6BasesLong fact, but with the cut 
 * chain length as specified during construction.)
 * @return the cutChainLength constraint
 */
public Formula cutChainLength() {
	Formula ret = Formula.FALSE;
	final Variable c = Variable.unary("c");
	for(int i = 0; i < neighbor.length; i++) {
		ret = ret.or(c.join(neighbor[i]).in(JoinLink));
	}
	return ret.forAll(c.oneOf(CutLink));
}
 
Example 9
Source File: Propagator.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Applies condition propagation to the given formula, using the information from the given simplifier.
 * @return a simplification of the given formula
 */
static Formula apply(Formula formula, Simplifier simplifier) {
	final Propagator p = new Propagator(kodkod.util.nodes.Nodes.roots(formula), simplifier.empties, simplifier.constants);
	final Set<Formula> out = new LinkedHashSet<Formula>();
	for(Formula f : p.conjuncts) { 
		final Formula visited = f.accept(p);
		if (visited==Formula.FALSE) { 
			return Formula.FALSE;
		} else if (visited!=Formula.TRUE) {
			out.add(visited);
		}
	}
	return Formula.and(out);
}
 
Example 10
Source File: Propagator.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
final boolean isFalse(Formula formula) {
	if (formula==Formula.FALSE) return true;
	else if (!conjuncts.contains(formula)) {
		if (formula.getClass()==NotFormula.class) { 
			return conjuncts.contains(((NotFormula)formula).formula());
		} else {
			return negs.contains(formula);
		}
	} 
	return false;
}
 
Example 11
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @return a simplification of !child, if possible, or null otherwise. */
final Formula simplify(Formula child) { 
	// can only simplify in the case of not with constants for the Propagator to work correctly
	if (child==Formula.TRUE) 
		return Formula.FALSE;
	else if (child==Formula.FALSE) 
		return Formula.TRUE;
	else if (child.getClass()==NotFormula.class) 
		return ((NotFormula)child).formula();
	else return null;
}
 
Example 12
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 13
Source File: SolutionIterator.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the trivial solution corresponding to the trivial translation stored
 * in {@code this.translation}, and if {@code this.translation.cnf.solve()} is
 * true, sets {@code this.translation} to a new translation that eliminates the
 * current trivial solution from the set of possible solutions. The latter has
 * the effect of forcing either the translator or the solver to come up with the
 * next solution or return UNSAT. If {@code this.translation.cnf.solve()} is
 * false, sets {@code this.translation} to null.
 *
 * @requires this.translation != null
 * @ensures this.translation is modified to eliminate the current trivial
 *          solution from the set of possible solutions
 * @return current solution
 */
private Solution nextTrivialSolution() {
    final Translation.Whole transl = this.translation;

    final Solution sol = Solver.trivial(transl, translTime); // this also
                                                            // frees up
                                                            // solver
                                                            // resources,
                                                            // if unsat

    if (sol.instance() == null) {
        translation = null; // unsat, no more solutions
    } else {
        trivial++;

        final Bounds bounds = transl.bounds();
        final Bounds newBounds = bounds.clone();
        final List<Formula> changes = new ArrayList<Formula>();

        for (Relation r : bounds.relations()) {
            final TupleSet lower = bounds.lowerBound(r);

            if (lower != bounds.upperBound(r)) { // r may change
                if (lower.isEmpty()) {
                    changes.add(r.some());
                } else {
                    final Relation rmodel = Relation.nary(r.name() + "_" + trivial, r.arity());
                    newBounds.boundExactly(rmodel, lower);
                    changes.add(r.eq(rmodel).not());
                }
            }
        }

        // nothing can change => there can be no more solutions (besides the
        // current trivial one).
        // note that transl.formula simplifies to the constant true with
        // respect to
        // transl.bounds, and that newBounds is a superset of transl.bounds.
        // as a result, finding the next instance, if any, for
        // transl.formula.and(Formula.or(changes))
        // with respect to newBounds is equivalent to finding the next
        // instance of Formula.or(changes) alone.
        final Formula formula = changes.isEmpty() ? Formula.FALSE : Formula.or(changes);

        final long startTransl = System.currentTimeMillis();
        translation = Translator.translate(formula, newBounds, transl.options());
        translTime += System.currentTimeMillis() - startTransl;
    }
    return sol;
}
 
Example 14
Source File: Solver.java    From kodkod with MIT License 4 votes vote down vote up
/**
 * Returns the trivial solution corresponding to the trivial translation stored in {@code this.translation},
 * and if {@code this.translation.cnf.solve()} is true, sets {@code this.translation} to a new translation 
 * that eliminates the current trivial solution from the set of possible solutions.  The latter has the effect 
 * of forcing either the translator or the solver to come up with the next solution or return UNSAT.
 * If {@code this.translation.cnf.solve()} is false, sets {@code this.translation} to null.
 * @requires this.translation != null
 * @ensures this.translation is modified to eliminate the current trivial solution from the set of possible solutions
 * @return current solution
 */
private Solution nextTrivialSolution() {
	final Translation.Whole transl = this.translation;
	
	final Solution sol = trivial(transl, translTime); // this also frees up solver resources, if unsat
	
	if (sol.instance()==null) {
		translation = null; // unsat, no more solutions
	} else {
		trivial++;
		
		final Bounds bounds = transl.bounds();
		final Bounds newBounds = bounds.clone();
		final List<Formula> changes = new ArrayList<Formula>();

		for(Relation r : bounds.relations()) {
			final TupleSet lower = bounds.lowerBound(r); 
			
			if (lower != bounds.upperBound(r)) { // r may change
				if (lower.isEmpty()) { 
					changes.add(r.some());
				} else {
					final Relation rmodel = Relation.nary(r.name()+"_"+trivial, r.arity());
					newBounds.boundExactly(rmodel, lower);	
					changes.add(r.eq(rmodel).not());
				}
			}
		}
		
		// nothing can change => there can be no more solutions (besides the current trivial one).
		// note that transl.formula simplifies to the constant true with respect to 
		// transl.bounds, and that newBounds is a superset of transl.bounds.
		// as a result, finding the next instance, if any, for transl.formula.and(Formula.or(changes)) 
		// with respect to newBounds is equivalent to finding the next instance of Formula.or(changes) alone.
		final Formula formula = changes.isEmpty() ? Formula.FALSE : Formula.or(changes);
		
		final long startTransl = System.currentTimeMillis();
		translation = Translator.translate(formula, newBounds, transl.options());
		translTime += System.currentTimeMillis() - startTransl;
	} 
	return sol;
}
 
Example 15
Source File: Simplifier.java    From quetzal with Eclipse Public License 2.0 votes vote down vote up
boolean isFalse(Formula formula) { return formula==Formula.FALSE; }