@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
@@ -132,21 +132,30 @@ assert.equal(evaluate(`
132
132
  my_list[T] = ${LIST}[T];
133
133
  {{}|0 car, {}|nil cdr}|cons ?my_list[my_bit[]]
134
134
  `, { libraries: [library] }).tag, "cons");
135
- // Unexported pattern templates from a library do not leak into scope:
135
+
136
+ // Library patterns are known by hash only unless --use / useSpecs is specified.
136
137
  assert.throws(
137
138
  () => evaluate("is_bit = ?bit[]; ()", { libraries: [library] }),
138
139
  /Unknown pattern template 'bit\[\]'/
139
140
  );
140
141
 
141
- const preambleBoth = buildUsePreamble(["bit", "list"], [library]);
142
- assert.equal(evaluate(preambleBoth + `
142
+ assert.equal(evaluate(`
143
143
  is_bit = ?bit[];
144
144
  is_pair = ?${PAIR}[bit[],bit[]];
145
145
  is_pair_list = ?list[${PAIR}[bit[],bit[]]];
146
146
  b = {}|0 is_bit;
147
147
  p = {b first, {}|1 second} is_pair;
148
148
  {p car, {}|nil cdr}|cons is_pair_list
149
- `, { libraries: [library] }).tag, "cons");
149
+ `, { libraries: [library], useSpecs: ["bit", "list"] }).tag, "cons");
150
+
151
+ assert.equal(evaluate(`
152
+ is_bit = ?my_bit[];
153
+ is_pair = ?${PAIR}[my_bit[],my_bit[]];
154
+ is_pair_list = ?my_list[${PAIR}[my_bit[],my_bit[]]];
155
+ b = {}|0 is_bit;
156
+ p = {b first, {}|1 second} is_pair;
157
+ {p car, {}|nil cdr}|cons is_pair_list
158
+ `, { libraries: [library], useSpecs: ["bit:my_bit", "list:my_list"] }).tag, "cons");
150
159
  assert.ok(evaluate(`
151
160
  @A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P[X,Y] = {X x,Y y,Y z};
152
161
  {{} x,{} y,{} z} ?@A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P[{},{}]
@@ -5,33 +5,36 @@ import path from "node:path";
5
5
  import { spawnSync } from "node:child_process";
6
6
  import k from "../index.mjs";
7
7
  import { compileLibrary, loadLibrary } from "../object.mjs";
8
+ import { useSpecsFor } from "./helpers/cli.mjs";
8
9
  import { compileWasmArtifact, instantiateWasmArtifact } from "../backends/wasm/src/wasm.mjs";
9
10
  import { fromJsonValue } from "../codecs/djson.mjs";
10
11
  import { textToStringValue } from "../codecs/runtime/unicode-string.mjs";
11
12
  import { parse as utf8 } from "../codecs/utf8.mjs";
12
13
  import { encodeToWire, decodeWire } from "../codecs/runtime/codec-sdk.mjs";
13
14
 
14
- let ieee = null;
15
+ let prev = null;
15
16
  for (const file of [
16
- "lib/bit.k",
17
- "lib/bits.k",
18
- "lib/list.k",
19
- "lib/btrie.k",
20
- "lib/string.k",
17
+ "core.k",
18
+ "lib/generic.k",
21
19
  "lib/ieee.k"
22
20
  ]) {
23
- ieee = loadLibrary(compileLibrary(readFileSync(new URL(`../${file}`, import.meta.url), "utf8"), {
21
+ const fileSource = readFileSync(new URL(`../${file}`, import.meta.url), "utf8");
22
+ const uses = useSpecsFor(file);
23
+ prev = loadLibrary(compileLibrary(fileSource, {
24
24
  source: file,
25
- libraries: ieee ? [ieee] : []
25
+ libraries: prev ? [prev] : [],
26
+ use: uses
26
27
  }));
27
28
  }
28
- const library = loadLibrary(compileLibrary(readFileSync(new URL("../lib/djson.k", import.meta.url), "utf8"), {
29
+ const djsonSource = readFileSync(new URL("../lib/djson.k", import.meta.url), "utf8");
30
+ const library = loadLibrary(compileLibrary(djsonSource, {
29
31
  source: "lib/djson.k",
30
- libraries: [ieee]
32
+ libraries: [prev],
33
+ use: useSpecsFor("lib/djson.k")
31
34
  }));
32
- const wasm = await instantiateWasmArtifact(await compileWasmArtifact(library.relAlias.parseJson, { libraries: [library] }));
35
+ const wasm = await instantiateWasmArtifact(await compileWasmArtifact(library.relAlias.parse_json, { libraries: [library] }));
33
36
  const fast = text => wasm.executeValue(textToStringValue(text));
34
- const parse = k.compile(library.relAlias.parseJson, { libraries: [library] });
37
+ const parse = k.compile(library.relAlias.parse_json, { libraries: [library] });
35
38
  const payload = value => JSON.parse(JSON.stringify(value));
36
39
  function check(text, evaluate = text => parse(textToStringValue(text))) {
37
40
  const actual = evaluate(text);
@@ -84,18 +87,16 @@ const text = '{"answer":42,"ok":true}';
84
87
  const scratch = mkdtempSync(path.join(os.tmpdir(), "k-json-test-"));
85
88
  try {
86
89
  const chain = [
87
- "lib/bit.k",
88
- "lib/bits.k",
89
- "lib/list.k",
90
- "lib/btrie.k",
91
- "lib/string.k",
90
+ "core.k",
91
+ "lib/generic.k",
92
92
  "lib/ieee.k",
93
93
  "lib/djson.k"
94
94
  ];
95
95
  let prev = null;
96
96
  for (let i = 0; i < chain.length; i++) {
97
97
  const out = path.join(scratch, `step-${i}.klib`);
98
- const args = ["objects/compile.mjs", ...(prev ? ["--lib", prev] : []), chain[i], out];
98
+ const useArgs = useSpecsFor(chain[i]).flatMap((u) => ["--use", u]);
99
+ const args = ["objects/compile.mjs", ...(prev ? ["--lib", prev] : []), ...useArgs, chain[i], out];
99
100
  const built = spawnSync(process.execPath, args, {
100
101
  cwd: new URL("..", import.meta.url)
101
102
  });
@@ -104,7 +105,7 @@ try {
104
105
  prev = out;
105
106
  }
106
107
  const artifact = prev;
107
- const cli = spawnSync(process.execPath, ["k.mjs", "--lib", artifact, "--use", "parseJson", "parseJson"], {
108
+ const cli = spawnSync(process.execPath, ["k.mjs", "--lib", artifact, "--use", "parse_json", "parse_json"], {
108
109
  cwd: new URL("..", import.meta.url), input: encodeToWire(utf8(text))
109
110
  });
110
111
  assert.ifError(cli.error);
@@ -7,10 +7,8 @@ import { createState, evaluateInput } from "../backend-api.mjs";
7
7
 
8
8
  const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
9
9
  const polySources = [
10
- "lib/bit.k",
11
- "lib/bits.k",
12
- "lib/arithmetics.k",
13
- "lib/list.k",
10
+ "core.k",
11
+ "lib/integer.k",
14
12
  "Examples/poly.k"
15
13
  ];
16
14
 
@@ -63,14 +61,14 @@ async function evalK(snippet) {
63
61
  {
64
62
  const res0 = await evalK(`
65
63
  l = {{}|item0 car, {{}|item1 car, {{}|item2 car, nil cdr} cons cdr} cons cdr} cons;
66
- {0 int n, l xs} get_nth
64
+ {0 nat n, l xs} get_nth
67
65
  `);
68
66
  assert.match(res0, /\|item0/);
69
67
 
70
- const res1 = await evalK(`{1 int n, l xs} get_nth`);
68
+ const res1 = await evalK(`{1 n, l xs} get_nth`);
71
69
  assert.match(res1, /\|item1/);
72
70
 
73
- const res2 = await evalK(`{2 int n, l xs} get_nth`);
71
+ const res2 = await evalK(`{{}|_|0|1 nat n, l xs} get_nth`);
74
72
  assert.match(res2, /\|item2/);
75
73
  console.log("Test 4 (get_nth): Passed");
76
74
  }
@@ -79,7 +77,7 @@ async function evalK(snippet) {
79
77
  {
80
78
  const res = await evalK(`
81
79
  l = {{}|a car, {{}|b car, {{}|c car, {{}|d car, nil cdr} cons cdr} cons cdr} cons cdr} cons;
82
- {2 int n, l xs} split_by
80
+ {{}|_|0|1 nat n, l xs} split_by
83
81
  `);
84
82
  assert.match(res, /first/);
85
83
  assert.match(res, /second/);
@@ -90,12 +88,12 @@ async function evalK(snippet) {
90
88
  {
91
89
  const resTake = await evalK(`
92
90
  l = {{}|a car, {{}|b car, {{}|c car, nil cdr} cons cdr} cons cdr} cons;
93
- {2 int n, l xs} take
91
+ {{}|_|0|1 nat n, l xs} take
94
92
  `);
95
93
  assert.match(resTake, /\|a car.*\|b car/);
96
94
 
97
95
  const resDrop = await evalK(`
98
- {2 int n, l xs} drop
96
+ {{}|_|0|1 nat n, l xs} drop
99
97
  `);
100
98
  assert.match(resDrop, /\|c car/);
101
99
  console.log("Test 6 (take & drop): Passed");
@@ -104,7 +102,7 @@ async function evalK(snippet) {
104
102
  // Test 7: Repeat
105
103
  {
106
104
  const res = await evalK(`
107
- {3 int n, {}|val x} repeat
105
+ {{}|_|1|1 nat n, {}|val x} repeat
108
106
  `);
109
107
  assert.match(res, /\|val car.*\|val car.*\|val car/);
110
108
  console.log("Test 7 (repeat): Passed");
@@ -124,7 +122,7 @@ async function evalK(snippet) {
124
122
  {
125
123
  const res = await evalK(`
126
124
  l1 = {{}|k1 car, {{}|k2 car, nil cdr} cons cdr} cons;
127
- l2 = {1 int car, {2 int car, nil cdr} cons cdr} cons;
125
+ l2 = {1 car, {{}|_|0|1 nat car, nil cdr} cons cdr} cons;
128
126
  {l1 xs, l2 ys} zip
129
127
  `);
130
128
  assert.match(res, /\|k1 x/);
@@ -138,19 +136,22 @@ async function evalK(snippet) {
138
136
  assert.match(resNil, /\|nil/);
139
137
 
140
138
  const resSingle = await evalK(`
141
- l = {1 int car, nil cdr} cons;
139
+ l = {1 car, nil cdr} cons;
142
140
  l sort
143
141
  `);
144
142
  assert.match(resSingle, /1/);
145
143
 
146
144
  const resSplit = await evalK(`
147
- l4 = {1 int car, {2 int car, {3 int car, {4 int car, nil cdr} cons cdr} cons cdr} cons cdr} cons;
145
+ n2 = {}|_|0|1 nat;
146
+ n3 = {}|_|1|1 nat;
147
+ n4 = {}|_|0|0|1 nat;
148
+ l4 = {1 car, {n2 car, {n3 car, {n4 car, nil cdr} cons cdr} cons cdr} cons cdr} cons;
148
149
  l4 split
149
150
  `);
150
151
  assert.match(resSplit, /first.*second/);
151
152
 
152
153
  const resSorted = await evalK(`
153
- gt_int? = { { .x /"+" x, .y /"+" y } GT if, () then } .then;
154
+ gt_int? = { { .x x, .y y } gt? if, () then } .then;
154
155
  _merge = <
155
156
  {.xs /nil if, .ys then} .then,
156
157
  {.ys /nil if, .xs then} .then,
@@ -167,7 +168,11 @@ async function evalK(snippet) {
167
168
  |cons split {.first _sort xs, .second _sort ys} _merge
168
169
  >
169
170
  > ? list[X];
170
- l = {5 int car, {2 int car, {4 int car, {1 int car, {3 int car, nil cdr} cons cdr} cons cdr} cons cdr} cons cdr} cons;
171
+ n2 = {}|_|0|1 nat;
172
+ n3 = {}|_|1|1 nat;
173
+ n4 = {}|_|0|0|1 nat;
174
+ n5 = {}|_|1|0|1 nat;
175
+ l = {n5 car, {n2 car, {n4 car, {1 car, {n3 car, nil cdr} cons cdr} cons cdr} cons cdr} cons cdr} cons;
171
176
  l _sort
172
177
  `);
173
178
  assert.match(resSorted, /\|1.*\|0\|1.*\|1\|1.*\|0\|0\|1.*\|1\|0\|1/);
@@ -35,7 +35,7 @@ assert(html.includes("<style>"), "repl.html must contain inlined CSS styles");
35
35
  assert(html.includes('<script type="module">'), "repl.html must contain inlined module script");
36
36
  assert(!html.includes('<script src="http'), "repl.html must NOT contain external remote scripts");
37
37
  assert(html.includes("wasm-in-process"), "repl.html must mention wasm-in-process engine");
38
- assert(html.includes("arithmetics.k"), "repl.html must embed standard examples");
38
+ assert(html.includes("nat.k"), "repl.html must embed standard examples");
39
39
  assert(html.includes("btn-codecs"), "repl.html must contain Codecs button");
40
40
  assert(html.includes("codecs-modal"), "repl.html must contain Codecs modal");
41
41
  assert(html.includes("enabled-codecs-grid"), "repl.html must contain enabled codecs grid");
@@ -150,63 +150,60 @@ if (chromiumBin) {
150
150
  // Verify VFS curated files
151
151
  const vfsList = await evaluateAsync(`window.kRepl.getAllVfsFiles().sort()`);
152
152
  const expectedFiles = [
153
- "arithmetics.k",
154
- "bit.k",
155
- "bits.k",
156
- "btrie.k",
157
153
  "core.k",
158
154
  "djson.k",
159
155
  "djson.mjs",
160
- "ieee.k",
156
+ "generic.k",
161
157
  "ieee-arithmetics.k",
158
+ "ieee.k",
162
159
  "ieee.mjs",
163
160
  "int.mjs",
161
+ "integer.k",
164
162
  "json.mjs",
165
- "list.k",
163
+ "nat.k",
166
164
  "poly.k",
167
- "string.k",
168
165
  "unit.mjs",
169
166
  "utf8.mjs"
170
167
  ].sort();
171
168
  assert.deepStrictEqual(vfsList, expectedFiles, `getAllVfsFiles() must match curated files list, got: ${JSON.stringify(vfsList)}`);
172
169
 
173
- // Test :load bit.k, bits.k, arithmetics.k
174
- const loadOut = await evaluateAsync(`(async () => {
175
- await window.kRepl.executeCommand(":load bit.k");
176
- await window.kRepl.executeCommand(":load bits.k");
177
- await window.kRepl.executeCommand(":load arithmetics.k");
170
+ // Test :load Examples/nat.k (then :reset so 0 alias does not linger)
171
+ const loadNatOut = await evaluateAsync(`(async () => {
172
+ await window.kRepl.executeCommand(":load Examples/nat.k");
178
173
  const lines = Array.from(document.querySelectorAll(".entry-line")).map(el => el.textContent);
174
+ await window.kRepl.executeCommand(":reset");
179
175
  return lines[lines.length - 1];
180
176
  })()`);
181
- assert(loadOut.includes("loaded arithmetics.k"), `Expected loaded arithmetics.k, got: ${loadOut}`);
177
+ assert(loadNatOut.includes("loaded Examples/nat.k"), `Expected loaded Examples/nat.k, got: ${loadNatOut}`);
182
178
 
183
- // Test :load Examples/arithmetics.k
184
- const loadExamplesArithOut = await evaluateAsync(`(async () => {
185
- await window.kRepl.executeCommand(":load Examples/arithmetics.k");
179
+ // Test :load core.k, integer.k
180
+ const loadOut = await evaluateAsync(`(async () => {
181
+ await window.kRepl.executeCommand(":load core.k");
182
+ await window.kRepl.executeCommand(":load integer.k");
186
183
  const lines = Array.from(document.querySelectorAll(".entry-line")).map(el => el.textContent);
187
184
  return lines[lines.length - 1];
188
185
  })()`);
189
- assert(loadExamplesArithOut.includes("loaded Examples/arithmetics.k"), `Expected loaded Examples/arithmetics.k, got: ${loadExamplesArithOut}`);
186
+ assert(loadOut.includes("loaded integer.k"), `Expected loaded integer.k, got: ${loadOut}`);
190
187
 
191
- // Test evaluating {1 int x, 1 int y} karatsuba
192
- const karatsubaVal = await evaluateAsync(`(async () => {
193
- await window.kRepl.executeCommand("{1 int x, 1 int y} karatsuba");
188
+ // Test evaluating {1 x, 1 y} times
189
+ const timesVal = await evaluateAsync(`(async () => {
190
+ await window.kRepl.executeCommand("{1 x, 1 y} times");
194
191
  const lines = Array.from(document.querySelectorAll(".entry-line")).map(el => el.textContent);
195
192
  return lines[lines.length - 1];
196
193
  })()`);
197
- assert(karatsubaVal.includes("{}|_|1|+"), `Expected {}|_|1|+ in output, got: ${karatsubaVal}`);
194
+ assert(timesVal.includes("{}|_|1|+"), `Expected {}|_|1|+ in output, got: ${timesVal}`);
198
195
 
199
- // Test evaluating 10
196
+ // Test evaluating {}|_|0|1|0|1 (bits 10)
200
197
  const valOut = await evaluateAsync(`(async () => {
201
- await window.kRepl.executeCommand("10");
198
+ await window.kRepl.executeCommand("{}|_|0|1|0|1");
202
199
  const lines = Array.from(document.querySelectorAll(".entry-line")).map(el => el.textContent);
203
200
  return lines[lines.length - 1];
204
201
  })()`);
205
- assert(valOut.includes("{}|_|0|1|0|1"), `Expected 10 in output, got: ${valOut}`);
202
+ assert(valOut.includes("{}|_|0|1|0|1"), `Expected 10 bits in output, got: ${valOut}`);
206
203
 
207
- // Test {10 int x, 5 int y} plus
204
+ // Test {{}|_|0|1|0|1 nat x, {}|_|1|0|1 nat y} plus
208
205
  const plusOut = await evaluateAsync(`(async () => {
209
- await window.kRepl.executeCommand("{10 int x, 5 int y} plus");
206
+ await window.kRepl.executeCommand("{{}|_|0|1|0|1 nat x, {}|_|1|0|1 nat y} plus");
210
207
  const lines = Array.from(document.querySelectorAll(".entry-line")).map(el => el.textContent);
211
208
  return lines[lines.length - 1];
212
209
  })()`);
@@ -232,9 +229,9 @@ if (chromiumBin) {
232
229
  const badgeCount = await evaluateAsync(`document.getElementById("codecs-count-badge")?.textContent`);
233
230
  assert.strictEqual(badgeCount, "1", "Codecs badge count should be 1 after loading int.mjs codec");
234
231
 
235
- // Test evaluating 10 int with int.mjs codec active -> output should format using int.mjs serializer
232
+ // Test evaluating {}|_|0|1|0|1 nat with int.mjs codec active -> output should format using int.mjs serializer
236
233
  const formattedVal = await evaluateAsync(`(async () => {
237
- await window.kRepl.executeCommand("10 int");
234
+ await window.kRepl.executeCommand("{}|_|0|1|0|1 nat");
238
235
  const lines = Array.from(document.querySelectorAll(".entry-line")).map(el => el.textContent);
239
236
  return lines[lines.length - 1];
240
237
  })()`);
@@ -334,10 +331,10 @@ if (chromiumBin) {
334
331
 
335
332
  // Test JSON codec with float64 in browser: :input json.mjs then {"a":"Woj","n":123}
336
333
  await evaluateAsync(`(async () => {
337
- await window.kRepl.executeCommand(":load list.k");
338
- await window.kRepl.executeCommand(":load btrie.k");
339
- await window.kRepl.executeCommand(":load string.k");
334
+ await window.kRepl.executeCommand(":load core.k");
335
+ await window.kRepl.executeCommand(":load generic.k");
340
336
  await window.kRepl.executeCommand(":load ieee.k");
337
+ await window.kRepl.executeCommand(":load djson.k");
341
338
  await window.kRepl.executeCommand(":codec load json.mjs");
342
339
  await window.kRepl.executeCommand(":input json.mjs");
343
340
  })()`);
@@ -398,16 +395,14 @@ if (chromiumBin) {
398
395
  })()`);
399
396
  assert(ieeeResult.includes('ieee.mjs: 3.14159'), `Expected ieee.mjs: 3.14159, got: ${ieeeResult}`);
400
397
 
401
- // Test :load poly.k and evaluate '10 int (gen_list sort)' in WASM mode
398
+ // Test :load poly.k and evaluate '{}|_|0|1|0|1 nat (gen_list sort)' in WASM mode
402
399
  const sortListResult = await evaluateAsync(`(async () => {
403
400
  await window.kRepl.executeCommand(":reset");
404
- await window.kRepl.executeCommand(":load bit.k");
405
- await window.kRepl.executeCommand(":load bits.k");
406
- await window.kRepl.executeCommand(":load arithmetics.k");
407
- await window.kRepl.executeCommand(":load list.k");
401
+ await window.kRepl.executeCommand(":load core.k");
402
+ await window.kRepl.executeCommand(":load integer.k");
408
403
  await window.kRepl.executeCommand(":load poly.k");
409
404
  await window.kRepl.executeCommand(":codec load int.mjs");
410
- await window.kRepl.executeCommand("10 int (gen_list sort)");
405
+ await window.kRepl.executeCommand("{}|_|0|1|0|1 nat (gen_list sort)");
411
406
  const lines = Array.from(document.querySelectorAll(".entry-line")).map(el => el.textContent);
412
407
  return lines[lines.length - 1];
413
408
  })()`);
@@ -415,7 +410,7 @@ if (chromiumBin) {
415
410
 
416
411
  // Test deep tail-loop list generation and compaction (10,000 elements) in browser WASM mode
417
412
  const deepWasmResult = await evaluateAsync(`(async () => {
418
- await window.kRepl.executeCommand("{{{10 int x, 10 int y} times x, 10 int y} times x, 10 int y} times (gen_list car)");
413
+ await window.kRepl.executeCommand("ten = {}|_|0|1|0|1 nat; {{{ten x, ten y} times x, ten y} times x, ten y} times (gen_list car)");
419
414
  const lines = Array.from(document.querySelectorAll(".entry-line")).map(el => el.textContent);
420
415
  return lines[lines.length - 1];
421
416
  })()`);
@@ -510,12 +505,12 @@ if (chromiumBin) {
510
505
  assert.strictEqual(treeCheck.unfoldedHasE, true, "Unfolding level 4 should lazily populate its children");
511
506
 
512
507
  // 2. Test recursive pattern tree with cycle detection
513
- await evaluateAsync(`window.kRepl.executeCommand("10")`);
508
+ await evaluateAsync(`window.kRepl.executeCommand("{}|_|0|1|0|1 ?bits[]")`);
514
509
  const patCycleCheck = await evaluateAsync(`(() => {
515
510
  const entries = document.querySelectorAll(".terminal-entry");
516
511
  const lastEntry = entries[entries.length - 1];
517
512
  const patSection = lastEntry.querySelector(".k-tree-pat-section");
518
- if (!patSection) return { error: "No pattern section for 10" };
513
+ if (!patSection) return { error: "No pattern section for {}|_|0|1|0|1 ?bits[]" };
519
514
 
520
515
  const cycleBadges = patSection.querySelectorAll(".k-badge-cycle");
521
516
  const varDefs = patSection.querySelectorAll(".k-pat-vardef");
@@ -529,13 +524,13 @@ if (chromiumBin) {
529
524
  assert.strictEqual(patCycleCheck.hasCycleBadge, true, "Pattern tree for recursive type should have cycle badge");
530
525
  assert.strictEqual(patCycleCheck.hasVarDef, true, "Pattern tree for recursive type should have variable definition (=X0)");
531
526
 
532
- // 3. Test variant chain depth limits and Expand All / Collapse All on 10
527
+ // 3. Test variant chain depth limits and Expand All / Collapse All on {}|_|0|1|0|1
533
528
  const variantDepthAndActionsCheck = await evaluateAsync(`(() => {
534
529
  const entries = document.querySelectorAll(".terminal-entry");
535
530
  const lastEntry = entries[entries.length - 1];
536
531
  const valSection = lastEntry.querySelector(".k-tree-value-section");
537
532
 
538
- // Hierarchy for 10 (variant chain: + -> 1 -> 0 -> 1 -> ...)
533
+ // Hierarchy for {}|_|0|1|0|1 (variant chain: 1 -> 0 -> 1 -> 0 -> _)
539
534
  const v1 = valSection?.querySelector(".k-tree-section-body > .k-tree-node.k-tree-variant");
540
535
  const v2 = v1?.querySelector(".k-tree-children .k-tree-node.k-tree-variant");
541
536
  const v3 = v2?.querySelector(".k-tree-children .k-tree-node.k-tree-variant");
@@ -799,7 +794,7 @@ if (chromiumBin) {
799
794
  const wasmLenCheck = await evaluateAsync(`(async () => {
800
795
  await window.kRepl.executeCommand(":engine wasm");
801
796
  await window.kRepl.executeCommand(":codec load int.mjs");
802
- await window.kRepl.executeCommand("len = {()list,0 int len} len_; len_ = ?X <{.list/nil if, .len then} .then, {.list/cons.cdr list, .len inc len} ?X len_ >;");
797
+ await window.kRepl.executeCommand("len = {()list,0 nat len} len_; len_ = ?X <{.list/nil if, .len then} .then, {.list/cons.cdr list, .len inc len} ?X len_ >;");
803
798
  await window.kRepl.executeCommand(":input int.mjs [1,2,3]");
804
799
  await window.kRepl.executeCommand("len");
805
800
  const lines = Array.from(document.querySelectorAll(".entry-line")).map(el => el.textContent);
@@ -309,10 +309,9 @@ assert.equal(tcoState.value.toJSON(), "zero");
309
309
 
310
310
  // Verify loading library and evaluating constant/recursive relation from library
311
311
  const arithState = createState();
312
- await evaluateInput(":load lib/bit.k", arithState);
313
- await evaluateInput(":load lib/bits.k", arithState);
314
- output = await evaluateInput(":load lib/arithmetics.k", arithState);
315
- assert.equal(output[0], "loaded lib/arithmetics.k");
312
+ await evaluateInput(":load core.k", arithState);
313
+ output = await evaluateInput(":load lib/integer.k", arithState);
314
+ assert.equal(output[0], "loaded lib/integer.k");
316
315
  output = await evaluateInput(":pat bits", arithState);
317
316
  assert.equal(
318
317
  output[0],
@@ -325,9 +324,9 @@ assert.equal(
325
324
  "int[] = <<X0 0, X0 1, {} _>=X0 +, X0 ->; -- " +
326
325
  "@Nws3vysWqC9PznraTP4kWPp44Rd77NAqrx9dgbtHAjGA[]"
327
326
  );
328
- output = await evaluateInput("10", arithState);
329
- assert.match(output[0], /^\{\}\|_\|0\|1\|0\|1/);
330
- output = await evaluateInput("{10 int x, 5 int y} plus", arithState);
327
+ output = await evaluateInput("{}|_|0|1|0|1 nat", arithState);
328
+ assert.match(output[0], /^\{\}\|_\|0\|1\|0\|1\|\+/);
329
+ output = await evaluateInput("{{}|_|0|1|0|1 nat x, {}|_|1|0|1 nat y} plus", arithState);
331
330
  assert.match(output[0], /^\{\}\|_\|1\|1\|1\|1\|\+/);
332
331
 
333
332
  // Verify codecs loading, listing, and unloading
@@ -385,9 +384,8 @@ assert.match(output[0], /^\{\}\|a/);
385
384
 
386
385
  // Explicit loading of dependencies and codec testing (no auto-loading)
387
386
  const intState = createState();
388
- await evaluateInput(":load lib/bit.k", intState);
389
- await evaluateInput(":load lib/bits.k", intState);
390
- await evaluateInput(":load lib/arithmetics.k", intState);
387
+ await evaluateInput(":load core.k", intState);
388
+ await evaluateInput(":load lib/integer.k", intState);
391
389
  output = await evaluateInput(":codec load ./codecs/int.mjs", intState);
392
390
  assert.match(output[0], /^loaded codec int\.mjs/);
393
391
  output = await evaluateInput(":input int.mjs", intState);
@@ -406,7 +404,7 @@ output = await evaluateInput("test utf8", utf8AutoState);
406
404
  assert.match(output[0], /utf8\.mjs: test utf8/);
407
405
 
408
406
  const ieeeAutoState = createState();
409
- for (const file of ["lib/bit.k", "lib/bits.k", "lib/list.k", "lib/btrie.k", "lib/string.k", "lib/ieee.k"]) {
407
+ for (const file of ["core.k", "lib/ieee.k"]) {
410
408
  await evaluateInput(`:load ${file}`, ieeeAutoState);
411
409
  }
412
410
  output = await evaluateInput(":codec load ./codecs/ieee.mjs", ieeeAutoState);
@@ -416,7 +414,7 @@ output = await evaluateInput(":input ieee.mjs 1.5", ieeeAutoState);
416
414
  assert.match(output[0], /ieee\.mjs: 1\.5/);
417
415
 
418
416
  const jsonInputState = createState();
419
- for (const file of ["lib/bit.k", "lib/bits.k", "lib/list.k", "lib/btrie.k", "lib/string.k", "lib/ieee.k"]) {
417
+ for (const file of ["core.k", "lib/generic.k", "lib/ieee.k", "lib/djson.k"]) {
420
418
  await evaluateInput(`:load ${file}`, jsonInputState);
421
419
  }
422
420
  await evaluateInput(":codec load ./codecs/json.mjs", jsonInputState);
@@ -475,7 +473,7 @@ assert.equal(promptForState(unitState), "unit.mjs> ");
475
473
  output = await evaluateInput("{}", unitState);
476
474
  assert.match(output[0], /unit\.mjs: \{\}/);
477
475
 
478
- // Verify that poly.k does NOT auto-load arithmetics.k (no autohooks)
476
+ // Verify that poly.k does NOT auto-load lib/integer.k (no autohooks)
479
477
  const polyFreshState = createState();
480
478
  await assert.rejects(
481
479
  () => evaluateInput(":load Examples/poly.k", polyFreshState),
@@ -484,7 +482,7 @@ await assert.rejects(
484
482
 
485
483
  // Explicitly loading dependencies then poly.k succeeds
486
484
  const polyExplicitState = createState();
487
- for (const file of ["lib/bit.k", "lib/bits.k", "lib/arithmetics.k", "lib/list.k"]) {
485
+ for (const file of ["core.k", "lib/integer.k"]) {
488
486
  await evaluateInput(`:load ${file}`, polyExplicitState);
489
487
  }
490
488
  output = await evaluateInput(
@@ -498,7 +496,7 @@ assert.ok(
498
496
  );
499
497
  assert.ok(
500
498
  polyExplicitState.relAliases.int,
501
- "arithmetics int loaded"
499
+ "integer int loaded"
502
500
  );
503
501
 
504
502
  // Verify timing reporting is always on and reflects active engine
@@ -569,11 +567,10 @@ output = await evaluateInput("not = </true | false, /false | true >;\n{} | true
569
567
  assert.match(output[0], /^\{\}\|false/);
570
568
  assert.match(output[1], /\(js\)/);
571
569
 
572
- await evaluateInput(":load lib/bit.k", jsState);
573
- await evaluateInput(":load lib/bits.k", jsState);
574
- output = await evaluateInput(":load lib/arithmetics.k", jsState);
575
- assert.equal(output[0], "loaded lib/arithmetics.k");
576
- output = await evaluateInput("{ 10 int x, 5 int y } plus", jsState);
570
+ await evaluateInput(":load core.k", jsState);
571
+ output = await evaluateInput(":load lib/integer.k", jsState);
572
+ assert.equal(output[0], "loaded lib/integer.k");
573
+ output = await evaluateInput("{ {}|_|0|1|0|1 nat x, {}|_|1|0|1 nat y } plus", jsState);
577
574
  assert.match(output[0], /^\{\}\|_\|1\|1\|1\|1\|\+/);
578
575
  assert.match(output[1], /\(js\)/);
579
576
 
@@ -595,19 +592,17 @@ output = await evaluateInput(
595
592
  assert.match(output[0], /^\{\}\|false/);
596
593
  assert.match(output[1], /\(pkir\)/);
597
594
 
598
- await evaluateInput(":load lib/bit.k", pkirState);
599
- await evaluateInput(":load lib/bits.k", pkirState);
600
- output = await evaluateInput(":load lib/arithmetics.k", pkirState);
601
- assert.equal(output[0], "loaded lib/arithmetics.k");
602
- output = await evaluateInput("{ 10 int x, 5 int y } plus", pkirState);
595
+ await evaluateInput(":load core.k", pkirState);
596
+ output = await evaluateInput(":load lib/integer.k", pkirState);
597
+ assert.equal(output[0], "loaded lib/integer.k");
598
+ output = await evaluateInput("{ {}|_|0|1|0|1 nat x, {}|_|1|0|1 nat y } plus", pkirState);
603
599
  assert.match(output[0], /^\{\}\|_\|1\|1\|1\|1\|\+/);
604
600
  assert.match(output[1], /\(pkir\)/);
605
601
 
606
602
  // Verify open product projection in wasm engine
607
603
  const wasmListState = createState({ engine: "wasm" });
608
- await evaluateInput(":load lib/bit.k", wasmListState);
609
- await evaluateInput(":load lib/bits.k", wasmListState);
610
- await evaluateInput(":load lib/arithmetics.k", wasmListState);
604
+ await evaluateInput(":load core.k", wasmListState);
605
+ await evaluateInput(":load lib/integer.k", wasmListState);
611
606
  await evaluateInput(":codec load codecs/int.mjs", wasmListState);
612
607
  await evaluateInput("len = {()list,0 int len} len_; len_ = ?X <{.list/nil if, .len then} .then, {.list/cons.cdr list, .len inc len} ?X len_ >;", wasmListState);
613
608
  await evaluateInput(":input int.mjs [3,3,3]", wasmListState);
@@ -626,10 +621,9 @@ assert.equal(output[0], "output limit set to 40");
626
621
  output = await evaluateInput(":limit", limitState);
627
622
  assert.equal(output[0], "Current output limit: 40");
628
623
 
629
- await evaluateInput(":load lib/bit.k", limitState);
630
- await evaluateInput(":load lib/bits.k", limitState);
631
- output = await evaluateInput(":load lib/arithmetics.k", limitState);
632
- output = await evaluateInput("{ 10 int x, 5 int y } plus", limitState);
624
+ await evaluateInput(":load core.k", limitState);
625
+ output = await evaluateInput(":load lib/integer.k", limitState);
626
+ output = await evaluateInput("{ {}|_|0|1|0|1 nat x, {}|_|1|0|1 nat y } plus", limitState);
633
627
  assert(output[0].includes("..."));
634
628
  assert(output[0].length <= 80);
635
629
 
@@ -640,7 +634,7 @@ assert.equal(output[0], "Current output limit: none");
640
634
 
641
635
  // Verify lib/djson.k (json?, get, nth, set, del)
642
636
  const djsonKState = createState({ engine: "wasm" });
643
- for (const file of ["lib/bit.k", "lib/bits.k", "lib/list.k", "lib/btrie.k", "lib/string.k", "lib/ieee.k"]) {
637
+ for (const file of ["core.k", "lib/generic.k", "lib/ieee.k"]) {
644
638
  await evaluateInput(`:load ${file}`, djsonKState);
645
639
  }
646
640
  output = await evaluateInput(":load lib/djson.k", djsonKState);
@@ -651,8 +645,8 @@ await evaluateInput(
651
645
  djsonKState
652
646
  );
653
647
  output = await evaluateInput(
654
- "{{0 index, () json} nth /string key, " +
655
- "{1 index, () json} nth json} get",
648
+ "{{{}|_|0 index, () json} nth /string key, " +
649
+ "{{}|_|1 index, () json} nth json} get",
656
650
  djsonKState
657
651
  );
658
652
  assert.match(output[0], /djson\.mjs: 30/);
@@ -662,8 +656,8 @@ await evaluateInput(
662
656
  djsonKState
663
657
  );
664
658
  output = await evaluateInput(
665
- "{{0 index, () json} nth /string key, " +
666
- "{1 index, () json} nth json} del",
659
+ "{{{}|_|0 index, () json} nth /string key, " +
660
+ "{{}|_|1 index, () json} nth json} del",
667
661
  djsonKState
668
662
  );
669
663
  assert.match(output[0], /djson\.mjs: \{"name":"Alice"\}/);
@@ -781,10 +775,9 @@ assert.equal(
781
775
  "@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm[A, B]"
782
776
  );
783
777
 
784
- await evaluateInput(":load lib/bit.k", patState);
785
- await evaluateInput(":load lib/bits.k", patState);
786
- output = await evaluateInput(":load lib/list.k", patState);
787
- assert.equal(output[0], "loaded lib/list.k");
778
+ await evaluateInput(":load core.k", patState);
779
+ output = await evaluateInput(":load lib/generic.k", patState);
780
+ assert.equal(output[0], "loaded lib/generic.k");
788
781
  output = await evaluateInput(":patterns", patState);
789
782
  assert(output[0].includes("bit[] = @Mjusy"));
790
783
  assert(output[0].includes("list[X] = @JagVex"));
@@ -995,40 +988,34 @@ assert.equal(
995
988
  output[0],
996
989
  "reverse = ?<{(...) car, X0 cdr} cons, {} nil>=X0 " +
997
990
  "{() x, nil y} rev_append ?X0; " +
998
- "-- @peiZq2evd5u4Qk42AT3W4YmWZZ3JAeXNKTnmDtpb9EZh"
991
+ "-- @ekNJ8QvXWXi9J83HXXA7XrtgWjnZ4VYYVUxKRRkQV942"
999
992
  );
1000
993
  await evaluateInput(":max-pat-length 30", polyExplicitState);
1001
- output = await evaluateInput(":rel car", polyExplicitState);
1002
- assert.equal(
1003
- output[0],
1004
- "car = ?@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe[X0] " +
1005
- "/cons .car ?X0; " +
1006
- "-- @zjRvxpKqktzNfR3VYSqq4bF9XLQTnCp4MpP82JLmysaP"
1007
- );
994
+ await evaluateInput(":max-pat-length 30", djsonKState);
1008
995
  output = await evaluateInput(":rel reverse", polyExplicitState);
1009
996
  assert.equal(
1010
997
  output[0],
1011
998
  "reverse = ?@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe[X0] " +
1012
999
  "{() x, nil y} rev_append " +
1013
1000
  "?@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe[X0]; " +
1014
- "-- @peiZq2evd5u4Qk42AT3W4YmWZZ3JAeXNKTnmDtpb9EZh"
1001
+ "-- @ekNJ8QvXWXi9J83HXXA7XrtgWjnZ4VYYVUxKRRkQV942"
1015
1002
  );
1016
- output = await evaluateInput(":rel nil?", polyExplicitState);
1003
+ output = await evaluateInput(":rel nil?", djsonKState);
1017
1004
  assert.equal(
1018
1005
  output[0],
1019
- "nil? = ?@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe[(...)] " +
1020
- "un_nil nil " +
1021
- "?@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe[(...)]; " +
1022
- "-- @BqMbBgmNtqci4fKjrUh5dwMrAehZcGSeEpEdLaMeCXSA"
1006
+ "nil? = ?<(...) nil, ...>=X0 " +
1007
+ "/nil |nil " +
1008
+ "?X0; " +
1009
+ "-- @5Qr8JE4UUR2XhasfEd7cyV4SFwTpQYvzPdRWDLeJKy5e"
1023
1010
  );
1024
- output = await evaluateInput(":rel parseJson", djsonKState);
1011
+ output = await evaluateInput(":rel parse_json", djsonKState);
1025
1012
  assert.equal(
1026
1013
  output[0],
1027
- "parseJson = " +
1014
+ "parse_json = " +
1028
1015
  "?@Pt3MwQwzUPxQKu58zXR3qUBHWTzfycx5bSZGzcK6EZQK[] " +
1029
- "json_value {.rest ws un_nil end, .value value} .value " +
1016
+ "dj_json_value {.rest dj_ws /nil _, .value value} .value " +
1030
1017
  "?@P5C7TQKTSPnFUkYkYzTudJamxAQ3T3fHbvZNqwWMfAA2[]; " +
1031
- "-- @BNjpkLgR3SxNvVMuAE9xkBxvJFhaDUhaHHsBBjmx4RRK"
1018
+ "-- @w4Qr99UxapJGFraYLvGkAsc9VZFvsSsUV8i5isvG5Z6F"
1032
1019
  );
1033
1020
  const shortThresholdState = createState();
1034
1021
  output = await evaluateInput(":max-pat-length", shortThresholdState);