use of kodkod.ast.Formula in project org.alloytools.alloy by AlloyTools.
the class Skolemizer method visit.
/**
* 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);
}
use of kodkod.ast.Formula in project org.alloytools.alloy by AlloyTools.
the class Skolemizer method visit.
/**
* Calls super.visit(mf) after disabling skolemization and returns the result.
*
* @return super.visit(mf)
*/
@Override
public final Formula visit(MultiplicityFormula mf) {
final int oldDepth = skolemDepth;
// cannot skolemize inside a multiplicity formula
skolemDepth = -1;
final Formula ret = super.visit(mf);
skolemDepth = oldDepth;
return source(ret, mf);
}
use of kodkod.ast.Formula in project org.alloytools.alloy by AlloyTools.
the class Skolemizer method skolemize.
/**
* Skolemizes the given annotated formula using the given bounds and options. If
* Options.logTranslation is set and the formula is skolemizable, the resulting
* annotated formula will contain transitive source information for each of its
* subformulas. Specifically, let f be the returned annotated formula, t be a
* descendant of f.node, and s a descendant of annotated.node from which t was
* derived. Then, f.source[t] = annotated.source[s]. If options.logTranslation
* is false, no source information will be recorded (i.e. f.source[t] = t for
* all descendants t of f).
*
* @ensures upper bound mappings for skolem constants, if any, are added to the
* bounds
* @return the skolemized version of the given formula
* @throws NullPointerException any of the arguments are null
* @throws IllegalArgumentException some Relation & annotated.node.^children -
* bounds.relations
* @throws UnsupportedOperationException bounds is unmodifiable
*/
public static AnnotatedNode<Formula> skolemize(final AnnotatedNode<Formula> annotated, Bounds bounds, Options options) {
if (options.logTranslation() > 0) {
final Map<Node, Node> source = new IdentityHashMap<Node, Node>();
final Skolemizer r = new Skolemizer(annotated, bounds, options) {
@Override
protected Formula source(Formula f, Node n) {
// System.out.println("logging " + f + " <-- " + n);
final Node nsource = annotated.sourceOf(n);
if (f != nsource)
source.put(f, nsource);
return f;
}
};
final Formula f = annotated.node().accept(r);
return f == annotated.node() ? annotated : annotate(f, source);
} else {
final Skolemizer r = new Skolemizer(annotated, bounds, options) {
};
final Formula f = annotated.node().accept(r);
return f == annotated.node() ? annotated : annotate(f);
}
}
use of kodkod.ast.Formula in project org.alloytools.alloy by AlloyTools.
the class AbstractReplacer method visit.
/**
* Calls lookup(compFormula) 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 { c: ComparisonFormula | c.left = compFormula.left.accept(delegate)
* && c.right = compFormula.right.accept(delegate) && c.op =
* compFormula.op }
*/
@Override
public Formula visit(ComparisonFormula compFormula) {
Formula ret = lookup(compFormula);
if (ret != null)
return ret;
final Expression left = compFormula.left().accept(delegate);
final Expression right = compFormula.right().accept(delegate);
ret = (left == compFormula.left() && right == compFormula.right()) ? compFormula : left.compare(compFormula.op(), right);
return cache(compFormula, ret);
}
use of kodkod.ast.Formula in project org.alloytools.alloy by AlloyTools.
the class AbstractReplacer method visit.
/**
* Calls lookup(comprehension) and returns the cached value, if any. If a
* replacement has not been cached, visits the expression's children. If nothing
* changes, the argument is cached and returned, otherwise a replacement
* expression is cached and returned.
*
* @return { c: Comprehension | c.declarations =
* comprehension.declarations.accept(delegate) && c.formula =
* comprehension.formula.accept(delegate) }
*/
@Override
public Expression visit(Comprehension comprehension) {
Expression ret = lookup(comprehension);
if (ret != null)
return ret;
final Decls decls = comprehension.decls().accept(delegate);
final Formula formula = comprehension.formula().accept(delegate);
ret = (decls == comprehension.decls() && formula == comprehension.formula()) ? comprehension : formula.comprehension(decls);
return cache(comprehension, ret);
}
Aggregations