@fraczak/k 8.7.1 → 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 (65) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/DOCS/CODECS.md +42 -40
  3. package/DOCS/FILE_FORMATS.md +28 -25
  4. package/DOCS/GETTING_STARTED.md +10 -15
  5. package/DOCS/PATTERNS.md +3 -3
  6. package/DOCS/REPL.md +3 -6
  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 +1 -2
  12. package/backends/llvm/tests/integration.sh +2 -6
  13. package/backends/wasm/tests/integration.sh +5 -10
  14. package/browser/shims/fs.mjs +2 -6
  15. package/canonical-pattern.mjs +9 -29
  16. package/compiler.mjs +22 -12
  17. package/harness/suites/poly.mjs +9 -11
  18. package/index.mjs +119 -50
  19. package/lib/djson.k +981 -295
  20. package/lib/generic.k +701 -0
  21. package/lib/ieee.k +662 -638
  22. package/lib/integer.k +542 -0
  23. package/object.mjs +19 -12
  24. package/objects/README.md +6 -6
  25. package/package.json +1 -1
  26. package/scripts/benchmark-idempotency.mjs +16 -17
  27. package/scripts/benchmark.mjs +39 -29
  28. package/scripts/build-repl-html.mjs +45 -35
  29. package/scripts/perf-ieee.mjs +1 -5
  30. package/scripts/perf-int.mjs +12 -27
  31. package/scripts/perf-multiplication.mjs +8 -9
  32. package/scripts/perf-poly.mjs +7 -10
  33. package/scripts/render-benchmark.mjs +1 -1
  34. package/scripts/run-tests.mjs +4 -2
  35. package/scripts/test-package.mjs +4 -10
  36. package/tests/README.md +4 -5
  37. package/tests/helpers/cli.mjs +30 -7
  38. package/tests/integration.sh +5 -10
  39. package/tests/test-arithmetics.mjs +23 -16
  40. package/tests/test-djson-methods.mjs +17 -25
  41. package/tests/test-djson.mjs +273 -0
  42. package/tests/test-generic.mjs +239 -0
  43. package/tests/test-ieee-arithmetic.mjs +1 -5
  44. package/tests/test-ieee.mjs +153 -0
  45. package/tests/test-integer.mjs +264 -0
  46. package/tests/test-k-object.mjs +12 -18
  47. package/tests/test-kvm-polymorphic.mjs +6 -7
  48. package/tests/test-lib.mjs +62 -52
  49. package/tests/test-library-isolation.mjs +349 -26
  50. package/tests/test-parse-json.mjs +15 -24
  51. package/tests/test-poly.mjs +21 -16
  52. package/tests/test-repl-html.mjs +39 -44
  53. package/tests/test-repl.mjs +46 -59
  54. package/DOCS/book-combined.md +0 -3786
  55. package/DOCS/dependency-graph.md +0 -144
  56. package/Examples/arithmetics.k +0 -108
  57. package/Examples/esc.k +0 -9
  58. package/lib/arithmetics.k +0 -286
  59. package/lib/bit.k +0 -68
  60. package/lib/bits.k +0 -227
  61. package/lib/btrie.k +0 -187
  62. package/lib/list.k +0 -49
  63. package/lib/string.k +0 -588
  64. package/tests/test-ieee-text.mjs +0 -74
  65. 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[];
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) {
@@ -574,6 +576,7 @@ function compileLibrary(script, options = {}) {
574
576
  };
575
577
  }
576
578
  const libPatterns = {};
579
+ const inheritedPatterns = {};
577
580
  const patternAlias = {};
578
581
  if (options.libraries) {
579
582
  for (const lib of options.libraries) {
@@ -582,7 +585,13 @@ function compileLibrary(script, options = {}) {
582
585
  lib.defs?.patterns ||
583
586
  lib.sourceDefs?.patterns ||
584
587
  {};
585
- 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
+ }
586
595
  Object.assign(patternAlias, lib.patternAlias || {});
587
596
  }
588
597
  }
@@ -620,11 +629,13 @@ function compileLibrary(script, options = {}) {
620
629
  }
621
630
  }
622
631
 
632
+ const exportedPatterns = { ...inheritedPatterns, ...libPatterns };
633
+
623
634
  const libraryMetaHashes = new Set([
624
635
  ...Object.keys(allCodes),
625
636
  ...Object.keys(libRels),
626
637
  ...Object.values(patternAlias),
627
- ...Object.keys(libPatterns).filter((k) => k.startsWith("@"))
638
+ ...Object.keys(exportedPatterns).filter((k) => k.startsWith("@"))
628
639
  ]);
629
640
  const libMeta = Object.fromEntries(
630
641
  Object.entries(meta).filter(([hash]) => libraryMetaHashes.has(hash))
@@ -638,7 +649,7 @@ function compileLibrary(script, options = {}) {
638
649
  patternAlias,
639
650
  compileStats: serialized.compileStats,
640
651
  meta: libMeta,
641
- patterns: libPatterns,
652
+ patterns: exportedPatterns,
642
653
  source: options.source || null,
643
654
  main: null
644
655
  };
@@ -1088,9 +1099,7 @@ export {
1088
1099
  decompileObjectBuffer,
1089
1100
  extractAliasesFromObject,
1090
1101
  extractAliasesFromObjectBuffer,
1091
- buildUsePreamble,
1092
- parseHeaderDirectives,
1093
- parseHeaderUses
1102
+ buildUsePreamble
1094
1103
  };
1095
1104
 
1096
1105
  export default {
@@ -1113,7 +1122,5 @@ export default {
1113
1122
  decompileObjectBuffer,
1114
1123
  extractAliasesFromObject,
1115
1124
  extractAliasesFromObjectBuffer,
1116
- buildUsePreamble,
1117
- parseHeaderDirectives,
1118
- parseHeaderUses
1125
+ buildUsePreamble
1119
1126
  };
package/objects/README.md CHANGED
@@ -21,13 +21,13 @@ node objects/validate.mjs /tmp/k-ok.ko
21
21
  `--lib` loads a dependency. It does not select library output; use a `.klib`
22
22
  output filename or `--format klib`.
23
23
 
24
- When compiling source files, required library dependencies and friendly aliases
25
- can be documented directly in the source file header via
26
- `# --lib <dependency> --use <specs>` comments. When `--lib` is provided on the
27
- command line, all library relations and patterns are available in memory during
28
- compilation. CLI `--use` flags allow creating in-memory aliases when needed.
24
+ Leading `# --lib <dependency> --use <specs>` lines in source files are ordinary
25
+ human-readable comments documenting expected dependencies. When `--lib` is
26
+ provided on the command line, library definitions are available in memory
27
+ indexed strictly by content hash (`@<hash>`), and CLI `--use` flags explicitly
28
+ bring requested friendly names or aliases into compilation scope.
29
29
 
30
30
  The [file-format reference](../DOCS/FILE_FORMATS.md) is the source for container
31
- fields, executable/library distinctions, header directives, aliases, and
31
+ fields, executable/library distinctions, dependency comments, aliases, and
32
32
  complete workflows.
33
33
  [KIR-P](../DOCS/KIR_V1.md) documents the portable backend export.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fraczak/k",
3
- "version": "8.7.1",
3
+ "version": "8.8.0",
4
4
  "description": "k — a language for content-addressable schemas and data transformations over algebraic data types (products and tagged unions); includes runtime, contract derivation, REPL, and codec toolchain",
5
5
  "publishConfig": {
6
6
  "access": "public"