@fraczak/k 8.6.0 → 8.7.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 +44 -0
- package/DOCS/CODECS.md +20 -14
- package/DOCS/FILE_FORMATS.md +79 -12
- package/DOCS/GETTING_STARTED.md +17 -8
- package/DOCS/PATTERNS.md +17 -0
- package/DOCS/REPL.md +15 -8
- package/Examples/arithmetics.k +81 -468
- package/Examples/ieee-arithmetics.k +37 -33
- package/Examples/poly.k +107 -104
- package/README.md +19 -11
- package/backend-api.mjs +3 -1
- package/backends/llvm/README.md +1 -1
- package/backends/llvm/bin/k-llvm-build.mjs +5 -5
- package/backends/llvm/bin/k-llvm-compile.mjs +4 -4
- package/backends/llvm/bin/k-llvm.mjs +6 -6
- package/backends/llvm/src/cli.mjs +17 -12
- package/backends/llvm/tests/integration.sh +4 -1
- package/backends/wasm/README.md +3 -3
- package/backends/wasm/bin/k-wasm-compile.mjs +4 -4
- package/backends/wasm/bin/k-wasm.mjs +4 -4
- package/backends/wasm/src/cli.mjs +12 -10
- package/backends/wasm/src/wasm.mjs +6 -2
- package/backends/wasm/tests/integration.sh +8 -3
- package/browser/shims/fs.mjs +19 -3
- package/canonical-pattern.mjs +351 -82
- package/core.k +2 -1
- package/harness/suites/poly.mjs +14 -30
- package/k.mjs +17 -13
- package/kvm.mjs +13 -9
- package/lib/arithmetics.k +287 -0
- package/lib/bit.k +68 -0
- package/lib/bits.k +221 -0
- package/lib/btrie.k +187 -0
- package/lib/djson.k +326 -0
- package/lib/ieee.k +709 -0
- package/lib/list.k +49 -0
- package/lib/string.k +588 -0
- package/named-patterns.mjs +2 -2
- package/object.mjs +52 -10
- package/objects/README.md +10 -3
- package/objects/compile.mjs +10 -8
- package/package.json +1 -1
- package/repl.mjs +227 -22
- package/scripts/benchmark-idempotency.mjs +22 -15
- package/scripts/benchmark.mjs +29 -10
- package/scripts/build-repl-html.mjs +49 -14
- package/scripts/perf-ieee.mjs +9 -1
- package/scripts/perf-int.mjs +10 -17
- package/scripts/perf-multiplication.mjs +9 -15
- package/scripts/perf-poly.mjs +16 -32
- package/scripts/run-tests.mjs +2 -2
- package/scripts/test-package.mjs +8 -2
- package/tests/helpers/cli.mjs +11 -3
- package/tests/integration.sh +9 -4
- package/tests/test-arithmetics.mjs +4 -2
- package/tests/test-djson-methods.mjs +16 -12
- package/tests/test-ieee-arithmetic.mjs +11 -3
- package/tests/test-ieee-text.mjs +15 -4
- package/tests/test-k-object.mjs +11 -3
- package/tests/test-karatsuba.mjs +1 -1
- package/tests/test-kvm-polymorphic.mjs +54 -34
- package/tests/test-lib.mjs +115 -0
- package/tests/test-named-patterns.mjs +12 -3
- package/tests/test-parse-json.mjs +33 -13
- package/tests/test-poly.mjs +10 -4
- package/tests/test-repl-html.mjs +18 -7
- package/tests/test-repl.mjs +154 -54
- package/Examples/djson.k +0 -894
- package/Examples/ieee.k +0 -969
- package/Examples/karatsuba-mult.k +0 -227
- package/Examples/list.k +0 -10
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
-- Karatsuba multiplication O(n^1.585)
|
|
2
|
-
-- Self-contained and minimal implementation
|
|
3
|
-
-- for arbitrary-precision integers
|
|
4
|
-
|
|
5
|
-
yes = {} | yes;
|
|
6
|
-
no = {} | no;
|
|
7
|
-
|
|
8
|
-
bits[] = < {} _, B 0, B 1 > = B;
|
|
9
|
-
|
|
10
|
-
0b = {} | _;
|
|
11
|
-
0 = 0b |0;
|
|
12
|
-
1 = 0b |1;
|
|
13
|
-
|
|
14
|
-
int[] = < bits[] "+", bits[] "-" >;
|
|
15
|
-
|
|
16
|
-
bits_pair[] = { bits[] x, bits[] y };
|
|
17
|
-
inv_state[] = { bits[] x, bits[] r };
|
|
18
|
-
carry_bit[] = < {} 0, {} 1 >;
|
|
19
|
-
add_state[] = { bits[] x, bits[] y, carry_bit[] c, bits[] r };
|
|
20
|
-
times_state[] = { bits[] result, bits[] _x, bits[] y };
|
|
21
|
-
|
|
22
|
-
unat[] = < {} _, U 1 > = U;
|
|
23
|
-
|
|
24
|
-
0u = {} | _;
|
|
25
|
-
|
|
26
|
-
split_state[] = { bits[] x_rev, unat[] m, bits[] low };
|
|
27
|
-
split_result[] = { bits[] low, bits[] high };
|
|
28
|
-
half_state[] = { bits[] x, bits[] y, unat[] m };
|
|
29
|
-
int_pair[] = { int[] x, int[] y };
|
|
30
|
-
|
|
31
|
-
empty = < /_ yes, no >;
|
|
32
|
-
empty? = {empty /yes if, () then} .then;
|
|
33
|
-
|
|
34
|
-
inv = {()x, 0b r} _inv;
|
|
35
|
-
_inv = ? inv_state[]
|
|
36
|
-
<
|
|
37
|
-
{.x empty? if, .r then} .then,
|
|
38
|
-
<
|
|
39
|
-
{.x /0 x, .r |0 r},
|
|
40
|
-
{.x /1 x, .r |1 r}
|
|
41
|
-
> _inv
|
|
42
|
-
>;
|
|
43
|
-
|
|
44
|
-
concat = {.x inv x, .y y} concat_;
|
|
45
|
-
concat_ = ? bits_pair[]
|
|
46
|
-
<
|
|
47
|
-
{.x empty? if, .y then} .then,
|
|
48
|
-
<
|
|
49
|
-
{.x /0 x, .y |0 y},
|
|
50
|
-
{.x /1 x, .y |1 y}
|
|
51
|
-
> concat_
|
|
52
|
-
>;
|
|
53
|
-
|
|
54
|
-
remove_leading_zeros = < /0 remove_leading_zeros, () >;
|
|
55
|
-
nat = remove_leading_zeros <empty? 0, ()>;
|
|
56
|
-
|
|
57
|
-
int = nat | '+';
|
|
58
|
-
abs = </'+', /'-'>;
|
|
59
|
-
|
|
60
|
-
sign_plus = {} | '+';
|
|
61
|
-
sign_minus = {} | '-';
|
|
62
|
-
swap_sign = < /'+' | '-', /'-' | '+' >;
|
|
63
|
-
|
|
64
|
-
c0 = {} | 0;
|
|
65
|
-
c1 = {} | 1;
|
|
66
|
-
|
|
67
|
-
_plus = {.x x, .y y, .c c, 0b r} _plus_;
|
|
68
|
-
_plus_ = ? add_state[]
|
|
69
|
-
<
|
|
70
|
-
{.x empty? if, .y empty? and, .c/0 c, .r inv then} .then,
|
|
71
|
-
{.x empty? if, .y empty? and, {.r x, 1 r} _inv then} .then,
|
|
72
|
-
{.x empty? if, .c/0 and, {.r x, .y r} _inv then} .then,
|
|
73
|
-
<
|
|
74
|
-
{.x y, .y empty? x, .c c, .r r},
|
|
75
|
-
{<.c/0 0, 1> x, .y y, .x empty? c0 c, .r r},
|
|
76
|
-
{.x /0 x, .y /0 y, .c/0 c0 c, .r |0 r},
|
|
77
|
-
{.x /0 x, .y /0 y, .c/1 c0 c, .r |1 r},
|
|
78
|
-
{.x /0 x, .y /1 y, .c/0 c0 c, .r |1 r},
|
|
79
|
-
{.x /0 x, .y /1 y, .c/1 c1 c, .r |0 r},
|
|
80
|
-
{.x /1 x, .y /0 y, .c/0 c0 c, .r |1 r},
|
|
81
|
-
{.x /1 x, .y /0 y, .c/1 c1 c, .r |0 r},
|
|
82
|
-
{.x /1 x, .y /1 y, .c/0 c1 c, .r |0 r},
|
|
83
|
-
{.x /1 x, .y /1 y, .c/1 c1 c, .r |1 r}
|
|
84
|
-
> _plus_
|
|
85
|
-
>;
|
|
86
|
-
|
|
87
|
-
_minus = {.x x, .y y, .c c, 0b r} _minus_;
|
|
88
|
-
_minus_ = ? add_state[]
|
|
89
|
-
<
|
|
90
|
-
{.y empty? if, .c/0 and, {.r x, .x r} _inv then} .then,
|
|
91
|
-
<
|
|
92
|
-
{.x/0 x, .y empty? y, .c/1 c1 c, .r |1 r},
|
|
93
|
-
{.x/1 x, .y empty? y, .c/1 c0 c, .r |0 r},
|
|
94
|
-
{.x/0 x, .y/0 y, .c/0 c0 c, .r |0 r},
|
|
95
|
-
{.x/0 x, .y/0 y, .c/1 c1 c, .r |1 r},
|
|
96
|
-
{.x/0 x, .y/1 y, .c/0 c1 c, .r |1 r},
|
|
97
|
-
{.x/0 x, .y/1 y, .c/1 c1 c, .r |0 r},
|
|
98
|
-
{.x/1 x, .y/0 y, .c/0 c0 c, .r |1 r},
|
|
99
|
-
{.x/1 x, .y/0 y, .c/1 c0 c, .r |0 r},
|
|
100
|
-
{.x/1 x, .y/1 y, .c/0 c0 c, .r |0 r},
|
|
101
|
-
{.x/1 x, .y/1 y, .c/1 c1 c, .r |1 r}
|
|
102
|
-
> _minus_
|
|
103
|
-
>;
|
|
104
|
-
|
|
105
|
-
plus_bits = {.x inv x, .y inv y, c0 c} _plus inv nat;
|
|
106
|
-
minus_bits = {.x inv x, .y inv y, c0 c} _minus inv nat;
|
|
107
|
-
|
|
108
|
-
_times = ? times_state[]
|
|
109
|
-
<
|
|
110
|
-
{.y empty? if, .result then} .then,
|
|
111
|
-
<
|
|
112
|
-
{.result |0 result, ._x _x, .y /0 y},
|
|
113
|
-
{{._x x, .result |0 y, c0 c} _plus result, ._x _x, .y /1 y}
|
|
114
|
-
> _times
|
|
115
|
-
>;
|
|
116
|
-
times_bits = {0 result, .x inv _x, .y y} _times inv nat;
|
|
117
|
-
|
|
118
|
-
unat_to_zeros =
|
|
119
|
-
? unat[] < /_ 0b, /1 unat_to_zeros |0 > ? bits[];
|
|
120
|
-
unat_to_double_zeros =
|
|
121
|
-
? unat[] < /_ 0b, /1 unat_to_double_zeros |0 |0 > ? bits[];
|
|
122
|
-
|
|
123
|
-
_peel2 = < </0,/1> </0,/1>, </0,/1>, () >;
|
|
124
|
-
_half_unat = ? half_state[]
|
|
125
|
-
<
|
|
126
|
-
{.x empty? if, .y empty? and, .m then} .then,
|
|
127
|
-
{.x _peel2 x, .y _peel2 y, .m |1 m} _half_unat
|
|
128
|
-
>;
|
|
129
|
-
half_unat = {.x x, .y y, 0u m} _half_unat;
|
|
130
|
-
|
|
131
|
-
_split_rev_u = ? split_state[]
|
|
132
|
-
<
|
|
133
|
-
{.m /_ if, {.low nat low, .x_rev inv nat high} then} .then,
|
|
134
|
-
{.x_rev empty? if, {.low nat low, 0 high} then} .then,
|
|
135
|
-
<
|
|
136
|
-
{.x_rev /0 x_rev, .m /1 m, .low |0 low},
|
|
137
|
-
{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 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1 /1
|
|
159
|
-
yes, no
|
|
160
|
-
>;
|
|
161
|
-
|
|
162
|
-
shift_bits_u =
|
|
163
|
-
<
|
|
164
|
-
{.val empty? if, 0b then} .then,
|
|
165
|
-
{.val x, .m unat_to_zeros y} concat
|
|
166
|
-
>;
|
|
167
|
-
|
|
168
|
-
shift2_bits_u =
|
|
169
|
-
<
|
|
170
|
-
{.val empty? if, 0b then} .then,
|
|
171
|
-
{.val x, .m unat_to_double_zeros y} concat
|
|
172
|
-
>;
|
|
173
|
-
|
|
174
|
-
_karatsuba = ? bits_pair[]
|
|
175
|
-
<
|
|
176
|
-
-- Base case: if m < 256 (< 512 bits), fall back to times_bits
|
|
177
|
-
{{.x x, .y y} half_unat m, .x x, .y y}
|
|
178
|
-
<
|
|
179
|
-
{
|
|
180
|
-
.m at_least_256_u? /no if,
|
|
181
|
-
{.x x, .y y} times_bits then
|
|
182
|
-
} .then,
|
|
183
|
-
-- Recursive Karatsuba:
|
|
184
|
-
{
|
|
185
|
-
{.x x, .m m} split_at sx,
|
|
186
|
-
{.y x, .m m} split_at sy,
|
|
187
|
-
.m m
|
|
188
|
-
}
|
|
189
|
-
{
|
|
190
|
-
{.sx.high x, .sy.high y} _karatsuba z2,
|
|
191
|
-
{.sx.low x, .sy.low y} _karatsuba z0,
|
|
192
|
-
{
|
|
193
|
-
{.sx.high x, .sx.low y} plus_bits x,
|
|
194
|
-
{.sy.high x, .sy.low y} plus_bits y
|
|
195
|
-
} _karatsuba z_sum,
|
|
196
|
-
.m m
|
|
197
|
-
}
|
|
198
|
-
{
|
|
199
|
-
.z2 z2,
|
|
200
|
-
.z0 z0,
|
|
201
|
-
{{.z_sum x, .z2 y} minus_bits x, .z0 y} minus_bits z1,
|
|
202
|
-
.m m
|
|
203
|
-
}
|
|
204
|
-
{
|
|
205
|
-
{.z2 val, .m m} shift2_bits_u z2_s,
|
|
206
|
-
{.z1 val, .m m} shift_bits_u z1_s,
|
|
207
|
-
.z0 z0
|
|
208
|
-
}
|
|
209
|
-
{{.z2_s x, .z1_s y} plus_bits x, .z0 y} plus_bits
|
|
210
|
-
>
|
|
211
|
-
>;
|
|
212
|
-
|
|
213
|
-
karatsuba = ? int_pair[]
|
|
214
|
-
{
|
|
215
|
-
<
|
|
216
|
-
{.x/'+' if, .y /'+' and} sign_plus,
|
|
217
|
-
{.x/'-' if, .y /'-' and} sign_plus,
|
|
218
|
-
sign_minus
|
|
219
|
-
> sign,
|
|
220
|
-
{.x abs x, .y abs y} _karatsuba result
|
|
221
|
-
}
|
|
222
|
-
<
|
|
223
|
-
{.sign/'+' if, .result int then} .then,
|
|
224
|
-
{.sign/'-' if, .result int swap_sign then} .then
|
|
225
|
-
>;
|
|
226
|
-
|
|
227
|
-
()
|
package/Examples/list.k
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
-- Examples/list.k: Generic linked list patterns and relations
|
|
2
|
-
|
|
3
|
-
list[X] = < {} nil, { X car, Y cdr } cons > = Y;
|
|
4
|
-
|
|
5
|
-
nil = {} | nil ? list[(...)];
|
|
6
|
-
singleton = { () car, nil cdr } | cons ? list[(...)];
|
|
7
|
-
cons = { .car car, .cdr cdr } | cons ? list[(...)];
|
|
8
|
-
nil? = ? list[(...)] /nil nil;
|
|
9
|
-
car = ? list[X] /cons .car ?X;
|
|
10
|
-
cdr = ? list[(...)] /cons .cdr;
|