@fraczak/k 8.4.0 → 8.5.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 +59 -0
- package/DOCS/ARCHITECTURE.md +83 -0
- package/DOCS/CODECS.md +93 -204
- package/DOCS/DICTIONARY.md +42 -1241
- package/DOCS/FILE_FORMATS.md +170 -625
- package/DOCS/GETTING_STARTED.md +164 -0
- package/DOCS/KIR_V1.md +22 -33
- package/DOCS/KVM_EXECUTION_MODEL.md +88 -481
- package/DOCS/PATTERNS.md +156 -123
- package/DOCS/POLYMORPHIC_IR.md +83 -101
- package/DOCS/PROFILING_AND_TRACING.md +52 -166
- package/DOCS/README.md +35 -0
- package/DOCS/REPL.md +100 -371
- package/DOCS/TEXTUAL_VALUES.md +53 -81
- package/DOCS/TYPE_DERIVATION.md +95 -234
- package/DOCS/book-combined.md +3786 -0
- package/Examples/arithmetics.k +45 -59
- package/Examples/djson.k +102 -112
- package/Examples/karatsuba-mult.k +2 -3
- package/Examples/list.k +6 -7
- package/Examples/nat.k +6 -6
- package/Examples/poly.k +45 -45
- package/LICENSE +15 -0
- package/README.md +72 -410
- package/backends/llvm/README.md +75 -97
- package/backends/llvm/src/cli.mjs +0 -31
- package/backends/wasm/README.md +88 -66
- package/backends/wasm/src/cli.mjs +0 -31
- package/canonical-pattern.mjs +69 -35
- package/codecs/README.md +23 -261
- package/codecs/WIRE_FORMAT.md +132 -0
- package/core.k +5 -15
- package/index.mjs +5 -1
- package/named-patterns.mjs +55 -7
- package/object.mjs +28 -6
- package/objects/README.md +24 -109
- package/package.json +8 -3
- package/paper/Makefile +17 -0
- package/paper/k-paper.pdf +0 -0
- package/paper/k-paper.tex +435 -0
- package/parser.jison +0 -10
- package/parser.mjs +32 -43
- package/repl.mjs +79 -101
- package/scripts/benchmark-idempotency.mjs +3 -3
- package/scripts/run-tests.mjs +1 -1
- package/scripts/test-package.mjs +64 -0
- package/tests/README.md +66 -46
- package/tests/code-derivation/core.mjs +61 -0
- package/tests/helpers/cli.mjs +45 -0
- package/tests/integration.sh +2 -3
- package/tests/test-arithmetics.mjs +47 -237
- package/tests/test-djson-methods.mjs +71 -275
- package/tests/test-hash-normalization.mjs +22 -1
- package/tests/test-karatsuba.mjs +18 -126
- package/tests/test-named-patterns.mjs +174 -606
- package/tests/test-paper-examples.mjs +90 -0
- package/tests/test-poly.mjs +2 -2
- package/tests/test-repl-html.mjs +2 -2
- package/tests/test-repl.mjs +89 -38
- package/tests/test.mjs +27 -68
- package/AGENTS.md +0 -35
- package/CLAUDE.md +0 -64
- package/DOCS/BIG_PICTURE.md +0 -439
- package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +0 -74
- package/DOCS/CANONICAL_PATTERNS.md +0 -349
- package/DOCS/CODEC_ARCHITECTURE.md +0 -257
- package/DOCS/CONVERGENCE.md +0 -123
- package/DOCS/DIVERGENCE_DETECTION.md +0 -23
- package/DOCS/JSON_APPROACHES_COMPARISON.md +0 -302
- package/DOCS/KIR_SPECIALIZATION_DESIGN.md +0 -472
- package/DOCS/Makefile +0 -99
- package/DOCS/NAMED_PATTERNS.md +0 -340
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +0 -343
- package/DOCS/PATTERNS_AND_TYPED_VALUES.md +0 -665
- package/DOCS/WEB_REPL_REQUIREMENTS.md +0 -136
- package/DOCS/book/01-introduction.md +0 -48
- package/DOCS/book/02-syntax-and-values.md +0 -110
- package/DOCS/book/03-types-as-finite-automata.md +0 -155
- package/DOCS/book/04-partial-functions-and-composition.md +0 -145
- package/DOCS/book/05-typing-filters-and-normalization.md +0 -109
- package/DOCS/book/06-values-in-memory.md +0 -1085
- package/DOCS/book/07-the-partial-function-abi.md +0 -143
- package/DOCS/book/08-operational-semantics-and-execution.md +0 -196
- package/DOCS/book/09-compiler-architecture.md +0 -160
- package/DOCS/book/10-from-ast-to-intermediate-representation.md +0 -166
- package/DOCS/book/14-canonical-serialization.md +0 -176
- package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +0 -156
- package/DOCS/book/overview.txt +0 -68
- package/DOCS/book.md +0 -50
- package/Examples/bnat.k +0 -308
- package/Examples/buda.k +0 -66
- package/Examples/byte.k +0 -35
- package/Examples/code.k +0 -43
- package/Examples/vhdl_to_k.k +0 -67
- package/JOURNAL.md +0 -288
- package/TODO.md +0 -107
- package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +0 -233
- package/backends/wasm/TECHNICAL-DEBT.md +0 -58
- package/backends/wasm/demo_ieee.mjs +0 -318
- package/browser/vfs-data.mjs +0 -21
- package/codecs/BINARY_FORMAT.md +0 -53
- package/codecs/POLYMORPHIC_BINARY_FORMAT.md +0 -303
- package/codecs/pandoc.css +0 -71
- package/tests/code-derivation/blowup.mjs +0 -25
- package/tests/code-derivation/bnat.mjs +0 -11
- package/tests/code-derivation/dot.mjs +0 -37
- package/tests/code-derivation/index.mjs +0 -20
- package/tests/code-derivation/loop.mjs +0 -17
- package/tests/code-derivation/nat.mjs +0 -10
- package/tests/code-derivation/not-not.mjs +0 -14
- package/tests/code-derivation/product.mjs +0 -22
- package/tests/code-derivation/rlz.mjs +0 -13
- package/tests/test-fingerprint.mjs +0 -38
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { createState, evaluateInput } from "../repl.mjs";
|
|
4
|
+
import k from "../index.mjs";
|
|
5
|
+
import { resolvePatternCanonicalInfo } from "../canonical-pattern.mjs";
|
|
6
|
+
import { valueToK } from "../codecs/runtime/show-value.mjs";
|
|
7
|
+
|
|
8
|
+
const source = readFileSync(new URL("../paper/k-paper.tex", import.meta.url), "utf8");
|
|
9
|
+
const listings = [...source.matchAll(/\\begin\{lstlisting\}(?:\[[^\]]*\])?\s*([\s\S]*?)\\end\{lstlisting\}/g)]
|
|
10
|
+
.map((match) => match[1]);
|
|
11
|
+
function listing(needle) {
|
|
12
|
+
const source = listings.find((source) => source.includes(needle));
|
|
13
|
+
assert.ok(source, `paper listing containing ${needle}`);
|
|
14
|
+
return source.split("\n").filter((line) => !line.trimStart().startsWith("--"))
|
|
15
|
+
.join("\n").trim();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Section 2: combinators and constant relations.
|
|
19
|
+
{
|
|
20
|
+
const state = createState();
|
|
21
|
+
for (const [source, expected] of [
|
|
22
|
+
["{}", "{}"], ["()", "{}"], ["<>", undefined],
|
|
23
|
+
["{} |true", "{}|true"], ["{} |zero", "{}|zero"]
|
|
24
|
+
]) {
|
|
25
|
+
const output = await evaluateInput(source, state);
|
|
26
|
+
if (expected === undefined) assert.ok(output.some((line) => line.includes("undefined")));
|
|
27
|
+
else assert.equal(valueToK(state.value), expected, source);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Sections 4–6: published declarations, exact hashes, and routing.
|
|
32
|
+
const declarations = listing("-- Arity-0 leaf patterns:");
|
|
33
|
+
const pairs = listing("closed_same_pair[]");
|
|
34
|
+
{
|
|
35
|
+
const state = createState();
|
|
36
|
+
await evaluateInput(declarations, state);
|
|
37
|
+
const output = (await evaluateInput(":patterns", state)).join("\n");
|
|
38
|
+
for (const name of ["bit[]", "bits[]", "pair[A,B]", "list[X]"]) {
|
|
39
|
+
assert.ok(output.includes(name), name);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const { defs: { patterns } } = k.parse(pairs + "\n()");
|
|
43
|
+
for (const [name, args] of [
|
|
44
|
+
["pair", ["A", "B"]], ["same_pair", ["T", "T"]],
|
|
45
|
+
["closed_same_pair", ["T", "T"]], ["any_pair", ["(...)", "(...)"]]
|
|
46
|
+
]) {
|
|
47
|
+
const info = resolvePatternCanonicalInfo(name, patterns[name], { patterns });
|
|
48
|
+
assert.deepEqual([info.hash, info.def, info.aliasArgs],
|
|
49
|
+
["@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm",
|
|
50
|
+
"{X0 first, X1 second}", args], name);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
{
|
|
54
|
+
const state = createState();
|
|
55
|
+
await evaluateInput(declarations, state);
|
|
56
|
+
await evaluateInput(pairs, state);
|
|
57
|
+
await evaluateInput("{({} |0) first, ({} |1) second} ?same_pair[bit[]]", state);
|
|
58
|
+
assert.equal(valueToK(state.value), "{{}|0 first, {}|1 second}");
|
|
59
|
+
await assert.rejects(() =>
|
|
60
|
+
evaluateInput("{({} |0) first, {} second} ?same_pair[bit[]]", state), /Type Error/);
|
|
61
|
+
|
|
62
|
+
await evaluateInput("nil = {} |nil ?list[bit[]];", state);
|
|
63
|
+
await evaluateInput("cons = {({} |0) car, nil cdr} |cons ?list[bit[]];", state);
|
|
64
|
+
await evaluateInput("cons", state);
|
|
65
|
+
assert.equal(valueToK(state.value), "{{}|0 car, {}|nil cdr}|cons");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Section 7: execute the actual half-adder listing through Wasm.
|
|
69
|
+
{
|
|
70
|
+
const state = createState();
|
|
71
|
+
await evaluateInput(listing("half_add ="), state);
|
|
72
|
+
for (const [a, b, sum, carry] of [
|
|
73
|
+
[0, 0, 0, 0], [0, 1, 1, 0], [1, 0, 1, 0], [1, 1, 0, 1]
|
|
74
|
+
]) {
|
|
75
|
+
await evaluateInput(`{${a} a, ${b} b} half_add`, state);
|
|
76
|
+
assert.deepEqual([state.value.product.sum.tag, state.value.product.carry.tag],
|
|
77
|
+
[String(sum), String(carry)], `${a} + ${b}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Appendix: verify the publication's pattern schema against the real Unicode core.
|
|
82
|
+
{
|
|
83
|
+
const state = createState();
|
|
84
|
+
await evaluateInput(":load core.k", state);
|
|
85
|
+
const expected = await evaluateInput(":pattern pattern", state);
|
|
86
|
+
await evaluateInput(listing("edge[]"), state);
|
|
87
|
+
assert.deepEqual(await evaluateInput(":pattern pattern", state), expected);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
console.log("paper examples: ok");
|
package/tests/test-poly.mjs
CHANGED
|
@@ -154,13 +154,13 @@ async function evalK(snippet) {
|
|
|
154
154
|
} .then,
|
|
155
155
|
{.xs car car, {.xs cdr xs, .ys ys} _merge cdr} cons
|
|
156
156
|
>;
|
|
157
|
-
_sort =
|
|
157
|
+
_sort = ? list[X] <
|
|
158
158
|
/nil nil,
|
|
159
159
|
/cons <
|
|
160
160
|
{.cdr /nil if, .car singleton then} .then,
|
|
161
161
|
|cons split {.first _sort xs, .second _sort ys} _merge
|
|
162
162
|
>
|
|
163
|
-
> list
|
|
163
|
+
> ? list[X];
|
|
164
164
|
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;
|
|
165
165
|
l _sort
|
|
166
166
|
`);
|
package/tests/test-repl-html.mjs
CHANGED
|
@@ -43,8 +43,8 @@ assert(!html.includes("input-popup-modal"), "repl.html must NOT contain input po
|
|
|
43
43
|
console.log(" repl.html is valid, self-contained, and has size:", (html.length / 1024).toFixed(1), "KB");
|
|
44
44
|
|
|
45
45
|
// 3. If Chromium is available, run end-to-end browser test
|
|
46
|
-
const chromiumBin = ["
|
|
47
|
-
const res = spawnSync(
|
|
46
|
+
const chromiumBin = ["google-chrome", "chromium-browser", "chromium"].find(bin => {
|
|
47
|
+
const res = spawnSync(bin, ["--version"], { stdio: "ignore" });
|
|
48
48
|
return res.status === 0;
|
|
49
49
|
});
|
|
50
50
|
|
package/tests/test-repl.mjs
CHANGED
|
@@ -303,6 +303,24 @@ assert.equal(tcoState.value.toJSON(), "zero");
|
|
|
303
303
|
const arithState = createState();
|
|
304
304
|
output = await evaluateInput(":load Examples/arithmetics.k", arithState);
|
|
305
305
|
assert.equal(output[0], "loaded Examples/arithmetics.k");
|
|
306
|
+
output = await evaluateInput(":pat bits", arithState);
|
|
307
|
+
assert.equal(
|
|
308
|
+
output[0],
|
|
309
|
+
"bits[] = <B 0, B 1, {} _>=B; -- " +
|
|
310
|
+
"@VtPHxGf5GNMzzyVFxtv7gegFfJRYapBGtCeyV56bs5Zb[]"
|
|
311
|
+
);
|
|
312
|
+
// output = await evaluateInput(":pat bit", arithState);
|
|
313
|
+
// assert.equal(
|
|
314
|
+
// output[0],
|
|
315
|
+
// "bit[] = <{} 0, {} 1>; -- " +
|
|
316
|
+
// "@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg[]"
|
|
317
|
+
// );
|
|
318
|
+
output = await evaluateInput(":pat int[]", arithState);
|
|
319
|
+
assert.equal(
|
|
320
|
+
output[0],
|
|
321
|
+
"int[] = <bits[] +, bits[] ->; -- " +
|
|
322
|
+
"@Nws3vysWqC9PznraTP4kWPp44Rd77NAqrx9dgbtHAjGA[]"
|
|
323
|
+
);
|
|
306
324
|
output = await evaluateInput("10", arithState);
|
|
307
325
|
assert.match(output[0], /^\{\}\|_\|0\|1\|0\|1/);
|
|
308
326
|
output = await evaluateInput("{10 int x, 5 int y} plus", arithState);
|
|
@@ -396,12 +414,14 @@ await evaluateInput(":codec load ./codecs/json.mjs", jsonInputState);
|
|
|
396
414
|
output = await evaluateInput(':input json.mjs {"a":"Woj","n":123}', jsonInputState);
|
|
397
415
|
assert.match(output[0], /json\.mjs: \{"a":"Woj","n":123\}/);
|
|
398
416
|
|
|
417
|
+
console.log("==> All REPL tests passed successfully");
|
|
399
418
|
// Verify standalone codec input, one-line and interactive modes, and multi-codec printing
|
|
400
419
|
const multiCodecState = createState();
|
|
401
420
|
await evaluateInput(":codec load ./codecs/json.mjs", multiCodecState);
|
|
402
421
|
await evaluateInput(":codec load ./codecs/utf8.mjs", multiCodecState);
|
|
403
422
|
await evaluateInput(":codec load ./codecs/int.mjs", multiCodecState);
|
|
404
423
|
|
|
424
|
+
console.log("==> Running REPL :input command test suite");
|
|
405
425
|
// 1. One-line JSON input: :input json.mjs {"hello":"world"}
|
|
406
426
|
output = await evaluateInput(':input json.mjs {"hello":"world"}', multiCodecState);
|
|
407
427
|
assert.match(output[0], /json\.mjs: \{"hello":"world"\}/);
|
|
@@ -446,11 +466,33 @@ assert.equal(promptForState(unitState), "unit.mjs> ");
|
|
|
446
466
|
output = await evaluateInput("{}", unitState);
|
|
447
467
|
assert.match(output[0], /unit\.mjs: \{\}/);
|
|
448
468
|
|
|
449
|
-
|
|
450
|
-
|
|
469
|
+
// Verify that poly.k does NOT auto-load arithmetics.k (no autohooks)
|
|
470
|
+
const polyFreshState = createState();
|
|
471
|
+
await assert.rejects(
|
|
472
|
+
() => evaluateInput(":load Examples/poly.k", polyFreshState),
|
|
473
|
+
/Unknown pattern template/
|
|
474
|
+
);
|
|
475
|
+
|
|
476
|
+
// Explicitly loading arithmetics.k then poly.k succeeds
|
|
477
|
+
const polyExplicitState = createState();
|
|
478
|
+
output = await evaluateInput(
|
|
479
|
+
":load Examples/arithmetics.k",
|
|
480
|
+
polyExplicitState
|
|
481
|
+
);
|
|
482
|
+
assert.equal(output[0], "loaded Examples/arithmetics.k");
|
|
483
|
+
output = await evaluateInput(
|
|
484
|
+
":load Examples/poly.k",
|
|
485
|
+
polyExplicitState
|
|
486
|
+
);
|
|
451
487
|
assert.equal(output[0], "loaded Examples/poly.k");
|
|
452
|
-
assert.ok(
|
|
453
|
-
|
|
488
|
+
assert.ok(
|
|
489
|
+
polyExplicitState.relAliases.reverse,
|
|
490
|
+
"poly relations loaded"
|
|
491
|
+
);
|
|
492
|
+
assert.ok(
|
|
493
|
+
polyExplicitState.relAliases.int,
|
|
494
|
+
"arithmetics int loaded"
|
|
495
|
+
);
|
|
454
496
|
|
|
455
497
|
// Verify timing reporting is always on and reflects active engine
|
|
456
498
|
const timingState = createState();
|
|
@@ -633,20 +675,20 @@ assert.equal(
|
|
|
633
675
|
output = await evaluateInput(":pattern bit", patState);
|
|
634
676
|
assert.equal(
|
|
635
677
|
output[0],
|
|
636
|
-
"bit[] = <{}
|
|
637
|
-
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg"
|
|
678
|
+
"bit[] = <{} 0, {} 1>; -- " +
|
|
679
|
+
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg[]"
|
|
638
680
|
);
|
|
639
681
|
output = await evaluateInput(":pattern bit[]", patState);
|
|
640
682
|
assert.equal(
|
|
641
683
|
output[0],
|
|
642
|
-
"bit[] = <{}
|
|
643
|
-
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg"
|
|
684
|
+
"bit[] = <{} 0, {} 1>; -- " +
|
|
685
|
+
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg[]"
|
|
644
686
|
);
|
|
645
687
|
output = await evaluateInput(":pat bit", patState);
|
|
646
688
|
assert.equal(
|
|
647
689
|
output[0],
|
|
648
|
-
"bit[] = <{}
|
|
649
|
-
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg"
|
|
690
|
+
"bit[] = <{} 0, {} 1>; -- " +
|
|
691
|
+
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg[]"
|
|
650
692
|
);
|
|
651
693
|
output = await evaluateInput(
|
|
652
694
|
":pattern @MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg",
|
|
@@ -667,9 +709,18 @@ output = await evaluateInput(
|
|
|
667
709
|
output = await evaluateInput(":pattern byte", patState);
|
|
668
710
|
assert.equal(
|
|
669
711
|
output[0],
|
|
670
|
-
"byte[] = {
|
|
671
|
-
"
|
|
672
|
-
"@4ukq2gpiEBFa7WXxcfRm8pfdr2kgYLTTnwsmLHeMd9i5"
|
|
712
|
+
"byte[] = {bit[] b0, bit[] b1, bit[] b2, bit[] b3, " +
|
|
713
|
+
"bit[] b4, bit[] b5, bit[] b6, bit[] b7}; -- " +
|
|
714
|
+
"@4ukq2gpiEBFa7WXxcfRm8pfdr2kgYLTTnwsmLHeMd9i5[]"
|
|
715
|
+
);
|
|
716
|
+
|
|
717
|
+
await assert.rejects(
|
|
718
|
+
() => evaluateInput("bad[] = { missing[] x };", patState),
|
|
719
|
+
/Unknown pattern template 'missing\[\]'/
|
|
720
|
+
);
|
|
721
|
+
await assert.rejects(
|
|
722
|
+
() => evaluateInput("bad[] = missing[];", patState),
|
|
723
|
+
/Unknown pattern template 'missing\[\]'/
|
|
673
724
|
);
|
|
674
725
|
|
|
675
726
|
await assert.rejects(
|
|
@@ -708,8 +759,8 @@ output = await evaluateInput(
|
|
|
708
759
|
output = await evaluateInput(":pattern pair", patState);
|
|
709
760
|
assert.equal(
|
|
710
761
|
output[0],
|
|
711
|
-
"pair[
|
|
712
|
-
"@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm"
|
|
762
|
+
"pair[A, B] = {A first, B second}; -- " +
|
|
763
|
+
"@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm[A, B]"
|
|
713
764
|
);
|
|
714
765
|
|
|
715
766
|
output = await evaluateInput(":load Examples/list.k", patState);
|
|
@@ -722,8 +773,8 @@ assert(output[0].includes("pair[A,B] = @KqTB"));
|
|
|
722
773
|
output = await evaluateInput(":pattern list", patState);
|
|
723
774
|
assert.equal(
|
|
724
775
|
output[0],
|
|
725
|
-
"list[
|
|
726
|
-
"@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe"
|
|
776
|
+
"list[X] = <{} nil, {X car, Y cdr} cons>=Y; -- " +
|
|
777
|
+
"@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe[X]"
|
|
727
778
|
);
|
|
728
779
|
|
|
729
780
|
output = await evaluateInput("{} | nil ? list[bit[]]", patState);
|
|
@@ -745,19 +796,19 @@ output = await evaluateInput(":pattern bit", freshState);
|
|
|
745
796
|
assert.equal(
|
|
746
797
|
output[0],
|
|
747
798
|
"bit[] = <{}=X0 0, X0 1>; -- " +
|
|
748
|
-
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg"
|
|
799
|
+
"@MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg[]"
|
|
749
800
|
);
|
|
750
801
|
output = await evaluateInput(":pattern pair", freshState);
|
|
751
802
|
assert.equal(
|
|
752
803
|
output[0],
|
|
753
|
-
"pair[
|
|
754
|
-
"@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm"
|
|
804
|
+
"pair[A, B] = {X0 first, X1 second}; -- " +
|
|
805
|
+
"@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm[A, B]"
|
|
755
806
|
);
|
|
756
807
|
output = await evaluateInput(":pattern list", freshState);
|
|
757
808
|
assert.equal(
|
|
758
809
|
output[0],
|
|
759
|
-
"list[
|
|
760
|
-
"@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe"
|
|
810
|
+
"list[X] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- " +
|
|
811
|
+
"@JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe[X]"
|
|
761
812
|
);
|
|
762
813
|
output = await evaluateInput(
|
|
763
814
|
"same_pair[T] = { T first, T second };",
|
|
@@ -766,8 +817,8 @@ output = await evaluateInput(
|
|
|
766
817
|
output = await evaluateInput(":pattern same_pair", freshState);
|
|
767
818
|
assert.equal(
|
|
768
819
|
output[0],
|
|
769
|
-
"same_pair[
|
|
770
|
-
"@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm"
|
|
820
|
+
"same_pair[T] = {T first, T second}; -- " +
|
|
821
|
+
"@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm[T, T]"
|
|
771
822
|
);
|
|
772
823
|
output = await evaluateInput("{} | nil ? list[bit[]]", freshState);
|
|
773
824
|
assert.match(output[0], /\{\}\|nil/);
|
|
@@ -776,8 +827,8 @@ output = await evaluateInput("a[] = x;", freshState);
|
|
|
776
827
|
output = await evaluateInput(":pattern a", freshState);
|
|
777
828
|
assert.equal(
|
|
778
829
|
output[0],
|
|
779
|
-
"a[] =
|
|
780
|
-
"@QfyLpmn56wuppuGvrrUJz8LKtgaXfbMtad7RnzBcFk2S"
|
|
830
|
+
"a[] = x; -- " +
|
|
831
|
+
"@QfyLpmn56wuppuGvrrUJz8LKtgaXfbMtad7RnzBcFk2S[(...)]"
|
|
781
832
|
);
|
|
782
833
|
output = await evaluateInput(
|
|
783
834
|
"closed_same_pair[] = { T first, T second };",
|
|
@@ -786,8 +837,8 @@ output = await evaluateInput(
|
|
|
786
837
|
output = await evaluateInput(":pattern closed_same_pair", freshState);
|
|
787
838
|
assert.equal(
|
|
788
839
|
output[0],
|
|
789
|
-
"closed_same_pair[] = {
|
|
790
|
-
"@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm"
|
|
840
|
+
"closed_same_pair[] = {T first, T second}; -- " +
|
|
841
|
+
"@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm[T, T]"
|
|
791
842
|
);
|
|
792
843
|
|
|
793
844
|
output = await evaluateInput(
|
|
@@ -798,26 +849,26 @@ output = await evaluateInput(
|
|
|
798
849
|
output = await evaluateInput(":pat a", freshState);
|
|
799
850
|
assert.equal(
|
|
800
851
|
output[0],
|
|
801
|
-
"a[] = {
|
|
802
|
-
"@NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x"
|
|
852
|
+
"a[] = {X x, Y y}; -- " +
|
|
853
|
+
"@NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[(...), (...)]"
|
|
803
854
|
);
|
|
804
855
|
output = await evaluateInput(":pat b", freshState);
|
|
805
856
|
assert.equal(
|
|
806
857
|
output[0],
|
|
807
|
-
"b[
|
|
808
|
-
"@NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x"
|
|
858
|
+
"b[X] = {X x, Y y}; -- " +
|
|
859
|
+
"@NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X, (...)]"
|
|
809
860
|
);
|
|
810
861
|
output = await evaluateInput(":pat c", freshState);
|
|
811
862
|
assert.equal(
|
|
812
863
|
output[0],
|
|
813
|
-
"c[
|
|
814
|
-
"@NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x"
|
|
864
|
+
"c[X, Y] = {X x, Y y}; -- " +
|
|
865
|
+
"@NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X, Y]"
|
|
815
866
|
);
|
|
816
867
|
output = await evaluateInput(":pat d", freshState);
|
|
817
868
|
assert.equal(
|
|
818
869
|
output[0],
|
|
819
|
-
"d[
|
|
820
|
-
"@NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x"
|
|
870
|
+
"d[Y, X] = {X x, Y y}; -- " +
|
|
871
|
+
"@NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X, Y]"
|
|
821
872
|
);
|
|
822
873
|
output = await evaluateInput(":pats", freshState);
|
|
823
874
|
assert(
|
|
@@ -872,8 +923,8 @@ assert(
|
|
|
872
923
|
output = await evaluateInput(":pat p", freshState);
|
|
873
924
|
assert.equal(
|
|
874
925
|
output[0],
|
|
875
|
-
"p[
|
|
876
|
-
"@PScsPA3xYv4kSXUqHafwVQr5Cxkmspf3bj46KAcmyxmR"
|
|
926
|
+
"p[X] = {X x, Y y, Y z}; -- " +
|
|
927
|
+
"@PScsPA3xYv4kSXUqHafwVQr5Cxkmspf3bj46KAcmyxmR[X, Y, Y]"
|
|
877
928
|
);
|
|
878
929
|
|
|
879
930
|
output = await evaluateInput(
|
package/tests/test.mjs
CHANGED
|
@@ -1,75 +1,34 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
1
2
|
import k from "../index.mjs";
|
|
3
|
+
import { valueToK } from "../codecs/runtime/show-value.mjs";
|
|
2
4
|
|
|
3
|
-
[
|
|
4
|
-
[{
|
|
5
|
-
[{
|
|
6
|
-
[
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"{.year year, .age age}",
|
|
12
|
-
],
|
|
13
|
-
[{"duplicate": {}}, "()"],
|
|
14
|
-
[{"nesting": {}}, "()"],
|
|
15
|
-
[{"test": {"parseunit": {}}}, "()"],
|
|
16
|
-
].map(function ([data, script]) {
|
|
17
|
-
console.log(`k_expression = '${script}';`);
|
|
18
|
-
console.log(`k.run(k_expression,data});`);
|
|
19
|
-
console.log(`// RETURNS: ${JSON.stringify(k.run(script, data))}`);
|
|
20
|
-
return console.log("");
|
|
21
|
-
});
|
|
5
|
+
for (const [data, source] of [
|
|
6
|
+
[{ unit: {} }, "()"],
|
|
7
|
+
[{ name: { ala: {} }, age: { twentythree: {} } }, "{.name name, .age age}"],
|
|
8
|
+
[{ year: { twothousandtwo: {} }, age: { nineteen: {} } }, "{.year year, .age age}"],
|
|
9
|
+
[{ test: { parseunit: {} } }, "()"]
|
|
10
|
+
]) {
|
|
11
|
+
assert.equal(valueToK(k.run(source, data)), valueToK(k.fromObject(data)), source);
|
|
12
|
+
}
|
|
22
13
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
false = {} | false;
|
|
27
|
-
|
|
14
|
+
const list = k.compile(`
|
|
15
|
+
true = {} |true;
|
|
16
|
+
false = {} |false;
|
|
28
17
|
list? = ?< {} nil, {X car, Y cdr} cons > = Y;
|
|
29
18
|
nil = {}|nil list?;
|
|
30
19
|
nil? = list? /nil nil;
|
|
31
20
|
car = list? /cons .car;
|
|
32
|
-
list? {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
{"None": {}},
|
|
47
|
-
{"nil": {}},
|
|
48
|
-
{
|
|
49
|
-
"cons": {
|
|
50
|
-
"car": {"unit": {}},
|
|
51
|
-
"cdr": {
|
|
52
|
-
"cons": {
|
|
53
|
-
"car": {"unit": {}},
|
|
54
|
-
"cdr": {"nil": {}}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
"cons": {
|
|
61
|
-
"car": {"on": {}},
|
|
62
|
-
"cdr": {
|
|
63
|
-
"cons": {
|
|
64
|
-
"car": {"off": {}},
|
|
65
|
-
"cdr": {"nil": {}}
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
},
|
|
71
|
-
].map(function (data) {
|
|
72
|
-
console.log(`k_fn(${JSON.stringify(data)});`);
|
|
73
|
-
console.log(`// RETURNS: ${JSON.stringify(k_fn(k.fromObject(data)))}`);
|
|
74
|
-
return console.log("");
|
|
75
|
-
});
|
|
21
|
+
list? { <nil? true, false> nil_test, <car, {}|none> car }
|
|
22
|
+
`);
|
|
23
|
+
for (const [data, isNil, head] of [
|
|
24
|
+
[{ None: {} }, "false", "none"],
|
|
25
|
+
[{ nil: {} }, "true", "none"],
|
|
26
|
+
[{ cons: { car: { unit: {} }, cdr: { cons: { car: { unit: {} }, cdr: { nil: {} } } } } }, "false", "unit"],
|
|
27
|
+
[{ cons: { car: { on: {} }, cdr: { cons: { car: { off: {} }, cdr: { nil: {} } } } } }, "false", "on"]
|
|
28
|
+
]) {
|
|
29
|
+
const { product } = list(k.fromObject(data));
|
|
30
|
+
assert.equal(product.nil_test.tag, isNil);
|
|
31
|
+
assert.equal(product.car.tag, head);
|
|
32
|
+
}
|
|
33
|
+
assert.equal(k.compile("{} |ok")(k.fromObject({})).tag, "ok");
|
|
34
|
+
console.log("runtime API: ok");
|
package/AGENTS.md
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
# Repository Guidelines
|
|
2
|
-
|
|
3
|
-
## Project Structure & Module Organization
|
|
4
|
-
- Root `.mjs` files hold the core runtime and library entry points (e.g., `index.mjs`, `run.mjs`, `codes.mjs`, `typing.mjs`).
|
|
5
|
-
- `k_compiler/` contains the Python prototype compiler and supporting modules.
|
|
6
|
-
- `type_registry/` houses registry experiments and grammar artifacts.
|
|
7
|
-
- `Examples/` and `DOCS/` provide sample programs and design notes.
|
|
8
|
-
- Tests live in `tests/`, with type-derivation cases in `tests/code-derivation/`.
|
|
9
|
-
|
|
10
|
-
## Build, Test, and Development Commands
|
|
11
|
-
- `npm install` installs dev dependencies and runs `prepare` to generate parsers.
|
|
12
|
-
- `npm run prepare` regenerates `parser.mjs` and `valueParser.mjs` from `.jison` grammars.
|
|
13
|
-
- `npm test` runs the full suite through `scripts/run-tests.mjs`.
|
|
14
|
-
- `node repl.mjs` (or `k-repl`) starts the interactive REPL.
|
|
15
|
-
- `./k.mjs <file.k>` executes a k source/object file on binary pattern+value stream input from stdin or a file; if the argument is not an existing file, it is treated as an inline k expression.
|
|
16
|
-
|
|
17
|
-
## Coding Style & Naming Conventions
|
|
18
|
-
- JavaScript uses ES modules (`.mjs`) with 2-space indentation and semicolons.
|
|
19
|
-
- Prefer `const`/`let`, small focused modules, and descriptive lower-case filenames.
|
|
20
|
-
- Python modules in `k_compiler/` follow 4-space indentation and standard snake_case.
|
|
21
|
-
- Grammar sources are `parser.jison` and `valueParser.jison`; generated files should not be hand-edited.
|
|
22
|
-
|
|
23
|
-
## Testing Guidelines
|
|
24
|
-
- Add runtime tests alongside `tests/test.mjs` or new `tests/test-*.mjs` files.
|
|
25
|
-
- Add type-derivation coverage in `tests/code-derivation/` (one case per file is typical).
|
|
26
|
-
- Verify locally with `npm test` before opening a PR.
|
|
27
|
-
|
|
28
|
-
## Commit & Pull Request Guidelines
|
|
29
|
-
- Commits use short, imperative, sentence-case summaries (e.g., "Improve type derivation").
|
|
30
|
-
- PRs should include: a clear description, the commands run (`npm test` or specific subsets), and any relevant examples or docs updates.
|
|
31
|
-
- If changes touch parsing or grammars, note whether `npm run prepare` was run.
|
|
32
|
-
|
|
33
|
-
## Configuration Notes
|
|
34
|
-
- Requires Node.js 18+.
|
|
35
|
-
- The REPL and CLI binaries are `k-repl` and `k` (see `package.json` for mappings).
|
package/CLAUDE.md
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
# CLAUDE.md
|
|
2
|
-
|
|
3
|
-
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
-
|
|
5
|
-
## Project Overview
|
|
6
|
-
|
|
7
|
-
k is an experimental language for describing algebraic data shapes and composing typed transformations over them. Data descriptions and transformations share one syntax; the native k-like notation is canonical, and a JSON-like notation is available as a convenience for textual values.
|
|
8
|
-
|
|
9
|
-
## Essential Commands
|
|
10
|
-
|
|
11
|
-
**Build/Parser Generation:**
|
|
12
|
-
```bash
|
|
13
|
-
npm run prepare # Generate parsers from Jison grammar files
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
**Testing:**
|
|
17
|
-
```bash
|
|
18
|
-
npm test # Run comprehensive test suite (unit tests, derivation tests, shell tests)
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
**Running k programs:**
|
|
22
|
-
```bash
|
|
23
|
-
./k.mjs <file.k> # Execute k source/object file
|
|
24
|
-
./repl.mjs # Start interactive REPL (or: k-repl)
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
## Architecture
|
|
28
|
-
|
|
29
|
-
### Core Language Concepts
|
|
30
|
-
- **Partial Functions**: Functions may be undefined for some inputs (e.g., `.field` projection)
|
|
31
|
-
- **Function Composition**: `(f g h)` - sequential application
|
|
32
|
-
- **Union/Merge**: `<f, g>` - try f first, fallback to g
|
|
33
|
-
- **Product**: `{f label1, g label2}` - parallel application building records
|
|
34
|
-
- **Types (Codes)**: Finite tree automata defining value sets, no built-in types
|
|
35
|
-
- **Notation**: Prefer native k-like for products `{A l, ...}` and unions `<A t, ...>`; JSON-like `{l: A, ...}` and `<t: A, ...>` is supported for convenience
|
|
36
|
-
- **Variant value literals**: Use single-field product notation. Unit: `{{} tag}`; with payload `v`: `{ v tag }`. Angle brackets are for types/merge, not value literals.
|
|
37
|
-
|
|
38
|
-
### Key Implementation Files
|
|
39
|
-
- `parser.mjs` / `parser.jison` - Language parser (generated from Jison grammar)
|
|
40
|
-
- `run.mjs` - Runtime execution engine for partial functions
|
|
41
|
-
- `codes.mjs` - Type/code management and canonicalization
|
|
42
|
-
- `TypePatternGraph.mjs` - Core pattern graph data structure for type derivation
|
|
43
|
-
- `typing.mjs` - Type compression and advanced type system operations
|
|
44
|
-
- `Value.mjs` - Value representation and operations
|
|
45
|
-
- `index.mjs` - Main library entry point
|
|
46
|
-
|
|
47
|
-
### Multi-Stage Processing Pipeline
|
|
48
|
-
1. **Parsing**: Text → AST via Jison-generated parser
|
|
49
|
-
2. **Type Derivation**: Pattern-based type inference across expressions
|
|
50
|
-
3. **Canonicalization**: Types/functions get hash-based canonical names
|
|
51
|
-
4. **Execution**: Runtime evaluation with partial function semantics
|
|
52
|
-
|
|
53
|
-
### Type System
|
|
54
|
-
- **Product types**: `{type1 label1, type2 label2}` with projections `.label1`
|
|
55
|
-
- **Union types**: `<type1 label1, type2 label2>` with variant selection
|
|
56
|
-
- **Pattern matching**: Sophisticated constraint derivation system
|
|
57
|
-
- **Canonical names**: Hash-based universal registry for content-addressed modules
|
|
58
|
-
|
|
59
|
-
## Development Notes
|
|
60
|
-
|
|
61
|
-
- No external runtime dependencies (only jiwson for parser generation)
|
|
62
|
-
- Uses ES6 modules (.mjs files) throughout
|
|
63
|
-
- Extensive test suite in `tests/code-derivation/` for type system validation
|
|
64
|
-
- REPL supports commands like `:rel name` (show relation definition), `:rels`, etc.
|