@fraczak/k 8.9.0 → 9.0.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 (84) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/DOCS/ARCHITECTURE.md +5 -4
  3. package/DOCS/DICTIONARY.md +2 -2
  4. package/DOCS/FILE_FORMATS.md +11 -9
  5. package/DOCS/KIR_V1.md +5 -1
  6. package/DOCS/KTVM_SPEC.md +902 -0
  7. package/DOCS/KVM_EXECUTION_MODEL.md +3 -2
  8. package/DOCS/KVM_VS_KTVM_COMPARATIVE_STUDY.md +127 -0
  9. package/DOCS/PROFILING_AND_TRACING.md +6 -4
  10. package/DOCS/README.md +3 -2
  11. package/DOCS/REPL.md +16 -12
  12. package/README.md +132 -2
  13. package/backend-api.mjs +31 -16
  14. package/backends/llvm/README.md +4 -5
  15. package/backends/llvm/bin/k-llvm-build.mjs +7 -19
  16. package/backends/llvm/bin/k-llvm-compile.mjs +6 -18
  17. package/backends/llvm/bin/k-llvm.mjs +11 -33
  18. package/backends/llvm/src/cli.mjs +2 -12
  19. package/backends/llvm/src/executable.mjs +7 -95
  20. package/backends/llvm/src/kvm2llvm.mjs +1 -5
  21. package/backends/llvm/src/llvm.mjs +4 -111
  22. package/backends/llvm/tests/k-llvm-run.mjs +1 -6
  23. package/backends/llvm/tests/perf-support.mjs +3 -5
  24. package/backends/llvm/tests/test-llvm.mjs +0 -16
  25. package/backends/wasm/README.md +2 -3
  26. package/backends/wasm/bin/k-wasm-compile.mjs +4 -7
  27. package/backends/wasm/bin/k-wasm.mjs +4 -6
  28. package/backends/wasm/src/cli.mjs +8 -24
  29. package/backends/wasm/src/wasm.mjs +10 -56
  30. package/browser/repl-browser.mjs +9 -8
  31. package/browser/shims/buffer.mjs +31 -2
  32. package/browser/shims/child_process.mjs +26 -0
  33. package/browser/shims/fs.mjs +3 -1
  34. package/conformance/run.mjs +17 -2
  35. package/harness/adapters/ktvm-llvm.mjs +118 -0
  36. package/harness/adapters/ktvm-wasm.mjs +104 -0
  37. package/harness/adapters/ktvm.mjs +133 -0
  38. package/harness/registry.mjs +9 -0
  39. package/ktvm/ast.mjs +218 -0
  40. package/ktvm/bit-deque.mjs +482 -0
  41. package/ktvm/fixtures/integer-streaming.k +477 -0
  42. package/ktvm/index.mjs +119 -0
  43. package/ktvm/llvm/index.mjs +202 -0
  44. package/ktvm/llvm/lower-c.mjs +1258 -0
  45. package/ktvm/llvm/runtime.h +3542 -0
  46. package/ktvm/llvm/test-llvm.mjs +476 -0
  47. package/ktvm/lower-kir.mjs +2221 -0
  48. package/ktvm/optimize/fuse-kernel.mjs +2111 -0
  49. package/ktvm/optimize/index.mjs +116 -0
  50. package/ktvm/optimize/mwtr.mjs +283 -0
  51. package/ktvm/optimize/simplify-procs.mjs +5250 -0
  52. package/ktvm/optimize/synthesize-tables.mjs +1105 -0
  53. package/ktvm/parser.mjs +1101 -0
  54. package/ktvm/printer.mjs +281 -0
  55. package/ktvm/schema.mjs +543 -0
  56. package/ktvm/specialize.mjs +538 -0
  57. package/ktvm/test-conformance.mjs +597 -0
  58. package/ktvm/test-ktvm.mjs +1376 -0
  59. package/ktvm/test-optimize.mjs +598 -0
  60. package/ktvm/validator.mjs +1280 -0
  61. package/ktvm/vm.mjs +2225 -0
  62. package/ktvm/wasm/index.mjs +331 -0
  63. package/ktvm/wasm/lower-wasm.mjs +1272 -0
  64. package/ktvm/wasm/runtime.wat +4413 -0
  65. package/ktvm/wasm/test-wasm.mjs +426 -0
  66. package/ktvm/wire.mjs +489 -0
  67. package/package.json +1 -9
  68. package/repl.mjs +101 -20
  69. package/scripts/build-repl-html.mjs +40 -18
  70. package/scripts/perf-bigint-compare.mjs +12 -16
  71. package/scripts/perf-ieee.mjs +9 -229
  72. package/scripts/perf-int.mjs +14 -272
  73. package/scripts/perf-multiplication.mjs +8 -54
  74. package/scripts/perf-poly.mjs +9 -263
  75. package/scripts/run-tests.mjs +5 -2
  76. package/scripts/test-package.mjs +15 -3
  77. package/tests/README.md +3 -3
  78. package/tests/test-repl-html.mjs +72 -15
  79. package/tests/test-repl.mjs +24 -17
  80. package/DOCS/POLYMORPHIC_IR.md +0 -86
  81. package/access-table.mjs +0 -343
  82. package/pkir.mjs +0 -772
  83. package/tests/test-access-table.mjs +0 -213
  84. package/tests/test-pkir.mjs +0 -530
package/CHANGELOG.md CHANGED
@@ -5,6 +5,23 @@ All notable changes to this project will be documented here.
5
5
  The format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
6
6
  This project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [9.0.0] — 2026-11-26 — kTVM Packed-Wire Transducer VM, Wasm & LLVM Backends, and PKIR Removal
9
+
10
+ - **Breaking Changes (PKIR Removal)**:
11
+ - Remove the experimental PKIR layer (`pkir.mjs`, `access-table.mjs`, `DOCS/POLYMORPHIC_IR.md`, `tests/test-pkir.mjs`, `tests/test-access-table.mjs`) now that all execution backends (JS, kVM Wasm/LLVM, and kTVM JS/Wasm/LLVM) are natively polymorphic.
12
+ - Remove `--pkir`, `.pkir` input handling, `:pkir`, `:engine pkir`, `usePKIR`, and `compile*FromPKIR` APIs across `backend-api.mjs`, `backends/wasm/`, `backends/llvm/`, `repl.mjs`, `browser/repl-browser.mjs`, `scripts/build-repl-html.mjs`, `package.json`, and `scripts/perf-*.mjs`.
13
+ - **kTVM Packed-Wire Transducer VM (`ktvm/`)**:
14
+ - Introduce the standalone **kTVM** (`kTVM-P` / `kTVM`, `format: "k-tvm"`, v0.1) streaming pushdown transducer VM executing k relations directly over unmodified prefix-free `k-wire` bitstreams without materializing heap value trees.
15
+ - Implement core IR, `.ktvmp`/`.ktvm` DSL parser/printer, JSON serialization, Moore schema minimization, static validator (SPSC linearity, dual `(prodSchema, consSchema)` checking, DAG acyclicity, and table-to-CFG bisimulation verification), boundary envelope specializer (`specializeKTVM`), `KIR-P → kTVM-P` lowering (`lower-kir.mjs`), and strictly iterative `BitDeque` reference VM (`vm.mjs`, `wire.mjs`).
16
+ - Add verified `kTVM → kTVM` optimization pipeline (`ktvm/optimize/`): procedure deforestation, entry-guard `spec → switch` compilation, bounded short-bit unfolding, strict accumulator settlement (`after %tok`), CPS symbolic kernel fusion, TMC streaming with register/LIFO virtual-stack promotion, and L1-budgeted (64 KiB) SWAR bit-chunk/stack-chunk/FSM table synthesis via Howard's MWTR criterion.
17
+ - **kTVM WebAssembly & Native C/LLVM Backends (`ktvm/wasm/`, `ktvm/llvm/`)**:
18
+ - Add `ktvm/wasm/` lowering concrete `kTVM` modules to validated `.wasm` binaries via WABT with zero JS tree marshaling at the wire boundary, reusable `<= 128b`-inline + segmented-slab `BitDeque` buffers, slab freelist reclamation, and static table data segments.
19
+ - Add `ktvm/llvm/` lowering concrete `kTVM` modules to C99 compiled with `clang -O3` into standalone wire-to-wire native executables (register-cached fixed-width/self-looping DPDA, zero-`memset` inline-channel node slabs, eager `const_unit`/`wrap`/`splice` lowering, and batched slab reclamation).
20
+ - Integrate `ktvm-wasm` into the terminal and browser REPLs (`--ktvm`, `:ktvm`, `:engine ktvm`, and interactive 3-way badge cycling `wasm → ktvm → js`), and add `ktvm`, `ktvm-wasm`, and `ktvm-llvm` lanes to `harness/cli.mjs` and the five `ktvm/` test suites to `npm test`.
21
+ - **Documentation & Packaging**:
22
+ - Add [DOCS/KTVM_SPEC.md](DOCS/KTVM_SPEC.md) and [DOCS/KVM_VS_KTVM_COMPARATIVE_STUDY.md](DOCS/KVM_VS_KTVM_COMPARATIVE_STUDY.md), document all execution backends and usage in [README.md](README.md), and update all architecture, REPL, file-format, backend, and test guides.
23
+ - Tighten `.npmignore` (exclude `experiments/`, `DOCS/*.pdf`, `DOCS/ktvm-explainer.*`) and verify packed `ktvm/wasm/runtime.wat`, `ktvm/llvm/runtime.h`, and end-to-end kTVM Wasm execution in `scripts/test-package.mjs`.
24
+
8
25
  ## [8.9.0] — 2026-11-22 — Canonical Empty-Bit Zero Representation and BigInt-Free Integer Codec
9
26
 
10
27
  - **Canonical Zero Representation (`{}|_` / `{}|_|+`)**:
@@ -48,7 +48,7 @@ The execution representations have distinct roles:
48
48
  | --- | --- | --- |
49
49
  | KIR-P | `kir.mjs` | Portable export of object relations and pattern graphs |
50
50
  | kVM | `kvm.mjs` | Register instructions, polymorphic templates, and specialization |
51
- | PKIR | `pkir.mjs`, `access-table.mjs` | Experimental schema-state/layout-witness execution |
51
+ | kTVM | `ktvm/` | Packed-wire transducer VM: bit-stream lowering, optimization, Wasm & LLVM backends |
52
52
 
53
53
  KIR-P envelope specialization (`retypeObjectRelation` and related functions)
54
54
  can rerun contract derivation. kVM template specialization (`specializeKVM`)
@@ -57,11 +57,12 @@ Do not treat these as the same pass.
57
57
 
58
58
  The Wasm and LLVM backends lower kVM instructions through
59
59
  `backends/wasm/src/kvm2wasm.mjs` and `backends/llvm/src/kvm2llvm.mjs`.
60
- Their PKIR paths adapt the layout-oriented representation for target lowering.
60
+ In parallel, `ktvm/wasm/` and `ktvm/llvm/` lower packed-wire kTVM modules
61
+ directly to WebAssembly and LLVM IR over `BitDeque` bit-streams.
61
62
  `backend-api.mjs` exposes the shared compiler/backend API.
62
63
 
63
64
  See the [KIR-P](KIR_V1.md), [kVM](KVM_EXECUTION_MODEL.md), and
64
- [PKIR](POLYMORPHIC_IR.md) references for their contracts.
65
+ [kTVM](KTVM_SPEC.md) references for their contracts.
65
66
 
66
67
  ## Codecs and user interfaces
67
68
 
@@ -73,7 +74,7 @@ See the [KIR-P](KIR_V1.md), [kVM](KVM_EXECUTION_MODEL.md), and
73
74
  - `repl.mjs` manages sessions; `repl-codecs.mjs` handles codec loading/rendering.
74
75
  - `browser/repl-browser.mjs` implements the browser UI.
75
76
  - `scripts/build-repl-html.mjs` bundles browser shims, VFS libraries, codecs,
76
- and Wasm resources into one HTML file.
77
+ and Wasm / kTVM-Wasm resources into one HTML file.
77
78
 
78
79
  `core.k` defines the fixed `pattern[]` schema used to decode a stream's leading
79
80
  pattern value. This does not automatically import the library into user scope.
@@ -23,12 +23,12 @@ These terms are used throughout the [documentation](README.md).
23
23
  | **Wire stream** | The binary representation of an envelope followed by a payload encoded under it. |
24
24
  | **Codec** | A translator between an external representation and enveloped k values. |
25
25
  | **Contract derivation** | Compiler propagation and unification of pattern constraints, with fixed-point iteration for recursive relation groups. |
26
- | **Specialization** | Adapting a relation or execution artifact to a particular input envelope. The KIR-P and kVM passes use different mechanisms. |
26
+ | **Specialization** | Adapting a relation or execution artifact to a particular input envelope. The KIR-P, kVM, and kTVM passes use different mechanisms. |
27
27
  | **Partial failure** | Ordinary undefinedness during execution, distinct from a schema contradiction or system error. |
28
28
  | **Object / library** | Stored compiled definitions: `.ko` has an entry relation; `.klib` has no main entry. |
29
29
  | **KIR-P** | Portable relation and pattern export from compiled objects. |
30
30
  | **kVM** | Register-based execution representation, with polymorphic templates and specialized artifacts. |
31
- | **PKIR** | A separate experimental representation using schema states and layout witnesses for structural access. |
31
+ | **kTVM** | Packed-wire transducer virtual machine that executes k relations directly over canonical prefix-coded bit-streams (with JS, WebAssembly, and LLVM backends). |
32
32
 
33
33
  A contract's input pattern is a static constraint, not necessarily the exact
34
34
  mathematical domain of the relation. A relation can be undefined for values
@@ -12,7 +12,7 @@ Run the examples below from the repository root after `npm install`.
12
12
  | `.ko` | Compiled executable object with a main relation | `k-compile`, `k`, backend tools |
13
13
  | `.klib` | Compiled definitions with no main relation | `k-compile`, `:klib`, `--lib`, `:load` |
14
14
  | `.kvm` | JSON kVM artifact: polymorphic `KVM-P` template or specialized `KVM` program | `k-compile`, specialization API, backends |
15
- | `.pkir` | JSON experimental PKIR relation (`format: "pk-ir"`, version 1) | JavaScript API, backends |
15
+ | `.ktvm` / `.ktvmp` | Textual DSL or JSON kTVM module (`format: "k-tvm"`, version 1) | JavaScript API (`ktvm/`) |
16
16
  | `.wasm` | WebAssembly module with k runner metadata | `k-wasm-compile`, `k-wasm-run` |
17
17
 
18
18
  A source file executed directly needs a terminal expression. A library can
@@ -219,15 +219,17 @@ node codecs/unit.mjs --parse | node kvm.mjs /tmp/k-ok.ko |
219
219
  node codecs/k-print.mjs
220
220
  ```
221
221
 
222
- ## PKIR and targets
222
+ ## kTVM and target backends
223
223
 
224
- [PKIR](POLYMORPHIC_IR.md) is a separate experimental execution representation.
225
- `compileRelationToPKIR` in `backend-api.mjs` creates a relation artifact;
226
- `executePKIR` interprets it and `specializePKIR` resolves layout accesses.
224
+ [kTVM](KTVM_SPEC.md) is the packed-wire transducer VM (`ktvm/`), which lowers
225
+ KIR-P relations into polymorphic `.ktvmp` or concrete `.ktvm` modules
226
+ (`format: "k-tvm"`, version 1) and executes them directly over `k-wire`
227
+ bitstreams via its JavaScript interpreter, WebAssembly backend (`ktvm/wasm/`),
228
+ and native LLVM backend (`ktvm/llvm/`).
227
229
 
228
- The backend compile commands accept `.pkir` input, or `--pkir` when compiling
229
- source through that path. They also accept source, executable objects, and kVM
230
- programs. A library still needs an entry relation.
230
+ The kVM backend compile commands accept source, executable objects (`.ko`),
231
+ and kVM programs (`.kvm`). A `.klib` library still needs an entry relation.
231
232
 
232
- See the [WebAssembly](../backends/wasm/README.md) and
233
+ See the [kTVM specification](KTVM_SPEC.md),
234
+ [WebAssembly](../backends/wasm/README.md), and
233
235
  [LLVM](../backends/llvm/README.md) guides for target compilation and execution.
package/DOCS/KIR_V1.md CHANGED
@@ -184,6 +184,8 @@ The current kVM lowerer consumes KIR-P relation bodies directly:
184
184
  k source -> .ko / .klib -> KIR-P -> kVM template
185
185
  + input envelope
186
186
  -> specialized kVM -> interpreter / Wasm / LLVM
187
+ -> kTVM-P / kTVM (packed-wire)
188
+ -> interpreter / Wasm / LLVM
187
189
  ```
188
190
 
189
191
  KIR-P envelope specialization is also available as a separate derivation pass,
@@ -215,7 +217,9 @@ call-site worklists are local compiler data; they are not part of the
215
217
  serialized KIR artifact.
216
218
 
217
219
  - [DOCS/FILE_FORMATS.md](./FILE_FORMATS.md) —
218
- source, object, library, kVM, and PKIR workflows
220
+ source, object, library, and kVM workflows
219
221
  - [DOCS/KVM_EXECUTION_MODEL.md](./KVM_EXECUTION_MODEL.md) —
220
222
  kVM execution model and bytecode design
223
+ - [DOCS/KTVM_SPEC.md](./KTVM_SPEC.md) —
224
+ kTVM packed-wire transducer VM specification
221
225
  - [DOCS/DICTIONARY.md](./DICTIONARY.md) — concept names and terminology