kodkod.ast.NaryFormula Java Examples
The following examples show how to use
kodkod.ast.NaryFormula.
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: FormulaFlattener.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** * Visits the formula's children with appropriate settings for the negated flag * if bf has not been visited before. * * @see kodkod.ast.visitor.AbstractVoidVisitor#visit(kodkod.ast.NaryFormula) */ @Override public final void visit(NaryFormula nf) { if (visited(nf)) return; final FormulaOperator op = nf.op(); if ((negated && op == AND) || (!negated && op == OR)) { // can't break // down further // in these // cases addConjunct(nf); } else { // will break down further for (Formula f : nf) { f.accept(this); } } }
Example #2
Source File: TranslateKodkodToJava.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** {@inheritDoc} */ @Override public void visit(NaryFormula x) { String newname = makename(x); if (newname == null) return; String[] list = new String[x.size()]; for (int i = 0; i < list.length; i++) list[i] = make(x.child(i)); file.printf("Formula %s=Formula.compose(FormulaOperator.", newname); switch (x.op()) { case AND : file.print("AND"); break; case OR : file.print("OR"); break; default : throw new RuntimeException("Unknown kodkod operator \"" + x.op() + "\" encountered"); } for (int i = 0; i < list.length; i++) file.printf(", %s", list[i]); file.printf(");%n"); }
Example #3
Source File: AbstractReplacer.java From kodkod with MIT License | 6 votes |
/** * 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 #4
Source File: Nodes.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
public static List<Formula> allConjuncts(Formula formula, List<Formula> acc) { List<Formula> ans = acc != null ? acc : new ArrayList<Formula>(); if (formula instanceof BinaryFormula) { final BinaryFormula bin = (BinaryFormula) formula; if (bin.op() == FormulaOperator.AND) { allConjuncts(bin.left(), ans); allConjuncts(bin.right(), ans); return ans; } } if (formula instanceof NaryFormula) { final NaryFormula nf = (NaryFormula) formula; if (nf.op() == FormulaOperator.AND) { for (Formula child : nf) { allConjuncts(child, ans); } return ans; } } ans.add(formula); return ans; }
Example #5
Source File: PrettyPrinter.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** * @ensures appends the tokenization of the given node to this.tokens */ @Override public void visit(NaryFormula node) { final FormulaOperator op = node.op(); boolean parens = parenthesize(op, node.child(0)); if (parens) indent++; visitChild(node.child(0), parens); if (parens) indent--; for (int i = 1, size = node.size(); i < size; i++) { infix(op); newline(); parens = parenthesize(op, node.child(i)); if (parens) indent++; visitChild(node.child(i), parens); if (parens) indent--; } }
Example #6
Source File: FOL2BoolTranslator.java From kodkod with MIT License | 6 votes |
/** * Calls lookup(formula) 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(formula) | some t => t, * cache(formula, formula.op(formula.left.accept(this), formula.right.accept(this)) */ public final BooleanValue visit(NaryFormula formula) { final BooleanValue ret = lookup(formula); if (ret!=null) return ret; final FormulaOperator op = formula.op(); final Operator.Nary boolOp; switch(op) { case AND : boolOp = Operator.AND; break; case OR : boolOp = Operator.OR; break; default : throw new IllegalArgumentException("Unknown nary operator: " + op); } final BooleanAccumulator acc = BooleanAccumulator.treeGate(boolOp); final BooleanValue shortCircuit = boolOp.shortCircuit(); for(Formula child : formula) { if (acc.add(child.accept(this))==shortCircuit) break; } return cache(formula, interpreter.factory().accumulate(acc)); }
Example #7
Source File: PrenexNFConverter.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
@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 #8
Source File: PrettyPrinter.java From kodkod with MIT License | 6 votes |
/** @ensures appends the tokenization of the given node to this.tokens */ public void visit(NaryFormula node) { final FormulaOperator op = node.op(); boolean parens = parenthesize(op, node.child(0)); if (parens) indent++; visitChild(node.child(0), parens); if (parens) indent--; for(int i = 1, size = node.size(); i < size; i++) { infix(op); newline(); parens = parenthesize(op, node.child(i)); if (parens) indent++; visitChild(node.child(i), parens); if (parens) indent--; } }
Example #9
Source File: PrettyPrinter.java From quetzal with Eclipse Public License 2.0 | 6 votes |
/** @effects appends the tokenization of the given node to this.tokens */ public void visit(NaryFormula node) { if (displayed(node)) return; final boolean oldTop = top; final FormulaOperator op = node.op(); if ((negated && op==AND) || (!negated && op==OR)) { // not top in these cases top = false; } boolean parens = parenthesize(op, node.child(0)); negated = negated ^ (op==OR); if (parens) indent++; visitChild(node.child(0), parens); if (parens) indent--; for(int i = 1, size = node.size(); i < size; i++) { infix(op); if (top) newline(); parens = parenthesize(op, node.child(i)); if (parens) indent++; visitChild(node.child(i), parens); if (parens) indent--; } negated = negated ^ (op==OR); top = oldTop; }
Example #10
Source File: AbstractReplacer.java From org.alloytools.alloy with Apache License 2.0 | 6 votes |
/** * 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 #11
Source File: Nodes.java From kodkod with MIT License | 5 votes |
/** * Returns the roots of the given formula. * In other words, breaks up the given formula into its conjunctive * components, {f0, ..., fk}, * such that, for all 0<=i<=k, f<sub>i</sub> is not a conjunction and * [[f0 && ... && fk]] <=> [[formula]]. * @return subformulas, {f0, ..., fk}, of the given formula such that, for all 0<=i<=k, * f<sub>i</sub> is not a conjunction and [[f0 && ... && fk]] <=> [[formula]]. */ public static Set<Formula> roots(Formula formula) { final List<Formula> formulas = new LinkedList<Formula>(); formulas.add(formula); final ListIterator<Formula> itr = formulas.listIterator(); while(itr.hasNext()) { final Formula f = itr.next(); if (f instanceof BinaryFormula) { final BinaryFormula bin = (BinaryFormula) f; if (bin.op()==FormulaOperator.AND) { itr.remove(); itr.add(bin.left()); itr.add(bin.right()); itr.previous(); itr.previous(); } } else if (f instanceof NaryFormula) { final NaryFormula nf = (NaryFormula) f; if (nf.op()==FormulaOperator.AND) { itr.remove(); for(Formula child : nf) { itr.add(child); } for(int i = nf.size(); i>0; i--) { itr.previous(); } } } } return new LinkedHashSet<Formula>(formulas); }
Example #12
Source File: Nodes.java From kodkod with MIT License | 5 votes |
/** * Returns an unmodifiable set consisting of the children of the given formula, if the formula is a binary or an nary conjunction. Otherwise * returns a singleton set containing the formula itself. * @return an unmodifiable set consisting of children of the given formula, if the formula is a binary or an nary conjunction. Otherwise * returns a singleton set containing the formula itself. */ public static Set<Formula> conjuncts(Formula formula) { if (formula instanceof BinaryFormula) { final BinaryFormula bin = (BinaryFormula) formula; if (bin.op()==FormulaOperator.AND) { final Formula left = bin.left(), right = bin.right(); if (left==right) return Collections.singleton(left); else return new AbstractSet<Formula>() { @Override public boolean contains(Object o) { return left==o || right==o; } @Override public Iterator<Formula> iterator() { return Containers.iterate(left, right); } @Override public int size() { return 2; } }; } } else if (formula instanceof NaryFormula) { final NaryFormula nf = (NaryFormula) formula; if (nf.op()==FormulaOperator.AND) { final LinkedHashSet<Formula> children = new LinkedHashSet<Formula>(1+(nf.size()*4)/3); for(Formula child : nf) { children.add(child); } return Collections.unmodifiableSet(children); } } return Collections.singleton(formula); }
Example #13
Source File: AnnotatedNode.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
/** * Visits the children of the given formula if it has not been visited already * with the given value of the negated flag and if formula.op==OR && negated or * formula.op==AND && !negated. Otherwise does nothing. * * @see kodkod.ast.visitor.AbstractVoidVisitor#visit(kodkod.ast.NaryFormula) */ @Override public void visit(NaryFormula formula) { if (visited(formula)) return; final FormulaOperator op = formula.op(); if ((!negated && op == AND) || (negated && op == OR)) { // op==AND // || op==OR for (Formula child : formula) { child.accept(this); } } }
Example #14
Source File: FormulaFlattener.java From kodkod with MIT License | 5 votes |
/** * Visits the formula's children with appropriate settings * for the negated flag if bf has not been visited before. * @see kodkod.ast.visitor.AbstractVoidVisitor#visit(kodkod.ast.NaryFormula) */ public final void visit(NaryFormula nf) { if (visited(nf)) return; final FormulaOperator op = nf.op(); if ((negated && op==AND) || (!negated && op==OR)) { // can't break down further in these cases addConjunct(nf); } else { // will break down further for(Formula f : nf) { f.accept(this); } } }
Example #15
Source File: AnnotatedNode.java From kodkod with MIT License | 5 votes |
/** * Visits the children of the given formula if it has not been visited already with * the given value of the negated flag and if formula.op==OR && negated or * formula.op==AND && !negated. Otherwise does nothing. * @see kodkod.ast.visitor.AbstractVoidVisitor#visit(kodkod.ast.NaryFormula) */ public void visit(NaryFormula formula) { if (visited(formula)) return; final FormulaOperator op = formula.op(); if ((!negated && op==AND) || (negated && op==OR)) { // op==AND || op==OR for(Formula child : formula) { child.accept(this); } } }
Example #16
Source File: Skolemizer.java From kodkod with MIT License | 5 votes |
/** * 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 #17
Source File: PrettyPrinter.java From quetzal with Eclipse Public License 2.0 | 5 votes |
/** @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: AbstractDetector.java From kodkod with MIT License | 5 votes |
/** * Calls lookup(formula) and returns the cached value, if any. * If no cached value exists, visits each child, caches the * disjunction of the children's return values and returns it. * @return let x = lookup(formula) | * x != null => x, * cache(formula, formula.child(0).accept(this) || ... || formula.child(formula.size()-1).accept(this)) */ public Boolean visit(NaryFormula formula) { final Boolean ret = lookup(formula); if (ret!=null) return ret; for(Formula child : formula) { if (child.accept(this)) return cache(formula, true); } return cache(formula, false); }
Example #19
Source File: AbstractCollector.java From kodkod with MIT License | 5 votes |
/** * Calls lookup(formula) 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(formula) | * x != null => x, * cache(formula, formula.child(0).accept(this) + .. + formula.child(formula.size()-1).accept(this)) */ public Set<T> visit(NaryFormula formula) { Set<T> ret = lookup(formula); if (ret!=null) return ret; ret = newSet(); for(Formula child : formula) { ret.addAll(child.accept(this)); } return cache(formula, ret); }
Example #20
Source File: AbstractVoidVisitor.java From kodkod with MIT License | 5 votes |
/** * Visits the children if this.visited(formula) returns false. Otherwise does nothing. * @ensures all i: [0..#formula.children) | formula.child(i).accept(this) */ public void visit(NaryFormula formula) { if (visited(formula)) return; for(Formula child : formula) { child.accept(this); } }
Example #21
Source File: Simplifier.java From quetzal with Eclipse Public License 2.0 | 5 votes |
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); } }
Example #22
Source File: AbstractVoidVisitor.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
/** * Visits the children if this.visited(formula) returns false. Otherwise does * nothing. * * @ensures all i: [0..#formula.children) | formula.child(i).accept(this) */ @Override public void visit(NaryFormula formula) { if (visited(formula)) return; for (Formula child : formula) { child.accept(this); } }
Example #23
Source File: Nodes.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
/** * Returns the roots of the given formula. In other words, breaks up the given * formula into its conjunctive components, {f0, ..., fk}, such that, for all * 0<=i<=k, f<sub>i</sub> is not a conjunction and [[f0 && ... && fk]] <=> * [[formula]]. * * @return subformulas, {f0, ..., fk}, of the given formula such that, for all * 0<=i<=k, f<sub>i</sub> is not a conjuction and [[f0 && ... && fk]] * <=> [[formula]]. */ public static Set<Formula> roots(Formula formula) { final List<Formula> formulas = new LinkedList<Formula>(); formulas.add(formula); final ListIterator<Formula> itr = formulas.listIterator(); while (itr.hasNext()) { final Formula f = itr.next(); if (f instanceof BinaryFormula) { final BinaryFormula bin = (BinaryFormula) f; if (bin.op() == FormulaOperator.AND) { itr.remove(); itr.add(bin.left()); itr.add(bin.right()); itr.previous(); itr.previous(); } } else if (f instanceof NaryFormula) { final NaryFormula nf = (NaryFormula) f; if (nf.op() == FormulaOperator.AND) { itr.remove(); for (Formula child : nf) { itr.add(child); } for (int i = nf.size(); i > 0; i--) { itr.previous(); } } } } return new LinkedHashSet<Formula>(formulas); }
Example #24
Source File: HOLTranslator.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
@Override public Proc visit(NaryFormula formula) { if (annotated.isFOLNode(formula)) return new Proc.FOL(bounds, formula); Proc ans = null; for (Formula f : formula) { Proc p = formula.op() == FormulaOperator.OR ? toProc(f) : f.accept(this); ans = ans == null ? p : ans.compose(formula.op(), p); } return ans; }
Example #25
Source File: HOL2ProcTranslator.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
@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 #26
Source File: FOL2BoolTranslator.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
/** * Calls lookup(formula) 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(formula) | some t => t, cache(formula, * formula.op(formula.left.accept(this), formula.right.accept(this)) */ @Override public final BooleanValue visit(NaryFormula formula) { final BooleanValue ret = lookup(formula); if (ret != null) return ret; final FormulaOperator op = formula.op(); final Operator.Nary boolOp; switch (op) { case AND : boolOp = Operator.AND; break; case OR : boolOp = Operator.OR; break; default : throw new IllegalArgumentException("Unknown nary operator: " + op); } final BooleanAccumulator acc = BooleanAccumulator.treeGate(boolOp); final BooleanValue shortCircuit = boolOp.shortCircuit(); for (Formula child : formula) { if (acc.add(child.accept(this)) == shortCircuit) break; } return cache(formula, interpreter.factory().accumulate(acc)); }
Example #27
Source File: Skolemizer.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
/** * 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 #28
Source File: AbstractDetector.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
/** * Calls lookup(formula) and returns the cached value, if any. If no cached * value exists, visits each child, caches the disjunction of the children's * return values and returns it. * * @return let x = lookup(formula) | x != null => x, cache(formula, * formula.child(0).accept(this) || ... || * formula.child(formula.size()-1).accept(this)) */ @Override public Boolean visit(NaryFormula formula) { final Boolean ret = lookup(formula); if (ret != null) return ret; for (Formula child : formula) { if (child.accept(this)) return cache(formula, true); } return cache(formula, false); }
Example #29
Source File: AbstractCollector.java From org.alloytools.alloy with Apache License 2.0 | 5 votes |
/** * Calls lookup(formula) 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(formula) | x != null => x, cache(formula, * formula.child(0).accept(this) + .. + * formula.child(formula.size()-1).accept(this)) */ @Override public Set<T> visit(NaryFormula formula) { Set<T> ret = lookup(formula); if (ret != null) return ret; ret = newSet(); for (Formula child : formula) { ret.addAll(child.accept(this)); } return cache(formula, ret); }
Example #30
Source File: AnnotatedNode.java From org.alloytools.alloy with Apache License 2.0 | 4 votes |
@Override public Boolean visit(NaryFormula f) { return checkVisitedThenAccum(f, Boolean.FALSE, f); }