Example #1
0
 private static String getAxioms(Definition definition) {
   StringBuilder sb = new StringBuilder();
   for (Rule rule : definition.functionRules().values()) {
     if (rule.containsAttribute(Attribute.SMT_LEMMA_KEY)) {
       try {
         KILtoSMTLib transformer = new KILtoSMTLib(false);
         String leftExpression =
             ((SMTLibTerm) rule.leftHandSide().accept(transformer)).expression();
         String rightExpression =
             ((SMTLibTerm) rule.rightHandSide().accept(transformer)).expression();
         sb.append("(assert (forall (");
         sb.append(getQuantifiedVariables(transformer.variables()));
         sb.append(") (! (= ");
         sb.append(leftExpression);
         sb.append(" ");
         sb.append(rightExpression);
         sb.append(") :pattern(");
         sb.append(leftExpression);
         sb.append("))))\n");
       } catch (UnsupportedOperationException e) {
       }
     }
   }
   return sb.toString();
 }
  @Override
  public ASTNode visit(org.kframework.kil.Definition node, Void _void) {
    Definition definition = new Definition(context, kem, indexingData);
    globalContext.setDefinition(definition);

    Module singletonModule = node.getSingletonModule();

    for (org.kframework.kil.Rule rule : singletonModule.getRules()) {
      if (rule.containsAttribute(Attribute.PREDICATE_KEY)) {
        continue;
      }

      definition.addRule((Rule) this.visitNode(rule));
    }

    for (String kLabelName : singletonModule.getModuleKLabels()) {
      definition.addKLabel(KLabelConstant.of(kLabelName, globalContext.getDefinition()));
    }

    return definition;
  }
  @Override
  public ASTNode transform(org.kframework.kil.Definition node) {
    Definition definition = new Definition(context);
    this.definition = definition;

    Module singletonModule = node.getSingletonModule();

    for (org.kframework.kil.Rule rule : singletonModule.getRules()) {
      if (!rule.containsAttribute(SymbolicBackend.SYMBOLIC)
          || rule.containsAttribute(Attribute.PREDICATE.getKey())
          || rule.containsAttribute(Attribute.ANYWHERE.getKey())) {
        continue;
      }

      try {
        definition.addRule((Rule) rule.accept(this));
      } catch (TransformerException e) {
        System.err.println(rule);
        System.err.flush();
        e.printStackTrace();
      }
    }

    for (String kLabelName : singletonModule.getModuleKLabels()) {
      definition.addKLabel(KLabelConstant.of(kLabelName, context));
    }

    /* collect the productions which have the attributes strict and seqstrict */
    Set<Production> productions = singletonModule.getSyntaxByTag("strict", context);
    productions.addAll(singletonModule.getSyntaxByTag("seqstrict", context));
    for (Production production : productions) {
      definition.addFrozenKLabel(KLabelConstant.of(production.getKLabel(), context));
    }

    this.definition = null;
    return definition;
  }
Example #4
0
  private static String getSortAndFunctionDeclarations(
      Definition definition, Set<Variable> variables) {
    Set<Sort> sorts = new HashSet<>();
    List<Production> functions = new ArrayList<>();
    for (Production production : definition.context().productions) {
      String smtlib = production.getAttribute(Attribute.SMTLIB_KEY);
      if (smtlib != null && !SMTLIB_BUILTIN_FUNCTIONS.contains(smtlib) && !smtlib.startsWith("(")) {
        functions.add(production);
        sorts.add(Sort.of(production.getSort()));
        for (int i = 0; i < production.getArity(); ++i) {
          sorts.add(Sort.of(production.getChildSort(i)));
        }
      }
    }
    for (Variable variable : variables) {
      sorts.add(variable.sort());
    }

    if (!Sets.intersection(sorts, RESERVED_Z3_SORTS).isEmpty()) {
      throw new UnsupportedOperationException("do not use sorts " + RESERVED_Z3_SORTS);
    }

    StringBuilder sb = new StringBuilder();

    for (Sort sort : Sets.difference(sorts, SMTLIB_BUILTIN_SORTS)) {
      sb.append("(declare-sort ");
      sb.append(sort);
      sb.append(")\n");
    }

    for (Production production : functions) {
      sb.append("(declare-fun ");
      sb.append(production.getAttribute(Attribute.SMTLIB_KEY));
      sb.append(" (");
      List<String> childrenSorts = new ArrayList<>();
      for (int i = 0; i < production.getArity(); ++i) {
        childrenSorts.add(getSortName(production.getChildNode(i)));
      }
      Joiner.on(" ").appendTo(sb, childrenSorts);
      sb.append(") ");
      sb.append(getSortName(production));
      sb.append(")\n");
    }

    return sb.toString();
  }
  /** Partially evaluate the right-hand side and the conditions for each rule. */
  private static Definition evaluateDefinition(TermContext termContext) {
    Definition definition = termContext.global().getDefinition();
    /* replace the unevaluated rules defining functions with their partially evaluated counterparts */
    ArrayList<Rule> partiallyEvaluatedRules = new ArrayList<>();
    /* iterate until a fixpoint is reached, because the evaluation with functions uses Term#substituteAndEvalaute */
    while (true) {
      boolean change = false;

      partiallyEvaluatedRules.clear();
      for (Rule rule :
          Iterables.concat(
              definition.functionRules().values(), definition.anywhereRules().values())) {
        Rule freshRule = rule.getFreshRule(termContext);
        Rule evaluatedRule = evaluateRule(freshRule, termContext);
        partiallyEvaluatedRules.add(evaluatedRule);

        if (!evaluatedRule.equals(freshRule)) {
          change = true;
        }
      }

      if (!change) {
        break;
      }

      definition.functionRules().clear();
      definition.anywhereRules().clear();
      definition.addRuleCollection(partiallyEvaluatedRules);
    }

    /* replace the unevaluated rules and macros with their partially evaluated counterparts */
    partiallyEvaluatedRules.clear();
    Iterable<Rule> rules =
        Iterables.concat(
            definition.rules(),
            definition.macros(),
            definition.patternRules().values(),
            definition.patternFoldingRules());
    for (Rule rule : rules) {
      partiallyEvaluatedRules.add(evaluateRule(rule, termContext));
    }
    definition.rules().clear();
    definition.macros().clear();
    definition.patternRules().clear();
    definition.patternFoldingRules().clear();
    definition.addRuleCollection(partiallyEvaluatedRules);

    return definition;
  }
Example #6
0
  @Override
  public void buildIndex() {

    /* populate the table of rules rewriting the top configuration */
    List<Index> indices = new ArrayList<Index>();
    indices.add(TopIndex.TOP);
    indices.add(BottomIndex.BOTTOM);
    for (KLabelConstant kLabel : definition.kLabels()) {
      indices.add(new KLabelIndex(kLabel));
      indices.add(new FreezerIndex(kLabel, -1));
      if (!kLabel.productions().isEmpty()) {
        int maxArity = getMaxArityForProductions(kLabel.productions());
        for (int i = 0; i < maxArity; ++i) {
          indices.add(new FreezerIndex(kLabel, i));
        }
      }
    }
    // for (KLabelConstant frozenKLabel : definition.frozenKLabels()) {
    //    for (int i = 0; i < frozenKLabel.productions().get(0).getArity(); ++i) {
    //        indices.add(new FreezerIndex(frozenKLabel, i));
    //    }
    // }
    for (String sort : definition.builtinSorts()) {
      indices.add(new TokenIndex(sort));
    }

    /* Map each index to a list of rules unifiable with that index */
    /* Heating rules and regular rules have their first index checked */
    /* Cooling rules have their second index checked */
    ImmutableMap.Builder<Index, List<Rule>> mapBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<Index, List<Rule>> heatingMapBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<Index, List<Rule>> coolingMapBuilder = ImmutableMap.builder();
    ImmutableMap.Builder<Index, List<Rule>> simulationMapBuilder = ImmutableMap.builder();

    for (Index index : indices) {
      ImmutableList.Builder<Rule> listBuilder = ImmutableList.builder();
      ImmutableList.Builder<Rule> heatingListBuilder = ImmutableList.builder();
      ImmutableList.Builder<Rule> coolingListBuilder = ImmutableList.builder();
      ImmutableList.Builder<Rule> simulationListBuilder = ImmutableList.builder();

      for (Rule rule : definition.rules()) {
        if (rule.containsAttribute("heat")) {
          if (index.isUnifiable(rule.indexingPair().first)) {
            heatingListBuilder.add(rule);
          }
        } else if (rule.containsAttribute("cool")) {
          if (index.isUnifiable(rule.indexingPair().second)) {
            coolingListBuilder.add(rule);
          }
        } else if (rule.containsAttribute("alphaRule")) {
          if (index.isUnifiable(rule.indexingPair().first)) {
            simulationListBuilder.add(rule);
          }

        } else {
          if (index.isUnifiable(rule.indexingPair().first)) {
            listBuilder.add(rule);
          }
        }
      }
      ImmutableList<Rule> rules = listBuilder.build();
      if (!rules.isEmpty()) {
        mapBuilder.put(index, rules);
      }
      rules = heatingListBuilder.build();
      if (!rules.isEmpty()) {
        heatingMapBuilder.put(index, rules);
      }
      rules = coolingListBuilder.build();
      if (!rules.isEmpty()) {
        coolingMapBuilder.put(index, rules);
      }
      rules = simulationListBuilder.build();
      if (!rules.isEmpty()) {
        simulationMapBuilder.put(index, rules);
      }
    }
    heatingRuleTable = heatingMapBuilder.build();
    coolingRuleTable = coolingMapBuilder.build();
    ruleTable = mapBuilder.build();
    simulationRuleTable = simulationMapBuilder.build();

    ImmutableList.Builder<Rule> listBuilder = ImmutableList.builder();
    for (Rule rule : definition.rules()) {
      if (!rule.containsKCell()) {
        listBuilder.add(rule);
      }
    }
    unindexedRules = listBuilder.build();
  }