@fraczak/k 8.4.1 → 8.5.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.
- package/CHANGELOG.md +32 -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/llvm/src/kvm2llvm.mjs +5 -4
- package/backends/llvm/src/llvm.mjs +2 -1
- package/backends/llvm/tests/integration.sh +7 -0
- package/backends/llvm/tests/test-llvm.mjs +20 -0
- package/backends/wasm/README.md +88 -66
- package/backends/wasm/src/kvm2wasm.mjs +64 -18
- package/backends/wasm/tests/integration.sh +5 -0
- package/backends/wasm/tests/test-wasm.mjs +86 -2
- 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 +22 -10
- package/tests/test-repl.mjs +49 -41
- package/tests/test.mjs +27 -68
- package/AGENTS.md +0 -35
- package/CLAUDE.md +0 -64
- package/DOCS/BIG_PICTURE.md +0 -439
- package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +0 -74
- package/DOCS/CANONICAL_PATTERNS.md +0 -362
- package/DOCS/CODEC_ARCHITECTURE.md +0 -257
- package/DOCS/CONVERGENCE.md +0 -123
- package/DOCS/DIVERGENCE_DETECTION.md +0 -23
- package/DOCS/JSON_APPROACHES_COMPARISON.md +0 -302
- package/DOCS/KIR_SPECIALIZATION_DESIGN.md +0 -472
- package/DOCS/Makefile +0 -99
- package/DOCS/NAMED_PATTERNS.md +0 -361
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +0 -343
- package/DOCS/PATTERNS_AND_TYPED_VALUES.md +0 -665
- package/DOCS/WEB_REPL_REQUIREMENTS.md +0 -134
- package/DOCS/book/01-introduction.md +0 -48
- package/DOCS/book/02-syntax-and-values.md +0 -110
- package/DOCS/book/03-types-as-finite-automata.md +0 -155
- package/DOCS/book/04-partial-functions-and-composition.md +0 -145
- package/DOCS/book/05-typing-filters-and-normalization.md +0 -109
- package/DOCS/book/06-values-in-memory.md +0 -1085
- package/DOCS/book/07-the-partial-function-abi.md +0 -143
- package/DOCS/book/08-operational-semantics-and-execution.md +0 -196
- package/DOCS/book/09-compiler-architecture.md +0 -160
- package/DOCS/book/10-from-ast-to-intermediate-representation.md +0 -166
- package/DOCS/book/14-canonical-serialization.md +0 -176
- package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +0 -156
- package/DOCS/book/overview.txt +0 -68
- package/DOCS/book.md +0 -50
- package/Examples/bnat.k +0 -308
- package/Examples/buda.k +0 -66
- package/Examples/byte.k +0 -35
- package/Examples/code.k +0 -43
- package/Examples/vhdl_to_k.k +0 -67
- package/JOURNAL.md +0 -288
- package/TODO.md +0 -107
- package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +0 -233
- package/backends/wasm/TECHNICAL-DEBT.md +0 -58
- package/backends/wasm/demo_ieee.mjs +0 -318
- package/browser/vfs-data.mjs +0 -21
- package/codecs/BINARY_FORMAT.md +0 -53
- package/codecs/POLYMORPHIC_BINARY_FORMAT.md +0 -303
- package/codecs/pandoc.css +0 -71
- package/tests/code-derivation/blowup.mjs +0 -25
- package/tests/code-derivation/bnat.mjs +0 -11
- package/tests/code-derivation/dot.mjs +0 -37
- package/tests/code-derivation/index.mjs +0 -20
- package/tests/code-derivation/loop.mjs +0 -17
- package/tests/code-derivation/nat.mjs +0 -10
- package/tests/code-derivation/not-not.mjs +0 -14
- package/tests/code-derivation/product.mjs +0 -22
- package/tests/code-derivation/rlz.mjs +0 -13
- package/tests/test-fingerprint.mjs +0 -38
|
@@ -1,472 +0,0 @@
|
|
|
1
|
-
# KIR: k Intermediate Representation, Envelope Specialization, and Conformance
|
|
2
|
-
|
|
3
|
-
The document defines an object IR and conformance platform for LLVM and
|
|
4
|
-
WebAssembly backends.
|
|
5
|
-
|
|
6
|
-
## Goals
|
|
7
|
-
|
|
8
|
-
- Define a portable polymorphic object IR.
|
|
9
|
-
- Define envelope specialization for concrete input envelope patterns.
|
|
10
|
-
- Define conformance fixtures for interpreter, object execution,
|
|
11
|
-
envelope-specialized execution, and future backends.
|
|
12
|
-
- Keep `.ko` and `.klib` inspectable.
|
|
13
|
-
- Record type-derivation convergence per relation.
|
|
14
|
-
|
|
15
|
-
## Out of Scope
|
|
16
|
-
|
|
17
|
-
- LLVM backend design.
|
|
18
|
-
- WebAssembly backend design.
|
|
19
|
-
- New language syntax.
|
|
20
|
-
- Replacing the current JavaScript evaluator.
|
|
21
|
-
|
|
22
|
-
## Execution Layers
|
|
23
|
-
|
|
24
|
-
### KIR-P: Polymorphic IR
|
|
25
|
-
|
|
26
|
-
KIR-P is the portable representation of a k relation. It keeps the current
|
|
27
|
-
pattern-carrying execution model.
|
|
28
|
-
|
|
29
|
-
KIR-P must:
|
|
30
|
-
|
|
31
|
-
- run any valid k program;
|
|
32
|
-
- preserve filters and pattern operations;
|
|
33
|
-
- store relation input and output patterns;
|
|
34
|
-
- be the canonical `.ko` / `.klib` relation format;
|
|
35
|
-
- be the reference for envelope specialization and backend tests.
|
|
36
|
-
|
|
37
|
-
Alternative:
|
|
38
|
-
|
|
39
|
-
- Store source and re-run type derivation on load. This gives smaller objects,
|
|
40
|
-
but makes object behavior depend on the compiler version. Do not use this for
|
|
41
|
-
KIR.
|
|
42
|
-
|
|
43
|
-
### Envelope-Specialized KIR-P
|
|
44
|
-
|
|
45
|
-
Envelope-Specialized KIR-P is produced from `KIR-P + input envelope pattern`.
|
|
46
|
-
|
|
47
|
-
Envelope Specialization must:
|
|
48
|
-
|
|
49
|
-
- be equivalent to typing the entry expression `?P __main__`;
|
|
50
|
-
- derive the output pattern for that invocation;
|
|
51
|
-
- validate filters and pattern constraints before value execution;
|
|
52
|
-
- preserve the same value-level partial function semantics;
|
|
53
|
-
- support multiple pattern contexts for the same helper relation internally;
|
|
54
|
-
- be cacheable by relation hash and input pattern hash without serializing that
|
|
55
|
-
cache key into KIR.
|
|
56
|
-
|
|
57
|
-
Alternative:
|
|
58
|
-
|
|
59
|
-
- Re-run full source compilation for each input pattern. This is acceptable for
|
|
60
|
-
the first prototype, but the public API should still be
|
|
61
|
-
`specializeEnvelope(partialFunction, inputEnvelope)`. Current implementation identifiers
|
|
62
|
-
may still use `retype...` until they are renamed.
|
|
63
|
-
|
|
64
|
-
### KIR-M: Backend IR
|
|
65
|
-
|
|
66
|
-
KIR-M is produced from envelope-specialized KIR-P after call-site pattern contexts, layout,
|
|
67
|
-
and ABI decisions are fixed.
|
|
68
|
-
|
|
69
|
-
KIR-M should:
|
|
70
|
-
|
|
71
|
-
- use static product field offsets;
|
|
72
|
-
- use static union tag dispatch;
|
|
73
|
-
- represent partial failure explicitly;
|
|
74
|
-
- avoid envelope propagation only where call-site pattern contexts prove it safe;
|
|
75
|
-
- require converged type derivation for every compiled relation;
|
|
76
|
-
- target LLVM, Wasm, C, or another backend.
|
|
77
|
-
|
|
78
|
-
Current baseline:
|
|
79
|
-
|
|
80
|
-
- Lower KIR-P directly to kVM. This gives a single backend-facing contract for
|
|
81
|
-
both polymorphic and envelope-specialized relations. Later KIR-M layout work can specialize
|
|
82
|
-
the same KIR-P input further instead of adding another serialized relation
|
|
83
|
-
shape.
|
|
84
|
-
|
|
85
|
-
Relationship to kVM:
|
|
86
|
-
|
|
87
|
-
- [KVM_EXECUTION_MODEL.md](KVM_EXECUTION_MODEL.md) sketches kVM as the concrete
|
|
88
|
-
execution contract for KIR-M.
|
|
89
|
-
- envelope specialization remains a pass that emits ordinary KIR-P.
|
|
90
|
-
- kVM lowering consumes KIR-P relation bodies directly.
|
|
91
|
-
- kVM keeps product and union as structured regions so backends can choose
|
|
92
|
-
sequential execution first, then safe parallel scheduling later.
|
|
93
|
-
|
|
94
|
-
## Object IR
|
|
95
|
-
|
|
96
|
-
### Step 1: Define KIR-P
|
|
97
|
-
|
|
98
|
-
Add `DOCS/KIR_V1.md`.
|
|
99
|
-
|
|
100
|
-
Define:
|
|
101
|
-
|
|
102
|
-
- top-level object fields: `format`, `codes`, `rels`, `relAlias`,
|
|
103
|
-
`compileStats`, `meta`, and `main`;
|
|
104
|
-
- canonical code table;
|
|
105
|
-
- polymorphic relation table;
|
|
106
|
-
- relation aliases;
|
|
107
|
-
- metadata;
|
|
108
|
-
- per-relation type-derivation status;
|
|
109
|
-
- optional origin source range;
|
|
110
|
-
- `.ko` main relation;
|
|
111
|
-
- `.klib` null main.
|
|
112
|
-
|
|
113
|
-
Current storage rules to preserve:
|
|
114
|
-
|
|
115
|
-
- `.klib` is plain UTF-8 JSON with no binary header;
|
|
116
|
-
- `.ko` is a `KOBJ\n` binary container around the JSON payload;
|
|
117
|
-
- no payload version field is stored;
|
|
118
|
-
- relation `typeDerivation` stores only `status`;
|
|
119
|
-
- code entries do not carry `typeDerivation`;
|
|
120
|
-
- relation bodies do not store generated input/output boundary filters;
|
|
121
|
-
- source ranges live only in metadata origin entries;
|
|
122
|
-
- origin entries do not have `kind`; the metadata entry has `type`.
|
|
123
|
-
|
|
124
|
-
Implementation:
|
|
125
|
-
|
|
126
|
-
- Add `kir.mjs`.
|
|
127
|
-
- Add a converter from current object payloads to KIR-P.
|
|
128
|
-
- Keep current object storage until KIR-P is validated.
|
|
129
|
-
|
|
130
|
-
Alternative:
|
|
131
|
-
|
|
132
|
-
- Change the stored object payload immediately. This is less code, but risks
|
|
133
|
-
mixing backend contracts with current JS evaluator internals.
|
|
134
|
-
|
|
135
|
-
### Step 1a: Record Type-Derivation Status
|
|
136
|
-
|
|
137
|
-
Each relation definition should carry:
|
|
138
|
-
|
|
139
|
-
```json
|
|
140
|
-
{
|
|
141
|
-
"typeDerivation": {
|
|
142
|
-
"status": "converged"
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
Rules:
|
|
148
|
-
|
|
149
|
-
- valid statuses are `converged`, `not-converged`, and `unknown`;
|
|
150
|
-
- LLVM and Wasm compilation require `converged`;
|
|
151
|
-
- codes/types do not carry type-derivation status;
|
|
152
|
-
- `.ko` and `.klib` may still store non-converged relations;
|
|
153
|
-
- object execution may still use the JS evaluator for non-converged relations.
|
|
154
|
-
|
|
155
|
-
Alternative:
|
|
156
|
-
|
|
157
|
-
- Store one object-level `backendEligible` flag. This hides which relation
|
|
158
|
-
blocks backend compilation and is not enough for libraries.
|
|
159
|
-
|
|
160
|
-
### Step 2: Normalize Opcodes
|
|
161
|
-
|
|
162
|
-
KIR-P should use a closed opcode set:
|
|
163
|
-
|
|
164
|
-
```json
|
|
165
|
-
{ "op": "identity" }
|
|
166
|
-
{ "op": "empty" }
|
|
167
|
-
{ "op": "dot", "label": "x" }
|
|
168
|
-
{ "op": "div", "tag": "some" }
|
|
169
|
-
{ "op": "vid", "tag": "some" }
|
|
170
|
-
{ "op": "code", "code": "@..." }
|
|
171
|
-
{ "op": "filter", "filter": {} }
|
|
172
|
-
{ "op": "ref", "ref": "@..." }
|
|
173
|
-
{ "op": "comp", "items": [] }
|
|
174
|
-
{ "op": "union", "items": [] }
|
|
175
|
-
{ "op": "product", "fields": [{ "label": "x", "expr": {} }] }
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
Alternative:
|
|
179
|
-
|
|
180
|
-
- Keep current AST field names. This is faster now, but backend code should not
|
|
181
|
-
depend on parser-shaped fields.
|
|
182
|
-
|
|
183
|
-
### Step 3: Normalize Codes
|
|
184
|
-
|
|
185
|
-
Represent codes as canonical product or union nodes:
|
|
186
|
-
|
|
187
|
-
```json
|
|
188
|
-
{
|
|
189
|
-
"@hash": {
|
|
190
|
-
"kind": "product",
|
|
191
|
-
"fields": [{ "label": "x", "code": "@..." }],
|
|
192
|
-
"canonical": "$C0={...};"
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
Rules:
|
|
198
|
-
|
|
199
|
-
- Sort labels and tags canonically.
|
|
200
|
-
- Store references as canonical hashes.
|
|
201
|
-
- Validate that each hash matches its canonical form.
|
|
202
|
-
|
|
203
|
-
Alternative:
|
|
204
|
-
|
|
205
|
-
- Omit `canonical` and recompute it. This reduces payload size. Keeping it helps
|
|
206
|
-
debugging and validation during the POC.
|
|
207
|
-
|
|
208
|
-
### Step 4: Normalize Pattern Graphs
|
|
209
|
-
|
|
210
|
-
Do not expose `TypePatternGraph` internals as the backend contract.
|
|
211
|
-
|
|
212
|
-
Use:
|
|
213
|
-
|
|
214
|
-
```json
|
|
215
|
-
{
|
|
216
|
-
"root": 0,
|
|
217
|
-
"nodes": [
|
|
218
|
-
{
|
|
219
|
-
"id": 0,
|
|
220
|
-
"kind": "open-product",
|
|
221
|
-
"edges": [{ "label": "x", "target": 1 }]
|
|
222
|
-
}
|
|
223
|
-
]
|
|
224
|
-
}
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
Rules:
|
|
228
|
-
|
|
229
|
-
- Valid kinds: `any`, `open-product`, `closed-product`, `open-union`,
|
|
230
|
-
`closed-union`, `type`.
|
|
231
|
-
- Type nodes carry a code hash.
|
|
232
|
-
- Edge targets must exist.
|
|
233
|
-
- Edge order must be canonical.
|
|
234
|
-
|
|
235
|
-
Alternative:
|
|
236
|
-
|
|
237
|
-
- Store union-find state as debug metadata. Do not use it as backend input.
|
|
238
|
-
|
|
239
|
-
## Envelope Specialization
|
|
240
|
-
|
|
241
|
-
### Step 5: Add Envelope Specialization
|
|
242
|
-
|
|
243
|
-
Input:
|
|
244
|
-
|
|
245
|
-
- KIR-P relation;
|
|
246
|
-
- concrete canonical input pattern;
|
|
247
|
-
- code table;
|
|
248
|
-
- relation table.
|
|
249
|
-
|
|
250
|
-
Output:
|
|
251
|
-
|
|
252
|
-
- envelope-specialized KIR-P relation;
|
|
253
|
-
- output pattern;
|
|
254
|
-
- compiler-local call-site worklist.
|
|
255
|
-
|
|
256
|
-
Algorithm:
|
|
257
|
-
|
|
258
|
-
1. Type the entry as if the program were `?inputPattern __main__`.
|
|
259
|
-
2. Re-run pattern derivation over the reachable relation graph.
|
|
260
|
-
3. Preserve value-level partial operations.
|
|
261
|
-
4. Record the derived output pattern.
|
|
262
|
-
5. Record call-site pattern contexts for helper calls.
|
|
263
|
-
|
|
264
|
-
Alternative:
|
|
265
|
-
|
|
266
|
-
- Implement this inside `run.mjs` first. This may be useful for bootstrapping,
|
|
267
|
-
but the final API should be separate from the evaluator.
|
|
268
|
-
|
|
269
|
-
### Step 6: Execution After Envelope Specialization
|
|
270
|
-
|
|
271
|
-
Safe baseline:
|
|
272
|
-
|
|
273
|
-
- keep current envelope-carrying execution;
|
|
274
|
-
- use envelope-specialized KIR-P only to derive the top-level output pattern;
|
|
275
|
-
- treat filters as already validated by envelope specialization.
|
|
276
|
-
|
|
277
|
-
Optimized mode:
|
|
278
|
-
|
|
279
|
-
- remove envelope propagation only when every call site has a known stable
|
|
280
|
-
pattern context;
|
|
281
|
-
- keep value-level partiality for `.field`, `/tag`, merge, and composition;
|
|
282
|
-
- attach the derived output pattern after execution.
|
|
283
|
-
|
|
284
|
-
Alternative:
|
|
285
|
-
|
|
286
|
-
- Never remove envelopes in the JS evaluator. This is simplest and remains the
|
|
287
|
-
reference path. Envelope-free execution can be left to KIR-M.
|
|
288
|
-
|
|
289
|
-
### Step 7: Call-Site Envelope Specialization
|
|
290
|
-
|
|
291
|
-
One relation may be called under multiple patterns:
|
|
292
|
-
|
|
293
|
-
```text
|
|
294
|
-
f@P1
|
|
295
|
-
f@P2
|
|
296
|
-
```
|
|
297
|
-
|
|
298
|
-
The envelope specialization cache should model these as separate instances:
|
|
299
|
-
|
|
300
|
-
```text
|
|
301
|
-
relation hash + input pattern hash
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
Recursive definitions require fixed-point iteration over these instances.
|
|
305
|
-
|
|
306
|
-
Alternative:
|
|
307
|
-
|
|
308
|
-
- Use one annotation per relation. This is easier, but too coarse for precise
|
|
309
|
-
output patterns and unsafe for envelope-free execution.
|
|
310
|
-
|
|
311
|
-
### Step 8: Cache Envelope Specialization Results
|
|
312
|
-
|
|
313
|
-
Cache key:
|
|
314
|
-
|
|
315
|
-
```text
|
|
316
|
-
relation-hash + input-pattern-hash
|
|
317
|
-
```
|
|
318
|
-
|
|
319
|
-
Cache value:
|
|
320
|
-
|
|
321
|
-
- envelope-specialized KIR-P relation;
|
|
322
|
-
- output pattern;
|
|
323
|
-
- optional compiler-local call-site worklist;
|
|
324
|
-
- optional backend artifact.
|
|
325
|
-
|
|
326
|
-
Alternative:
|
|
327
|
-
|
|
328
|
-
- Cache by a stable pattern encoding first. Replace with pattern hashes once
|
|
329
|
-
pattern hashing is stable.
|
|
330
|
-
|
|
331
|
-
## Conformance
|
|
332
|
-
|
|
333
|
-
### Step 9: Fixture Format
|
|
334
|
-
|
|
335
|
-
Use one directory per case:
|
|
336
|
-
|
|
337
|
-
```text
|
|
338
|
-
conformance/projection-field/
|
|
339
|
-
case.json
|
|
340
|
-
program.k
|
|
341
|
-
input.kv
|
|
342
|
-
expected.kv
|
|
343
|
-
```
|
|
344
|
-
|
|
345
|
-
Example:
|
|
346
|
-
|
|
347
|
-
```json
|
|
348
|
-
{
|
|
349
|
-
"name": "project field",
|
|
350
|
-
"program": "program.k",
|
|
351
|
-
"input": "input.kv",
|
|
352
|
-
"expected": "expected.kv",
|
|
353
|
-
"modes": ["source", "object", "envelope-specialized"],
|
|
354
|
-
"tags": ["runtime", "projection"]
|
|
355
|
-
}
|
|
356
|
-
```
|
|
357
|
-
|
|
358
|
-
Alternative:
|
|
359
|
-
|
|
360
|
-
- Store all fixtures in one JSON file. This is compact but harder to debug.
|
|
361
|
-
|
|
362
|
-
### Step 10: Runner
|
|
363
|
-
|
|
364
|
-
Add `conformance/run.mjs`.
|
|
365
|
-
|
|
366
|
-
Modes:
|
|
367
|
-
|
|
368
|
-
- `source`: compile source and run the JS evaluator;
|
|
369
|
-
- `object`: compile `.ko`, load it, and run object evaluation;
|
|
370
|
-
- `envelope-specialized`: specialize using the input wire pattern and run the envelope-specialized evaluator;
|
|
371
|
-
- future `wasm` / `llvm`: run backend artifacts.
|
|
372
|
-
|
|
373
|
-
Comparison:
|
|
374
|
-
|
|
375
|
-
- Parse `input.kv` through `k-parse`.
|
|
376
|
-
- Run one mode.
|
|
377
|
-
- Print through `k-print`.
|
|
378
|
-
- Compare normalized textual output.
|
|
379
|
-
|
|
380
|
-
Alternative:
|
|
381
|
-
|
|
382
|
-
- Compare JS values directly. This is faster, but fixtures should not depend on
|
|
383
|
-
JS object representation.
|
|
384
|
-
|
|
385
|
-
### Step 11: Initial Fixtures
|
|
386
|
-
|
|
387
|
-
Runtime:
|
|
388
|
-
|
|
389
|
-
- identity;
|
|
390
|
-
- product projection;
|
|
391
|
-
- union projection;
|
|
392
|
-
- variant construction;
|
|
393
|
-
- composition;
|
|
394
|
-
- merge fallback;
|
|
395
|
-
- product construction.
|
|
396
|
-
|
|
397
|
-
Typing and patterns:
|
|
398
|
-
|
|
399
|
-
- `?filter` expression;
|
|
400
|
-
- open product filter;
|
|
401
|
-
- open union filter;
|
|
402
|
-
- recursive list/tree filter;
|
|
403
|
-
- incompatible filter error.
|
|
404
|
-
|
|
405
|
-
Objects and libraries:
|
|
406
|
-
|
|
407
|
-
- compile `.klib`;
|
|
408
|
-
- compile `.ko` with `--lib`;
|
|
409
|
-
- load aliases from metadata;
|
|
410
|
-
- `:load --no-alias`;
|
|
411
|
-
- reload `extract-aliases` output.
|
|
412
|
-
|
|
413
|
-
Codecs:
|
|
414
|
-
|
|
415
|
-
- unit;
|
|
416
|
-
- nested product;
|
|
417
|
-
- nested variant;
|
|
418
|
-
- Unicode labels;
|
|
419
|
-
- explicit pattern vs derived witness pattern.
|
|
420
|
-
|
|
421
|
-
Envelope Specialization:
|
|
422
|
-
|
|
423
|
-
- output pattern derived from `?P __main__`;
|
|
424
|
-
- helper called under two different patterns;
|
|
425
|
-
- recursive helper envelope specialization convergence;
|
|
426
|
-
- envelope specialization cache reuse.
|
|
427
|
-
|
|
428
|
-
Backend eligibility:
|
|
429
|
-
|
|
430
|
-
- converged relation compiles to KIR-M;
|
|
431
|
-
- non-converged relation is rejected by LLVM/Wasm compiler;
|
|
432
|
-
- `.klib` with mixed relations reports the exact rejected aliases.
|
|
433
|
-
|
|
434
|
-
Alternative:
|
|
435
|
-
|
|
436
|
-
- Start with fuzzing. Do this later. First fixtures should be deterministic and
|
|
437
|
-
readable.
|
|
438
|
-
|
|
439
|
-
## Tooling Ideas
|
|
440
|
-
|
|
441
|
-
- `k-inspect-object`: print object sections, KIR-P, aliases, metadata, and
|
|
442
|
-
pattern summaries.
|
|
443
|
-
- `k-validate-object`: validate `.ko` and `.klib` schemas and references.
|
|
444
|
-
- `k-specialize-envelope`: precompute envelope-specialized KIR-P for one relation and input pattern.
|
|
445
|
-
- Envelope Specialization heat counters in the JS runtime.
|
|
446
|
-
- Pattern-hash backend artifact cache.
|
|
447
|
-
- Optional binary executable object payload after KIR stabilizes.
|
|
448
|
-
- Minimal C backend before LLVM.
|
|
449
|
-
- Wasm envelope-specialized KIR interpreter before native LLVM.
|
|
450
|
-
|
|
451
|
-
## Implementation Order
|
|
452
|
-
|
|
453
|
-
1. Write `DOCS/KIR_V1.md`.
|
|
454
|
-
2. Treat current `.ko` / `.klib` storage as the initial KIR-P input artifact.
|
|
455
|
-
3. Add `kir.mjs` to export KIR-P from current objects.
|
|
456
|
-
4. Add `objects/inspect.mjs --kir`.
|
|
457
|
-
5. Add `objects/validate.mjs`.
|
|
458
|
-
6. Add conformance fixture format and runner.
|
|
459
|
-
7. Add entry envelope specialization for `?P __main__`.
|
|
460
|
-
8. Add an envelope-specialized KIR JS evaluator or envelope-specialized execution wrapper.
|
|
461
|
-
9. Compare envelope-specialized KIR execution against `run.mjs`.
|
|
462
|
-
10. Add call-site envelope specialization for helper relations.
|
|
463
|
-
11. Add envelope specialization cache.
|
|
464
|
-
12. Reject non-converged relations in LLVM/Wasm compilation.
|
|
465
|
-
13. Prototype a small C or Wasm backend through kVM.
|
|
466
|
-
|
|
467
|
-
Rule:
|
|
468
|
-
|
|
469
|
-
- KIR-P is the portable semantic contract.
|
|
470
|
-
- envelope specialization emits KIR-P and may use local cache keys/worklists.
|
|
471
|
-
- KIR-M is backend material; kVM is the proposed concrete execution model for
|
|
472
|
-
that material.
|
package/DOCS/Makefile
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
# Dependencies: pandoc, dejavu-fonts-all, texlive-xetex
|
|
2
|
-
|
|
3
|
-
CHAPTERS = book/01-introduction.md \
|
|
4
|
-
book/02-syntax-and-values.md \
|
|
5
|
-
book/03-types-as-finite-automata.md \
|
|
6
|
-
book/04-partial-functions-and-composition.md \
|
|
7
|
-
book/05-typing-filters-and-normalization.md \
|
|
8
|
-
book/06-values-in-memory.md \
|
|
9
|
-
book/07-the-partial-function-abi.md \
|
|
10
|
-
book/08-operational-semantics-and-execution.md \
|
|
11
|
-
book/09-compiler-architecture.md \
|
|
12
|
-
book/10-from-ast-to-intermediate-representation.md \
|
|
13
|
-
book/11-llvm-basics.md \
|
|
14
|
-
book/12-code-generation.md \
|
|
15
|
-
book/13-linking-and-execution.md \
|
|
16
|
-
book/14-canonical-serialization.md \
|
|
17
|
-
book/15-optimization-and-folding.md \
|
|
18
|
-
book/16-toward-a-universal-schema-registry.md \
|
|
19
|
-
book/appendix-a-implementing-a-prototype-compiler-in-python.md \
|
|
20
|
-
book/appendix-b-incorporating-external-predefined-types-and-functions.md
|
|
21
|
-
|
|
22
|
-
all: book-combined.pdf book-combined.html book-combined.tex book.pdf book.html book.tex
|
|
23
|
-
|
|
24
|
-
# Combined book from individual chapters
|
|
25
|
-
book-combined.md: $(CHAPTERS)
|
|
26
|
-
echo "---" > book-combined.md
|
|
27
|
-
echo "title: \"k-language\"" >> book-combined.md
|
|
28
|
-
echo "author: \"W. Fraczak\"" >> book-combined.md
|
|
29
|
-
echo "---" >> book-combined.md
|
|
30
|
-
echo "" >> book-combined.md
|
|
31
|
-
echo "<div align=\"center\">" >> book-combined.md
|
|
32
|
-
echo "" >> book-combined.md
|
|
33
|
-
echo "\`\`\`" >> book-combined.md
|
|
34
|
-
echo " { } { }" >> book-combined.md
|
|
35
|
-
echo " / \\ {.lang name} |" >> book-combined.md
|
|
36
|
-
echo " lang by --> name" >> book-combined.md
|
|
37
|
-
echo " | | |" >> book-combined.md
|
|
38
|
-
echo " 'k' 'wojtek' 'k'" >> book-combined.md
|
|
39
|
-
echo "\`\`\`" >> book-combined.md
|
|
40
|
-
echo "" >> book-combined.md
|
|
41
|
-
echo "</div>" >> book-combined.md
|
|
42
|
-
echo "" >> book-combined.md
|
|
43
|
-
echo "# Table of Contents" >> book-combined.md
|
|
44
|
-
echo "" >> book-combined.md
|
|
45
|
-
echo "1. [Introduction](#chapter-1-introduction)" >> book-combined.md
|
|
46
|
-
echo "2. [Syntax and Values](#chapter-2-syntax-and-values)" >> book-combined.md
|
|
47
|
-
echo "3. [Types as Finite Automata](#chapter-3-types-as-finite-automata)" >> book-combined.md
|
|
48
|
-
echo "4. [Partial Functions and Composition](#chapter-4-partial-functions-and-composition)" >> book-combined.md
|
|
49
|
-
echo "5. [Typing, Filters, and Normalization](#chapter-5-typing-filters-and-normalization)" >> book-combined.md
|
|
50
|
-
echo "6. [Values in Memory](#chapter-6-values-in-memory)" >> book-combined.md
|
|
51
|
-
echo "7. [The Partial Function ABI](#chapter-7-the-partial-function-abi)" >> book-combined.md
|
|
52
|
-
echo "8. [Operational Semantics and Execution](#chapter-8-operational-semantics-and-execution)" >> book-combined.md
|
|
53
|
-
echo "9. [Compiler Architecture](#chapter-9-compiler-architecture)" >> book-combined.md
|
|
54
|
-
echo "10. [From AST to Intermediate Representation](#chapter-10-from-ast-to-intermediate-representation)" >> book-combined.md
|
|
55
|
-
echo "11. [LLVM Basics](#chapter-11-llvm-basics)" >> book-combined.md
|
|
56
|
-
echo "12. [Code Generation](#chapter-12-code-generation)" >> book-combined.md
|
|
57
|
-
echo "13. [Linking and Execution](#chapter-13-linking-and-execution)" >> book-combined.md
|
|
58
|
-
echo "14. [Canonical Serialization](#chapter-14-canonical-serialization)" >> book-combined.md
|
|
59
|
-
echo "15. [Optimization and Folding](#chapter-15-optimization-and-folding)" >> book-combined.md
|
|
60
|
-
echo "16. [Toward a Universal Schema Registry](#chapter-16-toward-a-universal-schema-registry)" >> book-combined.md
|
|
61
|
-
echo "" >> book-combined.md
|
|
62
|
-
echo "## Appendices" >> book-combined.md
|
|
63
|
-
echo "" >> book-combined.md
|
|
64
|
-
echo "- [Appendix A: Implementing a Prototype Compiler in Python](#appendix-a-implementing-a-prototype-compiler-in-python)" >> book-combined.md
|
|
65
|
-
echo "- [Appendix B: Incorporating External Predefined Types and Functions](#appendix-b-incorporating-external-predefined-types-and-functions)" >> book-combined.md
|
|
66
|
-
echo "" >> book-combined.md
|
|
67
|
-
echo "---" >> book-combined.md
|
|
68
|
-
echo "" >> book-combined.md
|
|
69
|
-
for chapter in $(CHAPTERS); do \
|
|
70
|
-
echo "" >> book-combined.md; \
|
|
71
|
-
cat $$chapter >> book-combined.md; \
|
|
72
|
-
echo "" >> book-combined.md; \
|
|
73
|
-
echo "---" >> book-combined.md; \
|
|
74
|
-
echo "" >> book-combined.md; \
|
|
75
|
-
done
|
|
76
|
-
|
|
77
|
-
book-combined.tex: book-combined.md
|
|
78
|
-
pandoc book-combined.md -t latex -s -o book-combined.tex
|
|
79
|
-
|
|
80
|
-
book-combined.pdf: book-combined.md
|
|
81
|
-
pandoc book-combined.md --pdf-engine=xelatex -V monofont="DejaVu Sans Mono" -V mainfont="DejaVu Serif" -o book-combined.pdf
|
|
82
|
-
|
|
83
|
-
book-combined.html: book-combined.md
|
|
84
|
-
pandoc book-combined.md -o book-combined.html
|
|
85
|
-
|
|
86
|
-
# Master file (table of contents only)
|
|
87
|
-
book.tex: book.md
|
|
88
|
-
pandoc book.md -t latex -s -o book.tex
|
|
89
|
-
|
|
90
|
-
book.pdf: book.md
|
|
91
|
-
pandoc book.md --pdf-engine=xelatex -V monofont="DejaVu Sans Mono" -V mainfont="DejaVu Serif" -o book.pdf
|
|
92
|
-
|
|
93
|
-
book.html: book.md
|
|
94
|
-
pandoc book.md -o book.html
|
|
95
|
-
|
|
96
|
-
clean:
|
|
97
|
-
rm -f book.pdf book.html book.tex book-combined.* *.aux *.log *.out
|
|
98
|
-
|
|
99
|
-
.PHONY: all clean
|