@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
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# Getting started
|
|
2
|
+
|
|
3
|
+
Use the [browser REPL](https://fraczak.github.io/k/) or start a local session
|
|
4
|
+
from the repository root:
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npm install
|
|
8
|
+
node repl.mjs
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Use Node.js 22+ for development. The examples below are REPL inputs; `>` is
|
|
12
|
+
the prompt, not part of the program.
|
|
13
|
+
|
|
14
|
+
## Unit, variants, and products
|
|
15
|
+
|
|
16
|
+
The current input starts as the empty product `{}`, called *unit*.
|
|
17
|
+
Each expression transforms the current input and makes its result the next input.
|
|
18
|
+
|
|
19
|
+
```k
|
|
20
|
+
{} |hello
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`{}` produces unit and `|hello` wraps it in a variant tagged `hello`.
|
|
24
|
+
The REPL prints the value followed by its pattern envelope and timing.
|
|
25
|
+
The value part is `{}|hello`.
|
|
26
|
+
|
|
27
|
+
A product constructs several fields from the same input:
|
|
28
|
+
|
|
29
|
+
```k
|
|
30
|
+
{ {} |hello x, {} |world y }
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Project a field with `.x`. Extract a variant payload with `/hello`:
|
|
34
|
+
|
|
35
|
+
```k
|
|
36
|
+
.x /hello
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The result is unit again.
|
|
40
|
+
|
|
41
|
+
## Relations and ordered choice
|
|
42
|
+
|
|
43
|
+
Define a reusable relation with `name = expression;`:
|
|
44
|
+
|
|
45
|
+
```k
|
|
46
|
+
swap = { .x y, .y x };
|
|
47
|
+
{ {} |hello x, {} |world y } swap
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Expressions composed left to right pass each result to the next expression.
|
|
51
|
+
Parentheses group a composition; `()` is identity.
|
|
52
|
+
|
|
53
|
+
Relations may be undefined. Choice tries branches in source order and uses
|
|
54
|
+
the first defined result:
|
|
55
|
+
|
|
56
|
+
```k
|
|
57
|
+
choose = < /left, /right >;
|
|
58
|
+
{} |hello |right choose
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
The value part is `{}|hello`. The `/left` branch is undefined for a
|
|
62
|
+
`right` variant, so `/right` supplies the result. A diverging branch does
|
|
63
|
+
not count as an undefined result.
|
|
64
|
+
|
|
65
|
+
The empty choice `<>` is always undefined. An undefined evaluation leaves the
|
|
66
|
+
REPL's current input unchanged. Structural type errors are reported separately.
|
|
67
|
+
|
|
68
|
+
## Schemas and filters
|
|
69
|
+
|
|
70
|
+
Named patterns describe reusable structures:
|
|
71
|
+
|
|
72
|
+
```k
|
|
73
|
+
bool[] = < {} false, {} true >;
|
|
74
|
+
not = ?bool[] < /false |true, /true |false > ?bool[];
|
|
75
|
+
{} |true not
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
The result is the `false` variant. `?bool[]` constrains the schema while
|
|
79
|
+
leaving the payload unchanged. The compiler derives input/output constraints;
|
|
80
|
+
runtime boundaries validate arriving schemas, and optimized paths can erase
|
|
81
|
+
guards already proved by specialization.
|
|
82
|
+
|
|
83
|
+
Square brackets distinguish pattern applications such as `bool[]` from
|
|
84
|
+
relation names such as `not`. The [language reference](PATTERNS.md) explains
|
|
85
|
+
variables, recursive knots, and parameterized patterns.
|
|
86
|
+
|
|
87
|
+
## Recursion: Peano numbers
|
|
88
|
+
|
|
89
|
+
Load the small arithmetic example:
|
|
90
|
+
|
|
91
|
+
```text
|
|
92
|
+
:load Examples/nat.k
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Its number schema is:
|
|
96
|
+
|
|
97
|
+
```k
|
|
98
|
+
nat[] = < {} 0, N +1 > = N;
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
A natural number is either zero or another natural wrapped in `+1`.
|
|
102
|
+
The binding `= N` closes the recursive cycle.
|
|
103
|
+
|
|
104
|
+
The library defines `0`, `succ`, `pred`, `add`, and `mult`. Try:
|
|
105
|
+
|
|
106
|
+
```k
|
|
107
|
+
0 succ succ
|
|
108
|
+
{ 0 succ succ x, 0 succ y } add
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
The second expression computes 2 + 1. Its value part is
|
|
112
|
+
`{}|0|+1|+1|+1`. These are ordinary relation names, not builtin numeric
|
|
113
|
+
literals.
|
|
114
|
+
|
|
115
|
+
Recursive filters give the contract-derivation engine finite structural
|
|
116
|
+
constraints. Derivation convergence does not prove that every program terminates.
|
|
117
|
+
|
|
118
|
+
## Libraries and external numbers
|
|
119
|
+
|
|
120
|
+
Load the integer library and a codec explicitly:
|
|
121
|
+
|
|
122
|
+
```text
|
|
123
|
+
:load Examples/arithmetics.k
|
|
124
|
+
:codec load codecs/int.mjs
|
|
125
|
+
:input int.mjs 42
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Now evaluate `inc` to obtain 43. The codec prints the decimal representation
|
|
129
|
+
alongside the structural value.
|
|
130
|
+
|
|
131
|
+
For list operations, load the arithmetic library first, then:
|
|
132
|
+
|
|
133
|
+
```text
|
|
134
|
+
:load Examples/poly.k
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
For a smaller self-contained list example, use `:load Examples/list.k`.
|
|
138
|
+
See the [REPL guide](REPL.md) for loading, pattern inspection, and exports.
|
|
139
|
+
|
|
140
|
+
## Shell pipelines
|
|
141
|
+
|
|
142
|
+
The command-line runtime receives binary schema+payload data, so use codecs
|
|
143
|
+
at text boundaries. From the repository root:
|
|
144
|
+
|
|
145
|
+
```sh
|
|
146
|
+
printf '42\n' | node codecs/int.mjs --parse |
|
|
147
|
+
node k.mjs --lib Examples/arithmetics.k --export inc 'inc' |
|
|
148
|
+
node codecs/int.mjs --print
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
This prints `43`. `--lib` loads definitions; `--export inc` brings the
|
|
152
|
+
friendly name into scope. Library relations are also available by their
|
|
153
|
+
content hashes.
|
|
154
|
+
|
|
155
|
+
To compile a small executable object and run it:
|
|
156
|
+
|
|
157
|
+
```sh
|
|
158
|
+
node objects/compile.mjs '{} |ok' /tmp/k-ok.ko
|
|
159
|
+
node codecs/unit.mjs --parse | node k.mjs /tmp/k-ok.ko |
|
|
160
|
+
node codecs/k-print.mjs
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
This prints `"ok"`. Continue with [file formats and tools](FILE_FORMATS.md)
|
|
164
|
+
or return to the [documentation index](README.md).
|
package/DOCS/KIR_V1.md
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
# KIR
|
|
1
|
+
# KIR-P export (version 1)
|
|
2
2
|
|
|
3
|
-
KIR
|
|
4
|
-
|
|
5
|
-
for the current `.ko` and `.klib` containers.
|
|
3
|
+
KIR-P is the portable backend export contract for compiled k objects.
|
|
4
|
+
It does not replace the stored `.ko` and `.klib` containers.
|
|
6
5
|
|
|
7
|
-
The
|
|
8
|
-
KIR-P keeps the same pattern-carrying semantics as current object
|
|
6
|
+
The format keeps the same pattern-carrying semantics as current object
|
|
9
7
|
execution, but normalizes names and graph IDs so backends can consume
|
|
10
8
|
object contents without depending on parser-shaped fields, current
|
|
11
9
|
object storage, or `TypePatternGraph` internals.
|
|
@@ -24,7 +22,7 @@ KIR v1 defines:
|
|
|
24
22
|
|
|
25
23
|
KIR v1 does not define:
|
|
26
24
|
|
|
27
|
-
-
|
|
25
|
+
- execution layouts and target ABI decisions;
|
|
28
26
|
- LLVM, Wasm, C, or native runtime layout;
|
|
29
27
|
- a new stored object format;
|
|
30
28
|
- a required physical encoding.
|
|
@@ -81,9 +79,9 @@ Rules:
|
|
|
81
79
|
`patternGraph`.
|
|
82
80
|
- Valid type-derivation statuses are `converged`, `not-converged`, and
|
|
83
81
|
`unknown`.
|
|
84
|
-
-
|
|
85
|
-
|
|
86
|
-
envelope
|
|
82
|
+
- Export validity alone does not establish backend eligibility. Code generators
|
|
83
|
+
must establish the contracts needed by their execution path, using stored
|
|
84
|
+
derivation status and input-envelope specialization as appropriate.
|
|
87
85
|
- `sourceNodeMap` maps original object pattern graph representative IDs
|
|
88
86
|
to KIR node IDs. It exists for inspection and debugging, not as a
|
|
89
87
|
semantic contract.
|
|
@@ -149,8 +147,7 @@ Rules:
|
|
|
149
147
|
- `union.items` preserve source branch priority.
|
|
150
148
|
- `product.fields` preserve the relation body field list. Backends may
|
|
151
149
|
sort or lay out fields later only after preserving observable semantics.
|
|
152
|
-
- `filter.filter`
|
|
153
|
-
revision may normalize filter internals.
|
|
150
|
+
- `filter.filter` preserves the stored filter tree.
|
|
154
151
|
|
|
155
152
|
## Current Tooling
|
|
156
153
|
|
|
@@ -179,31 +176,24 @@ validate KIR-P export for deterministic fixtures:
|
|
|
179
176
|
npm run conformance
|
|
180
177
|
```
|
|
181
178
|
|
|
182
|
-
## Backend
|
|
179
|
+
## Backend path
|
|
183
180
|
|
|
184
|
-
The
|
|
181
|
+
The current kVM lowerer consumes KIR-P relation bodies directly:
|
|
185
182
|
|
|
186
183
|
```text
|
|
187
|
-
k source
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
-> envelope-specialized KIR-P for an input envelope
|
|
191
|
-
-> KIR-M / kVM (.kvm)
|
|
192
|
-
-> LLVM / Wasm / C / other backend
|
|
184
|
+
k source -> .ko / .klib -> KIR-P -> kVM template
|
|
185
|
+
+ input envelope
|
|
186
|
+
-> specialized kVM -> interpreter / Wasm / LLVM
|
|
193
187
|
```
|
|
194
188
|
|
|
195
|
-
KIR-P is
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
be encoded by overloading KIR-P fields. The current kVM lowerer consumes
|
|
199
|
-
KIR-P relation bodies directly.
|
|
189
|
+
KIR-P envelope specialization is also available as a separate derivation pass,
|
|
190
|
+
producing another KIR-P object. It is distinct from specialization of a lowered
|
|
191
|
+
kVM template. See [compiler architecture](ARCHITECTURE.md).
|
|
200
192
|
|
|
201
193
|
## Envelope Specialization
|
|
202
194
|
|
|
203
|
-
The
|
|
204
|
-
|
|
205
|
-
legacy/current implementation naming; new public names should use
|
|
206
|
-
envelope specialization. The pass re-runs existing type derivation as
|
|
195
|
+
The API `retypeObjectRelation(object, relationName, inputPattern)` exports
|
|
196
|
+
envelope-specialized KIR-P. The pass re-runs contract derivation as
|
|
207
197
|
if the entry program were:
|
|
208
198
|
|
|
209
199
|
```k
|
|
@@ -219,14 +209,13 @@ The result is a normal KIR-P executable object:
|
|
|
219
209
|
relation's `patternGraph`;
|
|
220
210
|
- ordinary KIR relation bodies in `rels`.
|
|
221
211
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
ordinary KIR-P relation typing. Relation/input-pattern cache keys and
|
|
212
|
+
The specialized entry pattern is explicit through ordinary KIR-P relation typing.
|
|
213
|
+
Relation/input-pattern cache keys and
|
|
225
214
|
call-site worklists are local compiler data; they are not part of the
|
|
226
215
|
serialized KIR artifact.
|
|
227
216
|
|
|
228
217
|
- [DOCS/FILE_FORMATS.md](./FILE_FORMATS.md) —
|
|
229
|
-
|
|
218
|
+
source, object, library, kVM, and PKIR workflows
|
|
230
219
|
- [DOCS/KVM_EXECUTION_MODEL.md](./KVM_EXECUTION_MODEL.md) —
|
|
231
220
|
kVM execution model and bytecode design
|
|
232
221
|
- [DOCS/DICTIONARY.md](./DICTIONARY.md) — concept names and terminology
|