@fraczak/k 8.7.1 → 8.9.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 (69) hide show
  1. package/CHANGELOG.md +28 -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/codecs/djson.mjs +2 -3
  17. package/codecs/int.mjs +66 -14
  18. package/compiler.mjs +22 -12
  19. package/harness/suites/poly.mjs +9 -11
  20. package/index.mjs +119 -50
  21. package/lib/djson.k +976 -288
  22. package/lib/generic.k +686 -0
  23. package/lib/ieee.k +658 -639
  24. package/lib/integer.k +536 -0
  25. package/object.mjs +19 -12
  26. package/objects/README.md +6 -6
  27. package/package.json +1 -1
  28. package/scripts/benchmark-idempotency.mjs +16 -17
  29. package/scripts/benchmark.mjs +39 -29
  30. package/scripts/build-repl-html.mjs +45 -35
  31. package/scripts/perf-bigint-compare.mjs +279 -0
  32. package/scripts/perf-ieee.mjs +1 -5
  33. package/scripts/perf-int.mjs +12 -27
  34. package/scripts/perf-multiplication.mjs +8 -9
  35. package/scripts/perf-poly.mjs +7 -10
  36. package/scripts/render-benchmark.mjs +1 -1
  37. package/scripts/run-tests.mjs +4 -2
  38. package/scripts/test-package.mjs +4 -10
  39. package/tests/README.md +4 -5
  40. package/tests/helpers/cli.mjs +30 -7
  41. package/tests/integration.sh +5 -10
  42. package/tests/test-arithmetics.mjs +23 -16
  43. package/tests/test-djson-methods.mjs +16 -25
  44. package/tests/test-djson.mjs +272 -0
  45. package/tests/test-generic.mjs +237 -0
  46. package/tests/test-ieee-arithmetic.mjs +1 -5
  47. package/tests/test-ieee.mjs +153 -0
  48. package/tests/test-integer.mjs +264 -0
  49. package/tests/test-k-object.mjs +12 -18
  50. package/tests/test-kvm-polymorphic.mjs +6 -7
  51. package/tests/test-lib.mjs +62 -52
  52. package/tests/test-library-isolation.mjs +349 -26
  53. package/tests/test-parse-json.mjs +15 -24
  54. package/tests/test-poly.mjs +21 -16
  55. package/tests/test-repl-html.mjs +39 -44
  56. package/tests/test-repl.mjs +46 -59
  57. package/tests/test-structural-values.mjs +1 -1
  58. package/DOCS/book-combined.md +0 -3786
  59. package/DOCS/dependency-graph.md +0 -144
  60. package/Examples/arithmetics.k +0 -108
  61. package/Examples/esc.k +0 -9
  62. package/lib/arithmetics.k +0 -286
  63. package/lib/bit.k +0 -68
  64. package/lib/bits.k +0 -227
  65. package/lib/btrie.k +0 -187
  66. package/lib/list.k +0 -49
  67. package/lib/string.k +0 -588
  68. package/tests/test-ieee-text.mjs +0 -74
  69. package/tests/test-karatsuba.mjs +0 -25
package/lib/integer.k ADDED
@@ -0,0 +1,536 @@
1
+ -- =============================================================================
2
+ -- lib/integer.k -- Arbitrary-Precision Integer Arithmetic
3
+ --
4
+ -- Self-contained module (zero dependencies) representing
5
+ -- 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.
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 = {}|_|+ ?int[];
36
+ 1 = {}|_|1|+ ?int[];
37
+ -1 = {}|_|1|- ?int[];
38
+
39
+ -- Strips redundant leading '0' tags.
40
+ trim_bits = ?bits[] <
41
+ /_ |_,
42
+ /1 |1,
43
+ /0 trim_bits
44
+ > ?bits[];
45
+ msb_drop_zeros = trim_bits;
46
+
47
+ -- Lifts unsigned bits[] magnitude (MSB at root, '_' at leaf) to canonical non-negative int[].
48
+ nat = ?bits[] trim_bits |+ ?int[];
49
+
50
+ -- Canonicalizes a signed int[] (strips leading zeros and normalizes -0 to +0).
51
+ int = ?int[] <
52
+ /+ trim_bits |+,
53
+ /- trim_bits <
54
+ /_ |_|+,
55
+ /1 |1|-
56
+ >
57
+ > ?int[];
58
+
59
+ -- Identity filters (succeed unchanged on matching values, fail otherwise).
60
+ zero? = int { /+/_ _x, () x } .x ?int[];
61
+ pos? = int /+/1 |1|+ ?int[];
62
+ neg? = int /- |- ?int[];
63
+
64
+ -- Unsigned normalization to/from internal trimmed LSB-first bits[] ('_' = 0).
65
+ rev_bits = ?bits[] { () in, {} |_ acc } _rev_bits ?bits[];
66
+ _rev_bits = ?{ bits[] in, bits[] acc } <
67
+ { .in /_ _in, .acc acc } .acc,
68
+ <
69
+ { .in /0 in, .acc |0 acc },
70
+ { .in /1 in, .acc |1 acc }
71
+ > _rev_bits
72
+ > ?bits[];
73
+
74
+ lsb_trim = ?bits[] rev_bits msb_drop_zeros rev_bits ?bits[];
75
+ msb_to_lsb = ?bits[] msb_drop_zeros rev_bits ?bits[];
76
+ lsb_to_msb = ?bits[] rev_bits trim_bits ?bits[];
77
+
78
+
79
+ -- -----------------------------------------------------------------------------
80
+ -- 2. Magnitude Comparison & Bit-Shift Helpers
81
+ -- -----------------------------------------------------------------------------
82
+
83
+ -- Single-pass MSB-first comparison of two canonical trimmed bits[] values.
84
+ -- Accumulates first differing bit as `ord`; longer length strictly wins.
85
+ cmp_msb = ?{ bits[] x, bits[] y } { .x x, .y y, {} |eq ord } _cmp_msb ?order[];
86
+ _cmp_msb = ?{ bits[] x, bits[] y, order[] ord } <
87
+ { .x /_ _x, .y /_ _y, .ord ord } .ord,
88
+ .x /_ |lt,
89
+ .y /_ |gt,
90
+ <
91
+ { .x /0 x, .y /0 y, .ord ord },
92
+ { .x /1 x, .y /1 y, .ord ord },
93
+ { .x /0 x, .y /1 y, .ord </eq |lt, ()> ord },
94
+ { .x /1 x, .y /0 y, .ord </eq |gt, ()> ord }
95
+ > _cmp_msb
96
+ > ?order[];
97
+
98
+ -- Shifts a trimmed LSB-first bits[] left by 1 bit or by a tally `k`.
99
+ lsb_shl1 = ?bits[] < /_ |_, |0 > ?bits[];
100
+
101
+ lsb_shl = ?{ bits[] x, bits[] k } <
102
+ .x /_ |_,
103
+ { .x x, .k /_ _k } .x,
104
+ { .x |0 x, .k </0,/1> k } lsb_shl
105
+ > ?bits[];
106
+
107
+
108
+ -- -----------------------------------------------------------------------------
109
+ -- 3. Unsigned LSB Addition & Subtraction
110
+ -- -----------------------------------------------------------------------------
111
+
112
+ lsb_add = ?{ bits[] x, bits[] y } {
113
+ .x x, .y y, {}|0 c, {} |_ acc
114
+ } _lsb_add msb_drop_zeros rev_bits ?bits[];
115
+
116
+ -- Tail-recursive ripple-carry addition accumulating MSB-first in `acc`.
117
+ _lsb_add = ?{ bits[] x, bits[] y, bit[] c, bits[] acc } <
118
+ { .x /_ _x, .y /_ _y, .c /0 _c, .acc acc } .acc,
119
+ { .x /_ _x, .y /_ _y, .c /1 _c, .acc acc } .acc |1,
120
+ { .x /_ _x, .y < /0 |0, /1 |1 > y, .c /0 _c, .acc acc }
121
+ { .y in, .acc acc } _rev_bits,
122
+ { .x < /0 |0, /1 |1 > x, .y /_ _y, .c /0 _c, .acc acc }
123
+ { .x in, .acc acc } _rev_bits,
124
+ { .x _pop_lsb_bit px, .y _pop_lsb_bit py, .c c, .acc acc } {
125
+ .px .rest x,
126
+ .py .rest y,
127
+ { .px .b a, .py .b b, .c c } _full_add_bit s,
128
+ .acc acc
129
+ } {
130
+ .x x,
131
+ .y y,
132
+ .s .carry c,
133
+ { .s .sum b, .acc acc } _push_bit_acc acc
134
+ } _lsb_add
135
+ > ?bits[];
136
+
137
+ _full_add_bit = ?{ bit[] a, bit[] b, bit[] c } <
138
+ { .a /0 _a, .b /0 _b, .c /0 _c } { {} |0 sum, {} |0 carry },
139
+ { .a /0 _a, .b /0 _b, .c /1 _c } { {} |1 sum, {} |0 carry },
140
+ { .a /0 _a, .b /1 _b, .c /0 _c } { {} |1 sum, {} |0 carry },
141
+ { .a /0 _a, .b /1 _b, .c /1 _c } { {} |0 sum, {} |1 carry },
142
+ { .a /1 _a, .b /0 _b, .c /0 _c } { {} |1 sum, {} |0 carry },
143
+ { .a /1 _a, .b /0 _b, .c /1 _c } { {} |0 sum, {} |1 carry },
144
+ { .a /1 _a, .b /1 _b, .c /0 _c } { {} |0 sum, {} |1 carry },
145
+ { .a /1 _a, .b /1 _b, .c /1 _c } { {} |1 sum, {} |1 carry }
146
+ > ?{ bit[] sum, bit[] carry };
147
+
148
+ -- Subtracts y from x assuming x >= y (fails if underflow occurs).
149
+ lsb_sub = ?{ bits[] x, bits[] y }
150
+ { .x x, .y y, {} |0 b, {} |_ acc } _lsb_sub { .b /0 _b, .r r } .r ?bits[];
151
+
152
+ -- Tries x - y: returns { b: 0, r: x - y } if x >= y, or { b: 1, r: _ } if x < y.
153
+ lsb_try_sub = ?{ bits[] x, bits[] y }
154
+ { .x x, .y y, {} |0 b, {} |_ acc } _lsb_sub ?{ bit[] b, bits[] r };
155
+
156
+ -- Tail-recursive ripple-borrow subtraction on trimmed LSB-first bits[].
157
+ -- Short-circuits immediately with borrow=1 when x reaches '_' before non-empty y or borrow=1.
158
+ _lsb_sub = ?{ bits[] x, bits[] y, bit[] b, bits[] acc } <
159
+ { .x /_ _x, .y /_ _y, .b /0 _b, .acc acc }
160
+ { {} |0 b, .acc msb_drop_zeros rev_bits r },
161
+ .x /_ { {} |1 b, {} |_ r },
162
+ { .x < /0 |0, /1 |1 > x, .y /_ _y, .b /0 _b, .acc acc } {
163
+ {} |0 b,
164
+ { .x in, .acc acc } _rev_bits msb_drop_zeros rev_bits r
165
+ },
166
+ { .x < /0 |0, /1 |1 > _pop_lsb_bit px, .y _pop_lsb_bit py, .b b, .acc acc } {
167
+ .px .rest x,
168
+ .py .rest y,
169
+ { .px .b a, .py .b b, .b c } _full_sub_bit s,
170
+ .acc acc
171
+ } {
172
+ .x x,
173
+ .y y,
174
+ .s .borrow b,
175
+ { .s .diff b, .acc acc } _push_bit_acc acc
176
+ } _lsb_sub
177
+ > ?{ bit[] b, bits[] r };
178
+
179
+ _full_sub_bit = ?{ bit[] a, bit[] b, bit[] c } <
180
+ { .a /0 _a, .b /0 _b, .c /0 _c } { {} |0 diff, {} |0 borrow },
181
+ { .a /0 _a, .b /0 _b, .c /1 _c } { {} |1 diff, {} |1 borrow },
182
+ { .a /0 _a, .b /1 _b, .c /0 _c } { {} |1 diff, {} |1 borrow },
183
+ { .a /0 _a, .b /1 _b, .c /1 _c } { {} |0 diff, {} |1 borrow },
184
+ { .a /1 _a, .b /0 _b, .c /0 _c } { {} |1 diff, {} |0 borrow },
185
+ { .a /1 _a, .b /0 _b, .c /1 _c } { {} |0 diff, {} |0 borrow },
186
+ { .a /1 _a, .b /1 _b, .c /0 _c } { {} |0 diff, {} |0 borrow },
187
+ { .a /1 _a, .b /1 _b, .c /1 _c } { {} |1 diff, {} |1 borrow }
188
+ > ?{ bit[] diff, bit[] borrow };
189
+
190
+ _pop_lsb_bit = ?bits[] <
191
+ /_ { {} |0 b, {} |_ rest },
192
+ /0 { {} |0 b, () rest },
193
+ /1 { {} |1 b, () rest }
194
+ > ?{ bit[] b, bits[] rest };
195
+
196
+ _push_bit_acc = ?{ bit[] b, bits[] acc } <
197
+ { .b /0 _b, .acc acc } .acc |0,
198
+ { .b /1 _b, .acc acc } .acc |1
199
+ > ?bits[];
200
+
201
+
202
+ -- -----------------------------------------------------------------------------
203
+ -- 4. Fast Multiplication (Karatsuba + Schoolbook Base Case)
204
+ -- -----------------------------------------------------------------------------
205
+ -- Algorithm: Karatsuba multiplication (Anatolii Karatsuba, 1960).
206
+ -- For operands above the 16-bit cutoff, each operand is split at half the
207
+ -- maximum bit-width `k`:
208
+ -- x = x1 * 2^k + x0, y = y1 * 2^k + y0
209
+ -- Three recursive half-size products suffice:
210
+ -- z0 = x0 * y0
211
+ -- z2 = x1 * y1
212
+ -- z1 = (x0 + x1) * (y0 + y1) - z2 - z0
213
+ -- x * y = z0 + z1 * 2^k + z2 * 2^(2k)
214
+ -- Asymptotic complexity improves from O(n^2) to O(n^(log2 3)) ~= O(n^1.585),
215
+ -- while small or unbalanced operands with <= 16 bits use O(n) shift-and-add.
216
+
217
+ -- Unsigned Karatsuba multiplier on trimmed LSB-first bits[].
218
+ lsb_mul = ?{ bits[] x, bits[] y }
219
+ { .x x, .y y, {} |nil stack } |eval _lsb_mul ?bits[];
220
+
221
+ -- Explicit continuation frames and state machine for tail-recursive Karatsuba.
222
+ _k_split[] = { bits[] lo, bits[] hi };
223
+
224
+ _k_frame[] = <
225
+ { bits[] k, _k_split[] sx, _k_split[] sy } need_z2,
226
+ { bits[] k, _k_split[] sx, _k_split[] sy, bits[] z0 } need_z3,
227
+ { bits[] k, bits[] z0, bits[] z2 } combine
228
+ >;
229
+
230
+ _k_stack[] = < {} nil, { _k_frame[] car, S cdr } cons > = S;
231
+
232
+ _k_state[] = <
233
+ { bits[] x, bits[] y, _k_stack[] stack } eval,
234
+ { bits[] val, _k_stack[] stack } apply
235
+ >;
236
+
237
+ -- Tail-recursive explicit-stack Karatsuba step loop.
238
+ _lsb_mul = ?_k_state[] <
239
+ /apply { .stack /nil _s, .val val } .val,
240
+ <
241
+ /eval _lsb_mul_eval,
242
+ /apply { .val val, .stack /cons c } {
243
+ .val val, .c .car frame, .c .cdr rest
244
+ } _lsb_mul_apply
245
+ > _lsb_mul
246
+ > ?bits[];
247
+
248
+ _lsb_mul_eval = ?{ bits[] x, bits[] y, _k_stack[] stack } {
249
+ .x x,
250
+ .y y,
251
+ .stack stack,
252
+ .x _classify_bits cx,
253
+ .y _classify_bits cy
254
+ } <
255
+ { .x _x, .y _y, .cx /zero _cx, .cy _cy, .stack stack }
256
+ { {} |_ val, .stack stack } |apply,
257
+ { .x _x, .y _y, .cx < /short, /long > _cx, .cy /zero _cy, .stack stack }
258
+ { {} |_ val, .stack stack } |apply,
259
+ { .x x, .y y, .cx /short _cx, .cy < /short, /long > _cy, .stack stack }
260
+ { { .x iter, .y src, {} |_ acc } _lsb_mul_small val, .stack stack } |apply,
261
+ { .x x, .y y, .cx /long _cx, .cy /short _cy, .stack stack }
262
+ { { .y iter, .x src, {} |_ acc } _lsb_mul_small val, .stack stack } |apply,
263
+ { .x x, .y y, .cx /long _cx, .cy /long _cy, .stack stack } {
264
+ .x x,
265
+ .y y,
266
+ .stack stack,
267
+ { .x x, .y y } half_max_tally k
268
+ } {
269
+ .k k,
270
+ { .x x, .k k } lsb_split sx,
271
+ { .y x, .k k } lsb_split sy,
272
+ .stack stack
273
+ } {
274
+ .sx .lo x,
275
+ .sy .lo y,
276
+ { { .k k, .sx sx, .sy sy } |need_z2 car, .stack cdr } |cons stack
277
+ } |eval
278
+ > ?_k_state[];
279
+
280
+ _lsb_mul_apply = ?{ bits[] val, _k_frame[] frame, _k_stack[] rest } <
281
+ { .frame /need_z2 f, .val z0, .rest rest } {
282
+ .f .sx .hi x,
283
+ .f .sy .hi y,
284
+ {
285
+ { .f .k k, .f .sx sx, .f .sy sy, .z0 z0 } |need_z3 car,
286
+ .rest cdr
287
+ } |cons stack
288
+ } |eval,
289
+ { .frame /need_z3 f, .val z2, .rest rest } {
290
+ { .f .sx .lo x, .f .sx .hi y } lsb_add x,
291
+ { .f .sy .lo x, .f .sy .hi y } lsb_add y,
292
+ {
293
+ { .f .k k, .f .z0 z0, .z2 z2 } |combine car,
294
+ .rest cdr
295
+ } |cons stack
296
+ } |eval,
297
+ { .frame /combine f, .val z3, .rest rest } {
298
+ { .f .k k, .f .z0 z0, .f .z2 z2, .z3 z3 } _karatsuba_combine val,
299
+ .rest stack
300
+ } |apply
301
+ > ?_k_state[];
302
+
303
+ _karatsuba_combine = ?{ bits[] k, bits[] z0, bits[] z2, bits[] z3 } {
304
+ .z0 x,
305
+ {
306
+ { { { .z3 x, .z2 y } lsb_sub x, .z0 y } lsb_sub x, .k k } lsb_shl x,
307
+ { { .z2 x, .k k } lsb_shl x, .k k } lsb_shl y
308
+ } lsb_add y
309
+ } lsb_add ?bits[];
310
+
311
+ -- Base-case schoolbook shift-and-add over an operand of <= 16 bits.
312
+ _lsb_mul_small = ?{ bits[] iter, bits[] src, bits[] acc } <
313
+ { .iter /_ _i, .src _s, .acc acc } .acc,
314
+ { .iter < /0, /1 > _i, .src /_ _s, .acc acc } .acc,
315
+ <
316
+ { .iter /0 iter, .src < /0 |0, /1 |1 > src, .acc acc } {
317
+ .iter iter,
318
+ .src lsb_shl1 src,
319
+ .acc acc
320
+ },
321
+ { .iter /1 iter, .src < /0 |0, /1 |1 > src, .acc acc } {
322
+ .iter iter,
323
+ .src lsb_shl1 src,
324
+ { .acc x, .src y } lsb_add acc
325
+ }
326
+ > _lsb_mul_small
327
+ > ?bits[];
328
+
329
+ -- O(1) prefix classifier separating zero, <= 16 bits, and > 16 bits.
330
+ _classify_bits = <
331
+ /_ {} |zero,
332
+ /0 _drop16 < /_ {} |short, /0 {} |long, /1 {} |long >,
333
+ /1 _drop16 < /_ {} |short, /0 {} |long, /1 {} |long >
334
+ >;
335
+ _drop16 = _drop4 _drop4 _drop4 _drop4;
336
+ _drop4 = _drop2 _drop2;
337
+ _drop2 = _drop1 _drop1;
338
+ _drop1 = < /_ |_, /0, /1 >;
339
+
340
+ -- Computes floor(max(|x|, |y|) / 2) as a zero-tally bits[] in one pass.
341
+ half_max_tally = ?{ bits[] x, bits[] y } {
342
+ .x x, .y y, {} |_ acc
343
+ } _half_max_tally ?bits[];
344
+
345
+ _half_max_tally = ?{ bits[] x, bits[] y, bits[] acc } <
346
+ { .x _le1_bit? _x, .y _le1_bit? _y, .acc acc } .acc,
347
+ { .x x, .y y, .acc acc } { .x _drop2 x, .y _drop2 y, .acc |0 acc } _half_max_tally
348
+ > ?bits[];
349
+
350
+ -- Matches any bit-string of length <= 1 (base condition for _half_max_tally).
351
+ _le1_bit? = ?bits[] { < /_, /0 /_, /1 /_ > _b, () x } .x ?bits[];
352
+
353
+ -- Splits a trimmed LSB-first bits[] at tally `k` into { lo, hi }, preserving lsb_trim.
354
+ lsb_split = ?{ bits[] x, bits[] k } {
355
+ .x x, .k k, {} |_ lo_rev
356
+ } _lsb_split ?{ bits[] lo, bits[] hi };
357
+
358
+ _lsb_split = ?{ bits[] x, bits[] k, bits[] lo_rev } <
359
+ { .k /_ _k, .x x, .lo_rev lo_rev } {
360
+ .lo_rev msb_drop_zeros rev_bits lo,
361
+ .x hi
362
+ },
363
+ { .k < /0, /1 > _k, .x /_ _x, .lo_rev lo_rev } {
364
+ .lo_rev msb_drop_zeros rev_bits lo,
365
+ {} |_ hi
366
+ },
367
+ <
368
+ { .x /0 x, .k < /0, /1 > k, .lo_rev lo_rev } { .x x, .k k, .lo_rev |0 lo_rev },
369
+ { .x /1 x, .k < /0, /1 > k, .lo_rev lo_rev } { .x x, .k k, .lo_rev |1 lo_rev }
370
+ > _lsb_split
371
+ > ?{ bits[] lo, bits[] hi };
372
+
373
+
374
+ -- -----------------------------------------------------------------------------
375
+ -- 5. Fast Binary Long Division (MSB-First Shift-and-Subtract)
376
+ -- -----------------------------------------------------------------------------
377
+
378
+ -- Unsigned quotient and remainder on canonical MSB bits[] (fails when y == 0).
379
+ nat_divmod = ?{ bits[] x, bits[] y } {
380
+ .x trim_bits x,
381
+ .y trim_bits msb_to_lsb < /0 |0, /1 |1 > y_lsb
382
+ } {
383
+ .x x,
384
+ .y_lsb y_lsb,
385
+ {} |_ r_lsb,
386
+ {} |_ q_rev
387
+ } _nat_divmod ?{ bits[] q, bits[] r };
388
+
389
+ _nat_divmod = ?{ bits[] x, bits[] y_lsb, bits[] r_lsb, bits[] q_rev } <
390
+ { .x /_ _x, .y_lsb _y, .r_lsb r, .q_rev q } {
391
+ .q lsb_to_msb q,
392
+ .r lsb_to_msb r
393
+ },
394
+ <
395
+ { .x /0 x_tail, .y_lsb y_lsb, .r_lsb r, .q_rev q_rev } {
396
+ .x_tail x_tail,
397
+ .y_lsb y_lsb,
398
+ .r _lsb_push_0 r1,
399
+ .q_rev q_rev
400
+ },
401
+ { .x /1 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_1 r1,
405
+ .q_rev q_rev
406
+ }
407
+ > {
408
+ .x_tail x_tail,
409
+ .y_lsb y_lsb,
410
+ .r1 r1,
411
+ .q_rev q_rev,
412
+ { .r1 x, .y_lsb y } lsb_try_sub sub
413
+ } <
414
+ {
415
+ .x_tail x,
416
+ .y_lsb y_lsb,
417
+ .r1 _r1,
418
+ .q_rev q_rev,
419
+ .sub .b /0 _b,
420
+ .sub .r rem_next
421
+ } {
422
+ .x x,
423
+ .y_lsb y_lsb,
424
+ .rem_next r_lsb,
425
+ .q_rev |1 q_rev
426
+ },
427
+ {
428
+ .x_tail x,
429
+ .y_lsb y_lsb,
430
+ .r1 r1,
431
+ .q_rev q_rev,
432
+ .sub .b /1 _b,
433
+ .sub .r _r
434
+ } {
435
+ .x x,
436
+ .y_lsb y_lsb,
437
+ .r1 r_lsb,
438
+ .q_rev |0 q_rev
439
+ }
440
+ > _nat_divmod
441
+ > ?{ bits[] q, bits[] r };
442
+
443
+ -- Pushes MSB bit '0' or '1' as the new LSB onto a trimmed LSB-first remainder.
444
+ _lsb_push_0 = ?bits[] < /_ |_, /0 |0 |0, /1 |1 |0 > ?bits[];
445
+ _lsb_push_1 = ?bits[] < /_ |_ |1, /0 |0 |1, /1 |1 |1 > ?bits[];
446
+
447
+
448
+ -- -----------------------------------------------------------------------------
449
+ -- 6. Signed Integer Operations
450
+ -- -----------------------------------------------------------------------------
451
+
452
+ abs = ?int[] int < /+ |+, /- |+ > ?int[];
453
+
454
+ negate = ?int[] int <
455
+ /+ < /_ |_|+, |- >,
456
+ /- |+
457
+ > ?int[];
458
+
459
+ -- Signed comparison returning order[] (lt | eq | gt).
460
+ cmp = ?{ int[] x, int[] y } { .x int x, .y int y } <
461
+ { .x /+ x, .y /+ y } { .x x, .y y } cmp_msb,
462
+ { .x /+ _x, .y /- _y } {} |gt,
463
+ { .x /- _x, .y /+ _y } {} |lt,
464
+ { .x /- x, .y /- y } { .x y, .y x } cmp_msb
465
+ > ?order[];
466
+
467
+ lt? = ?{ int[] x, int[] y } { cmp /lt _ord, () xy } .xy ?{ int[] x, int[] y };
468
+ le? = ?{ int[] x, int[] y } { cmp < /lt, /eq > _ord, () xy } .xy ?{ int[] x, int[] y };
469
+ eq? = ?{ int[] x, int[] y } { cmp /eq _ord, () xy } .xy ?{ int[] x, int[] y };
470
+ gt? = ?{ int[] x, int[] y } { cmp /gt _ord, () xy } .xy ?{ int[] x, int[] y };
471
+ ge? = ?{ int[] x, int[] y } { cmp < /gt, /eq > _ord, () xy } .xy ?{ int[] x, int[] y };
472
+
473
+ plus = ?{ int[] x, int[] y } { .x int x, .y int y } <
474
+ { .x /+ x, .y /+ y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_add lsb_to_msb |+ int,
475
+ { .x /- x, .y /- y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_add lsb_to_msb |- int,
476
+ { .x /+ x, .y /- y } { .x x, .y y } _sub_signed_msb,
477
+ { .x /- x, .y /+ y } { .x y, .y x } _sub_signed_msb
478
+ > ?int[];
479
+
480
+ -- Subtracts two unsigned MSB bits[], tagging '+' if x >= y else '-' on |y - x|.
481
+ _sub_signed_msb = ?{ bits[] x, bits[] y } {
482
+ .x x,
483
+ .y y,
484
+ { .x x, .y y } cmp_msb ord
485
+ } <
486
+ { .ord /eq _ord, .x _x, .y _y } 0,
487
+ { .ord /gt _ord, .x x, .y y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_sub lsb_to_msb |+,
488
+ { .ord /lt _ord, .x x, .y y } { .y msb_to_lsb x, .x msb_to_lsb y } lsb_sub lsb_to_msb |-
489
+ > ?int[];
490
+
491
+ minus = ?{ int[] x, int[] y } { .x x, .y negate y } plus ?int[];
492
+
493
+ inc = ?int[] { () x, 1 y } plus ?int[];
494
+ dec = ?int[] { () x, 1 y } minus ?int[];
495
+
496
+ times = ?{ int[] x, int[] y } { .x int x, .y int y } <
497
+ { .x /+ x, .y /+ y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_mul lsb_to_msb |+,
498
+ { .x /- x, .y /- y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_mul lsb_to_msb |+,
499
+ { .x /+ x, .y /- y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_mul lsb_to_msb |- int,
500
+ { .x /- x, .y /+ y } { .x msb_to_lsb x, .y msb_to_lsb y } lsb_mul lsb_to_msb |- int
501
+ > ?int[];
502
+
503
+ -- Truncating signed division & remainder matching JS BigInt (/ and %):
504
+ -- x = q * y + r, sign(q) = sign(x) * sign(y), sign(r) = sign(x), y != 0.
505
+ divmod = ?{ int[] x, int[] y } { .x int x, .y int y } <
506
+ { .x /+ x, .y /+ y } { .x x, .y y } nat_divmod { .q |+ q, .r |+ r },
507
+ { .x /+ x, .y /- y } { .x x, .y y } nat_divmod { .q |- int q, .r |+ r },
508
+ { .x /- x, .y /+ y } { .x x, .y y } nat_divmod { .q |- int q, .r |- int r },
509
+ { .x /- x, .y /- y } { .x x, .y y } nat_divmod { .q |+ q, .r |- int r }
510
+ > ?{ int[] q, int[] r };
511
+
512
+ div = ?{ int[] x, int[] y } divmod .q ?int[];
513
+ mod = ?{ int[] x, int[] y } divmod .r ?int[];
514
+
515
+ gcd = ?{ int[] x, int[] y } { .x abs /+ a, .y abs /+ b } _gcd ?int[];
516
+
517
+ -- Non-negative greatest common divisor via Euclidean algorithm.
518
+ _gcd = ?{ bits[] a, bits[] b } <
519
+ { .a a, .b /_ |_ _b } .a |+,
520
+ { .a a, .b /1 b } {
521
+ .b |1 a,
522
+ { .a x, .b |1 y } nat_divmod .r b
523
+ } _gcd
524
+ > ?int[];
525
+
526
+ lcm = ?{ int[] x, int[] y } { .x abs x, .y abs y } <
527
+ { .x /+ /_ |_ _x, .y _y } 0,
528
+ { .x /+ /1 _x, .y /+ /_ |_ _y } 0,
529
+ { .x /+ /1 x, .y /+ /1 y } {
530
+ {
531
+ .x |1 |+ x,
532
+ { .x |1 |+ x, .y |1 |+ y } gcd y
533
+ } div x,
534
+ .y |1 |+ y
535
+ } times
536
+ > ?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.9.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"