forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLiftToKSequence.java
More file actions
90 lines (79 loc) · 2.58 KB
/
LiftToKSequence.java
File metadata and controls
90 lines (79 loc) · 2.58 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
// Copyright (c) 2015-2016 K Team. All Rights Reserved.
package org.kframework.compile;
import org.kframework.definition.Context;
import org.kframework.definition.Rule;
import org.kframework.definition.Sentence;
import org.kframework.kore.*;
import java.util.ArrayList;
import java.util.List;
import static org.kframework.definition.Constructors.*;
import static org.kframework.kore.KORE.*;
/**
* Raises all rules into a form with a KSequence in every position that expects a term of sort K.
*/
public class LiftToKSequence {
public Sentence lift(Sentence s) {
if (s instanceof Rule) {
return lift((Rule) s);
} else if (s instanceof Context) {
return lift((Context) s);
} else {
return s;
}
}
private Rule lift(Rule rule) {
return Rule(
lift(rule.body()),
lift(rule.requires()),
lift(rule.ensures()),
rule.att());
}
private Context lift(Context context) {
return Context(
lift(context.body()),
lift(context.requires()),
context.att());
}
public K lift(K term) {
K result = new TransformK() {
@Override
public K apply(KApply k) {
List<K> children = new ArrayList<>();
for (K child : k.klist().items()) {
K res = apply(child);
if (res instanceof KSequence) {
children.add(res);
} else {
children.add(KSequence(res));
}
}
return KApply(k.klabel(), KList(children), k.att());
}
@Override
public K apply(KAs k) {
K res = apply(k.pattern());
KVariable var = (KVariable) k.alias();
if (!(res instanceof KSequence) && var.att().<String>getOptional("sort").orElse("K").equals("K")) {
res = KSequence(res);
}
return KAs(res, k.alias(), k.att());
}
}.apply(term);
if (result instanceof KSequence) {
return result;
} else {
return KSequence(result);
}
}
public K lower(K term) {
return new TransformK() {
@Override
public K apply(KSequence k) {
if (k.items().size() == 1) {
return super.apply(k.items().get(0));
}
return super.apply(k);
}
}.apply(term);
}
}