/** Return the set of all known sorts which are a lower bound on * all sorts in {@code bounds}, leaving out internal sorts below "KBott" or above "K". */ private Set<Sort> lowerBounds(Collection<Sort> bounds) { Set<Sort> mins = new HashSet<>(); nextsort: for (Sort sort : iterable(sortSet)) { // for every declared sort // Sorts at or below KBott, or above K, are assumed to be // sorts from kast.k representing meta-syntax that is not a real sort. // This is done to prevent variables from being inferred as KBott or // as KList. if (subsorts.lessThanEq(sort, Sorts.KBott())) continue; if (subsorts.greaterThan(sort, Sorts.K())) continue; for (Sort bound : bounds) if (!subsorts.greaterThanEq(bound, sort)) continue nextsort; mins.add(sort); } return mins; }
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(); }
private Set<Sort> upperBounds(Collection<Sort> bounds) { Set<Sort> maxs = new HashSet<>(); nextsort: for (Sort sort : iterable(mod.definedSorts())) { // for every declared sort // Sorts at or below KBott, or above K, are assumed to be // sorts from kast.k representing meta-syntax that is not a real sort. // This is done to prevent variables from being inferred as KBott or // as KList. if (mod.subsorts().lessThanEq(sort, Sorts.KBott())) continue; if (mod.subsorts().greaterThan(sort, Sorts.K())) continue; for (Sort bound : bounds) if (!mod.subsorts().lessThanEq(bound, sort)) continue nextsort; maxs.add(sort); } return maxs; }
Sort childSort = getSort(child); if (listSorts.contains(expectedSort) && !(subsorts.lessThanEq(childSort, expectedSort) && listSorts.contains(childSort))) { final boolean isBracket = child.production().att().contains("bracket"); if (isBracket && child.production().klabel().get().name().equals("#KRewrite"))) { newItems.add(child); } else if (childSort.equals(Sorts.K()) || !subsorts.lessThan(childSort, expectedSort)) { String msg = "Found sort '" + childSort + "' where list sort '" + expectedSort + "' was expected. Moving on."; warnings.add(new ParseFailedException( newItems.add(child); } else { Set<Sort> least = subsorts.minimal(stream(listSorts).filter(s -> subsorts.greaterThanEq(lists.get(s).get(0).childSort, childSort) && subsorts.lessThanEq(s, expectedSort)).collect(Collectors.toList())); if (least.size() != 1) { KException ex = new KException(KException.ExceptionType.ERROR, KException.KExceptionGroup.INNER_PARSER, "Overloaded term does not have a least sort. Possible sorts: " + least, tc.source().orElse(null), tc.location().orElse(null)); Set<Sort> leastTerm = subsorts.minimal(stream(listSorts).filter(s -> subsorts.lessThanEq(s, expectedSort)).collect(Collectors.toList())); if (leastTerm.size() != 1) { KException ex = new KException(KException.ExceptionType.ERROR, KException.KExceptionGroup.INNER_PARSER, "List terminator for overloaded term does not have a least sort. Possible sorts: " + leastTerm, tc.source().orElse(null), tc.location().orElse(null));
@Override public Either<Set<ParseFailedException>, Term> apply(TermCons tc) { if (overloads.elements().contains(tc.production()) && tc.items().isEmpty()) { Set<Production> candidates = stream(overloads.elements()).filter(p -> p.klabel().isDefined() && p.klabelAtt().equals(tc.production().klabelAtt()) && overloads.lessThanEq(p, tc.production())).collect(Collectors.toSet()); candidates = overloads.minimal(candidates); if (candidates.size() != 1) { KException ex = new KException(KException.ExceptionType.ERROR, KException.KExceptionGroup.INNER_PARSER, "Overloaded term does not have a least sort. Possible sorts: " + candidates, tc.source().orElse(null), tc.location().orElse(null)); return Left.apply(Sets.newHashSet(new ParseFailedException(ex))); } Production prod = candidates.iterator().next(); prod = Production(prod.klabel(), prod.sort(), prod.items(), prod.att().add("originalPrd", Production.class, tc.production())); return super.apply(TermCons.apply(tc.items(), prod, tc.location(), tc.source())); } return super.apply(tc); } }
for (Production lesser : iterable(module.overloads().elements())) { for (Production greater : iterable(module.overloads().relations().get(lesser).getOrElse(() -> Collections.<Production>Set()))) { overloads.add(greater); if (module.subsorts().lessThan(s, sort)) { numTerms++; sb.append("\\or{"); for (Production lesser : iterable(module.overloads().elements())) { for (Production greater : iterable(module.overloads().relations().get(lesser).getOrElse(() -> Collections.<Production>Set()))) { sb.append(" axiom{R} \\equals{"); convert(greater.sort(), greater);
/** * Returns an element of sorts which is related to and no * greater than every other element of sorts, if any exists. */ private Optional<Sort> least(Iterable<Sort> sorts) { Iterator<Sort> iter = sorts.iterator(); if (!iter.hasNext()) { return Optional.empty(); } Sort min = iter.next(); while (iter.hasNext()) { Sort next = iter.next(); if (!subsorts.lessThanEq(min, next)) { // if min is unrelated to next, it can't be the least sort so // we also might as well switch min = next; } } for (Sort s : sorts) { if (!subsorts.lessThanEq(min, s)) { return Optional.empty(); } } return Optional.of(min); }
/** * Returns an element of sorts which is related to and no * less than every other element of sorts, if any exists. */ private Optional<Sort> greatest(Iterable<Sort> sorts) { Iterator<Sort> iter = sorts.iterator(); if (!iter.hasNext()) { return Optional.empty(); } Sort max = iter.next(); while (iter.hasNext()) { Sort next = iter.next(); if (!subsorts.greaterThanEq(max, next)) { // if min is unrelated to next, it can't be the least sort so // we also might as well switch max = next; } } for (Sort s : sorts) { if (!subsorts.greaterThanEq(max, s)) { return Optional.empty(); } } return Optional.of(max); }
private boolean isPriorityWrong(ProductionReference outer, ProductionReference inner, int position) { if (outer.production().klabel().isEmpty() || inner.production().klabel().isEmpty()) { return false; } Tag parentLabel = new Tag(outer.production().klabel().get().name()); Tag localLabel = new Tag(inner.production().klabel().get().name()); if (!m.subsorts().lessThanEq(inner.production().sort(), ((NonTerminal)outer.production().items().apply(position)).sort())) { return true; } if (m.priorities().lessThan(parentLabel, localLabel)) { return true; } if (m.leftAssoc().contains(new Tuple2<>(parentLabel, localLabel)) && position == outer.production().items().size() - 1) { return true; } if (m.rightAssoc().contains(new Tuple2<>(parentLabel, localLabel)) && position == 0) { return true; } return false; }
public Subsorts(Module module) { sorts = JavaConversions.asJavaCollection(module.definedSorts()).stream() .map(Sort::of) .collect(Collectors.toSet()); this.subsort = ArrayTable.create(sorts, sorts); for (org.kframework.kore.Sort sort1 : Collections.iterable(module.definedSorts())) { for (org.kframework.kore.Sort sort2 : Collections.iterable(module.definedSorts())) { subsort.put( Sort.of(sort1), Sort.of(sort2), module.subsorts().$greater(sort1, sort2)); } } }
private boolean equalsSyntax(Module _this, Module that) { if (!_this.productions().equals(that.productions())) return false; if (!_this.priorities().equals(that.priorities())) return false; if (!_this.leftAssoc().equals(that.leftAssoc())) return false; if (!_this.rightAssoc().equals(that.rightAssoc())) return false; return _this.sortDeclarations().equals(that.sortDeclarations()); }
private Set<Sort> sort(K k, Rule r) { if (k instanceof KVariable) { return Collections.singleton(k.att().get(Sort.class)); } else if (k instanceof KToken) { return Collections.singleton(((KToken)k).sort()); } else if (k instanceof KApply) { KApply kapp = (KApply)k; if (kapp.klabel() instanceof KVariable) { throw KEMException.compilerError("Cannot compute macros with klabel variables.", r); } Set<Production> prods = new HashSet<>(mutable(mod.productionsFor().apply(kapp.klabel()))); prods.removeIf(p -> p.arity() != kapp.items().size()); for (int i = 0; i < kapp.items().size(); i++) { final int idx = i; Set<Sort> sorts = sort(kapp.items().get(idx), r); prods.removeIf(p -> sorts.stream().noneMatch(s -> mod.subsorts().lessThanEq(s, p.nonterminal(idx).sort()))); } Set<Sort> candidates = prods.stream().map(Production::sort).collect(Collectors.toSet()); return candidates; } else { throw KEMException.compilerError("Cannot compute macros with sort check on terms that are not KApply, KToken, or KVariable.", r); } }
stream(mainModule.definedSorts()).filter(s -> mainModule.subsorts().greaterThanEq(predicateSort, s)).distinct() .filter(sort -> mainModule.sortAttributesFor().contains(sort)).forEach(sort -> { String sortHook = mainModule.sortAttributesFor().apply(sort).<String>getOptional("hook").orElse("");
if (!subsorts.lessThanEq(sortIter.next(), ((NonTerminal) item).sort())) {
private boolean isPriorityWrong(ProductionReference outer, ProductionReference inner) { Tag parentLabel = new Tag(outer.production().klabel().get().name()); Tag localLabel = new Tag(inner.production().klabel().get().name()); if (m.priorities().lessThan(parentLabel, localLabel)) { return true; } if (m.leftAssoc().contains(new Tuple2<>(parentLabel, localLabel))) { return true; } if (m.rightAssoc().contains(new Tuple2<>(parentLabel, localLabel))) { return true; } return false; }
public Term apply(Ambiguity a) { Set<Production> productions = new HashSet<>(); for (Term t : a.items()) { if (t instanceof TermCons) { TermCons tc = (TermCons)t; productions.add(tc.production()); } else { return a; } } Set<Production> candidates = overloads.minimal(productions); Ambiguity result = Ambiguity.apply(a.items().stream().filter(t -> candidates.contains(((ProductionReference)t).production())).collect(Collectors.toSet())); if (result.items().size() == 1) { return result.items().iterator().next(); } return result; } }
public Either<java.util.Set<ParseFailedException>, Term> apply(Constant c) { if (c.production().sort().equals(Sorts.KVariable())) { Sort declared = decl.get(getVarKey(c)); if (declared != null && !(declared.equals(Sorts.K()) && subsorts.lessThanEq(sort, Sorts.KList()))) { // if the declared/inferred sort is K, make sure it can fit in the context (ex. is not a KLabel) if ((!strictSortEquality && !subsorts.lessThanEq(declared, sort)) || (strictSortEquality && !declared.equals(sort))) { String msg = "Unexpected sort " + declared + " for term " + c.value() + ". Expected " + sort + "."; KException kex = new KException(KException.ExceptionType.ERROR, KException.KExceptionGroup.CRITICAL, msg, c.source().orElse(null), c.location().orElse(null)); return Left.apply(Sets.newHashSet(new VariableTypeClashException(kex))); } return wrapTermWithCast(c, declared); } } return Right.apply(c); }
public Either<java.util.Set<ParseFailedException>, Term> apply(TermCons tc) { if (tc.production().att().contains("bracket")) return Right.apply(tc); //if (Side.RIGHT == side && !(tc.production().items().apply(0) instanceof NonTerminal)) return Right.apply(tc); //if (Side.LEFT == side && !(tc.production().items().apply(tc.production().items().size() - 1) instanceof NonTerminal)) return Right.apply(tc); Tag parentLabel = new Tag(parent.production().klabel().get().name()); Tag localLabel = new Tag(tc.production().klabel().get().name()); if (priorities.lessThan(parentLabel, localLabel)) { String msg = "Priority filter exception. Cannot use " + localLabel + " as a child of " + parentLabel; KException kex = new KException(KException.ExceptionType.ERROR, KException.KExceptionGroup.CRITICAL, msg, tc.source().get(), tc.location().get()); return Left.apply(Sets.newHashSet(new PriorityException(kex))); } if (leftAssoc.contains(new Tuple2<>(parentLabel, localLabel)) && Side.RIGHT == side) { String msg = "Associativity filter exception. Cannot use " + localLabel + " as a right child of " + parentLabel; KException kex = new KException(KException.ExceptionType.ERROR, KException.KExceptionGroup.CRITICAL, msg, tc.source().get(), tc.location().get()); return Left.apply(Sets.newHashSet(new PriorityException(kex))); } if (rigthAssoc.contains(new Tuple2<>(parentLabel, localLabel)) && Side.LEFT == side) { String msg = "Associativity filter exception. Cannot use " + localLabel + " as a left child of " + parentLabel; KException kex = new KException(KException.ExceptionType.ERROR, KException.KExceptionGroup.CRITICAL, msg, tc.source().get(), tc.location().get()); return Left.apply(Sets.newHashSet(new PriorityException(kex))); } return Right.apply(tc); } }