@fraczak/k 8.6.1 → 8.7.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 (71) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/DOCS/CODECS.md +20 -14
  3. package/DOCS/FILE_FORMATS.md +79 -12
  4. package/DOCS/GETTING_STARTED.md +17 -8
  5. package/DOCS/PATTERNS.md +17 -0
  6. package/DOCS/REPL.md +13 -7
  7. package/Examples/arithmetics.k +81 -468
  8. package/Examples/ieee-arithmetics.k +37 -33
  9. package/Examples/poly.k +107 -104
  10. package/README.md +19 -11
  11. package/backend-api.mjs +3 -1
  12. package/backends/llvm/README.md +1 -1
  13. package/backends/llvm/bin/k-llvm-build.mjs +5 -5
  14. package/backends/llvm/bin/k-llvm-compile.mjs +4 -4
  15. package/backends/llvm/bin/k-llvm.mjs +6 -6
  16. package/backends/llvm/src/cli.mjs +17 -12
  17. package/backends/llvm/tests/integration.sh +4 -1
  18. package/backends/wasm/README.md +3 -3
  19. package/backends/wasm/bin/k-wasm-compile.mjs +4 -4
  20. package/backends/wasm/bin/k-wasm.mjs +4 -4
  21. package/backends/wasm/src/cli.mjs +12 -10
  22. package/backends/wasm/src/wasm.mjs +6 -2
  23. package/backends/wasm/tests/integration.sh +8 -3
  24. package/browser/shims/fs.mjs +19 -3
  25. package/canonical-pattern.mjs +217 -57
  26. package/core.k +2 -1
  27. package/harness/suites/poly.mjs +14 -30
  28. package/k.mjs +17 -13
  29. package/kvm.mjs +13 -9
  30. package/lib/arithmetics.k +287 -0
  31. package/lib/bit.k +68 -0
  32. package/lib/bits.k +221 -0
  33. package/lib/btrie.k +187 -0
  34. package/lib/djson.k +326 -0
  35. package/lib/ieee.k +709 -0
  36. package/lib/list.k +49 -0
  37. package/lib/string.k +588 -0
  38. package/named-patterns.mjs +2 -2
  39. package/object.mjs +51 -9
  40. package/objects/README.md +10 -3
  41. package/objects/compile.mjs +10 -8
  42. package/package.json +1 -1
  43. package/repl.mjs +28 -6
  44. package/scripts/benchmark-idempotency.mjs +22 -15
  45. package/scripts/benchmark.mjs +29 -10
  46. package/scripts/build-repl-html.mjs +41 -13
  47. package/scripts/perf-ieee.mjs +9 -1
  48. package/scripts/perf-int.mjs +10 -17
  49. package/scripts/perf-multiplication.mjs +9 -15
  50. package/scripts/perf-poly.mjs +16 -32
  51. package/scripts/run-tests.mjs +2 -2
  52. package/scripts/test-package.mjs +8 -2
  53. package/tests/helpers/cli.mjs +11 -3
  54. package/tests/integration.sh +9 -4
  55. package/tests/test-arithmetics.mjs +4 -2
  56. package/tests/test-djson-methods.mjs +16 -12
  57. package/tests/test-ieee-arithmetic.mjs +11 -3
  58. package/tests/test-ieee-text.mjs +15 -4
  59. package/tests/test-k-object.mjs +11 -3
  60. package/tests/test-karatsuba.mjs +1 -1
  61. package/tests/test-kvm-polymorphic.mjs +54 -34
  62. package/tests/test-lib.mjs +115 -0
  63. package/tests/test-named-patterns.mjs +12 -3
  64. package/tests/test-parse-json.mjs +33 -13
  65. package/tests/test-poly.mjs +10 -4
  66. package/tests/test-repl-html.mjs +18 -7
  67. package/tests/test-repl.mjs +50 -35
  68. package/Examples/djson.k +0 -894
  69. package/Examples/ieee.k +0 -969
  70. package/Examples/karatsuba-mult.k +0 -227
  71. package/Examples/list.k +0 -10
@@ -6,14 +6,20 @@ import { fileURLToPath } from "node:url";
6
6
  import { createState, evaluateInput } from "../backend-api.mjs";
7
7
 
8
8
  const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
9
- const arithmeticsPath = path.join(root, "Examples/arithmetics.k");
10
- const polyPath = path.join(root, "Examples/poly.k");
9
+ const polySources = [
10
+ "lib/bit.k",
11
+ "lib/bits.k",
12
+ "lib/arithmetics.k",
13
+ "lib/list.k",
14
+ "Examples/poly.k"
15
+ ];
11
16
 
12
17
  console.log("==> Running Polymorphic List Tests (Examples/poly.k)");
13
18
 
14
19
  const state = createState();
15
- await evaluateInput(`:load ${arithmeticsPath}`, state);
16
- await evaluateInput(`:load ${polyPath}`, state);
20
+ for (const relPath of polySources) {
21
+ await evaluateInput(`:load ${path.join(root, relPath)}`, state);
22
+ }
17
23
 
18
24
  async function evalK(snippet) {
19
25
  const res = await evaluateInput(snippet, state);
@@ -151,6 +151,9 @@ if (chromiumBin) {
151
151
  const vfsList = await evaluateAsync(`window.kRepl.getAllVfsFiles().sort()`);
152
152
  const expectedFiles = [
153
153
  "arithmetics.k",
154
+ "bit.k",
155
+ "bits.k",
156
+ "btrie.k",
154
157
  "core.k",
155
158
  "djson.k",
156
159
  "djson.mjs",
@@ -159,28 +162,31 @@ if (chromiumBin) {
159
162
  "ieee.mjs",
160
163
  "int.mjs",
161
164
  "json.mjs",
162
- "karatsuba-mult.k",
165
+ "list.k",
163
166
  "poly.k",
167
+ "string.k",
164
168
  "unit.mjs",
165
169
  "utf8.mjs"
166
170
  ].sort();
167
171
  assert.deepStrictEqual(vfsList, expectedFiles, `getAllVfsFiles() must match curated files list, got: ${JSON.stringify(vfsList)}`);
168
172
 
169
- // Test :load arithmetics.k
173
+ // Test :load bit.k, bits.k, arithmetics.k
170
174
  const loadOut = await evaluateAsync(`(async () => {
175
+ await window.kRepl.executeCommand(":load bit.k");
176
+ await window.kRepl.executeCommand(":load bits.k");
171
177
  await window.kRepl.executeCommand(":load arithmetics.k");
172
178
  const lines = Array.from(document.querySelectorAll(".entry-line")).map(el => el.textContent);
173
179
  return lines[lines.length - 1];
174
180
  })()`);
175
181
  assert(loadOut.includes("loaded arithmetics.k"), `Expected loaded arithmetics.k, got: ${loadOut}`);
176
182
 
177
- // Test :load karatsuba-mult.k
178
- const loadKaratsubaOut = await evaluateAsync(`(async () => {
179
- await window.kRepl.executeCommand(":load karatsuba-mult.k");
183
+ // Test :load Examples/arithmetics.k
184
+ const loadExamplesArithOut = await evaluateAsync(`(async () => {
185
+ await window.kRepl.executeCommand(":load Examples/arithmetics.k");
180
186
  const lines = Array.from(document.querySelectorAll(".entry-line")).map(el => el.textContent);
181
187
  return lines[lines.length - 1];
182
188
  })()`);
183
- assert(loadKaratsubaOut.includes("loaded karatsuba-mult.k"), `Expected loaded karatsuba-mult.k, got: ${loadKaratsubaOut}`);
189
+ assert(loadExamplesArithOut.includes("loaded Examples/arithmetics.k"), `Expected loaded Examples/arithmetics.k, got: ${loadExamplesArithOut}`);
184
190
 
185
191
  // Test evaluating {1 int x, 1 int y} karatsuba
186
192
  const karatsubaVal = await evaluateAsync(`(async () => {
@@ -328,7 +334,9 @@ if (chromiumBin) {
328
334
 
329
335
  // Test JSON codec with float64 in browser: :input json.mjs then {"a":"Woj","n":123}
330
336
  await evaluateAsync(`(async () => {
331
- await window.kRepl.executeCommand(":load core.k");
337
+ await window.kRepl.executeCommand(":load list.k");
338
+ await window.kRepl.executeCommand(":load btrie.k");
339
+ await window.kRepl.executeCommand(":load string.k");
332
340
  await window.kRepl.executeCommand(":load ieee.k");
333
341
  await window.kRepl.executeCommand(":codec load json.mjs");
334
342
  await window.kRepl.executeCommand(":input json.mjs");
@@ -393,7 +401,10 @@ if (chromiumBin) {
393
401
  // Test :load poly.k and evaluate '10 int (gen_list sort)' in WASM mode
394
402
  const sortListResult = await evaluateAsync(`(async () => {
395
403
  await window.kRepl.executeCommand(":reset");
404
+ await window.kRepl.executeCommand(":load bit.k");
405
+ await window.kRepl.executeCommand(":load bits.k");
396
406
  await window.kRepl.executeCommand(":load arithmetics.k");
407
+ await window.kRepl.executeCommand(":load list.k");
397
408
  await window.kRepl.executeCommand(":load poly.k");
398
409
  await window.kRepl.executeCommand(":codec load int.mjs");
399
410
  await window.kRepl.executeCommand("10 int (gen_list sort)");
@@ -309,8 +309,10 @@ 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
- output = await evaluateInput(":load Examples/arithmetics.k", arithState);
313
- assert.equal(output[0], "loaded Examples/arithmetics.k");
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");
314
316
  output = await evaluateInput(":pat bits", arithState);
315
317
  assert.equal(
316
318
  output[0],
@@ -383,7 +385,9 @@ assert.match(output[0], /^\{\}\|a/);
383
385
 
384
386
  // Explicit loading of dependencies and codec testing (no auto-loading)
385
387
  const intState = createState();
386
- await evaluateInput(":load Examples/arithmetics.k", intState);
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
391
  output = await evaluateInput(":codec load ./codecs/int.mjs", intState);
388
392
  assert.match(output[0], /^loaded codec int\.mjs/);
389
393
  output = await evaluateInput(":input int.mjs", intState);
@@ -402,8 +406,9 @@ output = await evaluateInput("test utf8", utf8AutoState);
402
406
  assert.match(output[0], /utf8\.mjs: test utf8/);
403
407
 
404
408
  const ieeeAutoState = createState();
405
- await evaluateInput(":load core.k", ieeeAutoState);
406
- await evaluateInput(":load Examples/ieee.k", ieeeAutoState);
409
+ for (const file of ["lib/bit.k", "lib/bits.k", "lib/list.k", "lib/btrie.k", "lib/string.k", "lib/ieee.k"]) {
410
+ await evaluateInput(`:load ${file}`, ieeeAutoState);
411
+ }
407
412
  output = await evaluateInput(":codec load ./codecs/ieee.mjs", ieeeAutoState);
408
413
  assert.match(output[0], /^loaded codec ieee\.mjs/);
409
414
  assert.ok(ieeeAutoState.codecs["ieee.mjs"], "ieee codec should be registered");
@@ -411,8 +416,9 @@ output = await evaluateInput(":input ieee.mjs 1.5", ieeeAutoState);
411
416
  assert.match(output[0], /ieee\.mjs: 1\.5/);
412
417
 
413
418
  const jsonInputState = createState();
414
- await evaluateInput(":load core.k", jsonInputState);
415
- await evaluateInput(":load Examples/ieee.k", jsonInputState);
419
+ for (const file of ["lib/bit.k", "lib/bits.k", "lib/list.k", "lib/btrie.k", "lib/string.k", "lib/ieee.k"]) {
420
+ await evaluateInput(`:load ${file}`, jsonInputState);
421
+ }
416
422
  await evaluateInput(":codec load ./codecs/json.mjs", jsonInputState);
417
423
  output = await evaluateInput(':input json.mjs {"a":"Woj","n":123}', jsonInputState);
418
424
  assert.match(output[0], /json\.mjs: \{"a":"Woj","n":123\}/);
@@ -473,16 +479,14 @@ assert.match(output[0], /unit\.mjs: \{\}/);
473
479
  const polyFreshState = createState();
474
480
  await assert.rejects(
475
481
  () => evaluateInput(":load Examples/poly.k", polyFreshState),
476
- /Unknown pattern template/
482
+ /Unknown pattern template|No definition found for/
477
483
  );
478
484
 
479
- // Explicitly loading arithmetics.k then poly.k succeeds
485
+ // Explicitly loading dependencies then poly.k succeeds
480
486
  const polyExplicitState = createState();
481
- output = await evaluateInput(
482
- ":load Examples/arithmetics.k",
483
- polyExplicitState
484
- );
485
- assert.equal(output[0], "loaded Examples/arithmetics.k");
487
+ for (const file of ["lib/bit.k", "lib/bits.k", "lib/arithmetics.k", "lib/list.k"]) {
488
+ await evaluateInput(`:load ${file}`, polyExplicitState);
489
+ }
486
490
  output = await evaluateInput(
487
491
  ":load Examples/poly.k",
488
492
  polyExplicitState
@@ -565,8 +569,10 @@ output = await evaluateInput("not = </true | false, /false | true >;\n{} | true
565
569
  assert.match(output[0], /^\{\}\|false/);
566
570
  assert.match(output[1], /\(js\)/);
567
571
 
568
- output = await evaluateInput(":load Examples/arithmetics.k", jsState);
569
- assert.equal(output[0], "loaded Examples/arithmetics.k");
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");
570
576
  output = await evaluateInput("{ 10 int x, 5 int y } plus", jsState);
571
577
  assert.match(output[0], /^\{\}\|_\|1\|1\|1\|1\|\+/);
572
578
  assert.match(output[1], /\(js\)/);
@@ -589,15 +595,19 @@ output = await evaluateInput(
589
595
  assert.match(output[0], /^\{\}\|false/);
590
596
  assert.match(output[1], /\(pkir\)/);
591
597
 
592
- output = await evaluateInput(":load Examples/arithmetics.k", pkirState);
593
- assert.equal(output[0], "loaded Examples/arithmetics.k");
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");
594
602
  output = await evaluateInput("{ 10 int x, 5 int y } plus", pkirState);
595
603
  assert.match(output[0], /^\{\}\|_\|1\|1\|1\|1\|\+/);
596
604
  assert.match(output[1], /\(pkir\)/);
597
605
 
598
606
  // Verify open product projection in wasm engine
599
607
  const wasmListState = createState({ engine: "wasm" });
600
- await evaluateInput(":load Examples/arithmetics.k", wasmListState);
608
+ await evaluateInput(":load lib/bit.k", wasmListState);
609
+ await evaluateInput(":load lib/bits.k", wasmListState);
610
+ await evaluateInput(":load lib/arithmetics.k", wasmListState);
601
611
  await evaluateInput(":codec load codecs/int.mjs", wasmListState);
602
612
  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);
603
613
  await evaluateInput(":input int.mjs [3,3,3]", wasmListState);
@@ -616,7 +626,9 @@ assert.equal(output[0], "output limit set to 40");
616
626
  output = await evaluateInput(":limit", limitState);
617
627
  assert.equal(output[0], "Current output limit: 40");
618
628
 
619
- output = await evaluateInput(":load Examples/arithmetics.k", limitState);
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);
620
632
  output = await evaluateInput("{ 10 int x, 5 int y } plus", limitState);
621
633
  assert(output[0].includes("..."));
622
634
  assert(output[0].length <= 80);
@@ -626,12 +638,13 @@ assert.equal(output[0], "output limit disabled");
626
638
  output = await evaluateInput(":limit", limitState);
627
639
  assert.equal(output[0], "Current output limit: none");
628
640
 
629
- // Verify Examples/djson.k (json?, get, nth, set, del)
641
+ // Verify lib/djson.k (json?, get, nth, set, del)
630
642
  const djsonKState = createState({ engine: "wasm" });
631
- await evaluateInput(":load core.k", djsonKState);
632
- await evaluateInput(":load Examples/ieee.k", djsonKState);
633
- output = await evaluateInput(":load Examples/djson.k", djsonKState);
634
- assert.equal(output[0], "loaded Examples/djson.k");
643
+ for (const file of ["lib/bit.k", "lib/bits.k", "lib/list.k", "lib/btrie.k", "lib/string.k", "lib/ieee.k"]) {
644
+ await evaluateInput(`:load ${file}`, djsonKState);
645
+ }
646
+ output = await evaluateInput(":load lib/djson.k", djsonKState);
647
+ assert.equal(output[0], "loaded lib/djson.k");
635
648
  await evaluateInput(":codec load codecs/djson.mjs", djsonKState);
636
649
  await evaluateInput(
637
650
  ':input djson.mjs ["age", {"name":"Alice","age":30}]',
@@ -768,8 +781,10 @@ assert.equal(
768
781
  "@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm[A, B]"
769
782
  );
770
783
 
771
- output = await evaluateInput(":load Examples/list.k", patState);
772
- assert.equal(output[0], "loaded Examples/list.k");
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");
773
788
  output = await evaluateInput(":patterns", patState);
774
789
  assert(output[0].includes("bit[] = @Mjusy"));
775
790
  assert(output[0].includes("list[X] = @JagVex"));
@@ -979,8 +994,8 @@ output = await evaluateInput(":rel reverse", polyExplicitState);
979
994
  assert.equal(
980
995
  output[0],
981
996
  "reverse = ?<{(...) car, X0 cdr} cons, {} nil>=X0 " +
982
- "{() xs, nil acc} _reverse ?X0; " +
983
- "-- @JbJ2aQugNct2GGeCJ65zKjN55Bf5YPyGHzsikTCjx82w"
997
+ "{() x, nil y} rev_append ?X0; " +
998
+ "-- @peiZq2evd5u4Qk42AT3W4YmWZZ3JAeXNKTnmDtpb9EZh"
984
999
  );
985
1000
  await evaluateInput(":max-pat-length 30", polyExplicitState);
986
1001
  output = await evaluateInput(":rel car", polyExplicitState);
@@ -994,26 +1009,26 @@ output = await evaluateInput(":rel reverse", polyExplicitState);
994
1009
  assert.equal(
995
1010
  output[0],
996
1011
  "reverse = ?@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe[X0] " +
997
- "{() xs, nil acc} _reverse " +
1012
+ "{() x, nil y} rev_append " +
998
1013
  "?@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe[X0]; " +
999
- "-- @JbJ2aQugNct2GGeCJ65zKjN55Bf5YPyGHzsikTCjx82w"
1014
+ "-- @peiZq2evd5u4Qk42AT3W4YmWZZ3JAeXNKTnmDtpb9EZh"
1000
1015
  );
1001
1016
  output = await evaluateInput(":rel nil?", polyExplicitState);
1002
1017
  assert.equal(
1003
1018
  output[0],
1004
1019
  "nil? = ?@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe[(...)] " +
1005
- "/nil nil " +
1020
+ "un_nil nil " +
1006
1021
  "?@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe[(...)]; " +
1007
- "-- @YHZEettEijtfW68ekdSKKiZRfiiEkqJjLDb9NNcJnwcg"
1022
+ "-- @BqMbBgmNtqci4fKjrUh5dwMrAehZcGSeEpEdLaMeCXSA"
1008
1023
  );
1009
1024
  output = await evaluateInput(":rel parseJson", djsonKState);
1010
1025
  assert.equal(
1011
1026
  output[0],
1012
1027
  "parseJson = " +
1013
1028
  "?@Pt3MwQwzUPxQKu58zXR3qUBHWTzfycx5bSZGzcK6EZQK[] " +
1014
- "json_value {.rest ws /nil end, .value value} .value " +
1029
+ "json_value {.rest ws un_nil end, .value value} .value " +
1015
1030
  "?@P5C7TQKTSPnFUkYkYzTudJamxAQ3T3fHbvZNqwWMfAA2[]; " +
1016
- "-- @tWaZchv9AbbWxSHVk8nBpz8jVnjswiwDNhpFZeFwg6Pt"
1031
+ "-- @BNjpkLgR3SxNvVMuAE9xkBxvJFhaDUhaHHsBBjmx4RRK"
1017
1032
  );
1018
1033
  const shortThresholdState = createState();
1019
1034
  output = await evaluateInput(":max-pat-length", shortThresholdState);