@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/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
|
package/Examples/ieee.k
CHANGED
|
@@ -1,52 +1,97 @@
|
|
|
1
1
|
// Primitive bit domains used throughout floating-point encodings.
|
|
2
|
-
// IEEE-754 double-precision building blocks: bit-level domains,
|
|
3
|
-
// and ripple-carry arithmetic helpers reused across
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
// IEEE-754 double-precision building blocks: bit-level domains,
|
|
3
|
+
// flag enums, and ripple-carry arithmetic helpers reused across
|
|
4
|
+
// add/sub/mul/div.
|
|
5
|
+
bit[] = < {} 0, {} 1 >;
|
|
6
|
+
sign[] = < {} "+", {} "-" >;
|
|
6
7
|
|
|
7
8
|
// Small utility products for pairing bits or signs.
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
bit_pair[] = { bit[] x, bit[] y };
|
|
10
|
+
sign_pair[] = { sign[] x, sign[] y };
|
|
10
11
|
|
|
11
12
|
// Comparator states and result lattice (accounts for NaN unordered case).
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
cmp_state[] = < {} eq, {} gt, {} lt >;
|
|
14
|
+
cmp[] = < {} eq_exact, {} eq_zero, {} gt, {} lt, {} unordered_nan >;
|
|
14
15
|
|
|
15
16
|
// IEEE-754 double fields: 11-bit exponent and 52-bit fraction.
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
exp11[] = {
|
|
18
|
+
bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4,
|
|
19
|
+
bit[] 5, bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10
|
|
20
|
+
};
|
|
21
|
+
frac52[] = {
|
|
22
|
+
bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5,
|
|
23
|
+
bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10, bit[] 11,
|
|
24
|
+
bit[] 12, bit[] 13, bit[] 14, bit[] 15, bit[] 16, bit[] 17,
|
|
25
|
+
bit[] 18, bit[] 19, bit[] 20, bit[] 21, bit[] 22, bit[] 23,
|
|
26
|
+
bit[] 24, bit[] 25, bit[] 26, bit[] 27, bit[] 28, bit[] 29,
|
|
27
|
+
bit[] 30, bit[] 31, bit[] 32, bit[] 33, bit[] 34, bit[] 35,
|
|
28
|
+
bit[] 36, bit[] 37, bit[] 38, bit[] 39, bit[] 40, bit[] 41,
|
|
29
|
+
bit[] 42, bit[] 43, bit[] 44, bit[] 45, bit[] 46, bit[] 47,
|
|
30
|
+
bit[] 48, bit[] 49, bit[] 50, bit[] 51
|
|
31
|
+
};
|
|
18
32
|
|
|
19
33
|
// Logical view of a double plus rounding/exception flag enums per op.
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
34
|
+
float64[] = { sign[] sign, exp11[] exponent, frac52[] fraction };
|
|
35
|
+
flags_add[] = <
|
|
36
|
+
{} none, {} invalid, {} overflow, {} underflow, {} inexact
|
|
37
|
+
>;
|
|
38
|
+
flags_sub[] = <
|
|
39
|
+
{} none, {} invalid, {} overflow, {} underflow, {} inexact
|
|
40
|
+
>;
|
|
41
|
+
flags_mul[] = <
|
|
42
|
+
{} none, {} invalid, {} overflow, {} underflow, {} inexact
|
|
43
|
+
>;
|
|
44
|
+
flags_div[] = <
|
|
45
|
+
{} none, {} invalid, {} div_by_zero,
|
|
46
|
+
{} overflow, {} underflow, {} inexact
|
|
47
|
+
>;
|
|
25
48
|
|
|
26
49
|
// Bundles pairing an arithmetic result with its raised flags.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
50
|
+
add_result[] = { float64[] result, flags_add[] flags };
|
|
51
|
+
sub_result[] = { float64[] result, flags_sub[] flags };
|
|
52
|
+
mul_result[] = { float64[] result, flags_mul[] flags };
|
|
53
|
+
div_result[] = { float64[] result, flags_div[] flags };
|
|
31
54
|
|
|
32
55
|
// Common products for operand pairs and extended significands.
|
|
33
|
-
|
|
34
|
-
|
|
56
|
+
float_pair[] = { float64[] x, float64[] y };
|
|
57
|
+
sig53[] = {
|
|
58
|
+
bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5,
|
|
59
|
+
bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10, bit[] 11,
|
|
60
|
+
bit[] 12, bit[] 13, bit[] 14, bit[] 15, bit[] 16, bit[] 17,
|
|
61
|
+
bit[] 18, bit[] 19, bit[] 20, bit[] 21, bit[] 22, bit[] 23,
|
|
62
|
+
bit[] 24, bit[] 25, bit[] 26, bit[] 27, bit[] 28, bit[] 29,
|
|
63
|
+
bit[] 30, bit[] 31, bit[] 32, bit[] 33, bit[] 34, bit[] 35,
|
|
64
|
+
bit[] 36, bit[] 37, bit[] 38, bit[] 39, bit[] 40, bit[] 41,
|
|
65
|
+
bit[] 42, bit[] 43, bit[] 44, bit[] 45, bit[] 46, bit[] 47,
|
|
66
|
+
bit[] 48, bit[] 49, bit[] 50, bit[] 51, bit[] 52
|
|
67
|
+
};
|
|
68
|
+
sig56[] = {
|
|
69
|
+
bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5,
|
|
70
|
+
bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10, bit[] 11,
|
|
71
|
+
bit[] 12, bit[] 13, bit[] 14, bit[] 15, bit[] 16, bit[] 17,
|
|
72
|
+
bit[] 18, bit[] 19, bit[] 20, bit[] 21, bit[] 22, bit[] 23,
|
|
73
|
+
bit[] 24, bit[] 25, bit[] 26, bit[] 27, bit[] 28, bit[] 29,
|
|
74
|
+
bit[] 30, bit[] 31, bit[] 32, bit[] 33, bit[] 34, bit[] 35,
|
|
75
|
+
bit[] 36, bit[] 37, bit[] 38, bit[] 39, bit[] 40, bit[] 41,
|
|
76
|
+
bit[] 42, bit[] 43, bit[] 44, bit[] 45, bit[] 46, bit[] 47,
|
|
77
|
+
bit[] 48, bit[] 49, bit[] 50, bit[] 51, bit[] 52, bit[] 53,
|
|
78
|
+
bit[] 54, bit[] 55
|
|
79
|
+
};
|
|
35
80
|
|
|
36
81
|
// Helper shapes for ripple-carry adders and small shifts.
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
82
|
+
exp_sum[] = { exp11[] sum, bit[] carry };
|
|
83
|
+
sig56_sum[] = { sig56[] sum, bit[] carry };
|
|
84
|
+
adder_out[] = { bit[] sum, bit[] carry };
|
|
85
|
+
shift6[] = { bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5 };
|
|
41
86
|
|
|
42
87
|
// Pairings used during exponent/fraction comparisons.
|
|
43
|
-
|
|
44
|
-
|
|
88
|
+
exp_pair[] = { exp11[] x, exp11[] y };
|
|
89
|
+
frac_pair[] = { frac52[] x, frac52[] y };
|
|
45
90
|
|
|
46
91
|
// Comparator pipelines carry current state alongside bit pairs.
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
92
|
+
cmp_step_in[] = { bit_pair[] bits, cmp_state[] state };
|
|
93
|
+
cmp_exp_ctx[] = { exp_pair[] pair, cmp_state[] state };
|
|
94
|
+
cmp_frac_ctx[] = { frac_pair[] pair, cmp_state[] state };
|
|
50
95
|
|
|
51
96
|
// Canonical bit literals for convenience.
|
|
52
97
|
0 = {} |0 ;
|
|
@@ -287,8 +332,11 @@ sig56_not = {
|
|
|
287
332
|
|
|
288
333
|
// 56-bit ripple-carry add with optional incoming carry.
|
|
289
334
|
// 14-bit ripple-carry adder
|
|
290
|
-
|
|
291
|
-
|
|
335
|
+
sig14[] = {
|
|
336
|
+
bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5, bit[] 6,
|
|
337
|
+
bit[] 7, bit[] 8, bit[] 9, bit[] 10, bit[] 11, bit[] 12, bit[] 13
|
|
338
|
+
};
|
|
339
|
+
sig14_sum[] = { sig14[] sum, bit[] carry };
|
|
292
340
|
sig14_add_cin =
|
|
293
341
|
{
|
|
294
342
|
.x x,
|
|
@@ -535,9 +583,29 @@ mux_bit =
|
|
|
535
583
|
|
|
536
584
|
# --- sig112 helpers (mul/div) ------------------------------------------------
|
|
537
585
|
// Wide 112-bit slice used for intermediate mul/div significands.
|
|
538
|
-
|
|
586
|
+
sig112[] = {
|
|
587
|
+
bit[] 0, bit[] 1, bit[] 2, bit[] 3, bit[] 4, bit[] 5,
|
|
588
|
+
bit[] 6, bit[] 7, bit[] 8, bit[] 9, bit[] 10, bit[] 11,
|
|
589
|
+
bit[] 12, bit[] 13, bit[] 14, bit[] 15, bit[] 16, bit[] 17,
|
|
590
|
+
bit[] 18, bit[] 19, bit[] 20, bit[] 21, bit[] 22, bit[] 23,
|
|
591
|
+
bit[] 24, bit[] 25, bit[] 26, bit[] 27, bit[] 28, bit[] 29,
|
|
592
|
+
bit[] 30, bit[] 31, bit[] 32, bit[] 33, bit[] 34, bit[] 35,
|
|
593
|
+
bit[] 36, bit[] 37, bit[] 38, bit[] 39, bit[] 40, bit[] 41,
|
|
594
|
+
bit[] 42, bit[] 43, bit[] 44, bit[] 45, bit[] 46, bit[] 47,
|
|
595
|
+
bit[] 48, bit[] 49, bit[] 50, bit[] 51, bit[] 52, bit[] 53,
|
|
596
|
+
bit[] 54, bit[] 55, bit[] 56, bit[] 57, bit[] 58, bit[] 59,
|
|
597
|
+
bit[] 60, bit[] 61, bit[] 62, bit[] 63, bit[] 64, bit[] 65,
|
|
598
|
+
bit[] 66, bit[] 67, bit[] 68, bit[] 69, bit[] 70, bit[] 71,
|
|
599
|
+
bit[] 72, bit[] 73, bit[] 74, bit[] 75, bit[] 76, bit[] 77,
|
|
600
|
+
bit[] 78, bit[] 79, bit[] 80, bit[] 81, bit[] 82, bit[] 83,
|
|
601
|
+
bit[] 84, bit[] 85, bit[] 86, bit[] 87, bit[] 88, bit[] 89,
|
|
602
|
+
bit[] 90, bit[] 91, bit[] 92, bit[] 93, bit[] 94, bit[] 95,
|
|
603
|
+
bit[] 96, bit[] 97, bit[] 98, bit[] 99, bit[] 100, bit[] 101,
|
|
604
|
+
bit[] 102, bit[] 103, bit[] 104, bit[] 105, bit[] 106, bit[] 107,
|
|
605
|
+
bit[] 108, bit[] 109, bit[] 110, bit[] 111
|
|
606
|
+
};
|
|
539
607
|
// Helper shape: sum and carry for wide 112-bit arithmetic.
|
|
540
|
-
|
|
608
|
+
sig112_sum[] = { sig112[] sum, bit[] carry };
|
|
541
609
|
// Bitwise inversion across the 112-bit extended significand.
|
|
542
610
|
sig112_not = {
|
|
543
611
|
.0 not_bit 0,
|
|
@@ -11,15 +11,29 @@ bits? = ? bits[];
|
|
|
11
11
|
0 = 0b |0;
|
|
12
12
|
1 = 0b |1;
|
|
13
13
|
|
|
14
|
+
int[] = < bits[] "+", bits[] "-" >;
|
|
15
|
+
int? = ? int[];
|
|
16
|
+
|
|
17
|
+
bits_pair[] = { bits[] x, bits[] y };
|
|
18
|
+
inv_state[] = { bits[] x, bits[] r };
|
|
19
|
+
carry_bit[] = < {} 0, {} 1 >;
|
|
20
|
+
add_state[] = { bits[] x, bits[] y, carry_bit[] c, bits[] r };
|
|
21
|
+
times_state[] = { bits[] result, bits[] _x, bits[] y };
|
|
22
|
+
|
|
14
23
|
unat[] = < {} _, U 1 > = U;
|
|
15
24
|
unat? = ? unat[];
|
|
16
25
|
0u = {} | _;
|
|
17
26
|
|
|
27
|
+
split_state[] = { bits[] x_rev, unat[] m, bits[] low };
|
|
28
|
+
split_result[] = { bits[] low, bits[] high };
|
|
29
|
+
half_state[] = { bits[] x, bits[] y, unat[] m };
|
|
30
|
+
int_pair[] = { int[] x, int[] y };
|
|
31
|
+
|
|
18
32
|
empty = < /_ yes, no >;
|
|
19
33
|
empty? = {empty /yes if, () then} .then;
|
|
20
34
|
|
|
21
35
|
inv = {()x, 0b r} _inv;
|
|
22
|
-
_inv = ?
|
|
36
|
+
_inv = ? inv_state[]
|
|
23
37
|
<
|
|
24
38
|
{.x empty? if, .r then} .then,
|
|
25
39
|
<
|
|
@@ -29,7 +43,7 @@ _inv = ?{ bits[] = X x, ... }
|
|
|
29
43
|
>;
|
|
30
44
|
|
|
31
45
|
concat = {.x inv x, .y y} concat_;
|
|
32
|
-
concat_ = ?
|
|
46
|
+
concat_ = ? bits_pair[]
|
|
33
47
|
<
|
|
34
48
|
{.x empty? if, .y then} .then,
|
|
35
49
|
<
|
|
@@ -52,7 +66,7 @@ c0 = {} | 0;
|
|
|
52
66
|
c1 = {} | 1;
|
|
53
67
|
|
|
54
68
|
_plus = {.x x, .y y, .c c, 0b r} _plus_;
|
|
55
|
-
_plus_ = ?
|
|
69
|
+
_plus_ = ? add_state[]
|
|
56
70
|
<
|
|
57
71
|
{.x empty? if, .y empty? and, .c/0 c, .r inv then} .then,
|
|
58
72
|
{.x empty? if, .y empty? and, {.r x, 1 r} _inv then} .then,
|
|
@@ -72,7 +86,7 @@ _plus_ = ?{ bits[] = X x, ... }
|
|
|
72
86
|
>;
|
|
73
87
|
|
|
74
88
|
_minus = {.x x, .y y, .c c, 0b r} _minus_;
|
|
75
|
-
_minus_ = ?
|
|
89
|
+
_minus_ = ? add_state[]
|
|
76
90
|
<
|
|
77
91
|
{.y empty? if, .c/0 and, {.r x, .x r} _inv then} .then,
|
|
78
92
|
<
|
|
@@ -92,7 +106,7 @@ _minus_ = ?{ bits[] = X x, ... }
|
|
|
92
106
|
plus_bits = {.x inv x, .y inv y, c0 c} _plus inv nat;
|
|
93
107
|
minus_bits = {.x inv x, .y inv y, c0 c} _minus inv nat;
|
|
94
108
|
|
|
95
|
-
_times = ?
|
|
109
|
+
_times = ? times_state[]
|
|
96
110
|
<
|
|
97
111
|
{.y empty? if, .result then} .then,
|
|
98
112
|
<
|
|
@@ -108,14 +122,14 @@ unat_to_double_zeros =
|
|
|
108
122
|
? unat[] < /_ 0b, /1 unat_to_double_zeros |0 |0 > ? bits[];
|
|
109
123
|
|
|
110
124
|
_peel2 = < </0,/1> </0,/1>, </0,/1>, () >;
|
|
111
|
-
_half_unat = ?
|
|
125
|
+
_half_unat = ? half_state[]
|
|
112
126
|
<
|
|
113
127
|
{.x empty? if, .y empty? and, .m then} .then,
|
|
114
128
|
{.x _peel2 x, .y _peel2 y, .m |1 m} _half_unat
|
|
115
129
|
>;
|
|
116
130
|
half_unat = {.x x, .y y, 0u m} _half_unat;
|
|
117
131
|
|
|
118
|
-
_split_rev_u = ?
|
|
132
|
+
_split_rev_u = ? split_state[]
|
|
119
133
|
<
|
|
120
134
|
{.m /_ if, {.low nat low, .x_rev inv nat high} then} .then,
|
|
121
135
|
{.x_rev empty? if, {.low nat low, 0 high} then} .then,
|
|
@@ -158,7 +172,7 @@ shift2_bits_u =
|
|
|
158
172
|
{.val x, .m unat_to_double_zeros y} concat
|
|
159
173
|
>;
|
|
160
174
|
|
|
161
|
-
_karatsuba = ?
|
|
175
|
+
_karatsuba = ? bits_pair[]
|
|
162
176
|
<
|
|
163
177
|
-- Base case: if m < 256 (< 512 bits), fall back to times_bits
|
|
164
178
|
{{.x x, .y y} half_unat m, .x x, .y y}
|
|
@@ -197,7 +211,7 @@ _karatsuba = ?{ bits[] = X x, bits[] = Y y }
|
|
|
197
211
|
>
|
|
198
212
|
>;
|
|
199
213
|
|
|
200
|
-
karatsuba =
|
|
214
|
+
karatsuba = ? int_pair[]
|
|
201
215
|
{
|
|
202
216
|
<
|
|
203
217
|
{.x/'+' if, .y /'+' and} sign_plus,
|
package/Examples/nat.k
CHANGED
|
@@ -7,9 +7,10 @@ pred = / +1;
|
|
|
7
7
|
|
|
8
8
|
nat[] = < {} 0, N +1 > = N;
|
|
9
9
|
nat? = ? nat[];
|
|
10
|
+
nat_pair[] = { nat[] x, nat[] y };
|
|
10
11
|
|
|
11
|
-
add = ?
|
|
12
|
-
mult = ?
|
|
12
|
+
add = ? nat_pair[] <{.x pred x, .y succ y} add, .y>;
|
|
13
|
+
mult = ? nat_pair[] <
|
|
13
14
|
{{.x pred x, .y y} mult x, .y y} add,
|
|
14
15
|
0
|
|
15
16
|
>;
|
package/Examples/poly.k
CHANGED
|
@@ -4,6 +4,20 @@
|
|
|
4
4
|
-- List constructors and destructors
|
|
5
5
|
list[X] = < {} nil, { X car, Y cdr } cons > = Y;
|
|
6
6
|
list? = ? list[X];
|
|
7
|
+
|
|
8
|
+
pair[A, B] = { A first, B second };
|
|
9
|
+
list_pair[X] = { list[X] xs, list[X] ys };
|
|
10
|
+
indexed_list[X] = { int[] n, list[X] xs };
|
|
11
|
+
split_result[X] = { list[X] first, list[X] second };
|
|
12
|
+
reverse_state[X] = { list[X] xs, list[X] acc };
|
|
13
|
+
split_by_state[X] = { int[] n, list[X] xs, list[X] acc };
|
|
14
|
+
repeat_args[X] = { int[] n, X x };
|
|
15
|
+
repeat_state[X] = { int[] n, X x, list[X] acc };
|
|
16
|
+
append_args[X] = { X x, list[X] xs };
|
|
17
|
+
split_step_state[X] = {
|
|
18
|
+
list[X] xs, list[X] first, list[X] second
|
|
19
|
+
};
|
|
20
|
+
int_pair[] = { int[] x, int[] y };
|
|
7
21
|
nil = {} | nil list?;
|
|
8
22
|
singleton = {() car, nil cdr} | cons list?;
|
|
9
23
|
cons = {.car car, .cdr cdr} | cons list?;
|
|
@@ -15,11 +29,11 @@ cdr = list? /cons .cdr;
|
|
|
15
29
|
pred_int = dec /'+' nat | '+';
|
|
16
30
|
|
|
17
31
|
-- Concatenate two lists: {xs, ys} -> list
|
|
18
|
-
concat = {.xs reverse xs, .ys acc} _reverse;
|
|
32
|
+
concat = ? list_pair[X] {.xs reverse xs, .ys acc} _reverse;
|
|
19
33
|
|
|
20
34
|
-- Reverse a list: xs -> list
|
|
21
|
-
reverse = {() xs, nil acc} _reverse;
|
|
22
|
-
_reverse = <
|
|
35
|
+
reverse = ? list[X] {() xs, nil acc} _reverse;
|
|
36
|
+
_reverse = ? reverse_state[X] <
|
|
23
37
|
{.xs cdr xs, {.xs car car, .acc cdr} cons acc} _reverse,
|
|
24
38
|
.acc
|
|
25
39
|
>;
|
|
@@ -32,41 +46,41 @@ _length = <
|
|
|
32
46
|
>;
|
|
33
47
|
|
|
34
48
|
-- Get N-th element of a list (0-indexed): {n, xs} -> element
|
|
35
|
-
get_nth = <
|
|
49
|
+
get_nth = ? indexed_list[X] <
|
|
36
50
|
{.n zero_int? if, .xs car then} .then,
|
|
37
51
|
{.n pred_int n, .xs cdr xs} get_nth
|
|
38
52
|
>;
|
|
39
53
|
|
|
40
54
|
-- Split list at index N: {n, xs} -> {first, second}
|
|
41
|
-
split_by = {.n n, .xs xs, nil acc} _split_by;
|
|
42
|
-
_split_by = <
|
|
55
|
+
split_by = ? indexed_list[X] {.n n, .xs xs, nil acc} _split_by;
|
|
56
|
+
_split_by = ? split_by_state[X] <
|
|
43
57
|
{.n zero_int? if, {.acc reverse first, .xs second} then} .then,
|
|
44
58
|
{.xs /nil if, {.acc reverse first, nil second} then} .then,
|
|
45
59
|
{.n pred_int n, .xs cdr xs, {.xs car car, .acc cdr} cons acc} _split_by
|
|
46
60
|
>;
|
|
47
61
|
|
|
48
62
|
-- Take the first N elements of a list: {n, xs} -> list
|
|
49
|
-
take = split_by .first;
|
|
63
|
+
take = ? indexed_list[X] split_by .first;
|
|
50
64
|
|
|
51
65
|
-- Drop the first N elements of a list: {n, xs} -> list
|
|
52
|
-
drop = <
|
|
66
|
+
drop = ? indexed_list[X] <
|
|
53
67
|
{.n zero_int? if, .xs then} .then,
|
|
54
68
|
{.xs /nil if, nil then} .then,
|
|
55
69
|
{.n pred_int n, .xs cdr xs} drop
|
|
56
70
|
>;
|
|
57
71
|
|
|
58
72
|
-- Repeat element X, N times: {n, x} -> list
|
|
59
|
-
repeat = {.n n, .x x, nil acc} _repeat;
|
|
60
|
-
_repeat = <
|
|
73
|
+
repeat = ? repeat_args[X] {.n n, .x x, nil acc} _repeat;
|
|
74
|
+
_repeat = ? repeat_state[X] <
|
|
61
75
|
{.n zero_int? if, .acc then} .then,
|
|
62
76
|
{.n pred_int n, .x x, {.x car, .acc cdr} cons acc} _repeat
|
|
63
77
|
>;
|
|
64
78
|
|
|
65
79
|
-- Append an element to the end of a list: {x, xs} -> list
|
|
66
|
-
append = {.xs xs, {.x car, nil cdr} cons ys} concat;
|
|
80
|
+
append = ? append_args[X] {.xs xs, {.x car, nil cdr} cons ys} concat;
|
|
67
81
|
|
|
68
82
|
-- Zip two lists into a list of {x, y} pairs: {xs, ys} -> list
|
|
69
|
-
zip = <
|
|
83
|
+
zip = ? list_pair[X] <
|
|
70
84
|
{.xs /nil if, nil then} .then,
|
|
71
85
|
{.ys /nil if, nil then} .then,
|
|
72
86
|
{
|
|
@@ -83,7 +97,7 @@ gen_list_ = <
|
|
|
83
97
|
>;
|
|
84
98
|
|
|
85
99
|
-- Identity filter on {x, y} when x is greater than y: ?{X x, X y}=Y -> ?Y
|
|
86
|
-
gt? =
|
|
100
|
+
gt? = ? int_pair[]
|
|
87
101
|
<
|
|
88
102
|
{.x/- if1, .y/+ if2, () then} .then,
|
|
89
103
|
{{.x/- y, .y/- x} GT if, () then} .then,
|
|
@@ -93,8 +107,8 @@ gt? =
|
|
|
93
107
|
|
|
94
108
|
-- Split list into two halves by alternating elements:
|
|
95
109
|
-- xs -> {first, second}
|
|
96
|
-
split = {() xs, nil first, nil second} _split;
|
|
97
|
-
_split = <
|
|
110
|
+
split = ? list[X] {() xs, nil first, nil second} _split;
|
|
111
|
+
_split = ? split_step_state[X] <
|
|
98
112
|
{.xs /nil if, {.first first, .second second} then} .then,
|
|
99
113
|
{
|
|
100
114
|
.xs cdr /nil if,
|
|
@@ -108,7 +122,7 @@ _split = <
|
|
|
108
122
|
>;
|
|
109
123
|
|
|
110
124
|
-- Merge two sorted lists: {xs, ys} -> list
|
|
111
|
-
merge = <
|
|
125
|
+
merge = ? list_pair[X] <
|
|
112
126
|
{.xs /nil if, .ys then} .then,
|
|
113
127
|
{.ys /nil if, .xs then} .then,
|
|
114
128
|
{
|
package/Examples/vhdl_to_k.k
CHANGED
|
@@ -16,6 +16,11 @@ byte[] = {
|
|
|
16
16
|
|
|
17
17
|
nibble[] = { bit[] 0, bit[] 1, bit[] 2, bit[] 3 };
|
|
18
18
|
|
|
19
|
+
full_adder_inputs[] = { bit[] a, bit[] b, bit[] c_in };
|
|
20
|
+
full_adder_outputs[] = { bit[] sum, bit[] carry };
|
|
21
|
+
nibble_pair[] = { nibble[] a, nibble[] b };
|
|
22
|
+
four_bit_adder_result[] = { nibble[] sum, bit[] overflow };
|
|
23
|
+
|
|
19
24
|
0 = {} | 0;
|
|
20
25
|
1 = {} | 1;
|
|
21
26
|
|
|
@@ -33,7 +38,7 @@ xor_bit = < {.x/0 if, .y then} .then, .y not_bit >;
|
|
|
33
38
|
xor_abc = {{.a x, .b y} xor_bit x, .c y} xor_bit;
|
|
34
39
|
|
|
35
40
|
-- VHDL: Full adder component
|
|
36
|
-
full_adder = ?
|
|
41
|
+
full_adder = ? full_adder_inputs[] {
|
|
37
42
|
{.a a, .b b, .c_in c} xor_abc sum,
|
|
38
43
|
{
|
|
39
44
|
{.a x, .b y} and_bit x,
|
|
@@ -42,7 +47,7 @@ full_adder = ?{ bit[] a, bit[] b, bit[] c_in } {
|
|
|
42
47
|
};
|
|
43
48
|
|
|
44
49
|
-- VHDL: architecture of 4-bit adder
|
|
45
|
-
four_bit_adder = ?
|
|
50
|
+
four_bit_adder = ? nibble_pair[] {
|
|
46
51
|
.a a, .b b,
|
|
47
52
|
{.a.0 a, .b.0 b, 0 c_in} full_adder fa0
|
|
48
53
|
} {
|
package/README.md
CHANGED
|
@@ -147,6 +147,28 @@ or inside relation parameter guards (`?{ nat[] = N x, ... }`). There are
|
|
|
147
147
|
no built-in primitive values: the empty product `{}` is the only leaf
|
|
148
148
|
in a non-recursive definition.
|
|
149
149
|
|
|
150
|
+
Like relations, patterns are **content-addressable**. Every definition
|
|
151
|
+
derives a minimal regular tree automaton identified by its cryptographic
|
|
152
|
+
hash `@hash`. A user-defined pattern name is an alias routing its
|
|
153
|
+
parameters into the canonical template slots, filling unexposed ports
|
|
154
|
+
with wildcards `(...)`:
|
|
155
|
+
|
|
156
|
+
```text
|
|
157
|
+
> a[] = {X x, Y y};
|
|
158
|
+
> b[X] = {X x, Y y};
|
|
159
|
+
> c[X,Y] = {X x, Y y};
|
|
160
|
+
> d[Y,X] = {X x, Y y};
|
|
161
|
+
> :pats
|
|
162
|
+
a[] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[(...),(...)]
|
|
163
|
+
b[X] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,(...)]
|
|
164
|
+
c[X,Y] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,Y]
|
|
165
|
+
d[Y,X] = @NPSHm9HbPgXmGHVGMJGJFq7rq4LiFiavPX27mUwyE26x[X,Y]
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Canonical patterns can also be instantiated or defined directly by
|
|
169
|
+
hash (`? @hash[...]`), and inspected with `:pat <name>`. See
|
|
170
|
+
[DOCS/CANONICAL_PATTERNS.md](DOCS/CANONICAL_PATTERNS.md) for full details.
|
|
171
|
+
|
|
150
172
|
**The four-bracket architecture**
|
|
151
173
|
|
|
152
174
|
k uses an orthogonal four-bracket convention with non-overlapping
|
|
@@ -217,11 +239,12 @@ file formats:
|
|
|
217
239
|
carrying principal schema contracts for instant runtime specialization
|
|
218
240
|
(`specializeKVM`).
|
|
219
241
|
|
|
220
|
-
In `k`, all relations and
|
|
221
|
-
content-addressed hashes (`@hash`). There is
|
|
222
|
-
friendly names (such as `plus`, `
|
|
223
|
-
in metadata. Similarly, there are no
|
|
224
|
-
identifiers like `5` or `10` are nullary
|
|
242
|
+
In `k`, all relations, schemas, and pattern templates are identified
|
|
243
|
+
canonically by immutable content-addressed hashes (`@hash`). There is
|
|
244
|
+
no global symbol namespace; friendly names (such as `plus`, `list`, or
|
|
245
|
+
`5`) are local aliases stored in metadata. Similarly, there are no
|
|
246
|
+
primitive numbers: numerical identifiers like `5` or `10` are nullary
|
|
247
|
+
relations mapping `{}` to `bits`.
|
|
225
248
|
|
|
226
249
|
For a detailed guide to all four formats, commands, and workflows, see
|
|
227
250
|
[DOCS/FILE_FORMATS.md](DOCS/FILE_FORMATS.md).
|
|
@@ -356,6 +379,7 @@ and standard libraries:
|
|
|
356
379
|
| File | Contents |
|
|
357
380
|
| --- | --- |
|
|
358
381
|
| `core.k` | Standard library: booleans, units, strings, optionals, and fundamental helpers |
|
|
382
|
+
| `djson.k` | Dynamic JSON with balanced binary bit-trie indexing |
|
|
359
383
|
| `arithmetics.k` | Integer and rational arithmetic built from bit-level relations from scratch |
|
|
360
384
|
| `ieee.k` | IEEE 754 binary64 floating-point arithmetic (`add`, `sub`, `mul`, `div`) |
|
|
361
385
|
| `poly.k` | Polymorphic list operations (`concat`, `reverse`, `length`, `get_nth`, `split_by`, `zip`) |
|
|
@@ -408,6 +432,10 @@ The suite covers:
|
|
|
408
432
|
- [DOCS/FILE_FORMATS.md](DOCS/FILE_FORMATS.md) - guide to
|
|
409
433
|
.k, .ko, .klib, and .kvm file formats and workflows
|
|
410
434
|
- [DOCS/DICTIONARY.md](DOCS/DICTIONARY.md) - concept names and terminology
|
|
435
|
+
- [DOCS/CANONICAL_PATTERNS.md](DOCS/CANONICAL_PATTERNS.md) -
|
|
436
|
+
content-addressable canonical patterns and parameter aliasing
|
|
437
|
+
- [DOCS/NAMED_PATTERNS.md](DOCS/NAMED_PATTERNS.md) -
|
|
438
|
+
parameterized pattern templates and type schemes
|
|
411
439
|
- [DOCS/REPL.md](DOCS/REPL.md) - interactive interpreter and codec details
|
|
412
440
|
- [DOCS/WEB_REPL_REQUIREMENTS.md](DOCS/WEB_REPL_REQUIREMENTS.md) -
|
|
413
441
|
Web REPL architecture and requirements
|
package/backend-api.mjs
CHANGED
|
@@ -17,7 +17,12 @@ export {
|
|
|
17
17
|
retypeObjectRelationForBackend,
|
|
18
18
|
specializeObjectRelations
|
|
19
19
|
} from "./kir.mjs";
|
|
20
|
-
export {
|
|
20
|
+
export {
|
|
21
|
+
compileLibrary,
|
|
22
|
+
decodeObject,
|
|
23
|
+
loadLibrary,
|
|
24
|
+
buildExportPreamble
|
|
25
|
+
} from "./object.mjs";
|
|
21
26
|
export { createState, evaluateInput } from "./repl.mjs";
|
|
22
27
|
export { parse as parseFloat64, print as printFloat64 } from "./codecs/ieee.mjs";
|
|
23
28
|
export { valueForCode } from "./repl-codecs.mjs";
|