Package kodkod.ast

Examples of kodkod.ast.Expression


              continue again;
           }
           for(Field f:s.getFields()) {
              boolean isOne = s.isOne!=null;
              if (isOne && f.decl().expr.mult()==ExprUnary.Op.EXACTLYOF) {
                 Expression sim = sim(f.decl().expr);
                 if (sim!=null) {
                    rep.bound("Field "+s.label+"."+f.label+" defined to be "+sim+"\n");
                    sol.addField(f, sol.a2k(s).product(sim));
                    continue;
                 }
              }
              Type t = isOne ? Sig.UNIV.type().join(f.type()) : f.type();
              TupleSet ub = factory.noneOf(t.arity());
              for(List<PrimSig> p:t.fold()) {
                 TupleSet upper=null;
                 for(PrimSig b:p) {
                    TupleSet tmp = sol.query(true, sol.a2k(b), false);
                    if (upper==null) upper=tmp; else upper=upper.product(tmp);
                 }
                 ub.addAll(upper);
              }
              Relation r = sol.addRel(s.label+"."+f.label, null, ub);
              sol.addField(f, isOne ? sol.a2k(s).product(r) : r);
           }
        }
        // Add any additional SIZE constraints
        for(Sig s:sigs) if (!s.builtin) {
            Expression exp = sol.a2k(s);
            TupleSet upper = sol.query(true,exp,false), lower=sol.query(false,exp,false);
            final int n = sc.sig2scope(s);
            if (s.isOne!=null && (lower.size()!=1 || upper.size()!=1)) {
                rep.bound("Sig "+s+" in "+upper+" with size==1\n");
                sol.addFormula(exp.one(), s.isOne);
                continue;
            }
            if (s.isSome!=null && lower.size()<1) sol.addFormula(exp.some(), s.isSome);
            if (s.isLone!=null && upper.size()>1) sol.addFormula(exp.lone(), s.isLone);
            if (n<0) continue; // This means no scope was specified
            if (lower.size()==n && upper.size()==n && sc.isExact(s)) {
                rep.bound("Sig "+s+" == "+upper+"\n");
            }
            else if (sc.isExact(s)) {
View Full Code Here


               form = s.isOne==null ? form.forAll(s.decl) : ExprLet.make(null, (ExprVar)(s.decl.get()), s, form);
               frame.addFormula(cform(form), f);
               // Given the above, we can be sure that every column is well-bounded (except possibly the first column).
               // Thus, we need to add a bound that the first column is a subset of s.
               if (s.isOne==null) {
                   Expression sr = a2k(s), fr = a2k(f);
                   for(int i=f.type().arity(); i>1; i--) fr=fr.join(Relation.UNIV);
                   frame.addFormula(fr.in(sr), f);
               }
            }
            if (s.isOne==null && d.disjoint2!=null) for(ExprHasName f: d.names) {
View Full Code Here

          case TRUE: return Formula.TRUE;
          case FALSE: return Formula.FALSE;
          case EMPTYNESS: return Expression.NONE;
          case IDEN: return Expression.IDEN.intersection(a2k(UNIV).product(Expression.UNIV));
          case STRING:
            Expression ans = s2k(x.string);
            if (ans==null) throw new ErrorFatal(x.pos, "String literal "+x+" does not exist in this instance.\n");
            return ans;
          case NUMBER:
            int n=x.num();
            if (n<min) throw new ErrorType(x.pos, "Current bitwidth is set to "+bitwidth+", thus this integer constant "+n+" is smaller than the minimum integer "+min);
View Full Code Here

            case TRANSPOSE:   return cset(x.sub).transpose();
            case CARDINALITY: return cset(x.sub).count();
            case CAST2SIGINT: return cint(x.sub).toExpression();
            case CAST2INT:    return sum(cset(x.sub));
            case RCLOSURE:
                Expression iden=Expression.IDEN.intersection(a2k(UNIV).product(Relation.UNIV));
                return cset(x.sub).closure().union(iden);
            case CLOSURE: return cset(x.sub).closure();
        }
        throw new ErrorFatal(x.pos, "Unsupported operator ("+x.op+") encountered during ExprUnary.visit()");
    }
View Full Code Here

    /* Evaluates a Field node. */
    /*=========================*/

    /** {@inheritDoc} */
    @Override public Object visit(Field x) throws Err {
        Expression ans = a2k(x);
        if (ans==null) throw new ErrorFatal(x.pos, "Field \""+x+"\" is not bound to a legal value during translation.\n");
        return ans;
    }
View Full Code Here

    /* Evaluates a Sig node. */
    /*=======================*/

    /** {@inheritDoc} */
    @Override public Object visit(Sig x) throws Err {
        Expression ans = a2k(x);
        if (ans==null) throw new ErrorFatal(x.pos, "Sig \""+x+"\" is not bound to a legal value during translation.\n");
        return ans;
    }
View Full Code Here

            if (maxRecursion==0) {
                Type t = f.returnDecl.type();
                if (t.is_bool) return Formula.FALSE;
                if (t.is_int) return IntConstant.constant(0);
                int i = t.arity();
                Expression ans = Expression.NONE;
                while(i>1) { ans = ans.product(Expression.NONE); i--; }
                return ans;
            }
            maxRecursion--;
        }
        Env<ExprVar,Object> newenv = new Env<ExprVar,Object>();
View Full Code Here

           if (x.args.size()==0) return (x.op==ExprList.Op.AND) ? Formula.TRUE : Formula.FALSE;
           Formula answer = getSingleFormula(x.op==ExprList.Op.AND, 1, x.args);
           return k2pos(answer, x);
        }
        if (x.op == ExprList.Op.TOTALORDER) {
            Expression elem = cset(x.args.get(0)), first = cset(x.args.get(1)), next = cset(x.args.get(2));
            if (elem instanceof Relation && first instanceof Relation && next instanceof Relation) {
                Relation lst = frame.addRel("", null, frame.query(true, (Relation)elem, false));
                totalOrderPredicates.add((Relation)elem); totalOrderPredicates.add((Relation)first); totalOrderPredicates.add(lst); totalOrderPredicates.add((Relation)next);
                return k2pos(((Relation)next).totalOrder((Relation)elem, (Relation)first, lst), x);
            }
            Formula f1 = elem.in(first.join(next.reflexiveClosure())); // every element is in the total order
            Formula f2 = next.join(first).no(); // first element has no predecessor
            Variable e = Variable.unary("");
            Formula f3 = e.eq(first).or(next.join(e).one()); // each element (except the first) has one predecessor
            Formula f4 = e.eq(elem.difference(next.join(elem))).or(e.join(next).one()); // each element (except the last) has one successor
            Formula f5 = e.in(e.join(next.closure())).not(); // there are no cycles
            return k2pos(f3.and(f4).and(f5).forAll(e.oneOf(elem)).and(f1).and(f2), x);
        }
        // This says  no(a&b) and no((a+b)&c) and no((a+b+c)&d)...
        // Emperically this seems to be more efficient than "no(a&b) and no(a&c) and no(b&c)"
        Formula answer = null;
        Expression a = null;
        for(Expr arg:x.args) {
            Expression b=cset(arg);
            if (a==null) {a=b;continue;}
            if (answer==null) answer=a.intersection(b).no(); else answer=a.intersection(b).no().and(answer);
            a=a.union(b);
        }
        if (answer!=null) return k2pos(answer, x); else return Formula.TRUE;
View Full Code Here

    /*===============================*/

    /** {@inheritDoc} */
    @Override public Object visit(ExprBinary x) throws Err {
        Expr a=x.left, b=x.right;
        Expression s, s2; IntExpression i; Formula f; Object obj;
        switch(x.op) {
            case IMPLIES: f=cform(a).not().or(cform(b)); return k2pos(f,x);
            case IN:      return k2pos(isIn(cset(a),b), x);
            case NOT_IN:  return k2pos(isIn(cset(a),b).not(), x);
            case LT:  i=cint(a);  f=i.lt(cint(b));   return k2pos(f,x);
            case LTE: i=cint(a);  f=i.lte(cint(b))return k2pos(f,x);
            case GT:  i=cint(a);  f=i.gt(cint(b));   return k2pos(f,x);
            case GTE: i=cint(a);  f=i.gte(cint(b))return k2pos(f,x);
            case NOT_LT:  i=cint(a);  f=i.lt(cint(b)).not();   return k2pos(f,x);
            case NOT_LTE: i=cint(a);  f=i.lte(cint(b)).not()return k2pos(f,x);
            case NOT_GT:  i=cint(a);  f=i.gt(cint(b)).not();   return k2pos(f,x);
            case NOT_GTE: i=cint(a);  f=i.gte(cint(b)).not()return k2pos(f,x);
            case AND: f=cform(a); f=f.and(cform(b)); return k2pos(f,x);
            case OR:  f=cform(a); f=f.or(cform(b))return k2pos(f,x);
            case IFF: f=cform(a); f=f.iff(cform(b)); return k2pos(f,x);
            case PLUSPLUS: s=cset(a); return s.override(cset(b));
            case MUL: i=cint(a); return i.multiply(cint(b));
            case DIV: i=cint(a); return i.divide(cint(b));
            case REM: i=cint(a); return i.modulo(cint(b));
            case SHL: i=cint(a); return i.shl(cint(b));
            case SHR: i=cint(a); return i.shr(cint(b));
            case SHA: i=cint(a); return i.sha(cint(b));
            case PLUS:
                obj = visitThis(a);
                if (obj instanceof IntExpression) { i=(IntExpression)obj; return i.plus(cint(b)); }
                s = (Expression)obj; return s.union(cset(b));
            case MINUS:
                // Special exception to allow "0-8" to not throw an exception, where 7 is the maximum allowed integer (when bitwidth==4)
                // (likewise, when bitwidth==5, then +15 is the maximum allowed integer, and we want to allow 0-16 without throwing an exception)
                if (a instanceof ExprConstant && ((ExprConstant)a).op==ExprConstant.Op.NUMBER && ((ExprConstant)a).num()==0)
                   if (b instanceof ExprConstant && ((ExprConstant)b).op==ExprConstant.Op.NUMBER && ((ExprConstant)b).num()==max+1)
                      return IntConstant.constant(min);
                obj=visitThis(a);
                if (obj instanceof IntExpression) { i=(IntExpression)obj; return i.minus(cint(b));}
                s=(Expression)obj; return s.difference(cset(b));
            case INTERSECT:
                s=cset(a); return s.intersection(cset(b));
            case ANY_ARROW_SOME: case ANY_ARROW_ONE: case ANY_ARROW_LONE:
            case SOME_ARROW_ANY: case SOME_ARROW_SOME: case SOME_ARROW_ONE: case SOME_ARROW_LONE:
            case ONE_ARROW_ANY: case ONE_ARROW_SOME: case ONE_ARROW_ONE: case ONE_ARROW_LONE:
            case LONE_ARROW_ANY: case LONE_ARROW_SOME: case LONE_ARROW_ONE: case LONE_ARROW_LONE:
            case ISSEQ_ARROW_LONE:
            case ARROW:
                s=cset(a); return s.product(cset(b));
            case JOIN:
                a=a.deNOP(); s=cset(a); s2=cset(b);
                if (a instanceof Sig && ((Sig)a).isOne!=null && s2 instanceof BinaryExpression) {
                    BinaryExpression bin = (BinaryExpression)s2;
                    if (bin.op()==ExprOperator.PRODUCT && bin.left()==s) return bin.right();
                }
                return s.join(s2);
            case EQUALS:
                obj=visitThis(a);
                if (obj instanceof IntExpression) { i=(IntExpression)obj; f=i.eq(cint(b));}
                else { s=(Expression)obj; f=s.eq(cset(b)); }
                return k2pos(f,x);
            case NOT_EQUALS:
                obj=visitThis(a);
                if (obj instanceof IntExpression) { i=(IntExpression)obj; f=i.eq(cint(b)).not();}
                else { s=(Expression)obj; f=s.eq(cset(b)).not(); }
                return k2pos(f,x);
            case DOMAIN:
                s=cset(a);
                s2=cset(b);
                for(int j=s2.arity(); j>1; j--) s=s.product(Expression.UNIV);
                return s.intersection(s2);
            case RANGE:
                s=cset(a);
                s2=cset(b);
                for(int j=s.arity(); j>1; j--) s2=Expression.UNIV.product(s2);
                return s.intersection(s2);
        }
        throw new ErrorFatal(x.pos, "Unsupported operator ("+x.op+") encountered during ExprBinary.accept()");
    }
View Full Code Here

        throw new ErrorFatal(x.pos, "Unsupported operator ("+x.op+") encountered during ExprBinary.accept()");
    }

    /** Helper method that translates the formula "a in b" into a Kodkod formula. */
    private Formula isIn(Expression a, Expr right) throws Err {
       Expression b;
       if (right instanceof ExprBinary && right.mult!=0 && ((ExprBinary)right).op.isArrow) {
          // Handles possible "binary" or higher-arity multiplicity
          return isInBinary(a, (ExprBinary)right);
       }
       switch(right.mult()) {
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.