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(); }
public AddEmptyLists(Module m) { this.m = m; subsorts = m.subsorts(); listSorts = m.listSorts(); lists = UserList.getLists(mutable(m.sentences())).stream().collect(Collectors.groupingBy(p -> p.sort)); }
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; }
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 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); } }
private void check(Production p) { if (p.att().contains("cell") && p.att().contains("stream")) { ProductionItem i = mutable(p.items()).get(1); if (i instanceof NonTerminal) { Sort sort = ((NonTerminal) i).sort(); if (!module.subsorts().lessThanEq(sort, Sorts.List())) { errors.add(KEMException.compilerError("Wrong sort in streaming cell. Expected List, but found " + sort.toString() + ".", p)); } } else { throw KEMException.internalError("Illegal arguments for stream cell."); } } } }
public void initialize() { disambModule.definedSorts(); disambModule.subsorts(); disambModule.priorities(); disambModule.leftAssoc(); disambModule.rightAssoc(); disambModule.productionsFor(); disambModule.overloads(); }
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("");
stream(mod.definedSorts()).filter(s -> mod.subsorts().lessThanEq(s, sort)).forEach(subsort -> { MutableBoolean isProtecting = new MutableBoolean(true); stream(mod.definedSorts()).filter(s -> mod.subsorts().lessThanEq(s, subsort)).forEach(candidateSort -> { if (predicateRules.stream().filter(r -> isPredicateFor(r, candidateSort)).findAny().isPresent()) { if (!mod.subsorts().lessThanEq(candidateProduction.nonterminal(i).sort(), possibleParentProduction.nonterminal(i).sort())) { stream(mod.productions()).filter(p -> mod.subsorts().lessThanEq(p.sort(), sort)).filter(p -> nonProtectingSubsorts.contains(p.sort())).distinct().forEach(p -> { if (p.klabel().isDefined() && !p.att().contains(Attribute.FUNCTION_KEY)) { List<K> klist = new ArrayList<>(); stream(mod.definedSorts()).filter(s -> mod.subsorts().lessThanEq(s, sort)).distinct().forEach(s -> { res.add(Rule(KRewrite(KApply(KLabel("is" + sort.toString()), KApply(KLabel("#KToken"), KToken(s.toString(), Sorts.KString()), KVariable("_"))), BooleanUtils.TRUE), BooleanUtils.TRUE,
public ProductionReference addBrackets(ProductionReference inner, TermCons outer, ProductionReference leftCapture, ProductionReference rightCapture) { if (requiresBracketWithSimpleAlgorithm(outer, leftCapture, rightCapture, inner)) { int position = getPosition(inner, outer); Sort outerSort = ((NonTerminal)outer.production().items().apply(position)).sort(); Sort innerSort = inner.production().sort(); for (Tuple2<Sort, scala.collection.immutable.List<Production>> sort : iterable(m.bracketProductionsFor())) { boolean isCorrectOuterSort = m.subsorts().lessThanEq(sort._1(), outerSort); if (isCorrectOuterSort) { for (Production p : mutable(sort._2())) { boolean isCorrectInnerSort = stream(p.items()) .filter(i -> i instanceof NonTerminal) .map(i -> (NonTerminal) i) .map(NonTerminal::sort) .filter(s -> m.subsorts().lessThanEq(innerSort, s)) .findAny().isPresent(); if (isCorrectInnerSort) { return TermCons.apply(ConsPStack.singleton(inner), p); } } } } return TermCons.apply(ConsPStack.singleton(inner), Production(Sorts.KBott(), Seq(NonTerminal(Sorts.K())))); } return inner; }
if (pattern.att().contains(Sort.class)) { Sort patternSort = pattern.att().get(Sort.class); if (sort(subject, r).stream().anyMatch(s -> mod.subsorts().lessThanEq(s, patternSort))) { subst.put((KVariable)pattern, subject); return true;
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; }
if (module.subsorts().lessThan(s, sort)) { numTerms++; sb.append("\\or{");
return new Tuple2<>(rez, warn); Term rez3 = new PushTopAmbiguityUp().apply(rez.right().get()); rez = new ApplyTypeCheckVisitor(disambModule.subsorts()).apply(rez3); if (rez.isLeft()) return new Tuple2<>(rez, warn); Tuple2<Either<Set<ParseFailedException>, Term>, Set<ParseFailedException>> rez2 = new VariableTypeInferenceFilter(disambModule.subsorts(), disambModule.definedSorts(), disambModule.productionsFor(), strict && inferSortChecks, true).apply(rez.right().get()); if (rez2._1().isLeft()) return rez2;
public static Term disambiguateForUnparse(Module mod, Term ambiguity) { Term rez3 = new PushTopAmbiguityUp().apply(ambiguity); Either<Set<ParseFailedException>, Term> rez = new ApplyTypeCheckVisitor(mod.subsorts()).apply(rez3); Tuple2<Either<Set<ParseFailedException>, Term>, Set<ParseFailedException>> rez2; if (rez.isLeft()) { rez2 = new AmbFilter(false).apply(rez3); return rez2._1().right().get(); } rez2 = new VariableTypeInferenceFilter(mod.subsorts(), mod.definedSorts(), mod.productionsFor(), false, false).apply(rez.right().get()); if (rez2._1().isLeft()) { rez2 = new AmbFilter(false).apply(rez.right().get()); return rez2._1().right().get(); } rez3 = new PushAmbiguitiesDownAndPreferAvoid(mod.overloads()).apply(rez2._1().right().get()); rez2 = new AmbFilter(false).apply(rez3); return rez2._1().right().get(); } }