forked from HeapVisCapstone/benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdaCalc.java
More file actions
231 lines (188 loc) · 5.48 KB
/
LambdaCalc.java
File metadata and controls
231 lines (188 loc) · 5.48 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import java.util.*;
public class LambdaCalc
{
private static Var v(String x) { return new Var(x); }
private static Apply ap(Term m, Term n) { return new Apply(m, n); }
private static Lam l(String x, Term e) { return new Lam(x, e); }
public static void main(String[] args) {
// \x . (x x) (\y. y) (\z . z)
// --> (\y . y) (\y . y) (\z . z)
// --> (\y . y) (\z . z)
// --> (\z . z)
Term t = ap(ap(l("x", ap(v("x"), v("x"))), l("y", v("y"))), l("z", v("z")));
try {
while (t != null) {
System.out.println(t);
t = t.reduce();
}
} catch (Exception e) {
System.out.println(t + " cannot be further reduced");
System.err.println(e);
}
}
}
class EvalError extends Exception {
public EvalError(String message) {
super(message);
}
}
// represents lambda terms using named terms
abstract class Term
{
private static int counter = 0;
public abstract Term copy() throws EvalError;
public abstract Set<String> freeVars();
public abstract boolean isVal();
public abstract boolean reducible();
public abstract Term reduce() throws EvalError;
// t[x -> e]
public abstract Term subst(String x, Term e) throws EvalError;
public abstract Term rename(String x, String y) throws EvalError;
public static String freshName(String base) {
String name = base + counter;
counter++;
return name;
}
}
// x
class Var extends Term
{
public String name;
public Var(String x) {
this.name = x;
}
public Var(Term t) throws EvalError {
if (!(t instanceof Var)) {
throw new EvalError("Cannot coerce term " + t + " to a variable form");
} else {
Var tV = (Var) t;
this.name = tV.name;
}
}
public Term copy() throws EvalError { return new Var(this); }
public boolean isVal() { return false; }
public boolean reducible() { return false; }
public Term reduce() {
return null;
}
public Term subst(String x, Term e) throws EvalError {
if (name.equals(x)) {
return e.copy();
} else {
return new Var(this);
}
}
public Term rename(String x, String y) throws EvalError {
if (name.equals(x)) {
return new Var(y);
} else {
return new Var(this);
}
}
public Set<String> freeVars() {
HashSet<String> hs = new HashSet<>();
hs.add(name);
return hs;
}
@Override
public String toString() {
return name;
}
}
// M N
class Apply extends Term
{
Term m, n;
public Apply(Term m, Term n) {
this.m = m;
this.n = n;
}
public Term copy() { return new Apply(this.m, this.n); }
public Term reduce() throws EvalError{
if (m.isVal()) {
if (n.reducible()) {
// n --> n'
// m n --> m n'
return new Apply(m, n.reduce());
} else if (m.reducible()) {
return new Apply(m.reduce(), n);
} else {
// [m = (\x . e)] n --> n[x->e]
// where m.body = e
Lam mL = (Lam) m;
return mL.body.subst(mL.name, n);
}
} else {
if (m.reducible()) {
// m --> m'
// m n --> m' n
return new Apply(m.reduce(), n);
} else {
throw new EvalError(m + " is not a valid lambda abstraction form");
}
}
}
public Term subst(String x, Term e) throws EvalError {
return new Apply(m.subst(x, e), n.subst(x, e));
}
public Term rename(String x, String y) throws EvalError {
return new Apply(m.rename(x, y), n.rename(x, y));
}
public Set<String> freeVars() {
Set<String> fv = m.freeVars();
fv.addAll(n.freeVars());
return fv;
}
public boolean isVal() { return false; }
public boolean reducible() { return true; }
@Override
public String toString() {
return "(" + m + ")" + " (" + n + ")";
}
}
// \x . M
class Lam extends Term
{
public String name;
public Term body;
public Lam(String x, Term m) {
this.name = x;
this.body = m;
}
public Lam(Lam l) {
this.name = l.name;
this.body = l.body;
}
public Term copy() { return new Lam(this); }
public boolean isVal() { return true; }
public boolean reducible() { return false; }
public Term reduce() {
return null;
}
public Term subst(String x, Term e) throws EvalError {
if (name.equals(x)) {
return new Lam(this);
} else if (!e.freeVars().contains(name)) {
return new Lam(name, body.subst(x, e));
} else {
Term renamed = this.rename(x, Term.freshName(x));
return renamed.subst(x, e);
}
}
public Term rename(String x, String y) throws EvalError {
if (name.equals(x)) {
return new Lam(y, body.rename(x, y));
} else {
return new Lam(name, body.rename(x, y));
}
}
public Set<String> freeVars() {
Set<String> fv = body.freeVars();
fv.remove(name);
return fv;
}
@Override
public String toString() {
return "\\" + name + " . " + body;
}
}