forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeconstructIntegerAndFloatLiterals.java
More file actions
171 lines (150 loc) · 4.82 KB
/
DeconstructIntegerAndFloatLiterals.java
File metadata and controls
171 lines (150 loc) · 4.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
// Copyright (c) 2015-2016 K Team. All Rights Reserved.
package org.kframework.compile;
import org.kframework.builtin.BooleanUtils;
import org.kframework.builtin.Sorts;
import org.kframework.definition.Context;
import org.kframework.definition.Rule;
import org.kframework.definition.Sentence;
import org.kframework.kil.Attribute;
import org.kframework.kore.*;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import static org.kframework.definition.Constructors.*;
import static org.kframework.kore.KORE.*;
/**
* Transforms patterns in the LHS of rules which have tokens of sort Int or Float
* into side conditions generating equality over a reconstructed value.
* Thus,
*
* rule 5 => .K
*
* becomes
*
* rule I:Int => .K when I ==K 5
*/
public class DeconstructIntegerAndFloatLiterals {
private Set<KApply> state = new HashSet<>();
private Set<KVariable> vars = new HashSet<>();
void reset() {
state.clear();
vars.clear();
}
void gatherVars(K term) {
new VisitK() {
@Override
public void apply(KVariable v) {
vars.add(v);
super.apply(v);
}
}.apply(term);
}
public Sentence convert(Sentence s) {
if (s.att().contains(Attribute.MACRO_KEY)) {
return s;
}
if (s instanceof Rule) {
return convert((Rule) s);
} else if (s instanceof Context) {
return convert((Context) s);
} else {
return s;
}
}
private Rule convert(Rule rule) {
reset();
gatherVars(rule.body());
gatherVars(rule.requires());
gatherVars(rule.ensures());
K body = convert(rule.body());
K requires = convertLookups(rule.requires());
return Rule(
body,
addSideCondition(requires),
rule.ensures(),
rule.att());
}
private K convertLookups(K requires) {
return new Transformer(false).apply(requires);
}
private Context convert(Context context) {
reset();
gatherVars(context.body());
gatherVars(context.requires());
K body = convert(context.body());
return Context(
body,
addSideCondition(context.requires()),
context.att());
}
private int counter = 0;
KVariable newDotVariable(Sort sort) {
KVariable newLabel;
do {
newLabel = KVariable("_" + (counter++), Att().add("sort", sort.name()));
} while (vars.contains(newLabel));
vars.add(newLabel);
return newLabel;
}
K addSideCondition(K requires) {
Optional<KApply> sideCondition = state.stream().reduce(BooleanUtils::and);
if (!sideCondition.isPresent()) {
return requires;
} else if (requires.equals(BooleanUtils.TRUE) && sideCondition.isPresent()) {
return sideCondition.get();
} else {
return BooleanUtils.and(requires, sideCondition.get());
}
}
private K convert(K term) {
return new Transformer(true).apply(term);
}
private class Transformer extends TransformK {
@Override
public K apply(KToken k) {
if (lhs) {
if (k.sort().equals(Sorts.Int()) || k.sort().equals(Sorts.Float())) {
KVariable var = newDotVariable(k.sort());
state.add(KApply(KLabel("_==" + k.sort().name() + "_"), var, k));
return var;
}
}
return super.apply(k);
}
private boolean lhs;
public Transformer(boolean lhs) {
this.lhs = lhs;
}
public boolean isLookupKLabel(KLabel k) {
return k.name().equals("#match") || k.name().equals("#mapChoice") || k.name().equals("#filterMapChoice") || k.name().equals("#setChoice");
}
@Override
public K apply(KApply k) {
if (isLookupKLabel(k.klabel())) {
assert k.klist().size() == 2;
K r = apply(k.klist().items().get(1));
lhs = true;
K l = apply(k.klist().items().get(0));
lhs = false;
if (l != k.klist().items().get(0) || r != k.klist().items().get(1)) {
return KApply(k.klabel(), l, r);
} else {
return k;
}
}
return super.apply(k);
}
@Override
public K apply(KRewrite k) {
K l = apply(k.left());
lhs = false;
K r = apply(k.right());
lhs = true;
if (l != k.left() || r != k.right()) {
return KRewrite(l, r, k.att());
} else {
return k;
}
}
}
}