forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResolveContexts.java
More file actions
292 lines (264 loc) · 10.5 KB
/
ResolveContexts.java
File metadata and controls
292 lines (264 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (c) 2015-2019 K Team. All Rights Reserved.
package org.kframework.compile;
import org.kframework.Collections;
import org.kframework.builtin.BooleanUtils;
import org.kframework.builtin.Sorts;
import org.kframework.definition.Context;
import org.kframework.definition.Definition;
import org.kframework.definition.Module;
import org.kframework.definition.Production;
import org.kframework.definition.ProductionItem;
import org.kframework.definition.Sentence;
import org.kframework.kompile.KompileOptions;
import org.kframework.kore.FindK;
import org.kframework.kore.FoldK;
import org.kframework.kore.K;
import org.kframework.kore.KApply;
import org.kframework.kore.KLabel;
import org.kframework.kore.KRewrite;
import org.kframework.kore.KVariable;
import org.kframework.kore.VisitK;
import org.kframework.kore.TransformK;
import org.kframework.utils.errorsystem.KEMException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.kframework.Collections.*;
import static org.kframework.definition.Constructors.*;
import static org.kframework.kore.KORE.*;
public class ResolveContexts {
private final KompileOptions kompileOptions;
public ResolveContexts(KompileOptions kompileOptions) {
this.kompileOptions = kompileOptions;
}
public Definition resolve(Definition d) {
klabels = new HashSet<>();
Module transformedMainModule = resolve(d.mainModule());
return Definition.apply(transformedMainModule, add(transformedMainModule, minus(d.mainModule(), d.entryModules())), d.att());
}
public Module resolve(Module input) {
Set<Sentence> rulesToAdd = stream(input.sentences())
.filter(s -> s instanceof Context)
.map(s -> (Context) s)
.flatMap(c -> this.resolve(c, input)).collect(Collectors.toCollection(HashSet::new));
if (!rulesToAdd.isEmpty()) {
rulesToAdd.add(SyntaxSort(Seq(), Sorts.K()));
}
return Module(input.name(), input.imports(), (scala.collection.Set<Sentence>) stream(input.localSentences()).filter(s -> !(s instanceof Context)).collect(Collections.toSet()).$bar(immutable(rulesToAdd)), input.att());
}
private Set<KLabel> klabels;
private KLabel getUniqueFreezerLabel(Module input, String nameHint) {
if (klabels.isEmpty()) {
klabels.addAll(mutable(input.definedKLabels()));
}
int counter = 0;
KLabel freezer;
do {
freezer = KLabel("#freezer" + nameHint + "_" + (counter++ == 0 ? "" : counter));
} while (klabels.contains(freezer));
klabels.add(freezer);
return freezer;
}
private Stream<? extends Sentence> resolve(Context context, Module input) {
checkContextValidity(context);
final SortedMap<KVariable, K> vars = new TreeMap<>((v1, v2) -> v1.name().compareTo(v2.name()));
K body = context.body();
K requiresHeat = context.requires();
K requiresCool = BooleanUtils.TRUE;
int currentHolePosition[] = new int[] { 0 };
int finalHolePosition[] = new int[] { 0 };
// does this context have a main cell?
boolean hasMainCell = new FoldK<Boolean>() {
@Override
public Boolean apply(KApply k) {
if (input.attributesFor().getOrElse(k.klabel(), () -> Att()).contains("maincell")) {
return true;
}
return super.apply(k);
}
@Override
public Boolean unit() {
return false;
}
@Override
public Boolean merge(Boolean a, Boolean b) {
return a || b;
}
}.apply(body);
// Find a heated hole
// e.g., context ++(HOLE => lvalue(HOLE))
K heated = new VisitK() {
K heated;
KVariable holeVar;
boolean inMainCell = false;
public K process(K k) {
apply(k);
if (heated != null)
return heated;
else
return holeVar;
}
@Override
public void apply(KRewrite k) {
heated = k.right();
super.apply(k);
}
@Override
public void apply(KVariable k) {
if (inMainCell || !hasMainCell) {
if (!k.name().equals("HOLE")) {
vars.put(k, k);
finalHolePosition[0] = currentHolePosition[0];
} else {
holeVar = k;
currentHolePosition[0]++;
}
}
super.apply(k);
}
@Override
public void apply(KApply k) {
if (input.attributesFor().getOrElse(k.klabel(), () -> Att()).contains("maincell")) {
inMainCell = true;
}
if (k.klabel() instanceof KVariable && (inMainCell || !hasMainCell))
vars.put((KVariable) k.klabel(), InjectedKLabel(k.klabel()));
super.apply(k);
if (input.attributesFor().getOrElse(k.klabel(), () -> Att()).contains("maincell")) {
inMainCell = false;
}
}
}.process(body);
K cooled = new VisitK() {
K cooled;
public K process(K k) {
apply(k);
if (cooled != null)
return cooled;
else
return k;
}
@Override
public void apply(KApply k) {
if (input.attributesFor().getOrElse(k.klabel(), () -> Att()).contains("maincell")) {
cooled = k.items().get(1);
}
super.apply(k);
}
}.process(RewriteToTop.toLeft(body));
// TODO(dwightguth): generate freezers better for pretty-printing purposes
List<ProductionItem> items = new ArrayList<>();
KLabel freezerLabel;
if (cooled instanceof KApply) {
KApply kApply = (KApply)cooled;
String name = kApply.klabel().name();
if (name.equals("#SemanticCastToK")) {
K firstArg = kApply.klist().items().get(0);
if (firstArg instanceof KApply)
name = ((KApply)firstArg).klabel().name();
}
freezerLabel = getUniqueFreezerLabel(input, name + finalHolePosition[0]);
} else {
freezerLabel = getUniqueFreezerLabel(input, "");
}
items.add(Terminal(freezerLabel.name()));
items.add(Terminal("("));
for (int i = 0; i < vars.size(); i++) {
items.add(NonTerminal(Sorts.K()));
items.add(Terminal(","));
}
if (vars.size() > 0) {
items.remove(items.size() - 1);
}
items.add(Terminal(")"));
Production freezer = Production(freezerLabel, Sorts.KItem(), immutable(items), Att());
K frozen = KApply(freezerLabel, vars.values().stream().collect(Collections.toList()));
return Stream.of(freezer,
Rule(insert(body, KRewrite(cooled, KSequence(heated, frozen)), input), requiresHeat, BooleanUtils.TRUE, context.att().add("heat")),
Rule(insert(body, KRewrite(KSequence(heated, frozen), cooled), input), requiresCool, BooleanUtils.TRUE, context.att().add("cool")));
}
private K insert(K body, K rewrite, Module mod) {
class Holder {
boolean found = false;
}
Holder h = new Holder();
K inserted = new TransformK() {
@Override
public K apply(KApply k) {
if (mod.attributesFor().getOrElse(k.klabel(), () -> Att()).contains("maincell")) {
h.found = true;
return KApply(k.klabel(), k.items().get(0), rewrite, k.items().get(2));
}
return super.apply(k);
}
}.apply(body);
if (h.found) {
return inserted;
} else {
return rewrite;
}
}
/**
* Check validity of context.
* <p>
* Currently the following conditions are checked:
* - Contexts must have at least one HOLE.
* - Contexts must have a single rewrite.
* - Only the HOLE can be rewritten in a context definition.
*
* @param context to be checked
*/
public static void checkContextValidity(Context context) {
K body = context.body();
int cntHoles = new FindK() {
@Override
public scala.collection.Set<K> apply(KVariable k) {
if (k.name().equals("HOLE")) {
return org.kframework.Collections.Set(k);
} else {
return super.apply(k);
}
}
}.apply(body).size();
if (cntHoles < 1) {
throw KEMException.compilerError("Contexts must have at least one HOLE.", context);
}
int cntRewrites = new FindK() {
@Override
public scala.collection.Set<K> apply(KRewrite k) {
return this.merge(org.kframework.Collections.Set(k), super.apply(k));
}
}.apply(body).size();
if (cntRewrites > 1) {
throw KEMException.compilerError("Cannot compile a context with multiple rewrites.", context);
}
new VisitK() {
@Override
public void apply(KRewrite k) {
if (!isHOLE(k.left())) {
throw KEMException.compilerError("Only the HOLE can be rewritten in a context definition", context);
}
super.apply(k);
}
// return true when k is either HOLE or #SemanticCastToX(HOLE)
private boolean isHOLE(K k) {
if (k instanceof KApply) {
KApply kapp = (KApply) k;
return kapp.klabel().name().startsWith("#SemanticCastTo") &&
kapp.klist().size() == 1 &&
isHOLEVar(kapp.klist().items().get(0));
} else {
return isHOLEVar(k);
}
}
private boolean isHOLEVar(K k) {
return k instanceof KVariable && ((KVariable) k).name().equals("HOLE");
}
}.apply(body);
}
}