Search in sources :

Example 1 with QuantifiedFormula

use of kodkod.ast.QuantifiedFormula 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 2 with QuantifiedFormula

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

the class Skolemizer method visit.

/**
 * Skolemizes the given formula, if possible, otherwise returns the result of
 * replacing its free variables according to the current replacement
 * environment.
 *
 * @see kodkod.ast.visitor.AbstractReplacer#visit(kodkod.ast.QuantifiedFormula)
 */
@Override
public final Formula visit(QuantifiedFormula qf) {
    Formula ret = lookup(qf);
    if (ret != null)
        return ret;
    final Environment<Expression, Expression> oldRepEnv = repEnv;
    final Quantifier quant = qf.quantifier();
    final Decls decls = qf.decls();
    if (skolemDepth >= 0 && (negated && quant == ALL || !negated && quant == SOME)) {
        // skolemizable
        // formula
        final List<Formula> rangeConstraints = new LinkedList<Formula>();
        final List<Formula> domConstraints = new LinkedList<Formula>();
        for (Decl decl : decls) {
            final Decl skolemDecl = visit(decl);
            Variable skVar = skolemDecl.variable();
            final Relation skolem = Relation.skolem("$" + skVar.name(), nonSkolems.size() + skVar.arity(), skVar, skolemDecl, quant);
            reporter.skolemizing(decl, skolem, nonSkolemsView);
            final Expression skolemExpr = skolemExpr(skolemDecl, skolem);
            final Multiplicity mult = decl.multiplicity();
            rangeConstraints.add(source(skolemExpr.in(skolemDecl.expression()), decl));
            if (mult != Multiplicity.SET) {
                rangeConstraints.add(source(skolemExpr.apply(mult), decl));
            }
            if (!nonSkolems.isEmpty())
                domConstraints.add(source(domainConstraint(skolemDecl, skolem), decl));
            repEnv = repEnv.extend(decl.variable(), decl.expression(), skolemExpr);
        }
        ret = source(Formula.and(rangeConstraints), decls).compose(negated ? IMPLIES : AND, qf.formula().accept(this));
        if (!domConstraints.isEmpty())
            topSkolemConstraints.add(source(Formula.and(domConstraints), decls));
    } else {
        // non-skolemizable formula
        final Decls newDecls = visit(qf.decls());
        if (skolemDepth >= nonSkolems.size() + newDecls.size()) {
            // below
            for (Decl d : newDecls) {
                nonSkolems.add(new DeclInfo(d));
            }
            final Formula domain = qf.domain().accept(this);
            final Formula body = qf.body().accept(this);
            ret = ((newDecls == decls && domain == qf.domain() && body == qf.body()) ? qf : body.quantify(quant, newDecls, domain));
            for (int i = newDecls.size(); i > 0; i--) {
                nonSkolems.remove(nonSkolems.size() - 1);
            }
        } else {
            // can't skolemize below
            final int oldDepth = skolemDepth;
            skolemDepth = -1;
            final Formula domain = qf.domain().accept(this);
            final Formula body = qf.body().accept(this);
            ret = ((newDecls == decls && domain == qf.domain() && body == qf.body()) ? qf : body.quantify(quant, newDecls, domain));
            skolemDepth = oldDepth;
        }
    }
    repEnv = oldRepEnv;
    if (repEnv.isEmpty() && !topSkolemConstraints.isEmpty()) {
        ret = source(Formula.and(topSkolemConstraints), qf).compose(negated ? IMPLIES : AND, ret);
    }
    return source(cache(qf, ret), qf);
}
Also used : Variable(kodkod.ast.Variable) Decls(kodkod.ast.Decls) Decl(kodkod.ast.Decl) LinkedList(java.util.LinkedList) 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) Relation(kodkod.ast.Relation) SumExpression(kodkod.ast.SumExpression) IntExpression(kodkod.ast.IntExpression) Expression(kodkod.ast.Expression) Multiplicity(kodkod.ast.operator.Multiplicity) Quantifier(kodkod.ast.operator.Quantifier)

Example 3 with QuantifiedFormula

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

the class TranslateKodkodToJava method countHeight.

/**
 * Count the height of the given Kodkod AST tree.
 */
public static int countHeight(Node node) {
    ReturnVisitor<Integer, Integer, Integer, Integer> vis = new ReturnVisitor<Integer, Integer, Integer, Integer>() {

        private int max(int a, int b) {
            return (a >= b) ? a : b;
        }

        private int max(int a, int b, int c) {
            return (a >= b) ? (a >= c ? a : c) : (b >= c ? b : c);
        }

        @Override
        public Integer visit(Relation x) {
            return 1;
        }

        @Override
        public Integer visit(IntConstant x) {
            return 1;
        }

        @Override
        public Integer visit(ConstantFormula x) {
            return 1;
        }

        @Override
        public Integer visit(Variable x) {
            return 1;
        }

        @Override
        public Integer visit(ConstantExpression x) {
            return 1;
        }

        @Override
        public Integer visit(NotFormula x) {
            return 1 + x.formula().accept(this);
        }

        @Override
        public Integer visit(IntToExprCast x) {
            return 1 + x.intExpr().accept(this);
        }

        @Override
        public Integer visit(Decl x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(ExprToIntCast x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(UnaryExpression x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(UnaryIntExpression x) {
            return 1 + x.intExpr().accept(this);
        }

        @Override
        public Integer visit(MultiplicityFormula x) {
            return 1 + x.expression().accept(this);
        }

        @Override
        public Integer visit(BinaryExpression x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(ComparisonFormula x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(BinaryFormula x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(BinaryIntExpression x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(IntComparisonFormula x) {
            return 1 + max(x.left().accept(this), x.right().accept(this));
        }

        @Override
        public Integer visit(IfExpression x) {
            return 1 + max(x.condition().accept(this), x.thenExpr().accept(this), x.elseExpr().accept(this));
        }

        @Override
        public Integer visit(IfIntExpression x) {
            return 1 + max(x.condition().accept(this), x.thenExpr().accept(this), x.elseExpr().accept(this));
        }

        @Override
        public Integer visit(SumExpression x) {
            return 1 + max(x.decls().accept(this), x.intExpr().accept(this));
        }

        @Override
        public Integer visit(QuantifiedFormula x) {
            return 1 + max(x.decls().accept(this), x.formula().accept(this));
        }

        @Override
        public Integer visit(FixFormula x) {
            return 1 + max(x.condition().accept(this), x.formula().accept(this));
        }

        @Override
        public Integer visit(Comprehension x) {
            return 1 + max(x.decls().accept(this), x.formula().accept(this));
        }

        @Override
        public Integer visit(Decls x) {
            int max = 0, n = x.size();
            for (int i = 0; i < n; i++) max = max(max, x.get(i).accept(this));
            return max;
        }

        @Override
        public Integer visit(ProjectExpression x) {
            int max = x.expression().accept(this);
            for (Iterator<IntExpression> t = x.columns(); t.hasNext(); ) {
                max = max(max, t.next().accept(this));
            }
            return max;
        }

        @Override
        public Integer visit(RelationPredicate x) {
            if (x instanceof Function) {
                Function f = ((Function) x);
                return max(f.domain().accept(this), f.range().accept(this));
            }
            return 1;
        }

        @Override
        public Integer visit(NaryExpression x) {
            int max = 0;
            for (int m = 0, n = x.size(), i = 0; i < n; i++) {
                m = x.child(i).accept(this);
                if (i == 0 || max < m)
                    max = m;
            }
            return max + 1;
        }

        @Override
        public Integer visit(NaryIntExpression x) {
            int max = 0;
            for (int m = 0, n = x.size(), i = 0; i < n; i++) {
                m = x.child(i).accept(this);
                if (i == 0 || max < m)
                    max = m;
            }
            return max + 1;
        }

        @Override
        public Integer visit(NaryFormula x) {
            int max = 0;
            for (int m = 0, n = x.size(), i = 0; i < n; i++) {
                m = x.child(i).accept(this);
                if (i == 0 || max < m)
                    max = m;
            }
            return max + 1;
        }
    };
    Object ans = node.accept(vis);
    if (ans instanceof Integer)
        return ((Integer) ans).intValue();
    else
        return 0;
}
Also used : Variable(kodkod.ast.Variable) UnaryIntExpression(kodkod.ast.UnaryIntExpression) Decls(kodkod.ast.Decls) ConstantExpression(kodkod.ast.ConstantExpression) ComparisonFormula(kodkod.ast.ComparisonFormula) IntComparisonFormula(kodkod.ast.IntComparisonFormula) UnaryExpression(kodkod.ast.UnaryExpression) Function(kodkod.ast.RelationPredicate.Function) Relation(kodkod.ast.Relation) BinaryExpression(kodkod.ast.BinaryExpression) IntConstant(kodkod.ast.IntConstant) NaryIntExpression(kodkod.ast.NaryIntExpression) NotFormula(kodkod.ast.NotFormula) QuantifiedFormula(kodkod.ast.QuantifiedFormula) IfExpression(kodkod.ast.IfExpression) IntToExprCast(kodkod.ast.IntToExprCast) ProjectExpression(kodkod.ast.ProjectExpression) IfIntExpression(kodkod.ast.IfIntExpression) IfIntExpression(kodkod.ast.IfIntExpression) UnaryIntExpression(kodkod.ast.UnaryIntExpression) NaryIntExpression(kodkod.ast.NaryIntExpression) BinaryIntExpression(kodkod.ast.BinaryIntExpression) IntExpression(kodkod.ast.IntExpression) RelationPredicate(kodkod.ast.RelationPredicate) NaryExpression(kodkod.ast.NaryExpression) BinaryIntExpression(kodkod.ast.BinaryIntExpression) Decl(kodkod.ast.Decl) ConstantFormula(kodkod.ast.ConstantFormula) Comprehension(kodkod.ast.Comprehension) NaryFormula(kodkod.ast.NaryFormula) ExprToIntCast(kodkod.ast.ExprToIntCast) MultiplicityFormula(kodkod.ast.MultiplicityFormula) BinaryFormula(kodkod.ast.BinaryFormula) SumExpression(kodkod.ast.SumExpression) FixFormula(kodkod.ast.FixFormula) ReturnVisitor(kodkod.ast.visitor.ReturnVisitor) IntComparisonFormula(kodkod.ast.IntComparisonFormula)

Example 4 with QuantifiedFormula

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

the class HOLSome4AllTest method testE8.

@Test
public void testE8() {
    // UNSAT: some s: ints - (-1) |
    // s > 3 || (all ns: set Node | #ns > s)
    Formula cnd = si.gt(I3);
    QuantifiedFormula allQF = (QuantifiedFormula) ns.count().gt(si).forAll(ns.setOf(Node));
    Decl someDecls = s.oneOf(Expression.INTS.difference(M1.toExpression()));
    {
        Formula f = cnd.or(allQF).forSome(someDecls);
        assertFalse(solve(f).sat());
    }
    // same thing, but inner flipped
    // UNSAT: some s: ints - (-1) |
    // s > 3 || (all ns: set Node | #ns > s)
    {
        Formula f = cnd.or(flip(allQF)).forSome(someDecls);
        assertFalse(solve(f).sat());
    }
}
Also used : QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) Decl(kodkod.ast.Decl) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Test(org.junit.Test)

Example 5 with QuantifiedFormula

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

the class HOLSome4AllTest method testE9.

@Test
public void testE9() {
    // UNSAT: some s: ints - (-1) |
    // s > 3 || (some ns: set Node | #ns > s + 3)
    Formula cnd = si.gt(I3);
    QuantifiedFormula innerSomeQF = (QuantifiedFormula) ns.count().gt(si.plus(I3)).forSome(ns.setOf(Node));
    Decl someDecls = s.oneOf(Expression.INTS.difference(M1.toExpression()));
    Formula f = cnd.or(innerSomeQF).forSome(someDecls);
    assertFalse(solve(f).sat());
}
Also used : QuantifiedFormula(kodkod.ast.QuantifiedFormula) Formula(kodkod.ast.Formula) Decl(kodkod.ast.Decl) QuantifiedFormula(kodkod.ast.QuantifiedFormula) Test(org.junit.Test)

Aggregations

QuantifiedFormula (kodkod.ast.QuantifiedFormula)14 Formula (kodkod.ast.Formula)12 Decl (kodkod.ast.Decl)9 BinaryFormula (kodkod.ast.BinaryFormula)7 NaryFormula (kodkod.ast.NaryFormula)7 NotFormula (kodkod.ast.NotFormula)7 ComparisonFormula (kodkod.ast.ComparisonFormula)6 IntComparisonFormula (kodkod.ast.IntComparisonFormula)6 MultiplicityFormula (kodkod.ast.MultiplicityFormula)6 ConstantFormula (kodkod.ast.ConstantFormula)5 Decls (kodkod.ast.Decls)5 Test (org.junit.Test)5 FixFormula (kodkod.ast.FixFormula)4 IntExpression (kodkod.ast.IntExpression)3 Variable (kodkod.ast.Variable)3 Quantifier (kodkod.ast.operator.Quantifier)3 Solution (kodkod.engine.Solution)3 ArrayList (java.util.ArrayList)2 BinaryExpression (kodkod.ast.BinaryExpression)2 ExprToIntCast (kodkod.ast.ExprToIntCast)2