@fraczak/k 8.4.1 → 8.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/DOCS/ARCHITECTURE.md +83 -0
- package/DOCS/CODECS.md +93 -204
- package/DOCS/DICTIONARY.md +42 -1241
- package/DOCS/FILE_FORMATS.md +170 -625
- package/DOCS/GETTING_STARTED.md +164 -0
- package/DOCS/KIR_V1.md +22 -33
- package/DOCS/KVM_EXECUTION_MODEL.md +88 -481
- package/DOCS/PATTERNS.md +155 -138
- package/DOCS/POLYMORPHIC_IR.md +83 -101
- package/DOCS/PROFILING_AND_TRACING.md +52 -166
- package/DOCS/README.md +35 -0
- package/DOCS/REPL.md +100 -377
- package/DOCS/TEXTUAL_VALUES.md +53 -81
- package/DOCS/TYPE_DERIVATION.md +95 -234
- package/DOCS/book-combined.md +3786 -0
- package/Examples/arithmetics.k +45 -59
- package/Examples/djson.k +102 -112
- package/Examples/karatsuba-mult.k +2 -3
- package/Examples/list.k +6 -7
- package/Examples/nat.k +6 -6
- package/Examples/poly.k +45 -45
- package/LICENSE +15 -0
- package/README.md +72 -410
- package/backends/llvm/README.md +75 -97
- package/backends/wasm/README.md +88 -66
- package/canonical-pattern.mjs +7 -3
- package/codecs/README.md +23 -261
- package/codecs/WIRE_FORMAT.md +132 -0
- package/core.k +5 -15
- package/named-patterns.mjs +19 -7
- package/objects/README.md +24 -109
- package/package.json +8 -3
- package/paper/Makefile +17 -0
- package/paper/k-paper.pdf +0 -0
- package/paper/k-paper.tex +435 -0
- package/parser.jison +0 -10
- package/parser.mjs +32 -43
- package/repl.mjs +74 -29
- package/scripts/benchmark-idempotency.mjs +3 -3
- package/scripts/run-tests.mjs +1 -1
- package/scripts/test-package.mjs +64 -0
- package/tests/README.md +66 -46
- package/tests/code-derivation/core.mjs +61 -0
- package/tests/helpers/cli.mjs +45 -0
- package/tests/integration.sh +2 -3
- package/tests/test-arithmetics.mjs +47 -237
- package/tests/test-djson-methods.mjs +71 -275
- package/tests/test-hash-normalization.mjs +22 -1
- package/tests/test-karatsuba.mjs +18 -126
- package/tests/test-named-patterns.mjs +174 -621
- package/tests/test-paper-examples.mjs +90 -0
- package/tests/test-poly.mjs +2 -2
- package/tests/test-repl-html.mjs +2 -2
- package/tests/test-repl.mjs +49 -41
- package/tests/test.mjs +27 -68
- package/AGENTS.md +0 -35
- package/CLAUDE.md +0 -64
- package/DOCS/BIG_PICTURE.md +0 -439
- package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +0 -74
- package/DOCS/CANONICAL_PATTERNS.md +0 -362
- package/DOCS/CODEC_ARCHITECTURE.md +0 -257
- package/DOCS/CONVERGENCE.md +0 -123
- package/DOCS/DIVERGENCE_DETECTION.md +0 -23
- package/DOCS/JSON_APPROACHES_COMPARISON.md +0 -302
- package/DOCS/KIR_SPECIALIZATION_DESIGN.md +0 -472
- package/DOCS/Makefile +0 -99
- package/DOCS/NAMED_PATTERNS.md +0 -361
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +0 -343
- package/DOCS/PATTERNS_AND_TYPED_VALUES.md +0 -665
- package/DOCS/WEB_REPL_REQUIREMENTS.md +0 -134
- package/DOCS/book/01-introduction.md +0 -48
- package/DOCS/book/02-syntax-and-values.md +0 -110
- package/DOCS/book/03-types-as-finite-automata.md +0 -155
- package/DOCS/book/04-partial-functions-and-composition.md +0 -145
- package/DOCS/book/05-typing-filters-and-normalization.md +0 -109
- package/DOCS/book/06-values-in-memory.md +0 -1085
- package/DOCS/book/07-the-partial-function-abi.md +0 -143
- package/DOCS/book/08-operational-semantics-and-execution.md +0 -196
- package/DOCS/book/09-compiler-architecture.md +0 -160
- package/DOCS/book/10-from-ast-to-intermediate-representation.md +0 -166
- package/DOCS/book/14-canonical-serialization.md +0 -176
- package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +0 -156
- package/DOCS/book/overview.txt +0 -68
- package/DOCS/book.md +0 -50
- package/Examples/bnat.k +0 -308
- package/Examples/buda.k +0 -66
- package/Examples/byte.k +0 -35
- package/Examples/code.k +0 -43
- package/Examples/vhdl_to_k.k +0 -67
- package/JOURNAL.md +0 -288
- package/TODO.md +0 -107
- package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +0 -233
- package/backends/wasm/TECHNICAL-DEBT.md +0 -58
- package/backends/wasm/demo_ieee.mjs +0 -318
- package/browser/vfs-data.mjs +0 -21
- package/codecs/BINARY_FORMAT.md +0 -53
- package/codecs/POLYMORPHIC_BINARY_FORMAT.md +0 -303
- package/codecs/pandoc.css +0 -71
- package/tests/code-derivation/blowup.mjs +0 -25
- package/tests/code-derivation/bnat.mjs +0 -11
- package/tests/code-derivation/dot.mjs +0 -37
- package/tests/code-derivation/index.mjs +0 -20
- package/tests/code-derivation/loop.mjs +0 -17
- package/tests/code-derivation/nat.mjs +0 -10
- package/tests/code-derivation/not-not.mjs +0 -14
- package/tests/code-derivation/product.mjs +0 -22
- package/tests/code-derivation/rlz.mjs +0 -13
- package/tests/test-fingerprint.mjs +0 -38
package/DOCS/BIG_PICTURE.md
DELETED
|
@@ -1,439 +0,0 @@
|
|
|
1
|
-
# Big Picture
|
|
2
|
-
|
|
3
|
-
`k` is a minimal, unified calculus of **content-addressable schemas** and
|
|
4
|
-
**relational data transformations**.
|
|
5
|
-
|
|
6
|
-
## Basics
|
|
7
|
-
|
|
8
|
-
The language describes first-order partial relations on labeled tree values.
|
|
9
|
-
Values are rooted, edge-labeled trees, intuitively similar to an XML or a JSON
|
|
10
|
-
document. Every value is self-describing at boundaries as a pair:
|
|
11
|
-
`(schema, payload)`.
|
|
12
|
-
|
|
13
|
-
### Schemas
|
|
14
|
-
|
|
15
|
-
Formally, a _schema_ is a finite tree automaton. The schema defines a set of
|
|
16
|
-
values, which corresponds to the language of trees accepted by the
|
|
17
|
-
automaton. Therefore, every schema admits a unique canonical normal form in
|
|
18
|
-
terms of its minimal deterministic tree automaton.
|
|
19
|
-
|
|
20
|
-
Schemas are built from two structural constructors:
|
|
21
|
-
|
|
22
|
-
1. **Product** (`{ ... }`)
|
|
23
|
-
2. **Union** (`< ... >`) (tagged or disjoint union of variants)
|
|
24
|
-
|
|
25
|
-
> Notation: Native k notation uses products `{ S l, ... }` and unions
|
|
26
|
-
> `< S t, ... >`. The empty product `{}` has a single value (the _unit_).
|
|
27
|
-
> The empty union `<>` has no values.
|
|
28
|
-
|
|
29
|
-
```bnf
|
|
30
|
-
schema ::= name | '{' schema_label_list '}' | '<' schema_label_list '>'
|
|
31
|
-
schema_label_list ::= /* empty */ | ( schema label ',' )* schema label
|
|
32
|
-
name ::= IDENTIFIER
|
|
33
|
-
label ::= STRING
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
Given a finite (possibly empty) set of schemas `s1, s2, ..., sn` and a
|
|
37
|
-
set of pairwise distinct labels `l1, l2, ..., ln`:
|
|
38
|
-
|
|
39
|
-
- `{ s1 l1, s2 l2, ..., sn ln }` defines a _product schema_ with
|
|
40
|
-
`n` projection relations: `.l1`, `.l2`, `...`, `.ln`.
|
|
41
|
-
Each projection maps a value matching the product schema into a value
|
|
42
|
-
matching `s1`, `s2`, `...`, `sn`, respectively.
|
|
43
|
-
- `< s1 l1, s2 l2, ..., sn ln >` defines a _union schema_ with
|
|
44
|
-
`n` projection relations `/l1`, `/l2`, `...`, `/ln`,
|
|
45
|
-
mapping a variant into its payload matching `s1`, `s2`, `...`, `sn`.
|
|
46
|
-
|
|
47
|
-
#### Variant (Union) Values
|
|
48
|
-
|
|
49
|
-
- Variant values are constructed using "|" (pipe) followed by the tag:
|
|
50
|
-
- Unit variant: `{} | nil`, `{} | zero`
|
|
51
|
-
- Variant with payload `v` at tag: `v | tag`
|
|
52
|
-
- Example (list): with `list? = ?< {} nil, { X car, L cdr } cons > = L;`:
|
|
53
|
-
- Empty list `[]`: `{} | nil`
|
|
54
|
-
- Singleton `[v]`: `{ v car, {} | nil cdr } | cons`
|
|
55
|
-
- Angle brackets `< ... >` denote ordered choice (union expressions); use
|
|
56
|
-
`| tag` for variant construction.
|
|
57
|
-
|
|
58
|
-
#### Equivalence of Schemas (Bisimilarity)
|
|
59
|
-
|
|
60
|
-
Two (possibly recursive) schemas are equivalent iff they are bisimilar
|
|
61
|
-
over their definition graphs. Concretely, there exists a relation B
|
|
62
|
-
such that `(s1, s2) ∈ B` if and only if:
|
|
63
|
-
- `s1` and `s2` have exactly the same set of labels/tags;
|
|
64
|
-
- `s1` and `s2` are simultaneously products or simultaneously unions;
|
|
65
|
-
- for each label/tag `ℓ`, the subschemas under `ℓ` are again related by B.
|
|
66
|
-
|
|
67
|
-
Canonicalization erases local variable names and deterministically orders
|
|
68
|
-
states, so any two structurally equivalent schemas yield identical canonical text.
|
|
69
|
-
|
|
70
|
-
### Filters
|
|
71
|
-
|
|
72
|
-
A _filter_ is a syntactic expression `?schema` that acts as a partial
|
|
73
|
-
identity relation on values conforming to `schema`. If the input value
|
|
74
|
-
matches the schema, it passes through unchanged; otherwise, the
|
|
75
|
-
relation is undefined.
|
|
76
|
-
|
|
77
|
-
In recursive relations, filters also serve as **inductive termination
|
|
78
|
-
witnesses**, ensuring type and contract derivation converges.
|
|
79
|
-
|
|
80
|
-
In general, a filter schema is defined by:
|
|
81
|
-
|
|
82
|
-
```bnf
|
|
83
|
-
filter ::= name | '{' filter_label_list '}'
|
|
84
|
-
| '<' filter_label_list '>' | '(' filter_label_list ')'
|
|
85
|
-
| name '[' filter_args ']'
|
|
86
|
-
| filter '=' name
|
|
87
|
-
filter_label_list ::= /* empty */ | '...'
|
|
88
|
-
| ( filter label ',' ) * filter label
|
|
89
|
-
filter_args ::= /* empty */ | ( filter ',' ) * filter
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
Examples:
|
|
93
|
-
|
|
94
|
-
```text
|
|
95
|
-
X -- unconstrained schema variable
|
|
96
|
-
( ... ) -- unconstrained schema
|
|
97
|
-
{ ... } -- any product schema
|
|
98
|
-
{ X x, X y } -- product with two fields of the same schema X
|
|
99
|
-
< X x, ... > = X -- recursive variant X with variant x of schema X
|
|
100
|
-
list[int[]] -- instantiated named pattern
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
### Named Patterns and Parameterized Schemas
|
|
104
|
-
|
|
105
|
-
k supports reusable, parameterized regular tree grammar templates via
|
|
106
|
-
square-bracket syntax `name[...]`:
|
|
107
|
-
|
|
108
|
-
```k
|
|
109
|
-
name[ P1, P2, ... Pn ] = filter;
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
For 0-arity templates, empty brackets `[]` are used:
|
|
113
|
-
|
|
114
|
-
```k
|
|
115
|
-
bit[] = < {} 0, {} 1 >;
|
|
116
|
-
bits[] = < {} _, B 0, B 1 > = B;
|
|
117
|
-
byte[] = {
|
|
118
|
-
bit[] 0, bit[] 1, bit[] 2, bit[] 3,
|
|
119
|
-
bit[] 4, bit[] 5, bit[] 6, bit[] 7
|
|
120
|
-
};
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
Parameterized templates introduce external ports:
|
|
124
|
-
|
|
125
|
-
```k
|
|
126
|
-
list[X] = < {} nil, { X car, Y cdr } cons > = Y;
|
|
127
|
-
pair[A, B] = { A first, B second };
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
Callers instantiate templates using `? name[...]`:
|
|
131
|
-
|
|
132
|
-
```k
|
|
133
|
-
list? = ? list[bits[]];
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
Or directly inside relation parameter guards:
|
|
137
|
-
|
|
138
|
-
```k
|
|
139
|
-
add = ?{ nat[] = N x, ... } < ... >;
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
Template expansion is purely structural and static: internal knots
|
|
143
|
-
(`= Y`) are scoped locally and alpha-renamed, and template calls form an
|
|
144
|
-
acyclic dependency DAG, preventing non-regular divergence.
|
|
145
|
-
|
|
146
|
-
### Partial functions
|
|
147
|
-
|
|
148
|
-
A _partial function_ is a function that is not defined for every value
|
|
149
|
-
of its domain.
|
|
150
|
-
|
|
151
|
-
There are three ways of combining partial functions:
|
|
152
|
-
|
|
153
|
-
1. __Composition__: we write a composition of `f` and `g` as `(f g)`,
|
|
154
|
-
meaning that if `f` is defined on value `x` producing `y`, and `g`
|
|
155
|
-
is defined on value `y` producing `z`, then `(f g)` is defined on
|
|
156
|
-
value `x` producing `z`.
|
|
157
|
-
2. __Union__: we write `< f1, f2 >` for a union of two functions `f1`
|
|
158
|
-
and `f2`, meaning that if `f1` is defined on value `x` producing
|
|
159
|
-
`y`, then `< f1, f2 >` is defined on value `x` producing `y`.
|
|
160
|
-
Otherwise, i.e., when `f1` is not defined for `x`, `< f1, f2 >`
|
|
161
|
-
acts as `f2`.
|
|
162
|
-
3. __Product__: we write `{f1 lab1, f2 lab2 }` for a product of two
|
|
163
|
-
functions `f1` and `f2`, meaning that if `f1` and `f2` are both
|
|
164
|
-
defined on value `x` producing `y1` and `y2`, respectively, then
|
|
165
|
-
`{f1 lab1, f2 lab2 }` is defined on value `x` producing
|
|
166
|
-
`{y1 lab1, y2 lab2}`. Otherwise, `{f1 lab1, f2 lab2 }` is not
|
|
167
|
-
defined for `x`.
|
|
168
|
-
|
|
169
|
-
Actually, composition, union, and product combine any number of
|
|
170
|
-
partial functions, not only two. For example, empty composition,
|
|
171
|
-
`()`, defines the identity function.
|
|
172
|
-
|
|
173
|
-
### Program
|
|
174
|
-
|
|
175
|
-
A `kernel-core` program is a set of definitions of relations (partial
|
|
176
|
-
functions), followed by an _expression_, which is the relation the
|
|
177
|
-
program evaluates.
|
|
178
|
-
|
|
179
|
-
The language syntax is defined by:
|
|
180
|
-
|
|
181
|
-
```bnf
|
|
182
|
-
program ::= function_definition* expression
|
|
183
|
-
function_definition ::= name '=' expression ';'
|
|
184
|
-
expression ::= name | '?' filter
|
|
185
|
-
| projection | constructor | composition | union | product
|
|
186
|
-
projection ::= '.' label | '/' label
|
|
187
|
-
constructor ::= '|' label
|
|
188
|
-
composition ::= '(' expression * ')'
|
|
189
|
-
union ::= '<' (expression ',') * expression '>'
|
|
190
|
-
product ::= '{' expr_label_list '}'
|
|
191
|
-
expr_label_list ::= /* empty */
|
|
192
|
-
| (expression label_name ',')* expression label
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
We assume that in a `kernel-core` program the names (for relations
|
|
196
|
-
and labels) are identifiers (strings) such that:
|
|
197
|
-
|
|
198
|
-
1. All defined relation names are distinct.
|
|
199
|
-
2. All label names within a `product` expression are locally distinct.
|
|
200
|
-
|
|
201
|
-
Filters act as partial identity relations defined only for the values
|
|
202
|
-
matching the corresponding schema, while also serving as inductive witnesses
|
|
203
|
-
for recursive relation termination.
|
|
204
|
-
|
|
205
|
-
That's it.
|
|
206
|
-
|
|
207
|
-
No `builtin` types, no `if` statement, no `loop`, no `throw`, no
|
|
208
|
-
_closure_, no _annotations_, and no macros. Just _schemas_, _filters_,
|
|
209
|
-
and _relations_.
|
|
210
|
-
|
|
211
|
-
### Normalization and Content-Addressing
|
|
212
|
-
|
|
213
|
-
All schemas are normalized to the canonical text representation of their
|
|
214
|
-
minimal deterministic tree automaton. That canonical representation is their
|
|
215
|
-
true identity; `@hash` is an immutable, fixed-width index digest.
|
|
216
|
-
|
|
217
|
-
Every relation is identified canonically by the content-addressed hash
|
|
218
|
-
of its normalized definition AST.
|
|
219
|
-
|
|
220
|
-
There is no global symbol namespace. Human-readable names (such as
|
|
221
|
-
`plus`, `int`, or `5`) are local aliases recorded in object metadata
|
|
222
|
-
(`relAlias`). When libraries are loaded with `--lib`, definitions are
|
|
223
|
-
identified by their canonical `@hash`, but local aliases are not
|
|
224
|
-
implicitly exported into the importing scope unless explicitly mapped
|
|
225
|
-
via `--export`.
|
|
226
|
-
|
|
227
|
-
This approach completely solves the problem of modules, name collisions,
|
|
228
|
-
and unversioned imports, establishing a universal registry of
|
|
229
|
-
schemas and relations.
|
|
230
|
-
|
|
231
|
-
## Schemas, Contracts, and Derivation
|
|
232
|
-
|
|
233
|
-
The abstract syntax tree of a program consists of a dictionary of
|
|
234
|
-
relation definitions and an optional entry expression.
|
|
235
|
-
|
|
236
|
-
The compiler computes canonical representations for schemas and derives
|
|
237
|
-
a **contract** for every relation: a connected $(input, output)$ schema
|
|
238
|
-
graph where shared vertices ($=X$) prove that input substructures are
|
|
239
|
-
preserved through to the output.
|
|
240
|
-
|
|
241
|
-
The contract derivation steps are:
|
|
242
|
-
|
|
243
|
-
1. Initialize the schema graph from primitive operations (projections, constructors, products) and explicit filter annotations.
|
|
244
|
-
2. Propagate schemas forward and backward through compositions, products, and choices until reaching a fixed point.
|
|
245
|
-
3. Validate inductive convergence for recursive relations using filter witnesses.
|
|
246
|
-
|
|
247
|
-
### Examples
|
|
248
|
-
|
|
249
|
-
#### Example 1
|
|
250
|
-
|
|
251
|
-
```text
|
|
252
|
-
bit? = ?< {} o, {} i >;
|
|
253
|
-
bit0 = | o;
|
|
254
|
-
bit1 = | i;
|
|
255
|
-
|
|
256
|
-
byte? = ?{ bit? b0, bit? b1, bit? b2, bit? b3 };
|
|
257
|
-
zero = bit0 { () b0, () b1, () b2, () b3 };
|
|
258
|
-
|
|
259
|
-
inc = byte?
|
|
260
|
-
<
|
|
261
|
-
{ bit0 overflown, { .b0 /o bit1 b0, .b1 b1, .b2 b2, .b3 b3 } byte },
|
|
262
|
-
{ bit0 overflown, { bit0 b0, .b1 /o bit1 b1, .b2 b2, .b3 b3 } byte },
|
|
263
|
-
{ bit0 overflown, { bit0 b0, bit0 b1, .b2 /o bit1 b2, .b3 b3 } byte },
|
|
264
|
-
{ bit0 overflown, { bit0 b0, bit0 b1, bit0 b2, .b3 /o bit1 b3 } byte },
|
|
265
|
-
{ bit1 overflown, zero byte }
|
|
266
|
-
>
|
|
267
|
-
;
|
|
268
|
-
|
|
269
|
-
-- inc with overflown flag
|
|
270
|
-
inc_o =
|
|
271
|
-
{ .byte inc inc, .overflown overflown }
|
|
272
|
-
{
|
|
273
|
-
.inc.byte byte,
|
|
274
|
-
<.overflown.i bit1, .inc.overflown > overflown
|
|
275
|
-
}
|
|
276
|
-
;
|
|
277
|
-
|
|
278
|
-
inc3 = inc inc_o inc_o;
|
|
279
|
-
|
|
280
|
-
inc3
|
|
281
|
-
```
|
|
282
|
-
|
|
283
|
-
In the above program, we define relation `bit` as a union of two unit variants,
|
|
284
|
-
and relation `byte` as a product of four `bit`s.
|
|
285
|
-
|
|
286
|
-
Relations `bit0`, `bit1`, and `zero` are "constant polymorphic
|
|
287
|
-
relations", meaning:
|
|
288
|
-
|
|
289
|
-
- constant: if defined, they always return exactly the same value;
|
|
290
|
-
- polymorphic: they are defined for more than one pair of input and
|
|
291
|
-
output schemas: relations `bit0` and `bit1` have contract `?X -> bit`,
|
|
292
|
-
and `zero` has contract `?X -> byte`, where `?X` denotes an
|
|
293
|
-
unconstrained open schema, also denoted as `?(...)`.
|
|
294
|
-
|
|
295
|
-
Relations `inc` and `inc3` are relations with contracts
|
|
296
|
-
`byte -> { bit overflown, byte byte }` and
|
|
297
|
-
`{ bit overflown, byte byte } -> { bit overflown, byte byte }`,
|
|
298
|
-
respectively.
|
|
299
|
-
|
|
300
|
-
Relation `inc_o` is a polymorphic relation with contract:
|
|
301
|
-
`?X -> { byte byte, bit overflown }` with the following input schema constraints:
|
|
302
|
-
|
|
303
|
-
- `?X` is a product with at least two fields: `byte` and
|
|
304
|
-
`overflown`, denoted by:
|
|
305
|
-
> `?{ byte byte, Z overflown, ...}`;
|
|
306
|
-
- `?Z` is a union with field `i` denoted by:
|
|
307
|
-
> `?< V i, ...>`;
|
|
308
|
-
- `V` is unconstrained, denoted by `(...)`;
|
|
309
|
-
|
|
310
|
-
We can write it as: `?{ byte byte, <(...) i, ...> overflown, ...}`.
|
|
311
|
-
|
|
312
|
-
The target schema `{ byte byte, bit overflown }` corresponds to filter
|
|
313
|
-
`?{ byte byte, bit overflown }`. Such a schema is called a _closed
|
|
314
|
-
schema_, as all constructors are fully determined.
|
|
315
|
-
|
|
316
|
-
#### Example 2
|
|
317
|
-
|
|
318
|
-
Polymorphic list relations:
|
|
319
|
-
|
|
320
|
-
```text
|
|
321
|
-
list? = ?< {} nil, {X car, Y cdr} cons > = Y;
|
|
322
|
-
nil = {} |nil list?;
|
|
323
|
-
singleton = {() car, nil cdr} |cons list?;
|
|
324
|
-
cons = {.car car, .cdr cdr} | cons list?;
|
|
325
|
-
nil? = list? /nil nil;
|
|
326
|
-
car = list? /cons .car;
|
|
327
|
-
cdr = list? /cons .cdr;
|
|
328
|
-
```
|
|
329
|
-
|
|
330
|
-
For example, the definition for `car` is:
|
|
331
|
-
|
|
332
|
-
```text
|
|
333
|
-
> :rel car
|
|
334
|
-
car = list? /cons .car; -- @...
|
|
335
|
-
```
|
|
336
|
-
|
|
337
|
-
## Universal Schema Registry
|
|
338
|
-
|
|
339
|
-
Since the normalization process for schemas is fast and deterministic,
|
|
340
|
-
we can build a universal schema registry that will store all discovered
|
|
341
|
-
schemas. The registry is a key-value store, where the primary identity is
|
|
342
|
-
the canonical textual definition (or fixed-width hash) of the normalized
|
|
343
|
-
schema, and the value is the normalized schema automaton itself.
|
|
344
|
-
|
|
345
|
-
All relations (monomorphic and polymorphic) are named by the hash
|
|
346
|
-
of their normalized definition and stored in a similar key-value store.
|
|
347
|
-
|
|
348
|
-
Relations can be indexed by their derived contracts—the connected $(input, output)$
|
|
349
|
-
schema graph—enabling instant lookup of transformations that map between specific schemas
|
|
350
|
-
or preserve specific subtrees ($=X$).
|
|
351
|
-
|
|
352
|
-
## Serialization and Compilation
|
|
353
|
-
|
|
354
|
-
The language is designed to transform self-describing binary streams of
|
|
355
|
-
`(schema, payload)`, where the payload is a labeled tree conforming to the
|
|
356
|
-
accompanying schema. The transformation is executed by partial relations
|
|
357
|
-
defined in the language. Non-recursive and tail-recursive (and even some
|
|
358
|
-
non-tail-recursive) relations can be compiled into deterministic finite
|
|
359
|
-
(pushdown) transducers.
|
|
360
|
-
|
|
361
|
-
Ahead-Of-Time (AOT) compilation produces four primary file formats (see
|
|
362
|
-
[`DOCS/FILE_FORMATS.md`](./FILE_FORMATS.md)):
|
|
363
|
-
- `.k`: Plain source text defining schemas and relations.
|
|
364
|
-
- `.ko`: AOT contract-checked executable object container (`KOBJ\n` + JSON).
|
|
365
|
-
- `.klib`: AOT contract-checked library container (JSON).
|
|
366
|
-
- `.kvm`: Polymorphic register-IR template (`layer: "KVM-P"`).
|
|
367
|
-
|
|
368
|
-
Downstream backends compile or specialize these into WebAssembly
|
|
369
|
-
(`.wasm`) and native binaries.
|
|
370
|
-
|
|
371
|
-
### AND-OR graphs for encoding and decoding payloads
|
|
372
|
-
|
|
373
|
-
This provides a generic and compact way to encode and decode payload
|
|
374
|
-
values under an algebraic schema. The idea is to use _prefix codes_ (a class of languages such
|
|
375
|
-
that no word is a prefix of another word) to encode variant payloads deterministically.
|
|
376
|
-
|
|
377
|
-
### Rust data structures
|
|
378
|
-
|
|
379
|
-
A schema can be translated into a Rust data structure, and a relation
|
|
380
|
-
can be translated into a Rust function.
|
|
381
|
-
|
|
382
|
-
## Linking with Other Languages
|
|
383
|
-
|
|
384
|
-
## Streams and Arrays (Map-Reduce)
|
|
385
|
-
|
|
386
|
-
## Bits and Absence of Built-in Numbers
|
|
387
|
-
|
|
388
|
-
In `k`, there are no primitive numbers or built-in integer literals.
|
|
389
|
-
Numerical identifiers such as `5` or `10` are nullary relations
|
|
390
|
-
(constants) mapping the unit `{}` to bit trees of contract `{} -> bits`.
|
|
391
|
-
Arithmetic relations operate on signed integers (`int`), requiring
|
|
392
|
-
conversion via `5 int`.
|
|
393
|
-
|
|
394
|
-
Bit trees are defined algebraically:
|
|
395
|
-
|
|
396
|
-
```text
|
|
397
|
-
bits? = ?< {} _, B 0, B 1 > = B;
|
|
398
|
-
```
|
|
399
|
-
|
|
400
|
-
### Literals for `@bits` are:
|
|
401
|
-
|
|
402
|
-
- `0b` for 0 bits
|
|
403
|
-
- `0b10` for 2 bits
|
|
404
|
-
- `0xF` for 4 bits
|
|
405
|
-
- `0xF0` for 8 bits
|
|
406
|
-
- `0o1` for 3 bits
|
|
407
|
-
- `0o7` for 3 bits
|
|
408
|
-
- "" for 0 bits
|
|
409
|
-
- "ala" for 24 bits
|
|
410
|
-
|
|
411
|
-
### Two operations on `bits`:
|
|
412
|
-
|
|
413
|
-
#### Eat `/`
|
|
414
|
-
|
|
415
|
-
- `bits / bits` --- division, e.g., `0b1011 / 0b10` = `0b11`,
|
|
416
|
-
`"abc" / "a"` = `"bc"`
|
|
417
|
-
|
|
418
|
-
#### Prepend `\`
|
|
419
|
-
|
|
420
|
-
- `bits \ bits` --- multiplication, e.g., `0b11 \ 0b10` = `0b1011`,
|
|
421
|
-
`"bc" \ "a"` = `"abc"`
|
|
422
|
-
|
|
423
|
-
Empty `bits` can be checked by:
|
|
424
|
-
|
|
425
|
-
```k
|
|
426
|
-
kind = bits
|
|
427
|
-
< {/ 0b0 \ 0b0 starts_with_0}, {/ 0b1 \ 0b1 starts_with_1}, {() empty} >
|
|
428
|
-
< bits starts_with_0, bits starts_with_1, bits empty >;
|
|
429
|
-
```
|
|
430
|
-
|
|
431
|
-
## Further Reading
|
|
432
|
-
|
|
433
|
-
- [DOCS/FILE_FORMATS.md](./FILE_FORMATS.md) — guide to
|
|
434
|
-
.k, .ko, .klib, and .kvm formats
|
|
435
|
-
- [DOCS/DICTIONARY.md](./DICTIONARY.md) — concept names and terminology
|
|
436
|
-
- [DOCS/TYPE_DERIVATION.md](./TYPE_DERIVATION.md) — type derivation
|
|
437
|
-
algorithm
|
|
438
|
-
- [DOCS/OBJECT_FILE_AND_PATTERN.md](./OBJECT_FILE_AND_PATTERN.md) —
|
|
439
|
-
object format and schema encoding
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
# Codec Wire Format Status
|
|
2
|
-
|
|
3
|
-
This note tracks the current `k-parse | k | k-print` pipeline after the move
|
|
4
|
-
to self-hosted binary pattern framing.
|
|
5
|
-
|
|
6
|
-
## Current Shape
|
|
7
|
-
|
|
8
|
-
The only wire format is:
|
|
9
|
-
|
|
10
|
-
```text
|
|
11
|
-
encode(pattern_value : pattern) encode(value : decoded_pattern)
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
where:
|
|
15
|
-
|
|
16
|
-
- the first segment is a normal k value of pattern `pattern`, encoded under the
|
|
17
|
-
fixed singleton pattern for `pattern` from [`core.k`](../core.k),
|
|
18
|
-
- that decoded pattern becomes the structural authority for the second segment,
|
|
19
|
-
- the second segment is the prefix-free value payload interpreted by that
|
|
20
|
-
pattern.
|
|
21
|
-
|
|
22
|
-
Decoding the stream produces a materialized `Value` that carries the decoded
|
|
23
|
-
pattern as `value.pattern`. The evaluator propagates the carried pattern, and
|
|
24
|
-
encoding uses it by default.
|
|
25
|
-
|
|
26
|
-
There is no second JSON transport format. Property-list JSON may still appear in
|
|
27
|
-
tests or debug output as a readable notation for pattern graphs, but it is not a
|
|
28
|
-
codec container and is not accepted at the command boundary.
|
|
29
|
-
|
|
30
|
-
## Current Pipeline
|
|
31
|
-
|
|
32
|
-
1. `k-parse`: text value plus optional pattern/type script -> binary pattern+value stream
|
|
33
|
-
2. `k`: binary input -> `Value(pattern, tree)` -> evaluate `k` expression -> binary pattern+value stream
|
|
34
|
-
3. `k-print`: binary input -> JSON-like textual k value
|
|
35
|
-
|
|
36
|
-
This keeps the evaluator stage free of formatting concerns while staying close
|
|
37
|
-
to the new semantic codec model.
|
|
38
|
-
|
|
39
|
-
When `k-parse` has no explicit input pattern or type, it builds the pattern from
|
|
40
|
-
the parsed value tree. Empty nodes become closed products, multi-child nodes
|
|
41
|
-
become closed products, and one-child nodes become open unions by default. An
|
|
42
|
-
explicit product pattern can force a one-child node to be treated as a singleton
|
|
43
|
-
product.
|
|
44
|
-
|
|
45
|
-
The derived wire pattern is canonicalized by collapsing finite closed
|
|
46
|
-
subtrees from the leaves upward, starting at `["closed-product", []]`. Open
|
|
47
|
-
pattern nodes remain distinct. This is pattern graph canonicalization, not
|
|
48
|
-
value-payload DAG compression.
|
|
49
|
-
|
|
50
|
-
## Verified Workflow
|
|
51
|
-
|
|
52
|
-
```bash
|
|
53
|
-
echo '["zebara", "ela", "kupa", ala, owca]' | \
|
|
54
|
-
k-parse | \
|
|
55
|
-
k '{.1 0,.3 1}' | \
|
|
56
|
-
k-print
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
Expected output:
|
|
60
|
-
|
|
61
|
-
```json
|
|
62
|
-
["ela","ala"]
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
## Open Work
|
|
66
|
-
|
|
67
|
-
- tighten the pattern/value validation rules,
|
|
68
|
-
- add direct coverage for more non-ASCII pattern labels in CLI fixtures,
|
|
69
|
-
- extend tests and examples for the prefix codec.
|
|
70
|
-
|
|
71
|
-
For the semantic design, see:
|
|
72
|
-
|
|
73
|
-
- [`codecs/README.md`](../codecs/README.md)
|
|
74
|
-
- [`codecs/POLYMORPHIC_BINARY_FORMAT.md`](../codecs/POLYMORPHIC_BINARY_FORMAT.md)
|