@fraczak/k 8.2.0 → 8.4.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 +73 -0
- package/DOCS/CANONICAL_PATTERNS.md +349 -0
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +12 -4
- package/DOCS/REPL.md +30 -3
- package/Examples/arithmetics.k +31 -14
- package/Examples/bnat.k +13 -6
- package/Examples/buda.k +9 -4
- package/Examples/byte.k +23 -9
- package/Examples/ieee.k +102 -34
- package/Examples/karatsuba-mult.k +23 -9
- package/Examples/nat.k +3 -2
- package/Examples/poly.k +30 -16
- package/Examples/vhdl_to_k.k +7 -2
- package/README.md +33 -5
- package/backend-api.mjs +6 -1
- package/backends/llvm/src/cli.mjs +3 -33
- package/backends/wasm/src/cli.mjs +3 -33
- package/backends/wasm/src/wasm.mjs +1 -29
- package/browser/vfs-data.mjs +8 -8
- package/canonical-pattern.mjs +407 -0
- package/k.mjs +12 -29
- package/kvm.mjs +2 -28
- package/named-patterns.mjs +35 -2
- package/object.mjs +137 -20
- package/objects/compile.mjs +2 -29
- package/objects/validate.mjs +13 -1
- package/package.json +1 -1
- package/parser.jison +72 -10
- package/parser.mjs +114 -49
- package/pretty.mjs +19 -4
- package/repl.mjs +319 -104
- package/scripts/build-repl-html.mjs +1 -1
- package/tests/test-k-object.mjs +5 -1
- package/tests/test-named-patterns.mjs +439 -0
- package/tests/test-repl.mjs +163 -12
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,79 @@ All notable changes to this project will be documented here.
|
|
|
5
5
|
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
6
6
|
This project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [8.4.0] — 2026-10-23 — Linear Skeletons & Parameter Diagonalization
|
|
9
|
+
|
|
10
|
+
### Pure Linear Skeleton Model for Parameterized Patterns
|
|
11
|
+
|
|
12
|
+
- **Never Equalize Leaves in Canonical Templates**:
|
|
13
|
+
- The canonicalizer derives a pure linear skeleton for all
|
|
14
|
+
parameterized patterns, treating every open leaf position as an
|
|
15
|
+
independent port (`X0, X1, X2, ...`).
|
|
16
|
+
- Leaves are never unified in the canonical pattern graph,
|
|
17
|
+
guaranteeing maximum template deduplication across all variations
|
|
18
|
+
of a structural schema (records, products, sums).
|
|
19
|
+
- **Parameter Diagonalization in Aliases**:
|
|
20
|
+
- Non-linear equality constraints across leaves are represented as
|
|
21
|
+
diagonalized argument vectors (`aliasArgs`) on the alias definition
|
|
22
|
+
itself rather than inside the canonical template graph:
|
|
23
|
+
* `same_pair[T] = @hash[T, T];` (formal parameter equality)
|
|
24
|
+
* `closed_same_pair[] = @hash[T, T];` (existential variable equality)
|
|
25
|
+
* `p[X] = @hash[X, Y, Y];` (mixed formal and existential equality)
|
|
26
|
+
* `p[] = @hash[X, X];` (pure existential equality)
|
|
27
|
+
- When instantiated, shared existential variables are renamed to fresh
|
|
28
|
+
unification variables (`_Y_np1`), enforcing structural equality at
|
|
29
|
+
use-time with full type safety.
|
|
30
|
+
- **Canonical Sharing Across Diagonalizations**:
|
|
31
|
+
- `pair[A, B]`, `same_pair[T]`, `closed_same_pair[]`, and `any_pair[]`
|
|
32
|
+
all compile to and share the exact same canonical pattern graph and
|
|
33
|
+
content hash (`@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm`).
|
|
34
|
+
- `diag_p[] = { X x, X y }` shares the same canonical hash as
|
|
35
|
+
`a[]`, `b[X]`, `c[X, Y]`, `d[Y, X]`.
|
|
36
|
+
- **Object File Persistence**:
|
|
37
|
+
- Ahead-of-time `.klib` and `.ko` object files persist canonical
|
|
38
|
+
deduplicated linear templates under `patterns[@hash]`, alias mappings
|
|
39
|
+
under `patternAlias`, and diagonalized routing vectors in
|
|
40
|
+
`meta[hash].origins[].aliasArgs`.
|
|
41
|
+
- **Documentation**:
|
|
42
|
+
- Updated `DOCS/CANONICAL_PATTERNS.md` and
|
|
43
|
+
`DOCS/OBJECT_FILE_AND_PATTERN.md`.
|
|
44
|
+
|
|
45
|
+
## [8.3.0] — 2026-10-22 — Content-Addressable Patterns & Aliasing
|
|
46
|
+
|
|
47
|
+
### Content-Addressable Canonical Patterns
|
|
48
|
+
|
|
49
|
+
- **Canonical Pattern Derivation & Minimal Automata**:
|
|
50
|
+
- All pattern definitions compile to minimal regular tree automata
|
|
51
|
+
identified by deterministic content-addressed hashes (`@hash`).
|
|
52
|
+
- Canonical patterns take the full arity of all discovered variable
|
|
53
|
+
nodes (e.g. `@hash[X0, X1] = {X0 x, X1 y};`).
|
|
54
|
+
- Open wildcards `(...)` lower to open type holes equivalent to
|
|
55
|
+
unbound variables, sharing canonical templates across definitions.
|
|
56
|
+
- **Named Patterns as Parameter Routing Aliases**:
|
|
57
|
+
- Named patterns (`p[X] = {X x, Y y};`) are lexical aliases routing
|
|
58
|
+
caller arguments to canonical parameter slots, padding unexposed
|
|
59
|
+
internal ports with wildcards: `p[X] = @hash[X, (...)];`.
|
|
60
|
+
- Isomorphic pattern permutations (`a[]`, `b[X]`, `c[X,Y]`, `d[Y,X]`)
|
|
61
|
+
share the identical canonical hash, enabling deduplication.
|
|
62
|
+
- Multi-level nested template applications and parameter reorderings
|
|
63
|
+
resolve and decompile accurately.
|
|
64
|
+
- **Unification Preservation for Wildcards**:
|
|
65
|
+
- Repeated internal existential variables in canonical templates
|
|
66
|
+
preserve structural type-equality constraints when instantiated
|
|
67
|
+
with wildcard arguments (`(...)`), preventing type pollution.
|
|
68
|
+
- **Knot Safety**:
|
|
69
|
+
- The grammar strictly rejects parameter names that conflict with
|
|
70
|
+
recursive knot variables (`= Knot`), preserving cycle semantics
|
|
71
|
+
($\mu$-fixpoints) and preventing recursion destruction.
|
|
72
|
+
- **REPL & Persistence**:
|
|
73
|
+
- `:pats` lists all defined aliases and their canonical port routing.
|
|
74
|
+
- `:pat <name>` decompiles and displays canonical definitions.
|
|
75
|
+
- Saving to `.klib` preserves parameter and alias routing metadata
|
|
76
|
+
origins without duplication.
|
|
77
|
+
- **Documentation**:
|
|
78
|
+
- Authored `DOCS/CANONICAL_PATTERNS.md` and updated `README.md` and
|
|
79
|
+
`DOCS/REPL.md`.
|
|
80
|
+
|
|
8
81
|
## [8.2.0] — 2026-10-21 — Canonical Pattern Decompilation & Parameters
|
|
9
82
|
|
|
10
83
|
### Interactive REPL & Tooling
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
# Content-Addressable Canonical Patterns and Parameter Aliasing
|
|
2
|
+
|
|
3
|
+
## 1. Overview and Core Philosophy
|
|
4
|
+
|
|
5
|
+
In k, all relations, codes, and patterns are **content-addressable**.
|
|
6
|
+
A human-readable name assigned to a pattern template (such as
|
|
7
|
+
`list[X]` or `pair[A, B]`) is purely a lexical **alias** pointing to an
|
|
8
|
+
underlying, uniquely identified canonical pattern graph addressed by its
|
|
9
|
+
cryptographic hash `@hash`.
|
|
10
|
+
|
|
11
|
+
This document specifies:
|
|
12
|
+
1. How canonical patterns are derived and hashed from directed graphs.
|
|
13
|
+
2. How user-defined named patterns act as parameter routing aliases
|
|
14
|
+
using wildcards `(...)` for unexposed internal holes.
|
|
15
|
+
3. Why the variable pointing to the root of a recursive pattern graph
|
|
16
|
+
(and cycle knots in general) is an internal structural fixed-point
|
|
17
|
+
and cannot be exposed as an external parameter.
|
|
18
|
+
4. How canonical patterns, aliases, and argument routing are persisted
|
|
19
|
+
in object files (`.klib` and `.ko`).
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## 2. Canonical Pattern Derivation: The Pure Linear Skeleton
|
|
24
|
+
|
|
25
|
+
When a pattern is compiled:
|
|
26
|
+
```k
|
|
27
|
+
p[X] = {X x, Y y, Y z};
|
|
28
|
+
```
|
|
29
|
+
the compiler evaluates the right-hand filter into a
|
|
30
|
+
**pure linear skeleton**.
|
|
31
|
+
|
|
32
|
+
### 2.1 Never Equalizing Pattern Leaves
|
|
33
|
+
|
|
34
|
+
In the pure skeleton model, two leaves of a pattern graph are **never
|
|
35
|
+
equalized** in the canonical template:
|
|
36
|
+
- Even if an author repeats an existential variable (like `Y` at fields
|
|
37
|
+
`y` and `z`), or repeats a formal parameter (like `T` in
|
|
38
|
+
`same_pair[T] = { T first, T second }`), the canonicalizer assigns an
|
|
39
|
+
independent, distinct canonical port `X0, X1, X2, ...` to every open
|
|
40
|
+
leaf slot.
|
|
41
|
+
- Any non-linear equality constraint between leaves is represented
|
|
42
|
+
outside the canonical graph, in the **diagonalized argument vector** of
|
|
43
|
+
the alias definition.
|
|
44
|
+
|
|
45
|
+
In the example `p[X] = {X x, Y y, Y z}`:
|
|
46
|
+
- Field `x` becomes canonical port `X0`.
|
|
47
|
+
- Field `y` becomes canonical port `X1`.
|
|
48
|
+
- Field `z` becomes canonical port `X2`.
|
|
49
|
+
|
|
50
|
+
The canonical pattern is the 3-port linear product:
|
|
51
|
+
```k
|
|
52
|
+
@PScsPA3xYv4kSXUqHafwVQr5Cxkmspf3bj46KAcmyxmR[X0, X1, X2] = {
|
|
53
|
+
X0 x, X1 y, X2 z
|
|
54
|
+
};
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Canonical patterns always take the full arity of all leaf positions
|
|
58
|
+
discovered in the pattern skeleton.
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## 3. Named Patterns as Parameter Routing and Diagonalization Aliases
|
|
63
|
+
|
|
64
|
+
When an author defines an alias like:
|
|
65
|
+
```k
|
|
66
|
+
p[X] = {X x, Y y, Y z};
|
|
67
|
+
```
|
|
68
|
+
the definition `p[X]` does **not** create a separate canonical pattern
|
|
69
|
+
graph. Instead, it defines an alias that routes:
|
|
70
|
+
- The author's formal parameter `X` to slot `X0`.
|
|
71
|
+
- The shared existential variable `Y` to both slots `X1` and `X2`:
|
|
72
|
+
|
|
73
|
+
```k
|
|
74
|
+
p[X] = @PScsPA3xYv4kSXUqHafwVQr5Cxkmspf3bj46KAcmyxmR[X, Y, Y];
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
When `p` is instantiated (e.g. `p[int]`), the template expansion engine:
|
|
78
|
+
1. Substitutes formal parameter `X` with caller argument `int`.
|
|
79
|
+
2. Renames internal existential variable `Y` to a unique fresh variable
|
|
80
|
+
`_Y_np1` across all occurrences in the argument list:
|
|
81
|
+
`@PScs...[int, _Y_np1, _Y_np1]`.
|
|
82
|
+
3. Expands the canonical template with those arguments, yielding:
|
|
83
|
+
`{ int x, _Y_np1 y, _Y_np1 z }`.
|
|
84
|
+
4. The type derivation engine unifies the type of field `y` with the type
|
|
85
|
+
of field `z` via the shared fresh variable `_Y_np1`.
|
|
86
|
+
|
|
87
|
+
Thus, existential equality constraints are preserved with full type-safety
|
|
88
|
+
at use-time, while enabling maximum structural template deduplication.
|
|
89
|
+
|
|
90
|
+
### 3.1 Canonical Pattern Sharing Across Diagonalizations and Permutations
|
|
91
|
+
|
|
92
|
+
Under this architecture, all variations over the same underlying product
|
|
93
|
+
or union structure share the **exact same canonical pattern graph and
|
|
94
|
+
content hash**:
|
|
95
|
+
|
|
96
|
+
#### Example 1: Pair Variations
|
|
97
|
+
```k
|
|
98
|
+
pair[A, B] = { A first, B second };
|
|
99
|
+
same_pair[T] = { T first, T second };
|
|
100
|
+
closed_same_pair[] = { T first, T second };
|
|
101
|
+
any_pair[] = { (...) first, (...) second };
|
|
102
|
+
```
|
|
103
|
+
All four compile to the single canonical skeleton:
|
|
104
|
+
```k
|
|
105
|
+
@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm[X0, X1] = {
|
|
106
|
+
X0 first, X1 second
|
|
107
|
+
};
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Their `:pats` alias representations reflect their respective parameter
|
|
111
|
+
routings and diagonalizations:
|
|
112
|
+
```text
|
|
113
|
+
> :pats
|
|
114
|
+
any_pair[] = @KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm[(...),(...)]
|
|
115
|
+
closed_same_pair[] = @KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm[T,T]
|
|
116
|
+
pair[A,B] = @KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm[A,B]
|
|
117
|
+
same_pair[T] = @KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm[T,T]
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
#### Example 2: Record `{ x, y }` Variations
|
|
121
|
+
```k
|
|
122
|
+
a[] = { (...) x, (...) y };
|
|
123
|
+
b[X] = { X x, (...) y };
|
|
124
|
+
c[X,Y] = { X x, Y y };
|
|
125
|
+
d[Y,X] = { X x, Y y };
|
|
126
|
+
diag_p[] = { X x, X y };
|
|
127
|
+
```
|
|
128
|
+
All five share `@NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x`:
|
|
129
|
+
```text
|
|
130
|
+
> :pats
|
|
131
|
+
a[] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[(...),(...)]
|
|
132
|
+
b[X] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,(...)]
|
|
133
|
+
c[X,Y] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,Y]
|
|
134
|
+
d[Y,X] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,Y]
|
|
135
|
+
diag_p[] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,X]
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
## 4. Direct Usability of Canonical Patterns
|
|
141
|
+
|
|
142
|
+
Because the alias is just an abbreviation, canonical patterns can be used
|
|
143
|
+
everywhere relations and aliases can be used:
|
|
144
|
+
|
|
145
|
+
1. **Direct Inspection**:
|
|
146
|
+
```text
|
|
147
|
+
> :pat @PScsPA3xYv4kSXUqHafwVQr5Cxkmspf3bj46KAcmyxmR
|
|
148
|
+
@PScsPA3xYv4kSXUqHafwVQr5Cxkmspf3bj46KAcmyxmR[X0, X1, X2] = {
|
|
149
|
+
X0 x, X1 y, X2 z
|
|
150
|
+
};
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
2. **Direct Matching in Expressions**:
|
|
154
|
+
Callers can instantiate canonical patterns directly by passing all
|
|
155
|
+
canonical arguments:
|
|
156
|
+
```k
|
|
157
|
+
{ {} x, {} y, {} z } ? @PScsPA3xYv4kSXUqHafwVQr5Cxkmspf3bj46KAcmyxmR[
|
|
158
|
+
{}, {}, {}
|
|
159
|
+
];
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
3. **Explicit Definition by Hash**:
|
|
163
|
+
Canonical pattern definitions are accepted directly by the grammar:
|
|
164
|
+
```k
|
|
165
|
+
@PScsPA3xYv4kSXUqHafwVQr5Cxkmspf3bj46KAcmyxmR[X0, X1, X2] = {
|
|
166
|
+
X0 x, X1 y, X2 z
|
|
167
|
+
};
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## 5. Alias Argument Generation Rules
|
|
173
|
+
|
|
174
|
+
For every canonical port `Xi` at path `P` in the canonical filter:
|
|
175
|
+
Let `t` be the token found at path `P` in the author's filter.
|
|
176
|
+
The corresponding alias argument is determined by:
|
|
177
|
+
1. **Wildcard leaf**: If `t` is absent or an explicit `(...)` wildcard,
|
|
178
|
+
the argument is `"(...)"`.
|
|
179
|
+
2. **Formal parameter**: If `t` is declared in `pattern_params`, the
|
|
180
|
+
argument is `t` (e.g. `[T, T]` for diagonalized parameter `T`).
|
|
181
|
+
3. **Repeated existential variable**: If `t` is an internal variable
|
|
182
|
+
appearing $> 1$ times in the filter, the argument is `t` (e.g. `[X, X]`
|
|
183
|
+
or `[Z, Y, Y]`), preserving existential equality across fields.
|
|
184
|
+
4. **Single-occurrence internal variable**: If `t` is an internal variable
|
|
185
|
+
appearing only once, it imposes no constraints and is normalized to
|
|
186
|
+
`"(...)"`.
|
|
187
|
+
|
|
188
|
+
---
|
|
189
|
+
|
|
190
|
+
## 6. Why Knots (and Root Knots) Cannot Be Exposed as Parameters
|
|
191
|
+
|
|
192
|
+
Consider the recursive linked list pattern:
|
|
193
|
+
```k
|
|
194
|
+
list[X] = < {} nil, { X car, Y cdr } cons > = Y;
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
In this definition:
|
|
198
|
+
- `= Y` binds the variable `Y` to the root union node of the pattern
|
|
199
|
+
graph.
|
|
200
|
+
- Inside the second variant `{ X car, Y cdr } cons`, the field `cdr`
|
|
201
|
+
references `Y`.
|
|
202
|
+
- This creates a directed back-edge from `cdr` to the root union, forming
|
|
203
|
+
a recursive cycle ($\mu$-fixpoint).
|
|
204
|
+
|
|
205
|
+
### 6.1 The Fundamental Distinction: Open Ports vs. Bound Knots
|
|
206
|
+
|
|
207
|
+
There is a fundamental semantic contradiction between **parameters** and
|
|
208
|
+
**knots**:
|
|
209
|
+
|
|
210
|
+
| Feature | Parameter (`[P]`) | Knot (`= KnotName`) |
|
|
211
|
+
| :--- | :--- | :--- |
|
|
212
|
+
| **Quantification** | Universal ($\forall$) | Fixed-point ($\mu$) |
|
|
213
|
+
| **Binding State** | **Unbound / Open Port** | **Bound / Node** |
|
|
214
|
+
| **Role** | Hole to fill by caller | Internal back-edge cycle |
|
|
215
|
+
| **Scope** | Caller interface | Purely internal topology |
|
|
216
|
+
|
|
217
|
+
- An **external parameter** is an open socket waiting for caller input.
|
|
218
|
+
- A **cycle knot** is an internal wire already soldered to a concrete
|
|
219
|
+
node in the graph.
|
|
220
|
+
|
|
221
|
+
### 6.2 The Shadowing Hazard: Destruction of Recursion
|
|
222
|
+
|
|
223
|
+
What happens if an author attempts to expose the root knot `Y` as a
|
|
224
|
+
parameter?
|
|
225
|
+
```k
|
|
226
|
+
list[X, Y] = < {} nil, { X car, Y cdr } cons > = Y; -- INVALID!
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
If this definition were permitted:
|
|
230
|
+
1. `Y` would appear in the formal parameter list `[X, Y]`.
|
|
231
|
+
2. When the template is instantiated (e.g. `list[int[], MyTail]`), the
|
|
232
|
+
argument `MyTail` would substitute every occurrence of `Y` in the
|
|
233
|
+
body.
|
|
234
|
+
3. Consequently, `{ X car, Y cdr }` would become
|
|
235
|
+
`{ int[] car, MyTail cdr }`.
|
|
236
|
+
4. The back-edge to the root union would be severed!
|
|
237
|
+
5. The recursive cycle would be destroyed, silently corrupting the
|
|
238
|
+
recursive list into a non-recursive pair
|
|
239
|
+
`<{} nil, {int[] car, MyTail cdr} cons>`.
|
|
240
|
+
|
|
241
|
+
### 6.3 Enforced Invariant: Conflict Rejection at Parse Time
|
|
242
|
+
|
|
243
|
+
Because cycle knots represent bound internal topology, exposing a knot
|
|
244
|
+
name in `pattern_params` is a direct contradiction.
|
|
245
|
+
|
|
246
|
+
The compiler strictly enforces that no formal parameter name may
|
|
247
|
+
conflict with any knot identifier in the filter:
|
|
248
|
+
|
|
249
|
+
```k
|
|
250
|
+
list[X, Y] = < {} nil, { X car, Y cdr } cons > = Y;
|
|
251
|
+
-- Parse Error: Pattern parameter 'Y' in pattern 'list'
|
|
252
|
+
-- conflicts with knot 'Y'. Knot variables cannot be pattern parameters.
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
The root node of a pattern graph is matched against incoming data by the
|
|
256
|
+
runtime; it is never an open parameter port.
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
## 7. Reflection of Patterns in Object Files (`.klib` and `.ko`)
|
|
261
|
+
|
|
262
|
+
Ahead-of-time compiled libraries (`.klib`) and executables (`.ko`) store
|
|
263
|
+
patterns in their top-level object container (`format: "k-object"`).
|
|
264
|
+
Patterns are persisted across three coordinated structures:
|
|
265
|
+
|
|
266
|
+
### 7.1 `patternAlias` (Lexical Names to Content Hashes)
|
|
267
|
+
|
|
268
|
+
Maps human-readable pattern names to their canonical content hashes.
|
|
269
|
+
All aliases sharing the same linear skeleton point to the same hash:
|
|
270
|
+
|
|
271
|
+
```json
|
|
272
|
+
"patternAlias": {
|
|
273
|
+
"pair": "@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm",
|
|
274
|
+
"same_pair": "@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm",
|
|
275
|
+
"closed_same_pair": "@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm",
|
|
276
|
+
"p": "@PScsPA3xYv4kSXUqHafwVQr5Cxkmspf3bj46KAcmyxmR"
|
|
277
|
+
}
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### 7.2 `patterns` (Deduplicated Canonical ASTs)
|
|
281
|
+
|
|
282
|
+
The `patterns` dictionary stores the canonical template definition under
|
|
283
|
+
its content hash `@hash`:
|
|
284
|
+
- **Deduplicated**: Even if a program defines multiple variations over a
|
|
285
|
+
pair or record, the canonical template is stored exactly once under
|
|
286
|
+
its `@hash`.
|
|
287
|
+
- **Linear Multi-Hole Functor**: Every leaf position is an independent
|
|
288
|
+
canonical parameter (`X0, X1, ...`), with no leaves unified in the
|
|
289
|
+
persisted filter AST.
|
|
290
|
+
|
|
291
|
+
```json
|
|
292
|
+
"patterns": {
|
|
293
|
+
"@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm": {
|
|
294
|
+
"params": ["X0", "X1"],
|
|
295
|
+
"filter": {
|
|
296
|
+
"type": "product",
|
|
297
|
+
"fields": {
|
|
298
|
+
"first": { "type": "name", "name": "X0", "open": true },
|
|
299
|
+
"second": { "type": "name", "name": "X1", "open": true }
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
### 7.3 `meta` (Provenance & `aliasArgs` Routing Vectors)
|
|
307
|
+
|
|
308
|
+
The `meta` dictionary maps each `@hash` to its canonical port list
|
|
309
|
+
and an `origins` array recording how every alias projects caller
|
|
310
|
+
parameters into the linear skeleton:
|
|
311
|
+
|
|
312
|
+
```json
|
|
313
|
+
"meta": {
|
|
314
|
+
"@KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm": {
|
|
315
|
+
"type": "pattern",
|
|
316
|
+
"params": ["X0", "X1"],
|
|
317
|
+
"origins": [
|
|
318
|
+
{
|
|
319
|
+
"name": "pair",
|
|
320
|
+
"params": ["A", "B"],
|
|
321
|
+
"aliasArgs": ["A", "B"],
|
|
322
|
+
"compiledAt": "2026-10-23T19:38:29.691Z"
|
|
323
|
+
},
|
|
324
|
+
{
|
|
325
|
+
"name": "same_pair",
|
|
326
|
+
"params": ["T"],
|
|
327
|
+
"aliasArgs": ["T", "T"],
|
|
328
|
+
"compiledAt": "2026-10-23T19:38:29.691Z"
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
"name": "closed_same_pair",
|
|
332
|
+
"params": [],
|
|
333
|
+
"aliasArgs": ["T", "T"],
|
|
334
|
+
"compiledAt": "2026-10-23T19:38:29.691Z"
|
|
335
|
+
}
|
|
336
|
+
]
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
The `aliasArgs` vector:
|
|
342
|
+
1. Records **parameter diagonalization** (e.g. `["T", "T"]`), instructing
|
|
343
|
+
the runtime to supply formal argument `T` to both canonical ports.
|
|
344
|
+
2. Preserves **existential constraints** (e.g. `["X", "Y", "Y"]`),
|
|
345
|
+
ensuring `Y` is renamed to a shared fresh variable when instantiated.
|
|
346
|
+
3. Enables **lossless alias export**: when generating `--export` preambles
|
|
347
|
+
or exporting `.klib` files, `buildExportPreamble` reads `origin.params`
|
|
348
|
+
and `origin.aliasArgs` to recreate the exact source-level alias
|
|
349
|
+
definitions.
|
|
@@ -124,15 +124,23 @@ The payload stores:
|
|
|
124
124
|
- each relation's type-pattern graph state,
|
|
125
125
|
- each relation's `typeDerivation.status`,
|
|
126
126
|
- relation aliases in `relAlias`,
|
|
127
|
+
- pattern aliases in `patternAlias`,
|
|
128
|
+
- deduplicated canonical pattern ASTs in `patterns`,
|
|
127
129
|
- compiler convergence details in top-level `compileStats`,
|
|
128
|
-
- metadata, including aliases
|
|
130
|
+
- metadata (`meta`), including aliases, origin source locations, and
|
|
131
|
+
pattern parameter routing vectors (`aliasArgs`),
|
|
129
132
|
- the main relation name.
|
|
130
133
|
|
|
131
134
|
Relation `def` values do not store source `start` / `end` markers. Source
|
|
132
135
|
locations live on `meta[hash].origins[]` entries. Each metadata entry has
|
|
133
|
-
`type: "code"` or `type: "
|
|
134
|
-
`compiledAt`,
|
|
135
|
-
`
|
|
136
|
+
`type: "code"`, `type: "rel"`, or `type: "pattern"`. Origin entries hold
|
|
137
|
+
`source`, `name`, `compiledAt`, optional `start` / `end`, and for
|
|
138
|
+
patterns, `params` and `aliasArgs` (recording parameter routing and
|
|
139
|
+
diagonalization). Origin entries do not have `kind`.
|
|
140
|
+
|
|
141
|
+
For complete details on canonical pattern skeletons, alias argument
|
|
142
|
+
routing, and object reflection, see
|
|
143
|
+
[`DOCS/CANONICAL_PATTERNS.md`](./CANONICAL_PATTERNS.md).
|
|
136
144
|
|
|
137
145
|
Relation `def` values also do not store generated input/output filters.
|
|
138
146
|
Those filters are derived from `def.patterns` and `typePatternGraph` when
|
package/DOCS/REPL.md
CHANGED
|
@@ -38,7 +38,7 @@ is complete.
|
|
|
38
38
|
| `:rel name` | Show relation definition |
|
|
39
39
|
| `:rels` | List relation aliases |
|
|
40
40
|
| `:pattern name` | Show canonical definition of pattern (alias: `:pat`) |
|
|
41
|
-
| `:patterns` | List
|
|
41
|
+
| `:patterns` | List pattern aliases as `alias = @hash` (alias: `:pats`) |
|
|
42
42
|
| `:limit [num\|none]` | Set maximum characters for value output (default: 4096) |
|
|
43
43
|
| `:codec load file` | Load a codec from an ES module file (e.g. `:codec load codecs/int.mjs`) |
|
|
44
44
|
| `:codec unload name` | Unload a registered codec |
|
|
@@ -159,8 +159,15 @@ aliases, and metadata into the session. Aliases are recovered from
|
|
|
159
159
|
|
|
160
160
|
### `:patterns` (or `:pats`)
|
|
161
161
|
|
|
162
|
-
Lists all
|
|
163
|
-
|
|
162
|
+
Lists all defined named pattern aliases and their canonical `@hash`
|
|
163
|
+
addresses (formatted as `alias = @hash`, matching `:rels`):
|
|
164
|
+
|
|
165
|
+
```text
|
|
166
|
+
> :pats
|
|
167
|
+
bit = @MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg
|
|
168
|
+
list = @JagVexzgseRr4rVZRmVpsMbr2AKXDjzZnz6Qnf8a9SNe
|
|
169
|
+
pair = @KqTBNkznjWWwync2Mm94XjCez63neJBJtgYcJJEE5kEm
|
|
170
|
+
```
|
|
164
171
|
|
|
165
172
|
### `:pattern <name>` (or `:pat <name>`, `:pat @hash`)
|
|
166
173
|
|
|
@@ -171,6 +178,12 @@ Displays the **canonical definition** of a named pattern template:
|
|
|
171
178
|
> :pat pair
|
|
172
179
|
pair[X0, X1] = {X0 first, X1 second}; -- @...
|
|
173
180
|
|
|
181
|
+
> c[X,Y] = {X x, Y y};d[Y,X] = {X x, Y y};
|
|
182
|
+
> :pat c
|
|
183
|
+
c[X0, X1] = {X0 x, X1 y}; -- @NPSHm9...
|
|
184
|
+
> :pat d
|
|
185
|
+
d[X1, X0] = {X0 x, X1 y}; -- @NPSHm9...
|
|
186
|
+
|
|
174
187
|
> list[T] = < {} nil, { T car, Y cdr } cons > = Y;
|
|
175
188
|
> :pat list
|
|
176
189
|
list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- @...
|
|
@@ -180,6 +193,20 @@ list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- @...
|
|
|
180
193
|
same_pair[X0] = {X0 first, X0 second}; -- @...
|
|
181
194
|
```
|
|
182
195
|
|
|
196
|
+
Listing defined pattern aliases with `:pats` shows how each pattern's
|
|
197
|
+
parameters map into the canonical template's slots, using `(...)` for
|
|
198
|
+
any unexposed pattern holes:
|
|
199
|
+
|
|
200
|
+
```text
|
|
201
|
+
> a[]= {X x, Y y}; b[X] = {X x, Y y};
|
|
202
|
+
> c[X,Y] = {X x, Y y}; d[Y,X] = {X x, Y y};
|
|
203
|
+
> :pats
|
|
204
|
+
a[] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[(...),(...)]
|
|
205
|
+
b[X] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,(...)]
|
|
206
|
+
c[X,Y] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,Y]
|
|
207
|
+
d[Y,X] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,Y]
|
|
208
|
+
```
|
|
209
|
+
|
|
183
210
|
Rather than echoing the syntax used to construct the template, the
|
|
184
211
|
canonical definition decompiles the underlying minimal finite tree
|
|
185
212
|
automaton. Local parameter and knot names chosen by the author are
|
package/Examples/arithmetics.k
CHANGED
|
@@ -6,6 +6,22 @@ no = yes swap_yes_no;
|
|
|
6
6
|
bits[] = < {} _, B 0, B 1 > = B;
|
|
7
7
|
bits? = ? bits[];
|
|
8
8
|
|
|
9
|
+
bits_pair[] = { bits[] x, bits[] y };
|
|
10
|
+
inv_state[] = { bits[] x, bits[] r };
|
|
11
|
+
yes_no[] = < {} yes, {} no >;
|
|
12
|
+
in_order_state[] = { bits[] x, bits[] y, yes_no[] inOrder };
|
|
13
|
+
carry_bit[] = < {} 0, {} 1 >;
|
|
14
|
+
add_state[] = { bits[] x, bits[] y, carry_bit[] c, bits[] r };
|
|
15
|
+
times_state[] = { bits[] result, bits[] _x, bits[] y };
|
|
16
|
+
div_restoring_state[] = {
|
|
17
|
+
bits[] x, bits[] y, bits[] q, bits[] r
|
|
18
|
+
};
|
|
19
|
+
pad_state[] = { bits[] x, bits[] steps, bits[] _y };
|
|
20
|
+
align_down_state[] = { bits[] x, bits[] y, bits[] _y };
|
|
21
|
+
div_down_state[] = {
|
|
22
|
+
bits[] x, bits[] _y, bits[] steps, bits[] q
|
|
23
|
+
};
|
|
24
|
+
|
|
9
25
|
0b = {} | _;
|
|
10
26
|
-- |1 = | 1;
|
|
11
27
|
-- |0 = | 0;
|
|
@@ -29,7 +45,7 @@ empty = < /_ yes, no >;
|
|
|
29
45
|
empty? = {empty /yes if, () then} .then;
|
|
30
46
|
|
|
31
47
|
concat = {.x inv x, .y y} concat_;
|
|
32
|
-
concat_ = ?
|
|
48
|
+
concat_ = ? bits_pair[]
|
|
33
49
|
<
|
|
34
50
|
{.x empty? if, .y then} .then,
|
|
35
51
|
<
|
|
@@ -38,7 +54,7 @@ concat_ = ?{ bits[] = X x, ... }
|
|
|
38
54
|
> concat_
|
|
39
55
|
>;
|
|
40
56
|
inv = {()x, 0b r} _inv;
|
|
41
|
-
_inv = ?
|
|
57
|
+
_inv = ? inv_state[]
|
|
42
58
|
<
|
|
43
59
|
{.x empty? if, .r then} .then,
|
|
44
60
|
<
|
|
@@ -48,7 +64,7 @@ _inv = ?{ bits[] = X x, ... }
|
|
|
48
64
|
>;
|
|
49
65
|
|
|
50
66
|
inOrder = {.x inv x, .y inv y, yes inOrder} inOrder_;
|
|
51
|
-
inOrder_ = ?
|
|
67
|
+
inOrder_ = ? in_order_state[]
|
|
52
68
|
<
|
|
53
69
|
{.x </0,/1> if1, .y /_ if2, .inOrder swap_yes_no then} .then,
|
|
54
70
|
{.x /_ if, .inOrder then} .then,
|
|
@@ -67,6 +83,7 @@ EQ = {inOrder /yes if, {.x y, .y x} inOrder /yes and, () then} .then;
|
|
|
67
83
|
--------------------------------------
|
|
68
84
|
int[] = < bits[] "+", bits[] "-" >;
|
|
69
85
|
int? = ? int[];
|
|
86
|
+
int_pair[] = { int[] x, int[] y };
|
|
70
87
|
|
|
71
88
|
remove_leading_zeros = <
|
|
72
89
|
/0 remove_leading_zeros,
|
|
@@ -87,7 +104,7 @@ inc = <
|
|
|
87
104
|
/'-' {inv x, 0b y} _dec nat |'-'
|
|
88
105
|
>;
|
|
89
106
|
|
|
90
|
-
_inc = ?
|
|
107
|
+
_inc = ? bits_pair[] <
|
|
91
108
|
{.x empty? if, .y |1 then} .then,
|
|
92
109
|
{.x /0 x, .y |1 y} concat_,
|
|
93
110
|
{.x /1 x, .y |0 y} _inc
|
|
@@ -99,7 +116,7 @@ dec = <
|
|
|
99
116
|
/'-' {inv x, 0b y} _inc nat |'-'
|
|
100
117
|
>;
|
|
101
118
|
|
|
102
|
-
_dec = ?
|
|
119
|
+
_dec = ? bits_pair[] <
|
|
103
120
|
{.x /1 x, .y |0 y} concat_,
|
|
104
121
|
{.x /0 x, .y |1 y} _dec
|
|
105
122
|
>;
|
|
@@ -130,7 +147,7 @@ _plus = {.x x, .y y, .c c, 0b r} _plus_;
|
|
|
130
147
|
|
|
131
148
|
_plus_ =
|
|
132
149
|
-- least significant bit first addition with carry
|
|
133
|
-
?
|
|
150
|
+
? add_state[]
|
|
134
151
|
<
|
|
135
152
|
{.x empty? if, .y empty? and, .c/0 c, .r inv then} .then,
|
|
136
153
|
{.x empty? if, .y empty? and, {.r x, 1 r} _inv then} .then,
|
|
@@ -158,7 +175,7 @@ _minus = {.x x, .y y, .c c, 0b r} _minus_;
|
|
|
158
175
|
|
|
159
176
|
_minus_ =
|
|
160
177
|
-- least significant bit first substraction with carry
|
|
161
|
-
?
|
|
178
|
+
? add_state[]
|
|
162
179
|
<
|
|
163
180
|
{.y empty? if, .c/0 and, {.r x, .x r} _inv then} .then,
|
|
164
181
|
<
|
|
@@ -201,7 +218,7 @@ times =
|
|
|
201
218
|
;
|
|
202
219
|
|
|
203
220
|
_times =
|
|
204
|
-
?
|
|
221
|
+
? times_state[]
|
|
205
222
|
<
|
|
206
223
|
{ .y empty? if, .result then } .then,
|
|
207
224
|
<
|
|
@@ -228,7 +245,7 @@ shift_in_1 = inv |1 inv nat;
|
|
|
228
245
|
sub_bits = {.x inv x, .y inv y, c0 c} _minus inv nat;
|
|
229
246
|
|
|
230
247
|
_div_restoring =
|
|
231
|
-
?
|
|
248
|
+
? div_restoring_state[]
|
|
232
249
|
<
|
|
233
250
|
{.x empty? if, {.q inv int div, .r rem} then} .then,
|
|
234
251
|
<
|
|
@@ -252,7 +269,7 @@ _div_restoring =
|
|
|
252
269
|
_div_restoring
|
|
253
270
|
>;
|
|
254
271
|
|
|
255
|
-
_pad_down = ?
|
|
272
|
+
_pad_down = ? pad_state[]
|
|
256
273
|
<
|
|
257
274
|
{.x empty? if, {._y _y, .steps steps} then} .then,
|
|
258
275
|
{
|
|
@@ -262,7 +279,7 @@ _pad_down = ?{ bits[] = X x, bits[] = S steps, ... }
|
|
|
262
279
|
} _pad_down
|
|
263
280
|
>;
|
|
264
281
|
|
|
265
|
-
_align_down = ?
|
|
282
|
+
_align_down = ? align_down_state[]
|
|
266
283
|
<
|
|
267
284
|
{
|
|
268
285
|
.x </0, /1> x,
|
|
@@ -282,7 +299,7 @@ _align_down = ?{ bits[] = X x, bits[] = Y y, ... }
|
|
|
282
299
|
} .then
|
|
283
300
|
>;
|
|
284
301
|
|
|
285
|
-
_div_down = ?
|
|
302
|
+
_div_down = ? div_down_state[]
|
|
286
303
|
{
|
|
287
304
|
.x x,
|
|
288
305
|
._y _y,
|
|
@@ -326,7 +343,7 @@ _div_down = ?{ bits[] = X steps, ... }
|
|
|
326
343
|
} _div_down
|
|
327
344
|
>;
|
|
328
345
|
|
|
329
|
-
_check_2y = ?
|
|
346
|
+
_check_2y = ? bits_pair[]
|
|
330
347
|
<
|
|
331
348
|
{.x empty? if, {} | down_shift then} .then,
|
|
332
349
|
{.y empty? if, {} | restore then} .then,
|
|
@@ -398,7 +415,7 @@ gcd =
|
|
|
398
415
|
> _gcd
|
|
399
416
|
;
|
|
400
417
|
|
|
401
|
-
_gcd = ?
|
|
418
|
+
_gcd = ? bits_pair[] <
|
|
402
419
|
{ .y remove_leading_zeros empty? if, .x nat then } .then,
|
|
403
420
|
{ .y x, mod y } _gcd
|
|
404
421
|
>;
|