Package kodkod.ast

Examples of kodkod.ast.Formula


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


   * child.  If nothing changes, the argument is cached and
   * returned, otherwise a replacement formula is cached and returned.
   * @return { n: NotFormula | n.child = not.child.accept(this) }
   */
  public Formula visit(NotFormula not) {
    Formula ret = lookup(not);
    if (ret!=null) return ret;

    final Formula child = not.formula().accept(this);
    ret = (child==not.formula()) ? not : child.not();
    return cache(not,ret);
  }
View Full Code Here

   * @return { c: ComparisonFormula | c.left = compFormula.left.accept(this) &&
   *                                  c.right = compFormula.right.accept(this) &&
   *                                  c.op = compFormula.op }
   */
  public Formula visit(ComparisonFormula compFormula) {
    Formula ret = lookup(compFormula);
    if (ret!=null) return ret;
     
    final Expression left  = compFormula.left().accept(this);
    final Expression right = compFormula.right().accept(this);
    ret =  (left==compFormula.left() && right==compFormula.right()) ?
View Full Code Here

   * returned, otherwise a replacement formula is cached and returned.
   * @return { m: MultiplicityFormula | m.multiplicity = multFormula.multiplicity &&
   *                                    m.expression = multFormula.expression.accept(this) }
   */
  public Formula visit(MultiplicityFormula multFormula) {
    Formula ret = lookup(multFormula);
    if (ret!=null) return ret;
   
    final Expression expression = multFormula.expression().accept(this);
    ret = (expression==multFormula.expression()) ?
        multFormula : expression.apply(multFormula.multiplicity());
View Full Code Here

   *                                  p.name = TOTAL_ORDERING => p.ordered = pred.ordered.accept(this) &&
   *                                                             p.first = pred.first.accept(this) &&
   *                                                             p.last = pred.last.accept(this) }
   */
  public Formula visit(RelationPredicate pred) {
    Formula ret = lookup(pred);
    if (ret!=null) return ret;
 
    final Relation r = (Relation)pred.relation().accept(this);
    switch(pred.name()) {
    case ACYCLIC : 
View Full Code Here

    /* Evaluates an ExprITE node. */
    /*============================*/

    /** {@inheritDoc} */
    @Override public Object visit(ExprITE x) throws Err {
        Formula c = cform(x.cond);
        Object l = visitThis(x.left);
        if (l instanceof Formula) {
            Formula c1 = c.implies((Formula)l);
            Formula c2 = c.not().implies(cform(x.right));
            return k2pos(c1.and(c2), x);
        }
        if (l instanceof Expression) {
            return c.thenElse((Expression)l, cset(x.right));
        }
View Full Code Here

    /** Helper method that merge a list of conjuncts or disjoints while minimizing the AST depth (external caller should use i==1) */
    private Formula getSingleFormula(boolean isConjunct, int i, List<Expr> formulas) throws Err {
        // We actually build a "binary heap" where node X's two children are node 2X and node 2X+1
        int n = formulas.size();
        if (n==0) return isConjunct ? Formula.TRUE : Formula.FALSE;
        Formula me = cform(formulas.get(i-1)), other;
        int child1=i+i, child2=child1+1;
        if (child1<i || child1>n) return me;
        other = getSingleFormula(isConjunct, child1, formulas); if (isConjunct) me=me.and(other); else me=me.or(other);
        if (child2<1 || child2>n) return me;
        other = getSingleFormula(isConjunct, child2, formulas); if (isConjunct) me=me.and(other); else me=me.or(other);
        return me;
    }
View Full Code Here

    /** {@inheritDoc} */
    @Override public Object visit(ExprList x) throws Err {
        if (x.op == ExprList.Op.AND || x.op == ExprList.Op.OR) {
           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);
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));
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.