@fraczak/k 6.3.3 → 6.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.
Files changed (157) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/DOCS/CODECS.md +258 -0
  3. package/DOCS/DICTIONARY.md +1172 -0
  4. package/DOCS/KIR_SPECIALIZATION_DESIGN.md +51 -46
  5. package/DOCS/KIR_V1.md +221 -0
  6. package/DOCS/KVM_EXECUTION_MODEL.md +56 -19
  7. package/DOCS/OBJECT_FILE_AND_PATTERN.md +2 -1
  8. package/DOCS/PROFILING_AND_TRACING.md +196 -0
  9. package/DOCS/REPL.md +61 -4
  10. package/DOCS/WEB_REPL_REQUIREMENTS.md +135 -0
  11. package/DOCS/dependency-graph.md +144 -0
  12. package/Examples/arithmetics.k +108 -82
  13. package/Examples/bnat.k +1 -1
  14. package/Examples/buda.k +44 -0
  15. package/Examples/ieee.k +300 -8671
  16. package/Examples/poly.k +67 -0
  17. package/README.md +68 -19
  18. package/Value.mjs +8 -2
  19. package/augmentation.mjs +9 -7
  20. package/backend-api.mjs +11 -2
  21. package/backends/arm64/README.md +104 -0
  22. package/backends/arm64/bin/k-arm64-compile.mjs +101 -0
  23. package/backends/arm64/bin/k-arm64-run.mjs +58 -0
  24. package/backends/arm64/bin/k-arm64.mjs +88 -0
  25. package/backends/arm64/scripts/run-tests.mjs +41 -0
  26. package/backends/arm64/src/arm64.mjs +229 -0
  27. package/backends/arm64/src/cli.mjs +155 -0
  28. package/backends/arm64/src/kvm2arm64.mjs +906 -0
  29. package/backends/arm64/src/runtime.c +728 -0
  30. package/backends/arm64/tests/integration.sh +112 -0
  31. package/backends/arm64/tests/test-arm64.mjs +240 -0
  32. package/backends/llvm/README.md +135 -0
  33. package/backends/llvm/bin/k-llvm-build.mjs +71 -0
  34. package/backends/llvm/bin/k-llvm-compile.mjs +68 -0
  35. package/backends/llvm/bin/k-llvm-jit.mjs +160 -0
  36. package/backends/llvm/bin/k-llvm-run.mjs +90 -0
  37. package/backends/llvm/runtime/krt.c +1508 -0
  38. package/backends/llvm/runtime/krt.h +109 -0
  39. package/backends/llvm/scripts/conformance.mjs +76 -0
  40. package/backends/llvm/scripts/run-tests.mjs +28 -0
  41. package/backends/llvm/src/executable.mjs +562 -0
  42. package/backends/llvm/src/kvm2llvm.mjs +998 -0
  43. package/backends/llvm/src/llvm.mjs +1092 -0
  44. package/backends/llvm/tests/composition-driver.c +22 -0
  45. package/backends/llvm/tests/identity-driver.c +27 -0
  46. package/backends/llvm/tests/integration.sh +153 -0
  47. package/backends/llvm/tests/perf-support.mjs +735 -0
  48. package/backends/llvm/tests/product-driver.c +23 -0
  49. package/backends/llvm/tests/projection-driver.c +19 -0
  50. package/backends/llvm/tests/relation-driver.c +29 -0
  51. package/backends/llvm/tests/test-llvm.mjs +240 -0
  52. package/backends/llvm/tests/union-driver.c +31 -0
  53. package/backends/llvm/tests/variant-driver.c +19 -0
  54. package/backends/llvm/tests/variant-projection-driver.c +21 -0
  55. package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +233 -0
  56. package/backends/wasm/README.md +97 -0
  57. package/backends/wasm/TECHNICAL-DEBT.md +58 -0
  58. package/backends/wasm/bin/k-wasm-compile.mjs +50 -0
  59. package/backends/wasm/bin/k-wasm-run.mjs +140 -0
  60. package/backends/wasm/bin/k-wasm.mjs +49 -0
  61. package/backends/wasm/demo_ieee.mjs +319 -0
  62. package/backends/wasm/runtime.wat +475 -0
  63. package/backends/wasm/scripts/run-tests.mjs +41 -0
  64. package/backends/wasm/src/cli.mjs +133 -0
  65. package/backends/wasm/src/kvm2wasm.mjs +697 -0
  66. package/backends/wasm/src/wasm.mjs +1054 -0
  67. package/backends/wasm/tests/integration.sh +182 -0
  68. package/backends/wasm/tests/perf-support.mjs +336 -0
  69. package/backends/wasm/tests/test-wasm.mjs +625 -0
  70. package/browser/repl-browser.mjs +1584 -0
  71. package/browser/shims/assert.mjs +19 -0
  72. package/browser/shims/buffer.mjs +224 -0
  73. package/browser/shims/crypto.mjs +103 -0
  74. package/browser/shims/fs.mjs +159 -0
  75. package/browser/shims/os.mjs +17 -0
  76. package/browser/shims/path.mjs +54 -0
  77. package/browser/shims/process.mjs +44 -0
  78. package/browser/shims/readline.mjs +11 -0
  79. package/browser/shims/url.mjs +12 -0
  80. package/browser/vfs-data.mjs +19 -0
  81. package/codecs/README.md +3 -0
  82. package/codecs/runtime/show-value.mjs +12 -3
  83. package/conformance/helper-reuse/case.json +8 -0
  84. package/conformance/helper-reuse/expected.kv +1 -0
  85. package/conformance/helper-reuse/input.kv +1 -0
  86. package/conformance/helper-reuse/program.k +2 -0
  87. package/conformance/identity/case.json +8 -0
  88. package/conformance/identity/expected.kv +1 -0
  89. package/conformance/identity/input.kv +1 -0
  90. package/conformance/identity/program.k +1 -0
  91. package/conformance/product-projection/case.json +8 -0
  92. package/conformance/product-projection/expected.kv +1 -0
  93. package/conformance/product-projection/input.kv +1 -0
  94. package/conformance/product-projection/program.k +1 -0
  95. package/conformance/run.mjs +149 -0
  96. package/conformance/variant-construction/case.json +8 -0
  97. package/conformance/variant-construction/expected.kv +1 -0
  98. package/conformance/variant-construction/input.kv +1 -0
  99. package/conformance/variant-construction/program.k +1 -0
  100. package/harness/adapters/arm64.mjs +105 -0
  101. package/harness/adapters/kvm.mjs +144 -0
  102. package/harness/adapters/llvm.mjs +118 -0
  103. package/harness/adapters/native.mjs +77 -0
  104. package/harness/adapters/wasm.mjs +169 -0
  105. package/harness/backend.mjs +95 -0
  106. package/harness/cli.mjs +130 -0
  107. package/harness/format.mjs +95 -0
  108. package/harness/index.mjs +25 -0
  109. package/harness/persistent.mjs +121 -0
  110. package/harness/registry.mjs +44 -0
  111. package/harness/suite.mjs +257 -0
  112. package/harness/suites/poly.mjs +156 -0
  113. package/harness/trace.mjs +50 -0
  114. package/harness/util.mjs +92 -0
  115. package/intrinsics.mjs +41 -0
  116. package/kir.mjs +399 -0
  117. package/kvm.mjs +585 -81
  118. package/objects/README.md +37 -3
  119. package/objects/compile.mjs +15 -22
  120. package/objects/inspect.mjs +96 -0
  121. package/objects/validate.mjs +415 -0
  122. package/package.json +51 -19
  123. package/repl-codecs.mjs +105 -6
  124. package/repl.mjs +335 -49
  125. package/run.mjs +2 -7
  126. package/scripts/benchmark.mjs +406 -22
  127. package/scripts/build-repl-html.mjs +1784 -0
  128. package/scripts/perf-ieee.mjs +586 -0
  129. package/scripts/perf-int.mjs +531 -0
  130. package/scripts/perf-poly.mjs +945 -0
  131. package/scripts/render-benchmark.mjs +12 -8
  132. package/scripts/run-tests.mjs +6 -0
  133. package/tests/integration.sh +19 -2
  134. package/tests/test-arithmetics.mjs +54 -0
  135. package/tests/test-ieee-arithmetic.mjs +4 -0
  136. package/tests/test-kir.mjs +85 -0
  137. package/tests/test-kvm-polymorphic.mjs +278 -0
  138. package/tests/test-kvm.mjs +25 -6
  139. package/tests/test-poly.mjs +129 -0
  140. package/tests/test-repl-html.mjs +303 -0
  141. package/tests/test-repl.mjs +144 -1
  142. package/tests/test-validate-object.mjs +40 -0
  143. package/a.klib +0 -1
  144. package/a.kvm +0 -142676
  145. package/a.wasm +0 -0
  146. package/benchmark-results.html +0 -532
  147. package/benchmark-results.json +0 -480
  148. package/k-vscode/.vscode/launch.json +0 -14
  149. package/k-vscode/README.md +0 -75
  150. package/k-vscode/extension.js +0 -273
  151. package/k-vscode/language-configuration.json +0 -42
  152. package/k-vscode/package.json +0 -79
  153. package/k-vscode/snippets/k.json +0 -92
  154. package/k-vscode/syntaxes/k.tmLanguage.json +0 -599
  155. package/k-vscode/test/background-ranges.test.js +0 -50
  156. package/k-vscode/themes/k-contrast-color-theme.json +0 -104
  157. package/scripts/git-hooks/pre-commit +0 -10
@@ -0,0 +1,1172 @@
1
+ # Concept Dictionary
2
+
3
+ This document defines the main concepts, artifacts, values, and transformations
4
+ of the k system. It is written as a standalone specification: terms are defined
5
+ by their role in the language and compilation pipeline, not by any particular
6
+ concrete compiler, storage encoding, or tool interface.
7
+
8
+ The goal is to keep one vocabulary across source semantics, object artifacts,
9
+ intermediate representations, value serialization, reference execution, and
10
+ target execution.
11
+
12
+ ## Naming Principles
13
+
14
+ - Use semantic names for all externally visible concepts.
15
+ - Distinguish a concept from its serialized representation.
16
+ - Distinguish source syntax from the semantic object represented by that syntax.
17
+ - Distinguish a mathematical function from the compiler artifacts used to
18
+ compile or execute it.
19
+ - Treat "pattern" as a type-context concept, not as ordinary value-pattern
20
+ matching over raw trees.
21
+
22
+ ## Source-Level Concepts
23
+
24
+ ### k Source
25
+
26
+ `k source` is the textual language read by the parser. It can define types,
27
+ partial functions, and an entry expression.
28
+
29
+ Use this term for source text before compiler passes have resolved names,
30
+ canonicalized types, or derived pattern information.
31
+
32
+ ### Expression
33
+
34
+ An `expression` is a source-level program fragment that denotes a partial
35
+ function over enveloped values.
36
+
37
+ Expressions include identity, composition, projection, variant introduction,
38
+ product construction, ordered union, type checks, pattern guards, and references
39
+ to named partial functions or intrinsics.
40
+
41
+ ### Partial Function
42
+
43
+ A partial function maps an input enveloped value to an output enveloped value
44
+ when it is defined. If it is not defined for the input, execution fails normally.
45
+ This failure is part of the language semantics, not a system exception.
46
+
47
+ Mathematically, a k program denotes a deterministic partial function.
48
+
49
+ ### Type
50
+
51
+ A `type` describes a set of value trees accepted by an algebraic shape. Types are
52
+ built from products and tagged unions.
53
+
54
+ Use "type" for the semantic concept. Avoid using "code" by itself in public docs
55
+ when the meaning is type.
56
+
57
+ ### Type Code
58
+
59
+ A `type code` is the canonical, hashable representation of a type.
60
+
61
+ Use "type" for the semantic concept and "type code" for its canonical
62
+ representation.
63
+
64
+ ### Pattern Guard
65
+
66
+ A `pattern guard` is an expression that constrains the input envelope to a type
67
+ pattern and otherwise behaves like identity.
68
+
69
+ Source syntax may present this operation as a filter expression. The semantic
70
+ operation is a pattern guard.
71
+
72
+ ## Values, Types, And Patterns
73
+
74
+ ### Value Tree
75
+
76
+ A `value tree` is the product/variant tree part of a value during execution.
77
+
78
+ By itself, a value tree is not enough to recover the semantic value at a binary
79
+ or polymorphic boundary. The same tree can be meaningful under different type
80
+ contexts.
81
+
82
+ ### Type Pattern
83
+
84
+ A `type pattern` describes a set of possible types. It is the semantic object
85
+ represented by source filter syntax and by the pattern graphs carried through
86
+ compilation and execution.
87
+
88
+ The short name `pattern` is acceptable once the context is clear. Use "type
89
+ pattern" when introducing the concept or when there is risk of confusion with
90
+ ordinary value matching.
91
+
92
+ Examples of type pattern kinds include:
93
+
94
+ - any type;
95
+ - open product;
96
+ - closed product;
97
+ - open union;
98
+ - closed union;
99
+ - singleton type pattern derived from a concrete type code.
100
+
101
+ ### Pattern Graph
102
+
103
+ A `pattern graph` is the graph representation of a type pattern. Nodes describe
104
+ pattern kind and openness. Edges describe required product fields or union tags.
105
+
106
+ Pattern graph node identity matters: if two positions point to the same pattern
107
+ node, they must share the same type context.
108
+
109
+ Use "pattern graph" for the representation, and "type pattern" for the semantic
110
+ constraint it represents.
111
+
112
+ ### Envelope
113
+
114
+ An `envelope` is the type pattern carried with a value at an execution or wire
115
+ boundary.
116
+
117
+ The envelope describes the type context in which the value tree should be
118
+ decoded, checked, projected, transformed, or encoded. The spelling should always
119
+ be `envelope`.
120
+
121
+ ### Enveloped Value
122
+
123
+ An `enveloped value` is a pair:
124
+
125
+ ```text
126
+ (value tree, envelope)
127
+ ```
128
+
129
+ Structural operations propagate or refine the envelope:
130
+
131
+ - projection selects a subpattern;
132
+ - product construction builds a closed product envelope from field envelopes;
133
+ - variant introduction builds an open union envelope for the introduced tag;
134
+ - pattern guards intersect the active envelope with a static type pattern.
135
+
136
+ The envelope is part of the semantic value at compilation and execution
137
+ boundaries. A value tree without an envelope is incomplete at those boundaries.
138
+
139
+ ### Pattern Signature
140
+
141
+ A `pattern signature` is the input/output type-pattern pair attached to an
142
+ expression or partial function:
143
+
144
+ ```text
145
+ input pattern -> output pattern
146
+ ```
147
+
148
+ Pattern signatures are compiler facts. They constrain valid inputs and describe
149
+ the envelope shape produced when evaluation succeeds.
150
+
151
+ ### Domain
152
+
153
+ Use "domain" carefully.
154
+
155
+ The input pattern of a partial function is a static type-context constraint, not
156
+ necessarily the exact mathematical domain of the function. A function may still
157
+ be undefined for some enveloped values that fit its input pattern.
158
+
159
+ Prefer:
160
+
161
+ - "input pattern";
162
+ - "accepted envelope";
163
+ - "static input constraint".
164
+
165
+ Use "domain" only when referring to the actual set of inputs for which the
166
+ partial function is defined.
167
+
168
+ ## Serialization And Boundaries
169
+
170
+ ### Wire Stream
171
+
172
+ A `wire stream` is the binary transport representation of an enveloped value.
173
+
174
+ The active format is:
175
+
176
+ ```text
177
+ encoded envelope, followed by value bits encoded under that envelope
178
+ ```
179
+
180
+ Use "wire stream" or "pattern+value wire stream" for external transport
181
+ boundaries. Use "enveloped value" for the semantic object used during
182
+ execution.
183
+
184
+ ### Codec
185
+
186
+ A `codec` converts between an external representation and a k wire stream.
187
+
188
+ Examples include the unit, JSON, integer, IEEE, UTF-8, and UTF-16 codecs. A
189
+ codec must preserve the envelope/value relationship: it either supplies an
190
+ envelope for parsing, decodes the envelope from the stream for printing, or
191
+ works with a universal representation that can infer a suitable envelope.
192
+
193
+ ### Input Envelope
194
+
195
+ An `input envelope` is the pattern carried by the input value at a program or
196
+ execution boundary.
197
+
198
+ Execution targets use the input envelope to specialize or validate execution.
199
+ The input envelope can be provided explicitly, derived from a type expression, or
200
+ decoded from a wire stream.
201
+
202
+ ### Output Envelope
203
+
204
+ An `output envelope` is the pattern attached to the output value after execution.
205
+
206
+ A correct runner must emit both the output envelope and the output value bits.
207
+ Rendered values can hide envelope differences, so conformance checks must compare
208
+ envelopes whenever envelope identity matters.
209
+
210
+ ## Compiler Stages
211
+
212
+ ### Parsed AST
213
+
214
+ The `parsed AST` is the direct parser output. It keeps source-shaped expression
215
+ nodes, source names, source ranges, and filter syntax.
216
+
217
+ Use this term only for the representation before type-code registration,
218
+ partial-function canonicalization, and pattern derivation.
219
+
220
+ ### Pattern-Annotated Core
221
+
222
+ `Pattern-annotated core` is the first semantic compiler artifact after parsing.
223
+
224
+ It keeps the expression structure close to k source, but it has:
225
+
226
+ - registered canonical type codes;
227
+ - resolved type references to canonical type-code names;
228
+ - partial-function dependency information;
229
+ - canonical aliases for partial functions;
230
+ - a type-pattern graph for each partial function;
231
+ - a pattern signature on each expression;
232
+ - type-derivation convergence status.
233
+
234
+ The key product is still a k-shaped core expression graph, now annotated by
235
+ type-pattern facts. This stage is a semantic artifact; it does not imply a
236
+ particular serialized format.
237
+
238
+ ### k Object
239
+
240
+ A `k object` is a compiled semantic object.
241
+
242
+ It stores canonical type codes, partial functions, aliases, metadata,
243
+ type-pattern graphs, and type-derivation status.
244
+
245
+ An executable object has a main partial function. A library object contains
246
+ reusable definitions but no selected main partial function.
247
+
248
+ ### KIR-P
249
+
250
+ `KIR-P` is the portable, polymorphic K Intermediate Representation.
251
+
252
+ It is a stable artifact contract over k objects. It preserves pattern-carrying
253
+ semantics while normalizing names, expression opcodes, and pattern graph node IDs
254
+ so consumers do not depend on parser-shaped fields or object encoding details.
255
+
256
+ Use KIR-P as the shared semantic contract between k objects and lower-level
257
+ execution artifacts.
258
+
259
+ ### Envelope-Specialized KIR-P
260
+
261
+ `Envelope-specialized KIR-P` is KIR-P rebuilt for a concrete input envelope.
262
+
263
+ Envelope-specialization:
264
+
265
+ - takes a KIR-P object or k object;
266
+ - takes an input envelope;
267
+ - re-runs pattern derivation as if the entry expression were guarded by that
268
+ envelope;
269
+ - derives the corresponding output envelope;
270
+ - emits ordinary KIR-P for the specialized entry partial function.
271
+
272
+ ### KIR-M
273
+
274
+ `KIR-M` is a materialized execution layer after envelope-specialized KIR-P.
275
+
276
+ It should contain layout and ABI decisions such as field offsets, tag dispatch,
277
+ call-site specialization, and envelope-free execution assumptions.
278
+
279
+ ### Lowered Execution Model
280
+
281
+ A `lowered execution model` is an executable middle layer.
282
+
283
+ It lowers KIR-P partial-function bodies into a small instruction model with
284
+ explicit partial failure, pattern guards, projections, construction, calls, and
285
+ returns.
286
+ It is an execution contract that a lower-level compiler or executor can
287
+ consume. It is not defined by a particular byte encoding.
288
+
289
+ `kVM` is the name used in this specification for such a lowered execution model.
290
+
291
+ ### Lowered Execution Artifact
292
+
293
+ A `lowered execution artifact` is a post-envelope-specialization execution
294
+ artifact.
295
+
296
+ It is produced by taking a source or object program, specializing it against a
297
+ concrete input envelope, lowering the resulting KIR-P to a lowered execution
298
+ model, and wrapping the entry metadata and functions. It is not the same thing
299
+ as a general k object.
300
+
301
+ ## Execution Concepts
302
+
303
+ ### Envelope-Aware Execution
304
+
305
+ `Envelope-aware execution` carries envelopes through inner operations.
306
+
307
+ This mode matches the reference execution and codec model. It is the safest
308
+ semantic baseline because values keep their envelope throughout execution.
309
+
310
+ ### Envelope-Free Execution
311
+
312
+ `Envelope-free execution` uses static layout and pattern information inside the
313
+ compiled function and attaches the derived output envelope only at the boundary.
314
+
315
+ This is valid only when envelope-specialization and type derivation prove the
316
+ needed pattern contexts.
317
+
318
+ ### Partial Failure
319
+
320
+ `Partial failure` is the normal result when a partial function is undefined for
321
+ its input.
322
+
323
+ In high-level execution this is represented as absence of an output value. In
324
+ lower-level execution contracts it may be represented explicitly as a failed
325
+ result. It is distinct from a `type error`, malformed artifacts, system
326
+ failures, and host exceptions.
327
+
328
+ ### Type Error
329
+
330
+ A `type error` is a semantic contradiction indicating that the type pattern of a
331
+ value or expression does not conform to the expected input pattern of the
332
+ partial function to which it is applied.
333
+
334
+ A type error is fundamentally distinct from `partial failure`:
335
+
336
+ - `Partial failure` is a normal, valid semantic outcome representing mathematical
337
+ undefinedness for an input value that nonetheless conforms to the expected
338
+ input pattern (for example, failing a condition in an inner branch, which in
339
+ an ordered union allows subsequent branches to be evaluated).
340
+ - A `type error` represents a type-system contradiction or boundary contract
341
+ violation. It is not an ordinary execution result and cannot be recovered by
342
+ branching constructs like ordered union.
343
+
344
+ A type error can occur at two distinct stages:
345
+
346
+ 1. **Compilation time (Type Derivation)**: Triggered during
347
+ `pattern-signature-derivation` when an expression's type pattern cannot be
348
+ unified with, or does not satisfy, the required input pattern of a function
349
+ it is passed to (such as disjoint types or unsatisfiable pattern guards).
350
+ This causes compilation or type derivation to fail with a diagnostic.
351
+ 2. **Runtime (Evaluation Boundary)**: Triggered when an `enveloped value` carries
352
+ an `input envelope` that does not match or conform to the expected input
353
+ pattern of the function being executed (for example, passing an incompatible
354
+ envelope to an entry partial function or target executable). At runtime, a
355
+ type error halts execution with a fatal diagnostic or error code (such as exit
356
+ status 5 in compiled targets) rather than producing partial failure.
357
+
358
+ ### Intrinsic
359
+
360
+ An `intrinsic` is a predefined operation supplied by the execution environment and
361
+ known to the compiler.
362
+
363
+ Intrinsics need explicit type and lowering rules. Pure identity-like intrinsics
364
+ can participate in ordinary partial-function semantics. Effectful or host-only
365
+ intrinsics require explicit ordering constraints or must be excluded from pure
366
+ lowering targets.
367
+
368
+ ## Transformation Graphs
369
+
370
+ This section names the transformations between the concepts above.
371
+
372
+ The diagrams use a Petri-net-like convention:
373
+
374
+ - all nodes are rectangles;
375
+ - blue nodes are artifacts or semantic concepts;
376
+ - amber nodes are transformations;
377
+ - green nodes are execution values or wire streams;
378
+ - purple nodes are lowered execution artifacts;
379
+ - every transformation is shown as an explicit node between its inputs and
380
+ outputs;
381
+ - a transformation with several incoming arrows needs all shown inputs.
382
+
383
+ ### Whole-System Artifact Graph
384
+
385
+ This graph starts with a partial function definition in k syntax and an input
386
+ enveloped value. It ends with an output enveloped value. Lowering paths branch
387
+ after the k object.
388
+
389
+ ```mermaid
390
+ %%{init: {"flowchart": {"htmlLabels": true, "nodeSpacing": 42, "rankSpacing": 58}, "themeVariables": {"fontSize": "18px"}} }%%
391
+ flowchart TB
392
+ classDef artifact fill:#e8f2ff,stroke:#2563eb,stroke-width:1.5px,color:#0f172a;
393
+ classDef transform fill:#fff4d6,stroke:#d97706,stroke-width:1.5px,color:#3b2300;
394
+ classDef value fill:#e8f7ee,stroke:#16a34a,stroke-width:1.5px,color:#052e16;
395
+ classDef lowered fill:#f3e8ff,stroke:#7e22ce,stroke-width:1.5px,color:#2e1065;
396
+ classDef result fill:#fff1f2,stroke:#e11d48,stroke-width:1.5px,color:#4c0519;
397
+
398
+ subgraph Compile["compile source into a semantic object"]
399
+ direction LR
400
+ Src["k source<br/>program"]:::artifact
401
+ Parse["source<br/>parsing"]:::transform
402
+ AST["parsed<br/>AST"]:::artifact
403
+ Annotate["pattern-signature<br/>derivation"]:::transform
404
+ Core["pattern-annotated<br/>core"]:::artifact
405
+ Package["object<br/>packaging"]:::transform
406
+ Object["k<br/>object"]:::artifact
407
+ Src --> Parse --> AST --> Annotate --> Core --> Package --> Object
408
+ end
409
+
410
+ subgraph Input["prepare the input value"]
411
+ direction LR
412
+ InputValue["input<br/>enveloped value"]:::value
413
+ ExtractEnvelope["input-envelope<br/>extraction"]:::transform
414
+ InputEnvelope["input<br/>envelope"]:::artifact
415
+ InputValue --> ExtractEnvelope --> InputEnvelope
416
+ end
417
+
418
+ subgraph Reference["reference envelope-aware execution"]
419
+ direction LR
420
+ Select["entry<br/>selection"]:::transform
421
+ EntryFunction["entry partial<br/>function"]:::artifact
422
+ ReferenceRun["reference<br/>evaluation"]:::transform
423
+ OutputValue["output<br/>enveloped value"]:::result
424
+ Select --> EntryFunction --> ReferenceRun --> OutputValue
425
+ end
426
+
427
+ subgraph KIRPrep["prepare lowered artifacts"]
428
+ direction LR
429
+ ExportKIR["KIR-P<br/>export"]:::transform
430
+ KIRP["KIR-P"]:::artifact
431
+ Specialize["envelope<br/>specialization"]:::transform
432
+ SpecializedKIRP["envelope-specialized<br/>KIR-P"]:::artifact
433
+ OutputEnvelope["output<br/>envelope"]:::artifact
434
+ ExportKIR --> KIRP --> Specialize --> SpecializedKIRP
435
+ Specialize --> OutputEnvelope
436
+ end
437
+
438
+ subgraph LoweredPath["lowered execution path"]
439
+ direction LR
440
+ LowerModel["execution-model<br/>lowering"]:::transform
441
+ LoweredArtifact["lowered execution<br/>artifact"]:::lowered
442
+ ExecuteLowered["lowered<br/>evaluation"]:::transform
443
+ LowerModel --> LoweredArtifact --> ExecuteLowered
444
+ end
445
+
446
+ subgraph TargetPath["target execution path"]
447
+ direction LR
448
+ TargetLowering["target<br/>lowering"]:::transform
449
+ TargetArtifact["target<br/>artifact"]:::lowered
450
+ TargetExecution["target<br/>execution"]:::transform
451
+ TargetLowering --> TargetArtifact --> TargetExecution
452
+ end
453
+
454
+ Object --> Select
455
+ InputValue --> ReferenceRun
456
+ Object --> ExportKIR
457
+ InputEnvelope --> Specialize
458
+ SpecializedKIRP --> LowerModel
459
+ LoweredArtifact --> TargetLowering
460
+ InputValue --> ExecuteLowered
461
+ InputValue --> TargetExecution
462
+ ExecuteLowered --> OutputValue
463
+ TargetExecution --> OutputValue
464
+ ```
465
+
466
+ ### Reference Execution Graph
467
+
468
+ The reference path is the semantic baseline. It can run directly from source or
469
+ from a loaded k object. It stays envelope-aware: the value keeps its envelope
470
+ through projections, constructors, pattern guards, and partial-function calls.
471
+
472
+ ```mermaid
473
+ %%{init: {"flowchart": {"htmlLabels": true, "nodeSpacing": 42, "rankSpacing": 50}, "themeVariables": {"fontSize": "18px"}} }%%
474
+ flowchart LR
475
+ classDef artifact fill:#e8f2ff,stroke:#2563eb,stroke-width:1.5px,color:#0f172a;
476
+ classDef transform fill:#fff4d6,stroke:#d97706,stroke-width:1.5px,color:#3b2300;
477
+ classDef value fill:#e8f7ee,stroke:#16a34a,stroke-width:1.5px,color:#052e16;
478
+ classDef result fill:#fff1f2,stroke:#e11d48,stroke-width:1.5px,color:#4c0519;
479
+
480
+ Source["partial function<br/>definition in k syntax"]:::artifact
481
+ Input["input<br/>enveloped value"]:::value
482
+ Parse["source<br/>parsing"]:::transform
483
+ AST["parsed<br/>AST"]:::artifact
484
+ Annotate["pattern-signature<br/>derivation"]:::transform
485
+ Core["pattern-annotated<br/>core"]:::artifact
486
+ Select["entry<br/>selection"]:::transform
487
+ Transform["partial function with<br/>pattern signature"]:::artifact
488
+ Run["reference<br/>evaluation"]:::transform
489
+ Output["output<br/>enveloped value"]:::result
490
+
491
+ Source --> Parse --> AST --> Annotate --> Core --> Select --> Transform
492
+ Transform --> Run
493
+ Input --> Run
494
+ Run --> Output
495
+ ```
496
+
497
+ ### Lowering Preparation Graph
498
+
499
+ The lowering preparation path starts from a k object and an input envelope. It
500
+ specializes the program for that envelope, derives the output envelope, lowers
501
+ to a lowered execution artifact, and may then lower to a concrete execution
502
+ target.
503
+
504
+ ```mermaid
505
+ %%{init: {"flowchart": {"htmlLabels": true, "nodeSpacing": 42, "rankSpacing": 50}, "themeVariables": {"fontSize": "18px"}} }%%
506
+ flowchart LR
507
+ classDef artifact fill:#e8f2ff,stroke:#2563eb,stroke-width:1.5px,color:#0f172a;
508
+ classDef transform fill:#fff4d6,stroke:#d97706,stroke-width:1.5px,color:#3b2300;
509
+ classDef lowered fill:#f3e8ff,stroke:#7e22ce,stroke-width:1.5px,color:#2e1065;
510
+
511
+ Object["k<br/>object"]:::artifact
512
+ InputEnvelope["input<br/>envelope"]:::artifact
513
+ Export["KIR-P<br/>export"]:::transform
514
+ KIRP["KIR-P"]:::artifact
515
+ Specialize["envelope<br/>specialization"]:::transform
516
+ Specialized["envelope-specialized<br/>KIR-P"]:::artifact
517
+ OutputEnvelope["output<br/>envelope"]:::artifact
518
+ LowerModel["execution-model<br/>lowering"]:::transform
519
+ Lowered["lowered execution<br/>artifact"]:::lowered
520
+ TargetLowering["target<br/>lowering"]:::transform
521
+ Target["target<br/>artifact"]:::lowered
522
+
523
+ Object --> Export --> KIRP
524
+ KIRP --> Specialize
525
+ InputEnvelope --> Specialize
526
+ Specialize --> Specialized
527
+ Specialize --> OutputEnvelope
528
+ Specialized --> LowerModel --> Lowered
529
+ Lowered --> TargetLowering --> Target
530
+ ```
531
+
532
+ ### Wire Boundary Graph
533
+
534
+ The wire boundary is separate from program compilation. It converts between a
535
+ binary pattern+value stream and the in-memory enveloped value that execution
536
+ uses.
537
+
538
+ ```mermaid
539
+ %%{init: {"flowchart": {"htmlLabels": true, "nodeSpacing": 42, "rankSpacing": 50}, "themeVariables": {"fontSize": "18px"}} }%%
540
+ flowchart LR
541
+ classDef transform fill:#fff4d6,stroke:#d97706,stroke-width:1.5px,color:#3b2300;
542
+ classDef value fill:#e8f7ee,stroke:#16a34a,stroke-width:1.5px,color:#052e16;
543
+ classDef result fill:#fff1f2,stroke:#e11d48,stroke-width:1.5px,color:#4c0519;
544
+
545
+ InputWire["input wire<br/>stream"]:::value
546
+ Decode["wire<br/>decoding"]:::transform
547
+ InputValue["input<br/>enveloped value"]:::value
548
+ Execute["reference/target<br/>execution"]:::transform
549
+ OutputValue["output<br/>enveloped value"]:::result
550
+ Encode["wire<br/>encoding"]:::transform
551
+ OutputWire["output wire<br/>stream"]:::result
552
+
553
+ InputWire --> Decode --> InputValue
554
+ InputValue --> Execute --> OutputValue
555
+ OutputValue --> Encode --> OutputWire
556
+ ```
557
+
558
+ ### Transformation Naming Scheme
559
+
560
+ Use noun phrases ending in the operation kind:
561
+
562
+ - `Parsing` for syntax to AST.
563
+ - `Registration` for assigning canonical identities.
564
+ - `Construction` for building semantic tables from parsed material.
565
+ - `Derivation` for computing type-pattern information.
566
+ - `Canonicalization` for stable names and hashes.
567
+ - `Packaging` for serializable artifacts.
568
+ - `Loading` for reconstructing semantic objects from artifacts.
569
+ - `Selection` for choosing an entry object from a larger artifact.
570
+ - `Evaluation` for executing a semantic or lowered execution artifact.
571
+ - `Extraction` for reading a component from an artifact without changing it.
572
+ - `Export` for a stable inspection or interchange view.
573
+ - `Specialization` for rewriting an artifact for a concrete envelope.
574
+ - `Lowering` for moving to a lower-level execution contract.
575
+ - `Encoding` and `Decoding` for wire boundaries.
576
+ - `Rendering` for human or external presentation.
577
+
578
+ Use lowercase hyphenated identifiers for transformation contracts. These
579
+ identifiers name abstract transformation boundaries, not concrete function
580
+ names.
581
+
582
+ ### Transformation Contract Summary
583
+
584
+ | Identifier | Preferred name | Inputs | Outputs |
585
+ | --- | --- | --- | --- |
586
+ | `source-parsing` | Source Parsing | k source program | parsed AST |
587
+ | `type-code-registration` | Type-Code Registration | parsed AST | canonical type-code table |
588
+ | `partial-function-table-construction` | Partial-Function Table Construction | parsed AST, canonical type-code table | source partial-function table |
589
+ | `pattern-signature-derivation` | Pattern-Signature Derivation | source partial-function table, canonical type-code table | pattern-annotated core |
590
+ | `partial-function-canonicalization` | Partial-Function Canonicalization | pattern-annotated core | canonical partial-function aliases |
591
+ | `object-packaging` | Object Packaging | pattern-annotated core, canonical partial-function aliases | k object |
592
+ | `object-loading` | Object Loading | k object | loaded k object |
593
+ | `entry-selection` | Entry Selection | pattern-annotated core or loaded k object | entry partial function |
594
+ | `reference-evaluation` | Reference Evaluation | entry partial function, input enveloped value | output enveloped value or partial failure |
595
+ | `input-envelope-extraction` | Input-Envelope Extraction | input enveloped value | input envelope |
596
+ | `kir-p-export` | KIR-P Export | k object | KIR-P |
597
+ | `envelope-specialization` | Envelope Specialization | KIR-P or k object, input envelope | envelope-specialized KIR-P, output envelope |
598
+ | `execution-model-lowering` | Execution-Model Lowering | envelope-specialized KIR-P | lowered execution artifact |
599
+ | `lowered-evaluation` | Lowered Evaluation | lowered execution artifact, input enveloped value | output enveloped value or partial failure |
600
+ | `target-lowering` | Target Lowering | lowered execution artifact | target artifact |
601
+ | `target-execution` | Target Execution | target artifact, input enveloped value or input wire stream | output enveloped value or output wire stream |
602
+ | `wire-decoding` | Wire Decoding | input wire stream | input enveloped value |
603
+ | `wire-encoding` | Wire Encoding | output enveloped value | output wire stream |
604
+ | `external-rendering` | External Rendering | output wire stream or output enveloped value | external text, JSON, or host value |
605
+
606
+ ### Abstract Transformation Contracts
607
+
608
+ Each transformation should be implementable and testable as an independent
609
+ contract. A concrete compiler may combine several transformations for
610
+ performance or convenience, but tests should still be able to isolate these
611
+ boundaries.
612
+
613
+ #### Source Parsing (`source-parsing`)
614
+
615
+ Inputs:
616
+
617
+ - k source program.
618
+
619
+ Outputs:
620
+
621
+ - parsed AST.
622
+
623
+ Contract:
624
+
625
+ - Parse textual k syntax into source-shaped AST nodes.
626
+ - Preserve source names, source ranges, and source-level filter syntax.
627
+ - Do not resolve type names, derive patterns, or assign canonical hashes.
628
+ - Reject malformed syntax with a diagnostic tied to the source location when
629
+ available.
630
+
631
+ Independent tests:
632
+
633
+ - Minimal expression parses to the expected AST shape.
634
+ - Type definitions and partial-function definitions remain distinct.
635
+ - Source ranges survive for expressions and nested filters.
636
+ - Invalid syntax fails before any later compiler transformation runs.
637
+
638
+ #### Type-Code Registration (`type-code-registration`)
639
+
640
+ Inputs:
641
+
642
+ - parsed AST.
643
+
644
+ Outputs:
645
+
646
+ - canonical type-code table.
647
+ - source-name to type-code alias map.
648
+
649
+ Contract:
650
+
651
+ - Convert source type definitions into canonical type-code identities.
652
+ - Resolve structural type definitions into stable hashable representations.
653
+ - Preserve enough alias information to report source names in diagnostics and
654
+ metadata.
655
+ - Do not derive partial-function pattern signatures or execute partial-function
656
+ bodies.
657
+
658
+ Independent tests:
659
+
660
+ - Structurally equivalent type definitions produce the same canonical type code.
661
+ - Distinct product/union structures produce distinct type codes.
662
+ - Recursive type definitions produce stable identities.
663
+ - Undefined type references fail at this boundary, not during later lowering.
664
+
665
+ #### Partial-Function Table Construction (`partial-function-table-construction`)
666
+
667
+ Inputs:
668
+
669
+ - parsed AST.
670
+ - canonical type-code table.
671
+
672
+ Outputs:
673
+
674
+ - source partial-function table.
675
+
676
+ Contract:
677
+
678
+ - Collect named partial-function definitions and the entry expression.
679
+ - Rewrite type references in expressions to canonical type-code names where the
680
+ source syntax explicitly names a type.
681
+ - Preserve source partial-function names before canonicalization.
682
+ - Preserve expression structure; do not add inferred boundary guards or lowering
683
+ instructions.
684
+
685
+ Independent tests:
686
+
687
+ - Named definitions and the entry expression are both present.
688
+ - References to known source types resolve through the type-code table.
689
+ - References to unknown partial functions remain diagnosable.
690
+ - Expression ordering in composition and ordered union is preserved.
691
+
692
+ #### Pattern-Signature Derivation (`pattern-signature-derivation`)
693
+
694
+ Inputs:
695
+
696
+ - source partial-function table.
697
+ - canonical type-code table.
698
+
699
+ Outputs:
700
+
701
+ - pattern-annotated core.
702
+
703
+ Contract:
704
+
705
+ - Build one type-pattern graph for each partial function.
706
+ - Attach a pattern signature to each expression node.
707
+ - Convert source filter syntax into type-pattern graph nodes.
708
+ - Propagate constraints through composition, projection, product construction,
709
+ ordered union, references, and type-code expressions.
710
+ - Record type-derivation convergence status.
711
+
712
+ Independent tests:
713
+
714
+ - Identity has the same input and output pattern representative.
715
+ - Composition unifies each stage output with the next stage input.
716
+ - Product construction derives a closed product output pattern.
717
+ - Projection derives an open product or open union input pattern as appropriate.
718
+ - Pattern guards constrain input and output to the same pattern.
719
+ - Recursive partial functions report converged or not-converged status
720
+ deterministically.
721
+
722
+ #### Partial-Function Canonicalization (`partial-function-canonicalization`)
723
+
724
+ Inputs:
725
+
726
+ - pattern-annotated core.
727
+
728
+ Outputs:
729
+
730
+ - canonical partial-function aliases.
731
+ - updated canonical references.
732
+
733
+ Contract:
734
+
735
+ - Compute stable canonical representations for partial functions.
736
+ - Assign canonical hash aliases after dependency and convergence analysis.
737
+ - Keep source aliases for diagnostics, metadata, and decompilation.
738
+ - Preserve semantics: canonicalization must not change evaluation results.
739
+
740
+ Independent tests:
741
+
742
+ - Whitespace and source alias changes do not change canonical aliases.
743
+ - Semantically different bodies produce different canonical aliases.
744
+ - Mutually recursive groups canonicalize deterministically.
745
+ - References inside canonicalized definitions point at the intended canonical
746
+ partial functions.
747
+
748
+ #### Object Packaging (`object-packaging`)
749
+
750
+ Inputs:
751
+
752
+ - pattern-annotated core.
753
+ - canonical partial-function aliases.
754
+
755
+ Outputs:
756
+
757
+ - k object.
758
+
759
+ Contract:
760
+
761
+ - Produce a k object artifact from the compiled semantic object.
762
+ - Preserve canonical type codes, partial functions, aliases, metadata,
763
+ type-pattern graphs, and type-derivation status.
764
+ - For executable objects, preserve the entry partial function.
765
+ - For library objects, preserve exported reusable definitions without an entry.
766
+ - Do not require an input enveloped value.
767
+
768
+ Independent tests:
769
+
770
+ - Packaged executable objects retain a main entry.
771
+ - Packaged libraries have no main entry.
772
+ - Object round trips preserve type-pattern graph structure and aliases.
773
+ - Unreachable semantic material is either intentionally pruned or intentionally
774
+ preserved according to the object contract.
775
+
776
+ #### Object Loading (`object-loading`)
777
+
778
+ Inputs:
779
+
780
+ - k object.
781
+
782
+ Outputs:
783
+
784
+ - loaded k object.
785
+
786
+ Contract:
787
+
788
+ - Reconstruct a usable semantic object from a k object artifact.
789
+ - Preserve the artifact's semantic content exactly.
790
+ - Make the object usable by reference evaluation, KIR-P export, and lowering
791
+ preparation.
792
+ - Do not re-run source parsing or type derivation.
793
+
794
+ Independent tests:
795
+
796
+ - Loading followed by packaging is semantically stable.
797
+ - Pattern graph representatives are usable after loading.
798
+ - Loaded objects evaluate the same as freshly compiled objects.
799
+ - Malformed object artifacts fail at loading.
800
+
801
+ #### Entry Selection (`entry-selection`)
802
+
803
+ Inputs:
804
+
805
+ - pattern-annotated core or loaded k object.
806
+ - optional requested partial-function name.
807
+
808
+ Outputs:
809
+
810
+ - entry partial function.
811
+
812
+ Contract:
813
+
814
+ - Resolve the entry partial function to execute or specialize.
815
+ - Default to the executable object's main partial function when no explicit name is
816
+ provided.
817
+ - Resolve source aliases to canonical names where needed.
818
+ - Fail if the requested entry cannot be found or if no entry exists.
819
+
820
+ Independent tests:
821
+
822
+ - Executable objects select their main partial function by default.
823
+ - Explicit source aliases resolve to the intended canonical partial function.
824
+ - Library objects require an explicit exported partial function.
825
+ - Missing entries produce targeted diagnostics.
826
+
827
+ #### Reference Evaluation (`reference-evaluation`)
828
+
829
+ Inputs:
830
+
831
+ - entry partial function.
832
+ - input enveloped value.
833
+
834
+ Outputs:
835
+
836
+ - output enveloped value, or partial failure.
837
+
838
+ Contract:
839
+
840
+ - Execute the envelope-aware reference semantics.
841
+ - Intersect input envelopes with static input patterns.
842
+ - Propagate subpatterns through projections.
843
+ - Build output envelopes for product and variant construction.
844
+ - Return partial failure when the partial function is undefined for a conforming input.
845
+ - Signal a type error when an input envelope contradicts the static input pattern constraint.
846
+ - Throw diagnostics only for malformed artifacts or fatal system errors.
847
+
848
+ Independent tests:
849
+
850
+ - Identity returns the same enveloped value.
851
+ - Projection returns the correct child with the correct sub-envelope.
852
+ - Product construction builds the expected output tree and envelope.
853
+ - Ordered union tries later branches only after earlier partial failure.
854
+ - Input envelope mismatch triggers a type error rather than partial failure.
855
+ - Pattern guard failure on a conforming value is reported as partial failure.
856
+
857
+ #### Input-Envelope Extraction (`input-envelope-extraction`)
858
+
859
+ Inputs:
860
+
861
+ - input enveloped value.
862
+
863
+ Outputs:
864
+
865
+ - input envelope.
866
+
867
+ Contract:
868
+
869
+ - Read the envelope carried by the input value.
870
+ - Do not inspect or transform the value tree except as needed to validate that
871
+ the envelope exists and is well formed.
872
+ - Preserve the envelope representation expected by specialization and lowering
873
+ transformations.
874
+
875
+ Independent tests:
876
+
877
+ - Extraction returns the exact pattern attached to the input value.
878
+ - Missing envelopes fail when the caller requires specialization.
879
+ - Malformed pattern graphs are rejected before lowering preparation.
880
+
881
+ #### KIR-P Export (`kir-p-export`)
882
+
883
+ Inputs:
884
+
885
+ - k object.
886
+
887
+ Outputs:
888
+
889
+ - KIR-P.
890
+
891
+ Contract:
892
+
893
+ - Produce the portable polymorphic view of a k object.
894
+ - Normalize expression op names into the KIR-P vocabulary.
895
+ - Convert per-partial-function pattern graphs to dense local node IDs.
896
+ - Preserve type codes, aliases, metadata, and type-derivation status needed by
897
+ consumers.
898
+ - Do not specialize for any input envelope.
899
+ - Do not make consumers depend on any particular object encoding.
900
+
901
+ Independent tests:
902
+
903
+ - Export is deterministic for the same object.
904
+ - Pattern graph IDs are dense and local to each partial function.
905
+ - KIR-P expression opcodes are from the closed KIR-P vocabulary.
906
+ - KIR-P export preserves enough information for reference/lowered conformance
907
+ tests.
908
+
909
+ #### Envelope Specialization (`envelope-specialization`)
910
+
911
+ Inputs:
912
+
913
+ - KIR-P or k object.
914
+ - input envelope.
915
+ - optional entry partial-function name.
916
+
917
+ Outputs:
918
+
919
+ - envelope-specialized KIR-P.
920
+ - output envelope.
921
+
922
+ Contract:
923
+
924
+ - Specialize the entry partial function as if guarded by the input envelope.
925
+ - Re-run pattern-signature derivation for the specialized entry context.
926
+ - Preserve ordinary KIR-P shape in the emitted artifact.
927
+ - Derive the output envelope for that invocation.
928
+ - Keep call-site worklists, cache keys, and instance metadata internal unless a
929
+ later artifact contract explicitly exposes them.
930
+
931
+ Independent tests:
932
+
933
+ - Specialization of `P f` matches compiling an entry expression guarded by `P`.
934
+ - Output envelope matches reference execution for representative inputs.
935
+ - The emitted artifact remains ordinary KIR-P, not a separate wrapper format.
936
+ - A helper used under two input envelopes can specialize differently without
937
+ corrupting the unspecialized object.
938
+
939
+ #### Execution-Model Lowering (`execution-model-lowering`)
940
+
941
+ Inputs:
942
+
943
+ - envelope-specialized KIR-P.
944
+
945
+ Outputs:
946
+
947
+ - lowered execution artifact.
948
+
949
+ Contract:
950
+
951
+ - Lower KIR-P partial-function bodies to an explicit execution model.
952
+ - Preserve input and output pattern metadata on the entry function.
953
+ - Represent partial failure explicitly.
954
+ - Lower pattern guards, type checks, projections, constructors, calls, ordered
955
+ unions, and sequencing according to the lowered execution semantics.
956
+ - Enforce artifact eligibility rules such as accepted input-envelope shape at
957
+ the packaging boundary.
958
+
959
+ Independent tests:
960
+
961
+ - Identity lowers to a return of the input.
962
+ - Pattern guards lower to guard instructions or proven redundant checks.
963
+ - Product and variant operations preserve field/tag semantics.
964
+ - Ordered union preserves branch order and rollback/failure behavior.
965
+ - Non-eligible input envelopes are rejected with a targeted diagnostic when a
966
+ lowered artifact requires a narrower envelope.
967
+
968
+ #### Lowered Evaluation (`lowered-evaluation`)
969
+
970
+ Inputs:
971
+
972
+ - lowered execution artifact.
973
+ - input enveloped value.
974
+
975
+ Outputs:
976
+
977
+ - output enveloped value, or partial failure.
978
+
979
+ Contract:
980
+
981
+ - Execute the lowered execution model.
982
+ - Validate the input envelope against the artifact input pattern, signaling a runtime type error on mismatch.
983
+ - Return output values under the artifact output pattern.
984
+ - Preserve partial failure as an ordinary execution result when undefined for a conforming input.
985
+ - Match reference evaluation for the specialized artifact.
986
+
987
+ Independent tests:
988
+
989
+ - Lowered evaluation matches reference evaluation on conformance fixtures.
990
+ - Input envelope mismatch aborts with a runtime type error before producing output.
991
+ - Recursive or repeated calls preserve deterministic results.
992
+ - Partial failure matches reference undefinedness.
993
+
994
+ #### Target Lowering (`target-lowering`)
995
+
996
+ Inputs:
997
+
998
+ - lowered execution artifact.
999
+
1000
+ Outputs:
1001
+
1002
+ - target artifact.
1003
+
1004
+ Contract:
1005
+
1006
+ - Lower the lowered execution artifact to a concrete execution target.
1007
+ - Preserve the input and output envelope contract at the target boundary.
1008
+ - Emit explicit success/failure result status.
1009
+ - Reject unsupported operations with diagnostics rather than silently changing
1010
+ semantics.
1011
+
1012
+ Independent tests:
1013
+
1014
+ - Generated target artifact has the expected entry shape.
1015
+ - Supported lowered operations lower without fallback to unrelated generic paths.
1016
+ - Unsupported operations fail predictably.
1017
+ - Target output matches reference output for supported fixtures.
1018
+
1019
+ #### Target Execution (`target-execution`)
1020
+
1021
+ Inputs:
1022
+
1023
+ - target artifact.
1024
+ - input enveloped value or input wire stream.
1025
+
1026
+ Outputs:
1027
+
1028
+ - output enveloped value or output wire stream.
1029
+
1030
+ Contract:
1031
+
1032
+ - Run the target artifact for a specialized partial function.
1033
+ - Validate or decode the input envelope at the boundary, triggering a runtime type error on mismatch.
1034
+ - Emit the compiled output envelope with the output value.
1035
+ - Preserve partial failure according to the target execution contract.
1036
+
1037
+ Independent tests:
1038
+
1039
+ - Target execution matches reference output on supported fixtures.
1040
+ - Input envelope mismatch triggers a runtime type error (e.g. exit code 5) rather than partial failure.
1041
+ - Wire output includes the expected output envelope.
1042
+ - Persistent and one-shot runners produce the same results.
1043
+ - Partial failure is distinguishable from process, system, or type-error failure.
1044
+
1045
+ #### Wire Decoding (`wire-decoding`)
1046
+
1047
+ Inputs:
1048
+
1049
+ - input wire stream.
1050
+
1051
+ Outputs:
1052
+
1053
+ - input enveloped value.
1054
+
1055
+ Contract:
1056
+
1057
+ - Decode the leading envelope representation.
1058
+ - Decode the following value bits under that envelope.
1059
+ - Attach the decoded envelope to the execution value.
1060
+ - Reject malformed streams, invalid envelopes, and non-zero padding according to
1061
+ the wire format.
1062
+
1063
+ Independent tests:
1064
+
1065
+ - Encoding followed by decoding returns the same enveloped value.
1066
+ - Decoding rejects truncated streams.
1067
+ - Decoding rejects value bits that are impossible under the decoded envelope.
1068
+ - The decoded value carries the decoded envelope, not a recomputed substitute.
1069
+
1070
+ #### Wire Encoding (`wire-encoding`)
1071
+
1072
+ Inputs:
1073
+
1074
+ - output enveloped value.
1075
+ - optional explicit output envelope.
1076
+
1077
+ Outputs:
1078
+
1079
+ - output wire stream.
1080
+
1081
+ Contract:
1082
+
1083
+ - Choose the explicit envelope when supplied, otherwise use the value envelope.
1084
+ - Validate or coerce the value against the chosen envelope according to the codec
1085
+ contract.
1086
+ - Write the encoded envelope followed by the value bits encoded under it.
1087
+ - Produce deterministic bytes for the same value and envelope.
1088
+
1089
+ Independent tests:
1090
+
1091
+ - Decoding the encoded stream returns the original enveloped value.
1092
+ - Explicit envelopes override carried envelopes only when compatible.
1093
+ - Incompatible explicit envelopes fail before bytes are emitted.
1094
+ - Equivalent values under equivalent envelopes produce stable byte streams.
1095
+
1096
+ #### External Rendering (`external-rendering`)
1097
+
1098
+ Inputs:
1099
+
1100
+ - output wire stream or output enveloped value.
1101
+
1102
+ Outputs:
1103
+
1104
+ - external text, JSON, or host value.
1105
+
1106
+ Contract:
1107
+
1108
+ - Render values for humans or external systems.
1109
+ - Preserve envelope visibility when debug or inspection mode requests it.
1110
+ - Avoid presenting rendered tree equality as proof of envelope equality.
1111
+ - Do not feed rendered output back into target parity tests unless the envelope
1112
+ is also checked.
1113
+
1114
+ Independent tests:
1115
+
1116
+ - Rendering a known enveloped value produces expected text/JSON.
1117
+ - Debug rendering includes the envelope.
1118
+ - Two values with the same tree and different envelopes can be distinguished in
1119
+ inspection mode.
1120
+ - Rendering errors do not imply execution errors.
1121
+
1122
+ ### Transformation Notes
1123
+
1124
+ - Compilation transformations do not need an input value until
1125
+ envelope-specialization or execution.
1126
+ - Reference execution can use the pattern-annotated core directly; target
1127
+ execution goes through KIR-P and a lowered execution artifact.
1128
+ - Envelope-specialization is not a new source language feature. It is a compiler
1129
+ transformation driven by the input envelope.
1130
+ - The output envelope is an artifact of execution or specialization. It must be
1131
+ preserved at binary boundaries even when printed value trees look identical.
1132
+ - Partial failure is an ordinary result of execution transformations. It should
1133
+ not be modeled as a malformed artifact.
1134
+
1135
+ ## Recommended Pipeline Vocabulary
1136
+
1137
+ Use this pipeline in high-level docs:
1138
+
1139
+ ```text
1140
+ k source
1141
+ -> parsed AST
1142
+ -> pattern-annotated core
1143
+ -> k object
1144
+ -> KIR-P
1145
+ -> envelope-specialized KIR-P
1146
+ -> lowered execution artifact
1147
+ -> target artifact
1148
+ ```
1149
+
1150
+ For the execution and codec boundary:
1151
+
1152
+ ```text
1153
+ wire stream <-> enveloped value <-> partial function <-> enveloped value <-> wire stream
1154
+ ```
1155
+
1156
+ ## Terminology Rules
1157
+
1158
+ These rules are normative for this specification:
1159
+
1160
+ - Use "partial function" as the dominant public term. Avoid "partial
1161
+ transform"; it can read as unfinished rather than undefined for some inputs.
1162
+ - Keep "pattern-annotated core" as the name for the normalized,
1163
+ anonymized, pattern-annotated AST stage.
1164
+ - Use "envelope-specialized KIR-P" for KIR-P rebuilt under a concrete input
1165
+ envelope.
1166
+ - Use "type code" for the canonical representation of a type, and "type" for
1167
+ the semantic concept.
1168
+ - Use "envelope" for the type pattern carried with a value at an execution or
1169
+ wire boundary.
1170
+ - Use "pattern guard" for the semantic operation that constrains an envelope;
1171
+ reserve "filter expression" for source syntax when the syntax itself is being
1172
+ discussed.