-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrts.js
351 lines (319 loc) · 8.52 KB
/
rts.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
function ev( x ) {
if ( x !== undefined && x.eOrV !== undefined ) {
var x_upd = x ;
do {
if ( typeof x.eOrV == 'function' ) {
var xx = x.eOrV() ;
x.eOrV = xx ;
x = xx ;
} else {
x = x.eOrV ;
}
}
while ( x !== undefined && x.eOrV !== undefined ) ;
while ( x_upd.eOrV !== undefined ) {
var x_next = x_upd.eOrV ;
x_upd.eOrV = x ;
x_upd = x_next ;
}
}
return x ;
}
// Apply node, not enough args
AppLT.prototype = {
applyN : function ( args ) {
var needs = this.needsNrArgs() ;
if ( args.length < needs ) {
return new AppLT( this, args ) ;
} else if ( args.length == needs ) {
return this.fun.applyN( this.args.concat( args ) ) ;
} else {
var fun = ev( this.applyN( args.slice( 0, needs ) ) ) ;
return {
eOrV : function() {
return fun.applyN( args.slice( needs ) ) ;
} } ;
}
} ,
needsNrArgs : function() {
return this.fun.needsNrArgs() - this.args.length ;
} ,
}
function AppLT( fun, args ) {
// this.needs = fun.needs - args.length ;
this.fun = fun ;
this.args = args ;
}
// Apply node, unknown how much is missing or too much
App.prototype = {
applyN : function ( args ) {
var fun = ev(this) ;
return {
eOrV : function() {
return fun.applyN( args ) ;
} } ;
} ,
}
function App( fun, args ) {
this.eOrV = function() {
return fun.applyN( args ) ;
}
}
// Function node
Fun.prototype = {
applyN : function ( args ) {
if ( args.length < this.needs ) {
return new AppLT( this, args ) ;
} else if ( args.length == this.needs ) {
var x = this.fun.apply( null, args ) ;
return x ;
} else {
var fun = ev( this.fun.apply( null, args.slice( 0, this.needs ) ) ) ;
var remargs = args.slice( this.needs ) ;
return {
eOrV : function() {
return fun.applyN( remargs ) ;
} } ;
}
} ,
needsNrArgs : function() {
return this.needs ;
} ,
}
function Fun( fun ) {
this.needs = fun.length ;
this.fun = fun ;
}
// lazy application wrappers
function _a0_(f) {
return new App(f,[]) ;
}
// indirection
function ind() {
return new App(function(){throw "ind: attempt to prematurely evaluate indirection";},[]) ;
}
function indset_(i,x) {
i.eOrV = x ;
}
//[8
//]
// function construction wrappers
function fun(f) {
return new Fun(f) ;
}
//[8
//]
// strict application wrappers
function eval1(f,a) {
return ev( f.applyN([a]) ) ;
}
function eval2(f,a,b) {
return ev( f.applyN([a,b]) ) ;
}
function _e3_(f,a,b,c) {
return ev( f.applyN([a,b,c]) ) ;
}
function _e4_(f,a,b,c,d) {
return ev( f.applyN([a,b,c,d]) ) ;
}
function _e5_(f,a,b,c,d,e) {
return ev( f.applyN([a,b,c,d,e]) ) ;
}
function _eN_(f,a) {
return ev( f.applyN(a) ) ;
}
//[8
// lazy application wrappers
function _a0_(f) {
return new App(f,[]) ;
}
//]
function app1(f,a) {
return new App(f,[a]) ;
}
function app2(f,a,b) {
return new App(f,[a,b]) ;
}
function _a3_(f,a,b,c) {
return new App(f,[a,b,c]) ;
}
function _a4_(f,a,b,c,d) {
return new App(f,[a,b,c,d]) ;
}
function _a5_(f,a,b,c,d,e) {
return new App(f,[a,b,c,d,e]) ;
}
function _aN_(f,a) {
return new App(f,a) ;
}
//[8
// indirection
function ind() {
return new App(function(){throw "ind: attempt to prematurely evaluate indirection";},[]) ;
}
function indset_(i,x) {
i.eOrV = x ;
}
//]
// setup
function init() {
}
function cons(x,y) { return [0,x,y]; }
function head(l) { return l[1]; }
function tail(l) { return l[2]; }
var nil = [1] ;
function isNil(x) { return x[0] == 1 ; }
function show( x ) {
var x = ev(x) ;
document.write( ""+ev(x) ) ;
}
function showList( l ) {
var list = ev(l) ;
switch (list[0]) {
case 0 :
document.write( ev(head(list)) + ":" ) ;
showList( tail(list) ) ;
break ;
case 1 :
document.write( "[]" ) ;
break ;
}
}
// test: sieve
function testSieve() {
var id = fun( function(a) {
// trace( "id: " + a ) ;
return a ;
} ) ;
var even = fun( function(a) {
// return _eval_(a[0]) % 2 == 0 ;
return app2( eq, app2( mod, a, 2 ), 0 ) ;
} ) ;
var eq = fun( function(a,b) {
return ev(a) == ev(b) ;
} ) ;
var ne = fun( function(a,b) {
return ev(a) != ev(b) ;
} ) ;
var add = fun( function(a,b) {
return ev(a) + ev(b) ;
} ) ;
var sub = fun( function(a,b) {
return ev(a) - ev(b) ;
} ) ;
var mul = fun( function(a,b) {
return ev(a) * ev(b) ;
} ) ;
var div = fun( function(a,b) {
return Math.floor ( ev(a) / ev(b) ) ;
} ) ;
var mod = fun( function(a,b) {
return ( ev(a) % ev(b) ) ;
} ) ;
var from = fun( function(a) {
return cons( a, app1( from, app2( add, a, 1 ) ) ) ;
} ) ;
var last = fun( function(a) {
var list = ev(a) ;
switch (list[0]) {
case 0 :
var list2 = ev(tail(list)) ;
switch (list2[0]) {
case 0 :
return app1( last, tail(list) ) ;
case 1 :
return head(list) ;
}
case 1 :
return undefined ;
}
} ) ;
var take = fun( function(a,b) {
var len = ev(a) ;
var list = ev(b) ;
if ( len <= 0 || isNil(list) ) {
return nil ;
} else {
return cons( head(list), app2( take, app2( sub, len, 1 ), tail(list) ) ) ;
}
} ) ;
var filter = fun( function(a,b) {
var list = ev(b) ;
var test = eval1( a, head(list) ) ;
if ( test ) {
return cons( head(list), app2( filter, a, tail(list) ) ) ;
} else {
return app2( filter, a, tail(list) ) ;
}
} ) ;
var notMultiple = fun( function(a,b) {
return app2( ne, app2( mul, app2( div, b, a), a ), b ) ;
} ) ;
var notMultiple2 = fun( function(a,b) {
var x = ev(a) ;
var y = ev(b) ;
// return (Math.floor(y / x) * x) != y ;
return y % x != 0 ;
} ) ;
var sieve = fun( function(a) {
var list = ev(a) ;
return cons( head(list), app1( sieve, app2( filter, app1( notMultiple2, head(list) ), tail(list) ) ) ) ;
} ) ;
var sieve2 = fun( function(nmz,a) {
var list = ev(a) ;
return cons( head(list), app2( sieve2, app1( id, nmz ), app2( filter, app1( nmz, head(list) ), tail(list) ) ) ) ;
} ) ;
// Firefox: 400 (not more, because of to deep recursion)
// Chrome: 1000
var mainSieve = app2( take, 400, app1( sieve, app1( from, 2 ) ) ) ;
var mainSieve2 = app2( take, 500, app2( sieve2, app1( id, notMultiple2 ), app1( from, 2 ) ) ) ;
var testInf = app2( filter, app1( eq, 0 ), app1( from, 2 ) ) ;
// running it...
evalCounter = 0 ;
var d = new Date() ;
var t1 = d.getTime() ;
// showList( mainSieve ) ;
show( app1( last, mainSieve ) ) ;
d = new Date() ;
var t2 = d.getTime() - t1 ;
document.write("<hr/>time= " + t2 + " ms" + ((evalCounter>0) ? ", nreval= " + evalCounter + ", ms/ev= " + (t2/evalCounter) : "") + "<br/>") ;
}
function testMisc() {
trace("load & init ok") ;
var plus = fun( function(a,b){return ev(a)+ev(b);}) ;
trace("plus: " + plus) ;
var inc1 = fun( function(a){trace("inc: " + a) ; var x = ev(a) ; return x+1;}) ;
trace("inc1: " + inc1) ;
var inc2 = plus.applyN([10]) ;
trace("inc2: " + inc2) ;
var two1 = 2 ;
// var two2 = new AppN_WHNF(2) ;
var two3 = new App(new Fun(0,function(){return 2;}),[]) ;
var arr = [two1] ;
// trace("two2: " + two2) ;
trace("two3: " + two3) ;
trace("two3 eval: " + ev(two3)) ;
trace("two3: " + two3) ;
trace("two3 eval: " + ev(two3)) ;
trace("arr: " + arr) ;
var x1 = inc2.applyN( arr ) ;
trace("inc 2: " + x1) ;
var x2 = new App( inc2, arr ) ;
trace("inc del 2: " + x2) ;
trace("inc del 2 eval: " + ev(x2)) ;
}
function tryOut() {
var f = function(a,b) {} ;
var l = cons(1,nil) ;
// trace(ToPropertyDescriptor(f)) ;
// trace(ToPropertyDescriptor(Function)) ;
// trace(ToPropertyDescriptor("a")) ;
// trace(ToPropertyDescriptor(String)) ;
trace("f "+f.length) ;
}
function main() {
init() ;
// testMisc() ;
// tryOut() ;
testSieve() ;
}