/** 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;
  }
Beispiel #2
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();
  }