Skip to content

Commit f8790c5

Browse files
committed
Initial version of sort-contains
1 parent f30e11b commit f8790c5

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

include/kllvm/ast/AST.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,9 +1040,9 @@ class KOREDefinition {
10401040
*
10411041
* The relation will contain an entry:
10421042
*
1043-
* S_R |-> { S_1, S'_1, ..., S_N, T_1, ..., T_N }
1043+
* S_R |-> { S_R, S'_R, S_1, S'_1, ..., S_N, T_1, ..., T_N }
10441044
*
1045-
* where S'_1 is a _subsort_ of S_1 (that is, given that the first argument of
1045+
* where S'_X is a _subsort_ of S_X (that is, given that the first argument of
10461046
* `c` is of sort S_1, a term whose sort is a subsort of S_1 may also occur in
10471047
* the first argument position).
10481048
*/

include/kllvm/util/transitive_closure.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ Map transitive_closure(Map relations) {
2929
return relations;
3030
}
3131

32+
template <typename Map>
33+
Map reflexive_closure(Map relations) {
34+
for (auto &[from, to_set] : relations) {
35+
to_set.insert(from);
36+
}
37+
38+
return relations;
39+
}
40+
3241
} // namespace kllvm
3342

3443
#endif

lib/ast/definition.cpp

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <kllvm/parser/KOREParser.h>
44
#include <kllvm/util/transitive_closure.h>
55

6+
#include <iostream>
67
#include <string>
78
#include <unordered_set>
89

@@ -104,7 +105,53 @@ void KOREDefinition::buildOverloadRelation() {
104105
overloads = transitive_closure(overloads);
105106
}
106107

107-
void KOREDefinition::buildSortContainsRelation() { }
108+
void KOREDefinition::buildSortContainsRelation() {
109+
auto const &supersorts = getSupersorts();
110+
111+
for (auto const &[name, decl] : getSymbolDeclarations()) {
112+
auto const &atts = decl->getAttributes();
113+
if (atts.find("function") != atts.end()) {
114+
// TODO: exclude collection sorts
115+
continue;
116+
}
117+
118+
auto *symbol = decl->getSymbol();
119+
auto const &return_sort
120+
= std::dynamic_pointer_cast<KORECompositeSort>(symbol->getSort());
121+
if (!return_sort) {
122+
continue;
123+
}
124+
125+
auto &children = sortContains[return_sort.get()];
126+
children.insert(return_sort.get());
127+
128+
for (auto const &arg : symbol->getArguments()) {
129+
children.insert(arg.get());
130+
}
131+
132+
auto subsorts = std::vector<KORESort *>{};
133+
for (auto const &child_sort : children) {
134+
if (supersorts.find(child_sort) != supersorts.end()) {
135+
for (auto const &child_subsort : supersorts.at(child_sort)) {
136+
subsorts.push_back(child_subsort);
137+
}
138+
}
139+
}
140+
141+
std::copy(
142+
subsorts.begin(), subsorts.end(),
143+
std::inserter(children, children.begin()));
144+
}
145+
146+
sortContains = transitive_closure(sortContains);
147+
148+
// Hooked sorts produce empty relations in the transitive closure because they
149+
// cannot occur as the return sort of a constructor symbol, and cannot be
150+
// subsorted. However, we want the relation to encode that a term of hooked
151+
// sort H _can_ contain a term of hooked sort H, and so we take the reflexive
152+
// closure of the transitive closure to ensure this.
153+
sortContains = reflexive_closure(sortContains);
154+
}
108155

109156
// NOLINTNEXTLINE(*-function-cognitive-complexity)
110157
void KOREDefinition::preprocess() {

0 commit comments

Comments
 (0)