示例#1
0
文件: KILtoSMTLib.java 项目: kszr/k
 public static String translateImplication(
     SymbolicConstraint leftHandSide, SymbolicConstraint rightHandSide) {
   KILtoSMTLib leftTransformer = new KILtoSMTLib(true);
   KILtoSMTLib rightTransformer = new KILtoSMTLib(false);
   String leftExpression = ((SMTLibTerm) leftHandSide.accept(leftTransformer)).expression();
   String rightExpression = ((SMTLibTerm) rightHandSide.accept(rightTransformer)).expression();
   StringBuilder sb = new StringBuilder();
   sb.append(
       getSortAndFunctionDeclarations(
           leftHandSide.termContext().definition(),
           Sets.union(leftTransformer.variables(), rightTransformer.variables())));
   sb.append(getAxioms(leftHandSide.termContext().definition()));
   sb.append(getConstantDeclarations(leftTransformer.variables()));
   sb.append("(assert (and ");
   sb.append(leftExpression);
   sb.append(" (not ");
   Set<Variable> rightHandSideOnlyVariables =
       Sets.difference(rightTransformer.variables(), leftTransformer.variables());
   if (!rightHandSideOnlyVariables.isEmpty()) {
     sb.append("(exists (");
     sb.append(getQuantifiedVariables(rightHandSideOnlyVariables));
     sb.append(") ");
   }
   sb.append(rightExpression);
   if (!rightHandSideOnlyVariables.isEmpty()) {
     sb.append(")");
   }
   sb.append(")))");
   return sb.toString();
 }
示例#2
0
 @Override
 public void visit(SymbolicConstraint node) {
   for (Map.Entry<Variable, Term> entry : node.substitution().entrySet()) {
     entry.getKey().accept(this);
     entry.getValue().accept(this);
   }
   for (SymbolicConstraint.Equality equality : node.equalities()) {
     equality.leftHandSide().accept(this);
     equality.rightHandSide().accept(this);
   }
 }
示例#3
0
文件: KILtoSMTLib.java 项目: kszr/k
 public static String translateConstraint(SymbolicConstraint constraint) {
   KILtoSMTLib transformer = new KILtoSMTLib(true);
   String expression = ((SMTLibTerm) constraint.accept(transformer)).expression();
   return getSortAndFunctionDeclarations(
           constraint.termContext().definition(), transformer.variables())
       + getAxioms(constraint.termContext().definition())
       + getConstantDeclarations(transformer.variables())
       + "(assert "
       + expression
       + ")";
 }
示例#4
0
  private void narrowByRule(ConstrainedTerm constrainedTerm, Rule rule) {
    stopwatch.reset();
    stopwatch.start();

    constrainedTermResults = new ArrayList<ConstrainedTerm>();

    SymbolicConstraint leftHandSideConstraint =
        new SymbolicConstraint(constrainedTerm.termContext());
    leftHandSideConstraint.addAll(rule.requires());
    for (Variable variable : rule.freshVariables()) {
      leftHandSideConstraint.add(variable, IntToken.fresh());
    }

    ConstrainedTerm leftHandSide =
        new ConstrainedTerm(
            rule.leftHandSide(),
            rule.lookups().getSymbolicConstraint(constrainedTerm.termContext()),
            leftHandSideConstraint,
            constrainedTerm.termContext());

    for (SymbolicConstraint constraint : constrainedTerm.unify(leftHandSide)) {
      constraint.addAll(rule.ensures());
      /* rename rule variables in the constraints */
      Map<Variable, Variable> freshSubstitution = constraint.rename(rule.variableSet());

      Term result = rule.rightHandSide();
      /* rename rule variables in the rule RHS */
      result = result.substituteWithBinders(freshSubstitution, constrainedTerm.termContext());
      /* apply the constraints substitution on the rule RHS */
      result =
          result.substituteWithBinders(constraint.substitution(), constrainedTerm.termContext());
      /* evaluate pending functions in the rule RHS */
      result = result.evaluate(constrainedTerm.termContext());
      /* eliminate anonymous variables */
      constraint.eliminateAnonymousVariables();

      /* compute all results */
      constrainedTermResults.add(
          new ConstrainedTerm(result, constraint, constrainedTerm.termContext()));
    }

    stopwatch.stop();
  }
示例#5
0
  // apply rule by matching
  private void rewriteByRule(Term term, Rule rule) {
    stopwatch.reset();
    stopwatch.start();

    termResults = new ArrayList<Term>();

    TermContext context = TermContext.of(definition);
    ConstrainedTerm constrainedTerm = new ConstrainedTerm(term, context);

    SymbolicConstraint leftHandSideConstraint = new SymbolicConstraint(context);
    leftHandSideConstraint.addAll(rule.requires());
    for (Variable variable : rule.freshVariables()) {
      leftHandSideConstraint.add(variable, IntToken.fresh());
    }

    ConstrainedTerm leftHandSide =
        new ConstrainedTerm(
            rule.leftHandSide(),
            rule.lookups().getSymbolicConstraint(context),
            leftHandSideConstraint,
            context);

    for (SymbolicConstraint constraint : constrainedTerm.unify(leftHandSide)) {
      if (!constraint.isMatching(leftHandSide)) {
        continue;
      }

      constraint.orientSubstitution(leftHandSide.variableSet());

      Term result = rule.rightHandSide();
      /* apply the constraints substitution on the rule RHS */
      result = result.substituteAndEvaluate(constraint.substitution(), context);

      /* compute all results */
      termResults.add(result);
    }

    stopwatch.stop();
  }
  @Override
  public ASTNode transform(org.kframework.kil.Rule node) throws TransformerException {
    assert node.getBody() instanceof org.kframework.kil.Rewrite;

    org.kframework.kil.Rewrite rewrite = (org.kframework.kil.Rewrite) node.getBody();

    Term leftHandSide = (Term) rewrite.getLeft().accept(this);
    Term rightHandSide = (Term) rewrite.getRight().accept(this);

    Collection<Term> requires = new ArrayList<Term>();
    Collection<Variable> freshVariables = new ArrayList<Variable>();
    // TODO: Deal with Ensures
    if (node.getRequires() != null) {
      Term term = (Term) node.getRequires().accept(this);
      if (term instanceof KItem && ((KItem) term).kLabel().toString().equals("'_andBool_")) {
        for (Term item : ((KItem) term).kList().getItems()) {
          if (item instanceof KItem && ((KItem) item).kLabel().toString().equals("'fresh(_)")) {
            freshVariables.add((Variable) ((KItem) item).kList().get(0));
          } else {
            requires.add(item);
          }
        }
      } else {
        if (term instanceof KItem && ((KItem) term).kLabel().toString().equals("'fresh(_)")) {
          freshVariables.add((Variable) ((KItem) term).kList().get(0));
        } else {
          requires.add(term);
        }
      }
    }

    SymbolicConstraint lookups = new SymbolicConstraint(new TermContext(definition));
    for (org.kframework.kil.BuiltinLookup lookup : node.getLookups()) {
      Variable base = (Variable) lookup.base().accept(this);
      Term key = (Term) lookup.key().accept(this);

      if (lookup instanceof org.kframework.kil.SetLookup) {
        lookups.add(new SetLookup(base, key), BoolToken.TRUE);
      } else {
        Term value = (Term) lookup.value().accept(this);
        if (lookup instanceof org.kframework.kil.MapLookup) {
          lookups.add(new MapLookup(base, key), value);
        } else { // ListLookup
          lookups.add(new ListLookup(base, key), value);
        }
      }
    }

    assert leftHandSide.kind() == rightHandSide.kind()
        || ((leftHandSide.kind() == Kind.KITEM
                || leftHandSide.kind() == Kind.K
                || leftHandSide.kind() == Kind.KLIST)
            && (rightHandSide.kind() == Kind.KITEM
                || rightHandSide.kind() == Kind.K
                || rightHandSide.kind() == Kind.KLIST));

    Rule rule =
        new Rule(
            leftHandSide, rightHandSide, requires, freshVariables, lookups, node.getAttributes());

    return rule.getFreshRule(new TermContext(definition));
  }