@fraczak/k 8.6.1 → 8.7.1

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 (75) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/DOCS/CODECS.md +20 -14
  3. package/DOCS/FILE_FORMATS.md +53 -12
  4. package/DOCS/GETTING_STARTED.md +15 -8
  5. package/DOCS/PATTERNS.md +10 -0
  6. package/DOCS/REPL.md +12 -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 -15
  17. package/backends/llvm/tests/integration.sh +6 -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 +4 -4
  23. package/backends/wasm/tests/integration.sh +24 -4
  24. package/browser/shims/fs.mjs +19 -3
  25. package/canonical-pattern.mjs +214 -57
  26. package/compiler.mjs +26 -0
  27. package/core.k +2 -1
  28. package/harness/suites/poly.mjs +14 -30
  29. package/index.mjs +67 -2
  30. package/k.mjs +14 -18
  31. package/kir.mjs +3 -1
  32. package/kvm.mjs +11 -11
  33. package/lib/arithmetics.k +286 -0
  34. package/lib/bit.k +68 -0
  35. package/lib/bits.k +227 -0
  36. package/lib/btrie.k +187 -0
  37. package/lib/djson.k +327 -0
  38. package/lib/ieee.k +709 -0
  39. package/lib/list.k +49 -0
  40. package/lib/string.k +588 -0
  41. package/named-patterns.mjs +2 -14
  42. package/object.mjs +32 -7
  43. package/objects/README.md +9 -3
  44. package/objects/compile.mjs +11 -14
  45. package/package.json +1 -1
  46. package/repl.mjs +24 -5
  47. package/scripts/benchmark-idempotency.mjs +22 -15
  48. package/scripts/benchmark.mjs +29 -10
  49. package/scripts/build-repl-html.mjs +41 -13
  50. package/scripts/perf-ieee.mjs +9 -1
  51. package/scripts/perf-int.mjs +10 -17
  52. package/scripts/perf-multiplication.mjs +9 -15
  53. package/scripts/perf-poly.mjs +16 -32
  54. package/scripts/run-tests.mjs +3 -2
  55. package/scripts/test-package.mjs +13 -2
  56. package/tests/helpers/cli.mjs +19 -3
  57. package/tests/integration.sh +25 -5
  58. package/tests/test-arithmetics.mjs +4 -2
  59. package/tests/test-djson-methods.mjs +21 -12
  60. package/tests/test-ieee-arithmetic.mjs +11 -3
  61. package/tests/test-ieee-text.mjs +19 -4
  62. package/tests/test-k-object.mjs +15 -4
  63. package/tests/test-karatsuba.mjs +1 -1
  64. package/tests/test-kvm-polymorphic.mjs +58 -34
  65. package/tests/test-lib.mjs +117 -0
  66. package/tests/test-library-isolation.mjs +381 -0
  67. package/tests/test-named-patterns.mjs +21 -3
  68. package/tests/test-parse-json.mjs +44 -14
  69. package/tests/test-poly.mjs +10 -4
  70. package/tests/test-repl-html.mjs +18 -7
  71. package/tests/test-repl.mjs +50 -35
  72. package/Examples/djson.k +0 -894
  73. package/Examples/ieee.k +0 -969
  74. package/Examples/karatsuba-mult.k +0 -227
  75. package/Examples/list.k +0 -10
@@ -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);