@fraczak/k 8.7.0 → 8.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/CHANGELOG.md +40 -8
  2. package/DOCS/CODECS.md +42 -40
  3. package/DOCS/FILE_FORMATS.md +41 -64
  4. package/DOCS/GETTING_STARTED.md +10 -17
  5. package/DOCS/PATTERNS.md +3 -10
  6. package/DOCS/REPL.md +5 -9
  7. package/Examples/ieee-arithmetics.k +14 -29
  8. package/Examples/poly.k +18 -29
  9. package/README.md +14 -18
  10. package/backend-api.mjs +1 -3
  11. package/backends/llvm/src/cli.mjs +7 -11
  12. package/backends/llvm/tests/integration.sh +2 -4
  13. package/backends/wasm/src/wasm.mjs +2 -6
  14. package/backends/wasm/tests/integration.sh +17 -7
  15. package/browser/shims/fs.mjs +2 -6
  16. package/canonical-pattern.mjs +9 -32
  17. package/compiler.mjs +36 -0
  18. package/harness/suites/poly.mjs +9 -11
  19. package/index.mjs +140 -6
  20. package/k.mjs +4 -12
  21. package/kir.mjs +3 -1
  22. package/kvm.mjs +4 -8
  23. package/lib/djson.k +981 -294
  24. package/lib/generic.k +701 -0
  25. package/lib/ieee.k +662 -638
  26. package/lib/integer.k +542 -0
  27. package/named-patterns.mjs +0 -12
  28. package/object.mjs +22 -32
  29. package/objects/README.md +6 -7
  30. package/objects/compile.mjs +5 -10
  31. package/package.json +1 -1
  32. package/repl.mjs +4 -7
  33. package/scripts/benchmark-idempotency.mjs +16 -17
  34. package/scripts/benchmark.mjs +39 -29
  35. package/scripts/build-repl-html.mjs +45 -35
  36. package/scripts/perf-ieee.mjs +1 -5
  37. package/scripts/perf-int.mjs +12 -27
  38. package/scripts/perf-multiplication.mjs +8 -9
  39. package/scripts/perf-poly.mjs +7 -10
  40. package/scripts/render-benchmark.mjs +1 -1
  41. package/scripts/run-tests.mjs +5 -2
  42. package/scripts/test-package.mjs +6 -7
  43. package/tests/README.md +4 -5
  44. package/tests/helpers/cli.mjs +31 -0
  45. package/tests/integration.sh +17 -7
  46. package/tests/test-arithmetics.mjs +23 -16
  47. package/tests/test-djson-methods.mjs +19 -22
  48. package/tests/test-djson.mjs +273 -0
  49. package/tests/test-generic.mjs +239 -0
  50. package/tests/test-ieee-arithmetic.mjs +1 -5
  51. package/tests/test-ieee.mjs +153 -0
  52. package/tests/test-integer.mjs +264 -0
  53. package/tests/test-k-object.mjs +11 -14
  54. package/tests/test-kvm-polymorphic.mjs +7 -4
  55. package/tests/test-lib.mjs +62 -50
  56. package/tests/test-library-isolation.mjs +704 -0
  57. package/tests/test-named-patterns.mjs +13 -4
  58. package/tests/test-parse-json.mjs +20 -19
  59. package/tests/test-poly.mjs +21 -16
  60. package/tests/test-repl-html.mjs +39 -44
  61. package/tests/test-repl.mjs +46 -59
  62. package/DOCS/book-combined.md +0 -3786
  63. package/DOCS/dependency-graph.md +0 -144
  64. package/Examples/arithmetics.k +0 -108
  65. package/Examples/esc.k +0 -9
  66. package/lib/arithmetics.k +0 -287
  67. package/lib/bit.k +0 -68
  68. package/lib/bits.k +0 -221
  69. package/lib/btrie.k +0 -187
  70. package/lib/list.k +0 -49
  71. package/lib/string.k +0 -588
  72. package/tests/test-ieee-text.mjs +0 -70
  73. package/tests/test-karatsuba.mjs +0 -25
package/lib/integer.k ADDED
@@ -0,0 +1,542 @@
1
+ -- =============================================================================
2
+ -- lib/integer.k -- Signed Arbitrary-Precision Integer Arithmetic
3
+ --
4
+ -- Self-contained module (zero dependencies) representing arbitrary-precision
5
+ -- signed integers using canonical sign-magnitude representation:
6
+ -- int[] = < bits[] "+", bits[] "-" >
7
+ --
8
+ -- Canonical invariants & style conventions:
9
+ -- * Public bits[] values store MSB at the root and '_' at the leaf.
10
+ -- * No redundant leading zero bits except canonical zero: + (_ |0).
11
+ -- * Zero is always normalized to '+'; there is no negative zero.
12
+ -- * Relations ending in '?' are pure identity filters (?X -> ?X).
13
+ -- * All recursive relations are tail-recursive.
14
+ -- * Internal helpers are prefixed with '_' and follow their primary relation;
15
+ -- discarded product fields are prefixed with '_' (e.g. _x, _ord).
16
+ --
17
+ -- Internal unsigned addition, subtraction, multiplication (Karatsuba), and
18
+ -- binary long division operate on trimmed LSB-first bit sequences ('_' = 0),
19
+ -- converting at the boundary so multi-limb arithmetic avoids reversal inside
20
+ -- recursion.
21
+ -- =============================================================================
22
+
23
+
24
+ -- -----------------------------------------------------------------------------
25
+ -- 1. Core Types, Constants, Normalization, and Predicates
26
+ -- -----------------------------------------------------------------------------
27
+
28
+ bit[] = < {} 0, {} 1 >;
29
+ bits[] = < {} _, B 0, B 1 > = B;
30
+
31
+ order[] = < {} lt, {} eq, {} gt >;
32
+
33
+ int[] = < bits[] "+", bits[] "-" >;
34
+
35
+ 0 = {} |_ |0 |+;
36
+ 1 = {} |_ |1 |+;
37
+
38
+ -- Strips redundant leading '0' tags while keeping at least one bit for zero (_ |0).
39
+ trim_bits = ?bits[] <
40
+ /_ |_ |0,
41
+ /1 |1,
42
+ /0 /_ |_ |0,
43
+ /0 trim_bits
44
+ > ?bits[];
45
+
46
+ -- Drops all leading '0' tags (canonical zero becomes empty '_').
47
+ msb_drop_zeros = ?bits[] <
48
+ /_ |_,
49
+ /1 |1,
50
+ /0 msb_drop_zeros
51
+ > ?bits[];
52
+
53
+ -- Lifts unsigned bits[] magnitude (MSB at root, '_' at leaf) to canonical non-negative int[].
54
+ nat = ?bits[] trim_bits |+ ?int[];
55
+
56
+ -- Canonicalizes a signed int[] (strips leading zeros and normalizes -0 to +0).
57
+ int = ?int[] <
58
+ /+ trim_bits |+,
59
+ /- trim_bits <
60
+ /0 /_ |_ |0 |+,
61
+ /1 |1 |-
62
+ >
63
+ > ?int[];
64
+
65
+ -- Identity filters (succeed unchanged on matching values, fail otherwise).
66
+ zero? = int { /+ /0 /_ _x, () x } .x ?int[];
67
+ pos? = int /+ /1 |1 |+ ?int[];
68
+ neg? = int /- |- ?int[];
69
+
70
+ -- Unsigned normalization to/from internal trimmed LSB-first bits[] ('_' = 0).
71
+ rev_bits = ?bits[] { () in, {} |_ acc } _rev_bits ?bits[];
72
+ _rev_bits = ?{ bits[] in, bits[] acc } <
73
+ { .in /_ _in, .acc acc } .acc,
74
+ <
75
+ { .in /0 in, .acc |0 acc },
76
+ { .in /1 in, .acc |1 acc }
77
+ > _rev_bits
78
+ > ?bits[];
79
+
80
+ lsb_trim = ?bits[] rev_bits msb_drop_zeros rev_bits ?bits[];
81
+ msb_to_lsb = ?bits[] msb_drop_zeros rev_bits ?bits[];
82
+ lsb_to_msb = ?bits[] rev_bits trim_bits ?bits[];
83
+
84
+
85
+ -- -----------------------------------------------------------------------------
86
+ -- 2. Magnitude Comparison & Bit-Shift Helpers
87
+ -- -----------------------------------------------------------------------------
88
+
89
+ -- Single-pass MSB-first comparison of two canonical trimmed bits[] values.
90
+ -- Accumulates first differing bit as `ord`; longer length strictly wins.
91
+ cmp_msb = ?{ bits[] x, bits[] y } { .x x, .y y, {} |eq ord } _cmp_msb ?order[];
92
+ _cmp_msb = ?{ bits[] x, bits[] y, order[] ord } <
93
+ { .x /_ _x, .y /_ _y, .ord ord } .ord,
94
+ .x /_ |lt,
95
+ .y /_ |gt,
96
+ <
97
+ { .x /0 x, .y /0 y, .ord ord },
98
+ { .x /1 x, .y /1 y, .ord ord },
99
+ { .x /0 x, .y /1 y, .ord </eq |lt, ()> ord },
100
+ { .x /1 x, .y /0 y, .ord </eq |gt, ()> ord }
101
+ > _cmp_msb
102
+ > ?order[];
103
+
104
+ -- Shifts a trimmed LSB-first bits[] left by 1 bit or by a tally `k`.
105
+ lsb_shl1 = ?bits[] < /_ |_, /0 |0 |0, /1 |1 |0 > ?bits[];
106
+
107
+ lsb_shl = ?{ bits[] x, bits[] k } <
108
+ { .x /_ _x, .k _k } {} |_,
109
+ { .x < /0 |0, /1 |1 > x, .k /_ _k } .x,
110
+ { .x < /0 |0, /1 |1 > x, .k < /0, /1 > k } { .x |0 x, .k k } lsb_shl
111
+ > ?bits[];
112
+
113
+
114
+ -- -----------------------------------------------------------------------------
115
+ -- 3. Unsigned LSB Addition & Subtraction
116
+ -- -----------------------------------------------------------------------------
117
+
118
+ lsb_add = ?{ bits[] x, bits[] y } {
119
+ .x x, .y y, {} |0 c, {} |_ acc
120
+ } _lsb_add msb_drop_zeros rev_bits ?bits[];
121
+
122
+ -- Tail-recursive ripple-carry addition accumulating MSB-first in `acc`.
123
+ _lsb_add = ?{ bits[] x, bits[] y, bit[] c, bits[] acc } <
124
+ { .x /_ _x, .y /_ _y, .c /0 _c, .acc acc } .acc,
125
+ { .x /_ _x, .y /_ _y, .c /1 _c, .acc acc } .acc |1,
126
+ { .x /_ _x, .y < /0 |0, /1 |1 > y, .c /0 _c, .acc acc }
127
+ { .y in, .acc acc } _rev_bits,
128
+ { .x < /0 |0, /1 |1 > x, .y /_ _y, .c /0 _c, .acc acc }
129
+ { .x in, .acc acc } _rev_bits,
130
+ { .x _pop_lsb_bit px, .y _pop_lsb_bit py, .c c, .acc acc } {
131
+ .px .rest x,
132
+ .py .rest y,
133
+ { .px .b a, .py .b b, .c c } _full_add_bit s,
134
+ .acc acc
135
+ } {
136
+ .x x,
137
+ .y y,
138
+ .s .carry c,
139
+ { .s .sum b, .acc acc } _push_bit_acc acc
140
+ } _lsb_add
141
+ > ?bits[];
142
+
143
+ _full_add_bit = ?{ bit[] a, bit[] b, bit[] c } <
144
+ { .a /0 _a, .b /0 _b, .c /0 _c } { {} |0 sum, {} |0 carry },
145
+ { .a /0 _a, .b /0 _b, .c /1 _c } { {} |1 sum, {} |0 carry },
146
+ { .a /0 _a, .b /1 _b, .c /0 _c } { {} |1 sum, {} |0 carry },
147
+ { .a /0 _a, .b /1 _b, .c /1 _c } { {} |0 sum, {} |1 carry },
148
+ { .a /1 _a, .b /0 _b, .c /0 _c } { {} |1 sum, {} |0 carry },
149
+ { .a /1 _a, .b /0 _b, .c /1 _c } { {} |0 sum, {} |1 carry },
150
+ { .a /1 _a, .b /1 _b, .c /0 _c } { {} |0 sum, {} |1 carry },
151
+ { .a /1 _a, .b /1 _b, .c /1 _c } { {} |1 sum, {} |1 carry }
152
+ > ?{ bit[] sum, bit[] carry };
153
+
154
+ -- Subtracts y from x assuming x >= y (fails if underflow occurs).
155
+ lsb_sub = ?{ bits[] x, bits[] y }
156
+ { .x x, .y y, {} |0 b, {} |_ acc } _lsb_sub { .b /0 _b, .r r } .r ?bits[];
157
+
158
+ -- Tries x - y: returns { b: 0, r: x - y } if x >= y, or { b: 1, r: _ } if x < y.
159
+ lsb_try_sub = ?{ bits[] x, bits[] y }
160
+ { .x x, .y y, {} |0 b, {} |_ acc } _lsb_sub ?{ bit[] b, bits[] r };
161
+
162
+ -- Tail-recursive ripple-borrow subtraction on trimmed LSB-first bits[].
163
+ -- Short-circuits immediately with borrow=1 when x reaches '_' before non-empty y or borrow=1.
164
+ _lsb_sub = ?{ bits[] x, bits[] y, bit[] b, bits[] acc } <
165
+ { .x /_ _x, .y /_ _y, .b /0 _b, .acc acc }
166
+ { {} |0 b, .acc msb_drop_zeros rev_bits r },
167
+ .x /_ { {} |1 b, {} |_ r },
168
+ { .x < /0 |0, /1 |1 > x, .y /_ _y, .b /0 _b, .acc acc } {
169
+ {} |0 b,
170
+ { .x in, .acc acc } _rev_bits msb_drop_zeros rev_bits r
171
+ },
172
+ { .x < /0 |0, /1 |1 > _pop_lsb_bit px, .y _pop_lsb_bit py, .b b, .acc acc } {
173
+ .px .rest x,
174
+ .py .rest y,
175
+ { .px .b a, .py .b b, .b c } _full_sub_bit s,
176
+ .acc acc
177
+ } {
178
+ .x x,
179
+ .y y,
180
+ .s .borrow b,
181
+ { .s .diff b, .acc acc } _push_bit_acc acc
182
+ } _lsb_sub
183
+ > ?{ bit[] b, bits[] r };
184
+
185
+ _full_sub_bit = ?{ bit[] a, bit[] b, bit[] c } <
186
+ { .a /0 _a, .b /0 _b, .c /0 _c } { {} |0 diff, {} |0 borrow },
187
+ { .a /0 _a, .b /0 _b, .c /1 _c } { {} |1 diff, {} |1 borrow },
188
+ { .a /0 _a, .b /1 _b, .c /0 _c } { {} |1 diff, {} |1 borrow },
189
+ { .a /0 _a, .b /1 _b, .c /1 _c } { {} |0 diff, {} |1 borrow },
190
+ { .a /1 _a, .b /0 _b, .c /0 _c } { {} |1 diff, {} |0 borrow },
191
+ { .a /1 _a, .b /0 _b, .c /1 _c } { {} |0 diff, {} |0 borrow },
192
+ { .a /1 _a, .b /1 _b, .c /0 _c } { {} |0 diff, {} |0 borrow },
193
+ { .a /1 _a, .b /1 _b, .c /1 _c } { {} |1 diff, {} |1 borrow }
194
+ > ?{ bit[] diff, bit[] borrow };
195
+
196
+ _pop_lsb_bit = ?bits[] <
197
+ /_ { {} |0 b, {} |_ rest },
198
+ /0 { {} |0 b, () rest },
199
+ /1 { {} |1 b, () rest }
200
+ > ?{ bit[] b, bits[] rest };
201
+
202
+ _push_bit_acc = ?{ bit[] b, bits[] acc } <
203
+ { .b /0 _b, .acc acc } .acc |0,
204
+ { .b /1 _b, .acc acc } .acc |1
205
+ > ?bits[];
206
+
207
+
208
+ -- -----------------------------------------------------------------------------
209
+ -- 4. Fast Multiplication (Karatsuba + Schoolbook Base Case)
210
+ -- -----------------------------------------------------------------------------
211
+ -- Algorithm: Karatsuba multiplication (Anatolii Karatsuba, 1960).
212
+ -- For operands above the 16-bit cutoff, each operand is split at half the
213
+ -- maximum bit-width `k`:
214
+ -- x = x1 * 2^k + x0, y = y1 * 2^k + y0
215
+ -- Three recursive half-size products suffice:
216
+ -- z0 = x0 * y0
217
+ -- z2 = x1 * y1
218
+ -- z1 = (x0 + x1) * (y0 + y1) - z2 - z0
219
+ -- x * y = z0 + z1 * 2^k + z2 * 2^(2k)
220
+ -- Asymptotic complexity improves from O(n^2) to O(n^(log2 3)) ~= O(n^1.585),
221
+ -- while small or unbalanced operands with <= 16 bits use O(n) shift-and-add.
222
+
223
+ -- Unsigned Karatsuba multiplier on trimmed LSB-first bits[].
224
+ lsb_mul = ?{ bits[] x, bits[] y }
225
+ { .x x, .y y, {} |nil stack } |eval _lsb_mul ?bits[];
226
+
227
+ -- Explicit continuation frames and state machine for tail-recursive Karatsuba.
228
+ _k_split[] = { bits[] lo, bits[] hi };
229
+
230
+ _k_frame[] = <
231
+ { bits[] k, _k_split[] sx, _k_split[] sy } need_z2,
232
+ { bits[] k, _k_split[] sx, _k_split[] sy, bits[] z0 } need_z3,
233
+ { bits[] k, bits[] z0, bits[] z2 } combine
234
+ >;
235
+
236
+ _k_stack[] = < {} nil, { _k_frame[] car, S cdr } cons > = S;
237
+
238
+ _k_state[] = <
239
+ { bits[] x, bits[] y, _k_stack[] stack } eval,
240
+ { bits[] val, _k_stack[] stack } apply
241
+ >;
242
+
243
+ -- Tail-recursive explicit-stack Karatsuba step loop.
244
+ _lsb_mul = ?_k_state[] <
245
+ /apply { .stack /nil _s, .val val } .val,
246
+ <
247
+ /eval _lsb_mul_eval,
248
+ /apply { .val val, .stack /cons c } {
249
+ .val val, .c .car frame, .c .cdr rest
250
+ } _lsb_mul_apply
251
+ > _lsb_mul
252
+ > ?bits[];
253
+
254
+ _lsb_mul_eval = ?{ bits[] x, bits[] y, _k_stack[] stack } {
255
+ .x x,
256
+ .y y,
257
+ .stack stack,
258
+ .x _classify_bits cx,
259
+ .y _classify_bits cy
260
+ } <
261
+ { .x _x, .y _y, .cx /zero _cx, .cy _cy, .stack stack }
262
+ { {} |_ val, .stack stack } |apply,
263
+ { .x _x, .y _y, .cx < /short, /long > _cx, .cy /zero _cy, .stack stack }
264
+ { {} |_ val, .stack stack } |apply,
265
+ { .x x, .y y, .cx /short _cx, .cy < /short, /long > _cy, .stack stack }
266
+ { { .x iter, .y src, {} |_ acc } _lsb_mul_small val, .stack stack } |apply,
267
+ { .x x, .y y, .cx /long _cx, .cy /short _cy, .stack stack }
268
+ { { .y iter, .x src, {} |_ acc } _lsb_mul_small val, .stack stack } |apply,
269
+ { .x x, .y y, .cx /long _cx, .cy /long _cy, .stack stack } {
270
+ .x x,
271
+ .y y,
272
+ .stack stack,
273
+ { .x x, .y y } half_max_tally k
274
+ } {
275
+ .k k,
276
+ { .x x, .k k } lsb_split sx,
277
+ { .y x, .k k } lsb_split sy,
278
+ .stack stack
279
+ } {
280
+ .sx .lo x,
281
+ .sy .lo y,
282
+ { { .k k, .sx sx, .sy sy } |need_z2 car, .stack cdr } |cons stack
283
+ } |eval
284
+ > ?_k_state[];
285
+
286
+ _lsb_mul_apply = ?{ bits[] val, _k_frame[] frame, _k_stack[] rest } <
287
+ { .frame /need_z2 f, .val z0, .rest rest } {
288
+ .f .sx .hi x,
289
+ .f .sy .hi y,
290
+ {
291
+ { .f .k k, .f .sx sx, .f .sy sy, .z0 z0 } |need_z3 car,
292
+ .rest cdr
293
+ } |cons stack
294
+ } |eval,
295
+ { .frame /need_z3 f, .val z2, .rest rest } {
296
+ { .f .sx .lo x, .f .sx .hi y } lsb_add x,
297
+ { .f .sy .lo x, .f .sy .hi y } lsb_add y,
298
+ {
299
+ { .f .k k, .f .z0 z0, .z2 z2 } |combine car,
300
+ .rest cdr
301
+ } |cons stack
302
+ } |eval,
303
+ { .frame /combine f, .val z3, .rest rest } {
304
+ { .f .k k, .f .z0 z0, .f .z2 z2, .z3 z3 } _karatsuba_combine val,
305
+ .rest stack
306
+ } |apply
307
+ > ?_k_state[];
308
+
309
+ _karatsuba_combine = ?{ bits[] k, bits[] z0, bits[] z2, bits[] z3 } {
310
+ .z0 x,
311
+ {
312
+ { { { .z3 x, .z2 y } lsb_sub x, .z0 y } lsb_sub x, .k k } lsb_shl x,
313
+ { { .z2 x, .k k } lsb_shl x, .k k } lsb_shl y
314
+ } lsb_add y
315
+ } lsb_add ?bits[];
316
+
317
+ -- Base-case schoolbook shift-and-add over an operand of <= 16 bits.
318
+ _lsb_mul_small = ?{ bits[] iter, bits[] src, bits[] acc } <
319
+ { .iter /_ _i, .src _s, .acc acc } .acc,
320
+ { .iter < /0, /1 > _i, .src /_ _s, .acc acc } .acc,
321
+ <
322
+ { .iter /0 iter, .src < /0 |0, /1 |1 > src, .acc acc } {
323
+ .iter iter,
324
+ .src lsb_shl1 src,
325
+ .acc acc
326
+ },
327
+ { .iter /1 iter, .src < /0 |0, /1 |1 > src, .acc acc } {
328
+ .iter iter,
329
+ .src lsb_shl1 src,
330
+ { .acc x, .src y } lsb_add acc
331
+ }
332
+ > _lsb_mul_small
333
+ > ?bits[];
334
+
335
+ -- O(1) prefix classifier separating zero, <= 16 bits, and > 16 bits.
336
+ _classify_bits = <
337
+ /_ {} |zero,
338
+ /0 _drop16 < /_ {} |short, /0 {} |long, /1 {} |long >,
339
+ /1 _drop16 < /_ {} |short, /0 {} |long, /1 {} |long >
340
+ >;
341
+ _drop16 = _drop4 _drop4 _drop4 _drop4;
342
+ _drop4 = _drop2 _drop2;
343
+ _drop2 = _drop1 _drop1;
344
+ _drop1 = < /_ |_, /0, /1 >;
345
+
346
+ -- Computes floor(max(|x|, |y|) / 2) as a zero-tally bits[] in one pass.
347
+ half_max_tally = ?{ bits[] x, bits[] y } {
348
+ .x x, .y y, {} |_ acc
349
+ } _half_max_tally ?bits[];
350
+
351
+ _half_max_tally = ?{ bits[] x, bits[] y, bits[] acc } <
352
+ { .x _le1_bit? _x, .y _le1_bit? _y, .acc acc } .acc,
353
+ { .x x, .y y, .acc acc } { .x _drop2 x, .y _drop2 y, .acc |0 acc } _half_max_tally
354
+ > ?bits[];
355
+
356
+ -- Matches any bit-string of length <= 1 (base condition for _half_max_tally).
357
+ _le1_bit? = ?bits[] { < /_, /0 /_, /1 /_ > _b, () x } .x ?bits[];
358
+
359
+ -- Splits a trimmed LSB-first bits[] at tally `k` into { lo, hi }, preserving lsb_trim.
360
+ lsb_split = ?{ bits[] x, bits[] k } {
361
+ .x x, .k k, {} |_ lo_rev
362
+ } _lsb_split ?{ bits[] lo, bits[] hi };
363
+
364
+ _lsb_split = ?{ bits[] x, bits[] k, bits[] lo_rev } <
365
+ { .k /_ _k, .x x, .lo_rev lo_rev } {
366
+ .lo_rev msb_drop_zeros rev_bits lo,
367
+ .x hi
368
+ },
369
+ { .k < /0, /1 > _k, .x /_ _x, .lo_rev lo_rev } {
370
+ .lo_rev msb_drop_zeros rev_bits lo,
371
+ {} |_ hi
372
+ },
373
+ <
374
+ { .x /0 x, .k < /0, /1 > k, .lo_rev lo_rev } { .x x, .k k, .lo_rev |0 lo_rev },
375
+ { .x /1 x, .k < /0, /1 > k, .lo_rev lo_rev } { .x x, .k k, .lo_rev |1 lo_rev }
376
+ > _lsb_split
377
+ > ?{ bits[] lo, bits[] hi };
378
+
379
+
380
+ -- -----------------------------------------------------------------------------
381
+ -- 5. Fast Binary Long Division (MSB-First Shift-and-Subtract)
382
+ -- -----------------------------------------------------------------------------
383
+
384
+ -- Unsigned quotient and remainder on canonical MSB bits[] (fails when y == 0).
385
+ nat_divmod = ?{ bits[] x, bits[] y } {
386
+ .x trim_bits x,
387
+ .y trim_bits msb_to_lsb < /0 |0, /1 |1 > y_lsb
388
+ } {
389
+ .x x,
390
+ .y_lsb y_lsb,
391
+ {} |_ r_lsb,
392
+ {} |_ q_rev
393
+ } _nat_divmod ?{ bits[] q, bits[] r };
394
+
395
+ _nat_divmod = ?{ bits[] x, bits[] y_lsb, bits[] r_lsb, bits[] q_rev } <
396
+ { .x /_ _x, .y_lsb _y, .r_lsb r, .q_rev q } {
397
+ .q lsb_to_msb q,
398
+ .r lsb_to_msb r
399
+ },
400
+ <
401
+ { .x /0 x_tail, .y_lsb y_lsb, .r_lsb r, .q_rev q_rev } {
402
+ .x_tail x_tail,
403
+ .y_lsb y_lsb,
404
+ .r _lsb_push_0 r1,
405
+ .q_rev q_rev
406
+ },
407
+ { .x /1 x_tail, .y_lsb y_lsb, .r_lsb r, .q_rev q_rev } {
408
+ .x_tail x_tail,
409
+ .y_lsb y_lsb,
410
+ .r _lsb_push_1 r1,
411
+ .q_rev q_rev
412
+ }
413
+ > {
414
+ .x_tail x_tail,
415
+ .y_lsb y_lsb,
416
+ .r1 r1,
417
+ .q_rev q_rev,
418
+ { .r1 x, .y_lsb y } lsb_try_sub sub
419
+ } <
420
+ {
421
+ .x_tail x,
422
+ .y_lsb y_lsb,
423
+ .r1 _r1,
424
+ .q_rev q_rev,
425
+ .sub .b /0 _b,
426
+ .sub .r rem_next
427
+ } {
428
+ .x x,
429
+ .y_lsb y_lsb,
430
+ .rem_next r_lsb,
431
+ .q_rev |1 q_rev
432
+ },
433
+ {
434
+ .x_tail x,
435
+ .y_lsb y_lsb,
436
+ .r1 r1,
437
+ .q_rev q_rev,
438
+ .sub .b /1 _b,
439
+ .sub .r _r
440
+ } {
441
+ .x x,
442
+ .y_lsb y_lsb,
443
+ .r1 r_lsb,
444
+ .q_rev |0 q_rev
445
+ }
446
+ > _nat_divmod
447
+ > ?{ bits[] q, bits[] r };
448
+
449
+ -- Pushes MSB bit '0' or '1' as the new LSB onto a trimmed LSB-first remainder.
450
+ _lsb_push_0 = ?bits[] < /_ |_, /0 |0 |0, /1 |1 |0 > ?bits[];
451
+ _lsb_push_1 = ?bits[] < /_ |_ |1, /0 |0 |1, /1 |1 |1 > ?bits[];
452
+
453
+
454
+ -- -----------------------------------------------------------------------------
455
+ -- 6. Signed Integer Operations
456
+ -- -----------------------------------------------------------------------------
457
+
458
+ abs = ?int[] int < /+ |+, /- |+ > ?int[];
459
+
460
+ negate = ?int[] int <
461
+ /+ < /0 /_ |_ |0 |+, /1 |1 |- >,
462
+ /- |+
463
+ > ?int[];
464
+
465
+ -- Signed comparison returning order[] (lt | eq | gt).
466
+ cmp = ?{ int[] x, int[] y } { .x int x, .y int y } <
467
+ { .x /+ x, .y /+ y } { .x x, .y y } cmp_msb,
468
+ { .x /+ _x, .y /- _y } {} |gt,
469
+ { .x /- _x, .y /+ _y } {} |lt,
470
+ { .x /- x, .y /- y } { .x y, .y x } cmp_msb
471
+ > ?order[];
472
+
473
+ lt? = ?{ int[] x, int[] y } { cmp /lt _ord, () xy } .xy ?{ int[] x, int[] y };
474
+ le? = ?{ int[] x, int[] y } { cmp < /lt, /eq > _ord, () xy } .xy ?{ int[] x, int[] y };
475
+ eq? = ?{ int[] x, int[] y } { cmp /eq _ord, () xy } .xy ?{ int[] x, int[] y };
476
+ gt? = ?{ int[] x, int[] y } { cmp /gt _ord, () xy } .xy ?{ int[] x, int[] y };
477
+ ge? = ?{ int[] x, int[] y } { cmp < /gt, /eq > _ord, () xy } .xy ?{ int[] x, int[] y };
478
+
479
+ plus = ?{ int[] x, int[] y } { .x int x, .y int y } <
480
+ { .x /+ x, .y /+ y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_add lsb_to_msb |+ int,
481
+ { .x /- x, .y /- y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_add lsb_to_msb |- int,
482
+ { .x /+ x, .y /- y } { .x x, .y y } _sub_signed_msb,
483
+ { .x /- x, .y /+ y } { .x y, .y x } _sub_signed_msb
484
+ > ?int[];
485
+
486
+ -- Subtracts two unsigned MSB bits[], tagging '+' if x >= y else '-' on |y - x|.
487
+ _sub_signed_msb = ?{ bits[] x, bits[] y } {
488
+ .x x,
489
+ .y y,
490
+ { .x x, .y y } cmp_msb ord
491
+ } <
492
+ { .ord /eq _ord, .x _x, .y _y } 0,
493
+ { .ord /gt _ord, .x x, .y y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_sub lsb_to_msb |+,
494
+ { .ord /lt _ord, .x x, .y y } { .y msb_to_lsb x, .x msb_to_lsb y } lsb_sub lsb_to_msb |-
495
+ > ?int[];
496
+
497
+ minus = ?{ int[] x, int[] y } { .x x, .y negate y } plus ?int[];
498
+
499
+ inc = ?int[] { () x, 1 y } plus ?int[];
500
+ dec = ?int[] { () x, 1 y } minus ?int[];
501
+
502
+ times = ?{ int[] x, int[] y } { .x int x, .y int y } <
503
+ { .x /+ x, .y /+ y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_mul lsb_to_msb |+,
504
+ { .x /- x, .y /- y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_mul lsb_to_msb |+,
505
+ { .x /+ x, .y /- y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_mul lsb_to_msb |- int,
506
+ { .x /- x, .y /+ y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_mul lsb_to_msb |- int
507
+ > ?int[];
508
+
509
+ -- Truncating signed division & remainder matching JS BigInt (/ and %):
510
+ -- x = q * y + r, sign(q) = sign(x) * sign(y), sign(r) = sign(x), y != 0.
511
+ divmod = ?{ int[] x, int[] y } { .x int x, .y int y } <
512
+ { .x /+ x, .y /+ y } { .x x, .y y } nat_divmod { .q |+ q, .r |+ r },
513
+ { .x /+ x, .y /- y } { .x x, .y y } nat_divmod { .q |- int q, .r |+ r },
514
+ { .x /- x, .y /+ y } { .x x, .y y } nat_divmod { .q |- int q, .r |- int r },
515
+ { .x /- x, .y /- y } { .x x, .y y } nat_divmod { .q |+ q, .r |- int r }
516
+ > ?{ int[] q, int[] r };
517
+
518
+ div = ?{ int[] x, int[] y } divmod .q ?int[];
519
+ mod = ?{ int[] x, int[] y } divmod .r ?int[];
520
+
521
+ gcd = ?{ int[] x, int[] y } { .x abs /+ a, .y abs /+ b } _gcd ?int[];
522
+
523
+ -- Non-negative greatest common divisor via Euclidean algorithm.
524
+ _gcd = ?{ bits[] a, bits[] b } <
525
+ { .a a, .b /0 /_ |_ |0 _b } .a |+,
526
+ { .a a, .b /1 b } {
527
+ .b |1 a,
528
+ { .a x, .b |1 y } nat_divmod .r b
529
+ } _gcd
530
+ > ?int[];
531
+
532
+ lcm = ?{ int[] x, int[] y } { .x abs x, .y abs y } <
533
+ { .x /+ /0 /_ |_ |0 _x, .y _y } 0,
534
+ { .x /+ /1 _x, .y /+ /0 /_ |_ |0 _y } 0,
535
+ { .x /+ /1 x, .y /+ /1 y } {
536
+ {
537
+ .x |1 |+ x,
538
+ { .x |1 |+ x, .y |1 |+ y } gcd y
539
+ } div x,
540
+ .y |1 |+ y
541
+ } times
542
+ > ?int[];
@@ -286,11 +286,6 @@ export function expandPatternsInRels(rels, patterns = {}, options = {}) {
286
286
  allPatterns[name] = pat;
287
287
  }
288
288
  }
289
- for (const [name, hash] of Object.entries(lib.patternAlias || {})) {
290
- if (libPatterns[name] && !(hash in allPatterns)) {
291
- allPatterns[hash] = libPatterns[name];
292
- }
293
- }
294
289
  }
295
290
  }
296
291
 
@@ -322,13 +317,6 @@ export function validatePatternDefinitions(
322
317
  allPatterns[name] = pat;
323
318
  }
324
319
  }
325
- for (const [name, hash] of Object.entries(
326
- lib.patternAlias || {}
327
- )) {
328
- if (libPatterns[name] && !(hash in allPatterns)) {
329
- allPatterns[hash] = libPatterns[name];
330
- }
331
- }
332
320
  }
333
321
  }
334
322
 
package/object.mjs CHANGED
@@ -6,8 +6,6 @@ import { patterns2filters, prettyRel } from "./pretty.mjs";
6
6
  import {
7
7
  resolvePatternCanonicalInfo,
8
8
  buildUsePreamble,
9
- parseHeaderDirectives,
10
- parseHeaderUses,
11
9
  canonicalBoundaryFilterSources
12
10
  } from "./canonical-pattern.mjs";
13
11
 
@@ -372,7 +370,11 @@ function buildMeta(annotated, source, options = {}) {
372
370
  lib.defs?.patterns ||
373
371
  lib.sourceDefs?.patterns ||
374
372
  {};
375
- Object.assign(allPatterns, lp);
373
+ for (const [patName, patDef] of Object.entries(lp)) {
374
+ if (patName.startsWith("@") && !(patName in allPatterns)) {
375
+ allPatterns[patName] = patDef;
376
+ }
377
+ }
376
378
  }
377
379
  }
378
380
  if (options.patterns) {
@@ -478,16 +480,7 @@ function collectReachable(mainName, rels, allCodes) {
478
480
  }
479
481
 
480
482
  function compileObject(script, options = {}) {
481
- let scriptToAnnotate = script;
482
- if (typeof script === "string" && options.libraries?.length > 0) {
483
- const headerUses = parseHeaderUses(script);
484
- const allSpecs = [...headerUses, ...(options.useSpecs || options.use || [])];
485
- if (allSpecs.length > 0) {
486
- const preamble = buildUsePreamble(allSpecs, options.libraries);
487
- scriptToAnnotate = preamble + script;
488
- }
489
- }
490
- const annotated = annotate(scriptToAnnotate, options);
483
+ const annotated = annotate(script, options);
491
484
  const allCodes = stripDebugFieldsFromCodes(codes.dump());
492
485
  const serialized = serializeAnnotated(annotated);
493
486
  const meta = mergeMetaEntries(
@@ -544,16 +537,7 @@ function rewriteRefsToCanonical(exp, relAlias) {
544
537
  }
545
538
 
546
539
  function compileLibrary(script, options = {}) {
547
- let scriptToAnnotate = script;
548
- if (typeof script === "string" && options.libraries?.length > 0) {
549
- const headerUses = parseHeaderUses(script);
550
- const allSpecs = [...headerUses, ...(options.useSpecs || options.use || [])];
551
- if (allSpecs.length > 0) {
552
- const preamble = buildUsePreamble(allSpecs, options.libraries);
553
- scriptToAnnotate = preamble + script;
554
- }
555
- }
556
- const annotated = annotateLibrary(scriptToAnnotate, options);
540
+ const annotated = annotateLibrary(script, options);
557
541
  const allCodes = stripDebugFieldsFromCodes(codes.dump());
558
542
  const serialized = serializeAnnotated(annotated);
559
543
  const meta = mergeMetaEntries(
@@ -583,6 +567,7 @@ function compileLibrary(script, options = {}) {
583
567
  }
584
568
  for (const [name, rel] of Object.entries(serialized.rels)) {
585
569
  if (name === "__main__") continue;
570
+ if (rel._library) continue;
586
571
  const canonicalName = libRelAlias[name] || name;
587
572
  const { varRefs, ...relWithoutVarRefs } = rel;
588
573
  libRels[canonicalName] = {
@@ -591,6 +576,7 @@ function compileLibrary(script, options = {}) {
591
576
  };
592
577
  }
593
578
  const libPatterns = {};
579
+ const inheritedPatterns = {};
594
580
  const patternAlias = {};
595
581
  if (options.libraries) {
596
582
  for (const lib of options.libraries) {
@@ -599,7 +585,13 @@ function compileLibrary(script, options = {}) {
599
585
  lib.defs?.patterns ||
600
586
  lib.sourceDefs?.patterns ||
601
587
  {};
602
- Object.assign(libPatterns, lp);
588
+ for (const [k, v] of Object.entries(lp)) {
589
+ if (k.startsWith("@")) {
590
+ if (!(k in libPatterns)) libPatterns[k] = v;
591
+ } else {
592
+ inheritedPatterns[k] = v;
593
+ }
594
+ }
603
595
  Object.assign(patternAlias, lib.patternAlias || {});
604
596
  }
605
597
  }
@@ -637,11 +629,13 @@ function compileLibrary(script, options = {}) {
637
629
  }
638
630
  }
639
631
 
632
+ const exportedPatterns = { ...inheritedPatterns, ...libPatterns };
633
+
640
634
  const libraryMetaHashes = new Set([
641
635
  ...Object.keys(allCodes),
642
636
  ...Object.keys(libRels),
643
637
  ...Object.values(patternAlias),
644
- ...Object.keys(libPatterns).filter((k) => k.startsWith("@"))
638
+ ...Object.keys(exportedPatterns).filter((k) => k.startsWith("@"))
645
639
  ]);
646
640
  const libMeta = Object.fromEntries(
647
641
  Object.entries(meta).filter(([hash]) => libraryMetaHashes.has(hash))
@@ -655,7 +649,7 @@ function compileLibrary(script, options = {}) {
655
649
  patternAlias,
656
650
  compileStats: serialized.compileStats,
657
651
  meta: libMeta,
658
- patterns: libPatterns,
652
+ patterns: exportedPatterns,
659
653
  source: options.source || null,
660
654
  main: null
661
655
  };
@@ -1105,9 +1099,7 @@ export {
1105
1099
  decompileObjectBuffer,
1106
1100
  extractAliasesFromObject,
1107
1101
  extractAliasesFromObjectBuffer,
1108
- buildUsePreamble,
1109
- parseHeaderDirectives,
1110
- parseHeaderUses
1102
+ buildUsePreamble
1111
1103
  };
1112
1104
 
1113
1105
  export default {
@@ -1130,7 +1122,5 @@ export default {
1130
1122
  decompileObjectBuffer,
1131
1123
  extractAliasesFromObject,
1132
1124
  extractAliasesFromObjectBuffer,
1133
- buildUsePreamble,
1134
- parseHeaderDirectives,
1135
- parseHeaderUses
1125
+ buildUsePreamble
1136
1126
  };