forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResolveSemanticCasts.java
More file actions
142 lines (124 loc) · 4.37 KB
/
ResolveSemanticCasts.java
File metadata and controls
142 lines (124 loc) · 4.37 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
// Copyright (c) 2015-2016 K Team. All Rights Reserved.
package org.kframework.compile;
import org.kframework.builtin.BooleanUtils;
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.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import static org.kframework.kore.KORE.*;
/**
* Created by dwightguth on 4/17/15.
*/
public class ResolveSemanticCasts {
private final boolean skipSortPredicates;
private Set<KApply> casts = new HashSet<>();
private Map<KVariable, KVariable> varToTypedVar = new HashMap<>();
public ResolveSemanticCasts(boolean skipSortPredicates) {
this.skipSortPredicates = skipSortPredicates;
}
void resetCasts() {
casts.clear();
varToTypedVar.clear();
}
public K resolve(K k) {
resetCasts();
gatherCasts(k);
return transform(k);
}
private Rule resolve(Rule rule) {
resetCasts();
gatherCasts(rule.body());
gatherCasts(rule.requires());
gatherCasts(rule.ensures());
return new Rule(
transform(rule.body()),
addSideCondition(transform(rule.requires()), rule.att().contains("macro")),
transform(rule.ensures()),
rule.att());
}
private Context resolve(Context context) {
resetCasts();
gatherCasts(context.body());
gatherCasts(context.requires());
return new Context(
transform(context.body()),
addSideCondition(transform(context.requires()), false),
context.att());
}
K addSideCondition(K requires, boolean macro) {
if (skipSortPredicates || macro)
return requires;
else {
Optional<KApply> sideCondition = casts.stream().map(k -> {
return new TransformK() {
@Override
public K apply(KVariable k) {
if (varToTypedVar.containsKey(k)) {
return varToTypedVar.get(k);
}
return k;
}
}.apply(k);
}).map(k -> KApply(KLabel("is" + getSortNameOfCast((KApply) k)), transform(k))).reduce(BooleanUtils::and);
if (!sideCondition.isPresent()) {
return requires;
} else if (requires.equals(BooleanUtils.TRUE) && sideCondition.isPresent()) {
return sideCondition.get();
} else {
return BooleanUtils.and(sideCondition.get(), requires);
}
}
}
public static String getSortNameOfCast(KApply kapp) {
return kapp.klabel().name().substring("#SemanticCastTo".length());
}
void gatherCasts(K term) {
new VisitK() {
@Override
public void apply(KApply v) {
if (v.klabel().name().startsWith("#SemanticCastTo")) {
casts.add(v);
K child = v.klist().items().get(0);
if (child instanceof KVariable) {
KVariable var = (KVariable) child;
varToTypedVar.put(var, KVariable(var.name(), var.att().contains(Attribute.SORT_KEY) ? var.att() : var.att().add(Attribute.SORT_KEY, getSortNameOfCast(v))));
}
}
super.apply(v);
}
}.apply(term);
}
K transform(K term) {
return new TransformK() {
@Override
public K apply(KApply k) {
if (casts.contains(k)) {
return super.apply(k.klist().items().get(0));
}
return super.apply(k);
}
@Override
public K apply(KVariable k) {
if (varToTypedVar.containsKey(k)) {
return varToTypedVar.get(k);
}
return super.apply(k);
}
}.apply(term);
}
public synchronized Sentence resolve(Sentence s) {
if (s instanceof Rule) {
return resolve((Rule) s);
} else if (s instanceof Context) {
return resolve((Context) s);
} else {
return s;
}
}
}