@fraczak/k 8.8.0 → 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.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,17 @@ All notable changes to this project will be documented here.
5
5
  The format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
6
6
  This project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [8.9.0] — 2026-11-22 — Canonical Empty-Bit Zero Representation and BigInt-Free Integer Codec
9
+
10
+ - **Canonical Zero Representation (`{}|_` / `{}|_|+`)**:
11
+ - Represent canonical unsigned zero in `bits[]` as empty `{}` tagged with `_` (`{}|_`) and signed zero in `int[]` as `{}|_|+` (`{"+":{"_":{}}}`), eliminating the redundant `'0'` bit tag across `lib/integer.k`, `lib/generic.k` (`trim_bits`), `lib/ieee.k`, `lib/djson.k`, and `codecs/djson.mjs` (`indexToBitPath(0) => []`).
12
+ - Clean up `lib/*.k` formatting and internal helpers (`f64_trim_msb`, `dj_lsb_to_msb`, `dj_next_index`, `dj_pack_list`, `dj_parse_expect_val`).
13
+ - **BigInt-Free Integer Codec (`codecs/int.mjs`)**:
14
+ - Replace `BigInt` parsing and printing in `codecs/int.mjs` with direct 20-bit-chunk conversion over base-10^9 limbs (`decimalToBits` and `printSingleInt`), validating bit tags and `_` leaf structure strictly.
15
+ - **Benchmarks & Tests**:
16
+ - Add `scripts/perf-bigint-compare.mjs` for end-to-end decimal parse, consecutive squaring, and decimal print comparisons between JS `BigInt` and K backends (`llvm`, `llvm-pkir`, `wasm`, `pkir`, `kvm`, `js`).
17
+ - Update `tests/test-generic.mjs`, `tests/test-structural-values.mjs`, and `tests/test-repl.mjs` for the canonical zero representation and refreshed `parse_json` content hash.
18
+
8
19
  ## [8.8.0] — 2026-11-13 — Standard Library Consolidation, Tail-Recursive Conventions, and Plain Dependency Comments
9
20
 
10
21
  - **Standard Library Consolidation (`lib/`)**:
package/codecs/djson.mjs CHANGED
@@ -16,7 +16,7 @@
16
16
  * >
17
17
  *
18
18
  * - In `list`, element at index i >= 0 is stored at the canonical binary
19
- * bit-path of i (0 -> [0], 1 -> [1], 2 -> [1,0], 3 -> [1,1], ...).
19
+ * bit-path of i (0 -> [], 1 -> [1], 2 -> [1,0], 3 -> [1,1], ...).
20
20
  * - In `object`, entry with key k is stored at the prefix-free Unicode
21
21
  * character bit-path of k.
22
22
  */
@@ -167,7 +167,7 @@ function readBitsMSB(bits, offset, width) {
167
167
  }
168
168
 
169
169
  export function indexToBitPath(index) {
170
- if (index === 0) return [0];
170
+ if (index === 0) return [];
171
171
  const bits = [];
172
172
  let n = index;
173
173
  while (n > 0) {
@@ -179,7 +179,6 @@ export function indexToBitPath(index) {
179
179
  }
180
180
 
181
181
  export function bitPathToIndex(bits) {
182
- if (bits.length === 1 && bits[0] === 0) return 0;
183
182
  let n = 0;
184
183
  for (const b of bits) {
185
184
  n = n * 2 + b;
package/codecs/int.mjs CHANGED
@@ -32,12 +32,42 @@ const INT_LIST_PATTERN = patternFromFilter(`
32
32
  > = list
33
33
  `);
34
34
 
35
- // Build bits Value (MSB outermost) from a non-negative BigInt.
36
- function buildBits(n) {
37
- const bitChars = n === 0n ? ["0"] : n.toString(2).split("");
35
+ const DEC_BASE = 1_000_000_000;
36
+ const DEC_CHUNK = 9;
37
+ const BIT_CHUNK = 20;
38
+ const BIT_BASE = 1 << BIT_CHUNK; // 2^20 * 10^9 < Number.MAX_SAFE_INTEGER
39
+
40
+ // Build bits Value (MSB outermost) directly from a validated non-negative decimal string.
41
+ function decimalToBits(str) {
38
42
  let v = Value.variant("_", Value.product({}));
39
- for (let i = bitChars.length - 1; i >= 0; i--) {
40
- v = Value.variant(bitChars[i], v);
43
+ if (str === "0") return v;
44
+ const limbs = [];
45
+ const firstLen = str.length % DEC_CHUNK || DEC_CHUNK;
46
+ limbs.push(Number(str.slice(0, firstLen)));
47
+ for (let i = firstLen; i < str.length; i += DEC_CHUNK) {
48
+ limbs.push(Number(str.slice(i, i + DEC_CHUNK)));
49
+ }
50
+ let start = 0;
51
+ while (start < limbs.length) {
52
+ let rem = 0;
53
+ for (let i = start; i < limbs.length; i++) {
54
+ const cur = rem * DEC_BASE + limbs[i];
55
+ const q = Math.floor(cur / BIT_BASE);
56
+ limbs[i] = q;
57
+ rem = cur - q * BIT_BASE;
58
+ }
59
+ while (start < limbs.length && limbs[start] === 0) start++;
60
+ if (start < limbs.length) {
61
+ for (let b = 0; b < BIT_CHUNK; b++) {
62
+ v = Value.variant((rem & 1) === 1 ? "1" : "0", v);
63
+ rem >>>= 1;
64
+ }
65
+ } else {
66
+ while (rem > 0) {
67
+ v = Value.variant((rem & 1) === 1 ? "1" : "0", v);
68
+ rem >>>= 1;
69
+ }
70
+ }
41
71
  }
42
72
  return v;
43
73
  }
@@ -51,9 +81,8 @@ function parseIntStr(str) {
51
81
  if (!/^(0|[1-9][0-9]*)$/.test(str)) {
52
82
  throw new Error(`Invalid integer syntax: ${JSON.stringify(str)}`);
53
83
  }
54
- const n = BigInt(str);
55
- if (n === 0n) sign = "+";
56
- return Value.variant(sign, buildBits(n));
84
+ if (str === "0") sign = "+";
85
+ return Value.variant(sign, decimalToBits(str));
57
86
  }
58
87
 
59
88
  // Parse a list of decimal integers, e.g. "[0,1,2]" or "[]"
@@ -94,15 +123,38 @@ function printSingleInt(value) {
94
123
  throw new Error("Not a valid k int value");
95
124
  }
96
125
  const sign = value.tag;
97
- let bits = "";
126
+ const limbs = []; // base-10^9, least-significant limb at index 0
98
127
  let node = value.value;
99
128
  while (isVariant(node) && node.tag !== "_") {
100
- bits += node.tag;
101
- node = node.value;
129
+ let chunkVal = 0;
130
+ let chunkBits = 0;
131
+ while (chunkBits < BIT_CHUNK && isVariant(node) && node.tag !== "_") {
132
+ if (node.tag !== "0" && node.tag !== "1") {
133
+ throw new Error("Not a valid k int value");
134
+ }
135
+ chunkVal = (chunkVal << 1) | (node.tag === "1" ? 1 : 0);
136
+ chunkBits++;
137
+ node = node.value;
138
+ }
139
+ const mult = 1 << chunkBits;
140
+ let carry = chunkVal;
141
+ for (let i = 0; i < limbs.length; i++) {
142
+ const cur = limbs[i] * mult + carry;
143
+ carry = Math.floor(cur / DEC_BASE);
144
+ limbs[i] = cur - carry * DEC_BASE;
145
+ }
146
+ if (carry > 0) {
147
+ limbs.push(carry);
148
+ }
149
+ }
150
+ if (!isVariant(node) || node.tag !== "_" || !isProduct(node.value) || Object.keys(node.value.product).length !== 0) {
151
+ throw new Error("Not a valid k int value");
152
+ }
153
+ if (limbs.length === 0) return "0";
154
+ let digits = String(limbs[limbs.length - 1]);
155
+ for (let i = limbs.length - 2; i >= 0; i--) {
156
+ digits += String(limbs[i]).padStart(DEC_CHUNK, "0");
102
157
  }
103
- const n = bits === "" ? 0n : BigInt("0b" + bits);
104
- const digits = n.toString(10);
105
- if (n === 0n) return "0";
106
158
  return (sign === "-" ? "-" : "") + digits;
107
159
  }
108
160