-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
533 lines (490 loc) · 10 KB
/
lib.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// Base ////////////////////////////////////////////////////////////
function combine(f) {
return function (g) {
return function (a) {
return g(f(a));
};
};
}
// Equivalents: $true
function $const(a) {
return function (b) {
return a;
};
}
// SKI Combinator /////////////////////////////////////////////////////////////////
function I(t) {
return t;
}
function K(a) {
return function (b) {
return a;
};
}
function S(x) {
return function (y) {
return function (z) {
return x(z)(y(z));
};
};
}
// y combinator //////////////////////////////////////////////////////////////////
function y(r) {
return (function (f) {
return f(f);
})(function (f) {
return r(function (x) {
return f(f)(x);
});
});
}
function unit(t) {
return t;
}
// bottom //////////////////////////////////////////////////////////////////////
function bottom(t) {
return bottom(t);
}
function $sqr(x) {
return x(x);
}
function tuple(a) {
return function (b) {
return function (f) {
return f(a)(b);
};
};
}
function fst(t) {
return t($true);
}
function snd(t) {
return t($false);
}
function left(x) {
return function (l) {
return function (r) {
return l(x);
};
};
}
function right(x) {
return function (l) {
return function (r) {
return r(x);
};
};
}
function either(e) {
return e;
}
// Equivalents: const
function $true(f) {
return function (t) {
return f;
};
}
function $false(f) {
return function (t) {
return t;
};
}
function not(x) {
return x($false)($true);
}
function and(x) {
return function (y) {
return y(x($true)($false))($false);
};
}
function or(x) {
return function (y) {
return y($true)(x($true)($false));
};
}
function then(x) {
return function (y) {
return or(not(x))(y);
};
}
function iff(x) {
return function (y) {
return and(then(x)(y))(then(y)(x));
};
}
function xor(x) {
return function (y) {
return not(iff(x)(y));
};
}
function $if(condition) {
return condition;
}
function unless(condition) {
return not(condition);
}
function lif(condition) {
return function (_then) {
return function (_else) {
return condition(_then)(_else)(unit);
};
};
}
function nothing(l) {
return function (r) {
return l(unit);
};
}
function just(x) {
return function (l) {
return function (r) {
return r(x);
};
};
}
function maybe(m) {
return function (l) {
return function (r) {
return m(l)(r);
};
};
}
function nil(b) {
return function (c) {
return nothing(b)(c);
};
}
function cons(x) {
return function (xs) {
return just(tuple(x)(xs));
};
}
function head(xs) {
return maybe(xs)(bottom)(fst);
}
function tail(xs) {
return maybe(xs)(bottom)(snd);
}
function isNil(xs) {
return maybe(xs)($const($true))($const($false));
}
function map(f) {
return function (xs) {
return lif(isNil(xs))($const(nil))(function (_) {
return cons(f(head(xs)))(map(f)(tail(xs)));
});
};
}
function foldl(f) {
return function (s) {
return function (xs) {
return lif(isNil(xs))($const(s))(function (_) {
return foldl(f)(f(s)(head(xs)))(tail(xs));
});
};
};
}
function unfoldr(f) {
return function (s) {
return maybe(f(s))($const(nil))(function (t) {
return cons(fst(t))(unfoldr(f)(snd(t)));
});
};
}
function len(xs) {
return foldl(function (n) {
return function (_) {
return succ(n);
};
})(zero)(xs);
}
function concat(xs) {
return function (ys) {
return maybe(xs)(function (_) {
return ys;
})(function (c) {
return cons(fst(c))(concat(snd(c))(ys));
});
};
}
function flatten(xxs) {
return foldl(concat)(xxs);
}
function box(t) {
return cons(t)(nil);
}
function concat3(xs) {
return function (ys) {
return concat(concat(xs)(ys));
};
}
function concat4(xs) {
return function (ws) {
return concat3(concat(xs)(ws));
};
}
function concat5(xs) {
return function (ks) {
return concat4(concat(xs)(ks));
};
}
function concat6(vs) {
return function (xs) {
return concat5(concat(vs)(xs));
};
}
function concat7(xs) {
return function (ys) {
return concat6(concat(xs)(ys));
};
}
function concat8(xs) {
return function (ws) {
return concat7(concat(xs)(ws));
};
}
function concat9(xs) {
return function (ks) {
return concat8(concat(xs)(ks));
};
}
function leaf(x) {
return left(x);
}
function branch(a) {
return function (b) {
return right(tuple(a)(b));
};
}
function zero(f) {
return function (t) {
return t;
};
}
function succ(n) {
return function (f) {
return function (t) {
return f(n(f)(t));
};
};
}
function isZero(n) {
return n(function (_) {
return $false;
})($true);
}
function pred(n) {
return function (f) {
return function (x) {
return n(function (g) {
return function (h) {
return h(g(f));
};
})(function (u) {
return x;
})(function (u) {
return u;
});
};
};
}
function add(m) {
return function (n) {
return m(succ)(n);
};
}
function sub(m) {
return function (n) {
return n(pred)(m);
};
}
function mul(m) {
return function (n) {
return function (f) {
return m(n(f));
};
};
}
function pow(m) {
return function (n) {
return n(m);
};
}
function sqr(m) {
return m(m);
}
function _0(f) {
return function (t) {
return t;
};
}
function _1(f) {
return function (t) {
return f(t);
};
}
function _2(f) {
return function (t) {
return f(f(t));
};
}
function _3(f) {
return function (t) {
return f(f(f(t)));
};
}
function _4(f) {
return function (t) {
return f(f(f(f(t))));
};
}
function _5(f) {
return function (t) {
return f(f(f(f(f(t)))));
};
}
function _6(f) {
return function (t) {
return f(f(f(f(f(f(t))))));
};
}
function _7(f) {
return function (t) {
return f(f(f(f(f(f(f(t)))))));
};
}
function _8(f) {
return function (t) {
return f(f(f(f(f(f(f(f(t))))))));
};
}
function _9(f) {
return function (t) {
return f(f(f(f(f(f(f(f(f(t)))))))));
};
}
function _d(f) {
return function (t) {
return f(f(f(f(f(f(f(f(f(f(t))))))))));
};
}
//function _256<T>(_: Unit): Natural {
// return (n => n(n)) (n => n(n)) ((f: (t: T)=>T) => (x: T) => f(f(x)));
//}
function d(n) {
return function (m) {
return add(mul(n)(_d))(m);
};
}
function d2(y) {
return function (z) {
return d(y)(z);
};
}
function d3(x) {
return function (y) {
return function (z) {
return d(d(x)(y))(z);
};
};
}
function d4(w) {
return function (x) {
return function (y) {
return function (z) {
return d(d(d(w)(x))(y))(z);
};
};
};
}
function d5(v) {
return function (w) {
return function (x) {
return function (y) {
return function (z) {
return d(d(d(d(v)(w))(x))(y))(z);
};
};
};
};
}
// Confusing function name! Hmm...
function char(x) {
return function (y) {
return function (z) {
return box(d3(x)(y)(z));
};
};
}
function showBoolean(x) {
return lif(x)($const(concat4(char(_1)(_1)(_6))(char(_1)(_1)(_4))(char(_1)(_1)(_7))(char(_1)(_0)(_1))))($const(concat5(char(_1)(_0)(_2))(char(_0)(_9)(_7))(char(_1)(_0)(_8))(char(_1)(_1)(_5))(char(_1)(_0)(_1))));
}
function showList(show) {
return function (xs) {
return (concat3(char(_0)(_9)(_1))(foldl(function (x) {
return function (y) {
return concat3(x)(show(y))(char(_0)(_4)(_4));
};
})(nil)(xs))(char(_0)(_9)(_3)));
};
}
function showNum(n) {
return box(n(succ)(d2(_4)(_8)));
}
function showMaybe(showR) {
return function (m) {
return maybe(m)(function (_) {
return nil;
})(function (r) {
return showR(r);
});
};
}
function showTuple(showF) {
return function (showS) {
return function (t) {
return concat5(box(d3(_0)(_4)(_0)))(showF(fst(t)))(box(d3(_0)(_4)(_4)))(showS(snd(t)))(box(d3(_0)(_4)(_1)));
};
};
}
function showEither(showL) {
return function (showR) {
return function (e) {
return either(e)(function (l) {
return showL(l);
})(function (r) {
return showR(r);
});
};
};
}
function showTree(show) {
return function (xs) {
return either(xs)(function (b) {
return show(b);
})(function (r) {
return concat5(box(d3(_0)(_6)(_0)))(showTree(show)(fst(r)))(box(d3(_0)(_4)(_4)))(showTree(show)(snd(r)))(box(d3(_0)(_6)(_2)));
});
};
}
// encode/decode /////////////////////////////////////////////////////////////////////////////////////////////////////////////
function decode(empty) {
return function (append) {
return function (reader) {
return function (y) {
return foldl(append)(empty)(map(reader)(y));
};
};
};
}
function encode(read) {
return function (size) {
return unfoldr(function (i) {
return lif(isZero(sub(size)(i)))(function (_) {
return nothing;
})(function (_) {
return just(tuple(read(i))(succ(i)));
});
})(_0);
};
}