forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRefreshRules.java
More file actions
78 lines (66 loc) · 2.44 KB
/
RefreshRules.java
File metadata and controls
78 lines (66 loc) · 2.44 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
// Copyright (c) 2015-2019 K Team. All Rights Reserved.
package org.kframework.compile;
import org.kframework.definition.Rule;
import org.kframework.kore.K;
import org.kframework.kore.KVariable;
import org.kframework.kore.TransformK;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.stream.Collectors;
import static org.kframework.definition.Constructors.*;
import static org.kframework.kore.KORE.*;
/**
* refreshes variable names in terms and sentences according to alpha equivalence.
* Variables that have previously been refreshed are not normalized on succeeding passes,
* allowing the user to fine-tune the applyRefresh process such that arbitrary subterms can share
* a common prefix.
*/
public class RefreshRules {
private int counter = 0;
private Map<KVariable, String> vars = new HashMap<>();
/**
* Refreshes a rule
* @param rule The rule to be refreshed.
* @return The refreshed version of {@code rule}, in which each variable
* is alpha-renamed to fresh variable.
* name.
*/
public static Rule refresh(Rule rule) {
return new RefreshRules().applyRefresh(rule);
}
/**
* Refreshes a set of rules such that no two rules would have variables in common.
* @param rules The rules to be refreshed.
* @return The refreshed version of {@code rules}
*/
public static Collection<Rule> refresh(Collection<Rule> rules) {
RefreshRules refreshRules = new RefreshRules();
return rules.stream().map(refreshRules::applyRefreshResetVars).collect(Collectors.toCollection(LinkedList::new));
}
private Rule applyRefresh(Rule rule) {
return Rule(
applyRefresh(rule.body()),
applyRefresh(rule.requires()),
applyRefresh(rule.ensures()),
rule.att());
}
private Rule applyRefreshResetVars(Rule rule) {
vars.clear();
return applyRefresh(rule);
}
private K applyRefresh(K term) {
return new TransformK() {
@Override
public K apply(KVariable var) {
if (var.att().contains("refreshed"))
return var;
if (!vars.containsKey(var)) {
vars.put(var, "_" + counter++);
}
return KVariable(vars.get(var), var.att().add("refreshed", var.name()));
}
}.apply(term);
}
}