@fraczak/k 8.4.0 → 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 +59 -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 +156 -123
- 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 -371
- 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/llvm/src/cli.mjs +0 -31
- package/backends/wasm/README.md +88 -66
- package/backends/wasm/src/cli.mjs +0 -31
- package/canonical-pattern.mjs +69 -35
- package/codecs/README.md +23 -261
- package/codecs/WIRE_FORMAT.md +132 -0
- package/core.k +5 -15
- package/index.mjs +5 -1
- package/named-patterns.mjs +55 -7
- package/object.mjs +28 -6
- 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 +79 -101
- 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 -606
- 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 +89 -38
- 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 -349
- 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 -340
- 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 -136
- 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,172 +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
|
-
|
|
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.
|
|
60
|
+
```k
|
|
61
|
+
?{ X first, X second }
|
|
62
|
+
```
|
|
16
63
|
|
|
17
|
-
|
|
64
|
+
The two fields must have compatible shared schema constraints; their payloads
|
|
65
|
+
need not be equal. Anonymous wildcards do not impose that sharing:
|
|
18
66
|
|
|
19
|
-
```
|
|
20
|
-
|
|
67
|
+
```k
|
|
68
|
+
?{ (...) first, (...) second }
|
|
21
69
|
```
|
|
22
70
|
|
|
23
|
-
|
|
71
|
+
Pattern variables in a relation have relation-definition scope.
|
|
24
72
|
|
|
25
|
-
|
|
26
|
-
- `"open-product"`
|
|
27
|
-
- `"open-union"`
|
|
28
|
-
- `"closed-product"`
|
|
29
|
-
- `"closed-union"`
|
|
73
|
+
## Named patterns
|
|
30
74
|
|
|
31
|
-
|
|
75
|
+
Square brackets distinguish reusable templates from bare variables:
|
|
32
76
|
|
|
33
|
-
```
|
|
34
|
-
[
|
|
77
|
+
```k
|
|
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;
|
|
35
82
|
```
|
|
36
83
|
|
|
37
|
-
|
|
84
|
+
A filter applies a template:
|
|
38
85
|
|
|
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
|
-
]
|
|
86
|
+
```k
|
|
87
|
+
?pair[bit[], bits[]]
|
|
46
88
|
```
|
|
47
89
|
|
|
48
|
-
|
|
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.
|
|
49
93
|
|
|
50
|
-
|
|
94
|
+
Variables have three roles:
|
|
51
95
|
|
|
52
|
-
|
|
53
|
-
|
|
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.
|
|
54
100
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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.
|
|
58
106
|
|
|
59
|
-
|
|
107
|
+
## Filters and contracts
|
|
60
108
|
|
|
61
|
-
|
|
62
|
-
bit[] = < {} 0, {} 1 >;
|
|
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
|
-
};
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
Parameterized patterns declare external ports in the bracket list:
|
|
109
|
+
A relation can combine structural guards with ordinary computation:
|
|
71
110
|
|
|
72
111
|
```k
|
|
73
|
-
|
|
74
|
-
|
|
112
|
+
bool[] = < {} false, {} true >;
|
|
113
|
+
not = ?bool[] < /false |true, /true |false > ?bool[];
|
|
75
114
|
```
|
|
76
115
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
- As template arguments: `list[byte[]]` or `pair[A, B]`
|
|
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.
|
|
81
119
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
is disallowed to ensure every template dependency graph is an acyclic
|
|
86
|
-
DAG, guaranteeing finite regular tree automaton compilation.
|
|
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).
|
|
87
123
|
|
|
88
|
-
In the
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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.
|
|
92
128
|
|
|
93
|
-
##
|
|
129
|
+
## Canonical identity and routing
|
|
94
130
|
|
|
95
|
-
|
|
131
|
+
Relations are hashed from normalized definitions; that does not decide general
|
|
132
|
+
semantic equality of programs.
|
|
96
133
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
- a named pattern invocation (`? name[...]`)
|
|
100
|
-
|
|
101
|
-
Examples:
|
|
134
|
+
Named patterns factor into a canonical linear skeleton and alias arguments.
|
|
135
|
+
Every open leaf slot of the skeleton has its own port:
|
|
102
136
|
|
|
103
137
|
```k
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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 };
|
|
108
142
|
```
|
|
109
143
|
|
|
110
|
-
|
|
144
|
+
All four use the skeleton:
|
|
111
145
|
|
|
112
146
|
```k
|
|
113
|
-
|
|
147
|
+
@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm[X0, X1] = {
|
|
148
|
+
X0 first, X1 second
|
|
149
|
+
};
|
|
114
150
|
```
|
|
115
151
|
|
|
116
|
-
|
|
152
|
+
Their arguments retain the differences:
|
|
117
153
|
|
|
118
|
-
|
|
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[]` | `[(...), (...)]` |
|
|
119
160
|
|
|
120
|
-
|
|
121
|
-
|
|
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.
|
|
122
164
|
|
|
123
|
-
|
|
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.
|
|
124
170
|
|
|
125
|
-
|
|
126
|
-
2. runs `k.annotate(...)`
|
|
127
|
-
3. resolves `__main__`
|
|
128
|
-
4. requires `__main__` to be either:
|
|
129
|
-
- a filter expression
|
|
130
|
-
- a relation name
|
|
131
|
-
5. exports the corresponding root pattern
|
|
132
|
-
6. prints it as the canonical property-list array
|
|
171
|
+
## Inspection and persistence
|
|
133
172
|
|
|
134
|
-
|
|
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.
|
|
135
177
|
|
|
136
|
-
|
|
137
|
-
echo '?< {} nil, { {} car, X cdr } cons > = X' | \
|
|
138
|
-
node ./patterns/from-k.mjs
|
|
139
|
-
```
|
|
178
|
+
Object and library containers preserve:
|
|
140
179
|
|
|
141
|
-
|
|
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.
|
|
142
183
|
|
|
143
|
-
|
|
144
|
-
[
|
|
145
|
-
["closed-union", [["nil", 1], ["cons", 2]]],
|
|
146
|
-
["closed-product", []],
|
|
147
|
-
["closed-product", [["car", 1], ["cdr", 0]]]
|
|
148
|
-
]
|
|
149
|
-
```
|
|
184
|
+
Repeated existential variables must survive export and loading. The
|
|
185
|
+
[file-format reference](FILE_FORMATS.md) describes the complete container.
|
|
150
186
|
|
|
151
|
-
|
|
187
|
+
For a host-side pattern export, use:
|
|
152
188
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
encode(schema_value : schema) encode(payload : decoded_schema)
|
|
189
|
+
```sh
|
|
190
|
+
printf '%s\n' '?< {} nil, { {} car, X cdr } cons > = X' |
|
|
191
|
+
node patterns/from-k.mjs
|
|
157
192
|
```
|
|
158
193
|
|
|
159
|
-
|
|
160
|
-
readable externalization of the same schema information carried in the
|
|
161
|
-
wire stream.
|
|
162
|
-
|
|
163
|
-
`k-parse --input-pattern ...` produces the root schema used to interpret
|
|
164
|
-
textual values.
|
|
194
|
+
Run this from the repository root. The canonical property-list result is:
|
|
165
195
|
|
|
166
|
-
|
|
196
|
+
```json
|
|
197
|
+
[
|
|
198
|
+
["closed-union", [["cons", 1], ["nil", 2]]],
|
|
199
|
+
["closed-product", [["car", 2], ["cdr", 0]]],
|
|
200
|
+
["closed-product", []]
|
|
201
|
+
]
|
|
202
|
+
```
|
|
167
203
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
- [DOCS/TEXTUAL_VALUES.md](./TEXTUAL_VALUES.md)
|
|
171
|
-
- [codecs/README.md](../codecs/README.md)
|
|
172
|
-
- [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`.
|