Skip to content

Commit

Permalink
Add support for strict casts
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott-Guest committed Dec 17, 2023
1 parent a9bac14 commit 23aa1e7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public BoundedSort sortToBoundedSort(Sort sort, ProductionReference prOrNull) {
return new BoundedSort.Constructor(sort.head());
}

public BoundedSort returnSort(ProductionReference pr) throws ConstraintError {
BoundedSort resSort = new BoundedSort.Variable();
constrain(sortToBoundedSort(pr.production().sort(), pr), resSort, pr);
return resSort;
}

/**
* Update sub-/super-type constraints to record the fact that lhs <: rhs.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.kframework.definition.Module;
import org.kframework.definition.NonTerminal;
import org.kframework.definition.Production;
import org.kframework.kore.KLabel;
import org.kframework.kore.Sort;
import org.kframework.kore.SortHead;
import org.kframework.parser.Ambiguity;
Expand Down Expand Up @@ -88,10 +87,10 @@ public SortInferencer(Module mod) {

/**
* Determine whether a Term is supported by the current SortInferencer algorithm. Supported terms
* can contain neither ambiguities, strict casts, nor parametric sorts.
* can contain neither ambiguities nor parametric sorts.
*/
public static boolean isSupported(Term t) {
return !hasAmbiguity(t) && !hasStrictCast(t) && !hasParametricSorts(t);
return !hasAmbiguity(t) && !hasParametricSorts(t);
}

private static boolean hasAmbiguity(Term t) {
Expand All @@ -104,24 +103,6 @@ private static boolean hasAmbiguity(Term t) {
return ((TermCons) t).items().stream().anyMatch(SortInferencer::hasAmbiguity);
}

private static boolean hasStrictCast(Term t) {
if (t instanceof Ambiguity amb) {
return amb.items().stream().anyMatch(SortInferencer::hasStrictCast);
}
ProductionReference pr = (ProductionReference) t;
if (pr.production().klabel().isDefined()) {
KLabel klabel = pr.production().klabel().get();
String label = klabel.name();
if (label.equals("#SyntacticCast") || label.equals("#SyntacticCastBraced")) {
return true;
}
}
if (t instanceof Constant) {
return false;
}
return ((TermCons) t).items().stream().anyMatch(SortInferencer::hasStrictCast);
}

private static boolean hasParametricSorts(Term t) {
if (t instanceof Ambiguity amb) {
return amb.items().stream().anyMatch(SortInferencer::hasParametricSorts);
Expand Down Expand Up @@ -271,23 +252,30 @@ private BoundedSort infer(Term t, boolean isAnywhereRule, InferenceDriver driver
BoundedSort rewriteParam = driver.sortToBoundedSort(tc.production().sort(), tc);
driver.constrain(lhsSort, rewriteParam, tc);
driver.constrain(rhsSort, rewriteParam, tc);
BoundedSort resSort = new BoundedSort.Variable();
driver.constrain(rewriteParam, resSort, tc);
return resSort;
return driver.returnSort(tc);
}

if (tc.production()
.klabel()
.filter(k -> k.name().equals("#SyntacticCast") || k.name().equals("#SyntacticCastBraced"))
.isDefined()) {
BoundedSort castedSort = driver.sortToBoundedSort(tc.production().sort(), tc);
BoundedSort childSort = infer(tc.get(0), isAnywhereRule, driver);
driver.constrain(castedSort, childSort, tc);
driver.constrain(childSort, castedSort, tc);
return driver.returnSort(tc);
}

for (int prodI = 0, tcI = 0; prodI < tc.production().items().size(); prodI++) {
if (!(tc.production().items().apply(prodI) instanceof NonTerminal nt)) {
continue;
}
BoundedSort expectedSort = driver.sortToBoundedSort(nt.sort(), pr);
BoundedSort expectedSort = driver.sortToBoundedSort(nt.sort(), tc);
BoundedSort childSort = infer(tc.get(tcI), isAnywhereRule, driver);
driver.constrain(childSort, expectedSort, pr);
driver.constrain(childSort, expectedSort, tc);
tcI++;
}
BoundedSort resSort = new BoundedSort.Variable();
driver.constrain(driver.sortToBoundedSort(tc.production().sort(), pr), resSort, pr);
return resSort;
return driver.returnSort(tc);
}

/**
Expand Down

0 comments on commit 23aa1e7

Please sign in to comment.