Repository navigation
Expand file tree
/
Copy pathruntime.js
More file actions
3635 lines (3099 loc) · 126 KB
/
Copy pathruntime.js
File metadata and controls
3635 lines (3099 loc) · 126 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
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// The runtime of the JavaScript that LeanScript generates: one module.
//
// Every function exported here is an operation of `JsTerm/Ops/Imported.lean` (`JsOpImported`), of
// the same name: the name of the extern it implements, behind the JavaScript representation
// of its arguments and result (`bigint_nat__lean_nat_div` on `BigInt`s, `uint53__lean_nat_div`
// on numbers below 2^53; see `scripts/gen_js_ops.py` for the naming). A generated module
// imports the operations it calls. The operations that are one JavaScript operator or call
// of a global are not here: they are written inline (`JsOpInlinable`).
//
// An alias `export const a = b;` of an operation at the same signature is not an operation of
// its own: the extern of `a` is compiled to `b` (`lean_array_get_borrowed` is `lean_array_get`).
//
// Every function is pure (it never mutates an argument), except the `_mutable` array
// updates, which the backend calls only on an array nothing else refers to. Every array
// update comes as the pair of the operations of `JsTerm/Ops/Imported.lean` that
// `JsOpImported.toMutable?` relates, at the same signature:
//
// `…_immutable` (pure: returns an updated copy) `…_mutable` (effectful: updates in place)
// `array__lean_array_push_immutable` `array__lean_array_push_mutable`
// `array__lean_array_pop_immutable` `array__lean_array_pop_mutable`
// `<ix>__lean_array_set_immutable` `<ix>__lean_array_set_mutable`
// `<ix>__lean_array_swap_immutable` `<ix>__lean_array_swap_mutable`
// `<ix>__lean_array_fset_immutable` `<ix>__lean_array_fset_mutable`
// `<ix>__lean_array_fswap_immutable` `<ix>__lean_array_fswap_mutable`
//
// where `<ix>` is the representation of the index (`bigint_nat` or `uint53`). `push` / `pop`
// on a typed array (`typedArray__lean_array_push_immutable`, `…_pop_immutable`) have only the
// `_immutable` version: a typed array cannot grow or shrink. A function that may throw is
// one that contains a `throw` or calls one that may: the generator reads that off this file.
//
// Every function gets exactly the representations its name says (a `bigint_nat` argument is
// always a `BigInt`, a `uint53` one always a safe non-negative number, …), so no function
// tests the type of an argument or converts one it does not need to. The only throws are the
// results of a `uint53` / `int53` operation that are not safe integers; a function whose
// result always is one (`uint53__lean_uint64_div`, `int53__lean_int64_abs`, …) does not
// check, and so does not throw. A count too large for memory (`Array.replicate`,
// `String.pushn`) fails in JavaScript's own allocation, as it runs out of memory in Lean.
//
// The `@param` / `@returns` tags of every exported function give the JavaScript type and the
// `JsTy` of its arguments and result, from its signature in `JsTerm/Ops/Imported.lean`; they are written
// by `python3 scripts/annotate_runtime.py` (run it again after changing a signature).
/* ------------------------------------------------------------ private helpers */
const encoder = new TextEncoder();
/** The UTF-8 size of the character of code point `cp`. */
const $cpSize = (cp) => (cp < 128 ? 1 : cp < 2048 ? 2 : cp < 65536 ? 3 : 4);
const $utf8At = (s, p) => {
let off = 0;
for (const ch of s) {
const n = $cpSize(ch.codePointAt(0));
if (off === p) {
return [ch, n];
}
if (off > p) {
return undefined;
}
off = off + n;
}
return undefined;
};
const $utf8 = (s) => encoder.encode(s);
/** The error of a `uint53` / `int53` result that is not a safe integer. */
const $overflow = () => {
throw new RangeError(
"LeanScript: integer overflow: the result does not fit in a number (use the bigint representation)",
);
};
/** A `BigInt` result as a safe integer (one that is not throws). */
const $toNum53 = (x) => (x > 9007199254740991n || x < -9007199254740991n ? $overflow() : Number(x));
/** A number result that must be a safe integer (one that is not throws). */
const $chk53 = (x) => (Number.isSafeInteger(x) ? x : $overflow());
// `a ** e` of a safe integer `a` and a `number` `e >= 0`, exactly, by squaring (`Math.pow` need not be
// exact): every product is checked, and a square is only taken when it is multiplied into the
// result later, so it overflows only when the result does.
const $pow53 = (a, e) => {
let r = 1;
let x = a;
while (e > 0) {
if (e % 2 === 1) {
r = $chk53(r * x);
}
e = Math.floor(e / 2);
if (e > 0) {
x = $chk53(x * x);
}
}
return r;
};
const $utf8Extract = (s, b, e) => {
if (b >= e) {
return "";
}
let off = 0;
let out = "";
let started = false;
for (const ch of s) {
if (!started && off === b) {
started = true;
}
if (started) {
if (off === e) {
return out;
}
out = out + ch;
}
off = off + $cpSize(ch.codePointAt(0));
}
return out;
};
const $utf8Set = (s, p, c) => {
let off = 0;
let i = 0;
for (const ch of s) {
if (off === p) {
return s.slice(0, i) + c + s.slice(i + ch.length);
}
off = off + $cpSize(ch.codePointAt(0));
i = i + ch.length;
}
return s;
};
const $bigPow = (a, b) => {
let r = 1n;
let x = a;
let e = b;
while (e > 0n) {
if ((e & 1n) === 1n) {
r = r * x;
}
e = e >> 1n;
if (e > 0n) {
x = x * x;
}
}
return r;
};
/** `Nat.land` below `2^53`: on the high 21 bits and the low 32 bits apart. */
const $land53 = (a, b) => (((a / 0x100000000) | 0) & ((b / 0x100000000) | 0)) * 0x100000000 + ((a & b) >>> 0);
/** `Nat.lor` below `2^53`. */
const $lor53 = (a, b) => (((a / 0x100000000) | 0) | ((b / 0x100000000) | 0)) * 0x100000000 + ((a | b) >>> 0);
/** `Nat.xor` below `2^53`. */
const $xor53 = (a, b) => (((a / 0x100000000) | 0) ^ ((b / 0x100000000) | 0)) * 0x100000000 + ((a ^ b) >>> 0);
/** `Nat.log2` below `2^53`. */
const $log2_53 = (a) => (a < 0x100000000 ? (a === 0 ? 0 : 31 - Math.clz32(a)) : 63 - Math.clz32(a / 0x100000000));
/** The `BigInt` `x` taken modulo `2^64`. */
const U64 = (x) => BigInt.asUintN(64, x);
/** Lean's hash of a sequence of bytes (`hash_str` of the Lean runtime: MurmurHash64A). */
const $hashBytes = (bytes, seed = 11n) => {
const m = 0xc6a4a7935bd1e995n;
const len = bytes.length;
let h = U64(seed ^ U64(BigInt(len) * m));
const n8 = len - (len % 8);
for (let i = 0; i < n8; i += 8) {
let k = 0n;
for (let j = 7; j >= 0; j--) k = (k << 8n) | BigInt(bytes[i + j]);
k = U64(k * m);
k ^= k >> 47n;
k = U64(k * m);
h ^= k;
h = U64(h * m);
}
if (len % 8 > 0) {
for (let j = (len % 8) - 1; j >= 0; j--) h ^= BigInt(bytes[n8 + j]) << BigInt(8 * j);
h = U64(h * m);
}
h ^= h >> 47n;
h = U64(h * m);
h ^= h >> 47n;
return h;
};
/** `lean_uint64_mix_hash` of the Lean runtime, on `BigInt`s. */
const $mixHash = (h, k) => {
const m = 0xc6a4a7935bd1e995n;
k = U64(k * m);
k ^= k >> 47n;
k ^= m;
h ^= k;
return U64(h * m);
};
/** The byte positions at which the characters of `s` start, and the end position. */
const $starts = (s) => {
const out = [];
let off = 0;
for (const ch of s) {
out.push(off);
off += $cpSize(ch.codePointAt(0));
}
return [out, off];
};
/** `String.Pos.Raw.get`: the character that starts at `p`, or `'A'`. */
const $get = (s, p) => {
const r = $utf8At(s, p);
return r === undefined ? "A" : r[0];
};
/** `String.Pos.Raw.next`: the position after the character at `p` (`p + 1` if none starts there). */
const $next = (s, p) => {
const r = $utf8At(s, p);
return r === undefined ? p + 1 : p + r[1];
};
/** `String.Pos.Raw.prev`: the start of the last character before `p` (`p - 1` past the end). */
const $prev = (s, p) => {
if (p === 0) return 0;
const [st, end] = $starts(s);
if (p > end) return p - 1;
let r = 0;
for (const i of st) {
if (i < p) r = i;
else break;
}
return r;
};
/** Is `b` the first byte of the UTF-8 encoding of a character? */
const $isFirstByte = (b) =>
(b & 0x80) === 0 || (b & 0xe0) === 0xc0 || (b & 0xf0) === 0xe0 || (b & 0xf8) === 0xf0;
/** `String.Pos.Raw.isValid`. */
const $isValid = (s, p) => {
const bytes = encoder.encode(s);
return p < bytes.length ? $isFirstByte(bytes[p]) : p === bytes.length;
};
/** The code point order of two strings (`-1`, `0`, `1`), which is Lean's order on `String`
* (JavaScript's `<` compares UTF-16 code units, which differs above `U+FFFF`). */
const $cmpStr = (a, b) => {
const n = Math.min(a.length, b.length);
for (let i = 0; i < n; i++) {
let x = a.charCodeAt(i);
let y = b.charCodeAt(i);
if (x !== y) {
if (x >= 0xd800 && y >= 0xd800) {
x = x >= 0xe000 ? x - 0x800 : x + 0x2000;
y = y >= 0xe000 ? y - 0x800 : y + 0x2000;
}
return x < y ? -1 : 1;
}
}
return a.length < b.length ? -1 : a.length === b.length ? 0 : 1;
};
/** Whitespace, as Lean's `Char.isWhitespace`. */
const $isWs = (c) => c === " " || c === "\t" || c === "\r" || c === "\n";
/** A double as `m * 2^e` exactly (`m` a `BigInt`, `x` finite and non-negative). */
const $decompose = (x) => {
const dv = new DataView(new ArrayBuffer(8));
dv.setFloat64(0, x);
const bits = dv.getBigUint64(0);
const ex = Number((bits >> 52n) & 0x7ffn);
const frac = bits & 0xfffffffffffffn;
return ex === 0 ? [frac, -1074] : [frac | 0x10000000000000n, ex - 1075];
};
/** `Float.toString`: C's `%f` (six decimals, rounded to nearest, ties to even, of the exact
* value), `NaN`, `inf` and `-inf`. */
const $fmtF6 = (x) => {
if (Number.isNaN(x)) return "NaN";
if (x === Infinity) return "inf";
if (x === -Infinity) return "-inf";
const neg = x < 0 || Object.is(x, -0);
const [m, e] = $decompose(Math.abs(x));
let num = m * 1000000n;
let den = 1n;
if (e >= 0) num <<= BigInt(e);
else den <<= BigInt(-e);
let q = num / den;
const r2 = (num % den) * 2n;
if (r2 > den || (r2 === den && (q & 1n) === 1n)) q += 1n;
const d = q.toString().padStart(7, "0");
return (neg ? "-" : "") + d.slice(0, -6) + "." + d.slice(-6);
};
/** `Float.round`: to the nearest integer, halves away from zero (C's `round`). */
const $round = (x) => (x < 0 ? -Math.round(-x) : Math.round(x));
/** A float truncated towards zero into `[lo, hi]` (`NaN` is `0`), as Lean's conversions of a
* float to a fixed-width integer do (on numbers). */
const $satNum = (x, lo, hi) => (Number.isNaN(x) ? 0 : x <= lo ? lo : x >= hi ? hi : Math.trunc(x) + 0);
/** The same, on `BigInt`s. */
const $satBig = (x, lo, hi) =>
Number.isNaN(x) ? 0n : x <= Number(lo) ? lo : x >= Number(hi) ? hi : BigInt(Math.trunc(x));
/** `frexp`: `[m, e]` with `x = m * 2^e` and `0.5 <= |m| < 1` (`[x, 0]` for zeros, infinities and
* `NaN`). */
const $frexp = (x) => {
if (x === 0 || !Number.isFinite(x)) return [x, 0];
const dv = new DataView(new ArrayBuffer(8));
dv.setFloat64(0, x);
const hi = dv.getUint32(0);
const ex = (hi >>> 20) & 0x7ff;
if (ex === 0) {
const [m, e] = $frexp(x * 2 ** 64);
return [m, e - 64];
}
dv.setUint32(0, ((hi & 0x800fffff) | (1022 << 20)) >>> 0);
return [dv.getFloat64(0), ex - 1022];
};
/** An exponent held as a `BigInt`, as a number clamped to `±5000` (beyond that every scaling
* overflows or underflows alike). */
const $clampExp = (n) => (n > 5000n ? 5000 : n < -5000n ? -5000 : Number(n));
/** `scalbn`: `x * 2^n`, rounded once (musl's algorithm). */
const $scalbn = (x, n) => {
let y = x;
if (n > 1023) {
y *= 2 ** 1023;
n -= 1023;
if (n > 1023) {
y *= 2 ** 1023;
n -= 1023;
if (n > 1023) n = 1023;
}
} else if (n < -1022) {
y *= 2 ** -969;
n += 969;
if (n < -1022) {
y *= 2 ** -969;
n += 969;
if (n < -1022) n = -1022;
}
}
return y * 2 ** n;
};
/** `scalbnf`: the same in single precision (musl's algorithm). */
const $scalbnf = (x, n) => {
const f = Math.fround;
let y = x;
if (n > 127) {
y = f(y * 2 ** 127);
n -= 127;
if (n > 127) {
y = f(y * 2 ** 127);
n -= 127;
if (n > 127) n = 127;
}
} else if (n < -126) {
y = f(y * 2 ** -102);
n += 102;
if (n < -126) {
y = f(y * 2 ** -102);
n += 102;
if (n < -126) n = -126;
}
}
return f(y * 2 ** n);
};
/** A `BigInt` as a `float32`: rounded once, to nearest, ties to even (C's `(float)` cast of a
* 64-bit integer). `Math.fround(Number(x))` would round twice (to a double, then to a
* float), which differs on e.g. `2n ** 63n + 2n ** 39n + 1n`: the double is the midpoint
* `2^63 + 2^39` of two floats, which `Math.fround` rounds to even, down to `2^63`, while the
* integer is above it and rounds up to `2^63 + 2^40`. So a wide `x` is first cut to its 30
* leading bits, the lowest of them set when a dropped bit is (a sticky bit): that number is a
* double exactly and lies on the same side of every midpoint of floats as `x`. */
const $bigToF32 = (x) => {
const neg = x < 0n;
let m = neg ? -x : x;
if (m >= 9007199254740992n) {
const s = BigInt(m.toString(2).length - 30);
const dropped = m & ((1n << s) - 1n);
m = ((m >> s) | (dropped === 0n ? 0n : 1n)) << s;
}
const f = Math.fround(Number(m));
return neg ? -f : f;
};
/** The bits of a double, as a `BigInt`. */
const $toBits = (x) => {
const dv = new DataView(new ArrayBuffer(8));
dv.setFloat64(0, x);
return dv.getBigUint64(0);
};
/** The double of the given bits (a `BigInt`). */
const $ofBits = (b) => {
const dv = new DataView(new ArrayBuffer(8));
dv.setBigUint64(0, b);
return dv.getFloat64(0);
};
/** A `Substring.Raw` or a `String.Slice` `[s, b, e]`, as the string of its bytes `[b, e)`. */
const $sliceBytes = (ss) => encoder.encode(ss[0]).subarray(ss[1], ss[2]);
/** `Substring.Raw.next`: the relative position after the relative position `p`. */
const $subNext = (ss, p) => {
const absP = ss[1] + p;
return absP === ss[2] ? p : $next(ss[0], absP) - ss[1];
};
/* ------------------------------------------------------------ non_configurable */
/** `Array.pop`: a copy without the last element (on a generic array).
* @template α the element type
* @param {Array<α>} a `array α`
* @returns {Array<α>} `array α` */
export const array__lean_array_pop_immutable = (a) => a.slice(0, -1);
/** `Array.pop`, in place (on a generic array only: a typed array cannot shrink).
* @template α the element type
* @param {Array<α>} a `array α`
* @returns {Array<α>} `array α` */
export const array__lean_array_pop_mutable = (a) => {
a.pop();
return a;
};
/** `Array.push`: a copy with one more element (on a generic array).
* @template α the element type
* @param {Array<α>} a `array α`
* @param {α} x `α`
* @returns {Array<α>} `array α` */
export const array__lean_array_push_immutable = (a, x) => [...a, x];
/** `Array.push`, in place (on a generic array only: a typed array cannot grow).
* @template α the element type
* @param {Array<α>} a `array α`
* @param {α} x `α`
* @returns {Array<α>} `array α` */
export const array__lean_array_push_mutable = (a, x) => {
a.push(x);
return a;
};
/** `Array.pop`: a copy without the last element, on a typed array (which cannot shrink, so
* this has no `_mutable` version).
* @template t the typed-array element (`JsTypedElem`); `TypedArray` is `t.kind`
* @param {TypedArray} a `typedArray t`
* @returns {TypedArray} `typedArray t` */
export const typedArray__lean_array_pop_immutable = (a) => a.slice(0, -1);
/** `Array.push`: a copy with one more element, on a typed array (which cannot grow, so this
* has no `_mutable` version).
* @template t the typed-array element (`JsTypedElem`); `TypedArray` is `t.kind`
* @param {TypedArray} a `typedArray t`
* @param {number|bigint} x `t.leaf`
* @returns {TypedArray} `typedArray t` */
export const typedArray__lean_array_push_immutable = (a, x) => {
const r = new a.constructor(a.length + 1);
r.set(a);
r[a.length] = x;
return r;
};
/** `Int16.ofInt`.
* @param {bigint} a `bigint_int`
* @returns {number} `int16` */
export const bigint_int__lean_int16_of_int = (a) => Number(BigInt.asIntN(16, a));
/** `Int32.ofInt`.
* @param {bigint} a `bigint_int`
* @returns {number} `int32` */
export const bigint_int__lean_int32_of_int = (a) => Number(BigInt.asIntN(32, a));
/** `Int8.ofInt`.
* @param {bigint} a `bigint_int`
* @returns {number} `int8` */
export const bigint_int__lean_int8_of_int = (a) => Number(BigInt.asIntN(8, a));
/** `Int64.toInt16`.
* @param {bigint} a `bigint_int`
* @returns {number} `int16` */
export const bigint_int__lean_int64_to_int16 = (a) => Number(BigInt.asIntN(16, a));
/** `Int64.toInt32`.
* @param {bigint} a `bigint_int`
* @returns {number} `int32` */
export const bigint_int__lean_int64_to_int32 = (a) => Number(BigInt.asIntN(32, a));
/** `Int64.toInt8`.
* @param {bigint} a `bigint_int`
* @returns {number} `int8` */
export const bigint_int__lean_int64_to_int8 = (a) => Number(BigInt.asIntN(8, a));
/** `Int16.ofInt` (`ToInt32` takes a safe integer modulo `2^32`).
* @param {number} a `int53`
* @returns {number} `int16` */
export const int53__lean_int16_of_int = (a) => (a << 16) >> 16;
/** `Int32.ofInt`.
* @param {number} a `int53`
* @returns {number} `int32` */
export const int53__lean_int32_of_int = (a) => a | 0;
/** `Int8.ofInt`.
* @param {number} a `int53`
* @returns {number} `int8` */
export const int53__lean_int8_of_int = (a) => (a << 24) >> 24;
/** `Int64.toInt16`.
* @param {number} a `int53`
* @returns {number} `int16` */
export const int53__lean_int64_to_int16 = (a) => (a << 16) >> 16;
/** `Int64.toInt32`.
* @param {number} a `int53`
* @returns {number} `int32` */
export const int53__lean_int64_to_int32 = (a) => a | 0;
/** `Int64.toInt8`.
* @param {number} a `int53`
* @returns {number} `int8` */
export const int53__lean_int64_to_int8 = (a) => (a << 24) >> 24;
/** `Array.get!Internal`: `a[i]`, or the default `d` out of bounds.
* @template A, E the array layout `l : JsArrayLayout A E`: `A` is `array E`, or `typedArray t` with `E` = `terminal t.leaf`
* @param {E} d `E`
* @param {Array<E>|TypedArray} a `A`
* @param {bigint} i `bigint_nat`
* @returns {E} `E` */
export const bigint_nat__lean_array_get = (d, a, i) => {
const k = Number(i);
return k < a.length ? a[k] : d;
};
/** `Array.set!`: a copy with one element replaced (the array itself out of bounds).
* @template A, E the array layout `l : JsArrayLayout A E`: `A` is `array E`, or `typedArray t` with `E` = `terminal t.leaf`
* @param {Array<E>|TypedArray} a `A`
* @param {bigint} i `bigint_nat`
* @param {E} x `E`
* @returns {Array<E>|TypedArray} `A` */
export const bigint_nat__lean_array_set_immutable = (a, i, x) => {
const k = Number(i);
if (k >= a.length) return a;
const r = a.slice();
r[k] = x;
return r;
};
/** `Array.set!`, in place.
* @template A, E the array layout `l : JsArrayLayout A E`: `A` is `array E`, or `typedArray t` with `E` = `terminal t.leaf`
* @param {Array<E>|TypedArray} a `A`
* @param {bigint} i `bigint_nat`
* @param {E} x `E`
* @returns {Array<E>|TypedArray} `A` */
export const bigint_nat__lean_array_set_mutable = (a, i, x) => {
const k = Number(i);
if (k < a.length) a[k] = x;
return a;
};
/** `Array.swapIfInBounds`: a copy with two elements swapped (the array itself out of bounds).
* @template A, E the array layout `l : JsArrayLayout A E`: `A` is `array E`, or `typedArray t` with `E` = `terminal t.leaf`
* @param {Array<E>|TypedArray} a `A`
* @param {bigint} i `bigint_nat`
* @param {bigint} j `bigint_nat`
* @returns {Array<E>|TypedArray} `A` */
export const bigint_nat__lean_array_swap_immutable = (a, i, j) => {
const k = Number(i);
const l = Number(j);
if (k >= a.length || l >= a.length) return a;
const r = a.slice();
const t = r[k];
r[k] = r[l];
r[l] = t;
return r;
};
/** `Array.swapIfInBounds`, in place.
* @template A, E the array layout `l : JsArrayLayout A E`: `A` is `array E`, or `typedArray t` with `E` = `terminal t.leaf`
* @param {Array<E>|TypedArray} a `A`
* @param {bigint} i `bigint_nat`
* @param {bigint} j `bigint_nat`
* @returns {Array<E>|TypedArray} `A` */
export const bigint_nat__lean_array_swap_mutable = (a, i, j) => {
const k = Number(i);
const l = Number(j);
if (k < a.length && l < a.length) {
const t = a[k];
a[k] = a[l];
a[l] = t;
}
return a;
};
/** `Array.set`: a copy with one element replaced (the bound `i < a.size` is proved, so it is
* not checked).
* @template A, E the array layout `l : JsArrayLayout A E`: `A` is `array E`, or `typedArray t` with `E` = `terminal t.leaf`
* @param {Array<E>|TypedArray} a `A`
* @param {bigint} i `bigint_nat`
* @param {E} x `E`
* @returns {Array<E>|TypedArray} `A` */
export const bigint_nat__lean_array_fset_immutable = (a, i, x) => {
const r = a.slice();
r[Number(i)] = x;
return r;
};
/** `Array.set`, in place (the bound is proved).
* @template A, E the array layout `l : JsArrayLayout A E`: `A` is `array E`, or `typedArray t` with `E` = `terminal t.leaf`
* @param {Array<E>|TypedArray} a `A`
* @param {bigint} i `bigint_nat`
* @param {E} x `E`
* @returns {Array<E>|TypedArray} `A` */
export const bigint_nat__lean_array_fset_mutable = (a, i, x) => {
a[Number(i)] = x;
return a;
};
/** `Array.swap`: a copy with two elements swapped (the bounds are proved, so they are not
* checked).
* @template A, E the array layout `l : JsArrayLayout A E`: `A` is `array E`, or `typedArray t` with `E` = `terminal t.leaf`
* @param {Array<E>|TypedArray} a `A`
* @param {bigint} i `bigint_nat`
* @param {bigint} j `bigint_nat`
* @returns {Array<E>|TypedArray} `A` */
export const bigint_nat__lean_array_fswap_immutable = (a, i, j) => {
const k = Number(i);
const l = Number(j);
const r = a.slice();
const t = r[k];
r[k] = r[l];
r[l] = t;
return r;
};
/** `Array.swap`, in place (the bounds are proved).
* @template A, E the array layout `l : JsArrayLayout A E`: `A` is `array E`, or `typedArray t` with `E` = `terminal t.leaf`
* @param {Array<E>|TypedArray} a `A`
* @param {bigint} i `bigint_nat`
* @param {bigint} j `bigint_nat`
* @returns {Array<E>|TypedArray} `A` */
export const bigint_nat__lean_array_fswap_mutable = (a, i, j) => {
const k = Number(i);
const l = Number(j);
const t = a[k];
a[k] = a[l];
a[l] = t;
return a;
};
/** `Int16.ofNat`.
* @param {bigint} a `bigint_nat`
* @returns {number} `int16` */
export const bigint_nat__lean_int16_of_nat = bigint_int__lean_int16_of_int;
/** `Int32.ofNat`.
* @param {bigint} a `bigint_nat`
* @returns {number} `int32` */
export const bigint_nat__lean_int32_of_nat = bigint_int__lean_int32_of_int;
/** `Int8.ofNat`.
* @param {bigint} a `bigint_nat`
* @returns {number} `int8` */
export const bigint_nat__lean_int8_of_nat = bigint_int__lean_int8_of_int;
/** `Array.replicate`, on a generic array.
* @template α the element type
* @param {bigint} n `bigint_nat`
* @param {α} v `α`
* @returns {Array<α>} `array α` */
export const bigint_nat__lean_mk_array = (n, v) => new Array(Number(n)).fill(v);
/** `String.Internal.pushn`.
* @param {string} a `string`
* @param {string} b `string`
* @param {bigint} c `bigint_nat`
* @returns {string} `string` */
export const bigint_nat__lean_string_pushn = (a, b, c) => a + b.repeat(Number(c));
/** `UInt16.ofNat`, `UInt16.ofNatLT`.
* @param {bigint} a `bigint_nat`
* @returns {number} `uint16` */
export const bigint_nat__lean_uint16_of_nat__UInt16_ofNat = (a) => Number(BigInt.asUintN(16, a));
/** `Char.ofNatAux`.
* @param {bigint} a `bigint_nat`
* @returns {string} `string` */
export const bigint_nat__lean_uint32_of_nat__Char_ofNatAux = (a) => String.fromCodePoint(Number(a));
/** `UInt32.ofNat`, `UInt32.ofNatLT`.
* @param {bigint} a `bigint_nat`
* @returns {number} `uint32` */
export const bigint_nat__lean_uint32_of_nat__UInt32_ofNat = (a) => Number(BigInt.asUintN(32, a));
/** `UInt64.toUInt16`.
* @param {bigint} a `bigint_nat`
* @returns {number} `uint16` */
export const bigint_nat__lean_uint64_to_uint16 = (a) => Number(BigInt.asUintN(16, a));
/** `UInt64.toUInt32`.
* @param {bigint} a `bigint_nat`
* @returns {number} `uint32` */
export const bigint_nat__lean_uint64_to_uint32 = (a) => Number(BigInt.asUintN(32, a));
/** `UInt64.toUInt8`.
* @param {bigint} a `bigint_nat`
* @returns {number} `uint8` */
export const bigint_nat__lean_uint64_to_uint8 = (a) => Number(BigInt.asUintN(8, a));
/** `UInt8.ofNat`, `UInt8.ofNatLT`.
* @param {bigint} a `bigint_nat`
* @returns {number} `uint8` */
export const bigint_nat__lean_uint8_of_nat__UInt8_ofNat = (a) => Number(BigInt.asUintN(8, a));
/** `Bool.toInt16`.
* @param {boolean} a `bool`
* @returns {number} `int16` */
export const bool__lean_bool_to_int16 = (a) => a ? 1 : 0;
/** `Bool.toInt32`.
* @param {boolean} a `bool`
* @returns {number} `int32` */
export const bool__lean_bool_to_int32 = (a) => a ? 1 : 0;
/** `Bool.toInt8`.
* @param {boolean} a `bool`
* @returns {number} `int8` */
export const bool__lean_bool_to_int8 = (a) => a ? 1 : 0;
/** `Bool.toUInt16`.
* @param {boolean} a `bool`
* @returns {number} `uint16` */
export const bool__lean_bool_to_uint16 = (a) => a ? 1 : 0;
/** `Bool.toUInt32`.
* @param {boolean} a `bool`
* @returns {number} `uint32` */
export const bool__lean_bool_to_uint32 = (a) => a ? 1 : 0;
/** `Bool.toUInt8`.
* @param {boolean} a `bool`
* @returns {number} `uint8` */
export const bool__lean_bool_to_uint8 = (a) => a ? 1 : 0;
/** `Float32.add`.
* @param {number} a `float32`
* @param {number} b `float32`
* @returns {number} `float32` */
export const float32__lean_float32_add = (a, b) => Math.fround(a + b);
/** `Float32.div`.
* @param {number} a `float32`
* @param {number} b `float32`
* @returns {number} `float32` */
export const float32__lean_float32_div = (a, b) => Math.fround(a / b);
/** `Float32.isFinite`.
* @param {number} a `float32`
* @returns {boolean} `bool` */
export const float32__lean_float32_isfinite = (a) => Number.isFinite(a);
/** `Float32.isInf`.
* @param {number} a `float32`
* @returns {boolean} `bool` */
export const float32__lean_float32_isinf = (a) => a === Infinity || a === -Infinity;
/** `Float32.isNaN`.
* @param {number} a `float32`
* @returns {boolean} `bool` */
export const float32__lean_float32_isnan = (a) => Number.isNaN(a);
/** `Float32.mul`.
* @param {number} a `float32`
* @param {number} b `float32`
* @returns {number} `float32` */
export const float32__lean_float32_mul = (a, b) => Math.fround(a * b);
/** `Float32.sub`.
* @param {number} a `float32`
* @param {number} b `float32`
* @returns {number} `float32` */
export const float32__lean_float32_sub = (a, b) => Math.fround(a - b);
/** `Float.isFinite`.
* @param {number} a `float`
* @returns {boolean} `bool` */
export const float__lean_float_isfinite = (a) => Number.isFinite(a);
/** `Float.isInf`.
* @param {number} a `float`
* @returns {boolean} `bool` */
export const float__lean_float_isinf = (a) => a === Infinity || a === -Infinity;
/** `Float.isNaN`.
* @param {number} a `float`
* @returns {boolean} `bool` */
export const float__lean_float_isnan = (a) => Number.isNaN(a);
/** `Float.toFloat32`.
* @param {number} a `float`
* @returns {number} `float32` */
export const float__lean_float_to_float32 = (a) => Math.fround(a);
/** `UInt64.toFloat32` (on `BigInt`s).
* @param {bigint} a `bigint_nat`
* @returns {number} `float32` */
export const bigint_nat__lean_uint64_to_float32 = (a) => $bigToF32(a);
/** `Int64.toFloat32` (on `BigInt`s).
* @param {bigint} a `bigint_int`
* @returns {number} `float32` */
export const bigint_int__lean_int64_to_float32 = (a) => $bigToF32(a);
/** `Int16.abs`.
* @param {number} a `int16`
* @returns {number} `int16` */
export const int16__lean_int16_abs = (a) => (Math.abs(a) << 16) >> 16;
/** `Int16.add`.
* @param {number} a `int16`
* @param {number} b `int16`
* @returns {number} `int16` */
export const int16__lean_int16_add = (a, b) => ((a + b) << 16) >> 16;
/** `Int16.complement`.
* @param {number} a `int16`
* @returns {number} `int16` */
export const int16__lean_int16_complement = (a) => (~a << 16) >> 16;
/** `Int16.div`.
* @param {number} a `int16`
* @param {number} b `int16`
* @returns {number} `int16` */
export const int16__lean_int16_div = (a, b) => b === 0 ? 0 : (Math.trunc(a / b) << 16) >> 16;
/** `Int16.land`.
* @param {number} a `int16`
* @param {number} b `int16`
* @returns {number} `int16` */
export const int16__lean_int16_land = (a, b) => ((a & b) << 16) >> 16;
/** `Int16.lor`.
* @param {number} a `int16`
* @param {number} b `int16`
* @returns {number} `int16` */
export const int16__lean_int16_lor = (a, b) => ((a | b) << 16) >> 16;
/** `Int16.mod`.
* @param {number} a `int16`
* @param {number} b `int16`
* @returns {number} `int16` */
export const int16__lean_int16_mod = (a, b) => b === 0 ? a : a % b;
/** `Int16.mul`.
* @param {number} a `int16`
* @param {number} b `int16`
* @returns {number} `int16` */
export const int16__lean_int16_mul = (a, b) => ((a * b) << 16) >> 16;
/** `Int16.neg`.
* @param {number} a `int16`
* @returns {number} `int16` */
export const int16__lean_int16_neg = (a) => (-a << 16) >> 16;
/** `Int16.shiftLeft`.
* @param {number} a `int16`
* @param {number} b `int16`
* @returns {number} `int16` */
export const int16__lean_int16_shift_left = (a, b) => ((a << (((b % 16) + 16) % 16)) << 16) >> 16;
/** `Int16.shiftRight`.
* @param {number} a `int16`
* @param {number} b `int16`
* @returns {number} `int16` */
export const int16__lean_int16_shift_right = (a, b) => a >> (((b % 16) + 16) % 16);
/** `Int16.sub`.
* @param {number} a `int16`
* @param {number} b `int16`
* @returns {number} `int16` */
export const int16__lean_int16_sub = (a, b) => ((a - b) << 16) >> 16;
/** `Int16.toInt8`.
* @param {number} a `int16`
* @returns {number} `int8` */
export const int16__lean_int16_to_int8 = (a) => (a << 24) >> 24;
/** `Int16.xor`.
* @param {number} a `int16`
* @param {number} b `int16`
* @returns {number} `int16` */
export const int16__lean_int16_xor = (a, b) => ((a ^ b) << 16) >> 16;
/** `Int32.abs`.
* @param {number} a `int32`
* @returns {number} `int32` */
export const int32__lean_int32_abs = (a) => Math.abs(a) | 0;
/** `Int32.add`.
* @param {number} a `int32`
* @param {number} b `int32`
* @returns {number} `int32` */
export const int32__lean_int32_add = (a, b) => (a + b) | 0;
/** `Int32.complement`.
* @param {number} a `int32`
* @returns {number} `int32` */
export const int32__lean_int32_complement = (a) => ~a | 0;
/** `Int32.div`.
* @param {number} a `int32`
* @param {number} b `int32`
* @returns {number} `int32` */
export const int32__lean_int32_div = (a, b) => b === 0 ? 0 : Math.trunc(a / b) | 0;
/** `Int32.land`.
* @param {number} a `int32`
* @param {number} b `int32`
* @returns {number} `int32` */
export const int32__lean_int32_land = (a, b) => (a & b) | 0;
/** `Int32.lor`.
* @param {number} a `int32`
* @param {number} b `int32`
* @returns {number} `int32` */
export const int32__lean_int32_lor = (a, b) => a | b | 0;
/** `Int32.mod`.
* @param {number} a `int32`
* @param {number} b `int32`
* @returns {number} `int32` */
export const int32__lean_int32_mod = (a, b) => b === 0 ? a : a % b;
/** `Int32.mul`.
* @param {number} a `int32`
* @param {number} b `int32`
* @returns {number} `int32` */
export const int32__lean_int32_mul = (a, b) => Math.imul(a, b) | 0;
/** `Int32.neg`.
* @param {number} a `int32`
* @returns {number} `int32` */
export const int32__lean_int32_neg = (a) => -a | 0;
/** `Int32.shiftLeft`.
* @param {number} a `int32`
* @param {number} b `int32`
* @returns {number} `int32` */
export const int32__lean_int32_shift_left = (a, b) => (a << (((b % 32) + 32) % 32)) | 0;
/** `Int32.shiftRight`.
* @param {number} a `int32`
* @param {number} b `int32`
* @returns {number} `int32` */
export const int32__lean_int32_shift_right = (a, b) => a >> (((b % 32) + 32) % 32);
/** `Int32.sub`.
* @param {number} a `int32`
* @param {number} b `int32`
* @returns {number} `int32` */
export const int32__lean_int32_sub = (a, b) => (a - b) | 0;
/** `Int32.toInt16`.
* @param {number} a `int32`
* @returns {number} `int16` */
export const int32__lean_int32_to_int16 = (a) => (a << 16) >> 16;
/** `Int32.toInt8`.
* @param {number} a `int32`
* @returns {number} `int8` */
export const int32__lean_int32_to_int8 = (a) => (a << 24) >> 24;
/** `Int32.xor`.
* @param {number} a `int32`
* @param {number} b `int32`
* @returns {number} `int32` */
export const int32__lean_int32_xor = (a, b) => (a ^ b) | 0;
/** `Int8.abs`.
* @param {number} a `int8`
* @returns {number} `int8` */
export const int8__lean_int8_abs = (a) => (Math.abs(a) << 24) >> 24;
/** `Int8.add`.
* @param {number} a `int8`
* @param {number} b `int8`
* @returns {number} `int8` */
export const int8__lean_int8_add = (a, b) => ((a + b) << 24) >> 24;
/** `Int8.complement`.
* @param {number} a `int8`
* @returns {number} `int8` */
export const int8__lean_int8_complement = (a) => (~a << 24) >> 24;
/** `Int8.div`.