@fraczak/k 6.0.3 → 6.1.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.
@@ -82,34 +82,58 @@ type still needs design work because the concrete input value is typed by the
82
82
  decoded pattern. The important boundary is that object files should carry
83
83
  ordinary k values and patterns, not a JSON codec object.
84
84
 
85
- Later versions can add relation IR, constant tables, symbol dictionaries,
86
- relocation/linking records, optimization metadata, and optional indexes.
85
+ The format can add relation IR, constant tables, symbol dictionaries,
86
+ relocation/linking records, optimization metadata, and optional indexes as the
87
+ implementation changes.
87
88
 
88
- ## Executable Object Container v1
89
+ ## Object And Library Containers
89
90
 
90
- The current executable object format is a small binary container:
91
+ The current executable `.ko` format is a small binary container:
91
92
 
92
93
  ```text
93
- "KOBJ" 0x00 0x01 0x0a
94
+ "KOBJ" 0x0a
94
95
  uint32-be payload-byte-length
95
96
  utf8-json-payload
96
97
  ```
97
98
 
99
+ Library files (`.klib`) are plain UTF-8 JSON with no binary header.
100
+
101
+ Both containers use the same JSON payload shape. Executable `.ko` files set
102
+ `main` to the entry relation name. Library `.klib` files set `main` to `null`.
103
+ There is no payload version field.
104
+
98
105
  The payload stores:
99
106
 
100
107
  - the normalized code repository needed by the object,
101
- - the compiled relation IR,
108
+ - the compiled relation IR without generated boundary filters,
102
109
  - each relation's type-pattern graph state,
110
+ - each relation's `typeDerivation.status`,
111
+ - relation aliases in `relAlias`,
112
+ - compiler convergence details in top-level `compileStats`,
113
+ - metadata, including aliases and origin source locations,
103
114
  - the main relation name.
104
115
 
116
+ Relation `def` values do not store source `start` / `end` markers. Source
117
+ locations live on `meta[hash].origins[]` entries. Each metadata entry has
118
+ `type: "code"` or `type: "rel"`. Origin entries hold `source`, `name`,
119
+ `compiledAt`, and optional `start` / `end`; origin entries do not have `kind`.
120
+
121
+ Relation `def` values also do not store the generated input/output filters.
122
+ Those filters are derived from `def.patterns` and `typePatternGraph` when
123
+ printing or enforcing relation boundaries.
124
+
125
+ Codes/types do not carry `typeDerivation`. Type derivation is a relation
126
+ property, and the persisted `typeDerivation` object currently contains only
127
+ `status`.
128
+
105
129
  Loading an object hydrates the type-pattern graphs back into runtime
106
- `TypePatternGraph` instances and executes them through `run.mjs`. This avoids
107
- source parsing and type derivation on the execution path.
130
+ `TypePatternGraph` instances and executes relation boundaries through
131
+ `run.mjs`. This avoids source parsing and type derivation on the execution path.
108
132
 
109
133
  Object execution through `k.mjs`:
110
134
 
111
135
  ```sh
112
- echo "..." | ./some-codec --parse | ./k.mjs -k path/to/program.ko | ./some-codec --print
136
+ echo "..." | some-codec --parse | k -k path/to/program.ko | some-codec --print
113
137
  ```
114
138
 
115
139
  The `-k` option accepts either an object file or a source `.k` file. `k.mjs`
@@ -119,15 +143,17 @@ compilation.
119
143
  Standalone helpers live in `objects/`:
120
144
 
121
145
  ```sh
122
- ./objects/compile.mjs path/to/program.k path/to/program.ko
123
- ./objects/decompile.mjs path/to/program.ko path/to/program.decompiled.k
146
+ k-compile path/to/program.k path/to/program.ko
147
+ k-compile-lib path/to/library.k path/to/library.klib
148
+ k-decompile path/to/program.ko path/to/program.decompiled.k
149
+ k-extract-aliases path/to/library.klib path/to/aliases.k
124
150
  ```
125
151
 
126
- With no arguments, both helpers read from stdin and write to stdout:
152
+ With no arguments, the helpers read from stdin and write to stdout:
127
153
 
128
154
  ```sh
129
- cat path/to/program.k | ./objects/compile.mjs > path/to/program.ko
130
- cat path/to/program.ko | ./objects/decompile.mjs > path/to/program.decompiled.k
155
+ cat path/to/program.k | k-compile > path/to/program.ko
156
+ cat path/to/program.ko | k-decompile > path/to/program.decompiled.k
131
157
  ```
132
158
 
133
159
  The decompiled source is canonical source regenerated from object IR. It uses
@@ -147,14 +173,14 @@ For a tiny source file:
147
173
  compile it with:
148
174
 
149
175
  ```sh
150
- ./objects/compile.mjs id.k id.ko
176
+ k-compile id.k id.ko
151
177
  ```
152
178
 
153
179
  The first bytes of `id.ko` are the binary container header:
154
180
 
155
181
  ```text
156
- 4b 4f 42 4a 00 01 0a 00 00 03 0b ...
157
- K O B J \0 v1 \n payload-length
182
+ 4b 4f 42 4a 0a 00 00 03 0b ...
183
+ K O B J \n payload-length
158
184
  ```
159
185
 
160
186
  After the header, the payload is UTF-8 JSON. Pretty-printed and shortened, the
@@ -163,59 +189,119 @@ payload has this shape:
163
189
  ```json
164
190
  {
165
191
  "format": "k-object",
166
- "version": 1,
167
- "codes": {
168
- "@NiDZqYggx3VZ6b8quBZKTfkgJztWctkesuX4CrhTxM5c": {
169
- "code": "product",
170
- "product": {},
171
- "def": "$C0={};"
192
+ "codes": {},
193
+ "main": "__main__",
194
+ "rels": {
195
+ "__main__": {
196
+ "def": { "op": "identity", "patterns": [0, 0] },
197
+ "typeDerivation": {
198
+ "status": "converged"
199
+ },
200
+ "typePatternGraph": {
201
+ "patterns": {
202
+ "nodes": [{ "pattern": "(...)", "fields": [] }],
203
+ "parent": []
204
+ },
205
+ "edges": [{}],
206
+ "codeId": {}
207
+ }
172
208
  }
173
209
  },
174
- "main": "__main__",
175
- "defs": {
176
- "rels": {
177
- "__main__": {
178
- "def": {
179
- "op": "filter",
180
- "filter": { "type": null, "open": true, "fields": {}, "name": "X0" },
181
- "patterns": [0, 0],
210
+ "relAlias": { "__main__": "@QfyLpmn56wuppuGvrrUJz8LKtgaXfbMtad7RnzBcFk2S" },
211
+ "compileStats": { "sccs": [...], "sccCount": 1 },
212
+ "meta": {
213
+ "@QfyLpmn56wuppuGvrrUJz8LKtgaXfbMtad7RnzBcFk2S": {
214
+ "type": "rel",
215
+ "origins": [
216
+ {
217
+ "source": "id.k",
218
+ "name": "__main__",
219
+ "compiledAt": "2026-05-20T00:00:00.000Z",
182
220
  "start": { "line": 1, "column": 1 },
183
221
  "end": { "line": 1, "column": 3 }
184
- },
185
- "typePatternGraph": {
186
- "patterns": {
187
- "nodes": [{ "pattern": "(...)", "fields": [] }],
188
- "parent": []
189
- },
190
- "edges": [{}],
191
- "codeId": {}
192
222
  }
193
- }
194
- },
195
- "representatives": {},
196
- "relAlias": { "__main__": "..." },
197
- "compileStats": { "sccs": [...], "sccCount": 1 }
223
+ ]
224
+ }
198
225
  }
199
226
  }
200
227
  ```
201
228
 
202
229
  The `codes` field is the type repository snapshot needed by this object. The
203
- `defs.rels.__main__.def` field is the executable relation IR. The
230
+ `rels.__main__.def` field is the executable relation body. The
204
231
  `typePatternGraph` field is the compiled pattern graph used by `run.mjs` for
205
- input-envelope refinement. Loading the object reconstructs live
232
+ relation-boundary refinement. Loading the object reconstructs live
206
233
  `TypePatternGraph` instances from this JSON state and then evaluates the input
207
- value directly against the compiled IR.
234
+ value against the compiled IR.
208
235
 
209
236
  Decompiling this object produces canonical source with generated prefix names:
210
237
 
211
238
  ```k
212
239
  ----- codes -----
213
- $ NiDZ = {};
214
240
  ----- rels -----
215
- QfyL = ?X0;
241
+ QfyL = ?(...) () ?(...);
216
242
  ----- main -----
217
243
  ?(...) QfyL ?(...)
218
244
  ```
219
245
 
220
246
  In the `rels` section, relation definitions are printed in the same SCC order
221
247
  recorded during compilation. Non-empty SCC groups are separated by a blank line.
248
+
249
+ ## Small Library Example
250
+
251
+ For a definitions-only source file:
252
+
253
+ ```k
254
+ -- defs.k
255
+ $ nat = <{} zero, nat succ>;
256
+ succ = |succ;
257
+ ```
258
+
259
+ compile it with:
260
+
261
+ ```sh
262
+ k-compile-lib defs.k defs.klib
263
+ ```
264
+
265
+ The output file is plain JSON and starts with `{`, not a binary prefix. Its
266
+ shape is the same object payload shape with `main: null`:
267
+
268
+ ```json
269
+ {
270
+ "format": "k-object",
271
+ "codes": {
272
+ "@...": {
273
+ "code": "union",
274
+ "union": { "zero": "@...", "succ": "@..." },
275
+ "def": "$C0=<C1\"succ\",C2\"zero\">;..."
276
+ }
277
+ },
278
+ "rels": {
279
+ "@...": {
280
+ "def": { "op": "vid", "vid": "succ", "patterns": [0, 1] },
281
+ "typeDerivation": { "status": "converged" },
282
+ "typePatternGraph": { "patterns": "...", "edges": "...", "codeId": "..." }
283
+ }
284
+ },
285
+ "relAlias": { "succ": "@..." },
286
+ "compileStats": { "sccs": [...], "sccCount": 1 },
287
+ "meta": {
288
+ "@...": {
289
+ "type": "rel",
290
+ "origins": [
291
+ {
292
+ "source": "defs.k",
293
+ "name": "succ",
294
+ "compiledAt": "2026-05-20T00:00:00.000Z",
295
+ "start": { "line": 3, "column": 8 },
296
+ "end": { "line": 3, "column": 13 }
297
+ }
298
+ ]
299
+ }
300
+ },
301
+ "main": null
302
+ }
303
+ ```
304
+
305
+ When a library is compiled with `--lib`, the output is the merged library
306
+ closure. Imported codes, relations, aliases, and metadata are preserved, and the
307
+ new source adds its own origins.
package/DOCS/PATTERNS.md CHANGED
@@ -66,9 +66,10 @@ list
66
66
 
67
67
  In both cases, the compiler derives a root pattern from `__main__`.
68
68
 
69
- ## `k-pattern`
69
+ ## Pattern Export Helper
70
70
 
71
- `k-pattern` extracts that root pattern from a k script.
71
+ `patterns/from-k.mjs` extracts that root pattern from a k script. It is a
72
+ repository helper, not an installed `package.json` binary.
72
73
 
73
74
  It:
74
75
 
@@ -84,7 +85,7 @@ It:
84
85
  Example:
85
86
 
86
87
  ```bash
87
- echo '?< {} nil, { $bits car, X cdr } cons > = X' | k-pattern
88
+ echo '?< {} nil, { $bits car, X cdr } cons > = X' | node ./patterns/from-k.mjs
88
89
  ```
89
90
 
90
91
  Typical output:
@@ -101,8 +102,8 @@ The active wire format is:
101
102
  encode($pattern_value : $pattern) encode(value : decoded_pattern)
102
103
  ```
103
104
 
104
- So the property-list array printed by `k-pattern` is a readable externalization
105
- of the same pattern information carried in the wire stream.
105
+ So the property-list array printed by `patterns/from-k.mjs` is a readable
106
+ externalization of the same pattern information carried in the wire stream.
106
107
 
107
108
  `k-parse --input-pattern ...` and `k-parse --input-type ...` both ultimately
108
109
  produce the same kind of root pattern used to interpret textual values.
package/DOCS/REPL.md CHANGED
@@ -1,13 +1,14 @@
1
1
  # k Interpreter
2
2
 
3
- `repl2.mjs` is the interactive k interpreter. `k-repl` and `k-repl2` both
4
- start it.
3
+ `repl.mjs` is the interactive k interpreter. After linking or installing the
4
+ package, start it with `k-repl`.
5
5
 
6
6
  The interpreter keeps a live `.klib`-style state in memory:
7
7
 
8
8
  - registered codes
9
9
  - compiled relations
10
10
  - human aliases for both
11
+ - metadata origins used to recover aliases
11
12
  - the current value flowing through the session
12
13
 
13
14
  Raw k snippets compile on top of that state. You can also export the current
@@ -142,15 +143,16 @@ the file unless `--no-alias` is used.
142
143
 
143
144
  ### `:load [--no-alias] file.klib`
144
145
 
145
- Decodes the library file and merges its codes, relations, and metadata into the
146
- session. Aliases are recovered from library metadata unless `--no-alias` is
147
- used.
146
+ Reads the plain-JSON library file and merges its codes, relations, aliases, and
147
+ metadata into the session. Aliases are recovered from `meta[hash].origins[]`
148
+ unless `--no-alias` is used.
148
149
 
149
150
  ## Export
150
151
 
151
152
  ### `:klib file`
152
153
 
153
- Writes the current interpreter state as a `.klib` library.
154
+ Writes the current interpreter state as a plain-JSON `.klib` library. The
155
+ library has `main: null`; it has no binary header and no object payload version.
154
156
 
155
157
  ### `:ko file expr`
156
158
 
@@ -113,7 +113,7 @@ Each node in the value tree is encoded based on its automaton state:
113
113
 
114
114
  For type `bnat`, intuitively binary natural numbers defined by `$bnat = < bnat 0, bnat 1, {} _ >`, we get
115
115
 
116
- ```k-repl
116
+ ```text
117
117
  > :C bnat
118
118
  $ bnat = < @... 0, @... 1, {} _ >; -- @...
119
119
  ```
package/DOCS/book.md CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
- title: "k-language"
2
+ title: "k"
3
3
  author: "W. Fraczak"
4
4
  ---
5
5
 
@@ -15,32 +15,28 @@ author: "W. Fraczak"
15
15
 
16
16
  </div>
17
17
 
18
- # The k-language Book
19
-
20
- This book describes the k programming language - a minimal functional language for transforming tree-structured data using partial functions and algebraic data types.
21
-
22
- ## Table of Contents
23
-
24
- 1. [Introduction](book/01-introduction.md)
25
- 2. [Syntax and Values](book/02-syntax-and-values.md)
26
- 3. [Types as Finite Automata](book/03-types-as-finite-automata.md)
27
- 4. [Partial Functions and Composition](book/04-partial-functions-and-composition.md)
28
- 5. [Typing, Filters, and Normalization](book/05-typing-filters-and-normalization.md)
29
- 6. [Values in Memory](book/06-values-in-memory.md)
30
- 7. [The Partial Function ABI](book/07-the-partial-function-abi.md)
31
- 8. [Operational Semantics and Execution](book/08-operational-semantics-and-execution.md)
32
- 9. [Compiler Architecture](book/09-compiler-architecture.md)
33
- 10. [From AST to Intermediate Representation](book/10-from-ast-to-intermediate-representation.md)
34
- 11. [LLVM Basics](book/11-llvm-basics.md)
35
- 12. [Code Generation](book/12-code-generation.md)
36
- 13. [Linking and Execution](book/13-linking-and-execution.md)
37
- 14. [Canonical Serialization](book/14-canonical-serialization.md)
38
- 15. [Optimization and Folding](book/15-optimization-and-folding.md)
39
- 16. [Toward a Universal Schema Registry](book/16-toward-a-universal-schema-registry.md)
18
+ # The k Book
19
+
20
+ This book describes the k programming language: a minimal functional language
21
+ for transforming tree-structured data using partial functions and algebraic
22
+ data types.
23
+
24
+ ## Available Chapters
25
+
26
+ - [Introduction](book/01-introduction.md)
27
+ - [Syntax and Values](book/02-syntax-and-values.md)
28
+ - [Types as Finite Automata](book/03-types-as-finite-automata.md)
29
+ - [Partial Functions and Composition](book/04-partial-functions-and-composition.md)
30
+ - [Typing, Filters, and Normalization](book/05-typing-filters-and-normalization.md)
31
+ - [Values in Memory](book/06-values-in-memory.md)
32
+ - [The Partial Function ABI](book/07-the-partial-function-abi.md)
33
+ - [Operational Semantics and Execution](book/08-operational-semantics-and-execution.md)
34
+ - [Compiler Architecture](book/09-compiler-architecture.md)
35
+ - [From AST to Intermediate Representation](book/10-from-ast-to-intermediate-representation.md)
36
+ - [Canonical Serialization](book/14-canonical-serialization.md)
40
37
 
41
38
  ## Appendices
42
39
 
43
- - [Appendix A: Implementing a Prototype Compiler in Python](book/appendix-a-implementing-a-prototype-compiler-in-python.md)
44
40
  - [Appendix B: Incorporating External Predefined Types and Functions](book/appendix-b-incorporating-external-predefined-types-and-functions.md)
45
41
 
46
42
  ---