@fraczak/k 8.1.0 → 8.3.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 CHANGED
@@ -5,6 +5,69 @@ 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.3.0] — 2026-10-22 — Content-Addressable Patterns & Aliasing
9
+
10
+ ### Content-Addressable Canonical Patterns
11
+
12
+ - **Canonical Pattern Derivation & Minimal Automata**:
13
+ - All pattern definitions compile to minimal regular tree automata
14
+ identified by deterministic content-addressed hashes (`@hash`).
15
+ - Canonical patterns take the full arity of all discovered variable
16
+ nodes (e.g. `@hash[X0, X1] = {X0 x, X1 y};`).
17
+ - Open wildcards `(...)` lower to open type holes equivalent to
18
+ unbound variables, sharing canonical templates across definitions.
19
+ - **Named Patterns as Parameter Routing Aliases**:
20
+ - Named patterns (`p[X] = {X x, Y y};`) are lexical aliases routing
21
+ caller arguments to canonical parameter slots, padding unexposed
22
+ internal ports with wildcards: `p[X] = @hash[X, (...)];`.
23
+ - Isomorphic pattern permutations (`a[]`, `b[X]`, `c[X,Y]`, `d[Y,X]`)
24
+ share the identical canonical hash, enabling deduplication.
25
+ - Multi-level nested template applications and parameter reorderings
26
+ resolve and decompile accurately.
27
+ - **Unification Preservation for Wildcards**:
28
+ - Repeated internal existential variables in canonical templates
29
+ preserve structural type-equality constraints when instantiated
30
+ with wildcard arguments (`(...)`), preventing type pollution.
31
+ - **Knot Safety**:
32
+ - The grammar strictly rejects parameter names that conflict with
33
+ recursive knot variables (`= Knot`), preserving cycle semantics
34
+ ($\mu$-fixpoints) and preventing recursion destruction.
35
+ - **REPL & Persistence**:
36
+ - `:pats` lists all defined aliases and their canonical port routing.
37
+ - `:pat <name>` decompiles and displays canonical definitions.
38
+ - Saving to `.klib` preserves parameter and alias routing metadata
39
+ origins without duplication.
40
+ - **Documentation**:
41
+ - Authored `DOCS/CANONICAL_PATTERNS.md` and updated `README.md` and
42
+ `DOCS/REPL.md`.
43
+
44
+ ## [8.2.0] — 2026-10-21 — Canonical Pattern Decompilation & Parameters
45
+
46
+ ### Interactive REPL & Tooling
47
+
48
+ - **Canonical Pattern Decompilation (`:pattern`, `:pat`)**:
49
+ - `:pattern <name>` (or `:pat <name>`) prints the canonical pattern
50
+ definition decompiled from its underlying minimal automaton,
51
+ rather than echoing user-defined construction syntax.
52
+ - Automatically erases author-chosen parameter and knot names,
53
+ introducing canonical variables (`X0`, `X1`, `X2`, ...) in
54
+ deterministic traversal (discovery) order.
55
+ - Open type holes (`(...)`) are recorded as introduced parameters
56
+ in the pattern header in discovery order:
57
+ * `pair[X0, X1] = {X0 first, X1 second}; -- @hash`
58
+ * `list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- @hash`
59
+ * `same_pair[X0] = {X0 first, X0 second}; -- @hash`
60
+ * `dict[X1, X2] = <{{X1 first, X2 second} car, X0 cdr} cons,`
61
+ `{} nil>=X0; -- @hash`
62
+ - Added `patternGraphToCanonical` to `pretty.mjs` for decompiling
63
+ pattern graphs with introduced canonical variables.
64
+ - Supports direct pattern lookup by content-addressed hash
65
+ (`:pattern @hash`, `:pat @hash`).
66
+ - Displays canonical content-addressed digest comments (`-- @hash`).
67
+ - Added comprehensive unit tests in `tests/test-repl.mjs`.
68
+ - Updated documentation across `DOCS/REPL.md`,
69
+ `DOCS/NAMED_PATTERNS.md`, and `DOCS/PATTERNS.md`.
70
+
8
71
  ## [8.1.0] — 2026-10-20 — Dynamic JSON Bit-Trie & json? Standard Library
9
72
 
10
73
  ### Dynamic JSON (djson) Consolidation
@@ -0,0 +1,252 @@
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
+
19
+ ---
20
+
21
+ ## 2. Canonical Pattern Derivation
22
+
23
+ When a pattern is compiled:
24
+ ```k
25
+ p[X] = {X x, Y y, Y z};
26
+ ```
27
+ the compiler evaluates the right-hand filter into a minimal directed
28
+ graph (regular tree automaton).
29
+
30
+ ### 2.1 Full Variable Node Discovery
31
+
32
+ In the graph representation:
33
+ - `X` is an open port referenced at field `x`.
34
+ - `Y` is an existential variable shared between fields `y` and `z`,
35
+ constraining them to identical subtrees.
36
+
37
+ The canonicalizer walks the minimal automaton in deterministic
38
+ breadth-first traversal order, assigning canonical variable names
39
+ `X0`, `X1`, `X2`, ... to each open variable node discovered in the
40
+ graph.
41
+
42
+ Crucially, **all** variable nodes present in the filter graph are
43
+ retained in the canonical pattern definition. In the example above:
44
+ - `X` becomes canonical port `X0`.
45
+ - `Y` becomes canonical port `X1`.
46
+
47
+ The resulting minimal canonical pattern graph produces a deterministic
48
+ content hash (e.g. `@A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P`).
49
+ The canonical pattern definition is therefore:
50
+
51
+ ```k
52
+ @A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P[X0, X1] = {
53
+ X0 x, X1 y, X1 z
54
+ };
55
+ ```
56
+
57
+ Canonical patterns always take the full arity of all variable nodes
58
+ discovered in the graph.
59
+
60
+ ---
61
+
62
+ ## 3. Named Patterns as Parameter Routing Aliases
63
+
64
+ When an author defines an alias like:
65
+ ```k
66
+ p[X] = {X x, Y y, Y z};
67
+ ```
68
+ the user only exposed `X` in the parameter list `[X]`, leaving `Y`
69
+ unexposed.
70
+
71
+ The definition `p[X]` does **not** create a separate pattern graph.
72
+ Instead, it defines an alias that instantiates the underlying canonical
73
+ pattern `@A6vr...`, routing the author's parameter `X` to slot `X0`, and
74
+ plugging unexposed slots (here `X1`) with the open wildcard `(...)`:
75
+
76
+ ```k
77
+ p[X] = @A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P[X, (...)];
78
+ ```
79
+
80
+ ### 3.1 Canonical Pattern Sharing Across Permutations
81
+
82
+ Multiple alias definitions with different parameter exposures or
83
+ orderings over isomorphic filter graphs share the exact same canonical
84
+ hash:
85
+
86
+ ```k
87
+ a[] = {X x, Y y};
88
+ b[X] = {X x, Y y};
89
+ c[X,Y] = {X x, Y y};
90
+ d[Y,X] = {X x, Y y};
91
+ ```
92
+
93
+ All four patterns compile to the identical canonical graph:
94
+ `@NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X0, X1] = {X0 x, X1 y};`
95
+
96
+ Inspecting the aliases with `:pats` shows how each alias projects its
97
+ arguments into the shared canonical template:
98
+
99
+ ```text
100
+ > :pats
101
+ a[] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[(...),(...)]
102
+ b[X] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,(...)]
103
+ c[X,Y] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,Y]
104
+ d[Y,X] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,Y]
105
+ ```
106
+
107
+ Notice:
108
+ - `a[]` exposes no parameters, so both canonical ports are `(...)`.
109
+ - `b[X]` exposes only `X` (slot `X0`), so slot `X1` is filled with
110
+ `(...)`.
111
+ - `c[X,Y]` routes caller arguments `[X, Y]` to canonical slots `[X0, X1]`.
112
+ - `d[Y,X]` routes caller arguments `[Y, X]` to canonical slots `[X0, X1]`.
113
+
114
+ ---
115
+
116
+ ## 4. Direct Usability of Canonical Patterns
117
+
118
+ Because the alias is just an abbreviation, canonical patterns can be used
119
+ everywhere relations and aliases can be used:
120
+
121
+ 1. **Direct Inspection**:
122
+ ```text
123
+ > :pat @A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P
124
+ @A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P[X0, X1] = {
125
+ X0 x, X1 y, X1 z
126
+ };
127
+ ```
128
+
129
+ 2. **Direct Matching in Expressions**:
130
+ Callers can instantiate canonical patterns directly by passing all
131
+ canonical arguments:
132
+ ```k
133
+ { {} x, {} y, {} z } ? @A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P[
134
+ {}, {}
135
+ ];
136
+ ```
137
+
138
+ 3. **Explicit Definition by Hash**:
139
+ Canonical pattern definitions are accepted directly by the grammar:
140
+ ```k
141
+ @A6vrY7TP9s34tvNgVZsk7N5m4Cr4ZJERYPBevgmqnK7P[X, Y] = {
142
+ X x, Y y, Y z
143
+ };
144
+ ```
145
+
146
+ ---
147
+
148
+ ## 5. Unrestricted Patterns `(...)` as Anonymous Canonical Ports
149
+
150
+ An unrestricted pattern `(...)` represents an open wildcard hole with no
151
+ type constraints. In the type pattern graph, an unrestricted pattern
152
+ `(...)` and an unbound variable `X` lower to the exact same node type:
153
+ `{ pattern: '(...)' }`.
154
+
155
+ The canonicalizer treats every unrestricted node in the graph as an open
156
+ variable port. Consequently:
157
+ - Writing `{(...) x, (...) y, (...) z}` produces the **exact same
158
+ canonical pattern** as `{X x, Y y, Z z}`:
159
+ ```k
160
+ @PScsPA3xYv4kSXUqHafwVQr5Cxkmspf3bj46KAcmyxmR[X0, X1, X2] = {
161
+ X0 x, X1 y, X2 z
162
+ };
163
+ ```
164
+ - An alias definition like:
165
+ ```k
166
+ p[X] = {X x, (...) y};
167
+ ```
168
+ derives the generic canonical pattern for `{X x, Y y}`:
169
+ ```k
170
+ @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X0, X1] = {
171
+ X0 x, X1 y
172
+ };
173
+ ```
174
+ and defines the alias routing `X` to port `X0` and wildcard `(...)`
175
+ to port `X1`:
176
+ ```k
177
+ p[X] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X, (...)];
178
+ ```
179
+
180
+ This guarantees maximal canonical sharing: whether a hole is written as
181
+ a named variable or an inline `(...)`, both share the identical underlying
182
+ canonical template.
183
+
184
+ ---
185
+
186
+ ## 6. Why Knots (and Root Knots) Cannot Be Exposed as Parameters
187
+
188
+ Consider the recursive linked list pattern:
189
+ ```k
190
+ list[X] = < {} nil, { X car, Y cdr } cons > = Y;
191
+ ```
192
+
193
+ In this definition:
194
+ - `= Y` binds the variable `Y` to the root union node of the pattern
195
+ graph.
196
+ - Inside the second variant `{ X car, Y cdr } cons`, the field `cdr`
197
+ references `Y`.
198
+ - This creates a directed back-edge from `cdr` to the root union, forming
199
+ a recursive cycle ($\mu$-fixpoint).
200
+
201
+ ### 6.1 The Fundamental Distinction: Open Ports vs. Bound Knots
202
+
203
+ There is a fundamental semantic contradiction between **parameters** and
204
+ **knots**:
205
+
206
+ | Feature | Parameter (`[P]`) | Knot (`= KnotName`) |
207
+ | :--- | :--- | :--- |
208
+ | **Quantification** | Universal ($\forall$) | Fixed-point ($\mu$) |
209
+ | **Binding State** | **Unbound / Open Port** | **Bound / Node** |
210
+ | **Role** | Hole to fill by caller | Internal back-edge cycle |
211
+ | **Scope** | Caller interface | Purely internal topology |
212
+
213
+ - An **external parameter** is an open socket waiting for caller input.
214
+ - A **cycle knot** is an internal wire already soldered to a concrete
215
+ node in the graph.
216
+
217
+ ### 6.2 The Shadowing Hazard: Destruction of Recursion
218
+
219
+ What happens if an author attempts to expose the root knot `Y` as a
220
+ parameter?
221
+ ```k
222
+ list[X, Y] = < {} nil, { X car, Y cdr } cons > = Y; -- INVALID!
223
+ ```
224
+
225
+ If this definition were permitted:
226
+ 1. `Y` would appear in the formal parameter list `[X, Y]`.
227
+ 2. When the template is instantiated (e.g. `list[int[], MyTail]`), the
228
+ argument `MyTail` would substitute every occurrence of `Y` in the
229
+ body.
230
+ 3. Consequently, `{ X car, Y cdr }` would become
231
+ `{ int[] car, MyTail cdr }`.
232
+ 4. The back-edge to the root union would be severed!
233
+ 5. The recursive cycle would be destroyed, silently corrupting the
234
+ recursive list into a non-recursive pair
235
+ `<{} nil, {int[] car, MyTail cdr} cons>`.
236
+
237
+ ### 6.3 Enforced Invariant: Conflict Rejection at Parse Time
238
+
239
+ Because cycle knots represent bound internal topology, exposing a knot
240
+ name in `pattern_params` is a direct contradiction.
241
+
242
+ The compiler strictly enforces that no formal parameter name may
243
+ conflict with any knot identifier in the filter:
244
+
245
+ ```k
246
+ list[X, Y] = < {} nil, { X car, Y cdr } cons > = Y;
247
+ -- Parse Error: Pattern parameter 'Y' in pattern 'list'
248
+ -- conflicts with knot 'Y'. Knot variables cannot be pattern parameters.
249
+ ```
250
+
251
+ The root node of a pattern graph is matched against incoming data by the
252
+ runtime; it is never an open parameter port.
@@ -208,6 +208,29 @@ To guarantee decidability and termination:
208
208
  that yield isomorphic regular tree automata produce identical
209
209
  canonical hashes and wire encoders.
210
210
 
211
+ 4. **Canonical Form and REPL Decompilation**:
212
+ In the interactive REPL, inspecting a pattern via `:pattern <name>`
213
+ (or `:pat <name>`) displays its **canonical definition** rather than
214
+ the source syntax used to define it:
215
+ - User-defined parameter names (`A`, `B`, `X`) and knot names (`Y`,
216
+ `T`) are erased.
217
+ - The canonical decompiler introduces fresh variables (`X0`, `X1`,
218
+ `X2`, ...) in deterministic traversal (discovery) order.
219
+ - Open type ports are introduced in the pattern header (`name[...]`)
220
+ in order of discovery:
221
+ ```k
222
+ -- pair[A, B] = { A first, B second };
223
+ pair[X0, X1] = {X0 first, X1 second}; -- @...
224
+
225
+ -- list[X] = < {} nil, { X car, Y cdr } cons > = Y;
226
+ list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- @...
227
+
228
+ -- same_pair[T] = { T first, T second };
229
+ same_pair[X0] = {X0 first, X0 second}; -- @...
230
+ ```
231
+ - Two syntactically different definitions yielding isomorphic
232
+ automata produce the exact same canonical definition and hash.
233
+
211
234
  ---
212
235
 
213
236
  ## 7. Concrete Library Examples
package/DOCS/PATTERNS.md CHANGED
@@ -85,6 +85,11 @@ Direct or mutual recursion among templates (`bits[]` calling `bits[]`)
85
85
  is disallowed to ensure every template dependency graph is an acyclic
86
86
  DAG, guaranteeing finite regular tree automaton compilation.
87
87
 
88
+ In the interactive REPL, inspecting a pattern with `:pattern <name>` (or
89
+ `:pat <name>`) displays its canonical decompiled form with newly
90
+ introduced variables in discovery order (e.g.
91
+ `list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- @...`).
92
+
88
93
  ## Filters and Terminal Positions
89
94
 
90
95
  A k script can describe a pattern through:
package/DOCS/REPL.md CHANGED
@@ -37,8 +37,8 @@ is complete.
37
37
  | `:wasm` / `:pkir` / `:js` | Shortcut to switch engine |
38
38
  | `:rel name` | Show relation definition |
39
39
  | `:rels` | List relation aliases |
40
- | `:pattern name` | Show named pattern definition (alias: `:pat`) |
41
- | `:patterns` | List defined named pattern templates (alias: `:pats`) |
40
+ | `:pattern name` | Show canonical definition of pattern (alias: `:pat`) |
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 |
@@ -155,6 +155,74 @@ Reads the plain-JSON library file and merges its codes, relations,
155
155
  aliases, and metadata into the session. Aliases are recovered from
156
156
  `meta[hash].origins[]` unless `--no-alias` is used.
157
157
 
158
+ ## Pattern Inspection
159
+
160
+ ### `:patterns` (or `:pats`)
161
+
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
+ ```
171
+
172
+ ### `:pattern <name>` (or `:pat <name>`, `:pat @hash`)
173
+
174
+ Displays the **canonical definition** of a named pattern template:
175
+
176
+ ```text
177
+ > pair[A, B] = { A first, B second };
178
+ > :pat pair
179
+ pair[X0, X1] = {X0 first, X1 second}; -- @...
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
+
187
+ > list[T] = < {} nil, { T car, Y cdr } cons > = Y;
188
+ > :pat list
189
+ list[X1] = <{X1 car, X0 cdr} cons, {} nil>=X0; -- @...
190
+
191
+ > same_pair[T] = { T first, T second };
192
+ > :pat same_pair
193
+ same_pair[X0] = {X0 first, X0 second}; -- @...
194
+ ```
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
+
210
+ Rather than echoing the syntax used to construct the template, the
211
+ canonical definition decompiles the underlying minimal finite tree
212
+ automaton. Local parameter and knot names chosen by the author are
213
+ erased; instead, the canonical definition introduces its own variables
214
+ (`X0`, `X1`, `X2`, ...) in deterministic traversal (discovery) order:
215
+
216
+ - **External Ports**: Open type holes (`(...)`) are recorded as
217
+ canonical parameters in the template header (`[X0, X1]`, `[X1]`, etc.)
218
+ in discovery order.
219
+ - **Recursive Knots**: Cycles back to existing nodes are bound using
220
+ knot variables (`=X0`).
221
+ - **Content Hash**: Every canonical definition is annotated with its
222
+ deterministic content-addressed hash (`-- @hash`).
223
+
224
+ Lookup by canonical hash is also supported: `:pattern @hash`.
225
+
158
226
  ## Codecs and Numerical Values
159
227
 
160
228
  In `k`, there are no primitive numbers or built-in numerical literals.
@@ -334,6 +402,9 @@ instantly without exhausting stack or terminal buffers. Use
334
402
  {}|0 ?<{} 0, ...>
335
403
  > :rel inc
336
404
  inc = | +1; -- @...
405
+ > pair[A, B] = { A first, B second };
406
+ > :pat pair
407
+ pair[X0, X1] = {X0 first, X1 second}; -- @...
337
408
  > :klib nat.klib
338
409
  saved nat.klib
339
410
  > :ko inc.ko inc
@@ -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_ = ?{ bits[] = X x, ... }
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 = ?{ bits[] = X x, ... }
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_ = ?{ bits[] = X x, ... }
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 = ?{ bits[] = X x, ... } <
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 = ?{ bits[] = X x, ... } <
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
- ?{ bits[] = X x, ... }
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
- ?{ bits[] = X x, ... }
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
- ?{ bits[] = Y y, ... }
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
- ?{ bits[] = X x, ... }
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 = ?{ bits[] = X x, bits[] = S steps, ... }
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 = ?{ bits[] = X x, bits[] = Y y, ... }
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 = ?{ bits[] = X steps, ... }
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 = ?{ bits[] = X x, bits[] = Y y, ... }
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 = ?{ bits[] = X x, ... } <
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
  >;
package/Examples/bnat.k CHANGED
@@ -3,6 +3,13 @@
3
3
  bnat[] = < {} _, B 0, B 1 > = B;
4
4
  bnat? = ? bnat[];
5
5
 
6
+ bnat_pair[] = { bnat[] x, bnat[] y };
7
+ reverse_state[] = { bnat[] x, bnat[] result };
8
+ bnat_carry_state[] = { bnat[] x, bnat[] y, < {} 0, {} 1 > c };
9
+ times_state[] = { bnat[] result, bnat[] _x, bnat[] y };
10
+ align_state[] = { bnat[] _x, bnat[] _y, bnat[] x, bnat[] y };
11
+ cmp[] = < {} lt, {} eq, {} gt >;
12
+
6
13
  _ = {}|_;
7
14
  _1 = |1;
8
15
  _0 = |0;
@@ -24,7 +31,7 @@ remove_leading_zeros = < /0 remove_leading_zeros, () >;
24
31
  reverse = {() x, _ result} _reverse;
25
32
 
26
33
  _reverse =
27
- ?{ bnat[] = X x, ... }
34
+ ? reverse_state[]
28
35
  <
29
36
  {.x/_ if, .result then} .then,
30
37
  {.x/0 x, .result _0 result} _reverse,
@@ -53,7 +60,7 @@ plus =
53
60
 
54
61
  _plus =
55
62
  -- least significant bit first addition with carry
56
- ?{ bnat[] = X x, ... }
63
+ ? bnat_carry_state[]
57
64
  <
58
65
  {.x/_ if1, .y/_ if2, <.c/0 bnat_0, bnat_1> then} .then,
59
66
  {.x/_ if1, .c/0 if2, .y then} .then,
@@ -89,7 +96,7 @@ minus =
89
96
 
90
97
  _minus =
91
98
  -- least significat bit first substraction with carry
92
- ?{ bnat[] = X x, bnat[] = Y y, ... }
99
+ ? bnat_carry_state[]
93
100
  <
94
101
  {.x x, .y/_ y, .c/0 if} .x,
95
102
  {.x/1 x, .y/_ y, .c/1 if} .x _0,
@@ -117,7 +124,7 @@ times =
117
124
  ;
118
125
 
119
126
  _times =
120
- ?{ bnat[] = Y y, ... }
127
+ ? times_state[]
121
128
  <
122
129
  { .y/_ if, .result then } .then,
123
130
  { .result _0 result, ._x _x, .y/0 y } _times,
@@ -150,7 +157,7 @@ align =
150
157
  ;
151
158
 
152
159
  _align =
153
- ?{ bnat[] = X _x, bnat[] = Y _y, ... }
160
+ ? align_state[]
154
161
  <
155
162
  {._x/_ if1, ._y/_ if2, {.x x, .y y} then} .then,
156
163
  {._x </0, /_ _> _x, ._y </0, /_ _> _y, .x _0 x, .y _0 y} _align,
@@ -167,7 +174,7 @@ gt = {} |gt;
167
174
 
168
175
  compare = align _compare;
169
176
  _compare =
170
- ?{ bnat[] = X x, bnat[] = Y y, ... }
177
+ ? bnat_pair[]
171
178
  <
172
179
  {.x/_ if1, .y/_ if2 } eq,
173
180
  {.x/0 x, .y/0 y} _compare,