@fraczak/k 8.0.0 → 8.2.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 CHANGED
@@ -5,6 +5,57 @@ 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
+ ## [8.2.0] — 2026-10-21 — Canonical Pattern Decompilation & Parameters
9
+
10
+ ### Interactive REPL & Tooling
11
+
12
+ - **Canonical Pattern Decompilation (`:pattern`, `:pat`)**:
13
+ - `:pattern <name>` (or `:pat <name>`) prints the canonical pattern
14
+ definition decompiled from its underlying minimal automaton,
15
+ rather than echoing user-defined construction syntax.
16
+ - Automatically erases author-chosen parameter and knot names,
17
+ introducing canonical variables (`X0`, `X1`, `X2`, ...) in
18
+ deterministic traversal (discovery) order.
19
+ - Open type holes (`(...)`) are recorded as introduced parameters
20
+ in the pattern header in discovery order:
21
+ * `pair[X0, X1] = {X0 first, X1 second}; -- @hash`
22
+ * `list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- @hash`
23
+ * `same_pair[X0] = {X0 first, X0 second}; -- @hash`
24
+ * `dict[X1, X2] = <{{X1 first, X2 second} car, X0 cdr} cons,`
25
+ `{} nil>=X0; -- @hash`
26
+ - Added `patternGraphToCanonical` to `pretty.mjs` for decompiling
27
+ pattern graphs with introduced canonical variables.
28
+ - Supports direct pattern lookup by content-addressed hash
29
+ (`:pattern @hash`, `:pat @hash`).
30
+ - Displays canonical content-addressed digest comments (`-- @hash`).
31
+ - Added comprehensive unit tests in `tests/test-repl.mjs`.
32
+ - Updated documentation across `DOCS/REPL.md`,
33
+ `DOCS/NAMED_PATTERNS.md`, and `DOCS/PATTERNS.md`.
34
+
35
+ ## [8.1.0] — 2026-10-20 — Dynamic JSON Bit-Trie & json? Standard Library
36
+
37
+ ### Dynamic JSON (djson) Consolidation
38
+
39
+ - **Unified Binary Bit-Trie Representation**:
40
+ - Consolidated all dynamic JSON approaches onto the binary bit-trie
41
+ implementation (`btrie[V]`), eliminating linear cons-lists and
42
+ heap allocations in dynamic document navigation.
43
+ - Standardized `Examples/djson.k` as the single dynamic JSON
44
+ specification and `codecs/djson.mjs` (`k-djson`) as its codec.
45
+ - Retired obsolete cons-list and Patricia trie dynamic JSON
46
+ variants (`djson-btrie.k`, `djson-ptrie.k`, `djson-btrie.mjs`,
47
+ `djson-ptrie.mjs`, `k-djson-btrie`, `k-djson-ptrie`).
48
+ - **Complete Standard Library on `json?`**:
49
+ - Implemented standard dynamic JSON methods in `Examples/djson.k`
50
+ operating directly on the universal `json?` type:
51
+ * Object operations: `get`, `has?`, `set`, `del`
52
+ * List operations: `nth`, `has_index?`, `set_nth`, `del_nth`,
53
+ `push`
54
+ * Enumerations: `keys`, `keys_raw`, `values`, `values_raw`
55
+ * Cardinality: `length` (JSON float64 number), `length_nat`
56
+ (`bits?`)
57
+ * Type inspection: `type_of` and `is_*?` predicates
58
+
8
59
  ## [8.0.0] — 2026-10-20 — Named Patterns, Type Schemes & Trie Codecs
9
60
 
10
61
  ### Language & Type System
package/DOCS/CODECS.md CHANGED
@@ -158,32 +158,30 @@ The `codecs/` directory provides the following standard codecs:
158
158
  - JSON objects map directly to products `{ field: val, ... }`.
159
159
  - JSON arrays map to indexed products `{ "0": a0, "1": a1, ... }`.
160
160
  - Ideal for record-oriented projection (e.g. `.user .name`).
161
- - **`djson.mjs` (`k-djson`) — Universal Dynamic JSON (Cons-List)**:
162
- Maps every JSON document to a single universal recursive named pattern
163
- `djson[]`:
161
+ - **`djson.mjs` (`k-djson`) — Universal Dynamic JSON (Binary Bit-Trie)**:
162
+ Maps every JSON document to a single universal recursive named
163
+ pattern `djson[]`:
164
164
  ```k
165
+ btrie[V] = < {} nil, { opt[V] val, T 0, T 1 } trie > = T;
166
+
165
167
  djson[] = <
166
- float64 number,
167
- str string,
168
- < {} true, {} false > bool,
168
+ number[] number,
169
+ string[] string,
170
+ bool[] bool,
169
171
  {} null,
170
- < {} nil, { J car, L cdr } cons > = L list,
171
- < {} nil, { { str key, J val } car, O cdr } cons > = O object
172
+ btrie[J] list,
173
+ btrie[J] object
172
174
  > = J;
173
175
  ```
174
- Because every JSON value shares the exact same static 29-node pattern,
175
- a `k` relation can be compiled once against `djson[]` and process
176
- arbitrary JSON documents of any shape or array/object length.
177
- - **`djson-btrie.mjs` (`k-djson-btrie`) — Binary Bit-Trie Dynamic JSON**:
178
- Replaces the linear `list` and `object` cons-lists with a unified
179
- 27-node binary bit-trie (`TRIE`). Array indices (`bits?`) and string
180
- keys are stored along MSB-to-LSB bit paths, enabling $O(|K|)$ lookup
181
- (`dot` and `nth` in `Examples/djson-btrie.k`) via direct `.0`/`.1`
182
- projections with zero character-equality checks.
183
- - **`djson-ptrie.mjs` (`k-djson-ptrie`) — Patricia/Radix Dynamic JSON**:
184
- Models `list` and `object` as path-compressed radix tries (`trie-array`
185
- style) with `prefix` and $O(1)$ subtree `size` (`list_size` and
186
- `obj_size` in `Examples/djson-ptrie.k`).
176
+ Uses a unified 27-node binary bit-trie for both arrays and objects.
177
+ Array indices (`bits?`) and string keys are stored along MSB-to-LSB
178
+ bit paths, enabling $O(|K|)$ lookup via direct `.0`/`.1` projections
179
+ with zero allocations and zero character-equality loops.
180
+ Accompanied by a complete standard library on `json?` in
181
+ [`Examples/djson.k`](../Examples/djson.k) (`get`, `nth`, `has?`,
182
+ `has_index?`, `set`, `del`, `set_nth`, `del_nth`, `push`, `keys`,
183
+ `keys_raw`, `values`, `values_raw`, `length`, `length_nat`,
184
+ `type_of`).
187
185
 
188
186
  ## Testing a Codec
189
187
 
@@ -218,9 +216,9 @@ npm test
218
216
  ## Web REPL (VFS) Bundling
219
217
 
220
218
  When `repl.html` is generated (`npm run build:repl-html`):
221
- - Codecs (`int.mjs`, `json.mjs`, `djson.mjs`, `djson-btrie.mjs`,
222
- `djson-ptrie.mjs`, `utf8.mjs`, `unit.mjs`, `ieee.mjs`) are bundled into
223
- 100% self-contained ES modules with zero imports.
219
+ - Codecs (`int.mjs`, `json.mjs`, `djson.mjs`, `utf8.mjs`, `unit.mjs`,
220
+ `ieee.mjs`) are bundled into 100% self-contained ES modules with zero
221
+ imports.
224
222
  - `runCodecCLI` is shimmed as a no-op, and `Value` helpers are directly
225
223
  inlined.
226
224
  - In the browser, the VFS serves these self-contained modules directly to
@@ -213,9 +213,7 @@ To make `codecs/` intuitive and self-contained:
213
213
  codecs/
214
214
  ├── int.mjs # Integer & list codec
215
215
  ├── json.mjs # JSON codec (schema-specializing)
216
- ├── djson.mjs # Dynamic JSON codec (cons-list)
217
- ├── djson-btrie.mjs # Dynamic JSON codec (binary bit-trie)
218
- ├── djson-ptrie.mjs # Dynamic JSON codec (Patricia/radix trie)
216
+ ├── djson.mjs # Dynamic JSON codec (binary bit-trie)
219
217
  ├── utf8.mjs # UTF-8 string codec
220
218
  ├── utf16.mjs # UTF-16 string codec
221
219
  ├── ieee.mjs # IEEE 754 float codec
@@ -244,8 +242,8 @@ In the browser REPL (`repl.html`):
244
242
  imports**.
245
243
  - **No Runtime Files in VFS**: `codecs/runtime/*` is not included in the
246
244
  browser VFS. The VFS only contains verified, self-contained user-facing
247
- files (`int.mjs`, `json.mjs`, `djson.mjs`, `djson-btrie.mjs`,
248
- `djson-ptrie.mjs`, `utf8.mjs`, `unit.mjs`, `ieee.mjs`).
245
+ files (`int.mjs`, `json.mjs`, `djson.mjs`, `utf8.mjs`, `unit.mjs`,
246
+ `ieee.mjs`).
249
247
  - **Native Browser Loading**: In `repl-codecs.mjs`, the browser reads the
250
248
  codec source from VFS, wraps it in a Blob, and invokes native `await
251
249
  import(blobUrl)`. No regex manipulation, import stripping, or global
@@ -1,26 +1,22 @@
1
1
  # Comparison of JSON Approaches in k
2
2
 
3
- This document compares the four JSON codec and schema implementations in
4
- the **k** repository:
3
+ > [!NOTE]
4
+ > **Resolution**: The **binary bit-trie** was selected and standardized
5
+ > as the sole dynamic JSON implementation in `codecs/djson.mjs` and
6
+ > `Examples/djson.k`. The cons-list and Patricia trie implementations
7
+ > were removed. This document preserves the architectural comparison
8
+ > and benchmark findings that guided this design decision.
9
+
10
+ This document compares the JSON codec and schema implementations:
5
11
 
6
12
  1. **Schema-Specializing JSON** (`json`):
7
13
  [`codecs/json.mjs`][json-mjs]
8
- 2. **Dynamic Cons-List JSON** (`djson`):
14
+ 2. **Universal Dynamic JSON (Binary Bit-Trie)** (`djson`):
9
15
  [`codecs/djson.mjs`][djson-mjs] & [`Examples/djson.k`][djson-k]
10
- 3. **Dynamic Binary Bit-Trie JSON** (`djson-btrie`):
11
- [`codecs/djson-btrie.mjs`][djson-btrie-mjs] &
12
- [`Examples/djson-btrie.k`][djson-btrie-k]
13
- 4. **Dynamic Patricia/Radix Trie JSON** (`djson-ptrie`):
14
- [`codecs/djson-ptrie.mjs`][djson-ptrie-mjs] &
15
- [`Examples/djson-ptrie.k`][djson-ptrie-k]
16
16
 
17
17
  [json-mjs]: file:///home/wojtek/gits/K/k/codecs/json.mjs
18
18
  [djson-mjs]: file:///home/wojtek/gits/K/k/codecs/djson.mjs
19
19
  [djson-k]: file:///home/wojtek/gits/K/k/Examples/djson.k
20
- [djson-btrie-mjs]: file:///home/wojtek/gits/K/k/codecs/djson-btrie.mjs
21
- [djson-btrie-k]: file:///home/wojtek/gits/K/k/Examples/djson-btrie.k
22
- [djson-ptrie-mjs]: file:///home/wojtek/gits/K/k/codecs/djson-ptrie.mjs
23
- [djson-ptrie-k]: file:///home/wojtek/gits/K/k/Examples/djson-ptrie.k
24
20
 
25
21
  ---
26
22
 
@@ -208,6 +208,29 @@ To guarantee decidability and termination:
208
208
  that yield isomorphic regular tree automata produce identical
209
209
  canonical hashes and wire encoders.
210
210
 
211
+ 4. **Canonical Form and REPL Decompilation**:
212
+ In the interactive REPL, inspecting a pattern via `:pattern <name>`
213
+ (or `:pat <name>`) displays its **canonical definition** rather than
214
+ the source syntax used to define it:
215
+ - User-defined parameter names (`A`, `B`, `X`) and knot names (`Y`,
216
+ `T`) are erased.
217
+ - The canonical decompiler introduces fresh variables (`X0`, `X1`,
218
+ `X2`, ...) in deterministic traversal (discovery) order.
219
+ - Open type ports are introduced in the pattern header (`name[...]`)
220
+ in order of discovery:
221
+ ```k
222
+ -- pair[A, B] = { A first, B second };
223
+ pair[X0, X1] = {X0 first, X1 second}; -- @...
224
+
225
+ -- list[X] = < {} nil, { X car, Y cdr } cons > = Y;
226
+ list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- @...
227
+
228
+ -- same_pair[T] = { T first, T second };
229
+ same_pair[X0] = {X0 first, X0 second}; -- @...
230
+ ```
231
+ - Two syntactically different definitions yielding isomorphic
232
+ automata produce the exact same canonical definition and hash.
233
+
211
234
  ---
212
235
 
213
236
  ## 7. Concrete Library Examples
package/DOCS/PATTERNS.md CHANGED
@@ -85,6 +85,11 @@ Direct or mutual recursion among templates (`bits[]` calling `bits[]`)
85
85
  is disallowed to ensure every template dependency graph is an acyclic
86
86
  DAG, guaranteeing finite regular tree automaton compilation.
87
87
 
88
+ In the interactive REPL, inspecting a pattern with `:pattern <name>` (or
89
+ `:pat <name>`) displays its canonical decompiled form with newly
90
+ introduced variables in discovery order (e.g.
91
+ `list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- @...`).
92
+
88
93
  ## Filters and Terminal Positions
89
94
 
90
95
  A k script can describe a pattern through:
package/DOCS/REPL.md CHANGED
@@ -37,7 +37,7 @@ is complete.
37
37
  | `:wasm` / `:pkir` / `:js` | Shortcut to switch engine |
38
38
  | `:rel name` | Show relation definition |
39
39
  | `:rels` | List relation aliases |
40
- | `:pattern name` | Show named pattern definition (alias: `:pat`) |
40
+ | `:pattern name` | Show canonical definition of pattern (alias: `:pat`) |
41
41
  | `:patterns` | List defined named pattern templates (alias: `:pats`) |
42
42
  | `:limit [num\|none]` | Set maximum characters for value output (default: 4096) |
43
43
  | `:codec load file` | Load a codec from an ES module file (e.g. `:codec load codecs/int.mjs`) |
@@ -155,6 +155,47 @@ Reads the plain-JSON library file and merges its codes, relations,
155
155
  aliases, and metadata into the session. Aliases are recovered from
156
156
  `meta[hash].origins[]` unless `--no-alias` is used.
157
157
 
158
+ ## Pattern Inspection
159
+
160
+ ### `:patterns` (or `:pats`)
161
+
162
+ Lists all currently defined named pattern templates along with their
163
+ declared parameter signatures (e.g. `bit[]`, `list[X]`, `pair[A, B]`).
164
+
165
+ ### `:pattern <name>` (or `:pat <name>`, `:pat @hash`)
166
+
167
+ Displays the **canonical definition** of a named pattern template:
168
+
169
+ ```text
170
+ > pair[A, B] = { A first, B second };
171
+ > :pat pair
172
+ pair[X0, X1] = {X0 first, X1 second}; -- @...
173
+
174
+ > list[T] = < {} nil, { T car, Y cdr } cons > = Y;
175
+ > :pat list
176
+ list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- @...
177
+
178
+ > same_pair[T] = { T first, T second };
179
+ > :pat same_pair
180
+ same_pair[X0] = {X0 first, X0 second}; -- @...
181
+ ```
182
+
183
+ Rather than echoing the syntax used to construct the template, the
184
+ canonical definition decompiles the underlying minimal finite tree
185
+ automaton. Local parameter and knot names chosen by the author are
186
+ erased; instead, the canonical definition introduces its own variables
187
+ (`X0`, `X1`, `X2`, ...) in deterministic traversal (discovery) order:
188
+
189
+ - **External Ports**: Open type holes (`(...)`) are recorded as
190
+ canonical parameters in the template header (`[X0, X1]`, `[X1]`, etc.)
191
+ in discovery order.
192
+ - **Recursive Knots**: Cycles back to existing nodes are bound using
193
+ knot variables (`=X0`).
194
+ - **Content Hash**: Every canonical definition is annotated with its
195
+ deterministic content-addressed hash (`-- @hash`).
196
+
197
+ Lookup by canonical hash is also supported: `:pattern @hash`.
198
+
158
199
  ## Codecs and Numerical Values
159
200
 
160
201
  In `k`, there are no primitive numbers or built-in numerical literals.
@@ -334,6 +375,9 @@ instantly without exhausting stack or terminal buffers. Use
334
375
  {}|0 ?<{} 0, ...>
335
376
  > :rel inc
336
377
  inc = | +1; -- @...
378
+ > pair[A, B] = { A first, B second };
379
+ > :pat pair
380
+ pair[X0, X1] = {X0 first, X1 second}; -- @...
337
381
  > :klib nat.klib
338
382
  saved nat.klib
339
383
  > :ko inc.ko inc
@@ -112,10 +112,9 @@ The bundled VFS and UI examples must include only verified useful files:
112
112
 
113
113
  #### 1. Curated Files Explorer
114
114
  In the Web REPL "Files" (VFS) explorer, the visible files are strictly curated to verified examples and codecs:
115
- - **k-codes**: `core.k`, `arithmetics.k`, `ieee.k`, `djson.k`,
116
- `djson-btrie.k`, `djson-ptrie.k`
117
- - **codecs**: `json.mjs`, `djson.mjs`, `djson-btrie.mjs`,
118
- `djson-ptrie.mjs`, `utf8.mjs`, `int.mjs`, `unit.mjs`, `ieee.mjs`
115
+ - **k-codes**: `core.k`, `arithmetics.k`, `ieee.k`, `djson.k`
116
+ - **codecs**: `json.mjs`, `djson.mjs`, `utf8.mjs`, `int.mjs`, `unit.mjs`,
117
+ `ieee.mjs`
119
118
  - User-created or uploaded files remain visible.
120
119
  - Internal runtime artifacts (`backends/wasm/runtime.wat`) and redundant directory prefixes (`Examples/`, `codecs/`) are kept accessible internally but hidden from the default file listing.
121
120
  - Clicking "Load" on a `.mjs` file automatically executes `:codec load <filename>`.