forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddImplicitComputationCell.java
More file actions
92 lines (80 loc) · 3.35 KB
/
AddImplicitComputationCell.java
File metadata and controls
92 lines (80 loc) · 3.35 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
// Copyright (c) 2015-2016 K Team. All Rights Reserved.
package org.kframework.compile;
import org.kframework.compile.ConfigurationInfo;
import org.kframework.compile.ConfigurationInfoFromModule;
import org.kframework.compile.LabelInfo;
import org.kframework.compile.LabelInfoFromModule;
import org.kframework.definition.*;
import org.kframework.kil.Attribute;
import org.kframework.kore.K;
import org.kframework.kore.KApply;
import org.kframework.kore.KLabel;
import org.kframework.kore.KRewrite;
import java.util.List;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
/**
* If a SemanticSentence (Rule or Context) has a body that is not wrapped in any cell,
* wrap it in a {@code <k>} cell
*/
public class AddImplicitComputationCell implements UnaryOperator<Sentence> {
public static Definition transformDefinition(Definition input) {
ConfigurationInfoFromModule configInfo = new ConfigurationInfoFromModule(input.mainModule());
LabelInfo labelInfo = new LabelInfoFromModule(input.mainModule());
return DefinitionTransformer.fromSentenceTransformer(
new AddImplicitComputationCell(configInfo, labelInfo),
"concretizing configuration").apply(input);
}
public static Module transformModule(Module mod) {
ConfigurationInfoFromModule configInfo = new ConfigurationInfoFromModule(mod);
LabelInfo labelInfo = new LabelInfoFromModule(mod);
return ModuleTransformer.fromSentenceTransformer(
new AddImplicitComputationCell(configInfo, labelInfo),
"concretizing configuration").apply(mod);
}
private final ConfigurationInfo cfg;
private final LabelInfo labelInfo;
public AddImplicitComputationCell(ConfigurationInfo cfg, LabelInfo labelInfo) {
this.cfg = cfg;
this.labelInfo = labelInfo;
}
public Sentence apply(Sentence s) {
if (s.att().contains(Attribute.MACRO_KEY) || s.att().contains(Attribute.ANYWHERE_KEY)) {
return s;
}
if (s instanceof Rule) {
Rule rule = (Rule) s;
return new Rule(apply(rule.body()), rule.requires(), rule.ensures(), rule.att());
} else if (s instanceof Context) {
Context context = (Context) s;
return new Context(apply(context.body()), context.requires(), context.att());
} else {
return s;
}
}
private K apply(K term) {
if (labelInfo.isFunction(term)) return term;
List<K> items = IncompleteCellUtils.flattenCells(term);
if (items.size() != 1) {
return term;
}
K item = items.get(0);
if (isCell(item)) {
return term;
} else if (item instanceof KRewrite) {
final KRewrite rew = (KRewrite) item;
if (Stream.concat(
IncompleteCellUtils.flattenCells(rew.left()).stream(),
IncompleteCellUtils.flattenCells(rew.right()).stream())
.anyMatch(this::isCell)) {
return term;
}
}
KLabel computation = cfg.getCellLabel(cfg.getComputationCell());
return IncompleteCellUtils.make(computation, false, item, true);
}
private boolean isCell(K k) {
return k instanceof KApply
&& cfg.isCell(labelInfo.getCodomain(((KApply) k).klabel()));
}
}