Search in sources :

Example 66 with Formula

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);
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) MultiplicityFormula(kodkod.ast.MultiplicityFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) NotFormula(kodkod.ast.NotFormula) ComparisonFormula(kodkod.ast.ComparisonFormula) NaryFormula(kodkod.ast.NaryFormula) Formula(kodkod.ast.Formula) IntComparisonFormula(kodkod.ast.IntComparisonFormula) FormulaOperator(kodkod.ast.operator.FormulaOperator)

Example 67 with Formula

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);
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) MultiplicityFormula(kodkod.ast.MultiplicityFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) NotFormula(kodkod.ast.NotFormula) ComparisonFormula(kodkod.ast.ComparisonFormula) NaryFormula(kodkod.ast.NaryFormula) Formula(kodkod.ast.Formula) IntComparisonFormula(kodkod.ast.IntComparisonFormula)

Example 68 with Formula

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);
    }
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) MultiplicityFormula(kodkod.ast.MultiplicityFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) NotFormula(kodkod.ast.NotFormula) ComparisonFormula(kodkod.ast.ComparisonFormula) NaryFormula(kodkod.ast.NaryFormula) Formula(kodkod.ast.Formula) IntComparisonFormula(kodkod.ast.IntComparisonFormula) AnnotatedNode(kodkod.util.nodes.AnnotatedNode) Node(kodkod.ast.Node) IdentityHashMap(java.util.IdentityHashMap)

Example 69 with Formula

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);
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) MultiplicityFormula(kodkod.ast.MultiplicityFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) ConstantFormula(kodkod.ast.ConstantFormula) NotFormula(kodkod.ast.NotFormula) ComparisonFormula(kodkod.ast.ComparisonFormula) NaryFormula(kodkod.ast.NaryFormula) Formula(kodkod.ast.Formula) FixFormula(kodkod.ast.FixFormula) IntComparisonFormula(kodkod.ast.IntComparisonFormula) ProjectExpression(kodkod.ast.ProjectExpression) BinaryIntExpression(kodkod.ast.BinaryIntExpression) SumExpression(kodkod.ast.SumExpression) IfIntExpression(kodkod.ast.IfIntExpression) BinaryExpression(kodkod.ast.BinaryExpression) ConstantExpression(kodkod.ast.ConstantExpression) UnaryIntExpression(kodkod.ast.UnaryIntExpression) NaryIntExpression(kodkod.ast.NaryIntExpression) IntExpression(kodkod.ast.IntExpression) Expression(kodkod.ast.Expression) UnaryExpression(kodkod.ast.UnaryExpression) NaryExpression(kodkod.ast.NaryExpression) IfExpression(kodkod.ast.IfExpression)

Example 70 with Formula

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);
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) MultiplicityFormula(kodkod.ast.MultiplicityFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) ConstantFormula(kodkod.ast.ConstantFormula) NotFormula(kodkod.ast.NotFormula) ComparisonFormula(kodkod.ast.ComparisonFormula) NaryFormula(kodkod.ast.NaryFormula) Formula(kodkod.ast.Formula) FixFormula(kodkod.ast.FixFormula) IntComparisonFormula(kodkod.ast.IntComparisonFormula) Decls(kodkod.ast.Decls) ProjectExpression(kodkod.ast.ProjectExpression) BinaryIntExpression(kodkod.ast.BinaryIntExpression) SumExpression(kodkod.ast.SumExpression) IfIntExpression(kodkod.ast.IfIntExpression) BinaryExpression(kodkod.ast.BinaryExpression) ConstantExpression(kodkod.ast.ConstantExpression) UnaryIntExpression(kodkod.ast.UnaryIntExpression) NaryIntExpression(kodkod.ast.NaryIntExpression) IntExpression(kodkod.ast.IntExpression) Expression(kodkod.ast.Expression) UnaryExpression(kodkod.ast.UnaryExpression) NaryExpression(kodkod.ast.NaryExpression) IfExpression(kodkod.ast.IfExpression)

Aggregations

Formula (kodkod.ast.Formula)346 Variable (kodkod.ast.Variable)151 Solution (kodkod.engine.Solution)101 Expression (kodkod.ast.Expression)95 Bounds (kodkod.instance.Bounds)83 QuantifiedFormula (kodkod.ast.QuantifiedFormula)72 Solver (kodkod.engine.Solver)67 BinaryFormula (kodkod.ast.BinaryFormula)50 NaryFormula (kodkod.ast.NaryFormula)49 Relation (kodkod.ast.Relation)45 NotFormula (kodkod.ast.NotFormula)43 ComparisonFormula (kodkod.ast.ComparisonFormula)40 IntExpression (kodkod.ast.IntExpression)40 IntComparisonFormula (kodkod.ast.IntComparisonFormula)39 MultiplicityFormula (kodkod.ast.MultiplicityFormula)39 Universe (kodkod.instance.Universe)37 ArrayList (java.util.ArrayList)35 TupleFactory (kodkod.instance.TupleFactory)35 ConstantFormula (kodkod.ast.ConstantFormula)29 TupleSet (kodkod.instance.TupleSet)29