forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResolveStrict.java
More file actions
149 lines (137 loc) · 6.32 KB
/
ResolveStrict.java
File metadata and controls
149 lines (137 loc) · 6.32 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
// 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.Module;
import org.kframework.definition.NonTerminal;
import org.kframework.definition.Production;
import org.kframework.definition.Rule;
import org.kframework.definition.Sentence;
import org.kframework.kil.Attribute;
import org.kframework.kompile.KompileOptions;
import org.kframework.kore.K;
import org.kframework.kore.KApply;
import org.kframework.kore.Sort;
import org.kframework.utils.errorsystem.KEMException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import static org.kframework.Collections.*;
import static org.kframework.definition.Constructors.*;
import static org.kframework.kore.KORE.*;
public class ResolveStrict {
private final KompileOptions kompileOptions;
public ResolveStrict(KompileOptions kompileOptions) {
this.kompileOptions = kompileOptions;
}
public Set<Sentence> resolve(Set<Production> strictProductions) {
Set<Sentence> sentences = new HashSet<>();
for (Production prod : strictProductions) {
if (!prod.klabel().isDefined()) {
throw KEMException.compilerError("Only productions with a KLabel can be strict.", prod);
}
if (prod.att().contains(Attribute.STRICT_KEY)) {
sentences.addAll(resolve(prod, false));
}
if (prod.att().contains(Attribute.SEQSTRICT_KEY)) {
sentences.addAll(resolve(prod, true));
}
}
return sentences;
}
private static KApply cast(Sort sort, K k) {
return KApply(KLabel("#SemanticCastTo" + sort.name()), k);
}
public Set<Sentence> resolve(Production production, boolean sequential) {
long arity = stream(production.items()).filter(i -> i instanceof NonTerminal).count();
List<Integer> strictnessPositions = new ArrayList<>();
String attribute;
if (sequential) {
attribute = production.att().get(Attribute.SEQSTRICT_KEY);
} else {
attribute = production.att().get(Attribute.STRICT_KEY);
}
if (attribute.isEmpty()) {
for (int i = 1; i <= arity; i++) {
strictnessPositions.add(i);
}
} else {
String[] strictAttrs = attribute.split(",");
for (String strictAttr : strictAttrs) {
try {
int pos = Integer.parseInt(strictAttr.trim());
if (pos < 1 || pos > arity)
throw new IndexOutOfBoundsException();
strictnessPositions.add(pos);
} catch (NumberFormatException | IndexOutOfBoundsException e) {
throw KEMException.compilerError(
"Expecting a number between 1 and " + arity + ", but found " + strictAttr + " as a" +
" strict position in " + Arrays.toString(strictAttrs),
production);
}
}
}
Set<Sentence> sentences = new HashSet<>();
for (int i = 0; i < strictnessPositions.size(); i++) {
List<K> items = new ArrayList<>();
for (int j = 0; j < arity; j++) {
if (kompileOptions.strict()) {
// Preserve sort information of the production
items.add(cast(production.nonterminal(j).sort(), KVariable("K" + j)));
} else {
items.add(KVariable("K" + j));
}
}
int strictnessPosition = strictnessPositions.get(i) - 1;
if (kompileOptions.strict()) {
// Preserve sort information of the production
items.set(strictnessPosition, cast(production.nonterminal(strictnessPosition).sort(), KVariable("HOLE")));
} else {
items.set(strictnessPosition, cast(Sort("KItem"), KVariable("HOLE")));
}
// is seqstrict the elements before the argument should be KResult
Optional<KApply> sideCondition = strictnessPositions.subList(0, i).stream().map(j -> KApply(KLabel("isKResult"), KVariable("K" + (j - 1)))).reduce(BooleanUtils::and);
K requires;
if (!sideCondition.isPresent() || !sequential) {
requires = BooleanUtils.TRUE;
} else {
requires = sideCondition.get();
}
Context ctx = Context(KApply(production.klabel().get(), KList(items)), requires, production.att());
sentences.add(ctx);
}
if (production.att().contains("hybrid")) {
List<K> items = new ArrayList<>();
for (int j = 0; j < arity; j++) {
if (kompileOptions.strict()) {
// Preserve sort information of the production
items.add(cast(production.nonterminal(j).sort(), KVariable("K" + j)));
} else {
items.add(KVariable("K" + j));
}
}
K term = KApply(production.klabel().get(), KList(items));
Optional<KApply> sideCondition = strictnessPositions.stream().map(j -> KApply(KLabel("isKResult"), KVariable("K" + (j - 1)))).reduce(BooleanUtils::and);
K requires;
if (!sideCondition.isPresent()) {
requires = BooleanUtils.TRUE;
} else {
requires = sideCondition.get();
}
Rule hybrid = Rule(KRewrite(KApply(KLabel("isKResult"), term), BooleanUtils.TRUE), requires, BooleanUtils.TRUE);
sentences.add(hybrid);
}
return sentences;
}
public Module resolve(Module input) {
Set<Sentence> contextsToAdd = resolve(stream(input.localSentences())
.filter(s -> s instanceof Production)
.map(s -> (Production) s)
.filter(p -> p.att().contains("strict") || p.att().contains("seqstrict")).collect(Collectors.toSet()));
return Module(input.name(), input.imports(), (scala.collection.Set<Sentence>) input.localSentences().$bar(immutable(contextsToAdd)), input.att());
}
}