Package kodkod.ast

Examples of kodkod.ast.Expression


       }
    }

    /** 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);
        }
        return ans;
    }
View Full Code Here


      }
      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) {
View Full Code Here

                              // Error here is not fatal
                          }
                      }
                // The case above is STRICTLY an optimization; the entire statement can be removed without affecting correctness
                for(Field f: s.getFields()) {
                    Expression rel = sol.a2k(f);
                    if (s.isOne!=null) {
                        rel = right(rel);
                        if (!(rel instanceof Relation)) continue;
                        // Retrieve the old value from the previous solution, and convert it to the new unverse.
                        // This should always work since the new universe is not yet solved, and so it should have all possible atoms.
View Full Code Here

  public Decl visit(Decl decl) {
    Decl ret = lookup(decl);
    if (ret!=null) return ret;
   
    final Variable variable = (Variable) decl.variable().accept(this);
    final Expression expression = decl.expression().accept(this);
    ret = (variable==decl.variable() && expression==decl.expression()) ?
        decl : variable.declare(decl.multiplicity(), expression);
    return cache(decl,ret);
  }
View Full Code Here

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

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

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

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

   * returned, otherwise a replacement expression is cached and returned.
   * @return { b: BinaryExpression | b.left = binExpr.left.accept(this) &&
   *                                 b.right = binExpr.right.accept(this) && b.op = binExpr.op }
   */
  public Expression visit(BinaryExpression binExpr) {
    Expression ret = lookup(binExpr);
    if (ret!=null) return ret;
   
    final Expression left  = binExpr.left().accept(this);
    final Expression right = binExpr.right().accept(this);
    ret = (left==binExpr.left() && right==binExpr.right()) ?
        binExpr : left.compose(binExpr.op(), right);
    return cache(binExpr,ret);
  }
View Full Code Here

   * child.  If nothing changes, the argument is cached and
   * returned, otherwise a replacement expression is cached and returned.
   * @return { u: UnaryExpression | u.left = unaryExpr.expression.accept(this) && u.op = unaryExpr.op }
   */
  public Expression visit(UnaryExpression unaryExpr) {
    Expression ret = lookup(unaryExpr);
    if (ret!=null) return ret;

    final Expression child = unaryExpr.expression().accept(this);
    ret = (child==unaryExpr.expression()) ?
        unaryExpr : child.apply(unaryExpr.op());
    return cache(unaryExpr,ret);
  }
View Full Code Here

TOP

Related Classes of kodkod.ast.Expression

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.