@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.
- package/.idea/codeStyles/codeStyleConfig.xml +5 -0
- package/.idea/copilot.data.migration.agent.xml +6 -0
- package/.idea/copilot.data.migration.ask.xml +6 -0
- package/.idea/copilot.data.migration.ask2agent.xml +6 -0
- package/.idea/copilot.data.migration.edit.xml +6 -0
- package/.idea/k.iml +9 -0
- package/.idea/misc.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/vcs.xml +6 -0
- package/AGENTS.md +2 -2
- package/CHANGELOG.md +22 -5
- package/CLAUDE.md +4 -4
- package/DOCS/BIG_PICTURE.md +5 -5
- package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +3 -3
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +9 -9
- package/DOCS/PATTERNS.md +6 -5
- package/DOCS/REPL.md +2 -2
- package/DOCS/book/06-values-in-memory.md +1 -1
- package/DOCS/book.md +20 -24
- package/README.md +200 -274
- package/codecs/README.md +21 -2
- package/codecs/example-pipeline.mjs +14 -0
- package/codecs/ieee.mjs +13 -3
- package/codecs/int.mjs +13 -3
- package/codecs/json.mjs +13 -3
- package/codecs/k-parse.mjs +5 -0
- package/codecs/k-print.mjs +16 -1
- package/codecs/show.mjs +15 -1
- package/codecs/unit.mjs +13 -3
- package/codecs/utf16.mjs +13 -3
- package/codecs/utf8.mjs +13 -3
- package/k-vscode/README.md +3 -2
- package/k.mjs +9 -1
- package/k_compiler/runtime.c +205 -0
- package/k_compiler/runtime.h +34 -0
- package/objects/README.md +11 -8
- package/package.json +13 -7
- package/{repl2.mjs → repl.mjs} +47 -9
- package/{test-repl2.mjs → test-repl.mjs} +16 -5
- package/.codex +0 -0
- package/DOCS/a.html +0 -398
- package/NAT.k~ +0 -452
- package/UU/ta.k +0 -1
- package/UU/ta.k~ +0 -0
- package/UU/tb.k +0 -0
- package/UU/tc.k +0 -0
- package/codecs/BINARY_FORMAT.html +0 -471
- package/codecs/POLYMORPHIC_BINARY_FORMAT.html +0 -355
- package/core.json +0 -1
- package/core.klib +0 -1
- package/core.klib~ +0 -0
- package/ieee.klib +0 -0
- package/k-vscode/package-lock.json +0 -4176
- package/kkk.k +0 -197
- package/kkk.klib +0 -0
- package/kkk.klib~ +0 -0
- package/p.json +0 -5475
- package/type_registry/registry.mjs~ +0 -105
- package/type_registry/typeParser.jison~ +0 -132
package/README.md
CHANGED
|
@@ -1,375 +1,301 @@
|
|
|
1
|
-
# k
|
|
1
|
+
# k
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**k is a small language for typed data transformations.**
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
|
|
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
|
-
|
|
15
|
+
## Why k?
|
|
14
16
|
|
|
15
|
-
|
|
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
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
|
|
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
|
-
|
|
36
|
+
## A Small Example
|
|
44
37
|
|
|
45
|
-
|
|
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
|
|
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
|
-
|
|
44
|
+
zero = {} |0;
|
|
45
|
+
inc = |+1;
|
|
65
46
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
47
|
+
add = ?X <
|
|
48
|
+
{ .x /+1 x, .y inc y } ?X add,
|
|
49
|
+
.y
|
|
50
|
+
>;
|
|
51
|
+
```
|
|
71
52
|
|
|
72
|
-
|
|
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
|
-
|
|
56
|
+
In the REPL:
|
|
82
57
|
|
|
83
58
|
```text
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
66
|
+
## Try It
|
|
101
67
|
|
|
102
|
-
|
|
68
|
+
From a checkout:
|
|
103
69
|
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
70
|
+
```bash
|
|
71
|
+
node --version # requires Node.js 18+
|
|
72
|
+
npm install
|
|
73
|
+
npm test
|
|
74
|
+
npm link
|
|
108
75
|
```
|
|
109
76
|
|
|
110
|
-
|
|
77
|
+
Then run a tiny binary pipeline:
|
|
111
78
|
|
|
112
|
-
|
|
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
|
-
|
|
122
|
-
|
|
123
|
-
`{f label1, g label2}` applies `f` and `g` in parallel and builds a record.
|
|
83
|
+
Expected output:
|
|
124
84
|
|
|
125
|
-
```
|
|
126
|
-
|
|
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
|
-
|
|
91
|
+
```bash
|
|
92
|
+
k-repl
|
|
93
|
+
```
|
|
143
94
|
|
|
144
|
-
|
|
95
|
+
Every installed command supports `-h` and `--help`.
|
|
145
96
|
|
|
146
|
-
|
|
97
|
+
## What k Gives You
|
|
147
98
|
|
|
148
|
-
|
|
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
|
-
|
|
101
|
+
Types, called *codes*, are built from products and tagged unions:
|
|
155
102
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
|
|
109
|
+
There are no built-in primitive values. The empty product `{}` is the only leaf
|
|
110
|
+
in a non-recursive definition.
|
|
173
111
|
|
|
174
|
-
|
|
175
|
-
state.
|
|
112
|
+
**Composable partial functions**
|
|
176
113
|
|
|
177
|
-
|
|
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
|
-
|
|
185
|
-
|
|
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
|
-
|
|
128
|
+
**Derived input/output patterns**
|
|
188
129
|
|
|
189
|
-
|
|
190
|
-
|
|
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
|
-
|
|
133
|
+
**Binary-friendly runtime values**
|
|
195
134
|
|
|
196
|
-
|
|
135
|
+
The command-line pipeline uses a self-describing binary stream:
|
|
197
136
|
|
|
198
|
-
|
|
137
|
+
```text
|
|
138
|
+
encoded pattern, followed by value encoded under that pattern
|
|
139
|
+
```
|
|
199
140
|
|
|
200
|
-
|
|
201
|
-
The type derivation engine infers patterns automatically and can optionally be guided with filter expressions:
|
|
141
|
+
The boundary tools are:
|
|
202
142
|
|
|
203
|
-
```
|
|
204
|
-
|
|
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
|
-
|
|
208
|
-
|
|
209
|
-
---
|
|
149
|
+
**Inspectable objects and libraries**
|
|
210
150
|
|
|
211
|
-
|
|
151
|
+
k programs can be compiled into:
|
|
212
152
|
|
|
213
|
-
|
|
214
|
-
|
|
153
|
+
- `.klib`: plain JSON library objects
|
|
154
|
+
- `.ko`: executable binary object containers
|
|
215
155
|
|
|
216
|
-
|
|
217
|
-
|
|
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
|
-
|
|
159
|
+
## CLI Tour
|
|
223
160
|
|
|
224
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
176
|
+
**Built-in codecs**
|
|
242
177
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
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
|
-
|
|
186
|
+
**Object and library tooling**
|
|
249
187
|
|
|
250
|
-
|
|
251
|
-
|
|
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
|
-
|
|
254
|
-
|
|
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
|
-
|
|
199
|
+
## Node.js API
|
|
258
200
|
|
|
259
|
-
|
|
201
|
+
```js
|
|
202
|
+
import k from "@fraczak/k";
|
|
260
203
|
|
|
261
|
-
|
|
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
|
-
|
|
265
|
-
$ string = < {} nil, { unicode car, string cdr } cons >;
|
|
206
|
+
console.log(fn({})); // "ok"
|
|
266
207
|
```
|
|
267
208
|
|
|
268
|
-
|
|
209
|
+
For deeper inspection:
|
|
269
210
|
|
|
270
|
-
|
|
271
|
-
|
|
211
|
+
```js
|
|
212
|
+
const annotated = k.annotate(source, {
|
|
213
|
+
convergence: { strategy: "auto" }
|
|
214
|
+
});
|
|
272
215
|
|
|
273
|
-
|
|
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
|
-
|
|
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
|
-
|
|
292
|
-
|
|
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
|
-
|
|
224
|
+
Working today:
|
|
299
225
|
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
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
|
-
|
|
234
|
+
Still evolving:
|
|
305
235
|
|
|
306
|
-
|
|
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
|
-
|
|
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
|
-
|
|
245
|
+
k is small enough to study, but there are several useful directions:
|
|
318
246
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
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
|
|
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
|
-
| `
|
|
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
|
|
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
|
|
347
|
-
npm test # run full
|
|
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
|
-
-
|
|
353
|
-
- `Code-derivation-tests
|
|
354
|
-
-
|
|
355
|
-
-
|
|
356
|
-
-
|
|
357
|
-
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
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
|
|
128
|
-
|
|
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
|
|