@fraczak/k 6.0.4 → 6.1.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 (59) hide show
  1. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  2. package/.idea/copilot.data.migration.agent.xml +6 -0
  3. package/.idea/copilot.data.migration.ask.xml +6 -0
  4. package/.idea/copilot.data.migration.ask2agent.xml +6 -0
  5. package/.idea/copilot.data.migration.edit.xml +6 -0
  6. package/.idea/k.iml +9 -0
  7. package/.idea/misc.xml +6 -0
  8. package/.idea/modules.xml +8 -0
  9. package/.idea/vcs.xml +6 -0
  10. package/AGENTS.md +2 -2
  11. package/CHANGELOG.md +22 -5
  12. package/CLAUDE.md +4 -4
  13. package/DOCS/BIG_PICTURE.md +5 -5
  14. package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +3 -3
  15. package/DOCS/OBJECT_FILE_AND_PATTERN.md +9 -9
  16. package/DOCS/PATTERNS.md +6 -5
  17. package/DOCS/REPL.md +2 -2
  18. package/DOCS/book/06-values-in-memory.md +1 -1
  19. package/DOCS/book.md +20 -24
  20. package/README.md +200 -274
  21. package/codecs/README.md +21 -2
  22. package/codecs/example-pipeline.mjs +14 -0
  23. package/codecs/ieee.mjs +13 -3
  24. package/codecs/int.mjs +13 -3
  25. package/codecs/json.mjs +13 -3
  26. package/codecs/k-parse.mjs +5 -0
  27. package/codecs/k-print.mjs +16 -1
  28. package/codecs/show.mjs +15 -1
  29. package/codecs/unit.mjs +13 -3
  30. package/codecs/utf16.mjs +13 -3
  31. package/codecs/utf8.mjs +13 -3
  32. package/k-vscode/README.md +3 -2
  33. package/k.mjs +9 -1
  34. package/k_compiler/runtime.c +205 -0
  35. package/k_compiler/runtime.h +34 -0
  36. package/objects/README.md +11 -8
  37. package/package.json +13 -7
  38. package/{repl2.mjs → repl.mjs} +47 -9
  39. package/{test-repl2.mjs → test-repl.mjs} +16 -5
  40. package/.codex +0 -0
  41. package/DOCS/a.html +0 -398
  42. package/NAT.k~ +0 -452
  43. package/UU/ta.k +0 -1
  44. package/UU/ta.k~ +0 -0
  45. package/UU/tb.k +0 -0
  46. package/UU/tc.k +0 -0
  47. package/codecs/BINARY_FORMAT.html +0 -471
  48. package/codecs/POLYMORPHIC_BINARY_FORMAT.html +0 -355
  49. package/core.json +0 -1
  50. package/core.klib +0 -1
  51. package/core.klib~ +0 -0
  52. package/ieee.klib +0 -0
  53. package/k-vscode/package-lock.json +0 -4176
  54. package/kkk.k +0 -197
  55. package/kkk.klib +0 -0
  56. package/kkk.klib~ +0 -0
  57. package/p.json +0 -5475
  58. package/type_registry/registry.mjs~ +0 -105
  59. package/type_registry/typeParser.jison~ +0 -132
package/README.md CHANGED
@@ -1,375 +1,301 @@
1
- # k-language
1
+ # k
2
2
 
3
- > **k** is a language for composing first-order partial functions over algebraic data types.
3
+ **k is a small language for typed data transformations.**
4
4
 
5
- Values are tree-shaped and built solely from **products** (records) and **tagged unions** (variants).
6
- There are no built-in primitive types — the only leaf in a non-recursive definition is the empty product `{}`.
7
- Types (called *codes*) are finite tree automata; equivalence is bisimilarity.
5
+ It describes data as algebraic shapes, programs as first-order partial
6
+ functions, and runtime values as a binary `pattern + value` stream that can be
7
+ parsed, transformed, printed, compiled, and inspected.
8
8
 
9
- ---
9
+ Data descriptions and transformations share one syntax, so a k file can define
10
+ both the shapes of values and the relations that move between them.
10
11
 
11
- ## Quick Start
12
+ k is experimental, but it already has a working parser, type-derivation engine,
13
+ REPL, binary codec pipeline, object/library format, Node.js API, and test suite.
12
14
 
13
- **Prerequisites:** Node.js 18+
15
+ ## Why k?
14
16
 
15
- ```bash
16
- npm install # install dev deps and generate parsers
17
- npm test # run the full test suite
18
- k-repl # start the interactive REPL (or: node repl2.mjs)
19
- ./k.mjs <file.k> # execute a k script
20
- ```
21
-
22
- > After `npm link` (or `npm install -g .`) the binaries `k`, `k-repl`, `k-parse`, `k-print`, etc. are available globally.
23
-
24
- ---
17
+ Many systems need to answer the same questions:
25
18
 
26
- ## CLI Binaries
19
+ - What shape does this data have?
20
+ - Which transformations are valid for that shape?
21
+ - What does a program accept, produce, or reject?
22
+ - Can this transformation be serialized, tested, reused, or inspected?
27
23
 
28
- | Binary | Source | Purpose |
29
- |--------|--------|---------|
30
- | `k` | `k.mjs` | Execute a `.k` script; reads binary pattern+value stream from stdin |
31
- | `k-repl` / `k-repl2` | `repl2.mjs` | Interactive interpreter |
32
- | `k-parse` | `codecs/k-parse.mjs` | Encode textual k values to the binary pattern+value wire format |
33
- | `k-print` | `codecs/k-print.mjs` | Decode binary pattern+value stream to JSON-like textual k values |
34
- | `k-pattern` | `patterns/from-k.mjs` | Extract the canonical root pattern from a k script |
35
- | `k-compile-object` | `objects/compile.mjs` | Compile a `.k` source to an executable `.ko` object |
36
- | `k-decompile-object` | `objects/decompile.mjs` | Decompile a `.ko` or `.klib` file back to k source |
37
- | `k-extract-aliases` | `objects/extract-aliases.mjs` | Extract `.klib` metadata aliases as k source |
24
+ k explores a compact answer: define algebraic data shapes, compose partial
25
+ transformations over them, derive input/output patterns automatically, and move
26
+ values across process boundaries with a canonical binary representation.
38
27
 
39
- ---
28
+ That makes k interesting as a foundation for:
40
29
 
41
- ## Data Model: Codes = ADTs
30
+ - schema definitions and schema-to-schema transformations
31
+ - binary codecs and canonical serialization
32
+ - protocol, hardware, or test-vector transformation pipelines
33
+ - teaching algebraic data types and compositional programming
34
+ - research into partial functions, finite tree automata, and typed IRs
42
35
 
43
- Codes are algebraic data types built from two constructors:
36
+ ## A Small Example
44
37
 
45
- | Constructor | Native k notation | JSON-like notation |
46
- |-------------|------------------|--------------------|
47
- | **Product** (record) | `{ A label1, B label2 }` | `{ label1: A, label2: B }` |
48
- | **Tagged union** (variant) | `< A tag1, B tag2 >` | `< tag1: A, tag2: B >` |
49
-
50
- Native k-like notation is canonical; JSON-like is supported as syntactic sugar.
51
-
52
- For the JSON-like textual notation used by `k-parse` and `k-print`, see
53
- [DOCS/TEXTUAL_VALUES.md](DOCS/TEXTUAL_VALUES.md).
54
-
55
- ### Defining codes (`$`)
38
+ k can define recursive data and transformations in the same file. Peano natural
39
+ numbers are either `0` or one more than another natural number:
56
40
 
57
41
  ```k
58
- $ nat = < {} zero, nat succ >; -- Peano naturals
59
- $ bit = < {} 0, {} 1 >;
60
- $ byte = { bit 0, bit 1, bit 2, bit 3, bit 4, bit 5, bit 6, bit 7 };
61
- $ list = < {} nil, { bit car, list cdr } cons >;
62
- ```
42
+ $ nat = < {} 0, nat +1 >;
63
43
 
64
- Two codes are equal iff they are bisimilar: same label/tag sets, same constructor kind (product vs. union), and subcodes relate pointwise under each label/tag.
44
+ zero = {} |0;
45
+ inc = |+1;
65
46
 
66
- ---
67
-
68
- ## Partial Functions
69
-
70
- A partial function may be undefined on some inputs. Core primitives:
47
+ add = ?X <
48
+ { .x /+1 x, .y inc y } ?X add,
49
+ .y
50
+ >;
51
+ ```
71
52
 
72
- | Syntax | Name | Meaning |
73
- |--------|------|---------|
74
- | `.field` | product projection | extract `field` from a product value |
75
- | `/tag` | union projection | assert value has tag `tag`, extract payload |
76
- | `\|tag` | variant introduction | wrap value in tag `tag` |
77
- | `()` | identity | pass value through unchanged |
78
- | `<>` | always-undefined | undefined for all inputs |
79
- | `{}` | empty product | map any input to `{}` |
53
+ `add` takes a product `{ x, y }`. If `x` has a `+1`, it moves that successor
54
+ from `x` to `y` and recurses. When that no longer applies, it returns `y`.
80
55
 
81
- **Examples:**
56
+ In the REPL:
82
57
 
83
58
  ```text
84
- .toto :
85
- { "toto": {"5":{}}, "titi": {"10":{}} } --> {"5":{}}
86
- { "titi": {"10":{}} } ... undefined
87
-
88
- /toto :
89
- {"toto": {}} --> {}
90
- {"other": {}} ... undefined
91
-
92
- |toto :
93
- {"5":{}} --> { "toto": {"5":{}} }
59
+ > { {} |0 inc inc x, {} |0 inc y } add
60
+ {}|0|+1|+1|+1 ?<X0 +1, {} 0, ...>=X0
94
61
  ```
95
62
 
96
- ---
97
-
98
- ## Combining Functions
63
+ That evaluates `2 + 1` to `3`: a value starts at `0`, and each `+1` tag adds
64
+ one successor. The REPL also prints the inferred value envelope.
99
65
 
100
- ### 1. Composition — sequential application
66
+ ## Try It
101
67
 
102
- `(f g h)` applies `f`, then `g`, then `h`. Parentheses can be omitted.
68
+ From a checkout:
103
69
 
104
- ```text
105
- (.toto .titi) :
106
- { "toto": { "titi": {} } } --> {}
107
- { "toto": { "other": {} } } ... undefined
70
+ ```bash
71
+ node --version # requires Node.js 18+
72
+ npm install
73
+ npm test
74
+ npm link
108
75
  ```
109
76
 
110
- ### 2. Merge — first-defined wins
77
+ Then run a tiny binary pipeline:
111
78
 
112
- `<f, g>` tries `f` first; if undefined, tries `g`.
113
-
114
- ```text
115
- < .x .y, .z .y > :
116
- { "x": {"y": {"5":{}}}, "z": {"y": {"10":{}}} } --> {"5":{}}
117
- { "x": {"o": {}}, "z": {"y": {"10":{}}} } --> {"10":{}}
118
- { "x": {"o": {}}, "z": {"o": {}} } ... undefined
79
+ ```bash
80
+ k-unit --parse | k '{} |ok' | k-print
119
81
  ```
120
82
 
121
- ### 3. Product — parallel, labelled collection
122
-
123
- `{f label1, g label2}` applies `f` and `g` in parallel and builds a record.
83
+ Expected output:
124
84
 
125
- ```text
126
- { .toto TOTO, .titi TITI } :
127
- { "toto": {"5":{}}, "titi": {"10":{}}, "x":{} }
128
- --> { "TOTO": {"5":{}}, "TITI": {"10":{}} }
85
+ ```json
86
+ "ok"
129
87
  ```
130
88
 
131
- ---
132
-
133
- ## Syntactic Sugar
134
-
135
- - Parentheses may be omitted (except for empty composition `()`).
136
- - `.a .b /c` is equivalent to `(.a (.b /c))`.
137
- - Comments: `//`, `--`, `%`, `#` (single-line) or `/* ... */` (multi-line).
138
- - Both notation forms work in the same file.
139
-
140
- ---
89
+ Start the interactive interpreter:
141
90
 
142
- ## Interpreter
91
+ ```bash
92
+ k-repl
93
+ ```
143
94
 
144
- Start with `k-repl` (or `node repl2.mjs`). The prompt is `> `.
95
+ Every installed command supports `-h` and `--help`.
145
96
 
146
- `repl2.mjs` keeps a live `.klib`-style state in memory. You can:
97
+ ## What k Gives You
147
98
 
148
- - define types and relations incrementally
149
- - run expressions against the current value
150
- - load `.k` or `.klib` files into the session
151
- - export the session as `.klib`
152
- - export an executable `.ko` from an expression in the current context
99
+ **Algebraic data shapes**
153
100
 
154
- ### Main commands
101
+ Types, called *codes*, are built from products and tagged unions:
155
102
 
156
- | Command | Effect |
157
- |---------|--------|
158
- | `:type name = <...>` | Define a type alias |
159
- | `:rel name = expr` | Define a relation alias |
160
- | `:run expr` | Evaluate an expression on the current value |
161
- | `:t name` | Show relation input/output filters |
162
- | `:d name` | Show relation definition |
163
- | `:C name` | Show canonical code definition |
164
- | `:codes` / `:rels` | List type or relation aliases |
165
- | `:load [--no-alias] file` | Load `.k` or `.klib`; aliases are loaded unless `--no-alias` is used |
166
- | `:klib file` | Export current state as `.klib` |
167
- | `:ko file expr` | Export executable `.ko` |
168
- | `:val` | Print current value |
169
- | `:reset` | Clear state |
170
- | `:help` | Show command summary |
103
+ ```k
104
+ $ bit = < {} 0, {} 1 >;
105
+ $ byte = { bit 0, bit 1, bit 2, bit 3, bit 4, bit 5, bit 6, bit 7 };
106
+ $ bits = < {} _, bits 0, bits 1 >;
107
+ ```
171
108
 
172
- ### Raw snippets
109
+ There are no built-in primitive values. The empty product `{}` is the only leaf
110
+ in a non-recursive definition.
173
111
 
174
- Raw k input is also accepted. It compiles on top of the current interpreter
175
- state.
112
+ **Composable partial functions**
176
113
 
177
- ```text
178
- > $ bool = <{} true, {} false>;
179
- > not = $bool </true | false, {} | true>;
180
- > {} |true not
181
- {false: {}} ?<{} true, {} false>
182
- ```
114
+ Core expressions are deliberately small:
183
115
 
184
- Definitions-only snippets extend the session silently. Snippets with a terminal
185
- expression are evaluated immediately.
116
+ | Syntax | Meaning |
117
+ | --- | --- |
118
+ | `.field` | project a product field |
119
+ | `/tag` | project a tagged-union branch |
120
+ | `\|tag` | introduce a tagged-union branch |
121
+ | `(f g)` | compose transformations |
122
+ | `<f, g>` | try `f`, then `g` if `f` is undefined |
123
+ | `{f a, g b}` | build a product from parallel transformations |
124
+ | `()` | identity |
125
+ | `<>` | always undefined |
126
+ | `{}` | constant empty product |
186
127
 
187
- Tab completion covers:
128
+ **Derived input/output patterns**
188
129
 
189
- - command names
190
- - file paths for `:load`, `:klib`, and `:ko`
191
- - aliases
192
- - canonical names beginning with `@`
130
+ k derives structural constraints for expressions. Those patterns become useful
131
+ for diagnostics, REPL output, binary encoding, and object metadata.
193
132
 
194
- See [DOCS/REPL.md](DOCS/REPL.md) for the exact buffering and evaluation rules.
133
+ **Binary-friendly runtime values**
195
134
 
196
- ---
135
+ The command-line pipeline uses a self-describing binary stream:
197
136
 
198
- ## Code Derivation and Patterns
137
+ ```text
138
+ encoded pattern, followed by value encoded under that pattern
139
+ ```
199
140
 
200
- A **pattern** (or *filter*) is a constraint on the type of values flowing through a function.
201
- The type derivation engine infers patterns automatically and can optionally be guided with filter expressions:
141
+ The boundary tools are:
202
142
 
203
- ```k
204
- treeFilter = ?< (...) leaf, { T left, T right } tree > = T;
143
+ ```bash
144
+ k-parse # textual k value -> binary pattern+value stream
145
+ k # apply a k expression or .k/.ko program to the stream
146
+ k-print # binary pattern+value stream -> textual value
205
147
  ```
206
148
 
207
- This tells the derivation engine that `treeFilter` operates on values that are either a `leaf` or a `tree` with matched left/right subtypes.
208
-
209
- ---
149
+ **Inspectable objects and libraries**
210
150
 
211
- ## Using k as a Library (Node.js)
151
+ k programs can be compiled into:
212
152
 
213
- ```js
214
- import k from "@fraczak/k"; // or "./index.mjs"
153
+ - `.klib`: plain JSON library objects
154
+ - `.ko`: executable binary object containers
215
155
 
216
- // compile + run
217
- const fn = k.compile(`
218
- $ bool = < {} true, {} false >;
219
- not = < /true {} |false $bool, /false {} |true $bool >
220
- `);
156
+ These keep canonical code/relation definitions, aliases, metadata, and
157
+ type-derivation status.
221
158
 
222
- console.log(fn({ "true": {} })); // { false: {} }
159
+ ## CLI Tour
223
160
 
224
- // annotate only (type-check without running)
225
- const annotated = k.annotate(source, {
226
- convergence: { strategy: "auto" } // "auto" | "single_pass" | "fixed_point"
227
- });
228
- console.log(annotated.compileStats); // per-SCC strategy and iteration counts
229
- ```
161
+ **Running k**
230
162
 
231
- ### Convergence strategies
163
+ | Command | Purpose |
164
+ | --- | --- |
165
+ | `k` | Execute a k expression, or `-k` source/object file, over a binary stream |
166
+ | `k-repl` | Start the interactive interpreter |
232
167
 
233
- | Strategy | Behaviour |
234
- |----------|-----------|
235
- | `auto` *(default)* | Single-pass for non-recursive SCCs; fixed-point for recursive ones |
236
- | `single_pass` | Force single-pass (fast, use for acyclic modules) |
237
- | `fixed_point` | Force the full convergence loop (useful for debugging) |
168
+ **Serialization boundaries**
238
169
 
239
- ---
170
+ | Command | Purpose |
171
+ | --- | --- |
172
+ | `k-parse` | Convert textual k values to binary pattern+value streams |
173
+ | `k-print` | Convert binary pattern+value streams back to textual values |
174
+ | `k-show` | Pass a stream through while showing the decoded value/filter |
240
175
 
241
- ## Standard Library — `core.k`
176
+ **Built-in codecs**
242
177
 
243
- `core.k` is the k standard core library. It is **not** pre-loaded into ordinary k
244
- programs or relation evaluation. Instead, it is the reference source for a few
245
- canonical definitions that the binary codec depends on, especially `$pattern`.
246
- It defines four things, in order:
178
+ | Command | Purpose |
179
+ | --- | --- |
180
+ | `k-json` | Convert JSON to/from the binary stream |
181
+ | `k-int` | Convert decimal integers to/from the binary stream |
182
+ | `k-ieee` | Convert float literals to/from the binary stream |
183
+ | `k-unit` | Produce or validate the unit value |
184
+ | `k-utf8` / `k-utf16` | Convert text to/from k string streams |
247
185
 
248
- ### §1 & §2 — `$bits` and arithmetic
186
+ **Object and library tooling**
249
187
 
250
- `$bits` is the canonical binary number type (LSB-first trie: empty `_`, extend with `0` or `1`).
251
- It is also used as node-index encoding inside pattern graphs.
188
+ | Command | Purpose |
189
+ | --- | --- |
190
+ | `k-compile` | Compile `.k` source to an executable `.ko` object |
191
+ | `k-compile-lib` | Compile `.k` source to a `.klib` library |
192
+ | `k-decompile` | Decompile `.ko` or `.klib` back to k source |
193
+ | `k-extract-aliases` | Recover metadata aliases as k source |
252
194
 
253
- ```k
254
- $ bits = < {} _, bits 0, bits 1 >;
255
- ```
195
+ Installed binary names are `k-` plus the source basename without `.mjs`, except
196
+ for `k.mjs` itself. Source names that already include `k-`, such as
197
+ `codecs/k-parse.mjs`, keep that name.
256
198
 
257
- Built-in functions: `inv`, `concat`, `succ`, `plus`, `times`, and integer constants `0`–`10`.
199
+ ## Node.js API
258
200
 
259
- ### §3 — `$unicode` and `$string`
201
+ ```js
202
+ import k from "@fraczak/k";
260
203
 
261
- Full Unicode scalar-value type, partitioned by plane and BMP range, built up from `$bit` and `$byte`.
262
- `$string` is a linked-list of `$unicode` values — used for field/tag label names in pattern graphs.
204
+ const fn = k.compile("{} |ok");
263
205
 
264
- ```k
265
- $ string = < {} nil, { unicode car, string cdr } cons >;
206
+ console.log(fn({})); // "ok"
266
207
  ```
267
208
 
268
- ### §4 — `$pattern`
209
+ For deeper inspection:
269
210
 
270
- The self-describing type of k pattern graphs. A pattern is a cons-list of `$pattern-node` values;
271
- node 0 is the root; edges carry `$string` labels and `$bits` target indices.
211
+ ```js
212
+ const annotated = k.annotate(source, {
213
+ convergence: { strategy: "auto" }
214
+ });
272
215
 
273
- ```k
274
- $ pattern-node = < {} any, edges open-product, edges open-union,
275
- edges closed-product, edges closed-union >;
276
- $ pattern = < {} nil, { pattern-node car, pattern cdr } cons >;
216
+ console.log(annotated.compileStats);
277
217
  ```
278
218
 
279
- The singleton pattern of `$pattern` itself is the fixed framing constant for the wire format:
280
- every k binary stream starts with a `$pattern` encoded under that constant,
281
- followed by the value encoded under the pattern just decoded.
282
- This is why `core.k` ends with a bare `$pattern` expression — it pins that
283
- canonical code/hash used by the wire codec for the leading pattern payload.
284
-
285
- ---
286
-
287
- ## Binary Codec Pipeline
288
-
289
- The `k` CLI reads and writes a binary stream: a serialised pattern followed by the value encoded under that pattern.
219
+ ## Project Status
290
220
 
291
- ```bash
292
- echo '{"cons":{"car":{"1":{}},"cdr":{"nil":{}}}}' \
293
- | k-parse \
294
- | k ./Examples/nat.k \
295
- | k-print
296
- ```
221
+ k is usable as an experimental language and toolkit, not a stable production
222
+ platform yet.
297
223
 
298
- See [`codecs/README.md`](codecs/README.md) for codec internals and format details.
224
+ Working today:
299
225
 
300
- For the textual boundary notation used by `k-parse` and `k-print`, see
301
- [DOCS/TEXTUAL_VALUES.md](DOCS/TEXTUAL_VALUES.md). For canonical exported
302
- patterns and `k-pattern`, see [DOCS/PATTERNS.md](DOCS/PATTERNS.md).
226
+ - parser and runtime for the core language
227
+ - type derivation over recursive algebraic data shapes
228
+ - REPL with aliases, loading, completion, `.klib` export, and `.ko` export
229
+ - binary pattern+value codec
230
+ - object and library files
231
+ - Node.js API
232
+ - regression tests for runtime, codecs, objects, hashes, and type derivation
303
233
 
304
- ## Object and Library Files
234
+ Still evolving:
305
235
 
306
- The object toolchain is documented in [objects/README.md](objects/README.md).
236
+ - surface syntax and diagnostics
237
+ - standard libraries
238
+ - documentation and tutorials
239
+ - object metadata format
240
+ - optimization and backend experiments
241
+ - larger real-world examples
307
242
 
308
- - `.klib` is plain JSON. It has `format: "k-object"`, `main: null`,
309
- `codes`, canonical hashed `rels`, `relAlias`, `compileStats`, and `meta`.
310
- - `.ko` is a binary `KOBJ\n` container around the same JSON payload shape, with
311
- `main: "__main__"` for the executable entry relation.
312
- - Relation `typeDerivation` stores only `status`.
313
- - Source locations live only in `meta[hash].origins[]` entries as `start` and
314
- `end`; definitions do not carry parser locations.
315
- - Relation definitions do not store generated input/output boundary filters.
243
+ ## Good Areas For Contributors
316
244
 
317
- Typical CLI usage:
245
+ k is small enough to study, but there are several useful directions:
318
246
 
319
- ```bash
320
- ./objects/compile-lib.mjs Examples/ieee.k ieee.klib
321
- ./objects/compile.mjs --lib ieee.klib Examples/nat.k nat.ko
322
- ./objects/extract-aliases.mjs ieee.klib aliases.k
323
- ./objects/decompile.mjs nat.ko
324
- ```
247
+ - examples: schema transformations, codecs, protocol examples, teaching tasks
248
+ - documentation: tutorials, diagrams, and clearer language walkthroughs
249
+ - tooling: formatter, editor integration, better diagnostics, REPL ergonomics
250
+ - compiler work: optimization, object inspection, backend experiments
251
+ - theory: normalization, equivalence, convergence, and pattern derivation
252
+ - applications: hardware modeling, asynchronous/synchronous test pipelines,
253
+ schema repositories, and data migration tooling
325
254
 
326
- ---
255
+ If you are interested in languages, compilers, data modeling, formal methods,
256
+ serialization, or teaching tools, there is room to shape the project.
327
257
 
328
258
  ## Examples
329
259
 
330
- The [`Examples/`](Examples/) directory contains ready-to-run `.k` scripts:
260
+ The [`Examples/`](Examples/) directory contains small language demonstrations:
331
261
 
332
262
  | File | Contents |
333
- |------|----------|
263
+ | --- | --- |
334
264
  | `nat.k` | Peano natural numbers |
335
- | `list.k` | Polymorphic lists |
336
- | `byte.k` | Byte type (8 bits) |
265
+ | `byte.k` | Byte type |
337
266
  | `ieee.k` | IEEE 754 floating-point layout |
338
267
  | `bnat.k` | Binary natural numbers |
339
- | `arithmetics.k` | Integer and rational arithmetic (binary encoding) |
268
+ | `arithmetics.k` | Integer and rational arithmetic built from scratch |
340
269
 
341
- ---
270
+ `list.k` is also useful as a focused demonstration of filters and patterns.
271
+
272
+ `ieee.k` is the largest example. It builds an IEEE-754 binary64 model from
273
+ bit-level types upward, including comparison and floating-point `add`, `sub`,
274
+ `mul`, and `div` relations. Those public aliases return `{ result, flags }`;
275
+ compose with `.result` when only the floating-point value is needed.
342
276
 
343
277
  ## Development
344
278
 
345
279
  ```bash
346
- npm run prepare # regenerate parsers from .jison grammars (after editing them)
347
- npm test # run full test suite
280
+ npm run prepare # regenerate parsers from .jison grammars
281
+ npm test # run the full suite
348
282
  ```
349
283
 
350
284
  The test suite covers:
351
285
 
352
- - `test.mjs` — core runtime and parser unit tests
353
- - `Code-derivation-tests/*.mjs` — type derivation suite
354
- - `test-fingerprint.mjs` — hash/fingerprint stability
355
- - `test-k-object.mjs` — object file round-trip
356
- - `test-repl2.mjs` — REPL scripted interaction
357
- - `tests.sh` — shell integration tests
358
-
359
- ---
360
-
361
- ## Quiz
362
-
363
- What does each of the following evaluate to?
364
-
365
- | Expression | Hint |
366
- |------------|------|
367
- | `()` | Empty composition |
368
- | `<>` | Empty merge |
369
- | `{}` | Empty product |
370
- | `\|s\|s\|s/s/s/s` | Tag and untag three times |
371
- | `({{{() a} b} c} .c .b .a)` | Nest and project |
372
-
373
- ---
374
-
375
- For more details see the [`DOCS/`](DOCS/) folder, and especially [`DOCS/book.md`](DOCS/book.md) for the language reference.
286
+ - core runtime and parser behavior
287
+ - type derivation cases in `Code-derivation-tests/`
288
+ - hash/fingerprint stability
289
+ - object file round-trips
290
+ - REPL scripted interaction
291
+ - shell integration tests
292
+
293
+ ## Further Reading
294
+
295
+ - [DOCS/REPL.md](DOCS/REPL.md) - interactive interpreter details
296
+ - [DOCS/TEXTUAL_VALUES.md](DOCS/TEXTUAL_VALUES.md) - textual boundary notation
297
+ - [DOCS/PATTERNS.md](DOCS/PATTERNS.md) - pattern representation
298
+ - [DOCS/OBJECT_FILE_AND_PATTERN.md](DOCS/OBJECT_FILE_AND_PATTERN.md) - object format
299
+ - [codecs/README.md](codecs/README.md) - binary codec internals
300
+ - [objects/README.md](objects/README.md) - object/library tools
301
+ - [DOCS/book.md](DOCS/book.md) - longer language reference
package/codecs/README.md CHANGED
@@ -82,6 +82,25 @@ notation for k values, not a separate JSON container format.
82
82
  `k.mjs` is only the command-line adapter for that boundary. The operational
83
83
  runtime sees and preserves the pattern on the `Value` itself.
84
84
 
85
+ ## Command-Line Tools
86
+
87
+ Installed codec binaries use the `k-` prefix followed by the source basename
88
+ without `.mjs`. Source names that already include `k-`, such as
89
+ `k-parse.mjs`, keep that name. Every installed codec binary supports `-h` and
90
+ `--help`.
91
+
92
+ | Binary | Source | Purpose |
93
+ | --- | --- | --- |
94
+ | `k-parse` | `k-parse.mjs` | Read a textual k value and write a binary pattern+value stream. Use `--input-type` or `--input-pattern` to supply the input envelope. |
95
+ | `k-print` | `k-print.mjs` | Read a binary pattern+value stream and write the decoded JSON-like value. Use `--debug` to include the decoded pattern. |
96
+ | `k-show` | `show.mjs` | Pass the wire stream through unchanged on stdout while printing the decoded value and filter to stderr. |
97
+ | `k-json` | `json.mjs` | Convert JSON to/from the binary stream with `--parse` or `--print`. |
98
+ | `k-int` | `int.mjs` | Convert decimal integers to/from the binary stream with `--parse` or `--print`. |
99
+ | `k-ieee` | `ieee.mjs` | Convert float literals to/from the binary stream with `--parse` or `--print`. |
100
+ | `k-unit` | `unit.mjs` | Write or validate the unit value `{}` with `--parse` or `--print`. |
101
+ | `k-utf8` | `utf8.mjs` | Convert UTF-8 text to/from a k string stream with `--parse` or `--print`. |
102
+ | `k-utf16` | `utf16.mjs` | Convert BOM-aware UTF-16 input or UTF-16LE output to/from a k string stream with `--parse` or `--print`. |
103
+
85
104
  ## Pattern Graph Representation
86
105
 
87
106
  For documentation and tests, a pattern graph may be shown as a property-list
@@ -124,8 +143,8 @@ Canonical rules:
124
143
  This JSON-like graph is only a readable notation. The wire representation is
125
144
  the ordinary k `$pattern` value from `core.k`.
126
145
 
127
- For user-facing pattern export, see [`../DOCS/PATTERNS.md`](../DOCS/PATTERNS.md)
128
- and the `k-pattern` CLI.
146
+ For pattern export, see [`../DOCS/PATTERNS.md`](../DOCS/PATTERNS.md) and the
147
+ repository helper `patterns/from-k.mjs`.
129
148
 
130
149
  ## Witness-Derived Patterns
131
150
 
@@ -16,6 +16,20 @@ import { encodeToWire, decodeWire } from './runtime/prefix-codec.mjs';
16
16
  import { patternToPropertyList } from './runtime/pattern-json.mjs';
17
17
  import { Product, Variant } from '../Value.mjs';
18
18
  import hash from '../hash.mjs';
19
+ import { argv, exit } from 'node:process';
20
+
21
+ function usage() {
22
+ console.log(`Usage: ${argv[1]}`);
23
+ console.log(" Run a self-contained demonstration of the codec pipeline.");
24
+ console.log("");
25
+ console.log("Options:");
26
+ console.log(" -h, --help Show this help.");
27
+ }
28
+
29
+ if (argv.slice(2).includes("-h") || argv.slice(2).includes("--help")) {
30
+ usage();
31
+ exit(0);
32
+ }
19
33
 
20
34
  console.log('=== K Codec Pipeline Example ===\n');
21
35