Package kodkod.ast

Examples of kodkod.ast.Formula


    /** Helper method that returns the constraint that the sig has exactly "n" elements, or at most "n" elements */
    private Formula size(Sig sig, int n, boolean exact) {
        Expression a = sol.a2k(sig);
        if (n<=0) return a.no();
        if (n==1) return exact ? a.one() : a.lone();
        Formula f = exact ? Formula.TRUE : null;
        Decls d = null;
        Expression sum = null;
        while(n>0) {
           n--;
           Variable v = Variable.unary("");
View Full Code Here


    /** Helper method that translates the formula "r in (a ?->? b)" into a Kodkod formula. */
    private Formula isInBinary(Expression r, ExprBinary ab) throws Err {
        final Expression a=cset(ab.left), b=cset(ab.right);
        Decls d=null, d2=null;
        Formula ans1, ans2;
        // "R in A ->op B" means for each tuple a in A, there are "op" tuples in r that begins with a.
        Expression atuple=null, ar=r;
        for(int i=a.arity(); i>0; i--) {
           Variable v=Variable.unary("");
           if (a.arity()==1) d=v.oneOf(a); else if (d==null) d=v.oneOf(Relation.UNIV); else d=v.oneOf(Relation.UNIV).and(d);
           ar=v.join(ar);
           if (atuple==null) atuple=v; else atuple=atuple.product(v);
        }
        ans1=isIn(ar, ab.right);
        switch(ab.op) {
           case ISSEQ_ARROW_LONE:
           case ANY_ARROW_LONE: case SOME_ARROW_LONE: case ONE_ARROW_LONE: case LONE_ARROW_LONE: ans1=ar.lone().and(ans1); break;
           case ANY_ARROW_ONE:  case SOME_ARROW_ONE:  case ONE_ARROW_ONE:  case LONE_ARROW_ONE:  ans1=ar.one().and(ans1)break;
           case ANY_ARROW_SOME: case SOME_ARROW_SOME: case ONE_ARROW_SOME: case LONE_ARROW_SOME: ans1=ar.some().and(ans1); break;
        }
        if (a.arity()>1) { Formula tmp=isIn(atuple, ab.left); if (tmp!=Formula.TRUE) ans1=tmp.implies(ans1); }
        ans1=ans1.forAll(d);
        // "R in A op-> B" means for each tuple b in B, there are "op" tuples in r that end with b.
        Expression btuple=null, rb=r;
        for(int i=b.arity(); i>0; i--) {
           Variable v=Variable.unary("");
           if (b.arity()==1) d2=v.oneOf(b); else if (d2==null) d2=v.oneOf(Relation.UNIV); else d2=v.oneOf(Relation.UNIV).and(d2);
           rb=rb.join(v);
           if (btuple==null) btuple=v; else btuple=v.product(btuple);
        }
        ans2=isIn(rb, ab.left);
        switch(ab.op) {
           case LONE_ARROW_ANY: case LONE_ARROW_SOME: case LONE_ARROW_ONE: case LONE_ARROW_LONE: ans2=rb.lone().and(ans2); break;
           case ONE_ARROW_ANY:  case ONE_ARROW_SOME:  case ONE_ARROW_ONE:  case ONE_ARROW_LONE:  ans2=rb.one().and(ans2)break;
           case SOME_ARROW_ANY: case SOME_ARROW_SOME: case SOME_ARROW_ONE: case SOME_ARROW_LONE: ans2=rb.some().and(ans2); break;
        }
        if (b.arity()>1) { Formula tmp=isIn(btuple, ab.right); if (tmp!=Formula.TRUE) ans2=tmp.implies(ans2); }
        ans2=ans2.forAll(d2);
        // Now, put everything together
        Formula ans=r.in(a.product(b)).and(ans1).and(ans2);
        if (ab.op==ExprBinary.Op.ISSEQ_ARROW_LONE) {
            Expression rr=r;
            while(rr.arity()>1) rr=rr.join(Relation.UNIV);
            ans=rr.difference(rr.join(A4Solution.KK_NEXT)).in(A4Solution.KK_ZERO).and(ans);
        }
View Full Code Here

         }
         if (op==ExprQt.Op.ONE && ok) return ((Expression) visit_qt(ExprQt.Op.COMPREHENSION, xvars, sub)).one();
         if (op==ExprQt.Op.LONE && ok) return ((Expression) visit_qt(ExprQt.Op.COMPREHENSION, xvars, sub)).lone();
      }
      if (op == ExprQt.Op.ONE) {
         Formula f1 = (Formula) visit_qt(ExprQt.Op.LONE, xvars, sub);
         Formula f2 = (Formula) visit_qt(ExprQt.Op.SOME, xvars, sub);
         return f1.and(f2);
      }
      if (op == ExprQt.Op.LONE) {
         QuantifiedFormula p1 = (QuantifiedFormula) visit_qt(ExprQt.Op.ALL, xvars, sub);
         QuantifiedFormula p2 = (QuantifiedFormula) visit_qt(ExprQt.Op.ALL, xvars, sub);
         Decls s1 = p1.decls(), s2 = p2.decls(), decls = null;
         Formula f1 = p1.formula(), f2 = p2.formula();
         Formula[] conjuncts = new Formula[s1.size()];
         for(int i=0; i<conjuncts.length; i++) {
            kodkod.ast.Decl d1 = s1.get(i), d2 = s2.get(i);
            conjuncts[i] = d1.variable().eq(d2.variable());
            if (decls==null) decls = d1.and(d2); else decls = decls.and(d1).and(d2);
         }
         return f1.and(f2).implies(Formula.and(conjuncts)).forAll(decls);
      }
      Decls dd = null;
      List<Formula> guards = new ArrayList<Formula>();
      for(Decl dep: xvars) {
        final Expr dexexpr = addOne(dep.expr);
        final Expression dv = cset(dexexpr);
        for(ExprHasName dex: dep.names) {
           final Variable v = Variable.nary(skolem(dex.label), dex.type().arity());
           final kodkod.ast.Decl newd;
           env.put((ExprVar)dex, v);
           if (dex.type().arity()!=1) {
              guards.add(isIn(v, dexexpr));
              newd = v.setOf(dv);
           } else switch(dexexpr.mult()) {
              case SETOF: newd = v.setOf(dv); break;
              case SOMEOF: newd = v.someOf(dv); break;
              case LONEOF: newd = v.loneOf(dv); break;
              default: newd = v.oneOf(dv);
           }
           if (frame!=null) frame.kv2typepos(v, dex.type(), dex.pos);
           if (dd==null) dd = newd; else dd = dd.and(newd);
        }
      }
      final Formula ans = (op==ExprQt.Op.SUM) ? null : cform(sub) ;
      final IntExpression ians = (op!=ExprQt.Op.SUM) ? null : cint(sub) ;
      for(Decl d: xvars) for(ExprHasName v: d.names) env.remove((ExprVar)v);
      if (op==ExprQt.Op.COMPREHENSION) return ans.comprehension(dd); // guards.size()==0, since each var has to be unary
      if (op==ExprQt.Op.SUM) return ians.sum(dd);                    // guards.size()==0, since each var has to be unary
      if (op==ExprQt.Op.SOME) {
         if (guards.size()==0) return ans.forSome(dd);
         guards.add(ans);
         return Formula.and(guards).forSome(dd);
      } else {
         if (guards.size()==0) return ans.forAll(dd);
         return Formula.and(guards).implies(ans).forAll(dd);
      }
   }
View Full Code Here

  public Expression visit(Comprehension comprehension) {
    Expression ret = lookup(comprehension);
    if (ret!=null) return ret;
   
    final Decls decls = (Decls)comprehension.decls().accept(this);
    final Formula formula = comprehension.formula().accept(this);
    ret = (decls==comprehension.decls() && formula==comprehension.formula()) ?
        comprehension : formula.comprehension(decls);
    return cache(comprehension,ret);
  }
View Full Code Here

   */
  public Expression visit(IfExpression ifExpr) {
    Expression ret = lookup(ifExpr);
    if (ret!=null) return ret;

    final Formula condition = ifExpr.condition().accept(this);
    final Expression thenExpr = ifExpr.thenExpr().accept(this);
    final Expression elseExpr = ifExpr.elseExpr().accept(this);
    ret = (condition==ifExpr.condition() && thenExpr==ifExpr.thenExpr() &&
         elseExpr==ifExpr.elseExpr()) ?
          ifExpr : condition.thenElse(thenExpr, elseExpr);
    return cache(ifExpr,ret);
  }
View Full Code Here

   */
  public IntExpression visit(IfIntExpression intExpr) {
    IntExpression ret = lookup(intExpr);
    if (ret!=null) return ret;

    final Formula condition = intExpr.condition().accept(this);
    final IntExpression thenExpr = intExpr.thenExpr().accept(this);
    final IntExpression elseExpr = intExpr.elseExpr().accept(this);
    ret = (condition==intExpr.condition() && thenExpr==intExpr.thenExpr() &&
         elseExpr==intExpr.elseExpr()) ?
          intExpr : condition.thenElse(thenExpr, elseExpr);
    return cache(intExpr,ret);
  }
View Full Code Here

   * children.  If nothing changes, the argument is cached and
   * returned, otherwise a replacement formula is cached and returned.
   * @return { c: Formula | [[c]] = intComp.left.accept(this) op intComp.right.accept(this) }
   */
    public Formula visit(IntComparisonFormula intComp) {
    Formula ret = lookup(intComp);
    if (ret!=null) return ret;

    final IntExpression left  = intComp.left().accept(this);
    final IntExpression right = intComp.right().accept(this);
    ret =  (left==intComp.left() && right==intComp.right()) ?
View Full Code Here

   * If a replacement has not been cached, the constant is cached and
   * returned.
   * @return constant
   */
  public Formula visit(ConstantFormula constant) {
    final Formula ret = lookup(constant);
    return ret==null ? cache(constant,constant) : constant;
  }
View Full Code Here

   * returned, otherwise a replacement formula is cached and returned.
   * @return { q: QuantifiedFormula | q.declarations = quantFormula.declarations.accept(this) &&
   *                                  q.formula = quantFormula.formula.accept(this) }
   */
  public Formula visit(QuantifiedFormula quantFormula) {
    Formula ret = lookup(quantFormula);
    if (ret!=null) return ret;
   
    final Decls decls = (Decls)quantFormula.decls().accept(this);
    final Formula formula = quantFormula.formula().accept(this);
    ret = (decls==quantFormula.decls() && formula==quantFormula.formula()) ?
        quantFormula : formula.quantify(quantFormula.quantifier(), decls);
    return cache(quantFormula,ret);
  }
View Full Code Here

   * children.  If nothing changes, the argument is cached and
   * returned, otherwise a replacement formula is cached and returned.
   * @return { e: Expression | e.op = formula.op && #e.children = #formula.children && all i: [0..formula.children) | e.child(i) = formula.child(i).accept(this) }
   */
  public Formula visit(NaryFormula formula) {
    Formula ret = lookup(formula);
    if (ret!=null) return ret;
   
    final Formula[] visited = new Formula[formula.size()];
    boolean allSame = true;
    for(int i = 0 ; i < visited.length; i++) {
      final Formula child = formula.child(i);
      visited[i] = child.accept(this);
      allSame = allSame && visited[i]==child;
    }
   
    ret = allSame ? formula : Formula.compose(formula.op(), visited);
    return cache(formula,ret);
View Full Code Here

TOP

Related Classes of kodkod.ast.Formula

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.