@fraczak/k 8.2.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 +36 -0
- package/DOCS/CANONICAL_PATTERNS.md +252 -0
- 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 +350 -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 +363 -0
- package/tests/test-repl.mjs +150 -10
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,42 @@ 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
|
+
|
|
8
44
|
## [8.2.0] — 2026-10-21 — Canonical Pattern Decompilation & Parameters
|
|
9
45
|
|
|
10
46
|
### Interactive REPL & Tooling
|
|
@@ -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.
|
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
|
>;
|
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
|
-
?
|
|
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
|
-
?
|
|
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
|
-
?
|
|
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
|
-
?
|
|
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
|
-
?
|
|
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
|
-
?
|
|
177
|
+
? bnat_pair[]
|
|
171
178
|
<
|
|
172
179
|
{.x/_ if1, .y/_ if2 } eq,
|
|
173
180
|
{.x/0 x, .y/0 y} _compare,
|
package/Examples/buda.k
CHANGED
|
@@ -7,12 +7,17 @@ int[] = < bits[] "+", bits[] "-" >;
|
|
|
7
7
|
list[X] = < {} nil, { X car, Y cdr } cons > = Y;
|
|
8
8
|
list? = ? list[X];
|
|
9
9
|
|
|
10
|
+
nths_args[X] = { int[] n, list[X] list };
|
|
11
|
+
reverse_state[X] = { list[X] reversed, list[X] list };
|
|
12
|
+
split_state[X] = { list[X] odd, list[X] even, list[X] list };
|
|
13
|
+
split_result[X] = { list[X] odd, list[X] even };
|
|
14
|
+
|
|
10
15
|
nil = {} | nil list?;
|
|
11
16
|
cons = { .car car, .cdr cdr } | cons list?;
|
|
12
17
|
car = list? /cons .car;
|
|
13
18
|
cdr = list? /cons .cdr;
|
|
14
19
|
|
|
15
|
-
nths = ?
|
|
20
|
+
nths = ? nths_args[X]
|
|
16
21
|
<
|
|
17
22
|
{ .n pos? dec n, .list cdr list } nths,
|
|
18
23
|
{ .n zero_int? if, .list car then } .then
|
|
@@ -21,7 +26,7 @@ nths = ?{ int[] n, list[X] list }
|
|
|
21
26
|
reverse = ? list[X]
|
|
22
27
|
{ nil reversed, () list } _reverse;
|
|
23
28
|
|
|
24
|
-
_reverse = ?
|
|
29
|
+
_reverse = ? reverse_state[X]
|
|
25
30
|
<
|
|
26
31
|
{
|
|
27
32
|
{ .list car car, .reversed cdr } cons reversed,
|
|
@@ -35,7 +40,7 @@ split = ? list[X]
|
|
|
35
40
|
{ nil odd, nil even, reverse list } _split
|
|
36
41
|
;
|
|
37
42
|
|
|
38
|
-
_split = ?
|
|
43
|
+
_split = ? split_state[X]
|
|
39
44
|
<
|
|
40
45
|
{
|
|
41
46
|
{ .list car car, .odd cdr } cons odd,
|
|
@@ -47,7 +52,7 @@ _split = ?{ list[X] odd, list[X] even, list[X] list }
|
|
|
47
52
|
>
|
|
48
53
|
;
|
|
49
54
|
|
|
50
|
-
split_ = ?
|
|
55
|
+
split_ = ? split_state[X]
|
|
51
56
|
<
|
|
52
57
|
{
|
|
53
58
|
.odd odd,
|
package/Examples/byte.k
CHANGED
|
@@ -1,21 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
1 = {}|1;
|
|
1
|
+
-- Examples/byte.k: Byte and 7-bit ASCII representation
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
0 = {} | 0;
|
|
4
|
+
1 = {} | 1;
|
|
5
|
+
|
|
6
|
+
bit[] = < {} 0, {} 1 >;
|
|
7
|
+
|
|
8
|
+
byte[] = {
|
|
9
|
+
bit[] 0, bit[] 1, bit[] 2, bit[] 3,
|
|
10
|
+
bit[] 4, bit[] 5, bit[] 6, bit[] 7
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
ascii[] = {
|
|
14
|
+
bit[] 0, bit[] 1, bit[] 2, bit[] 3,
|
|
15
|
+
bit[] 4, bit[] 5, bit[] 6
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
_ = { 1 6, 0 5, 1 4, 1 3, 1 2, 1 1, 1 0 };
|
|
5
19
|
map = <
|
|
6
20
|
/ascii <
|
|
7
|
-
{.6/1 6, .5/1 5, .4/1 4, .3/1 3, .2/1 2, .1/1 1, .0/1 0 } _,
|
|
8
|
-
{{.6/0 6, .5/1 5} if, () then} .then,
|
|
9
|
-
{.6/1 if, () then} .then
|
|
21
|
+
{ .6/1 6, .5/1 5, .4/1 4, .3/1 3, .2/1 2, .1/1 1, .0/1 0 } _,
|
|
22
|
+
{ { .6/0 6, .5/1 5 } if, () then } .then,
|
|
23
|
+
{ .6/1 if, () then } .then
|
|
10
24
|
>,
|
|
11
25
|
_
|
|
12
26
|
> | ascii;
|
|
13
27
|
|
|
14
28
|
list[X] = < {} nil, { X car, Y cdr } cons > = Y;
|
|
15
29
|
|
|
16
|
-
filter = ? list[
|
|
17
|
-
/cons {.car map car, .cdr filter cdr} | cons,
|
|
30
|
+
filter = ? list[X] <
|
|
31
|
+
/cons { .car map car, .cdr filter cdr } | cons,
|
|
18
32
|
()
|
|
19
33
|
>;
|
|
20
34
|
|
|
21
|
-
filter
|
|
35
|
+
filter
|