kodkod.ast.QuantifiedFormula Java Examples

The following examples show how to use kodkod.ast.QuantifiedFormula. 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: TranslateKodkodToJava.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void visit(QuantifiedFormula x) {
    String newname = makename(x);
    if (newname == null)
        return;
    String d = make(x.decls());
    String f = make(x.formula());
    switch (x.quantifier()) {
        case ALL :
            file.printf("Formula %s=%s.forAll(%s);%n", newname, f, d);
            break;
        case SOME :
            file.printf("Formula %s=%s.forSome(%s);%n", newname, f, d);
            break;
        default :
            throw new RuntimeException("Unknown kodkod quantifier \"" + x.quantifier() + "\" encountered");
    }
}
 
Example #2
Source File: FOL2BoolTranslator.java    From kodkod with MIT License 6 votes vote down vote up
/** 
 * Calls lookup(quantFormula) and returns the cached value, if any.  
 * If a translation has not been cached, translates the formula,
 * calls cache(...) on it and returns it.
 * @return let t = lookup(quantFormula) | some t => t, 
 *   cache(quantFormula, translate(quantFormula))
 */
public final BooleanValue visit(QuantifiedFormula quantFormula) {
	BooleanValue ret = lookup(quantFormula);
	if (ret!=null) return ret;

	final Quantifier quantifier = quantFormula.quantifier();

	switch(quantifier) {
	case ALL		: 
		final BooleanAccumulator and = BooleanAccumulator.treeGate(Operator.AND);
		all(quantFormula.decls(), quantFormula.formula(), 0, BooleanConstant.FALSE, and); 
		ret = interpreter.factory().accumulate(and);
		break;
	case SOME	: 
		final BooleanAccumulator or = BooleanAccumulator.treeGate(Operator.OR);
		some(quantFormula.decls(), quantFormula.formula(), 0, BooleanConstant.TRUE, or); 
		ret = interpreter.factory().accumulate(or);
		break;
	default :
		throw new IllegalArgumentException("Unknown quantifier: " + quantifier);
	}

	return cache(quantFormula,ret);
}
 
Example #3
Source File: FullNegationPropagator.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
@Override
public final void visit(QuantifiedFormula qf) {
    if (visited(qf))
        return;
    FullNegationPropagator fneBody = new FullNegationPropagator(shared, annotations);
    fneBody.negated = negated;
    boolean wasNegated = negated;
    negated = false;
    qf.body().accept(fneBody);

    FullNegationPropagator fneDomain = new FullNegationPropagator(shared, annotations);
    qf.domain().accept(fneDomain);

    if (fneBody.hasChanged || fneDomain.hasChanged || wasNegated) {
        Formula qfBody = Formula.and(fneBody.conjuncts);
        Quantifier quant = wasNegated ? qf.quantifier().opposite : qf.quantifier();
        addConjunct(qfBody.quantify(quant, qf.decls(), Formula.and(fneDomain.conjuncts)), false, qf);
        hasChanged = true;
    } else {
        addConjunct(qf);
    }
    negated = wasNegated;
}
 
Example #4
Source File: FOL2BoolTranslator.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
/**
 * Calls lookup(quantFormula) and returns the cached value, if any. If a
 * translation has not been cached, translates the formula, calls cache(...) on
 * it and returns it.
 *
 * @return let t = lookup(quantFormula) | some t => t, cache(quantFormula,
 *         translate(quantFormula))
 */
@Override
public final BooleanValue visit(QuantifiedFormula quantFormula) {
    BooleanValue ret = lookup(quantFormula);
    if (ret != null)
        return ret;

    final Quantifier quantifier = quantFormula.quantifier();
    switch (quantifier) {
        case ALL :
            final BooleanAccumulator and = BooleanAccumulator.treeGate(Operator.AND);
            all(quantFormula.decls(), quantFormula.formula(), 0, BooleanConstant.FALSE, and);
            ret = interpreter.factory().accumulate(and);
            break;
        case SOME :
            final BooleanAccumulator or = BooleanAccumulator.treeGate(Operator.OR);
            some(quantFormula.decls(), quantFormula.formula(), 0, BooleanConstant.TRUE, or);
            ret = interpreter.factory().accumulate(or);
            break;
        default :
            throw new IllegalArgumentException("Unknown quantifier: " + quantifier);
    }
    return cache(quantFormula, ret);
}
 
Example #5
Source File: PrenexNFConverter.java    From org.alloytools.alloy with Apache License 2.0 6 votes vote down vote up
@Override
public Formula visit(NaryFormula formula) {
    final ArrayList<Formula> children = new ArrayList<Formula>(formula.size());
    boolean allSame = true;
    boolean noQuant = true;
    for (Formula ch : formula) {
        Formula ch2 = ch.accept(this);
        allSame = allSame && ch == ch2;
        noQuant = noQuant && !(ch2 instanceof QuantifiedFormula);
        children.add(ch2);
    }
    Formula ans;
    if (allSame && noQuant) {
        ans = formula;
    } else {
        ans = children.get(0);
        for (int i = 1; i < children.size(); i++)
            ans = new Pair(ans, children.get(i)).compose(formula.op());
    }
    return saveMapping(ans, formula);
}
 
Example #6
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 #7
Source File: AnnotatedNode.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a Detector that will return TRUE when applied to a descendent of
 * this.node iff the descendent contains a quantified formula.
 *
 * @return a Detector that will return TRUE when applied to a descendent of
 *         this.node iff the descendent contains a quantified formula.
 */
public final AbstractDetector quantifiedFormulaDetector() {
    return new AbstractDetector(sharedNodes) {

        @Override
        public Boolean visit(QuantifiedFormula quantFormula) {
            return cache(quantFormula, true);
        }
    };
}
 
Example #8
Source File: HOLSome4AllTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
@Test
public void testE9() {
    // UNSAT: some s: ints - (-1) |
    // s > 3 || (some ns: set Node | #ns > s + 3)
    Formula cnd = si.gt(I3);
    QuantifiedFormula innerSomeQF = (QuantifiedFormula) ns.count().gt(si.plus(I3)).forSome(ns.setOf(Node));
    Decl someDecls = s.oneOf(Expression.INTS.difference(M1.toExpression()));
    Formula f = cnd.or(innerSomeQF).forSome(someDecls);
    assertFalse(solve(f).sat());
}
 
Example #9
Source File: HOLSome4AllTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
@Test
public void testE9i() {
    // SAT: some s: ints |
    // s > 3 || (some ns: set Node | #ns > s + 3)
    Formula cnd = si.gt(I3);
    QuantifiedFormula innerSomeQF = (QuantifiedFormula) ns.count().gt(si.plus(I3)).forSome(ns.setOf(Node));
    Decl someDecls = s.oneOf(Expression.INTS);
    Formula f = cnd.or(innerSomeQF).forSome(someDecls);
    Solution sol = solve(f);
    assertTrue(sol.sat());
    assertEquals(-1, evalS(sol));
}
 
Example #10
Source File: HOLSome4AllTest.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
@Test
public void testE9ii() {
    // SAT: some s: ints - 1 |
    // s > 2 || (some ns: set Node | #ns > s + 3)
    Formula cnd = si.gt(I2);
    QuantifiedFormula innerSomeQF = (QuantifiedFormula) ns.count().gt(si.plus(I3)).forSome(ns.setOf(Node));
    Decl someDecls = s.oneOf(Expression.INTS.difference(M1.toExpression()));
    Formula f = cnd.or(innerSomeQF).forSome(someDecls);
    Solution sol = solve(f);
    assertTrue(sol.sat());
    assertEquals(3, evalS(sol));
}
 
Example #11
Source File: AbstractReplacer.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(quantFormula) 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 { q: QuantifiedFormula | q.declarations = quantFormula.declarations.accept(this) &&
 *                                  q.formula = quantFormula.formula.accept(this) }
 */
public Formula visit(QuantifiedFormula quantFormula) {
	Formula ret = lookup(quantFormula);
	if (ret!=null) return ret;
	
	final Decls decls = (Decls)quantFormula.decls().accept(this);
	final Formula formula = quantFormula.formula().accept(this);
	ret = (decls==quantFormula.decls() && formula==quantFormula.formula()) ? 
		  quantFormula : formula.quantify(quantFormula.quantifier(), decls);
	return cache(quantFormula,ret);
}
 
Example #12
Source File: AbstractCollector.java    From kodkod with MIT License 5 votes vote down vote up
/** 
 * Calls lookup(quantFormula) and returns the cached value, if any.  
 * If no cached value exists, visits each child, caches the
 * union of the children's return values and returns it. 
 * @return let x = lookup(quantFormula) | 
 *          x != null => x,  
 *          cache(quantFormula, quantFormula.declarations.accept(this) + quantFormula.formula.accept(this)) 
 */
public Set<T> visit(QuantifiedFormula quantFormula) {
	Set<T> ret = lookup(quantFormula);
	if (ret!=null) return ret;		
	ret = newSet();
	ret.addAll(quantFormula.decls().accept(this));
	ret.addAll(quantFormula.formula().accept(this));
	return cache(quantFormula, ret);
}
 
Example #13
Source File: FormulaFlattener.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 * @see kodkod.ast.visitor.AbstractVoidVisitor#visit(kodkod.ast.QuantifiedFormula)
 */
public final void visit(QuantifiedFormula qf) {
	if (visited(qf)) return;
	
	if (breakupQuantifiers) {
		
		final Quantifier quant = qf.quantifier();
		
		if ((!negated && quant==ALL) || (negated && quant==SOME)) { // may break down further
			final Map<Formula, Node> oldConjuncts = conjuncts;
			conjuncts = new LinkedHashMap<Formula, Node>();
			qf.formula().accept(this);
			if (conjuncts.size()>1) { // was broken down further
				final Decls decls = qf.decls();
				for(Map.Entry<Formula, Node> entry : conjuncts.entrySet()) { 
					oldConjuncts.put(entry.getKey().forAll(decls), entry.getValue());
				}
				conjuncts = oldConjuncts;
				return;
			} else { // wasn't broken down further
				conjuncts = oldConjuncts;
			}
		} // won't break down further
	} 

	addConjunct(qf);
	
}
 
Example #14
Source File: PrettyPrinter.java    From kodkod with MIT License 5 votes vote down vote up
/** @return true if the given formula needs to be parenthesized if a 
 * child of a binary formula with the given operator */
private boolean parenthesize(FormulaOperator op, Formula child) { 
	return child instanceof QuantifiedFormula || 
		   //child instanceof NaryFormula ||
	       (child instanceof BinaryFormula && 
	        (op==FormulaOperator.IMPLIES || 
	         ((BinaryFormula)child).op()!=op));
}
 
Example #15
Source File: PrettyPrinter.java    From kodkod with MIT License 5 votes vote down vote up
/** @ensures this.tokens' = concat[ this.tokens,  node.quantifier,
 *   tokenize[node.decls], "|", tokenize[ node.formula ] ]*/
public void visit(QuantifiedFormula node) {
	keyword(node.quantifier());
	node.decls().accept(this);
	infix("|");
	indent++;
	newline();
	node.formula().accept(this);
	indent--;
}
 
Example #16
Source File: AnnotatedNode.java    From kodkod with MIT License 5 votes vote down vote up
/**
 * Returns a Detector that will return TRUE when applied to a descendant
 * of this.node iff the descendant contains a quantified formula.
 * @return a Detector that will return TRUE when applied to a descendant
 * of this.node iff the descendant contains a quantified formula.
 */
public final AbstractDetector quantifiedFormulaDetector() {
	return new AbstractDetector(sharedNodes) {
		public Boolean visit(QuantifiedFormula quantFormula) {
			return cache(quantFormula, true);
		}
	};
}
 
Example #17
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @return true if the given formula needs to be parenthesized if a 
 * child of a binary formula with the given operator */
private boolean parenthesize(FormulaOperator op, Formula child) { 
	return child instanceof QuantifiedFormula || 
	       (child instanceof BinaryFormula && 
	        (op==FormulaOperator.IMPLIES || 
	       ((BinaryFormula)child).op()!=op)) || 
	        ((child instanceof NaryFormula) && ((NaryFormula)child).op()!=op);
}
 
Example #18
Source File: PrettyPrinter.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
/** @effects this.tokens' = concat[ this.tokens,  node.quantifier,
 *   tokenize[node.decls], "|", tokenize[ node.formula ] ]*/
public void visit(QuantifiedFormula node) {
	if (displayed(node)) return;
	keyword(node.quantifier());
	node.decls().accept(this);
	infix("|");
	indent++;
	if (top) newline();
	node.formula().accept(this);
	indent--;
}
 
Example #19
Source File: FormulaFlattener.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see kodkod.ast.visitor.AbstractVoidVisitor#visit(kodkod.ast.QuantifiedFormula)
 */
@Override
public final void visit(QuantifiedFormula qf) {
    if (visited(qf))
        return;

    if (breakupQuantifiers) {

        final Quantifier quant = qf.quantifier();

        if ((!negated && quant == ALL) || (negated && quant == SOME)) { // may
                                                                       // break
                                                                       // down
                                                                       // further
            final Map<Formula,Node> oldConjuncts = conjuncts;
            conjuncts = new LinkedHashMap<Formula,Node>();
            qf.formula().accept(this);
            if (conjuncts.size() > 1) { // was broken down further
                final Decls decls = qf.decls();
                for (Map.Entry<Formula,Node> entry : conjuncts.entrySet()) {
                    oldConjuncts.put(entry.getKey().forAll(decls), entry.getValue());
                }
                conjuncts = oldConjuncts;
                return;
            } else { // wasn't broken down further
                conjuncts = oldConjuncts;
            }
        } // won't break down further
    }

    addConjunct(qf);

}
 
Example #20
Source File: AbstractCollector.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(quantFormula) and returns the cached value, if any. If no cached
 * value exists, visits each child, caches the union of the children's return
 * values and returns it.
 *
 * @return let x = lookup(quantFormula) | x != null => x, cache(quantFormula,
 *         quantFormula.declarations.accept(this) +
 *         quantFormula.formula.accept(this))
 */
@Override
public Set<T> visit(QuantifiedFormula quantFormula) {
    Set<T> ret = lookup(quantFormula);
    if (ret != null)
        return ret;
    ret = newSet();
    ret.addAll(quantFormula.decls().accept(this));
    ret.addAll(quantFormula.formula().accept(this));
    return cache(quantFormula, ret);
}
 
Example #21
Source File: AbstractVoidVisitor.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Visits the declarations and the formula if this.visited(quantFormula) returns
 * false. Otherwise does nothing.
 *
 * @ensures quantFormula.declarations.accept(this) &&
 *          quantFormula.formula.accept(this)
 */
@Override
public void visit(QuantifiedFormula quantFormula) {
    if (visited(quantFormula))
        return;
    quantFormula.decls().accept(this);
    quantFormula.formula().accept(this);
}
 
Example #22
Source File: AbstractReplacer.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Calls lookup(quantFormula) 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 { q: QuantifiedFormula | q.declarations =
 *         quantFormula.declarations.accept(delegate) && q.formula =
 *         quantFormula.formula.accept(delegate) }
 */
@Override
public Formula visit(QuantifiedFormula quantFormula) {
    Formula ret = lookup(quantFormula);
    if (ret != null)
        return ret;

    final Decls decls = quantFormula.decls().accept(delegate);
    final Formula domain = quantFormula.domain().accept(delegate);
    final Formula body = quantFormula.body().accept(delegate);
    ret = (decls == quantFormula.decls() && domain == quantFormula.domain() && body == quantFormula.body()) ? quantFormula : body.quantify(quantFormula.quantifier(), decls, domain);
    return cache(quantFormula, ret);
}
 
Example #23
Source File: HOL2ProcTranslator.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
@Override
public Formula visit(QuantifiedFormula qf) {
    for (Decl decl : qf.decls()) {
        if (decl.multiplicity() != Multiplicity.ONE) {
            if (!isSkolemizableUpToNow())
                throw new HigherOrderDeclException(decl);
            assert qf.decls().size() == 1 : "not implemented for quantifiers with multiple decls";
            QuantifiedFormula revQuant = (QuantifiedFormula) qf.formula().quantify(qf.quantifier().opposite, decl);
            conversions.add(new Conversion(qf, revQuant));
            return revQuant;
        }
    }
    return super.visit(qf);
}
 
Example #24
Source File: HOL2ProcTranslator.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
@Override
protected void start(Node n) {
    stack.push(n);
    // ***NOTE*** assumes the formula is already in NNF !!!
    boolean skolemizableSoFar = skolemizable.empty() ? true : skolemizable.lastElement();
    if (!skolemizableSoFar) {
        skolemizable.push(false);
    } else {
        if ((n instanceof BinaryFormula && ((BinaryFormula) n).op() == FormulaOperator.AND) || (n instanceof NaryFormula && ((NaryFormula) n).op() == FormulaOperator.AND) || (n instanceof QuantifiedFormula && ((QuantifiedFormula) n).quantifier() == Quantifier.SOME))
            skolemizable.push(true);
        else
            skolemizable.push(false);
    }
}
 
Example #25
Source File: HOLTranslator.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
private void assertNNF(NotFormula not) {
    Formula f = not.formula();
    if (f instanceof BinaryFormula || f instanceof NaryFormula || f instanceof QuantifiedFormula)
        throw new IllegalStateException("Expected formula to be in NNF; got: " + not);
}
 
Example #26
Source File: HOLTranslator.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
Conversion(QuantifiedFormula origQF, QuantifiedFormula newQF) {
    this.origQF = origQF;
    this.newQF = newQF;
}
 
Example #27
Source File: PrettyPrinter.java    From kodkod with MIT License 4 votes vote down vote up
public void visit(QuantifiedFormula quantFormula) { 
	visit(quantFormula, quantFormula.quantifier(), quantFormula.decls(), quantFormula.formula());
}
 
Example #28
Source File: HOLTranslator.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
private void assertNotSkolemizable(QuantifiedFormula qf) {
    if (qf.quantifier() == Quantifier.SOME) {
        String msg = "It appears that the existential quantifier in '" + qf + "' is skolemizable, but it hasn't been";
        throw new IllegalStateException(msg);
    }
}
 
Example #29
Source File: TrivialProof.java    From org.alloytools.alloy with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(QuantifiedFormula quantFormula) {
    if (visited(quantFormula))
        return;
    relevant.add(quantFormula);
}
 
Example #30
Source File: TrivialProof.java    From kodkod with MIT License 4 votes vote down vote up
public void visit(QuantifiedFormula quantFormula) { 
	if (visited(quantFormula)) return;
	relevant.add(quantFormula);
}