better fix for sort inference
// Copyright (c) 2019 K Team. All Rights Reserved.
package org.kframework.parser.inner.disambiguation;
+import org.kframework.POSet;
import org.kframework.attributes.Att;
import org.kframework.Collections;
import org.kframework.TopologicalSort;
@@ -130,27 +131,39 @@ public class TypeInferencer implements AutoCloseable {
* @param mod
*/
private void push(Module mod) {
- int i = 0;
- for (Sort s : iterable(TopologicalSort.tsort(mod.syntacticSubsorts().directRelations()))) {
- if (!isRealSort(s.head())) {
- continue;
- }
- ordinals.put(s.head(), i++);
- }
-
// declare Sort datatype
print("(declare-datatypes () ((Sort ");
for (SortHead s : sorts) {
print("(|Sort" + s.name() + "| ");
- for (i = 0; i < s.params(); i++) {
+ for (int i = 0; i < s.params(); i++) {
print("(|Sort" + s.name() + "_" + i++ + "| Sort) ");
}
println(")");
}
println(")))");
+ makeSubsorts(mod, "<=Sort", mod.subsorts());
+ makeSubsorts(mod, "<=SortSyntax", mod.syntacticSubsorts());
+
+ if (DEBUG) {
+ debugPrelude = sb.toString();
+ }
+ }
+
+ private void makeSubsorts(Module mod, String name, POSet<Sort> relations) {
+ // map from each sort to an integer representing the topological sorting of the sorts. higher numbers mean greater
+ // sorts
+ Map<SortHead, Integer> ordinals = new HashMap<>();
+ int i = 0;
+
+ for (Sort s : iterable(TopologicalSort.tsort(relations.directRelations()))) {
+ if (!isRealSort(s.head())) {
+ continue;
+ }
+ ordinals.put(s.head(), i++);
+ }
// provide fixed interpretation of subsort relation
- println("(define-fun <=Sort ((s1 Sort) (s2 Sort)) Bool (or");
- for (Tuple2<Sort, Set<Sort>> relation : stream(mod.syntacticSubsorts().relations()).sorted(Comparator.comparing(t -> -ordinals.getOrDefault(t._1().head(), 0))).collect(Collectors.toList())) {
+ println("(define-fun " + name + " ((s1 Sort) (s2 Sort)) Bool (or");
+ for (Tuple2<Sort, Set<Sort>> relation : stream(relations.relations()).sorted(Comparator.comparing(t -> -ordinals.getOrDefault(t._1().head(), 0))).collect(Collectors.toList())) {
if (!isRealSort(relation._1().head())) {
continue;
}
@@ -177,17 +190,10 @@ public class TypeInferencer implements AutoCloseable {
println("))");
}
println("))");
- if (DEBUG) {
- debugPrelude = sb.toString();
- }
}
private String debugPrelude;
- // map from each sort to an integer representing the topological sorting of the sorts. higher numbers mean greater
- // sorts
- private final Map<SortHead, Integer> ordinals = new HashMap<>();
-
// list of names for variables and sort parameters in z3
private final List<String> variables = new ArrayList<>();
// list of names for sort parameters only in z3
@@ -832,7 +838,7 @@ public class TypeInferencer implements AutoCloseable {
java.util.Set<String> realVariables = new HashSet<>(variables);
realVariables.removeAll(parameters);
for (String var : realVariables) {
- print("(<=Sort |" + var + "| ");
+ print("(<=SortSyntax |" + var + "| ");
printSort(model.get(var));
print(") ");
}
@@ -908,7 +914,6 @@ public class TypeInferencer implements AutoCloseable {
parameters.clear();
variablesById.clear();
cacheById.clear();
- ordinals.clear();
nextId = 0;
if (destroyOnReset) {
z3.close();
