Package kodkod.ast

Examples of kodkod.ast.Formula


   * predicates replaced with equivalent constraints.
   */
  private AnnotatedNode<Formula> inlinePredicates(final AnnotatedNode<Formula> annotated, final Set<RelationPredicate> truePreds) {
    final AbstractReplacer inliner = new AbstractReplacer(annotated.sharedNodes()) {
      public Formula visit(RelationPredicate pred) {
        Formula ret = lookup(pred);
        if (ret!=null) return ret;
        return truePreds.contains(pred) ? cache(pred, Formula.TRUE) : cache(pred, pred.toConstraints());
      }
    };
    return annotate(annotated.node().accept(inliner))
View Full Code Here


          }
        }
        return super.cache(node, replacement);
      }
      public Formula visit(RelationPredicate pred) {
        Formula ret = lookup(pred);
        if (ret!=null) return ret;
        source = pred;
        if (simplified.containsKey(pred)) {
          ret = simplified.get(pred).accept(this);
        } else {
View Full Code Here

      final List<Formula> formulas = new LinkedList<Formula>();
    formulas.add(formula);
   
    final ListIterator<Formula> itr = formulas.listIterator();
    while(itr.hasNext()) {
      final Formula f = itr.next();
      if (f instanceof BinaryFormula) {
        final BinaryFormula bin = (BinaryFormula) f;
        if (bin.op()==FormulaOperator.AND) {
          itr.remove();
          itr.add(bin.left());
View Full Code Here

   */
  public static Set<Formula> conjuncts(Formula formula) {
    if (formula instanceof BinaryFormula) {
      final BinaryFormula bin = (BinaryFormula) formula;
      if (bin.op()==FormulaOperator.AND) {
        final Formula left = bin.left(), right = bin.right();
        if (left==right) return Collections.singleton(left);
        else return new AbstractSet<Formula>() {
          @Override
          public boolean contains(Object o) { return left==o || right==o; }
          @Override
View Full Code Here

   * The source map of the returned annotation object maps each descendant of the node to itself. 
   * The root conjunction itself is mapped to the input formula.
   * @return { a: AnnotatedNode<Formula> | a.node = Formula.and(Nodes.roots(formula)) && a.source = (node.^components<:iden) + a.node->formula }
   */
  public static AnnotatedNode<Formula> annotateRoots(Formula formula) {
    final Formula flat = Formula.and(Nodes.roots(formula));
    return new AnnotatedNode<Formula>(flat, Collections.singletonMap(flat, formula));
  }
View Full Code Here

      assert roots.size() == logMap.size();
      this.transl = new int[roots.size()];
      this.original = new Node[roots.size()];
      final Iterator<Formula> itr = roots.iterator();
      for(int i = 0; i < transl.length; i++) {
        final Formula root = itr.next();
        transl[i] = logMap.get(root).label();
        original[i] = annotated.sourceOf(root);
      }
    }
View Full Code Here

          final Node nsource = annotated.sourceOf(n);
          if (f!=nsource) source.put(f, nsource);
          return f;
        }
      };
      final Formula f = annotated.node().accept(r);
      return f==annotated.node() ? annotated : annotate(f, source);
    } else {
      final Skolemizer r = new Skolemizer(annotated, bounds, options) {};
      final Formula f = annotated.node().accept(r);
      return f==annotated.node() ? annotated : annotate(f);
    }
  }
View Full Code Here

  public final Expression visit(Comprehension expr) {
    Expression ret = lookup(expr);
    if (ret!=null) return ret;
    final Environment<Expression> oldRepEnv = repEnv; // skolemDepth < 0 at this point
    final Decls decls = visit((Decls)expr.decls());
    final Formula formula = expr.formula().accept(this);
    ret = (decls==expr.decls() && formula==expr.formula()) ? expr : formula.comprehension(decls);
    repEnv = oldRepEnv;   
    return cache(expr,ret);
  }
View Full Code Here

   * 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)
   */
  public final Formula visit(QuantifiedFormula qf) {
    Formula ret = lookup(qf);
    if (ret!=null) return ret;
   
    final Environment<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);
       
        final Relation skolem = Relation.nary("$"+ skolemDecl.variable().name(), nonSkolems.size() + skolemDecl.variable().arity());
        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(), 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((Decls)qf.decls());
      if (skolemDepth>=nonSkolems.size()+newDecls.size()) { // could skolemize below
        for(Decl d: newDecls) { nonSkolems.add(new DeclInfo(d)); }
        final Formula formula = qf.formula().accept(this);
        ret = ((newDecls==decls && formula==qf.formula()) ? qf : formula.quantify(quant, newDecls));
        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 formula = qf.formula().accept(this);
        ret = ((newDecls==decls && formula==qf.formula()) ? qf : formula.quantify(quant, newDecls));
        skolemDepth = oldDepth;
      }       
    } 
   
    repEnv = oldRepEnv;
View Full Code Here

  /**
   * Calls not.formula.accept(this) after flipping the negation flag and returns the result.
   * @see kodkod.ast.visitor.AbstractReplacer#visit(kodkod.ast.NotFormula)
   **/
  public final Formula visit(NotFormula not) {
    Formula ret = lookup(not);
    if (ret!=null) return ret;
    negated = !negated; // flip the negation flag
    final Formula retChild = not.formula().accept(this);
    negated = !negated;
    return retChild==not.formula() ? cache(not,not) : source(cache(not, retChild.not()), not);     
  }
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.