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
214 lines (191 loc) · 7.82 KB
/
ResolveContexts.java
File metadata and controls
214 lines (191 loc) · 7.82 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
// Copyright (c) 2015-2016 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.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.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(Sort("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 };
// Find a heated hole
// e.g., context ++(HOLE => lvalue(HOLE))
K heated = new VisitK() {
K heated;
KVariable holeVar;
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 (!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 (k.klabel() instanceof KVariable)
vars.put((KVariable) k.klabel(), InjectedKLabel(k.klabel()));
super.apply(k);
}
}.process(body);
K cooled = RewriteToTop.toLeft(body);
// TODO(dwightguth): generate freezers better for pretty-printing purposes
List<ProductionItem> items = new ArrayList<>();
KLabel freezerLabel;
if (cooled instanceof KApply) {
freezerLabel = getUniqueFreezerLabel(input, ((KApply)cooled).klabel().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(Sort("K")));
items.add(Terminal(","));
}
if (vars.size() > 0) {
items.remove(items.size() - 1);
}
items.add(Terminal(")"));
Production freezer = Production(freezerLabel.name(), Sorts.KItem(), immutable(items), Att());
K frozen = KApply(freezerLabel, vars.values().stream().collect(Collections.toList()));
return Stream.of(freezer,
Rule(KRewrite(cooled, KSequence(heated, frozen)), requiresHeat, BooleanUtils.TRUE, context.att().add("heat")),
Rule(KRewrite(KSequence(heated, frozen), cooled), requiresCool, BooleanUtils.TRUE, context.att().add("cool")));
}
/**
* 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);
}
}