public void convert(K k) { k = new AddSortInjections(module).addInjections(k); new VisitK() { @Override
private Sort lubSort(Sort leftSort, Sort rightSort, Sort expectedSort, HasLocation loc) { if (leftSort == null && rightSort == null) { return expectedSort; } else if (leftSort == null) { return rightSort; } else if (rightSort == null) { return leftSort; } return lub(Arrays.asList(leftSort, rightSort), loc); }
private Sort lub(Collection<Sort> entries, HasLocation loc) { Set<Sort> bounds = upperBounds(entries); Set<Sort> lub = mod.subsorts().minimal(bounds); if (lub.size() != 1) { throw KEMException.internalError("Could not compute least upper bound for rewrite sort.", loc); } return lub.iterator().next(); }
return kapp.klabel().params().apply(1); Production prod = production(kapp); if (prod.att().contains("poly")) { List<Set<Integer>> poly = RuleGrammarGenerator.computePositions(prod); Set<Sort> children = new HashSet<>(); for (int position : otherPositions) { children.add(sort(kapp.items().get(position-1), expectedSort)); return lub(children, term); return production((KApply)term).sort(); } else if (term instanceof KRewrite) { KRewrite rew = (KRewrite)term; Sort leftSort = sort(rew.left(), expectedSort); Sort rightSort = sort(rew.right(), expectedSort); return lubSort(leftSort, rightSort, expectedSort, term); } else if (term instanceof KToken) { return ((KToken) term).sort(); } else if (term instanceof KAs) { KAs as = (KAs) term; Sort patternSort = sort(as.pattern(), expectedSort); Sort rightSort = sort(as.alias(), expectedSort); return lubSort(patternSort, rightSort, expectedSort, term); } else { throw KEMException.internalError("Invalid category of k found.", term);
return term; Production prod = production(kapp); List<K> children = new ArrayList<>(); Set<Integer> polyPositions = Collections.emptySet(); children.add(addInjections(child, expectedSort)); return KRewrite(addInjections(rew.left(), actualSort), addInjections(rew.right(), actualSort), att); } else if (term instanceof KVariable) { return KVariable(((KVariable) term).name(), att); for (int i = 0; i < kseq.size(); i++) { K child = kseq.items().get(i); Sort childSort = sort(child, Sorts.KItem()); if (childSort.equals(Sorts.K())) { children.add(addInjections(child, Sorts.K())); } else { children.add(addInjections(child, Sorts.KItem())); } else if (term instanceof KAs) { KAs kas = (KAs)term; return KAs(addInjections(kas.pattern(), actualSort), kas.alias(), att); } else { throw KEMException.internalError("Invalid category of k found.", term);
public K addInjections(K term, Sort expectedSort) { Sort actualSort = sort(term, expectedSort); if (actualSort == null) { actualSort = expectedSort; return visitChildren(term, expectedSort, actualSort); } else if (expectedSort.equals(Sorts.K())) { if (actualSort.equals(Sorts.KItem())) { return KSequence(visitChildren(term, Sorts.KItem(), actualSort)); } else { return KSequence(KApply(KLabel("inj", actualSort, Sorts.KItem()), KList(visitChildren(term, Sorts.KItem(), actualSort)), Att.empty().add(Sort.class, Sorts.KItem()))); KApply k = (KApply)term; if (k.klabel().equals(wrappedLabel)) { if (collectionIsMap(collectionLabel)) { return KApply(elementLabel, KList(k.klist().items().get(0), visitChildren(k, expectedSort, actualSort)), Att.empty().add(Sort.class, expectedSort)); } else { return KApply(elementLabel, KList(visitChildren(k, expectedSort, actualSort)), Att.empty().add(Sort.class, expectedSort)); return KApply(KLabel("inj", actualSort, expectedSort), KList(visitChildren(term, expectedSort, actualSort)), Att.empty().add(Sort.class, expectedSort));
public K addInjections(K term) { if (new FoldK<Boolean>() { @Override public Boolean unit() { return false; } @Override public Boolean apply(KRewrite k) { return true; } @Override public Boolean merge(Boolean a, Boolean b) { return a || b; } }.apply(term)) { term = KRewrite(RewriteToTop.toLeft(term), RewriteToTop.toRight(term)); } Sort topSort = sort(term, Sorts.K()); K result = addInjections(term, topSort); return result; }
K transform(K term, boolean body) { AddSortInjections sorts = new AddSortInjections(module); return new RewriteAwareTransformer(body) { @Override public K apply(K k) { if (isRHS() && !isLHS()) { if (cache.containsKey(k)) { return cache.get(k); } } if (isLHS() && !isRHS()) { if (usedOnRhs.contains(k)) { return KAs(super.apply(k), cache.get(k), Att.empty().add(Sort.class, cache.get(k).att().get(Sort.class))); } } return super.apply(k); } }.apply(term); }
@Override public void apply(K k) { if (isLHS() && !isRHS() && !(k instanceof KVariable)) { cache.put(k, newDotVariable(sorts.sort(k, Sorts.K()))); } super.apply(k); }
void gatherTerms(K term, boolean body) { AddSortInjections sorts = new AddSortInjections(module); new RewriteAwareVisitor(body, new HashSet<>()) { @Override public void apply(K k) { if (isLHS() && !isRHS() && !(k instanceof KVariable)) { cache.put(k, newDotVariable(sorts.sort(k, Sorts.K()))); } super.apply(k); } @Override public void apply(KApply k) { String hook = module.attributesFor().get(k.klabel()).getOrElse(() -> Att.empty()).getOptional("hook").orElse(""); if (hook.equals("SET.element") || hook.equals("LIST.element") || hook.equals("LIST.concat") || hook.equals("MAP.concat") || hook.equals("SET.concat")) { return; } if (hook.equals("MAP.element")) { apply(k.items().get(1)); return; } super.apply(k); } }.apply(term); }
parsed = new AddSortInjections(compiledMod).addInjections(parsed, sort); converter.convert(parsed); System.out.println(converter.toString());
public static String getKompiledString(Module mainModule, KLabel topCellInitializer, FileUtil files, boolean heatCoolEquations) { mainModule = new GenerateSortPredicateRules(true).gen(mainModule); mainModule = ModuleTransformer.fromKTransformer(new AddSortInjections(mainModule)::addInjections, "Add sort injections").apply(mainModule); mainModule = ModuleTransformer.fromSentenceTransformer(new MinimizeTermConstruction(mainModule)::resolve, "Minimize term construction").apply(mainModule); ModuleToKORE moduleToKORE = new ModuleToKORE(mainModule, files, topCellInitializer); String kompiledString = moduleToKORE.convert(heatCoolEquations); Properties koreToKLabels = new Properties(); koreToKLabels.putAll(moduleToKORE.getKToKoreLabelMap().inverse()); try { FileOutputStream output = new FileOutputStream(files.resolveKompiled("kore_to_k_labels.properties")); koreToKLabels.store(output, "Properties file containing the mapping from kore to k labels"); } catch (IOException e) { throw KEMException.criticalError("Error while saving kore to K labels map", e); } return kompiledString; }