-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeBruijn.js
268 lines (247 loc) · 7.82 KB
/
DeBruijn.js
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
"use strict";
function p(x) {
console.log("###" + x + "###");
return p;
}
function getInternal(name) {
console.log("getInternal: name: " + "##" + name + "##");
if (name === "+") {
return ["int", (x) => ["int", (y) => ["int",
deBruijnLeftmost(x, false)[1] +
deBruijnLeftmost(y, false)[1]
]]];
}
if (name === "bind") {
console.log("bind~~~~~~~~~~~~~~~~");
return ["int", function (ioa) {
console.log("got ioa~~~~~~");
return ["int", function(f) {
console.log("got f~~~~~~");
return ["int", function(w0) {
var _w0 = deBruijnLeftmost(w0, false)[1];
var _f = deBruijnLeftmost(f, false);
console.log("00000");
console.log(JSON.stringify(_f));
console.log("1111");
_f = _f[1];
var _ioa = deBruijnLeftmost(ioa, false)[1];
console.log(">>>>>>");
console.log("_w0:");
console.log(_w0);
console.log("_f:");
console.log(_f.toString());
console.log("_ioa.toString()");
console.log(_ioa.toString());
console.log("<<<<<<<");
var [a, w1] = _ioa(_w0);
return ["int", _f(a)(w1)];
}];
}];
}];
}
if (name === "print") {
return ["int", (a) => ["int", (world) => {
console.log("print: " + deBruijnLeftmost(a, false)[1]);
console.log("world: ");
console.log(world);
var io = [88, deBruijnLeftmost(world, false)[1]];
var tmp = ["int", io];
console.log("print returns: ")
console.log(tmp);
return tmp;
}]];
}
if (name === "arrGet") {
return ["int", (arr) => ["int", (idx) => {
console.log("11111111");
var theArr = deBruijnLeftmost(arr, false)[1];
console.log("type of theArr: " + typeof(theArr));
console.log("value of theArr: " + theArr);
var theIdx = deBruijnLeftmost(idx, false)[1];
console.log("type of theIdx: " + typeof(theIdx));
console.log("value of theIdx: " + theIdx);
var tmp = ["int", theArr[theIdx]];
console.log("arrGet returns:");
console.log(tmp);
return tmp;
}]];
}
return ["int", parseInt(name)];
}
function toDeBruijn(exp, binds) {
if (exp[0] === "var") {
const name = exp[1];
const len = binds.length;
var i = 1;
while (true) {
if (i > len) { return getInternal(name); }
if (binds[len - i] === name) {
return i; // TODO: 1 .. n (without the "var" tag?)
}
i++;
}
}
if (exp[0] === "app") {
return ["app", toDeBruijn(exp[1], binds), toDeBruijn(exp[2], binds)];
}
if (exp[0] === "lam") {
var newBinds = [...binds, exp[1]]; // TODO: better data structure
return ["lam", toDeBruijn(exp[2], newBinds)];
}
throw "not here";
}
function fromDeBruijn(exp, binds, nextVarRef) {
if (exp[0] === "app") {
return ["app", fromDeBruijn(exp[1], binds, nextVarRef)
, fromDeBruijn(exp[2], binds, nextVarRef)];
}
if (exp[0] === "lam") {
const newVar = "_" + (nextVarRef[0]++);
return ["lam", newVar
, fromDeBruijn(exp[1], [...binds, newVar], nextVarRef)];
}
if (exp[0] === "int") {
return exp;
}
// var exp is a number
return ["var", binds[binds.length - exp]];
}
function beta(A, B) {
return doA(A, B, 1);
}
function doA(a, B, aLamDepth) {
if (a[0] === "app") {
return ["app", doA(a[1], B, aLamDepth), doA(a[2], B, aLamDepth)];
}
if (a[0] === "lam") {
return ["lam", doA(a[1], B, aLamDepth + 1)];
}
if (a[0] === "int") {
return a;
}
// var is just a number
if (a === aLamDepth) {
return doB(B, aLamDepth, 0);
} else if (a > aLamDepth) {
return a - 1;
} else {
return a;
}
}
function doB(b, aLamDepth, bLamDepth) {
if (b[0] === "app") {
return ["app", doB(b[1], aLamDepth, bLamDepth), doB(b[2], aLamDepth, bLamDepth)];
}
if (b[0] === "lam") {
return ["lam", doB(b[1], aLamDepth, bLamDepth + 1)];
}
if (b[0] === "int") {
return b;
}
// var is just a number
if (b > bLamDepth) {
return b + aLamDepth - 1;
} else {
return b;
}
}
function deBruijnLeftmost(exp, waitLam) {
if (exp[0] === "lam") {
if (waitLam) {
return exp;
} else {
return ["lam", deBruijnLeftmost(exp[1], false)];
}
}
if (exp[0] === "app") {
var newLeft = deBruijnLeftmost(exp[1], true);
if (newLeft[0] === "lam") {
return deBruijnLeftmost(beta(newLeft[1], exp[2]), waitLam);
} else if (newLeft[0] === "int" && newLeft[1] instanceof Function) {
return deBruijnLeftmost(newLeft[1](exp[2]), waitLam);
} else {
return ["app", newLeft, deBruijnLeftmost(exp[2], false)];
}
}
// ["int" ...] can also be just returned as-is
// var exp is just a number
return exp;
}
var _parse = require("./parse.js"),
parse = _parse.parse, unparse = _parse.unparse, parseFile = _parse.parseFile;
var _rename = require("./rename.js"),
rename = _rename.rename;
var generated_3_6_normal = JSON.parse(require("fs").readFileSync("generated_3_6_normal.json"));
for (let [exp, normal] of generated_3_6_normal) {
/*
// conversion test
var db = toDeBruijn(exp, []);
var exp2 = fromDeBruijn(db, [], [0]);
var x = JSON.stringify(exp);
var y = JSON.stringify(rename(exp2));
if (x !== y) {
console.log("x: " + x);
console.log("y: " + y);
throw "die";
}
*/
//console.log("==== normalize test ====");
//console.log("exp: " + JSON.stringify(exp));
var db = toDeBruijn(exp, []);
//console.log("db: " + JSON.stringify(db));
//console.log("----");
var normalDB = deBruijnLeftmost(db, false);
var exp2 = fromDeBruijn(normalDB, [], [0]);
var x = JSON.stringify(exp2);
var y = JSON.stringify(normal);
if (x !== y) {
console.log("----");
console.log("exp: " + JSON.stringify(exp));
console.log("db: " + JSON.stringify(db));
console.log("normalDB: " + JSON.stringify(normalDB));
console.log("x: " + x);
console.log("y: " + y);
throw "die";
}
}
console.log("done");
/*
var exp = parse("((λx (λy (x y)))(λx x))");
console.log(JSON.stringify(exp));
var db = toDeBruijn(exp, []);
console.log(JSON.stringify(db));
var exp2 = fromDeBruijn(db, [], [0]);
console.log(JSON.stringify(exp2));
*/
//var exp = parse("((λx (λy (x y)))(λx 5))");
//var exp = parse("( ( + ( (λy y) 2) ) ( (λy y) 3) )");
//var exp = parse("( ( + 2 ) 3 )");
//var bind = "(λioa (λf (λw0 ( (λa_w1 ((f ((arrGet a_w1) 0)) ((arrGet a_w1) 1)) ) (ioa w0) ))))"
//var exp = parse(`((λbind ( print 3 )) ${bind} )`);
var exp = parseFile("DeBruijn.ulc");
console.log("exp: " + JSON.stringify(exp));
var db = toDeBruijn(exp, []);
console.log("de Bruijn: "); // + JSON.stringify(db));
console.log(db);
console.log("=========");
var db = deBruijnLeftmost(db, false);
var exp2 = fromDeBruijn(db, [], [0]);
//console.log(JSON.stringify(exp2));
console.log("======= going to apply world");
exp2[1](["int", 999999]);
/*
function timeDiff(f) {
const t0 = Date.now();
console.log(f());
console.log(Date.now() - t0);
}
console.log("=================================");
var w = "(\\x (x (x (x (x (x (x (x (x (x x))))))))))";
w = `(${w} (${w} (${w} (${w} (${w} (${w} (\\x x)))))))`;
w = parse(w);
w = toDeBruijn(w, []);
//console.log(JSON.stringify(deBruijnLeftmost(w, false)));
console.log("1111");
timeDiff(() => deBruijnLeftmost(w, false));
console.log("2222");
*/