@fraczak/k 6.10.6 → 7.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/CLAUDE.md +1 -1
  3. package/DOCS/BIG_PICTURE.md +156 -172
  4. package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +3 -3
  5. package/DOCS/CODEC_ARCHITECTURE.md +2 -2
  6. package/DOCS/DICTIONARY.md +243 -250
  7. package/DOCS/DIVERGENCE_DETECTION.md +0 -1
  8. package/DOCS/FILE_FORMATS.md +5 -5
  9. package/DOCS/KIR_SPECIALIZATION_DESIGN.md +1 -1
  10. package/DOCS/OBJECT_FILE_AND_PATTERN.md +12 -12
  11. package/DOCS/PATTERNS.md +17 -17
  12. package/DOCS/PATTERNS_AND_TYPED_VALUES.md +1 -1
  13. package/DOCS/REPL.md +9 -19
  14. package/DOCS/TYPE_DERIVATION.md +2 -8
  15. package/DOCS/WEB_REPL_REQUIREMENTS.md +8 -8
  16. package/DOCS/book/01-introduction.md +8 -11
  17. package/DOCS/book/02-syntax-and-values.md +19 -20
  18. package/DOCS/book/03-types-as-finite-automata.md +42 -43
  19. package/DOCS/book/04-partial-functions-and-composition.md +2 -3
  20. package/DOCS/book/05-typing-filters-and-normalization.md +44 -46
  21. package/DOCS/book/06-values-in-memory.md +10 -10
  22. package/DOCS/book/08-operational-semantics-and-execution.md +21 -26
  23. package/DOCS/book/09-compiler-architecture.md +11 -11
  24. package/DOCS/book/10-from-ast-to-intermediate-representation.md +6 -7
  25. package/DOCS/book/14-canonical-serialization.md +2 -2
  26. package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +17 -17
  27. package/DOCS/book.md +5 -5
  28. package/Examples/arithmetics.k +49 -153
  29. package/Examples/bnat.k +13 -13
  30. package/Examples/byte.k +2 -50
  31. package/Examples/ieee.k +408 -423
  32. package/Examples/karatsuba-mult.k +30 -34
  33. package/Examples/nat.k +5 -6
  34. package/README.md +54 -49
  35. package/TODO.md +13 -18
  36. package/backend-api.mjs +9 -1
  37. package/backends/llvm/scripts/run-tests.mjs +5 -0
  38. package/backends/llvm/src/kvm2llvm.mjs +2 -1
  39. package/backends/llvm/src/llvm.mjs +18 -25
  40. package/backends/llvm/tests/integration.sh +2 -2
  41. package/backends/llvm/tests/test-llvm.mjs +3 -3
  42. package/backends/llvm/tests/test-open-product-misalignment.mjs +140 -0
  43. package/backends/wasm/demo_ieee.mjs +2 -3
  44. package/backends/wasm/runtime.wat +157 -5
  45. package/backends/wasm/src/kvm2wasm.mjs +57 -34
  46. package/backends/wasm/src/wasm.mjs +38 -17
  47. package/backends/wasm/tests/perf-support.mjs +4 -4
  48. package/backends/wasm/tests/test-wasm.mjs +13 -14
  49. package/browser/vfs-data.mjs +12 -12
  50. package/codecs/BINARY_FORMAT.md +4 -4
  51. package/codecs/POLYMORPHIC_BINARY_FORMAT.md +4 -4
  52. package/codecs/README.md +6 -6
  53. package/codecs/ieee.mjs +15 -11
  54. package/codecs/int.mjs +9 -5
  55. package/codecs/string-codec.mjs +46 -47
  56. package/core.k +123 -95
  57. package/kir.mjs +213 -17
  58. package/object.mjs +0 -7
  59. package/package.json +5 -2
  60. package/parser.jison +2 -41
  61. package/parser.mjs +55 -95
  62. package/pretty.mjs +11 -4
  63. package/repl.mjs +8 -63
  64. package/scripts/build-repl-html.mjs +8 -38
  65. package/tests/code-derivation/blowup.mjs +2 -4
  66. package/tests/code-derivation/bnat.mjs +2 -3
  67. package/tests/code-derivation/dot.mjs +5 -11
  68. package/tests/code-derivation/nat.mjs +2 -2
  69. package/tests/code-derivation/not-not.mjs +6 -6
  70. package/tests/code-derivation/pattern-envelope.mjs +4 -6
  71. package/tests/code-derivation/product.mjs +3 -6
  72. package/tests/code-derivation/rlz.mjs +4 -5
  73. package/tests/test-codecs.mjs +1 -1
  74. package/tests/test-fingerprint.mjs +36 -41
  75. package/tests/test-hash-normalization.mjs +4 -6
  76. package/tests/test-ieee-arithmetic.mjs +6 -7
  77. package/tests/test-k-object.mjs +11 -15
  78. package/tests/test-kir.mjs +0 -1
  79. package/tests/test-kvm-polymorphic.mjs +1 -2
  80. package/tests/test-kvm.mjs +8 -11
  81. package/tests/test-repl-html.mjs +20 -2
  82. package/tests/test-repl.mjs +48 -53
  83. package/tests/test.mjs +2 -3
@@ -14,75 +14,79 @@ target execution.
14
14
  - Use semantic names for all externally visible concepts.
15
15
  - Distinguish a concept from its serialized representation.
16
16
  - Distinguish source syntax from the semantic object represented by that syntax.
17
- - Distinguish a mathematical function from the compiler artifacts used to
17
+ - Distinguish a mathematical relation from the compiler artifacts used to
18
18
  compile or execute it.
19
- - Treat "pattern" as a type-context concept, not as ordinary value-pattern
20
- matching over raw trees.
19
+ - Use `schema` for data shapes (algebraic tree automata), `contract` for
20
+ relational signatures, `filter` for partial identity guards and inductive
21
+ witnesses, and `(schema, payload)` for framed runtime values.
21
22
 
22
23
  ## Source-Level Concepts
23
24
 
24
25
  ### k Source
25
26
 
26
- `k source` is the textual language read by the parser. It can define types,
27
- partial functions, and an entry expression.
27
+ `k source` is the textual language read by the parser. It defines relations
28
+ (including schemas defined as partial identities/filters) and an optional
29
+ entry expression.
28
30
 
29
31
  Use this term for source text before compiler passes have resolved names,
30
- canonicalized types, or derived pattern information.
32
+ canonicalized schemas, or derived relational contracts.
31
33
 
32
34
  ### Expression
33
35
 
34
36
  An `expression` is a source-level program fragment that denotes a partial
35
- function over enveloped values.
37
+ relation over framed values.
36
38
 
37
39
  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
+ product construction, ordered union, filters, and references to named
41
+ relations or intrinsics.
40
42
 
41
- ### Partial Function
43
+ ### Partial Relation
42
44
 
43
- A partial function maps an input enveloped value to an output enveloped value
45
+ A `partial relation` maps an input framed payload to an output framed payload
44
46
  when it is defined. If it is not defined for the input, execution fails normally.
45
47
  This failure is part of the language semantics, not a system exception.
46
48
 
47
- Mathematically, a k program denotes a deterministic partial function.
49
+ Mathematically, a k program denotes a deterministic partial relation
50
+ (partial function).
48
51
 
49
- ### Type
52
+ ### Schema
50
53
 
51
- A `type` describes a set of value trees accepted by an algebraic shape. Types are
52
- built from products and tagged unions.
54
+ A `schema` describes a set of labeled tree payloads accepted by a finite tree
55
+ automaton. Schemas are built from products and tagged unions. A schema can be
56
+ *closed* (all constructors determined) or *open* (containing schema parameters
57
+ $X, Y$).
53
58
 
54
- Use "type" for the semantic concept. Avoid using "code" by itself in public docs
55
- when the meaning is type.
59
+ Use "schema" for the semantic concept. Avoid using "code" or "type pattern" by
60
+ itself in public docs when the meaning is a schema.
56
61
 
57
- ### Type Code
62
+ ### Canonical Schema
58
63
 
59
- A `type code` is the canonical, hashable representation of a type.
64
+ A `canonical schema` is the deterministic, hashable normal form of a schema
65
+ automaton. Its canonical textual syntax is its primary universal identity in the
66
+ schema registry; its `@hash` is a fixed-width indexing digest.
60
67
 
61
- Use "type" for the semantic concept and "type code" for its canonical
62
- representation.
68
+ ### Filter
63
69
 
64
- ### Pattern Guard
70
+ A `filter` is an expression `?schema` that constrains the input payload to a
71
+ schema and otherwise behaves like identity.
65
72
 
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.
73
+ In recursive relations, filters also serve as **inductive termination witnesses**,
74
+ ensuring contract derivation converges.
71
75
 
72
76
  ### Canonical Content-Addressed Hash (`@hash`)
73
77
 
74
78
  A `canonical content-addressed hash` is the immutable, unique identifier
75
79
  derived from the normalized AST of a relation or the minimal deterministic
76
- tree automaton of a type.
80
+ tree automaton of a schema.
77
81
 
78
82
  In `k`, there is no global symbol namespace. Every compiled relation and
79
- type code is identified globally and uniquely by its `@hash`.
83
+ schema is identified globally and uniquely by its `@hash`.
80
84
 
81
85
  ### Local Alias
82
86
 
83
87
  A `local alias` is a human-readable identifier (such as `plus`, `int`,
84
88
  or `5`) mapped to a canonical hash in object metadata (`relAlias`,
85
- `typeAliases`).
89
+ `schemaAliases`).
86
90
 
87
91
  Aliases are local to their compilation scope. When loading libraries
88
92
  with `--lib`, canonical definitions are available by their `@hash`, but
@@ -93,120 +97,109 @@ unless explicitly mapped with `--export`.
93
97
 
94
98
  In `k`, there are no primitive numerical types or built-in integer
95
99
  literals. Numerical identifiers such as `5` or `10` are nullary
96
- relations (constants) mapping the unit `${}` to bit trees of type
97
- `${} -> $bits`. Operations on signed integers (`$int`) require
100
+ relations (constants) mapping the unit `{}` to bit trees of contract
101
+ `{} -> bits`. Operations on signed integers (`int`) require
98
102
  converting bit trees via the `int` constructor (`5 int`).
99
103
 
100
- ## Values, Types, And Patterns
101
-
102
- ### Value Tree
104
+ ## Values, Schemas, And Contracts
103
105
 
104
- A `value tree` is the product/variant tree part of a value during execution.
106
+ ### Payload Tree (or Payload)
105
107
 
106
- By itself, a value tree is not enough to recover the semantic value at a binary
107
- or polymorphic boundary. The same tree can be meaningful under different type
108
- contexts.
108
+ A `payload tree` (or `payload`) is the product/variant tree part of a value during
109
+ execution.
109
110
 
110
- ### Type Pattern
111
+ By itself, a raw payload tree is not self-describing at a binary or polymorphic
112
+ boundary. The same tree is meaningful only when framed by a schema.
111
113
 
112
- A `type pattern` describes a set of possible types. It is the semantic object
113
- represented by source filter syntax and by the pattern graphs carried through
114
- compilation and execution.
114
+ ### Schema
115
115
 
116
- The short name `pattern` is acceptable once the context is clear. Use "type
117
- pattern" when introducing the concept or when there is risk of confusion with
118
- ordinary value matching.
116
+ A `schema` describes a set of possible payload trees accepted by an algebraic
117
+ automaton. It is the semantic object represented by source filter syntax and by the
118
+ schema graphs carried through compilation and execution.
119
119
 
120
- Examples of type pattern kinds include:
120
+ Examples of schema kinds include:
121
121
 
122
- - any type;
122
+ - unconstrained schema parameter (`X`, `?X`);
123
123
  - open product;
124
124
  - closed product;
125
125
  - open union;
126
126
  - closed union;
127
- - singleton type pattern derived from a concrete type code.
128
-
129
- ### Pattern Graph
130
-
131
- A `pattern graph` is the graph representation of a type pattern. Nodes describe
132
- pattern kind and openness. Edges describe required product fields or union tags.
133
-
134
- Pattern graph node identity matters: if two positions point to the same pattern
135
- node, they must share the same type context.
127
+ - closed schema derived from concrete definitions.
136
128
 
137
- Use "pattern graph" for the representation, and "type pattern" for the semantic
138
- constraint it represents.
129
+ ### Schema Graph
139
130
 
140
- ### Envelope
131
+ A `schema graph` is the graph representation of a schema. Nodes describe
132
+ schema kind and openness. Edges describe required product fields or union tags.
141
133
 
142
- An `envelope` is the type pattern carried with a value at an execution or wire
143
- boundary.
134
+ Schema graph node identity matters: if two positions point to the same schema
135
+ node, they share the exact same schema structure.
144
136
 
145
- The envelope describes the type context in which the value tree should be
146
- decoded, checked, projected, transformed, or encoded. The spelling should always
147
- be `envelope`.
137
+ Use "schema graph" for the representation, and "schema" for the semantic
138
+ language it defines.
148
139
 
149
- ### Enveloped Value
140
+ ### Framed Value `(schema, payload)`
150
141
 
151
- An `enveloped value` is a pair:
142
+ A `framed value` (formerly called an enveloped value) is a pair:
152
143
 
153
144
  ```text
154
- (value tree, envelope)
145
+ (schema, payload)
155
146
  ```
156
147
 
157
- Structural operations propagate or refine the envelope:
148
+ Structural operations propagate or refine the framing schema:
158
149
 
159
- - projection selects a subpattern;
160
- - product construction builds a closed product envelope from field envelopes;
161
- - variant introduction builds an open union envelope for the introduced tag;
162
- - pattern guards intersect the active envelope with a static type pattern.
150
+ - projection selects a subschema;
151
+ - product construction builds a closed product schema from field schemas;
152
+ - variant introduction builds an open union schema for the introduced tag;
153
+ - filters intersect the active schema with a static schema constraint.
163
154
 
164
- The envelope is part of the semantic value at compilation and execution
165
- boundaries. A value tree without an envelope is incomplete at those boundaries.
155
+ The schema is part of the semantic value at compilation and execution
156
+ boundaries. A payload tree without a framing schema is incomplete at those boundaries.
166
157
 
167
- ### Pattern Signature
158
+ ### Contract
168
159
 
169
- A `pattern signature` is the input/output type-pattern pair attached to an
170
- expression or partial function:
160
+ A `contract` is the derived $(input, output)$ linked schema graph attached to an
161
+ expression or relation:
171
162
 
172
163
  ```text
173
- input pattern -> output pattern
164
+ input schema -> output schema
174
165
  ```
175
166
 
176
- Pattern signatures are compiler facts. They constrain valid inputs and describe
177
- the envelope shape produced when evaluation succeeds.
167
+ Contracts are compiler facts. Unlike independent types, a contract is a single
168
+ connected schema graph where shared vertices ($=X$) prove structure preservation:
169
+ input substructures flow through to the output without destruction or rebuilding.
170
+ Contracts guide diagnostics, REPL output, binary serialization, and optimizations.
178
171
 
179
172
  ### Domain
180
173
 
181
174
  Use "domain" carefully.
182
175
 
183
- The input pattern of a partial function is a static type-context constraint, not
184
- necessarily the exact mathematical domain of the function. A function may still
185
- be undefined for some enveloped values that fit its input pattern.
176
+ The input schema of a relation is a static constraint, not
177
+ necessarily the exact mathematical domain of the relation. A relation may still
178
+ be undefined for some payload trees that fit its input schema.
186
179
 
187
180
  Prefer:
188
181
 
189
- - "input pattern";
190
- - "accepted envelope";
182
+ - "input schema";
183
+ - "accepted schema";
191
184
  - "static input constraint".
192
185
 
193
186
  Use "domain" only when referring to the actual set of inputs for which the
194
- partial function is defined.
187
+ partial relation is defined.
195
188
 
196
189
  ## Serialization And Boundaries
197
190
 
198
191
  ### Wire Stream
199
192
 
200
- A `wire stream` is the binary transport representation of an enveloped value.
193
+ A `wire stream` is the binary transport representation of a framed value `(schema, payload)`.
201
194
 
202
195
  The active format is:
203
196
 
204
197
  ```text
205
- encoded envelope, followed by value bits encoded under that envelope
198
+ encoded schema, followed by payload bits encoded under that schema
206
199
  ```
207
200
 
208
- Use "wire stream" or "pattern+value wire stream" for external transport
209
- boundaries. Use "enveloped value" for the semantic object used during
201
+ Use "wire stream" or "schema+payload wire stream" for external transport
202
+ boundaries. Use "framed value" for the semantic object used during
210
203
  execution.
211
204
 
212
205
  ### Codec
@@ -214,26 +207,26 @@ execution.
214
207
  A `codec` converts between an external representation and a k wire stream.
215
208
 
216
209
  Examples include the unit, JSON, integer, IEEE, UTF-8, and UTF-16 codecs. A
217
- codec must preserve the envelope/value relationship: it either supplies an
218
- envelope for parsing, decodes the envelope from the stream for printing, or
219
- works with a universal representation that can infer a suitable envelope.
210
+ codec must preserve the schema/payload relationship: it either supplies a
211
+ schema for parsing, decodes the schema from the stream for printing, or
212
+ works with a universal representation that can infer a suitable schema.
220
213
 
221
- ### Input Envelope
214
+ ### Input Schema
222
215
 
223
- An `input envelope` is the pattern carried by the input value at a program or
216
+ An `input schema` is the schema framing the input payload at a program or
224
217
  execution boundary.
225
218
 
226
- Execution targets use the input envelope to specialize or validate execution.
227
- The input envelope can be provided explicitly, derived from a type expression, or
219
+ Execution targets use the input schema to specialize or validate execution.
220
+ The input schema can be provided explicitly, derived from a filter expression, or
228
221
  decoded from a wire stream.
229
222
 
230
- ### Output Envelope
223
+ ### Output Schema
231
224
 
232
- An `output envelope` is the pattern attached to the output value after execution.
225
+ An `output schema` is the schema attached to the output payload after execution.
233
226
 
234
- A correct runner must emit both the output envelope and the output value bits.
235
- Rendered values can hide envelope differences, so conformance checks must compare
236
- envelopes whenever envelope identity matters.
227
+ A correct runner must emit both the output schema and the output payload bits.
228
+ Rendered values can hide schema differences, so conformance checks must compare
229
+ schemas whenever schema identity matters.
237
230
 
238
231
  ## Compiler Stages
239
232
 
@@ -242,43 +235,43 @@ envelopes whenever envelope identity matters.
242
235
  The `parsed AST` is the direct parser output. It keeps source-shaped expression
243
236
  nodes, source names, source ranges, and filter syntax.
244
237
 
245
- Use this term only for the representation before type-code registration,
246
- partial-function canonicalization, and pattern derivation.
238
+ Use this term only for the representation before schema registration,
239
+ relation canonicalization, and contract derivation.
247
240
 
248
- ### Pattern-Annotated Core
241
+ ### Contract-Annotated Core
249
242
 
250
- `Pattern-annotated core` is the first semantic compiler artifact after parsing.
243
+ `Contract-annotated core` is the first semantic compiler artifact after parsing.
251
244
 
252
245
  It keeps the expression structure close to k source, but it has:
253
246
 
254
- - registered canonical type codes;
255
- - resolved type references to canonical type-code names;
256
- - partial-function dependency information;
257
- - canonical aliases for partial functions;
258
- - a type-pattern graph for each partial function;
259
- - a pattern signature on each expression;
260
- - type-derivation convergence status.
247
+ - registered canonical schemas;
248
+ - resolved schema references to canonical schema names;
249
+ - relation dependency information;
250
+ - canonical aliases for relations;
251
+ - a schema graph for each relation;
252
+ - a contract on each expression;
253
+ - contract-derivation convergence status.
261
254
 
262
255
  The key product is still a k-shaped core expression graph, now annotated by
263
- type-pattern facts. This stage is a semantic artifact; it does not imply a
256
+ schema contract facts. This stage is a semantic artifact; it does not imply a
264
257
  particular serialized format.
265
258
 
266
- ### Ahead-Of-Time (AOT) Type Checking
259
+ ### Ahead-Of-Time (AOT) Contract Checking
267
260
 
268
- `Ahead-of-time (AOT) type checking` refers to the compilation phase
269
- where parsing, relation expansion, structural type derivation, and
261
+ `Ahead-of-time (AOT) contract checking` refers to the compilation phase
262
+ where parsing, relation expansion, structural contract derivation, and
270
263
  fixed-point constraint convergence are solved in advance.
271
264
 
272
265
  The resulting compiled artifacts (`.ko`, `.klib`, `.kvm`) store fully
273
- derived principal type graphs and interned type codes, allowing downstream
274
- execution or linking to skip cold-start type derivation entirely.
266
+ derived principal schema contracts and interned schemas, allowing downstream
267
+ execution or linking to skip cold-start derivation entirely.
275
268
 
276
269
  ### k Object
277
270
 
278
271
  A `k object` is a compiled semantic object.
279
272
 
280
- It stores canonical type codes, partial functions, aliases, metadata,
281
- type-pattern graphs, and type-derivation status.
273
+ It stores canonical schemas, relations, aliases, metadata,
274
+ schema graphs, and contract-derivation status.
282
275
 
283
276
  An executable object (`.ko`) has an execution entrypoint (`main:
284
277
  "__main__"`). A library object (`.klib`) contains reusable definitions
@@ -289,66 +282,66 @@ but no selected main relation (`main: null`).
289
282
  The `k` toolchain defines four primary file formats (for comprehensive
290
283
  details and workflows, see [`DOCS/FILE_FORMATS.md`](FILE_FORMATS.md)):
291
284
 
292
- - **Source File (`.k`):** Plain UTF-8 source text defining algebraic data
293
- shapes, partial functions, and expressions.
294
- - **Executable Object (`.ko`):** AOT type-checked executable artifact
295
- with binary header `KOBJ\n` and JSON payload containing `codes`, `rels`,
285
+ - **Source File (`.k`):** Plain UTF-8 source text defining algebraic
286
+ schemas, relations, and expressions.
287
+ - **Executable Object (`.ko`):** AOT contract-checked executable artifact
288
+ with binary header `KOBJ\n` and JSON payload containing schemas, relations,
296
289
  and entrypoint `main: "__main__"`. Executable directly via `k.mjs`,
297
290
  WebAssembly, or LLVM.
298
- - **Library Object (`.klib`):** AOT type-checked library container stored
291
+ - **Library Object (`.klib`):** AOT contract-checked library container stored
299
292
  as plain UTF-8 JSON without an entrypoint (`main: null`). Provides
300
- reusable pre-converged relations and type graphs for downstream linking.
293
+ reusable pre-converged relations and schema graphs for downstream linking.
301
294
  - **Polymorphic kVM Template (`.kvm`):** Register-IR bytecode template
302
- (`format: "k-vm"`, `layer: "KVM-P"`) carrying principal pattern graphs.
295
+ (`format: "k-vm"`, `layer: "KVM-P"`) carrying principal contracts.
303
296
  Specialized on-the-fly at runtime (`specializeKVM`) against concrete
304
- input envelopes $(v, P_v)$ without re-running type inference.
297
+ input schemas $(v, S_v)$ without re-running type inference.
305
298
 
306
299
  ### Stream Transformer vs. Closed Constant Relation
307
300
 
308
- - A **stream transformer** is a relation with an open domain (e.g. `{() x,
301
+ - A **stream transformer** is a relation with an open input schema (e.g. `{() x,
309
302
  5 int y} plus`) that consumes external data arriving from a binary
310
303
  stream or standard input.
311
304
  - A **closed constant relation** is a relation with no free inputs (such
312
- as `{10 int x, 5 int y} plus`). Its domain is the unit type `${}`,
305
+ as `{10 int x, 5 int y} plus`). Its input schema is the unit `{}`,
313
306
  requiring an empty unit `{}` when executed as a standalone script.
314
307
 
315
308
  ### KIR-P
316
309
 
317
310
  `KIR-P` is the portable, polymorphic K Intermediate Representation.
318
311
 
319
- It is a stable artifact contract over k objects. It preserves pattern-carrying
320
- semantics while normalizing names, expression opcodes, and pattern graph node IDs
312
+ It is a stable artifact contract over k objects. It preserves schema-carrying
313
+ semantics while normalizing names, expression opcodes, and schema graph node IDs
321
314
  so consumers do not depend on parser-shaped fields or object encoding details.
322
315
 
323
316
  Use KIR-P as the shared semantic contract between k objects and lower-level
324
317
  execution artifacts.
325
318
 
326
- ### Envelope-Specialized KIR-P
319
+ ### Schema-Specialized KIR-P
327
320
 
328
- `Envelope-specialized KIR-P` is KIR-P rebuilt for a concrete input envelope.
321
+ `Schema-specialized KIR-P` is KIR-P rebuilt for a concrete input schema.
329
322
 
330
- Envelope-specialization:
323
+ Schema-specialization:
331
324
 
332
325
  - takes a KIR-P object or k object;
333
- - takes an input envelope;
334
- - re-runs pattern derivation as if the entry expression were guarded by that
335
- envelope;
336
- - derives the corresponding output envelope;
337
- - emits ordinary KIR-P for the specialized entry partial function.
326
+ - takes an input schema;
327
+ - re-runs contract derivation as if the entry expression were filtered by that
328
+ schema;
329
+ - derives the corresponding output schema;
330
+ - emits ordinary KIR-P for the specialized entry relation.
338
331
 
339
332
  ### KIR-M
340
333
 
341
- `KIR-M` is a materialized execution layer after envelope-specialized KIR-P.
334
+ `KIR-M` is a materialized execution layer after schema-specialized KIR-P.
342
335
 
343
336
  It should contain layout and ABI decisions such as field offsets, tag dispatch,
344
- call-site specialization, and envelope-free execution assumptions.
337
+ call-site specialization, and schema-free execution assumptions.
345
338
 
346
339
  ### Lowered Execution Model
347
340
 
348
341
  A `lowered execution model` is an executable middle layer.
349
342
 
350
- It lowers KIR-P partial-function bodies into a small instruction model with
351
- explicit partial failure, pattern guards, projections, construction, calls, and
343
+ It lowers KIR-P relation bodies into a small instruction model with
344
+ explicit partial failure, filters, projections, construction, calls, and
352
345
  returns.
353
346
  It is an execution contract that a lower-level compiler or executor can
354
347
  consume. It is not defined by a particular byte encoding.
@@ -357,69 +350,69 @@ consume. It is not defined by a particular byte encoding.
357
350
 
358
351
  ### Lowered Execution Artifact
359
352
 
360
- A `lowered execution artifact` is a post-envelope-specialization execution
353
+ A `lowered execution artifact` is a post-schema-specialization execution
361
354
  artifact.
362
355
 
363
356
  It is produced by taking a source or object program, specializing it against a
364
- concrete input envelope, lowering the resulting KIR-P to a lowered execution
365
- model, and wrapping the entry metadata and functions. It is not the same thing
357
+ concrete input schema, lowering the resulting KIR-P to a lowered execution
358
+ model, and wrapping the entry metadata and relations. It is not the same thing
366
359
  as a general k object.
367
360
 
368
361
  ## Execution Concepts
369
362
 
370
- ### Envelope-Aware Execution
363
+ ### Schema-Aware Execution
371
364
 
372
- `Envelope-aware execution` carries envelopes through inner operations.
365
+ `Schema-aware execution` carries framing schemas through inner operations.
373
366
 
374
367
  This mode matches the reference execution and codec model. It is the safest
375
- semantic baseline because values keep their envelope throughout execution.
368
+ semantic baseline because values keep their schema framing throughout execution.
376
369
 
377
- ### Envelope-Free Execution
370
+ ### Schema-Free Execution
378
371
 
379
- `Envelope-free execution` uses static layout and pattern information inside the
380
- compiled function and attaches the derived output envelope only at the boundary.
372
+ `Schema-free execution` uses static layout and schema information inside the
373
+ compiled relation and attaches the derived output schema only at the boundary.
381
374
 
382
- This is valid only when envelope-specialization and type derivation prove the
383
- needed pattern contexts.
375
+ This is valid only when schema-specialization and contract derivation prove the
376
+ needed schema contexts.
384
377
 
385
378
  ### Partial Failure
386
379
 
387
- `Partial failure` is the normal result when a partial function is undefined for
388
- its input.
380
+ `Partial failure` is the normal result when a partial relation is undefined for
381
+ its input payload.
389
382
 
390
383
  In high-level execution this is represented as absence of an output value. In
391
384
  lower-level execution contracts it may be represented explicitly as a failed
392
- result. It is distinct from a `type error`, malformed artifacts, system
385
+ result. It is distinct from a `contract violation`, malformed artifacts, system
393
386
  failures, and host exceptions.
394
387
 
395
- ### Type Error
388
+ ### Contract Violation (Type Error)
396
389
 
397
- A `type error` is a semantic contradiction indicating that the type pattern of a
398
- value or expression does not conform to the expected input pattern of the
399
- partial function to which it is applied.
390
+ A `contract violation` (or `type error`) is a semantic contradiction indicating
391
+ that the schema of a payload or expression does not conform to the expected
392
+ input schema of the relation to which it is applied.
400
393
 
401
- A type error is fundamentally distinct from `partial failure`:
394
+ A contract violation is fundamentally distinct from `partial failure`:
402
395
 
403
396
  - `Partial failure` is a normal, valid semantic outcome representing mathematical
404
397
  undefinedness for an input value that nonetheless conforms to the expected
405
- input pattern (for example, failing a condition in an inner branch, which in
398
+ input schema (for example, failing a condition in an inner branch, which in
406
399
  an ordered union allows subsequent branches to be evaluated).
407
- - A `type error` represents a type-system contradiction or boundary contract
400
+ - A `contract violation` represents a contract contradiction or boundary contract
408
401
  violation. It is not an ordinary execution result and cannot be recovered by
409
402
  branching constructs like ordered union.
410
403
 
411
- A type error can occur at two distinct stages:
412
-
413
- 1. **Compilation time (Type Derivation)**: Triggered during
414
- `pattern-signature-derivation` when an expression's type pattern cannot be
415
- unified with, or does not satisfy, the required input pattern of a function
416
- it is passed to (such as disjoint types or unsatisfiable pattern guards).
417
- This causes compilation or type derivation to fail with a diagnostic.
418
- 2. **Runtime (Evaluation Boundary)**: Triggered when an `enveloped value` carries
419
- an `input envelope` that does not match or conform to the expected input
420
- pattern of the function being executed (for example, passing an incompatible
421
- envelope to an entry partial function or target executable). At runtime, a
422
- type error halts execution with a fatal diagnostic or error code (such as exit
404
+ A contract violation can occur at two distinct stages:
405
+
406
+ 1. **Compilation time (Contract Derivation)**: Triggered during
407
+ `contract-derivation` when an expression's schema cannot be
408
+ unified with, or does not satisfy, the required input schema of a relation
409
+ it is passed to (such as disjoint schemas or unsatisfiable filters).
410
+ This causes compilation or contract derivation to fail with a diagnostic.
411
+ 2. **Runtime (Evaluation Boundary)**: Triggered when a `framed value` carries
412
+ an `input schema` that does not match or conform to the expected input
413
+ schema of the relation being executed (for example, passing an incompatible
414
+ schema to an entry relation or target executable). At runtime, a
415
+ contract violation halts execution with a fatal diagnostic or error code (such as exit
423
416
  status 5 in compiled targets) rather than producing partial failure.
424
417
 
425
418
  ### Intrinsic
@@ -427,8 +420,8 @@ A type error can occur at two distinct stages:
427
420
  An `intrinsic` is a predefined operation supplied by the execution environment and
428
421
  known to the compiler.
429
422
 
430
- Intrinsics need explicit type and lowering rules. Pure identity-like intrinsics
431
- can participate in ordinary partial-function semantics. Effectful or host-only
423
+ Intrinsics need explicit schema and lowering rules. Pure identity-like intrinsics
424
+ can participate in ordinary relation semantics. Effectful or host-only
432
425
  intrinsics require explicit ordering constraints or must be excluded from pure
433
426
  lowering targets.
434
427
 
@@ -449,8 +442,8 @@ The diagrams use a Petri-net-like convention:
449
442
 
450
443
  ### Whole-System Artifact Graph
451
444
 
452
- This graph starts with a partial function definition in k syntax and an input
453
- enveloped value. It ends with an output enveloped value. Lowering paths branch
445
+ This graph starts with a relation definition in k syntax and an input
446
+ framed value `(schema, payload)`. It ends with an output framed value. Lowering paths branch
454
447
  after the k object.
455
448
 
456
449
  ```mermaid
@@ -467,8 +460,8 @@ flowchart TB
467
460
  Src["k source<br/>program"]:::artifact
468
461
  Parse["source<br/>parsing"]:::transform
469
462
  AST["parsed<br/>AST"]:::artifact
470
- Annotate["pattern-signature<br/>derivation"]:::transform
471
- Core["pattern-annotated<br/>core"]:::artifact
463
+ Annotate["contract<br/>derivation"]:::transform
464
+ Core["contract-annotated<br/>core"]:::artifact
472
465
  Package["object<br/>packaging"]:::transform
473
466
  Object["k<br/>object"]:::artifact
474
467
  Src --> Parse --> AST --> Annotate --> Core --> Package --> Object
@@ -476,18 +469,18 @@ flowchart TB
476
469
 
477
470
  subgraph Input["prepare the input value"]
478
471
  direction LR
479
- InputValue["input<br/>enveloped value"]:::value
480
- ExtractEnvelope["input-envelope<br/>extraction"]:::transform
481
- InputEnvelope["input<br/>envelope"]:::artifact
472
+ InputValue["input framed value<br/>(schema, payload)"]:::value
473
+ ExtractEnvelope["input-schema<br/>extraction"]:::transform
474
+ InputEnvelope["input<br/>schema"]:::artifact
482
475
  InputValue --> ExtractEnvelope --> InputEnvelope
483
476
  end
484
477
 
485
- subgraph Reference["reference envelope-aware execution"]
478
+ subgraph Reference["reference schema-aware execution"]
486
479
  direction LR
487
480
  Select["entry<br/>selection"]:::transform
488
- EntryFunction["entry partial<br/>function"]:::artifact
481
+ EntryFunction["entry<br/>relation"]:::artifact
489
482
  ReferenceRun["reference<br/>evaluation"]:::transform
490
- OutputValue["output<br/>enveloped value"]:::result
483
+ OutputValue["output framed value<br/>(schema, payload)"]:::result
491
484
  Select --> EntryFunction --> ReferenceRun --> OutputValue
492
485
  end
493
486
 
@@ -495,9 +488,9 @@ flowchart TB
495
488
  direction LR
496
489
  ExportKIR["KIR-P<br/>export"]:::transform
497
490
  KIRP["KIR-P"]:::artifact
498
- Specialize["envelope<br/>specialization"]:::transform
499
- SpecializedKIRP["envelope-specialized<br/>KIR-P"]:::artifact
500
- OutputEnvelope["output<br/>envelope"]:::artifact
491
+ Specialize["schema<br/>specialization"]:::transform
492
+ SpecializedKIRP["schema-specialized<br/>KIR-P"]:::artifact
493
+ OutputEnvelope["output<br/>schema"]:::artifact
501
494
  ExportKIR --> KIRP --> Specialize --> SpecializedKIRP
502
495
  Specialize --> OutputEnvelope
503
496
  end
@@ -533,8 +526,8 @@ flowchart TB
533
526
  ### Reference Execution Graph
534
527
 
535
528
  The reference path is the semantic baseline. It can run directly from source or
536
- from a loaded k object. It stays envelope-aware: the value keeps its envelope
537
- through projections, constructors, pattern guards, and partial-function calls.
529
+ from a loaded k object. It stays schema-aware: the value keeps its schema framing
530
+ through projections, constructors, filters, and relation calls.
538
531
 
539
532
  ```mermaid
540
533
  %%{init: {"flowchart": {"htmlLabels": true, "nodeSpacing": 42, "rankSpacing": 50}, "themeVariables": {"fontSize": "18px"}} }%%
@@ -544,16 +537,16 @@ flowchart LR
544
537
  classDef value fill:#e8f7ee,stroke:#16a34a,stroke-width:1.5px,color:#052e16;
545
538
  classDef result fill:#fff1f2,stroke:#e11d48,stroke-width:1.5px,color:#4c0519;
546
539
 
547
- Source["partial function<br/>definition in k syntax"]:::artifact
548
- Input["input<br/>enveloped value"]:::value
540
+ Source["relation<br/>definition in k syntax"]:::artifact
541
+ Input["input framed value<br/>(schema, payload)"]:::value
549
542
  Parse["source<br/>parsing"]:::transform
550
543
  AST["parsed<br/>AST"]:::artifact
551
- Annotate["pattern-signature<br/>derivation"]:::transform
552
- Core["pattern-annotated<br/>core"]:::artifact
544
+ Annotate["contract<br/>derivation"]:::transform
545
+ Core["contract-annotated<br/>core"]:::artifact
553
546
  Select["entry<br/>selection"]:::transform
554
- Transform["partial function with<br/>pattern signature"]:::artifact
547
+ Transform["relation with<br/>contract"]:::artifact
555
548
  Run["reference<br/>evaluation"]:::transform
556
- Output["output<br/>enveloped value"]:::result
549
+ Output["output framed value<br/>(schema, payload)"]:::result
557
550
 
558
551
  Source --> Parse --> AST --> Annotate --> Core --> Select --> Transform
559
552
  Transform --> Run
@@ -563,8 +556,8 @@ flowchart LR
563
556
 
564
557
  ### Lowering Preparation Graph
565
558
 
566
- The lowering preparation path starts from a k object and an input envelope. It
567
- specializes the program for that envelope, derives the output envelope, lowers
559
+ The lowering preparation path starts from a k object and an input schema. It
560
+ specializes the program for that schema, derives the output schema, lowers
568
561
  to a lowered execution artifact, and may then lower to a concrete execution
569
562
  target.
570
563
 
@@ -576,12 +569,12 @@ flowchart LR
576
569
  classDef lowered fill:#f3e8ff,stroke:#7e22ce,stroke-width:1.5px,color:#2e1065;
577
570
 
578
571
  Object["k<br/>object"]:::artifact
579
- InputEnvelope["input<br/>envelope"]:::artifact
572
+ InputEnvelope["input<br/>schema"]:::artifact
580
573
  Export["KIR-P<br/>export"]:::transform
581
574
  KIRP["KIR-P"]:::artifact
582
- Specialize["envelope<br/>specialization"]:::transform
583
- Specialized["envelope-specialized<br/>KIR-P"]:::artifact
584
- OutputEnvelope["output<br/>envelope"]:::artifact
575
+ Specialize["schema<br/>specialization"]:::transform
576
+ Specialized["schema-specialized<br/>KIR-P"]:::artifact
577
+ OutputEnvelope["output<br/>schema"]:::artifact
585
578
  LowerModel["execution-model<br/>lowering"]:::transform
586
579
  Lowered["lowered execution<br/>artifact"]:::lowered
587
580
  TargetLowering["target<br/>lowering"]:::transform
@@ -599,7 +592,7 @@ flowchart LR
599
592
  ### Wire Boundary Graph
600
593
 
601
594
  The wire boundary is separate from program compilation. It converts between a
602
- binary pattern+value stream and the in-memory enveloped value that execution
595
+ binary schema+payload stream and the in-memory framed value that execution
603
596
  uses.
604
597
 
605
598
  ```mermaid
@@ -611,9 +604,9 @@ flowchart LR
611
604
 
612
605
  InputWire["input wire<br/>stream"]:::value
613
606
  Decode["wire<br/>decoding"]:::transform
614
- InputValue["input<br/>enveloped value"]:::value
607
+ InputValue["input framed value<br/>(schema, payload)"]:::value
615
608
  Execute["reference/target<br/>execution"]:::transform
616
- OutputValue["output<br/>enveloped value"]:::result
609
+ OutputValue["output framed value<br/>(schema, payload)"]:::result
617
610
  Encode["wire<br/>encoding"]:::transform
618
611
  OutputWire["output wire<br/>stream"]:::result
619
612
 
@@ -629,7 +622,7 @@ Use noun phrases ending in the operation kind:
629
622
  - `Parsing` for syntax to AST.
630
623
  - `Registration` for assigning canonical identities.
631
624
  - `Construction` for building semantic tables from parsed material.
632
- - `Derivation` for computing type-pattern information.
625
+ - `Derivation` for computing schema contracts.
633
626
  - `Canonicalization` for stable names and hashes.
634
627
  - `Packaging` for serializable artifacts.
635
628
  - `Loading` for reconstructing semantic objects from artifacts.
@@ -637,7 +630,7 @@ Use noun phrases ending in the operation kind:
637
630
  - `Evaluation` for executing a semantic or lowered execution artifact.
638
631
  - `Extraction` for reading a component from an artifact without changing it.
639
632
  - `Export` for a stable inspection or interchange view.
640
- - `Specialization` for rewriting an artifact for a concrete envelope.
633
+ - `Specialization` for rewriting an artifact for a concrete input schema.
641
634
  - `Lowering` for moving to a lower-level execution contract.
642
635
  - `Encoding` and `Decoding` for wire boundaries.
643
636
  - `Rendering` for human or external presentation.
@@ -651,24 +644,24 @@ names.
651
644
  | Identifier | Preferred name | Inputs | Outputs |
652
645
  | --- | --- | --- | --- |
653
646
  | `source-parsing` | Source Parsing | k source program | parsed AST |
654
- | `type-code-registration` | Type-Code Registration | parsed AST | canonical type-code table |
655
- | `partial-function-table-construction` | Partial-Function Table Construction | parsed AST, canonical type-code table | source partial-function table |
656
- | `pattern-signature-derivation` | Pattern-Signature Derivation | source partial-function table, canonical type-code table | pattern-annotated core |
657
- | `partial-function-canonicalization` | Partial-Function Canonicalization | pattern-annotated core | canonical partial-function aliases |
658
- | `object-packaging` | Object Packaging | pattern-annotated core, canonical partial-function aliases | k object |
647
+ | `schema-registration` | Schema Registration | parsed AST | canonical schema table |
648
+ | `relation-table-construction` | Relation Table Construction | parsed AST, canonical schema table | source relation table |
649
+ | `contract-derivation` | Contract Derivation | source relation table, canonical schema table | contract-annotated core |
650
+ | `relation-canonicalization` | Relation Canonicalization | contract-annotated core | canonical relation aliases |
651
+ | `object-packaging` | Object Packaging | contract-annotated core, canonical relation aliases | k object |
659
652
  | `object-loading` | Object Loading | k object | loaded k object |
660
- | `entry-selection` | Entry Selection | pattern-annotated core or loaded k object | entry partial function |
661
- | `reference-evaluation` | Reference Evaluation | entry partial function, input enveloped value | output enveloped value or partial failure |
662
- | `input-envelope-extraction` | Input-Envelope Extraction | input enveloped value | input envelope |
653
+ | `entry-selection` | Entry Selection | contract-annotated core or loaded k object | entry relation |
654
+ | `reference-evaluation` | Reference Evaluation | entry relation, input framed value | output framed value or partial failure |
655
+ | `input-schema-extraction` | Input-Schema Extraction | input framed value | input schema |
663
656
  | `kir-p-export` | KIR-P Export | k object | KIR-P |
664
- | `envelope-specialization` | Envelope Specialization | KIR-P or k object, input envelope | envelope-specialized KIR-P, output envelope |
665
- | `execution-model-lowering` | Execution-Model Lowering | envelope-specialized KIR-P | lowered execution artifact |
666
- | `lowered-evaluation` | Lowered Evaluation | lowered execution artifact, input enveloped value | output enveloped value or partial failure |
657
+ | `schema-specialization` | Schema Specialization | KIR-P or k object, input schema | schema-specialized KIR-P, output schema |
658
+ | `execution-model-lowering` | Execution-Model Lowering | schema-specialized KIR-P | lowered execution artifact |
659
+ | `lowered-evaluation` | Lowered Evaluation | lowered execution artifact, input framed value | output framed value or partial failure |
667
660
  | `target-lowering` | Target Lowering | lowered execution artifact | target artifact |
668
- | `target-execution` | Target Execution | target artifact, input enveloped value or input wire stream | output enveloped value or output wire stream |
669
- | `wire-decoding` | Wire Decoding | input wire stream | input enveloped value |
670
- | `wire-encoding` | Wire Encoding | output enveloped value | output wire stream |
671
- | `external-rendering` | External Rendering | output wire stream or output enveloped value | external text, JSON, or host value |
661
+ | `target-execution` | Target Execution | target artifact, input framed value or input wire stream | output framed value or output wire stream |
662
+ | `wire-decoding` | Wire Decoding | input wire stream | input framed value |
663
+ | `wire-encoding` | Wire Encoding | output framed value | output wire stream |
664
+ | `external-rendering` | External Rendering | output wire stream or output framed value | external text, JSON, or host value |
672
665
 
673
666
  ### Abstract Transformation Contracts
674
667
 
@@ -1189,13 +1182,13 @@ Independent tests:
1189
1182
  ### Transformation Notes
1190
1183
 
1191
1184
  - Compilation transformations do not need an input value until
1192
- envelope-specialization or execution.
1193
- - Reference execution can use the pattern-annotated core directly; target
1185
+ schema-specialization or execution.
1186
+ - Reference execution can use the contract-annotated core directly; target
1194
1187
  execution goes through KIR-P and a lowered execution artifact.
1195
- - Envelope-specialization is not a new source language feature. It is a compiler
1196
- transformation driven by the input envelope.
1197
- - The output envelope is an artifact of execution or specialization. It must be
1198
- preserved at binary boundaries even when printed value trees look identical.
1188
+ - Schema-specialization is not a new source language feature. It is a compiler
1189
+ transformation driven by the input schema.
1190
+ - The output schema is an artifact of execution or specialization. It must be
1191
+ preserved at binary boundaries even when printed payload trees look identical.
1199
1192
  - Partial failure is an ordinary result of execution transformations. It should
1200
1193
  not be modeled as a malformed artifact.
1201
1194
 
@@ -1206,10 +1199,10 @@ Use this pipeline in high-level docs:
1206
1199
  ```text
1207
1200
  k source
1208
1201
  -> parsed AST
1209
- -> pattern-annotated core
1202
+ -> contract-annotated core
1210
1203
  -> k object
1211
1204
  -> KIR-P
1212
- -> envelope-specialized KIR-P
1205
+ -> schema-specialized KIR-P
1213
1206
  -> lowered execution artifact
1214
1207
  -> target artifact
1215
1208
  ```
@@ -1217,23 +1210,23 @@ k source
1217
1210
  For the execution and codec boundary:
1218
1211
 
1219
1212
  ```text
1220
- wire stream <-> enveloped value <-> partial function <-> enveloped value <-> wire stream
1213
+ wire stream <-> framed value (schema, payload) <-> partial relation <-> framed value (schema, payload) <-> wire stream
1221
1214
  ```
1222
1215
 
1223
1216
  ## Terminology Rules
1224
1217
 
1225
1218
  These rules are normative for this specification:
1226
1219
 
1227
- - Use "partial function" as the dominant public term. Avoid "partial
1228
- transform"; it can read as unfinished rather than undefined for some inputs.
1229
- - Keep "pattern-annotated core" as the name for the normalized,
1230
- anonymized, pattern-annotated AST stage.
1231
- - Use "envelope-specialized KIR-P" for KIR-P rebuilt under a concrete input
1232
- envelope.
1233
- - Use "type code" for the canonical representation of a type, and "type" for
1234
- the semantic concept.
1235
- - Use "envelope" for the type pattern carried with a value at an execution or
1236
- wire boundary.
1237
- - Use "pattern guard" for the semantic operation that constrains an envelope;
1238
- reserve "filter expression" for source syntax when the syntax itself is being
1239
- discussed.
1220
+ - Use "relation" or "partial relation" as the dominant public term.
1221
+ - Keep "contract-annotated core" as the name for the normalized,
1222
+ contract-annotated AST stage.
1223
+ - Use "schema-specialized KIR-P" for KIR-P rebuilt under a concrete input
1224
+ schema.
1225
+ - Use "canonical schema" for the deterministic representation of a schema,
1226
+ and "schema" for the semantic language of payload trees.
1227
+ - Use "contract" for the connected $(input, output)$ schema graph derived
1228
+ for relations, capturing structural preservation ($=X$).
1229
+ - Use "framed value `(schema, payload)`" for the self-describing value
1230
+ at execution and wire boundaries.
1231
+ - Use "filter" for the syntactic expression `?schema` that constrains
1232
+ payloads and acts as inductive termination witness.