@fraczak/k 8.4.1 → 8.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/DOCS/ARCHITECTURE.md +83 -0
- package/DOCS/CODECS.md +93 -204
- package/DOCS/DICTIONARY.md +42 -1241
- package/DOCS/FILE_FORMATS.md +170 -625
- package/DOCS/GETTING_STARTED.md +164 -0
- package/DOCS/KIR_V1.md +22 -33
- package/DOCS/KVM_EXECUTION_MODEL.md +88 -481
- package/DOCS/PATTERNS.md +155 -138
- package/DOCS/POLYMORPHIC_IR.md +83 -101
- package/DOCS/PROFILING_AND_TRACING.md +52 -166
- package/DOCS/README.md +35 -0
- package/DOCS/REPL.md +100 -377
- package/DOCS/TEXTUAL_VALUES.md +53 -81
- package/DOCS/TYPE_DERIVATION.md +95 -234
- package/DOCS/book-combined.md +3786 -0
- package/Examples/arithmetics.k +45 -59
- package/Examples/djson.k +102 -112
- package/Examples/karatsuba-mult.k +2 -3
- package/Examples/list.k +6 -7
- package/Examples/nat.k +6 -6
- package/Examples/poly.k +45 -45
- package/LICENSE +15 -0
- package/README.md +72 -410
- package/backends/llvm/README.md +75 -97
- package/backends/wasm/README.md +88 -66
- package/canonical-pattern.mjs +7 -3
- package/codecs/README.md +23 -261
- package/codecs/WIRE_FORMAT.md +132 -0
- package/core.k +5 -15
- package/named-patterns.mjs +19 -7
- package/objects/README.md +24 -109
- package/package.json +8 -3
- package/paper/Makefile +17 -0
- package/paper/k-paper.pdf +0 -0
- package/paper/k-paper.tex +435 -0
- package/parser.jison +0 -10
- package/parser.mjs +32 -43
- package/repl.mjs +74 -29
- package/scripts/benchmark-idempotency.mjs +3 -3
- package/scripts/run-tests.mjs +1 -1
- package/scripts/test-package.mjs +64 -0
- package/tests/README.md +66 -46
- package/tests/code-derivation/core.mjs +61 -0
- package/tests/helpers/cli.mjs +45 -0
- package/tests/integration.sh +2 -3
- package/tests/test-arithmetics.mjs +47 -237
- package/tests/test-djson-methods.mjs +71 -275
- package/tests/test-hash-normalization.mjs +22 -1
- package/tests/test-karatsuba.mjs +18 -126
- package/tests/test-named-patterns.mjs +174 -621
- package/tests/test-paper-examples.mjs +90 -0
- package/tests/test-poly.mjs +2 -2
- package/tests/test-repl-html.mjs +2 -2
- package/tests/test-repl.mjs +49 -41
- package/tests/test.mjs +27 -68
- package/AGENTS.md +0 -35
- package/CLAUDE.md +0 -64
- package/DOCS/BIG_PICTURE.md +0 -439
- package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +0 -74
- package/DOCS/CANONICAL_PATTERNS.md +0 -362
- package/DOCS/CODEC_ARCHITECTURE.md +0 -257
- package/DOCS/CONVERGENCE.md +0 -123
- package/DOCS/DIVERGENCE_DETECTION.md +0 -23
- package/DOCS/JSON_APPROACHES_COMPARISON.md +0 -302
- package/DOCS/KIR_SPECIALIZATION_DESIGN.md +0 -472
- package/DOCS/Makefile +0 -99
- package/DOCS/NAMED_PATTERNS.md +0 -361
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +0 -343
- package/DOCS/PATTERNS_AND_TYPED_VALUES.md +0 -665
- package/DOCS/WEB_REPL_REQUIREMENTS.md +0 -134
- package/DOCS/book/01-introduction.md +0 -48
- package/DOCS/book/02-syntax-and-values.md +0 -110
- package/DOCS/book/03-types-as-finite-automata.md +0 -155
- package/DOCS/book/04-partial-functions-and-composition.md +0 -145
- package/DOCS/book/05-typing-filters-and-normalization.md +0 -109
- package/DOCS/book/06-values-in-memory.md +0 -1085
- package/DOCS/book/07-the-partial-function-abi.md +0 -143
- package/DOCS/book/08-operational-semantics-and-execution.md +0 -196
- package/DOCS/book/09-compiler-architecture.md +0 -160
- package/DOCS/book/10-from-ast-to-intermediate-representation.md +0 -166
- package/DOCS/book/14-canonical-serialization.md +0 -176
- package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +0 -156
- package/DOCS/book/overview.txt +0 -68
- package/DOCS/book.md +0 -50
- package/Examples/bnat.k +0 -308
- package/Examples/buda.k +0 -66
- package/Examples/byte.k +0 -35
- package/Examples/code.k +0 -43
- package/Examples/vhdl_to_k.k +0 -67
- package/JOURNAL.md +0 -288
- package/TODO.md +0 -107
- package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +0 -233
- package/backends/wasm/TECHNICAL-DEBT.md +0 -58
- package/backends/wasm/demo_ieee.mjs +0 -318
- package/browser/vfs-data.mjs +0 -21
- package/codecs/BINARY_FORMAT.md +0 -53
- package/codecs/POLYMORPHIC_BINARY_FORMAT.md +0 -303
- package/codecs/pandoc.css +0 -71
- package/tests/code-derivation/blowup.mjs +0 -25
- package/tests/code-derivation/bnat.mjs +0 -11
- package/tests/code-derivation/dot.mjs +0 -37
- package/tests/code-derivation/index.mjs +0 -20
- package/tests/code-derivation/loop.mjs +0 -17
- package/tests/code-derivation/nat.mjs +0 -10
- package/tests/code-derivation/not-not.mjs +0 -14
- package/tests/code-derivation/product.mjs +0 -22
- package/tests/code-derivation/rlz.mjs +0 -13
- package/tests/test-fingerprint.mjs +0 -38
|
@@ -1,492 +1,99 @@
|
|
|
1
|
-
# kVM
|
|
1
|
+
# kVM execution
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
[kvm.mjs](../kvm.mjs) lowers KIR-P into register instructions, interprets them,
|
|
4
|
+
and specializes polymorphic templates for input envelopes. Wasm and LLVM use
|
|
5
|
+
this representation for target lowering.
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
is an execution contract: a small value model, a small instruction set,
|
|
9
|
-
and precise rules for partial failure, products, unions, calls, and safe
|
|
10
|
-
scheduling.
|
|
7
|
+
## Artifacts and functions
|
|
11
8
|
|
|
12
|
-
|
|
9
|
+
Artifacts have `format: "k-vm"` and `version: 1`:
|
|
13
10
|
|
|
14
|
-
|
|
11
|
+
- `layer: "KVM-P"` is a polymorphic template containing principal contracts
|
|
12
|
+
and instruction streams.
|
|
13
|
+
- `layer: "KVM"` is a specialized execution artifact.
|
|
15
14
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
- Envelope-Specialized kVM: kVM functions instantiated for a concrete
|
|
20
|
-
input envelope, with dead branches pruned and redundant guards
|
|
21
|
-
eliminated.
|
|
22
|
-
- Target Lowering: direct emission from specialized kVM to WebAssembly
|
|
23
|
-
or native machine code.
|
|
15
|
+
`objectToKVMArtifact` produces the artifact form.
|
|
16
|
+
`lowerKIRRelationToKVM` lowers one portable relation.
|
|
17
|
+
`lowerToKVM` supports the compiled relation interface.
|
|
24
18
|
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
Functions record their name, input/output patterns, convergence flag, and
|
|
20
|
+
instruction body. Calls use relation names/hashes in the artifact's function
|
|
21
|
+
table. Function and branch bodies receive their input through `%in`;
|
|
22
|
+
temporary registers use names such as `%v0`.
|
|
27
23
|
|
|
28
|
-
|
|
29
|
-
[AOT Compilation Phase]
|
|
30
|
-
k source
|
|
31
|
-
-> AST
|
|
32
|
-
-> principal type derivation
|
|
33
|
-
-> polymorphic KIR-P object relations
|
|
34
|
-
-> polymorphic kVM template (.kvm)
|
|
24
|
+
See [file formats](FILE_FORMATS.md) for CLI compilation and execution.
|
|
35
25
|
|
|
36
|
-
|
|
37
|
-
kVM template + input envelope Pv
|
|
38
|
-
-> boundary type compatibility check (validates Pv against Pin;
|
|
39
|
-
raises Type Error on contradiction)
|
|
40
|
-
-> fast kVM specialization (specializeKVM: guard folding, dead
|
|
41
|
-
branch pruning, output pattern derivation)
|
|
42
|
-
-> specialized kVM function
|
|
43
|
-
-> Tier 0: direct interpretation in kVM runtime (< 1 ms cold start)
|
|
44
|
-
-> Tier 1: single-pass lowering to WebAssembly (kvm2wasm) or native
|
|
45
|
-
machine code (~17 ms peak speed)
|
|
46
|
-
```
|
|
26
|
+
## Instructions
|
|
47
27
|
|
|
48
|
-
|
|
49
|
-
compiles KIR-P into polymorphic kVM templates ahead of time. When
|
|
50
|
-
concrete input envelopes arrive at runtime, `specializeKVM` instantiates
|
|
51
|
-
and optimizes the kVM instruction stream in microseconds.
|
|
28
|
+
The current lowerer emits:
|
|
52
29
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
Every `k` relation is a pure first-order partial function:
|
|
56
|
-
|
|
57
|
-
```text
|
|
58
|
-
KRef -> KResult<KRef>
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
where:
|
|
62
|
-
|
|
63
|
-
- `KRef` is a reference to an immutable runtime value;
|
|
64
|
-
- `KResult` is either `ok(value)` or `fail`;
|
|
65
|
-
- failure is ordinary partial-function undefinedness, not an exception;
|
|
66
|
-
- user-level computation has no mutable environment and no observable
|
|
67
|
-
effects.
|
|
68
|
-
|
|
69
|
-
This purity is central. It means kVM can expose parallel product and
|
|
70
|
-
speculative union execution while preserving exactly the same
|
|
71
|
-
deterministic semantics as the interpreter.
|
|
72
|
-
|
|
73
|
-
Current debug or host operations such as `_log!` do not fit the pure
|
|
74
|
-
backend contract. They should either be excluded from kVM backend
|
|
75
|
-
eligibility or marked as effectful intrinsics that create ordering
|
|
76
|
-
barriers.
|
|
77
|
-
|
|
78
|
-
## Value Model
|
|
79
|
-
|
|
80
|
-
A kVM value reference should be more abstract than an LLVM pointer but
|
|
81
|
-
more explicit than the current JavaScript `Value` object.
|
|
82
|
-
|
|
83
|
-
```text
|
|
84
|
-
KRef {
|
|
85
|
-
repr: serialized | materialized | external,
|
|
86
|
-
addr: backend-specific handle,
|
|
87
|
-
pattern: optional runtime pattern,
|
|
88
|
-
staticPattern: optional kVM pattern id,
|
|
89
|
-
layout: optional layout id,
|
|
90
|
-
ownership: borrowed | owned
|
|
91
|
-
}
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
The important property is immutability. A `KRef` may point into the
|
|
95
|
-
original binary input stream, a materialized node, a constructed result,
|
|
96
|
-
or an external host value. Projections can often produce new `KRef`s
|
|
97
|
-
without allocation. Construction creates new immutable values.
|
|
98
|
-
|
|
99
|
-
There are two valid execution modes:
|
|
100
|
-
|
|
101
|
-
- **Envelope-aware mode**: each `KRef` may carry a runtime pattern,
|
|
102
|
-
matching the current interpreter and codec model.
|
|
103
|
-
- **Envelope-free mode**: envelope specialization has proven the input
|
|
104
|
-
and output patterns for every call site, so inner operations use
|
|
105
|
-
static layouts and attach the derived output pattern only at the
|
|
106
|
-
boundary.
|
|
107
|
-
|
|
108
|
-
The first mode is the reference path. The second mode is the main
|
|
109
|
-
LLVM/Wasm performance path.
|
|
110
|
-
|
|
111
|
-
## Function Shape
|
|
112
|
-
|
|
113
|
-
A kVM relation instance is specialized by relation hash plus input
|
|
114
|
-
pattern hash:
|
|
115
|
-
|
|
116
|
-
```text
|
|
117
|
-
fn @rel_hash__input_pattern_hash(%input: KRef) -> KResult<KRef>
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
Recursive relations compile to mutually recursive kVM functions. A
|
|
121
|
-
single source relation may produce several kVM functions when called
|
|
122
|
-
under different input patterns.
|
|
123
|
-
|
|
124
|
-
Each kVM function records:
|
|
125
|
-
|
|
126
|
-
- relation hash;
|
|
127
|
-
- input pattern id;
|
|
128
|
-
- output pattern id;
|
|
129
|
-
- convergence status;
|
|
130
|
-
- layout assumptions;
|
|
131
|
-
- called relation instances;
|
|
132
|
-
- required intrinsics.
|
|
133
|
-
|
|
134
|
-
LLVM and Wasm backends should reject functions whose relation derivation
|
|
135
|
-
is not `converged`, unless they intentionally target the envelope-aware
|
|
136
|
-
reference mode.
|
|
137
|
-
|
|
138
|
-
## Core Instructions
|
|
139
|
-
|
|
140
|
-
kVM should be register-based and block-oriented. Backends can lower it
|
|
141
|
-
to LLVM SSA, Wasm locals and blocks, C temporaries, or a JS interpreter.
|
|
142
|
-
|
|
143
|
-
Primitive instructions:
|
|
144
|
-
|
|
145
|
-
```text
|
|
146
|
-
id %v -> ok(%v)
|
|
147
|
-
fail -> fail
|
|
148
|
-
guard_pattern %v, @pattern -> ok(%v) | fail
|
|
149
|
-
guard_code %v, @code -> ok(%v) | fail
|
|
150
|
-
project_field %v, label_id, layout_hint -> ok(%child) | fail
|
|
151
|
-
project_variant %v, tag_id, layout_hint -> ok(%child) | fail
|
|
152
|
-
make_product [(label_id, %value), ...] -> %product
|
|
153
|
-
make_variant tag_id, %value -> %variant
|
|
154
|
-
call @function, %value -> ok(%result) | fail
|
|
155
|
-
call_intrinsic @symbol, %value -> ok(%result) | fail
|
|
156
|
-
return %value -> ok(%value)
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
Composition is ordinary control flow: run the next instruction only
|
|
160
|
-
after the previous one has returned `ok`.
|
|
161
|
-
|
|
162
|
-
Filters lower either to `guard_pattern` or disappear after envelope
|
|
163
|
-
specialization proves that the guard is redundant. Type/code
|
|
164
|
-
expressions lower to guards. Variant introduction lowers to
|
|
165
|
-
`make_variant`. Product construction lowers to a product region plus
|
|
166
|
-
`make_product`.
|
|
167
|
-
|
|
168
|
-
## Structured Regions
|
|
169
|
-
|
|
170
|
-
The two important high-level operations should survive lowering into kVM
|
|
171
|
-
as structured regions. They carry semantic information that is easy to
|
|
172
|
-
lose if everything is flattened too early.
|
|
173
|
-
|
|
174
|
-
### Product Region
|
|
175
|
-
|
|
176
|
-
A product expression applies several functions to the same input and
|
|
177
|
-
succeeds only if all of them succeed.
|
|
178
|
-
|
|
179
|
-
```text
|
|
180
|
-
%p = product %input [
|
|
181
|
-
field a: { ... -> ok(%a) | fail }
|
|
182
|
-
field b: { ... -> ok(%b) | fail }
|
|
183
|
-
] join all_success
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
Semantics:
|
|
187
|
-
|
|
188
|
-
1. Every field branch receives the same input `KRef`.
|
|
189
|
-
2. If every branch succeeds, the join constructs a product in canonical
|
|
190
|
-
field order.
|
|
191
|
-
3. If any branch fails, the whole product fails.
|
|
192
|
-
4. For terminating pure branches, branch evaluation order is not
|
|
193
|
-
observable.
|
|
194
|
-
|
|
195
|
-
Scheduling:
|
|
196
|
-
|
|
197
|
-
- A sequential backend may run branches left to right.
|
|
198
|
-
- A parallel backend may fork all branches.
|
|
199
|
-
- A result-oriented runtime may fail fast and cancel unfinished branches
|
|
200
|
-
after any failure, because failure has no observable payload in
|
|
201
|
-
release semantics.
|
|
202
|
-
- A strict operational runtime should preserve the chosen product
|
|
203
|
-
evaluation order for divergence-sensitive conformance. It may still
|
|
204
|
-
run later branches speculatively, but it should not expose a later
|
|
205
|
-
failure while an earlier branch is still pending.
|
|
206
|
-
- A debug runtime may optionally wait for all branches to report better
|
|
207
|
-
diagnostics.
|
|
208
|
-
|
|
209
|
-
This is the direct kVM form of parallel composition.
|
|
210
|
-
|
|
211
|
-
### Union Region
|
|
212
|
-
|
|
213
|
-
A union expression tries several functions and returns the first
|
|
214
|
-
successful branch in source order.
|
|
215
|
-
|
|
216
|
-
```text
|
|
217
|
-
%u = union %input [
|
|
218
|
-
case 0: { ... -> ok(%r0) | fail }
|
|
219
|
-
case 1: { ... -> ok(%r1) | fail }
|
|
220
|
-
case 2: { ... -> ok(%r2) | fail }
|
|
221
|
-
] join first_success_by_index
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
Semantics:
|
|
225
|
-
|
|
226
|
-
1. Every branch receives the same input `KRef`.
|
|
227
|
-
2. If no branch succeeds, the union fails.
|
|
228
|
-
3. If one or more branches succeed, the result is the successful branch
|
|
229
|
-
with the lowest index.
|
|
230
|
-
|
|
231
|
-
Scheduling:
|
|
232
|
-
|
|
233
|
-
- A sequential backend may run branches in source order and stop at the
|
|
234
|
-
first success.
|
|
235
|
-
- A parallel backend may speculatively run branches concurrently.
|
|
236
|
-
- A parallel backend may return branch `j` only after branch `j` has
|
|
237
|
-
succeeded and every branch `i < j` has failed.
|
|
238
|
-
- Once branch `j` is known to be selected, branches `i > j` may be
|
|
239
|
-
cancelled.
|
|
240
|
-
- If any earlier branch is still pending, a later success remains
|
|
241
|
-
provisional.
|
|
242
|
-
|
|
243
|
-
This gives concurrency without nondeterminism. The fastest branch does
|
|
244
|
-
not win; the lowest-index successful branch wins.
|
|
245
|
-
|
|
246
|
-
## Partiality And Cancellation
|
|
247
|
-
|
|
248
|
-
kVM failure is pure and unobservable except through product and union
|
|
249
|
-
joins. That gives simple cancellation laws:
|
|
250
|
-
|
|
251
|
-
- In a product, one failure is enough to fail the product.
|
|
252
|
-
- In a union, a later success is provisional until all earlier branches
|
|
253
|
-
fail.
|
|
254
|
-
- Cancelling an unfinished pure branch cannot change the result.
|
|
255
|
-
- Effectful intrinsics are not cancellable unless they declare that
|
|
256
|
-
property.
|
|
257
|
-
|
|
258
|
-
These laws are the bridge between mathematical `k` semantics and
|
|
259
|
-
practical task schedulers.
|
|
260
|
-
|
|
261
|
-
There is one important caveat: purity does not by itself erase
|
|
262
|
-
divergence. If a backend must preserve exact operational behavior in
|
|
263
|
-
the presence of nontermination, it should use the strict scheduling
|
|
264
|
-
profile: sequential lowering is always valid, and speculative parallel
|
|
265
|
-
work may not commit a result or failure that an earlier unresolved
|
|
266
|
-
branch could have prevented. If the contract is only result equivalence
|
|
267
|
-
for terminating computations, product fail-fast and broader cancellation
|
|
268
|
-
are valid optimizations.
|
|
269
|
-
|
|
270
|
-
## Lowering From Envelope-Specialized KIR-P
|
|
271
|
-
|
|
272
|
-
Suggested lowering rules:
|
|
273
|
-
|
|
274
|
-
| KIR operation | kVM lowering |
|
|
30
|
+
| Instruction | Operation |
|
|
275
31
|
| --- | --- |
|
|
276
|
-
| `
|
|
277
|
-
| `
|
|
278
|
-
| `
|
|
279
|
-
| `
|
|
280
|
-
| `
|
|
281
|
-
| `
|
|
282
|
-
| `
|
|
283
|
-
| `
|
|
284
|
-
| `
|
|
285
|
-
| `product` |
|
|
286
|
-
| `union` |
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
##
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
-
|
|
331
|
-
|
|
332
|
-
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
LLVM can lower each kVM function to a native function returning a
|
|
346
|
-
`KResult` struct. kVM registers map naturally to LLVM SSA values.
|
|
347
|
-
Product and union regions can lower first to ordinary blocks, then later
|
|
348
|
-
to runtime task calls or LLVM parallelism experiments.
|
|
349
|
-
|
|
350
|
-
Baseline product lowering:
|
|
351
|
-
|
|
352
|
-
```text
|
|
353
|
-
run branch 0
|
|
354
|
-
if fail return fail
|
|
355
|
-
run branch 1
|
|
356
|
-
if fail return fail
|
|
357
|
-
construct product
|
|
358
|
-
return ok
|
|
359
|
-
```
|
|
360
|
-
|
|
361
|
-
Baseline union lowering:
|
|
362
|
-
|
|
363
|
-
```text
|
|
364
|
-
run branch 0
|
|
365
|
-
if ok return branch 0
|
|
366
|
-
run branch 1
|
|
367
|
-
if ok return branch 1
|
|
368
|
-
return fail
|
|
369
|
-
```
|
|
370
|
-
|
|
371
|
-
Parallel lowering is an optimization, not a semantic requirement.
|
|
372
|
-
|
|
373
|
-
### WebAssembly
|
|
374
|
-
|
|
375
|
-
Wasm can use the same kVM control-flow shape with locals and blocks.
|
|
376
|
-
The MVP path should be sequential. Wasm threads, shared memory, or host
|
|
377
|
-
promises can later implement product and union regions without changing
|
|
378
|
-
kVM semantics.
|
|
379
|
-
|
|
380
|
-
The key Wasm constraint is that the kVM instruction set should avoid
|
|
381
|
-
relying on arbitrary pointer arithmetic in the IR itself.
|
|
382
|
-
Pointer/layout details should live behind runtime imports or explicit
|
|
383
|
-
memory-layout helpers.
|
|
384
|
-
|
|
385
|
-
### JS kVM Interpreter
|
|
386
|
-
|
|
387
|
-
A JS kVM interpreter is valuable as a conformance oracle. It can start
|
|
388
|
-
with sequential product and union execution, compare results against
|
|
389
|
-
`run.mjs` and `run_converged`, then add optional parallel scheduling
|
|
390
|
-
later.
|
|
391
|
-
|
|
392
|
-
## Validation Rules
|
|
393
|
-
|
|
394
|
-
A valid kVM function should satisfy:
|
|
395
|
-
|
|
396
|
-
1. Every function has one `KRef` input and returns `KResult<KRef>`.
|
|
397
|
-
2. Every register is assigned once.
|
|
398
|
-
3. Every failure path is explicit.
|
|
399
|
-
4. Product field labels are unique and canonical.
|
|
400
|
-
5. Union branch order is preserved.
|
|
401
|
-
6. Calls target concrete relation instances, not unspecialized source
|
|
402
|
-
names.
|
|
403
|
-
7. Layout hints match the input pattern expected by the instruction.
|
|
404
|
-
8. Envelope-free functions are allowed only when every reachable
|
|
405
|
-
relation instance has converged type derivation.
|
|
406
|
-
9. Parallel scheduling is allowed only through pure/cancellable regions.
|
|
407
|
-
|
|
408
|
-
## Example
|
|
409
|
-
|
|
410
|
-
For a source shape like:
|
|
411
|
-
|
|
412
|
-
```k
|
|
413
|
-
{ .x a, .y b }
|
|
414
|
-
```
|
|
415
|
-
|
|
416
|
-
kVM should keep the parallel product structure:
|
|
417
|
-
|
|
418
|
-
```text
|
|
419
|
-
fn @example(%in):
|
|
420
|
-
%out = product %in [
|
|
421
|
-
field a: {
|
|
422
|
-
%x = project_field %in, label:x
|
|
423
|
-
return %x
|
|
424
|
-
}
|
|
425
|
-
field b: {
|
|
426
|
-
%y = project_field %in, label:y
|
|
427
|
-
return %y
|
|
428
|
-
}
|
|
429
|
-
] join all_success
|
|
430
|
-
return %out
|
|
431
|
-
```
|
|
432
|
-
|
|
433
|
-
For:
|
|
434
|
-
|
|
435
|
-
```k
|
|
436
|
-
< f, g, h >
|
|
437
|
-
```
|
|
438
|
-
|
|
439
|
-
kVM should keep the deterministic union structure:
|
|
440
|
-
|
|
441
|
-
```text
|
|
442
|
-
fn @choice(%in):
|
|
443
|
-
%out = union %in [
|
|
444
|
-
case 0: { %r0 = call @f__P, %in; return %r0 }
|
|
445
|
-
case 1: { %r1 = call @g__P, %in; return %r1 }
|
|
446
|
-
case 2: { %r2 = call @h__P, %in; return %r2 }
|
|
447
|
-
] join first_success_by_index
|
|
448
|
-
return %out
|
|
449
|
-
```
|
|
450
|
-
|
|
451
|
-
The runtime may evaluate `f`, `g`, and `h` concurrently, but it may
|
|
452
|
-
return `g` only after `f` has failed.
|
|
453
|
-
|
|
454
|
-
## Open Questions
|
|
455
|
-
|
|
456
|
-
- Should the first persisted kVM encoding be textual for inspection,
|
|
457
|
-
compact binary for embedding, or both?
|
|
458
|
-
- Should product fail-fast be the only release behavior, or should there
|
|
459
|
-
be a standard diagnostic mode that collects all failed fields?
|
|
460
|
-
- How should effectful debug intrinsics be represented so they cannot
|
|
461
|
-
silently enter parallel backend execution?
|
|
462
|
-
- Is `guard_code` needed in the final envelope-free backend path, or
|
|
463
|
-
should all code/type checks be resolved into layout-specific
|
|
464
|
-
projections and guards?
|
|
465
|
-
- Should kVM expose a generic `match` instruction for union dispatch, or
|
|
466
|
-
are `project_variant` plus union regions enough?
|
|
467
|
-
|
|
468
|
-
## Implementation Order
|
|
469
|
-
|
|
470
|
-
1. Keep KIR-P as the current object-level relation format.
|
|
471
|
-
2. Define a kVM artifact schema for specialized relation instances.
|
|
472
|
-
3. Lower KIR-P relation ops to kVM in envelope-aware mode.
|
|
473
|
-
4. Add a sequential JS kVM interpreter and compare it against `run.mjs`.
|
|
474
|
-
5. Lower envelope-specialized and converged relation instances to
|
|
475
|
-
envelope-free kVM.
|
|
476
|
-
6. Add kVM validation and conformance fixtures.
|
|
477
|
-
7. Add a minimal C or Wasm backend for sequential kVM.
|
|
478
|
-
8. Add LLVM lowering.
|
|
479
|
-
9. Add optional runtime scheduling for product and union regions.
|
|
480
|
-
|
|
481
|
-
The important first milestone is not parallel speed. It is a stable
|
|
482
|
-
execution contract that makes sequential LLVM and Wasm generation
|
|
483
|
-
boring, while preserving the parallel structure that `k` gets from
|
|
484
|
-
purity.
|
|
485
|
-
|
|
486
|
-
## Further Reading
|
|
487
|
-
|
|
488
|
-
- [DOCS/FILE_FORMATS.md](./FILE_FORMATS.md) — guide to .k, .ko, .klib,
|
|
489
|
-
and .kvm formats
|
|
490
|
-
- [DOCS/KIR_V1.md](./KIR_V1.md) — KIR-P inspection/export contract for
|
|
491
|
-
backends
|
|
492
|
-
- [DOCS/DICTIONARY.md](./DICTIONARY.md) — concept names and terminology
|
|
32
|
+
| `id` | Forward a register |
|
|
33
|
+
| `fail` | Return ordinary partial failure |
|
|
34
|
+
| `guard_pattern` | Constrain a value with a pattern |
|
|
35
|
+
| `guard_code` | Check a closed schema |
|
|
36
|
+
| `project_field` | Select a labeled product child |
|
|
37
|
+
| `project_variant` | Select a matching variant payload |
|
|
38
|
+
| `make_variant` | Wrap a value with a tag |
|
|
39
|
+
| `call` | Invoke another relation |
|
|
40
|
+
| `call_intrinsic` | Invoke an explicitly registered intrinsic |
|
|
41
|
+
| `product` | Evaluate labeled branch bodies and build a product |
|
|
42
|
+
| `union` | Try branch bodies in source order |
|
|
43
|
+
| `return` | Return a register |
|
|
44
|
+
|
|
45
|
+
For example, `{ .x a, .y b }` becomes a product instruction with one branch
|
|
46
|
+
projecting `x` and another projecting `y`. Each branch receives the original
|
|
47
|
+
input. Registers use names such as `%v0`.
|
|
48
|
+
|
|
49
|
+
## Partiality and ordering
|
|
50
|
+
|
|
51
|
+
Composition stops when a step is undefined. A product succeeds only when all
|
|
52
|
+
field branches succeed. A union returns the first defined branch in source
|
|
53
|
+
order; if none succeed, it is undefined.
|
|
54
|
+
|
|
55
|
+
The current interpreter evaluates these branches sequentially. Purity alone
|
|
56
|
+
does not permit committing a later branch while an earlier branch diverges.
|
|
57
|
+
The representation does not require a parallel task scheduler.
|
|
58
|
+
|
|
59
|
+
A contradiction between the supplied envelope and static constraints is a type
|
|
60
|
+
error, not ordinary branch failure. Unsupported intrinsics must be diagnosed
|
|
61
|
+
explicitly rather than inferred from identifier spelling.
|
|
62
|
+
|
|
63
|
+
## Specialization
|
|
64
|
+
|
|
65
|
+
`specializeKVM(template, inputPattern)` checks the supplied input envelope
|
|
66
|
+
against the principal contract, propagates pattern information through the
|
|
67
|
+
instruction stream, derives output patterns, folds proven guards, and prunes
|
|
68
|
+
unreachable branches.
|
|
69
|
+
|
|
70
|
+
The specialized artifact retains boundary patterns. An envelope-free evaluator
|
|
71
|
+
can then avoid per-operation pattern propagation, while the caller validates
|
|
72
|
+
inputs and attaches the output envelope.
|
|
73
|
+
|
|
74
|
+
This pass is distinct from
|
|
75
|
+
[KIR-P envelope specialization](KIR_V1.md#envelope-specialization), which can
|
|
76
|
+
rerun source-level contract derivation.
|
|
77
|
+
|
|
78
|
+
## Execution modes and backends
|
|
79
|
+
|
|
80
|
+
`executeKVM` supports envelope-aware evaluation and an envelope-free option.
|
|
81
|
+
The latter requires compatible converged contracts and explicit execution
|
|
82
|
+
context for referenced functions.
|
|
83
|
+
|
|
84
|
+
- [Wasm](../backends/wasm/README.md) maps instructions to locals/blocks and an
|
|
85
|
+
arena in linear memory.
|
|
86
|
+
- [LLVM](../backends/llvm/README.md) emits target functions and uses the C
|
|
87
|
+
runtime.
|
|
88
|
+
- [PKIR](POLYMORPHIC_IR.md) has a separate layout-oriented instruction set
|
|
89
|
+
and a lowering adapter to kVM.
|
|
90
|
+
|
|
91
|
+
The representation preserves branch order and partial failure across these
|
|
92
|
+
paths. It does not specify a universal pointer layout for every backend.
|
|
93
|
+
|
|
94
|
+
## Verification
|
|
95
|
+
|
|
96
|
+
`tests/test-kvm.mjs` checks instructions and reference parity.
|
|
97
|
+
`tests/test-kvm-polymorphic.mjs` checks template emission, specialization,
|
|
98
|
+
envelope mismatch, branch pruning, and CLI behavior. Both run under `npm test`.
|
|
99
|
+
The separate backend suites validate target execution.
|