@fraczak/k 8.4.1 → 8.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/DOCS/ARCHITECTURE.md +83 -0
- package/DOCS/CODECS.md +93 -204
- package/DOCS/DICTIONARY.md +42 -1241
- package/DOCS/FILE_FORMATS.md +170 -625
- package/DOCS/GETTING_STARTED.md +164 -0
- package/DOCS/KIR_V1.md +22 -33
- package/DOCS/KVM_EXECUTION_MODEL.md +88 -481
- package/DOCS/PATTERNS.md +155 -138
- package/DOCS/POLYMORPHIC_IR.md +83 -101
- package/DOCS/PROFILING_AND_TRACING.md +52 -166
- package/DOCS/README.md +35 -0
- package/DOCS/REPL.md +100 -377
- package/DOCS/TEXTUAL_VALUES.md +53 -81
- package/DOCS/TYPE_DERIVATION.md +95 -234
- package/DOCS/book-combined.md +3786 -0
- package/Examples/arithmetics.k +45 -59
- package/Examples/djson.k +102 -112
- package/Examples/karatsuba-mult.k +2 -3
- package/Examples/list.k +6 -7
- package/Examples/nat.k +6 -6
- package/Examples/poly.k +45 -45
- package/LICENSE +15 -0
- package/README.md +72 -410
- package/backends/llvm/README.md +75 -97
- package/backends/wasm/README.md +88 -66
- package/canonical-pattern.mjs +7 -3
- package/codecs/README.md +23 -261
- package/codecs/WIRE_FORMAT.md +132 -0
- package/core.k +5 -15
- package/named-patterns.mjs +19 -7
- package/objects/README.md +24 -109
- package/package.json +8 -3
- package/paper/Makefile +17 -0
- package/paper/k-paper.pdf +0 -0
- package/paper/k-paper.tex +435 -0
- package/parser.jison +0 -10
- package/parser.mjs +32 -43
- package/repl.mjs +74 -29
- package/scripts/benchmark-idempotency.mjs +3 -3
- package/scripts/run-tests.mjs +1 -1
- package/scripts/test-package.mjs +64 -0
- package/tests/README.md +66 -46
- package/tests/code-derivation/core.mjs +61 -0
- package/tests/helpers/cli.mjs +45 -0
- package/tests/integration.sh +2 -3
- package/tests/test-arithmetics.mjs +47 -237
- package/tests/test-djson-methods.mjs +71 -275
- package/tests/test-hash-normalization.mjs +22 -1
- package/tests/test-karatsuba.mjs +18 -126
- package/tests/test-named-patterns.mjs +174 -621
- package/tests/test-paper-examples.mjs +90 -0
- package/tests/test-poly.mjs +2 -2
- package/tests/test-repl-html.mjs +2 -2
- package/tests/test-repl.mjs +49 -41
- package/tests/test.mjs +27 -68
- package/AGENTS.md +0 -35
- package/CLAUDE.md +0 -64
- package/DOCS/BIG_PICTURE.md +0 -439
- package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +0 -74
- package/DOCS/CANONICAL_PATTERNS.md +0 -362
- package/DOCS/CODEC_ARCHITECTURE.md +0 -257
- package/DOCS/CONVERGENCE.md +0 -123
- package/DOCS/DIVERGENCE_DETECTION.md +0 -23
- package/DOCS/JSON_APPROACHES_COMPARISON.md +0 -302
- package/DOCS/KIR_SPECIALIZATION_DESIGN.md +0 -472
- package/DOCS/Makefile +0 -99
- package/DOCS/NAMED_PATTERNS.md +0 -361
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +0 -343
- package/DOCS/PATTERNS_AND_TYPED_VALUES.md +0 -665
- package/DOCS/WEB_REPL_REQUIREMENTS.md +0 -134
- package/DOCS/book/01-introduction.md +0 -48
- package/DOCS/book/02-syntax-and-values.md +0 -110
- package/DOCS/book/03-types-as-finite-automata.md +0 -155
- package/DOCS/book/04-partial-functions-and-composition.md +0 -145
- package/DOCS/book/05-typing-filters-and-normalization.md +0 -109
- package/DOCS/book/06-values-in-memory.md +0 -1085
- package/DOCS/book/07-the-partial-function-abi.md +0 -143
- package/DOCS/book/08-operational-semantics-and-execution.md +0 -196
- package/DOCS/book/09-compiler-architecture.md +0 -160
- package/DOCS/book/10-from-ast-to-intermediate-representation.md +0 -166
- package/DOCS/book/14-canonical-serialization.md +0 -176
- package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +0 -156
- package/DOCS/book/overview.txt +0 -68
- package/DOCS/book.md +0 -50
- package/Examples/bnat.k +0 -308
- package/Examples/buda.k +0 -66
- package/Examples/byte.k +0 -35
- package/Examples/code.k +0 -43
- package/Examples/vhdl_to_k.k +0 -67
- package/JOURNAL.md +0 -288
- package/TODO.md +0 -107
- package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +0 -233
- package/backends/wasm/TECHNICAL-DEBT.md +0 -58
- package/backends/wasm/demo_ieee.mjs +0 -318
- package/browser/vfs-data.mjs +0 -21
- package/codecs/BINARY_FORMAT.md +0 -53
- package/codecs/POLYMORPHIC_BINARY_FORMAT.md +0 -303
- package/codecs/pandoc.css +0 -71
- package/tests/code-derivation/blowup.mjs +0 -25
- package/tests/code-derivation/bnat.mjs +0 -11
- package/tests/code-derivation/dot.mjs +0 -37
- package/tests/code-derivation/index.mjs +0 -20
- package/tests/code-derivation/loop.mjs +0 -17
- package/tests/code-derivation/nat.mjs +0 -10
- package/tests/code-derivation/not-not.mjs +0 -14
- package/tests/code-derivation/product.mjs +0 -22
- package/tests/code-derivation/rlz.mjs +0 -13
- package/tests/test-fingerprint.mjs +0 -38
package/DOCS/PATTERNS.md
CHANGED
|
@@ -1,188 +1,205 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Patterns and relations
|
|
2
|
+
|
|
3
|
+
k source defines relations, named patterns, and a terminal expression.
|
|
4
|
+
The terminal expression is the entry relation when executing a source file.
|
|
5
|
+
The REPL also accepts definitions on their own.
|
|
6
|
+
|
|
7
|
+
## Core relations
|
|
8
|
+
|
|
9
|
+
Every expression denotes a first-order partial function over product/variant
|
|
10
|
+
values.
|
|
11
|
+
|
|
12
|
+
| Syntax | Meaning |
|
|
13
|
+
| --- | --- |
|
|
14
|
+
| `()` | Identity |
|
|
15
|
+
| `{}` | Constant unit |
|
|
16
|
+
| `<>` | Always undefined |
|
|
17
|
+
| `.field` | Project a product field |
|
|
18
|
+
| `/tag` | Extract a matching variant payload |
|
|
19
|
+
| `\|tag` | Wrap the input in a variant |
|
|
20
|
+
| `(f g)` | Apply `f`, then `g` |
|
|
21
|
+
| `{f a, g b}` | Apply both relations to the same input and construct fields `a` and `b` |
|
|
22
|
+
| `<f, g>` | Try `f`, then `g` if `f` is undefined |
|
|
23
|
+
| `?P` | Constrain the schema to pattern `P` without changing the payload |
|
|
24
|
+
| `name` or `@hash` | Reference a relation |
|
|
25
|
+
|
|
26
|
+
Composition can omit parentheses when grouping is unambiguous.
|
|
27
|
+
A product is undefined if a field computation is undefined. Choice preserves
|
|
28
|
+
source priority; an earlier nonterminating branch does not fall through.
|
|
29
|
+
|
|
30
|
+
Definitions use `name = expression;`. There are no builtin numeric or string
|
|
31
|
+
values in relation syntax: a numeric identifier such as `5` is a relation
|
|
32
|
+
name, and quoted strings in label positions are labels. Use
|
|
33
|
+
[codecs](../codecs/README.md) for external numbers, text, or JSON.
|
|
34
|
+
|
|
35
|
+
Relations can refer to other definitions in the same source, including recursive
|
|
36
|
+
references. Labels can be identifiers or quoted strings with escapes. Line
|
|
37
|
+
comments use `--`, `//`, `#`, or `%`; block comments use `/* ... */`.
|
|
38
|
+
|
|
39
|
+
## Pattern syntax
|
|
40
|
+
|
|
41
|
+
Patterns describe schema constraints rather than computations:
|
|
42
|
+
|
|
43
|
+
| Pattern | Constraint |
|
|
44
|
+
| --- | --- |
|
|
45
|
+
| `(...)` or an unbound variable `X` | An unconstrained schema position |
|
|
46
|
+
| `{P a, Q b}` | A product with exactly the listed fields |
|
|
47
|
+
| `{P a, ...}` | A product with at least the listed fields |
|
|
48
|
+
| `<P a, Q b>` | A union with exactly the listed tags |
|
|
49
|
+
| `<P a, ...>` | A union allowing additional tags |
|
|
50
|
+
| `{}` | Empty product schema |
|
|
51
|
+
| `<>` | Empty union schema |
|
|
52
|
+
| `P = X` | Bind the pattern node as `X`, allowing recursive references |
|
|
53
|
+
| `name[P, Q]` | Instantiate a named pattern |
|
|
54
|
+
|
|
55
|
+
The distinction between open and closed union schemas concerns the schema's
|
|
56
|
+
available tags. A variant payload still selects one tag.
|
|
57
|
+
|
|
58
|
+
Repeated variables express equality of schema constraints:
|
|
2
59
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
serialization, schemas are represented as property-list arrays of
|
|
6
|
-
automata nodes. Schemas are used by:
|
|
7
|
-
|
|
8
|
-
- the contract-derivation engine
|
|
9
|
-
- the binary codec
|
|
10
|
-
- runtime framed values `(schema, payload)`
|
|
11
|
-
- filter expressions (`?schema` or `?name[...]`)
|
|
12
|
-
|
|
13
|
-
## Pattern Kinds
|
|
14
|
-
|
|
15
|
-
The exported canonical pattern form is a property-list array of nodes.
|
|
16
|
-
|
|
17
|
-
Each node has the form:
|
|
18
|
-
|
|
19
|
-
```text
|
|
20
|
-
[kind, edges]
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
where `kind` is one of:
|
|
24
|
-
|
|
25
|
-
- `"any"`
|
|
26
|
-
- `"open-product"`
|
|
27
|
-
- `"open-union"`
|
|
28
|
-
- `"closed-product"`
|
|
29
|
-
- `"closed-union"`
|
|
30
|
-
|
|
31
|
-
and `edges` is a list of:
|
|
32
|
-
|
|
33
|
-
```text
|
|
34
|
-
[label, targetNodeId]
|
|
60
|
+
```k
|
|
61
|
+
?{ X first, X second }
|
|
35
62
|
```
|
|
36
63
|
|
|
37
|
-
|
|
64
|
+
The two fields must have compatible shared schema constraints; their payloads
|
|
65
|
+
need not be equal. Anonymous wildcards do not impose that sharing:
|
|
38
66
|
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
["closed-union", [["nil", 1], ["cons", 2]]],
|
|
42
|
-
["closed-product", []],
|
|
43
|
-
["closed-product", [["car", 3], ["cdr", 0]]],
|
|
44
|
-
["closed-union", [["_", 1], ["0", 3], ["1", 3]]]
|
|
45
|
-
]
|
|
67
|
+
```k
|
|
68
|
+
?{ (...) first, (...) second }
|
|
46
69
|
```
|
|
47
70
|
|
|
48
|
-
|
|
71
|
+
Pattern variables in a relation have relation-definition scope.
|
|
49
72
|
|
|
50
|
-
## Named
|
|
73
|
+
## Named patterns
|
|
51
74
|
|
|
52
|
-
|
|
53
|
-
square brackets `[...]`:
|
|
75
|
+
Square brackets distinguish reusable templates from bare variables:
|
|
54
76
|
|
|
55
77
|
```k
|
|
56
|
-
|
|
78
|
+
bit[] = < {} 0, {} 1 >;
|
|
79
|
+
bits[] = < {} _, B 0, B 1 > = B;
|
|
80
|
+
pair[A, B] = { A first, B second };
|
|
81
|
+
list[X] = < {} nil, { X car, Y cdr } cons > = Y;
|
|
57
82
|
```
|
|
58
83
|
|
|
59
|
-
|
|
84
|
+
A filter applies a template:
|
|
60
85
|
|
|
61
86
|
```k
|
|
62
|
-
bit[]
|
|
63
|
-
bits[] = < {} _, B 0, B 1 > = B;
|
|
64
|
-
byte[] = {
|
|
65
|
-
bit[] 0, bit[] 1, bit[] 2, bit[] 3,
|
|
66
|
-
bit[] 4, bit[] 5, bit[] 6, bit[] 7
|
|
67
|
-
};
|
|
87
|
+
?pair[bit[], bits[]]
|
|
68
88
|
```
|
|
69
89
|
|
|
70
|
-
|
|
90
|
+
Template applications can be nested inside patterns, including arguments such
|
|
91
|
+
as `list[pair[A, B]]`. Every application must resolve in scope and have the
|
|
92
|
+
declared arity.
|
|
71
93
|
|
|
72
|
-
|
|
73
|
-
list[X] = < {} nil, { X car, Y cdr } cons > = Y;
|
|
74
|
-
pair[A, B] = { A first, B second };
|
|
75
|
-
```
|
|
94
|
+
Variables have three roles:
|
|
76
95
|
|
|
77
|
-
|
|
78
|
-
-
|
|
79
|
-
|
|
80
|
-
-
|
|
96
|
+
- Parameters in a header such as `[X]` are supplied by callers.
|
|
97
|
+
- Other free variables are private existential constraints. Repeated uses share
|
|
98
|
+
a constraint; different instantiations receive fresh private names.
|
|
99
|
+
- Bound variables such as `= Y` are internal cycle knots.
|
|
81
100
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
101
|
+
Knots cannot be parameters. For example,
|
|
102
|
+
`list[X, Y] = <{} nil, {X car, Y cdr} cons> = Y;` is rejected.
|
|
103
|
+
Template dependency cycles are also rejected: recursion uses a knot inside a
|
|
104
|
+
template, rather than the template calling itself. Undefined templates and
|
|
105
|
+
arity mismatches are rejected even in unused definitions.
|
|
87
106
|
|
|
88
|
-
|
|
89
|
-
`:pat <name>`) displays its canonical decompiled form with newly
|
|
90
|
-
introduced variables in discovery order (e.g.
|
|
91
|
-
`list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- @...`).
|
|
107
|
+
## Filters and contracts
|
|
92
108
|
|
|
93
|
-
|
|
109
|
+
A relation can combine structural guards with ordinary computation:
|
|
94
110
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
treated as an open variable or leaf.
|
|
111
|
+
```k
|
|
112
|
+
bool[] = < {} false, {} true >;
|
|
113
|
+
not = ?bool[] < /false |true, /true |false > ?bool[];
|
|
114
|
+
```
|
|
100
115
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
116
|
+
Both boundaries have the boolean schema, although the relation changes the
|
|
117
|
+
payload. Shared contract nodes describe schema relationships, not a separate
|
|
118
|
+
proof of preservation of the original data.
|
|
104
119
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
120
|
+
Explicit recursive patterns can make contract derivation converge. This is a
|
|
121
|
+
compiler property, not a guarantee of execution termination. See
|
|
122
|
+
[contract derivation](TYPE_DERIVATION.md).
|
|
108
123
|
|
|
109
|
-
|
|
124
|
+
In the envelope-aware runtime, constraints intersect the carried pattern and
|
|
125
|
+
contradictions raise type errors. Optimized execution may erase guards proved
|
|
126
|
+
by specialization. Ordinary partial failure remains distinct from a schema
|
|
127
|
+
contradiction.
|
|
110
128
|
|
|
111
|
-
|
|
129
|
+
## Canonical identity and routing
|
|
112
130
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
- a named pattern invocation (`? name[...]`)
|
|
131
|
+
Relations are hashed from normalized definitions; that does not decide general
|
|
132
|
+
semantic equality of programs.
|
|
116
133
|
|
|
117
|
-
|
|
134
|
+
Named patterns factor into a canonical linear skeleton and alias arguments.
|
|
135
|
+
Every open leaf slot of the skeleton has its own port:
|
|
118
136
|
|
|
119
137
|
```k
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
138
|
+
pair[A, B] = { A first, B second };
|
|
139
|
+
same_pair[T] = { T first, T second };
|
|
140
|
+
closed_same_pair[] = { T first, T second };
|
|
141
|
+
any_pair[] = { (...) first, (...) second };
|
|
124
142
|
```
|
|
125
143
|
|
|
126
|
-
|
|
144
|
+
All four use the skeleton:
|
|
127
145
|
|
|
128
146
|
```k
|
|
129
|
-
|
|
147
|
+
@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm[X0, X1] = {
|
|
148
|
+
X0 first, X1 second
|
|
149
|
+
};
|
|
130
150
|
```
|
|
131
151
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
## Pattern Export Helper
|
|
152
|
+
Their arguments retain the differences:
|
|
135
153
|
|
|
136
|
-
|
|
137
|
-
|
|
154
|
+
| Alias | Skeleton arguments |
|
|
155
|
+
| --- | --- |
|
|
156
|
+
| `pair[A, B]` | `[A, B]` |
|
|
157
|
+
| `same_pair[T]` | `[T, T]` |
|
|
158
|
+
| `closed_same_pair[]` | `[T, T]`, with a fresh private `T` per instantiation |
|
|
159
|
+
| `any_pair[]` | `[(...), (...)]` |
|
|
138
160
|
|
|
139
|
-
|
|
161
|
+
Formal parameters route to their supplied arguments. A repeated private
|
|
162
|
+
variable routes the same fresh variable to multiple slots. A private variable
|
|
163
|
+
used only once becomes a wildcard. Knots remain internal to the graph.
|
|
140
164
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
- a relation name
|
|
147
|
-
5. exports the corresponding root pattern
|
|
148
|
-
6. prints it as the canonical property-list array
|
|
165
|
+
Consequently, the skeleton hash alone does not identify all constraints of an
|
|
166
|
+
alias: its routing arguments matter too. Structural canonicalization also
|
|
167
|
+
does not identify every graph accepting the same finite-tree language; for
|
|
168
|
+
example, the current implementation distinguishes different unproductive
|
|
169
|
+
recursive structures.
|
|
149
170
|
|
|
150
|
-
|
|
171
|
+
## Inspection and persistence
|
|
151
172
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
173
|
+
In the REPL, `:pats` lists aliases with parameters, skeleton hashes, and routing
|
|
174
|
+
arguments. `:pat name` shows the alias definition; `:pat @hash` inspects a
|
|
175
|
+
registered canonical pattern. Hash-addressed patterns can also be defined or
|
|
176
|
+
instantiated directly, with all canonical arguments supplied.
|
|
156
177
|
|
|
157
|
-
|
|
178
|
+
Object and library containers preserve:
|
|
158
179
|
|
|
159
|
-
|
|
160
|
-
[
|
|
161
|
-
|
|
162
|
-
["closed-product", []],
|
|
163
|
-
["closed-product", [["car", 1], ["cdr", 0]]]
|
|
164
|
-
]
|
|
165
|
-
```
|
|
180
|
+
- `patterns[hash]`: canonical template parameters and filter AST;
|
|
181
|
+
- `patternAlias[name]`: alias-to-skeleton hash;
|
|
182
|
+
- `meta[hash].origins[]`: original alias parameters and `aliasArgs` routing.
|
|
166
183
|
|
|
167
|
-
|
|
184
|
+
Repeated existential variables must survive export and loading. The
|
|
185
|
+
[file-format reference](FILE_FORMATS.md) describes the complete container.
|
|
168
186
|
|
|
169
|
-
|
|
187
|
+
For a host-side pattern export, use:
|
|
170
188
|
|
|
171
|
-
```
|
|
172
|
-
|
|
189
|
+
```sh
|
|
190
|
+
printf '%s\n' '?< {} nil, { {} car, X cdr } cons > = X' |
|
|
191
|
+
node patterns/from-k.mjs
|
|
173
192
|
```
|
|
174
193
|
|
|
175
|
-
|
|
176
|
-
readable externalization of the same schema information carried in the
|
|
177
|
-
wire stream.
|
|
194
|
+
Run this from the repository root. The canonical property-list result is:
|
|
178
195
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
196
|
+
```json
|
|
197
|
+
[
|
|
198
|
+
["closed-union", [["cons", 1], ["nil", 2]]],
|
|
199
|
+
["closed-product", [["car", 2], ["cdr", 0]]],
|
|
200
|
+
["closed-product", []]
|
|
201
|
+
]
|
|
202
|
+
```
|
|
183
203
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
- [DOCS/TEXTUAL_VALUES.md](./TEXTUAL_VALUES.md)
|
|
187
|
-
- [codecs/README.md](../codecs/README.md)
|
|
188
|
-
- [DOCS/OBJECT_FILE_AND_PATTERN.md](./OBJECT_FILE_AND_PATTERN.md)
|
|
204
|
+
This is an inspection representation. The [wire format](../codecs/WIRE_FORMAT.md)
|
|
205
|
+
encodes the graph as an ordinary value under the fixed `pattern[]` schema.
|
package/DOCS/POLYMORPHIC_IR.md
CHANGED
|
@@ -1,104 +1,86 @@
|
|
|
1
|
-
# Polymorphic
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
precomputed `AccessTable` (Layout Witness), accessing fields and
|
|
15
|
-
variants via static slot numbers and array lookups with zero string
|
|
16
|
-
comparisons.
|
|
17
|
-
2. **Specialized Monomorphic Mode**: Given an `AccessTable`, a fast linear
|
|
18
|
-
pass (`specializePKIR`) inlines offsets directly into immediate machine
|
|
19
|
-
instructions, pruning unreachable union branches for peak LLVM/Wasm
|
|
20
|
-
performance.
|
|
21
|
-
|
|
22
|
-
---
|
|
23
|
-
|
|
24
|
-
## 2. Core Concepts
|
|
25
|
-
|
|
26
|
-
### 2.1 The Structural Access Profile
|
|
27
|
-
|
|
28
|
-
Every PKIR relation is accompanied by an **Access Profile** derived from
|
|
29
|
-
its pattern graph:
|
|
30
|
-
- For every product pattern node $u$: a list of accessed field labels
|
|
31
|
-
defining dense, zero-based **logical slots**:
|
|
32
|
-
$$\text{fields}(u) = [ f_0, f_1, \dots, f_{k-1} ]$$
|
|
33
|
-
where slot $i$ corresponds to field $f_i$.
|
|
34
|
-
- For every union pattern node $u$: a list of matched variant tags
|
|
35
|
-
defining dense **branch targets**:
|
|
36
|
-
$$\text{variants}(u) = [ v_0, v_1, \dots, v_{m-1} ]$$
|
|
37
|
-
where branch $j$ corresponds to variant $v_j$.
|
|
38
|
-
|
|
39
|
-
### 2.2 Dual-Form Values (`KVal`)
|
|
40
|
-
|
|
41
|
-
Values in PKIR conform to the hybrid representation:
|
|
42
|
-
- **Serialized (Wire Slice)**: A pointer into an immutable prefix-free
|
|
43
|
-
bitstream, carrying a bit offset and schema state.
|
|
44
|
-
- **Materialized (Arena Node)**: An allocated record in memory containing
|
|
45
|
-
word-aligned pointers to child values.
|
|
46
|
-
|
|
47
|
-
---
|
|
48
|
-
|
|
49
|
-
## 3. Instruction Set
|
|
50
|
-
|
|
51
|
-
PKIR instructions operate over a register file (`%v0`, `%v1`, ...):
|
|
52
|
-
|
|
53
|
-
### 3.1 Field & Variant Operations
|
|
54
|
-
|
|
55
|
-
- **`project_slot dest, src, state, slot`**:
|
|
56
|
-
Projects field at logical `slot` of pattern `state` from `src`.
|
|
57
|
-
At runtime with `AccessTable`:
|
|
58
|
-
```c
|
|
59
|
-
dest.ptr = src.ptr + table[src.state].slotToByteOffset[slot];
|
|
60
|
-
dest.state = table[src.state].slotToChildState[slot];
|
|
61
|
-
```
|
|
62
|
-
- **`match_tag src, state, branches`**:
|
|
63
|
-
Evaluates variant discriminator of `src` against pattern `state`.
|
|
64
|
-
At runtime with `AccessTable`:
|
|
65
|
-
```c
|
|
66
|
-
branch_idx = table[src.state].branchMap[src.tag];
|
|
67
|
-
dest.state = table[src.state].childStateMap[src.tag];
|
|
68
|
-
jump branches[branch_idx];
|
|
69
|
-
```
|
|
70
|
-
- **`make_variant dest, src, tag_id`**:
|
|
71
|
-
Wraps `src` into a tagged union with discriminator `tag_id`.
|
|
72
|
-
- **`make_product dest, [src0, src1, ...]`**:
|
|
73
|
-
Allocates a product node whose fields are registers `[src0, src1, ...]`.
|
|
74
|
-
|
|
75
|
-
### 3.2 Memory & Control Operations
|
|
76
|
-
|
|
77
|
-
- **`promote_spine dest, src`**:
|
|
78
|
-
If `src` is in serialized wire form, materializes only its immediate
|
|
79
|
-
outer record in the arena; children remain serialized wire slices.
|
|
80
|
-
If `src` is already materialized, acts as identity.
|
|
81
|
-
- **`dup dest1, dest2, src`**:
|
|
82
|
-
Zero-copy duplication of a value handle.
|
|
83
|
-
- **`call_rel dest, rel_name, src`**:
|
|
84
|
-
Invokes relation `rel_name` with input `src`.
|
|
85
|
-
- **`id dest, src`**:
|
|
86
|
-
Identity move.
|
|
87
|
-
- **`fail`**:
|
|
88
|
-
Signals partial function failure (backtracking or branch exit).
|
|
89
|
-
|
|
90
|
-
---
|
|
91
|
-
|
|
92
|
-
## 4. Execution Pipeline
|
|
1
|
+
# Polymorphic IR (PKIR)
|
|
2
|
+
|
|
3
|
+
PKIR is an experimental execution representation in [pkir.mjs](../pkir.mjs).
|
|
4
|
+
It uses schema states and structural access profiles to navigate values through
|
|
5
|
+
layout witnesses.
|
|
6
|
+
|
|
7
|
+
It is separate from [KIR-P](KIR_V1.md), the portable object export, and from
|
|
8
|
+
[kVM](KVM_EXECUTION_MODEL.md), the register execution representation used by
|
|
9
|
+
the backends.
|
|
10
|
+
|
|
11
|
+
## Relation artifact
|
|
12
|
+
|
|
13
|
+
`compileRelationToPKIR(relDef, name)` creates:
|
|
93
14
|
|
|
94
15
|
```text
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
-> direct emission to LLVM / WebAssembly
|
|
16
|
+
format: "pk-ir"
|
|
17
|
+
version: 1
|
|
18
|
+
name
|
|
19
|
+
inputReg / outputReg
|
|
20
|
+
inputPattern / outputPattern
|
|
21
|
+
patternGraph
|
|
22
|
+
accessProfile
|
|
23
|
+
instructions
|
|
104
24
|
```
|
|
25
|
+
|
|
26
|
+
The pattern graph supplies relation constraints; the access profile records
|
|
27
|
+
which fields and variants each state uses. `.pkir` is the JSON artifact
|
|
28
|
+
extension accepted by target tools.
|
|
29
|
+
|
|
30
|
+
## Access tables and values
|
|
31
|
+
|
|
32
|
+
`buildAccessTable(schema, pattern)` in
|
|
33
|
+
[access-table.mjs](../access-table.mjs) connects an input schema to the relation's
|
|
34
|
+
pattern graph. Descriptors map structural accesses to physical slots, child
|
|
35
|
+
states, tag identities, and offsets.
|
|
36
|
+
|
|
37
|
+
The JavaScript adapter `valueToKVal` creates materialized records with
|
|
38
|
+
`repr`, `state`, `tag`, and `words`; product records also retain field names.
|
|
39
|
+
`kvalToValue` converts these records back to `Value`. The execution code also
|
|
40
|
+
has support for serialized representations; use the adapter/codec contracts
|
|
41
|
+
rather than assuming that every PKIR value is a native pointer.
|
|
42
|
+
|
|
43
|
+
## Current instructions
|
|
44
|
+
|
|
45
|
+
| Instruction | Role |
|
|
46
|
+
| --- | --- |
|
|
47
|
+
| `id` | Forward a register |
|
|
48
|
+
| `fail` | Partial failure |
|
|
49
|
+
| `project_slot` | Project a product field through schema/layout information |
|
|
50
|
+
| `match_variant` | Extract a matching variant payload |
|
|
51
|
+
| `make_variant` | Construct a tagged value |
|
|
52
|
+
| `product` | Construct fields through nested instruction streams |
|
|
53
|
+
| `union` | Ordered branch selection |
|
|
54
|
+
| `call_rel` | Relation call, including recursion |
|
|
55
|
+
| `project_offset` | Specialized product access with a resolved offset |
|
|
56
|
+
| `match_tag_imm` | Specialized tag match with resolved discriminator information |
|
|
57
|
+
|
|
58
|
+
Instruction records retain labels/tags where needed for generic adapters.
|
|
59
|
+
Do not assume that generic execution universally eliminates string operations.
|
|
60
|
+
|
|
61
|
+
## Specialization and target lowering
|
|
62
|
+
|
|
63
|
+
`specializePKIR(pkir, table)` resolves product accesses to `project_offset`,
|
|
64
|
+
resolves variant matches to `match_tag_imm`, and removes branches whose
|
|
65
|
+
leading operation is known to fail. It returns an artifact with
|
|
66
|
+
`isSpecialized: true`.
|
|
67
|
+
|
|
68
|
+
`executePKIR` interprets generic or specialized instructions with the required
|
|
69
|
+
access table and relation context. `lowerPKIRToKVM` adapts instructions for
|
|
70
|
+
backend lowering. The shared [backend API](../backend-api.mjs) exports these
|
|
71
|
+
functions plus `compileWasmArtifactFromPKIR` and
|
|
72
|
+
`compileLLVMArtifactFromPKIR`.
|
|
73
|
+
|
|
74
|
+
The target CLIs accept `.pkir` files or `--pkir` for compiling source through
|
|
75
|
+
this path. See [WebAssembly](../backends/wasm/README.md) and
|
|
76
|
+
[LLVM](../backends/llvm/README.md).
|
|
77
|
+
|
|
78
|
+
Performance depends on the workload, adapter, and execution target. Use the
|
|
79
|
+
[benchmark guide](PROFILING_AND_TRACING.md) for measurements rather than
|
|
80
|
+
treating historical timings as interface guarantees.
|
|
81
|
+
|
|
82
|
+
## Verification
|
|
83
|
+
|
|
84
|
+
`tests/test-access-table.mjs` exercises descriptors and layout mapping.
|
|
85
|
+
`tests/test-pkir.mjs` checks execution, specialization, opaque payloads,
|
|
86
|
+
partial failure, and target integration. Both are registered in `npm test`.
|