@fraczak/k 6.10.1 → 6.10.3
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 +39 -0
- package/DOCS/BIG_PICTURE.md +174 -100
- package/DOCS/CODECS.md +74 -29
- package/DOCS/CODEC_ARCHITECTURE.md +87 -31
- package/DOCS/CONVERGENCE.md +42 -11
- package/DOCS/DICTIONARY.md +69 -2
- package/DOCS/FILE_FORMATS.md +385 -0
- package/DOCS/KIR_V1.md +53 -42
- package/DOCS/KVM_EXECUTION_MODEL.md +189 -147
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +35 -17
- package/DOCS/PATTERNS.md +10 -6
- package/DOCS/REPL.md +100 -60
- package/DOCS/TEXTUAL_VALUES.md +24 -19
- package/DOCS/TYPE_DERIVATION.md +68 -22
- package/README.md +92 -58
- package/codecs/README.md +78 -67
- package/codecs/runtime/show-value.mjs +68 -16
- package/objects/README.md +57 -37
- package/package.json +1 -1
- package/tests/README.md +28 -17
- package/tests/test-repl-html.mjs +19 -0
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
# File Formats in the K Universe
|
|
2
|
+
|
|
3
|
+
The `k` toolchain operates across four primary file formats: `.k`,
|
|
4
|
+
`.ko`, `.klib`, and `.kvm`. Each format occupies a distinct position
|
|
5
|
+
in the pipeline from human-authored source to ahead-of-time (AOT)
|
|
6
|
+
type-checked artifacts and JIT/native execution.
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Overview & Comparison
|
|
11
|
+
|
|
12
|
+
| Extension | Role | Internal Storage | Entrypoint (`main`) | Generated By | Consumed By |
|
|
13
|
+
|:----------:|:-----------------------------------|:------------------------------------------------|:-------------------------------|:----------------------------------|:--------------------------------------------------------|
|
|
14
|
+
| **`.k`** | Human-readable source code | UTF-8 plain text | Optional (trailing expression) | Editor, `k-decompile` | `k.mjs`, `k-repl`, `k-compile`, backends |
|
|
15
|
+
| **`.ko`** | Executable compiled object | Binary (`KOBJ\n` + JSON payload) | **Required** (`"__main__"`) | `k-compile`, REPL (`:ko`) | `k.mjs`, `k-wasm-compile`, `k-llvm-build`, inspect tools |
|
|
16
|
+
| **`.klib`**| Reusable compiled library | UTF-8 JSON payload | **None** (`null`) | `k-compile --lib`, REPL (`:klib`) | REPL (`:load`), CLI linkers (`--lib`) |
|
|
17
|
+
| **`.kvm`** | Polymorphic kVM bytecode template | JSON / CBOR (`format: "k-vm", layer: "KVM-P"`) | Per-relation functions | `k-compile --format kvm`, kvm.mjs | kVM runtime, JIT specializer, `k-wasm-compile` |
|
|
18
|
+
|
|
19
|
+
### Format Transformation Workflow
|
|
20
|
+
|
|
21
|
+
```mermaid
|
|
22
|
+
flowchart TD
|
|
23
|
+
subgraph Sources ["Source & Interactive"]
|
|
24
|
+
K[".k (Source Code)"]
|
|
25
|
+
REPL["REPL Session"]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
subgraph AOT_Artifacts ["AOT Type-Checked Artifacts"]
|
|
29
|
+
KLIB[".klib (Library)"]
|
|
30
|
+
KO[".ko (Executable Object)"]
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
subgraph IR_Artifacts ["Register IR"]
|
|
34
|
+
KVM[".kvm (Polymorphic Template)"]
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
subgraph Targets ["Execution Targets"]
|
|
38
|
+
WASM[".wasm (WebAssembly)"]
|
|
39
|
+
NATIVE["Native Binary"]
|
|
40
|
+
INTERP["Interpreter (k.mjs / kvm.mjs)"]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
%% Transformations from .k
|
|
44
|
+
K -->|"k-compile"| KO
|
|
45
|
+
K -->|"k-compile --lib"| KLIB
|
|
46
|
+
K -->|"k-compile --format kvm"| KVM
|
|
47
|
+
K -->|":load"| REPL
|
|
48
|
+
|
|
49
|
+
%% REPL exports
|
|
50
|
+
REPL -->|":klib"| KLIB
|
|
51
|
+
REPL -->|":ko"| KO
|
|
52
|
+
|
|
53
|
+
%% Linking from .klib
|
|
54
|
+
KLIB -->|"k-compile --lib"| KO
|
|
55
|
+
KLIB -->|"k-compile --lib --format kvm"| KVM
|
|
56
|
+
KLIB -->|":load"| REPL
|
|
57
|
+
|
|
58
|
+
%% Lowering from .ko
|
|
59
|
+
KO -->|"k-wasm-compile"| WASM
|
|
60
|
+
KO -->|"k-llvm-build"| NATIVE
|
|
61
|
+
KO -->|"k.mjs"| INTERP
|
|
62
|
+
KO -->|"k-decompile"| K
|
|
63
|
+
|
|
64
|
+
%% Lowering from .kvm
|
|
65
|
+
KVM -->|"k-wasm-compile"| WASM
|
|
66
|
+
KVM -->|"kvm.mjs / specializeKVM"| INTERP
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## 1. `.k` — Source Code File
|
|
72
|
+
|
|
73
|
+
### What It Is
|
|
74
|
+
`.k` files contain the high-level, human-readable source code of the
|
|
75
|
+
`k` language. A `.k` file defines:
|
|
76
|
+
- **Algebraic Data Types:** Products (`{ field1 label1, field2 label2 }`)
|
|
77
|
+
and tagged unions (`< tag1 arm1, tag2 arm2 >`).
|
|
78
|
+
- **Relations:** First-order partial functions composed via sequence,
|
|
79
|
+
union, projection, or product construction.
|
|
80
|
+
- **Top-level Expressions:** Computations evaluated when the file is run
|
|
81
|
+
as a script.
|
|
82
|
+
|
|
83
|
+
### How to Generate
|
|
84
|
+
- Written manually in any text editor.
|
|
85
|
+
- Decompiled from an existing `.ko` or `.klib` artifact:
|
|
86
|
+
```bash
|
|
87
|
+
k-decompile program.ko program.decompiled.k
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### How to Consume
|
|
91
|
+
- **Run directly over stream input:**
|
|
92
|
+
```bash
|
|
93
|
+
./k.mjs program.k
|
|
94
|
+
```
|
|
95
|
+
- **Load into interactive REPL session:**
|
|
96
|
+
```k
|
|
97
|
+
:load Examples/arithmetics.k
|
|
98
|
+
```
|
|
99
|
+
- **Compile into compiled artifacts:**
|
|
100
|
+
```bash
|
|
101
|
+
# Compile to executable object (.ko)
|
|
102
|
+
k-compile program.k program.ko
|
|
103
|
+
|
|
104
|
+
# Compile to reusable library (.klib)
|
|
105
|
+
k-compile --lib program.k program.klib
|
|
106
|
+
|
|
107
|
+
# Compile to polymorphic kVM template (.kvm)
|
|
108
|
+
k-compile --format kvm program.k program.kvm
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## 2. `.ko` — Executable Object File
|
|
114
|
+
|
|
115
|
+
### What It Is
|
|
116
|
+
`.ko` is an ahead-of-time (AOT) type-checked, self-contained executable
|
|
117
|
+
object artifact (`format: "k-object"`, `kind: "executable"`).
|
|
118
|
+
|
|
119
|
+
In the `k` toolchain, "ahead-of-time" means that parsing, relation
|
|
120
|
+
expansion, structural type derivation, and constraint convergence are
|
|
121
|
+
completed beforehand during compilation. The resulting artifact stores
|
|
122
|
+
the fully converged principal type graphs and interned type hashes.
|
|
123
|
+
- **Physical Layout:** Prefixed by a 5-byte magic header `KOBJ\n`,
|
|
124
|
+
followed by a JSON payload.
|
|
125
|
+
- **Payload Contents:**
|
|
126
|
+
- `codes`: Type-code dictionary (interned structural type hashes).
|
|
127
|
+
- `rels`: Compiled relation ASTs and converged principal type graphs.
|
|
128
|
+
- `main`: Fixed entrypoint name (usually `"__main__"`).
|
|
129
|
+
- `meta`: Compiler origins and source position mapping.
|
|
130
|
+
- **Key Advantage:** Skips both parsing and type derivation. Running a
|
|
131
|
+
`.ko` executes immediately without cold-start compilation overhead.
|
|
132
|
+
|
|
133
|
+
### How to Generate
|
|
134
|
+
|
|
135
|
+
#### From the CLI (`k-compile`):
|
|
136
|
+
Compile a source file into an executable object:
|
|
137
|
+
```bash
|
|
138
|
+
k-compile program.k program.ko
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Compile an inline expression that links a library (`.klib` or `.k`):
|
|
142
|
+
```bash
|
|
143
|
+
# Note: Library relation aliases referenced by name in the expression
|
|
144
|
+
# must be explicitly mapped into scope via --export:
|
|
145
|
+
k-compile --lib Examples/arithmetics.k \
|
|
146
|
+
--export plus \
|
|
147
|
+
--export int \
|
|
148
|
+
--export 5 \
|
|
149
|
+
'{() x, 5 int y} plus' \
|
|
150
|
+
add5.ko
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
#### From the REPL (`:ko`):
|
|
154
|
+
Export the current session into an executable object with a custom
|
|
155
|
+
entrypoint expression:
|
|
156
|
+
```k
|
|
157
|
+
:load Examples/arithmetics.k
|
|
158
|
+
:ko add5.ko {() x, 5 int y} plus
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### How to Consume
|
|
162
|
+
|
|
163
|
+
- **Execute using the standard interpreter (`k.mjs`):**
|
|
164
|
+
```bash
|
|
165
|
+
echo "10" | ./codecs/int.mjs --parse | \
|
|
166
|
+
./k.mjs add5.ko | ./codecs/int.mjs --print
|
|
167
|
+
# Output: 15
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
- **Compile to a standalone WebAssembly binary (`k-wasm-compile`):**
|
|
171
|
+
```bash
|
|
172
|
+
k-wasm-compile add5.ko add5.wasm
|
|
173
|
+
echo "10" | ./codecs/int.mjs --parse | \
|
|
174
|
+
node backends/wasm/bin/k-wasm-run.mjs add5.wasm | \
|
|
175
|
+
./codecs/int.mjs --print
|
|
176
|
+
# Output: 15
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
- **Compile to a native machine executable via LLVM (`k-llvm-build`):**
|
|
180
|
+
```bash
|
|
181
|
+
k-llvm-build add5.ko -o add5
|
|
182
|
+
echo "10" | ./codecs/int.mjs --parse | ./add5 | ./codecs/int.mjs --print
|
|
183
|
+
# Output: 15
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
- **Inspect, validate, or decompile:**
|
|
187
|
+
```bash
|
|
188
|
+
k-inspect-object add5.ko
|
|
189
|
+
k-validate-object add5.ko
|
|
190
|
+
k-decompile add5.ko decompiled.k
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
---
|
|
194
|
+
|
|
195
|
+
## 3. `.klib` — Library Object File
|
|
196
|
+
|
|
197
|
+
### What It Is
|
|
198
|
+
`.klib` is an AOT type-checked library container (`format: "k-object"`,
|
|
199
|
+
`kind: "library"`).
|
|
200
|
+
- **Physical Layout:** Stored as plain UTF-8 JSON (no `KOBJ\n` header).
|
|
201
|
+
- **Payload Contents:**
|
|
202
|
+
- `codes`: Type-code dictionary.
|
|
203
|
+
- `rels`: Compiled and type-converged relations.
|
|
204
|
+
- `main: null`: Unlike `.ko`, there is no single execution entrypoint.
|
|
205
|
+
- `relAlias`: Public alias mapping of exported relations.
|
|
206
|
+
- `typeAliases`: Public alias mapping of exported types.
|
|
207
|
+
- **Key Advantage:** Pre-derives types across large relation suites
|
|
208
|
+
(such as `Examples/arithmetics.k` or `poly.k`), allowing downstream
|
|
209
|
+
programs to link them instantaneously without re-running type
|
|
210
|
+
convergence.
|
|
211
|
+
|
|
212
|
+
### How to Generate
|
|
213
|
+
|
|
214
|
+
#### From the CLI (`k-compile`):
|
|
215
|
+
Compile a source file into a library (the format is automatically
|
|
216
|
+
inferred from the `.klib` extension or `--lib`/`--format` flag):
|
|
217
|
+
```bash
|
|
218
|
+
k-compile Examples/arithmetics.k math.klib
|
|
219
|
+
# Or explicitly:
|
|
220
|
+
k-compile --lib Examples/arithmetics.k math.klib
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
#### From the REPL (`:klib`):
|
|
224
|
+
Export all currently loaded relations and types into a reusable library:
|
|
225
|
+
```k
|
|
226
|
+
:load Examples/arithmetics.k
|
|
227
|
+
:load Examples/poly.k
|
|
228
|
+
:klib my_lib.klib
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### How to Consume
|
|
232
|
+
|
|
233
|
+
- **Load into an interactive REPL session:**
|
|
234
|
+
```k
|
|
235
|
+
:load math.klib
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
- **Link as a dependency in downstream compilation (`--lib`):**
|
|
239
|
+
```bash
|
|
240
|
+
k-compile --lib math.klib \
|
|
241
|
+
--export plus \
|
|
242
|
+
--export int \
|
|
243
|
+
--export 5 \
|
|
244
|
+
'{() x, 5 int y} plus' \
|
|
245
|
+
add5.ko
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
- **Link into standalone WebAssembly or native LLVM builds:**
|
|
249
|
+
```bash
|
|
250
|
+
k-wasm-compile --lib math.klib \
|
|
251
|
+
--export plus --export int --export 5 \
|
|
252
|
+
'{() x, 5 int y} plus' add5.wasm
|
|
253
|
+
|
|
254
|
+
k-llvm-build --lib math.klib \
|
|
255
|
+
--export plus --export int --export 5 \
|
|
256
|
+
'{() x, 5 int y} plus' -o add5
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
- **Inspect contents or extract alias declarations:**
|
|
260
|
+
```bash
|
|
261
|
+
# View object metadata summary (relation counts, type codes):
|
|
262
|
+
k-inspect-object math.klib
|
|
263
|
+
|
|
264
|
+
# Extract active alias definitions as valid k source snippet:
|
|
265
|
+
k-extract-aliases math.klib math-aliases.k
|
|
266
|
+
|
|
267
|
+
# Validate structure and KIR-P export:
|
|
268
|
+
k-validate-object math.klib
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
## 4. `.kvm` — Polymorphic kVM Template File
|
|
274
|
+
|
|
275
|
+
### What It Is
|
|
276
|
+
`.kvm` is a polymorphic register-IR template artifact
|
|
277
|
+
(`format: "k-vm"`, `layer: "KVM-P"`, `isPolymorphic: true`).
|
|
278
|
+
|
|
279
|
+
#### How `.kvm` Differs from `.ko` and `.klib`:
|
|
280
|
+
- **Abstraction Level:** `.ko` and `.klib` store high-level relational
|
|
281
|
+
expressions (KIR) and structural type graphs. In contrast, `.kvm` is
|
|
282
|
+
lowered into explicit register-based virtual machine instructions
|
|
283
|
+
(`alloc`, `make_variant`, `project_variant`, `union`, `call`, and
|
|
284
|
+
tail loops).
|
|
285
|
+
- **Polymorphic Template vs. Fixed Object:**
|
|
286
|
+
- `.ko` is a concrete executable bound to an entrypoint (`main`).
|
|
287
|
+
- `.klib` is a library of high-level relation definitions for linking.
|
|
288
|
+
- `.kvm` stores compiled bytecode functions paired with principal
|
|
289
|
+
pattern graphs ($P_{in}, P_{out}$).
|
|
290
|
+
- **Instant Specialization:** Instead of running the compiler's full
|
|
291
|
+
constraint-derivation engine, the runtime specializer (`specializeKVM`)
|
|
292
|
+
prunes dead union branches and folds redundant guards on-the-fly
|
|
293
|
+
against concrete input envelopes $(v, P_v)$.
|
|
294
|
+
|
|
295
|
+
### How to Generate
|
|
296
|
+
|
|
297
|
+
#### From the CLI (`k-compile`):
|
|
298
|
+
Specify the `.kvm` extension or `--format kvm`:
|
|
299
|
+
```bash
|
|
300
|
+
# Compile directly from an expression linking a library:
|
|
301
|
+
k-compile --lib math.klib \
|
|
302
|
+
--export plus --export int --export 5 \
|
|
303
|
+
'{() x, 5 int y} plus' \
|
|
304
|
+
add5.kvm
|
|
305
|
+
|
|
306
|
+
# Or lower an existing .ko object directly into a .kvm template:
|
|
307
|
+
k-compile add5.ko add5.kvm
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### How to Consume
|
|
311
|
+
|
|
312
|
+
- **Direct lowering from `.kvm` to WebAssembly (`k-wasm-compile`):**
|
|
313
|
+
```bash
|
|
314
|
+
k-wasm-compile add5.kvm add5.wasm
|
|
315
|
+
echo "10" | ./codecs/int.mjs --parse | \
|
|
316
|
+
node backends/wasm/bin/k-wasm-run.mjs add5.wasm | \
|
|
317
|
+
./codecs/int.mjs --print
|
|
318
|
+
# Output: 15
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
- **Programmatic runtime specialization (`specializeKVM`):**
|
|
322
|
+
In JavaScript / Node.js host embeddings:
|
|
323
|
+
```javascript
|
|
324
|
+
import { specializeKVM, executeKVM } from "./kvm.mjs";
|
|
325
|
+
|
|
326
|
+
// Specialize polymorphic template to runtime input envelope (P, v):
|
|
327
|
+
const specialized = specializeKVM(kvmArtifact, inputEnvelope);
|
|
328
|
+
const result = executeKVM(specialized.func, value, context);
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
---
|
|
332
|
+
|
|
333
|
+
## Practical Rules & Common Gotchas
|
|
334
|
+
|
|
335
|
+
### 1. Library Aliases Require Explicit `--export`
|
|
336
|
+
In `k`, relations and types are identified canonically by their
|
|
337
|
+
content-addressed hashes (`@hash`). There is no global symbol namespace.
|
|
338
|
+
|
|
339
|
+
Human-readable identifiers (such as `plus`, `int`, or `5`) are *aliases*
|
|
340
|
+
stored in the object metadata (`relAlias`, `typeAliases`). When linking a
|
|
341
|
+
library via `--lib math.klib`, all compiled relations are loaded under
|
|
342
|
+
their canonical hashes, but their human-readable aliases are not
|
|
343
|
+
automatically imported into the consumer's local alias scope.
|
|
344
|
+
|
|
345
|
+
To use friendly names in an expression, explicitly map them into scope
|
|
346
|
+
using `--export`:
|
|
347
|
+
```bash
|
|
348
|
+
# Correct: imports 'plus', 'int', and '5' aliases into local scope:
|
|
349
|
+
k-compile --lib math.klib \
|
|
350
|
+
--export plus --export int --export 5 \
|
|
351
|
+
'{() x, 5 int y} plus' add5.ko
|
|
352
|
+
|
|
353
|
+
# Optional renaming syntax ('libAlias:localAlias'):
|
|
354
|
+
k-compile --lib math.klib \
|
|
355
|
+
--export plus:+ --export int --export 5 \
|
|
356
|
+
'{() x, 5 int y}+' add5.ko
|
|
357
|
+
```
|
|
358
|
+
You can also reference any library relation directly by its canonical
|
|
359
|
+
hash without `--export`.
|
|
360
|
+
|
|
361
|
+
### 2. No Built-in Numbers (Relations as Values)
|
|
362
|
+
In `k`, there are no primitive numerical types or built-in integer
|
|
363
|
+
literals. Identifiers such as `5` or `10` are not language keywords;
|
|
364
|
+
they are defined relations (aliases) in `Examples/arithmetics.k`:
|
|
365
|
+
- `5` and `10` are nullary relations (constants) of type `${} -> $bits`,
|
|
366
|
+
defined as binary digit trees over `$bits = < {} _, bits 0, bits 1 >`.
|
|
367
|
+
- Arithmetic operations (`plus`, `minus`, `times`, `inc`) operate on
|
|
368
|
+
signed integers (`$int = < bits "+", bits "-" >`).
|
|
369
|
+
- Passing a bit-representation like `5` to an integer relation requires
|
|
370
|
+
applying the `int` constructor (`5 int`), producing a constant
|
|
371
|
+
relation of type `${} -> $int`.
|
|
372
|
+
|
|
373
|
+
### 3. Constant Expressions vs. Stream Transformers
|
|
374
|
+
- **Stream Transformers:** An expression using the identity relation `()`
|
|
375
|
+
(such as `{() x, 5 int y} plus`) depends on external input. It reads
|
|
376
|
+
an enveloped value from `stdin` (here, a signed integer decoded via
|
|
377
|
+
`./codecs/int.mjs --parse`).
|
|
378
|
+
- **Closed Constant Relations:** A relation with no free inputs (such
|
|
379
|
+
as `{10 int x, 5 int y} plus`) is a constant relation with input
|
|
380
|
+
type of unit `${}`. When executed through `./k.mjs`, it expects
|
|
381
|
+
an empty unit `{}` on `stdin`:
|
|
382
|
+
```bash
|
|
383
|
+
echo "{}" | node codecs/k-parse.mjs | \
|
|
384
|
+
./k.mjs add_constants.ko | ./codecs/int.mjs --print
|
|
385
|
+
```
|
package/DOCS/KIR_V1.md
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# KIR v1
|
|
2
2
|
|
|
3
|
-
KIR v1 is the first backend-facing artifact contract for compiled k
|
|
4
|
-
is an inspection and export contract, not a replacement
|
|
5
|
-
and `.klib` containers.
|
|
3
|
+
KIR v1 is the first backend-facing artifact contract for compiled k
|
|
4
|
+
objects. It is an inspection and export contract, not a replacement
|
|
5
|
+
for the current `.ko` and `.klib` containers.
|
|
6
6
|
|
|
7
|
-
The first layer is **KIR-P**, the portable polymorphic relation format.
|
|
8
|
-
keeps the same pattern-carrying semantics as current object
|
|
9
|
-
normalizes names and graph IDs so backends can consume
|
|
10
|
-
depending on parser-shaped fields, current
|
|
11
|
-
`TypePatternGraph` internals.
|
|
7
|
+
The first layer is **KIR-P**, the portable polymorphic relation format.
|
|
8
|
+
KIR-P keeps the same pattern-carrying semantics as current object
|
|
9
|
+
execution, but normalizes names and graph IDs so backends can consume
|
|
10
|
+
object contents without depending on parser-shaped fields, current
|
|
11
|
+
object storage, or `TypePatternGraph` internals.
|
|
12
12
|
|
|
13
13
|
## Scope
|
|
14
14
|
|
|
@@ -19,7 +19,8 @@ KIR v1 defines:
|
|
|
19
19
|
- a closed expression opcode vocabulary;
|
|
20
20
|
- dense per-relation pattern graph IDs;
|
|
21
21
|
- relation type-derivation status;
|
|
22
|
-
- object aliases, metadata, compile statistics, and code table
|
|
22
|
+
- object aliases, metadata, compile statistics, and code table
|
|
23
|
+
pass-through.
|
|
23
24
|
|
|
24
25
|
KIR v1 does not define:
|
|
25
26
|
|
|
@@ -52,10 +53,10 @@ Rules:
|
|
|
52
53
|
|
|
53
54
|
- `kind` is `executable` when `main` names an entry relation.
|
|
54
55
|
- `kind` is `library` when `main` is `null`.
|
|
55
|
-
- `codes`, `relAlias`, `compileStats`, and `meta` preserve the current
|
|
56
|
-
payload content, sorted for stable inspection.
|
|
57
|
-
- KIR consumers must not infer backend eligibility from the top level
|
|
58
|
-
Each relation has its own `typeDerivation.status`.
|
|
56
|
+
- `codes`, `relAlias`, `compileStats`, and `meta` preserve the current
|
|
57
|
+
object payload content, sorted for stable inspection.
|
|
58
|
+
- KIR consumers must not infer backend eligibility from the top level
|
|
59
|
+
alone. Each relation has its own `typeDerivation.status`.
|
|
59
60
|
|
|
60
61
|
## Relations
|
|
61
62
|
|
|
@@ -80,11 +81,12 @@ Rules:
|
|
|
80
81
|
`patternGraph`.
|
|
81
82
|
- Valid type-derivation statuses are `converged`, `not-converged`, and
|
|
82
83
|
`unknown`.
|
|
83
|
-
- LLVM and optimized Wasm backends should reject relations whose status
|
|
84
|
-
`converged`, unless they intentionally implement an
|
|
85
|
-
mode.
|
|
86
|
-
- `sourceNodeMap` maps original object pattern graph representative IDs
|
|
87
|
-
node IDs. It exists for inspection and debugging, not as a
|
|
84
|
+
- LLVM and optimized Wasm backends should reject relations whose status
|
|
85
|
+
is not `converged`, unless they intentionally implement an
|
|
86
|
+
envelope-aware reference mode.
|
|
87
|
+
- `sourceNodeMap` maps original object pattern graph representative IDs
|
|
88
|
+
to KIR node IDs. It exists for inspection and debugging, not as a
|
|
89
|
+
semantic contract.
|
|
88
90
|
|
|
89
91
|
## Pattern Graphs
|
|
90
92
|
|
|
@@ -116,11 +118,11 @@ Type nodes carry a type-code hash:
|
|
|
116
118
|
Rules:
|
|
117
119
|
|
|
118
120
|
- Edge labels are sorted lexically.
|
|
119
|
-
- Parallel edges are represented as repeated edge records with the same
|
|
120
|
-
and different targets.
|
|
121
|
+
- Parallel edges are represented as repeated edge records with the same
|
|
122
|
+
label and different targets.
|
|
121
123
|
- Edge targets always name nodes in the same relation pattern graph.
|
|
122
|
-
- The graph is already union-find-normalized; KIR consumers should use
|
|
123
|
-
IDs directly.
|
|
124
|
+
- The graph is already union-find-normalized; KIR consumers should use
|
|
125
|
+
KIR node IDs directly.
|
|
124
126
|
|
|
125
127
|
## Expression Opcodes
|
|
126
128
|
|
|
@@ -145,8 +147,8 @@ Rules:
|
|
|
145
147
|
- `patterns` entries are KIR pattern graph node IDs.
|
|
146
148
|
- `comp.items` preserve composition order.
|
|
147
149
|
- `union.items` preserve source branch priority.
|
|
148
|
-
- `product.fields` preserve the relation body field list. Backends may
|
|
149
|
-
lay out fields later only after preserving observable semantics.
|
|
150
|
+
- `product.fields` preserve the relation body field list. Backends may
|
|
151
|
+
sort or lay out fields later only after preserving observable semantics.
|
|
150
152
|
- `filter.filter` currently preserves the stored filter tree. A later KIR
|
|
151
153
|
revision may normalize filter internals.
|
|
152
154
|
|
|
@@ -167,11 +169,11 @@ Inside a checkout:
|
|
|
167
169
|
```
|
|
168
170
|
|
|
169
171
|
This command decodes the current object format, hydrates relation pattern
|
|
170
|
-
graphs through the normal object loader, and emits the current KIR-P
|
|
171
|
-
encoding.
|
|
172
|
+
graphs through the normal object loader, and emits the current KIR-P
|
|
173
|
+
textual encoding.
|
|
172
174
|
|
|
173
|
-
Use the conformance runner to compare source and object execution and
|
|
174
|
-
KIR-P export for deterministic fixtures:
|
|
175
|
+
Use the conformance runner to compare source and object execution and
|
|
176
|
+
validate KIR-P export for deterministic fixtures:
|
|
175
177
|
|
|
176
178
|
```sh
|
|
177
179
|
npm run conformance
|
|
@@ -186,21 +188,23 @@ k source
|
|
|
186
188
|
-> .ko / .klib object
|
|
187
189
|
-> KIR-P
|
|
188
190
|
-> envelope-specialized KIR-P for an input envelope
|
|
189
|
-
-> KIR-M / kVM
|
|
191
|
+
-> KIR-M / kVM (.kvm)
|
|
190
192
|
-> LLVM / Wasm / C / other backend
|
|
191
193
|
```
|
|
192
194
|
|
|
193
|
-
KIR-P is the shared semantic object contract. Envelope Specialization
|
|
194
|
-
object whose entry relation is specialized by an
|
|
195
|
-
separate backend contract and should not
|
|
196
|
-
|
|
195
|
+
KIR-P is the shared semantic object contract. Envelope Specialization
|
|
196
|
+
produces another KIR-P object whose entry relation is specialized by an
|
|
197
|
+
input envelope. KIR-M remains a separate backend contract and should not
|
|
198
|
+
be encoded by overloading KIR-P fields. The current kVM lowerer consumes
|
|
199
|
+
KIR-P relation bodies directly.
|
|
197
200
|
|
|
198
201
|
## Envelope Specialization
|
|
199
202
|
|
|
200
203
|
The current API `retypeObjectRelation(object, relationName, inputPattern)`
|
|
201
|
-
exports envelope-specialized KIR-P. The `retype...` spelling is
|
|
202
|
-
implementation naming; new public names should use
|
|
203
|
-
pass re-runs existing type derivation as
|
|
204
|
+
exports envelope-specialized KIR-P. The `retype...` spelling is
|
|
205
|
+
legacy/current implementation naming; new public names should use
|
|
206
|
+
envelope specialization. The pass re-runs existing type derivation as
|
|
207
|
+
if the entry program were:
|
|
204
208
|
|
|
205
209
|
```k
|
|
206
210
|
?inputPattern relationName
|
|
@@ -211,11 +215,18 @@ The result is a normal KIR-P executable object:
|
|
|
211
215
|
- `layer: "KIR-P"`;
|
|
212
216
|
- `kind: "executable"`;
|
|
213
217
|
- `main` naming the specialized entry relation;
|
|
214
|
-
- relation `inputPattern` and `outputPattern` roots in the entry
|
|
215
|
-
`patternGraph`;
|
|
218
|
+
- relation `inputPattern` and `outputPattern` roots in the entry
|
|
219
|
+
relation's `patternGraph`;
|
|
216
220
|
- ordinary KIR relation bodies in `rels`.
|
|
217
221
|
|
|
218
|
-
This is the safe baseline: it preserves envelope-aware execution
|
|
219
|
-
making the specialized entry pattern explicit through
|
|
220
|
-
typing. Relation/input-pattern cache keys and
|
|
221
|
-
compiler data; they are not part of the
|
|
222
|
+
This is the safe baseline: it preserves envelope-aware execution
|
|
223
|
+
semantics while making the specialized entry pattern explicit through
|
|
224
|
+
ordinary KIR-P relation typing. Relation/input-pattern cache keys and
|
|
225
|
+
call-site worklists are local compiler data; they are not part of the
|
|
226
|
+
serialized KIR artifact.
|
|
227
|
+
|
|
228
|
+
- [DOCS/FILE_FORMATS.md](./FILE_FORMATS.md) —
|
|
229
|
+
guide to .k, .ko, .klib, and .kvm formats
|
|
230
|
+
- [DOCS/KVM_EXECUTION_MODEL.md](./KVM_EXECUTION_MODEL.md) —
|
|
231
|
+
kVM execution model and bytecode design
|
|
232
|
+
- [DOCS/DICTIONARY.md](./DICTIONARY.md) — concept names and terminology
|