Search in sources :

Example 11 with Formula

use of kodkod.ast.Formula in project org.alloytools.alloy by AlloyTools.

the class Translator method inlinePredicates.

/**
 * Returns an annotated formula f such that f.node is equivalent to
 * annotated.node with its <tt>simplified</tt> predicates replaced with their
 * corresponding Formulas and the remaining predicates replaced with equivalent
 * constraints. The annotated formula f will contain transitive source
 * information for each of the subformulas of f.node. Specifically, let t be a
 * subformula of f.node, and s be a descdendent of annotated.node from which t
 * was derived. Then, f.source[t] = annotated.source[s].
 * </p>
 *
 * @requires simplified.keySet() in
 *           annotated.predicates()[RelationPredicate.NAME]
 * @requires no disj p, p': simplified.keySet() | simplified.get(p) =
 *           simplifed.get(p') // this must hold in order to maintain the
 *           invariant that each subformula of the returned formula has exactly
 *           one source
 * @requires for each p in simplified.keySet(), the formulas "p and
 *           [[this.bounds]]" and "simplified.get(p) and [[this.bounds]]" are
 *           equisatisfiable
 * @return an annotated formula f such that f.node is equivalent to
 *         annotated.node with its <tt>simplified</tt> predicates replaced with
 *         their corresponding Formulas and the remaining predicates replaced
 *         with equivalent constraints.
 */
private AnnotatedNode<Formula> inlinePredicates(final AnnotatedNode<Formula> annotated, final Map<RelationPredicate, Formula> simplified) {
    final Map<Node, Node> sources = new IdentityHashMap<Node, Node>();
    final AbstractReplacer inliner = new AbstractReplacer(annotated.sharedNodes()) {

        private RelationPredicate source = null;

        @Override
        protected <N extends Node> N cache(N node, N replacement) {
            if (replacement instanceof Formula) {
                if (source == null) {
                    final Node nsource = annotated.sourceOf(node);
                    if (replacement != nsource)
                        sources.put(replacement, nsource);
                } else {
                    sources.put(replacement, source);
                }
            }
            return super.cache(node, replacement);
        }

        @Override
        public Formula visit(RelationPredicate pred) {
            Formula ret = lookup(pred);
            if (ret != null)
                return ret;
            source = pred;
            if (simplified.containsKey(pred)) {
                ret = simplified.get(pred).accept(this);
            } else {
                ret = pred.toConstraints().accept(this);
            }
            source = null;
            return cache(pred, ret);
        }
    };
    return annotate(annotated.node().accept(inliner), sources);
}
Also used : BooleanFormula(kodkod.engine.bool.BooleanFormula) Formula(kodkod.ast.Formula) AnnotatedNode(kodkod.util.nodes.AnnotatedNode) Node(kodkod.ast.Node) IdentityHashMap(java.util.IdentityHashMap) RelationPredicate(kodkod.ast.RelationPredicate) AbstractReplacer(kodkod.ast.visitor.AbstractReplacer)

Example 12 with Formula

use of kodkod.ast.Formula in project org.alloytools.alloy by AlloyTools.

the class HOL2ProcTranslator method translate.

public static HOLTranslation translate(AnnotatedNode<Formula> annotated, Bounds bounds, Options options) {
    HOL2ProcTranslator tr = new HOL2ProcTranslator(annotated.sharedNodes());
    Formula converted = annotated.node().accept(tr.withHistory());
    if (tr.conversions.size() == 0) {
        return new HOLTranslationOld.FOL(annotated, bounds, options);
    } else {
        return new HOLTranslationOld.Some4All(annotated, converted, tr.conversions, bounds, options);
    }
}
Also used : BinaryFormula(kodkod.ast.BinaryFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) NaryFormula(kodkod.ast.NaryFormula)

Example 13 with Formula

use of kodkod.ast.Formula in project org.alloytools.alloy by AlloyTools.

the class Handshake method main.

/**
 * Usage: java examples.Handshake [# persons, must be >= 2]
 */
public static void main(String[] args) {
    if (args.length < 1)
        usage();
    final Handshake model = new Handshake();
    final Solver solver = new Solver();
    try {
        final int persons = Integer.parseInt(args[0]);
        if (persons < 2)
            usage();
        solver.options().setBitwidth(6);
        // solver.options().setSolver(SATFactory.ZChaff);
        solver.options().setSolver(SATFactory.MiniSat);
        solver.options().setSymmetryBreaking(0);
        solver.options().setBitwidth(32 - Integer.numberOfLeadingZeros(persons));
        solver.options().setReporter(new ConsoleReporter());
        final Bounds b = model.bounds(persons);
        // .and(model.Person.count().eq(IntConstant.constant(persons)));
        final Formula f = model.runPuzzle();
        Solution sol = solver.solve(f, b);
        System.out.println(sol);
    } catch (NumberFormatException nfe) {
        usage();
    }
}
Also used : Formula(kodkod.ast.Formula) Solver(kodkod.engine.Solver) ConsoleReporter(kodkod.engine.config.ConsoleReporter) Bounds(kodkod.instance.Bounds) Solution(kodkod.engine.Solution)

Example 14 with Formula

use of kodkod.ast.Formula in project org.alloytools.alloy by AlloyTools.

the class Handshake method declarations.

/**
 * Returns the declarations
 *
 * @return
 *
 *         <pre>
 * sig Person {spouse: Person, shaken: set Person}
 * one sig Jocelyn, Hilary extends Person {}
 *         </pre>
 */
public Formula declarations() {
    final Formula f0 = spouse.function(Person, Person);
    final Formula f1 = shaken.in(Person.product(Person));
    final Formula f2 = Hilary.one().and(Jocelyn.one());
    return f0.and(f1).and(f2);
}
Also used : Formula(kodkod.ast.Formula)

Example 15 with Formula

use of kodkod.ast.Formula in project org.alloytools.alloy by AlloyTools.

the class Lists method main.

/**
 * Usage: java examples.Lists [scope]
 */
public static void main(String[] args) {
    if (args.length < 1)
        usage();
    try {
        final int n = Integer.parseInt(args[0]);
        final Lists model = new Lists();
        final Bounds b = model.bounds(n);
        final Solver solver = new Solver();
        solver.options().setSolver(SATFactory.MiniSat);
        // solver.options().setFlatten(false);
        // solver.options().setSkolemize(false);
        Formula f = model.runShow();
        System.out.println("running show");
        Solution s = solver.solve(f, b);
        System.out.println(s);
        f = model.checkEmpties();
        System.out.println("checking empties");
        s = solver.solve(f, b);
        System.out.println(s);
        f = model.checkReflexive();
        System.out.println("checking reflexive");
        s = solver.solve(f, b);
        System.out.println(s);
        f = model.checkSymmetric();
        System.out.println("checking symmetric");
        s = solver.solve(f, b);
        System.out.println(s);
    } catch (NumberFormatException nfe) {
        usage();
    } catch (HigherOrderDeclException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnboundLeafException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : Formula(kodkod.ast.Formula) Solver(kodkod.engine.Solver) UnboundLeafException(kodkod.engine.fol2sat.UnboundLeafException) Bounds(kodkod.instance.Bounds) HigherOrderDeclException(kodkod.engine.fol2sat.HigherOrderDeclException) Solution(kodkod.engine.Solution)

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