Search in sources :

Example 1 with Decl

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

the class SkolemizationTest method testDeepSkolems.

public final void testDeepSkolems() {
    solver.options().setSkolemDepth(3);
    testDeepSkolems(Multiplicity.ONE);
    testDeepSkolems(Multiplicity.LONE);
    testDeepSkolems(Multiplicity.SOME);
    testDeepSkolems(Multiplicity.SET);
    final Variable va = Variable.unary("va");
    final Variable vb = Variable.unary("vb");
    Decl da1 = va.oneOf(r1a);
    Decl db = vb.oneOf(r1b);
    final Formula f0 = va.in(vb.join(r2b));
    final Formula f1 = f0.forAll(db).not().forAll(da1);
    final Formula f2 = f0.forSome(db).forSome(da1);
    Instance inst = solve(f1.and(f2));
    assertEquals(bounds.relations().size() + 3, inst.relations().size());
}
Also used : Formula(kodkod.ast.Formula) Variable(kodkod.ast.Variable) Instance(kodkod.instance.Instance) Decl(kodkod.ast.Decl)

Example 2 with Decl

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

the class SkolemizationTest method testNoSkolems.

private final void testNoSkolems(Multiplicity mult) {
    final Variable v = Variable.unary("v");
    final Decl d = v.declare(mult, r1a);
    testNoSkolems(d, v.join(r2a).some().forAll(d).not());
    testNoSkolems(d, v.join(r2a).some().forSome(d));
}
Also used : Variable(kodkod.ast.Variable) Decl(kodkod.ast.Decl)

Example 3 with Decl

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

the class HOL2ProcTranslator method visit.

@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);
}
Also used : HigherOrderDeclException(kodkod.engine.fol2sat.HigherOrderDeclException) Decl(kodkod.ast.Decl) QuantifiedFormula(kodkod.ast.QuantifiedFormula)

Example 4 with Decl

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

the class HOLTranslator method noNewHOLSkolems.

private boolean noNewHOLSkolems(Collection<Relation> newSkolems, Collection<Relation> oldSkolems) {
    Set<Relation> diff = new HashSet<Relation>(newSkolems);
    diff.removeAll(oldSkolems);
    for (Relation sk : diff) {
        Decl d = sk.getSkolemVarDecl();
        if (d != null && d.multiplicity() != Multiplicity.ONE)
            return false;
    }
    return true;
}
Also used : Relation(kodkod.ast.Relation) Decl(kodkod.ast.Decl) HashSet(java.util.HashSet)

Example 5 with Decl

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

the class A4Solution method solve.

// ===================================================================================================//
/**
 * Solve for the solution if not solved already; if cmd==null, we will simply
 * use the lowerbound of each relation as its value.
 */
A4Solution solve(final A4Reporter rep, Command cmd, Simplifier simp, boolean tryBookExamples) throws Err, IOException {
    // If already solved, then return this object as is
    if (solved)
        return this;
    // the lower bound of each relation
    if (cmd == null) {
        Instance inst = new Instance(bounds.universe());
        for (int max = max(), i = min(); i <= max; i++) {
            Tuple it = factory.tuple("" + i);
            inst.add(i, factory.range(it, it));
        }
        for (Relation r : bounds.relations()) inst.add(r, bounds.lowerBound(r));
        eval = new Evaluator(inst, solver.options());
        rename(this, null, null, new UniqueNameGenerator());
        solved();
        return this;
    }
    // Otherwise, prepare to do the solve...
    final A4Options opt = originalOptions;
    long time = System.currentTimeMillis();
    rep.debug("Simplifying the bounds...\n");
    if (opt.inferPartialInstance && simp != null && formulas.size() > 0 && !simp.simplify(rep, this, formulas))
        addFormula(Formula.FALSE, Pos.UNKNOWN);
    rep.translate(opt.solver.id(), bitwidth, maxseq, solver.options().skolemDepth(), solver.options().symmetryBreaking());
    Formula fgoal = Formula.and(formulas);
    rep.debug("Generating the solution...\n");
    kEnumerator = null;
    Solution sol = null;
    final Reporter oldReporter = solver.options().reporter();
    final boolean[] solved = new boolean[] { true };
    solver.options().setReporter(new // Set up a
    AbstractReporter() {

        // reporter to
        // catch the
        // type+pos of
        // skolems
        @Override
        public void skolemizing(Decl decl, Relation skolem, List<Decl> predecl) {
            try {
                Type t = kv2typepos(decl.variable()).a;
                if (t == Type.EMPTY)
                    return;
                for (int i = (predecl == null ? -1 : predecl.size() - 1); i >= 0; i--) {
                    Type pp = kv2typepos(predecl.get(i).variable()).a;
                    if (pp == Type.EMPTY)
                        return;
                    t = pp.product(t);
                }
                kr2type(skolem, t);
            }// Exception here is not fatal
             catch (Throwable ex) {
            }
        }

        @Override
        public void solvingCNF(int primaryVars, int vars, int clauses) {
            if (solved[0])
                return;
            else
                // initially solved[0] is true, so we
                solved[0] = true;
            // won't report the # of vars/clauses
            if (rep != null)
                rep.solve(primaryVars, vars, clauses);
        }
    });
    if (!opt.solver.equals(SatSolver.CNF) && !opt.solver.equals(SatSolver.KK) && tryBookExamples) {
        // try
        // book
        // examples
        A4Reporter r = AlloyCore.isDebug() ? rep : null;
        try {
            sol = BookExamples.trial(r, this, fgoal, solver, cmd.check);
        } catch (Throwable ex) {
            sol = null;
        }
    }
    // this allows the reporter to report the # of
    solved[0] = false;
    // vars/clauses
    for (Relation r : bounds.relations()) {
        formulas.add(r.eq(r));
    }
    // Without this, kodkod refuses to grow unmentioned relations
    fgoal = Formula.and(formulas);
    // Now pick the solver and solve it!
    if (opt.solver.equals(SatSolver.KK)) {
        File tmpCNF = File.createTempFile("tmp", ".java", new File(opt.tempDirectory));
        String out = tmpCNF.getAbsolutePath();
        Util.writeAll(out, debugExtractKInput());
        rep.resultCNF(out);
        return null;
    }
    if (opt.solver.equals(SatSolver.CNF)) {
        File tmpCNF = File.createTempFile("tmp", ".cnf", new File(opt.tempDirectory));
        String out = tmpCNF.getAbsolutePath();
        solver.options().setSolver(WriteCNF.factory(out));
        try {
            sol = solver.solve(fgoal, bounds);
        } catch (WriteCNF.WriteCNFCompleted ex) {
            rep.resultCNF(out);
            return null;
        }
        // The formula is trivial (otherwise, it would have thrown an
        // exception)
        // Since the user wants it in CNF format, we manually generate a
        // trivially satisfiable (or unsatisfiable) CNF file.
        Util.writeAll(out, sol.instance() != null ? "p cnf 1 1\n1 0\n" : "p cnf 1 2\n1 0\n-1 0\n");
        rep.resultCNF(out);
        return null;
    }
    if (!solver.options().solver().incremental()) /*
                                                      * || solver.options().solver()==SATFactory. ZChaffMincost
                                                      */
    {
        if (sol == null)
            sol = solver.solve(fgoal, bounds);
    } else {
        kEnumerator = new Peeker<Solution>(solver.solveAll(fgoal, bounds));
        if (sol == null)
            sol = kEnumerator.next();
    }
    if (!solved[0])
        rep.solve(0, 0, 0);
    final Instance inst = sol.instance();
    // To ensure no more output during SolutionEnumeration
    solver.options().setReporter(oldReporter);
    // If unsatisfiable, then retreive the unsat core if desired
    if (inst == null && solver.options().solver() == SATFactory.MiniSatProver) {
        try {
            lCore = new LinkedHashSet<Node>();
            Proof p = sol.proof();
            if (sol.outcome() == UNSATISFIABLE) {
                // only perform the minimization if it was UNSATISFIABLE,
                // rather than TRIVIALLY_UNSATISFIABLE
                int i = p.highLevelCore().size();
                rep.minimizing(cmd, i);
                if (opt.coreMinimization == 0)
                    try {
                        p.minimize(new RCEStrategy(p.log()));
                    } catch (Throwable ex) {
                    }
                if (opt.coreMinimization == 1)
                    try {
                        p.minimize(new HybridStrategy(p.log()));
                    } catch (Throwable ex) {
                    }
                rep.minimized(cmd, i, p.highLevelCore().size());
            }
            for (Iterator<TranslationRecord> it = p.core(); it.hasNext(); ) {
                Object n = it.next().node();
                if (n instanceof Formula)
                    lCore.add((Formula) n);
            }
            Map<Formula, Node> map = p.highLevelCore();
            hCore = new LinkedHashSet<Node>(map.keySet());
            hCore.addAll(map.values());
        } catch (Throwable ex) {
            lCore = hCore = null;
        }
    }
    // If satisfiable, then add/rename the atoms and skolems
    if (inst != null) {
        eval = new Evaluator(inst, solver.options());
        rename(this, null, null, new UniqueNameGenerator());
    }
    // report the result
    solved();
    time = System.currentTimeMillis() - time;
    if (inst != null)
        rep.resultSAT(cmd, time, this);
    else
        rep.resultUNSAT(cmd, time, this);
    return this;
}
Also used : HybridStrategy(kodkod.engine.ucore.HybridStrategy) Instance(kodkod.instance.Instance) Node(kodkod.ast.Node) BinaryFormula(kodkod.ast.BinaryFormula) Formula(kodkod.ast.Formula) Relation(kodkod.ast.Relation) RCEStrategy(kodkod.engine.ucore.RCEStrategy) Solution(kodkod.engine.Solution) A4Reporter(edu.mit.csail.sdg.alloy4.A4Reporter) A4Reporter(edu.mit.csail.sdg.alloy4.A4Reporter) AbstractReporter(kodkod.engine.config.AbstractReporter) Reporter(kodkod.engine.config.Reporter) TranslationRecord(kodkod.engine.fol2sat.TranslationRecord) Decl(kodkod.ast.Decl) Evaluator(kodkod.engine.Evaluator) UniqueNameGenerator(edu.mit.csail.sdg.alloy4.UniqueNameGenerator) Type(edu.mit.csail.sdg.ast.Type) Proof(kodkod.engine.Proof) File(java.io.File) Tuple(kodkod.instance.Tuple)

Aggregations

Decl (kodkod.ast.Decl)32 Formula (kodkod.ast.Formula)12 Variable (kodkod.ast.Variable)12 QuantifiedFormula (kodkod.ast.QuantifiedFormula)9 Decls (kodkod.ast.Decls)7 Expression (kodkod.ast.Expression)6 IntExpression (kodkod.ast.IntExpression)6 Relation (kodkod.ast.Relation)6 SumExpression (kodkod.ast.SumExpression)5 Solution (kodkod.engine.Solution)5 Test (org.junit.Test)5 BinaryFormula (kodkod.ast.BinaryFormula)4 BooleanFactory (kodkod.engine.bool.BooleanFactory)4 BooleanMatrix (kodkod.engine.bool.BooleanMatrix)4 BooleanValue (kodkod.engine.bool.BooleanValue)4 Instance (kodkod.instance.Instance)4 HashSet (java.util.HashSet)3 ComparisonFormula (kodkod.ast.ComparisonFormula)3 IntComparisonFormula (kodkod.ast.IntComparisonFormula)3 MultiplicityFormula (kodkod.ast.MultiplicityFormula)3