@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/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Wojciech Fraczak
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,454 +1,116 @@
|
|
|
1
1
|
# k
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
k is a small language for describing algebraic data shapes and composing
|
|
4
|
+
transformations over them. Programs are first-order partial functions, called
|
|
5
|
+
*relations*, built from products, tagged unions, and composition. Named patterns
|
|
6
|
+
describe reusable schemas; the compiler derives structural input/output contracts.
|
|
4
7
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
**[Try k in the browser](https://fraczak.github.io/k/)** ·
|
|
9
|
+
**[Getting started](DOCS/GETTING_STARTED.md)** ·
|
|
10
|
+
**[Documentation](DOCS/README.md)**
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
The [paper on Zenodo](https://zenodo.org/records/23652382) introduces the language.
|
|
13
|
+
Its [LaTeX source](paper/k-paper.tex) and [PDF](paper/k-paper.pdf) are in this
|
|
14
|
+
repository.
|
|
12
15
|
|
|
13
|
-
|
|
14
|
-
define both data schemas and the relations that transform between them.
|
|
16
|
+
## A small example
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
engine, REPL, binary codec pipeline, object/library format, Node.js API,
|
|
18
|
-
and test suite.
|
|
19
|
-
|
|
20
|
-
## Why k?
|
|
21
|
-
|
|
22
|
-
Many systems need to answer the same questions:
|
|
23
|
-
|
|
24
|
-
- What schema does this data have?
|
|
25
|
-
- Which transformations are valid for that schema?
|
|
26
|
-
- What does a program accept, produce, or preserve?
|
|
27
|
-
- Can this transformation be serialized, tested, reused, or verified?
|
|
28
|
-
|
|
29
|
-
k explores a compact answer: define algebraic schemas, compose
|
|
30
|
-
partial relations over them, derive input/output contracts
|
|
31
|
-
automatically, and move values across process boundaries with a canonical,
|
|
32
|
-
content-addressable binary representation.
|
|
33
|
-
|
|
34
|
-
That makes k interesting as a foundation for:
|
|
35
|
-
|
|
36
|
-
- content-addressable schema registries and schema-to-schema pipelines
|
|
37
|
-
- binary codecs and canonical serialization
|
|
38
|
-
- protocol, hardware, or test-vector transformation pipelines
|
|
39
|
-
- teaching algebraic data types and compositional relational programming
|
|
40
|
-
- research into partial relations, finite tree automata, and typed IRs
|
|
41
|
-
|
|
42
|
-
## A Small Example
|
|
43
|
-
|
|
44
|
-
k can define recursive data and transformations in the same file. Peano
|
|
45
|
-
natural numbers are either `0` or one more than another natural number:
|
|
18
|
+
This relation swaps two fields:
|
|
46
19
|
|
|
47
20
|
```k
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
inc = | +1;
|
|
51
|
-
dec = / +1; # undefined for '0'
|
|
52
|
-
add = ?{ nat[] = N x, ... } <
|
|
53
|
-
{ .x dec x, .y inc y } add, # defined if 'x > 0'
|
|
54
|
-
.y # else, return 'y'
|
|
55
|
-
>;
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
`add` takes a product `{ x, y }`. If `x` has a `+1`, it moves that
|
|
59
|
-
successor from `x` to `y` and recurses. When that no longer applies, it
|
|
60
|
-
returns `y`. The inductive filter `?{ nat[] = N x, ... }`
|
|
61
|
-
explicitly witnesses that recursive calls shrink `x`.
|
|
62
|
-
|
|
63
|
-
In the REPL:
|
|
64
|
-
|
|
65
|
-
```text
|
|
66
|
-
> { 0 inc inc x, 0 inc y } add
|
|
67
|
-
{}|0|+1|+1|+1 ?<X0 +1, {} 0>=X0
|
|
21
|
+
swap = { .x y, .y x };
|
|
22
|
+
{ {} |hello x, {} |world y } swap
|
|
68
23
|
```
|
|
69
24
|
|
|
70
|
-
|
|
71
|
-
|
|
25
|
+
Each field expression receives the same input. The result has `world` under
|
|
26
|
+
`x` and `hello` under `y`. Relations can be undefined for some inputs, and
|
|
27
|
+
ordered choice `<f, g>` tries `g` when `f` returns undefined.
|
|
72
28
|
|
|
73
|
-
|
|
29
|
+
All values are built from products and variants. Integers and strings are
|
|
30
|
+
library-defined structures, with codecs for familiar external representations.
|
|
74
31
|
|
|
75
|
-
|
|
32
|
+
## Run from source
|
|
76
33
|
|
|
77
|
-
|
|
78
|
-
|
|
34
|
+
Use Node.js 22+ for development and the browser tests.
|
|
35
|
+
The published runtime supports Node.js 18+.
|
|
79
36
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
The Web REPL is self-contained, powered by an in-process WebAssembly
|
|
83
|
-
compiler and execution engine with built-in codecs, autocompletion,
|
|
84
|
-
interactive input, and standard libraries.
|
|
85
|
-
|
|
86
|
-
### From Source
|
|
87
|
-
|
|
88
|
-
From a checkout:
|
|
89
|
-
|
|
90
|
-
```bash
|
|
91
|
-
node --version # requires Node.js 18+
|
|
37
|
+
```sh
|
|
92
38
|
npm install
|
|
93
|
-
|
|
94
|
-
npm link
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
Then run a tiny binary pipeline:
|
|
98
|
-
|
|
99
|
-
```bash
|
|
100
|
-
k-unit --parse | k '{} |ok' | k-print
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
Expected output:
|
|
104
|
-
|
|
105
|
-
```json
|
|
106
|
-
"ok"
|
|
39
|
+
node repl.mjs
|
|
107
40
|
```
|
|
108
41
|
|
|
109
|
-
|
|
42
|
+
`npm install` generates the parsers. From another terminal at the repository
|
|
43
|
+
root, try a complete pipeline:
|
|
110
44
|
|
|
111
|
-
```
|
|
112
|
-
k-
|
|
45
|
+
```sh
|
|
46
|
+
node codecs/unit.mjs --parse | node k.mjs '{} |ok' | node codecs/k-print.mjs
|
|
113
47
|
```
|
|
114
48
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
```bash
|
|
118
|
-
npm run build:repl-html
|
|
119
|
-
# generates repl.html (open in any web browser)
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
Every installed command supports `-h` and `--help`.
|
|
123
|
-
|
|
124
|
-
## What k Gives You
|
|
125
|
-
|
|
126
|
-
**Algebraic data shapes and named patterns**
|
|
127
|
-
|
|
128
|
-
All data structures in k are built from products and tagged unions
|
|
129
|
-
(variants). Reusable named patterns describe structural type schemas
|
|
130
|
-
using square brackets `name[...]`:
|
|
131
|
-
|
|
132
|
-
```k
|
|
133
|
-
bit[] = < {} 0, {} 1 >;
|
|
134
|
-
bits[] = < {} _, B 0, B 1 > = B;
|
|
135
|
-
byte[] = {
|
|
136
|
-
bit[] 0, bit[] 1, bit[] 2, bit[] 3,
|
|
137
|
-
bit[] 4, bit[] 5, bit[] 6, bit[] 7
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
-- Parameterized type templates:
|
|
141
|
-
list[X] = < {} nil, { X car, Y cdr } cons > = Y;
|
|
142
|
-
pair[A, B] = { A first, B second };
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
Callers instantiate patterns using `? name[...]` as standalone filters
|
|
146
|
-
or inside relation parameter guards (`?{ nat[] = N x, ... }`). There are
|
|
147
|
-
no built-in primitive values: the empty product `{}` is the only leaf
|
|
148
|
-
in a non-recursive definition.
|
|
149
|
-
|
|
150
|
-
Like relations, patterns are **content-addressable**. Every definition
|
|
151
|
-
derives a minimal regular tree automaton identified by its cryptographic
|
|
152
|
-
hash `@hash`. A user-defined pattern name is an alias routing its
|
|
153
|
-
parameters into the canonical template slots, filling unexposed ports
|
|
154
|
-
with wildcards `(...)`:
|
|
155
|
-
|
|
156
|
-
```text
|
|
157
|
-
> a[] = {X x, Y y};
|
|
158
|
-
> b[X] = {X x, Y y};
|
|
159
|
-
> c[X,Y] = {X x, Y y};
|
|
160
|
-
> d[Y,X] = {X x, Y y};
|
|
161
|
-
> :pats
|
|
162
|
-
a[] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[(...),(...)]
|
|
163
|
-
b[X] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,(...)]
|
|
164
|
-
c[X,Y] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,Y]
|
|
165
|
-
d[Y,X] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,Y]
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
Canonical patterns can also be instantiated or defined directly by
|
|
169
|
-
hash (`? @hash[...]`), and inspected with `:pat <name>`. See
|
|
170
|
-
[DOCS/CANONICAL_PATTERNS.md](DOCS/CANONICAL_PATTERNS.md) for full details.
|
|
171
|
-
|
|
172
|
-
**The four-bracket architecture**
|
|
173
|
-
|
|
174
|
-
k uses an orthogonal four-bracket convention with non-overlapping
|
|
175
|
-
semantic domains:
|
|
176
|
-
|
|
177
|
-
| Brackets | Domain | Purpose & Concrete Example |
|
|
178
|
-
| :--- | :--- | :--- |
|
|
179
|
-
| `{ ... }` | Products | Record construction: `{ f a, g b }`, `{}` |
|
|
180
|
-
| `< ... >` | Unions | Variant choice: `< f, g >`, `<>` |
|
|
181
|
-
| `( ... )` | Relations | Composition & identity: `(f g)`, `()` |
|
|
182
|
-
| `[ ... ]` | Patterns | Reusable templates: `list[X]`, `bit[]` |
|
|
183
|
-
|
|
184
|
-
**Composable partial functions**
|
|
185
|
-
|
|
186
|
-
Core expressions are deliberately small:
|
|
187
|
-
|
|
188
|
-
| Syntax | Meaning |
|
|
189
|
-
| --- | --- |
|
|
190
|
-
| `.field` | project a product field |
|
|
191
|
-
| `/tag` | project a tagged-union branch |
|
|
192
|
-
| `\|tag` | introduce a variant (tagged union) |
|
|
193
|
-
| `(f g)` | compose transformations |
|
|
194
|
-
| `<f, g>` | try `f`, then `g` if `f` is undefined |
|
|
195
|
-
| `{f a, g b}` | build a product from parallel transformations |
|
|
196
|
-
| `?filter` | schema filter / inductive termination witness |
|
|
197
|
-
| `name[...]` | instantiate named pattern template |
|
|
198
|
-
| `()` | identity |
|
|
199
|
-
| `<>` | always undefined |
|
|
200
|
-
| `{}` | constant empty product |
|
|
201
|
-
|
|
202
|
-
**Derived relational contracts**
|
|
49
|
+
It prints `"ok"`. The pipeline carries a binary schema followed by a payload;
|
|
50
|
+
the codec commands handle the text boundaries.
|
|
203
51
|
|
|
204
|
-
|
|
205
|
-
$(input, output)$ schema graph that specifies accepted input schemas,
|
|
206
|
-
guaranteed output schemas, and preserved data structures ($=X$). Those
|
|
207
|
-
contracts guide diagnostics, REPL output, binary serialization, and
|
|
208
|
-
optimizations.
|
|
52
|
+
To expose the commands on your path, run `npm link`. The equivalent pipeline is:
|
|
209
53
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
The command-line pipeline uses a self-describing binary stream:
|
|
213
|
-
|
|
214
|
-
```text
|
|
215
|
-
encoded schema, followed by payload encoded under that schema
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
The boundary tools are:
|
|
219
|
-
|
|
220
|
-
```bash
|
|
221
|
-
k-parse # textual k value -> binary schema+payload stream
|
|
222
|
-
k # apply a k expression or .k/.ko program to the stream
|
|
223
|
-
k-print # binary schema+payload stream -> textual value
|
|
224
|
-
k-show # inspect decoded schema and payload value
|
|
54
|
+
```sh
|
|
55
|
+
k-unit --parse | k '{} |ok' | k-print
|
|
225
56
|
```
|
|
226
57
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
file formats:
|
|
231
|
-
|
|
232
|
-
- `.k`: human-readable source code defining algebraic schemas and relations.
|
|
233
|
-
- `.klib`: ahead-of-time (AOT) library objects (`main: null`) storing
|
|
234
|
-
canonical relation definitions, schemas, and local aliases.
|
|
235
|
-
- `.ko`: ahead-of-time (AOT) executable containers (`KOBJ\n` magic header)
|
|
236
|
-
with entrypoint `main: "__main__"`, skipping parsing and contract
|
|
237
|
-
derivation at runtime.
|
|
238
|
-
- `.kvm`: polymorphic register-IR template artifacts (`layer: "KVM-P"`)
|
|
239
|
-
carrying principal schema contracts for instant runtime specialization
|
|
240
|
-
(`specializeKVM`).
|
|
241
|
-
|
|
242
|
-
In `k`, all relations, schemas, and pattern templates are identified
|
|
243
|
-
canonically by immutable content-addressed hashes (`@hash`). There is
|
|
244
|
-
no global symbol namespace; friendly names (such as `plus`, `list`, or
|
|
245
|
-
`5`) are local aliases stored in metadata. Similarly, there are no
|
|
246
|
-
primitive numbers: numerical identifiers like `5` or `10` are nullary
|
|
247
|
-
relations mapping `{}` to `bits`.
|
|
248
|
-
|
|
249
|
-
For a detailed guide to all four formats, commands, and workflows, see
|
|
250
|
-
[DOCS/FILE_FORMATS.md](DOCS/FILE_FORMATS.md).
|
|
251
|
-
|
|
252
|
-
## CLI Tour
|
|
253
|
-
|
|
254
|
-
**Running k**
|
|
58
|
+
Build the standalone browser REPL with `npm run build:repl-html`, then open
|
|
59
|
+
`repl.html`. See the [walkthrough](DOCS/GETTING_STARTED.md) for recursive
|
|
60
|
+
relations, libraries, and codecs.
|
|
255
61
|
|
|
256
|
-
|
|
257
|
-
| --- | --- |
|
|
258
|
-
| `k` | Execute a k expression, or an existing source/object file, over a binary stream |
|
|
259
|
-
| `k-repl` | Start the interactive interpreter |
|
|
260
|
-
|
|
261
|
-
**Serialization boundaries**
|
|
262
|
-
|
|
263
|
-
| Command | Purpose |
|
|
264
|
-
| --- | --- |
|
|
265
|
-
| `k-parse` | Convert textual k values to binary schema+payload streams |
|
|
266
|
-
| `k-print` | Convert binary schema+payload streams back to textual values |
|
|
267
|
-
| `k-show` | Pass a stream through while showing the decoded payload and schema |
|
|
268
|
-
|
|
269
|
-
**Built-in codecs**
|
|
270
|
-
|
|
271
|
-
| Command | Purpose |
|
|
272
|
-
| --- | --- |
|
|
273
|
-
| `k-json` | Convert JSON to/from binary stream (specializing) |
|
|
274
|
-
| `k-djson` | Convert JSON to/from dynamic JSON (binary bit-trie) |
|
|
275
|
-
| `k-int` | Convert decimal integers to/from binary stream |
|
|
276
|
-
| `k-ieee` | Convert float literals to/from binary stream |
|
|
277
|
-
| `k-unit` | Produce or validate the unit value |
|
|
278
|
-
| `k-utf8` / `k-utf16` | Convert text to/from k string streams |
|
|
279
|
-
|
|
280
|
-
**Object and library tooling**
|
|
62
|
+
## Libraries and examples
|
|
281
63
|
|
|
282
|
-
|
|
|
64
|
+
| File | Purpose |
|
|
283
65
|
| --- | --- |
|
|
284
|
-
|
|
|
285
|
-
|
|
|
286
|
-
|
|
|
287
|
-
|
|
|
288
|
-
|
|
|
289
|
-
|
|
|
290
|
-
|
|
|
291
|
-
|
|
|
292
|
-
| `k-llvm` / `k-llvm-build` / `k-llvm-compile` | Compile and run via LLVM backend |
|
|
293
|
-
|
|
294
|
-
Installed binary names are `k-` plus the source basename without
|
|
295
|
-
`.mjs`, except for `k.mjs` itself. Source names that already include
|
|
296
|
-
`k-`, such as `codecs/k-parse.mjs`, keep that name.
|
|
297
|
-
|
|
298
|
-
## Backends
|
|
299
|
-
|
|
300
|
-
Backends live under [`backends/`](backends/) as npm workspaces:
|
|
301
|
-
|
|
302
|
-
- [`backends/wasm`](backends/wasm/) lowers typed k programs through kVM
|
|
303
|
-
into WebAssembly artifacts. Powers both the CLI REPL (`wasm-in-process`)
|
|
304
|
-
and the standalone Web REPL.
|
|
305
|
-
- [`backends/llvm`](backends/llvm/) lowers polymorphic kVM programs into
|
|
306
|
-
LLVM IR and compiled native test executables.
|
|
66
|
+
| [core.k](core.k) | Bit, Unicode string, and self-hosted pattern schemas |
|
|
67
|
+
| [Examples/nat.k](Examples/nat.k) | Small Peano arithmetic example |
|
|
68
|
+
| [Examples/list.k](Examples/list.k) | Small generic list example |
|
|
69
|
+
| [Examples/arithmetics.k](Examples/arithmetics.k) | Arbitrary-precision integer and rational arithmetic |
|
|
70
|
+
| [Examples/poly.k](Examples/poly.k) | Polymorphic list operations; load arithmetics.k first |
|
|
71
|
+
| [Examples/djson.k](Examples/djson.k) | Dynamic JSON operations over binary tries |
|
|
72
|
+
| [Examples/ieee.k](Examples/ieee.k) | Binary64 floating-point operations returning `{result, flags}` |
|
|
73
|
+
| [Examples/karatsuba-mult.k](Examples/karatsuba-mult.k) | Alternative integer multiplication algorithm |
|
|
307
74
|
|
|
308
|
-
|
|
309
|
-
[
|
|
75
|
+
Load libraries explicitly with `:load` in the REPL. The CLI uses `--lib` with
|
|
76
|
+
explicit `--export` aliases; see [file formats and tools](DOCS/FILE_FORMATS.md).
|
|
310
77
|
|
|
311
|
-
##
|
|
78
|
+
## JavaScript API
|
|
312
79
|
|
|
313
80
|
```js
|
|
314
81
|
import k from "@fraczak/k";
|
|
315
82
|
|
|
316
83
|
const fn = k.compile("{} |ok");
|
|
317
|
-
|
|
318
|
-
console.log(
|
|
319
|
-
```
|
|
320
|
-
|
|
321
|
-
For deeper inspection:
|
|
322
|
-
|
|
323
|
-
```js
|
|
324
|
-
const annotated = k.annotate(source, {
|
|
325
|
-
convergence: { strategy: "auto" }
|
|
326
|
-
});
|
|
327
|
-
|
|
328
|
-
console.log(annotated.compileStats);
|
|
84
|
+
const result = fn(k.fromObject({}));
|
|
85
|
+
console.log(result.tag); // "ok"
|
|
329
86
|
```
|
|
330
87
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
k is usable as an experimental language and toolkit, not a stable
|
|
334
|
-
production platform yet.
|
|
335
|
-
|
|
336
|
-
Working today:
|
|
337
|
-
|
|
338
|
-
- parser and runtime for the core language
|
|
339
|
-
- type derivation over recursive algebraic data shapes
|
|
340
|
-
- REPL with aliases, loading, completion, `.klib` export, and `.ko` export
|
|
341
|
-
- binary pattern+value codec
|
|
342
|
-
- object and library files
|
|
343
|
-
- Node.js API
|
|
344
|
-
- regression tests for runtime, codecs, objects, hashes, and type
|
|
345
|
-
derivation
|
|
346
|
-
|
|
347
|
-
Still evolving:
|
|
348
|
-
|
|
349
|
-
- surface syntax and diagnostics
|
|
350
|
-
- standard libraries
|
|
351
|
-
- documentation and tutorials
|
|
352
|
-
- object metadata format
|
|
353
|
-
- optimization and backend experiments
|
|
354
|
-
- larger real-world examples
|
|
355
|
-
|
|
356
|
-
## Good Areas for Contributors
|
|
357
|
-
|
|
358
|
-
k is small enough to study, but there are several useful directions:
|
|
359
|
-
|
|
360
|
-
- examples: schema transformations, codecs, protocol examples, teaching
|
|
361
|
-
tasks
|
|
362
|
-
- documentation: tutorials, diagrams, and clearer language walkthroughs
|
|
363
|
-
- tooling: formatter, editor integration, better diagnostics, REPL
|
|
364
|
-
ergonomics
|
|
365
|
-
- compiler work: optimization, object inspection, backend experiments
|
|
366
|
-
- theory: normalization, equivalence, convergence, and pattern derivation
|
|
367
|
-
- applications: hardware modeling, asynchronous/synchronous pipelines,
|
|
368
|
-
schema repositories, and data migration tooling
|
|
369
|
-
|
|
370
|
-
If you are interested in languages, compilers, data modeling, formal
|
|
371
|
-
methods, serialization, or teaching tools, there is room to shape the
|
|
372
|
-
project.
|
|
373
|
-
|
|
374
|
-
## Examples
|
|
375
|
-
|
|
376
|
-
The [`Examples/`](Examples/) directory contains language demonstrations
|
|
377
|
-
and standard libraries:
|
|
378
|
-
|
|
379
|
-
| File | Contents |
|
|
380
|
-
| --- | --- |
|
|
381
|
-
| `core.k` | Standard library: booleans, units, strings, optionals, and fundamental helpers |
|
|
382
|
-
| `djson.k` | Dynamic JSON with balanced binary bit-trie indexing |
|
|
383
|
-
| `arithmetics.k` | Integer and rational arithmetic built from bit-level relations from scratch |
|
|
384
|
-
| `ieee.k` | IEEE 754 binary64 floating-point arithmetic (`add`, `sub`, `mul`, `div`) |
|
|
385
|
-
| `poly.k` | Polymorphic list operations (`concat`, `reverse`, `length`, `get_nth`, `split_by`, `zip`) |
|
|
386
|
-
| `nat.k` | Peano natural numbers |
|
|
387
|
-
| `byte.k` | Byte type |
|
|
388
|
-
| `bnat.k` | Binary natural numbers |
|
|
389
|
-
| `buda.k` | Compact relational data transformations |
|
|
390
|
-
|
|
391
|
-
`list.k` is also useful as a focused demonstration of filters and
|
|
392
|
-
patterns.
|
|
393
|
-
|
|
394
|
-
`ieee.k` is a complete IEEE-754 binary64 model built from bit-level
|
|
395
|
-
types upward with hierarchical significand adders, including comparison
|
|
396
|
-
and floating-point `add`, `sub`, `mul`, and `div` relations. Those public
|
|
397
|
-
aliases return `{ result, flags }`; compose with `.result` when only the
|
|
398
|
-
floating-point value is needed.
|
|
88
|
+
Compiled functions return a `Value` or `undefined`. A `Value` carries its
|
|
89
|
+
structural pattern as well as its payload.
|
|
399
90
|
|
|
400
91
|
## Development
|
|
401
92
|
|
|
402
|
-
```
|
|
403
|
-
npm
|
|
404
|
-
npm
|
|
405
|
-
npm
|
|
406
|
-
npm run test:llvm # run the LLVM backend tests
|
|
407
|
-
npm run build:repl-html # build the standalone single-file Web REPL (repl.html)
|
|
408
|
-
npm run compare # run multi-backend benchmark and conformance harness
|
|
409
|
-
npm run perf:poly # run polymorphic benchmark across backends
|
|
410
|
-
npm run perf:poly:trace # run execution phase tracing & call profiling
|
|
411
|
-
npm run perf:int # run integer arithmetic benchmark across backends
|
|
412
|
-
npm run perf:ieee # run IEEE-754 arithmetic benchmark across backends
|
|
93
|
+
```sh
|
|
94
|
+
npm test
|
|
95
|
+
npm run test:backends
|
|
96
|
+
npm --prefix k-vscode test
|
|
413
97
|
```
|
|
414
98
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
99
|
+
See [tests/README.md](tests/README.md) for suite boundaries and optional tools.
|
|
100
|
+
Grammar changes require `npm run prepare`; generated parsers are not edited
|
|
101
|
+
by hand.
|
|
102
|
+
|
|
103
|
+
The [compiler guide](DOCS/ARCHITECTURE.md) maps the implementation and execution
|
|
104
|
+
layers. [WebAssembly](backends/wasm/README.md),
|
|
105
|
+
[LLVM](backends/llvm/README.md), and the [VS Code extension](k-vscode/README.md)
|
|
106
|
+
have their own usage instructions.
|
|
418
107
|
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
- hash/fingerprint stability
|
|
422
|
-
- object file round-trips
|
|
423
|
-
- REPL scripted interaction
|
|
424
|
-
- Web REPL headless browser verification
|
|
425
|
-
- polymorphic kVM and specialization
|
|
426
|
-
- shell integration tests
|
|
108
|
+
k is experimental: syntax, diagnostics, libraries, and artifact formats may
|
|
109
|
+
change. Release history is in [CHANGELOG.md](CHANGELOG.md).
|
|
427
110
|
|
|
428
|
-
##
|
|
111
|
+
## Release checks
|
|
429
112
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
- [DOCS/DICTIONARY.md](DOCS/DICTIONARY.md) - concept names and terminology
|
|
435
|
-
- [DOCS/CANONICAL_PATTERNS.md](DOCS/CANONICAL_PATTERNS.md) -
|
|
436
|
-
content-addressable canonical patterns and parameter aliasing
|
|
437
|
-
- [DOCS/NAMED_PATTERNS.md](DOCS/NAMED_PATTERNS.md) -
|
|
438
|
-
parameterized pattern templates and type schemes
|
|
439
|
-
- [DOCS/REPL.md](DOCS/REPL.md) - interactive interpreter and codec details
|
|
440
|
-
- [DOCS/WEB_REPL_REQUIREMENTS.md](DOCS/WEB_REPL_REQUIREMENTS.md) -
|
|
441
|
-
Web REPL architecture and requirements
|
|
442
|
-
- [DOCS/KVM_EXECUTION_MODEL.md](DOCS/KVM_EXECUTION_MODEL.md) -
|
|
443
|
-
kVM execution model and bytecode design
|
|
444
|
-
- [DOCS/TEXTUAL_VALUES.md](DOCS/TEXTUAL_VALUES.md) -
|
|
445
|
-
textual boundary notation
|
|
446
|
-
- [DOCS/PATTERNS.md](DOCS/PATTERNS.md) - pattern representation
|
|
447
|
-
- [DOCS/OBJECT_FILE_AND_PATTERN.md](DOCS/OBJECT_FILE_AND_PATTERN.md) -
|
|
448
|
-
object format and pattern encoding
|
|
449
|
-
- [DOCS/PROFILING_AND_TRACING.md](DOCS/PROFILING_AND_TRACING.md) -
|
|
450
|
-
execution phase tracing and function call profiling across backends
|
|
451
|
-
- [DOCS/CODECS.md](DOCS/CODECS.md) - writing external codecs
|
|
452
|
-
- [codecs/README.md](codecs/README.md) - binary codec internals
|
|
453
|
-
- [objects/README.md](objects/README.md) - object/library tools
|
|
454
|
-
- [DOCS/book.md](DOCS/book.md) - longer language reference
|
|
113
|
+
GitHub CI runs the main, backend, and editor suites, then installs the packed
|
|
114
|
+
archive with production dependencies and checks its API and commands on Node.js
|
|
115
|
+
22 and 18. Run `npm run test:package -- /path/to/installed/@fraczak/k` to repeat
|
|
116
|
+
the package check. `npm publish` runs the main suite before publishing.
|