@fraczak/k 8.4.0 → 8.5.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 +59 -0
- package/DOCS/ARCHITECTURE.md +83 -0
- package/DOCS/CODECS.md +93 -204
- package/DOCS/DICTIONARY.md +42 -1241
- package/DOCS/FILE_FORMATS.md +170 -625
- package/DOCS/GETTING_STARTED.md +164 -0
- package/DOCS/KIR_V1.md +22 -33
- package/DOCS/KVM_EXECUTION_MODEL.md +88 -481
- package/DOCS/PATTERNS.md +156 -123
- package/DOCS/POLYMORPHIC_IR.md +83 -101
- package/DOCS/PROFILING_AND_TRACING.md +52 -166
- package/DOCS/README.md +35 -0
- package/DOCS/REPL.md +100 -371
- package/DOCS/TEXTUAL_VALUES.md +53 -81
- package/DOCS/TYPE_DERIVATION.md +95 -234
- package/DOCS/book-combined.md +3786 -0
- package/Examples/arithmetics.k +45 -59
- package/Examples/djson.k +102 -112
- package/Examples/karatsuba-mult.k +2 -3
- package/Examples/list.k +6 -7
- package/Examples/nat.k +6 -6
- package/Examples/poly.k +45 -45
- package/LICENSE +15 -0
- package/README.md +72 -410
- package/backends/llvm/README.md +75 -97
- package/backends/llvm/src/cli.mjs +0 -31
- package/backends/wasm/README.md +88 -66
- package/backends/wasm/src/cli.mjs +0 -31
- package/canonical-pattern.mjs +69 -35
- package/codecs/README.md +23 -261
- package/codecs/WIRE_FORMAT.md +132 -0
- package/core.k +5 -15
- package/index.mjs +5 -1
- package/named-patterns.mjs +55 -7
- package/object.mjs +28 -6
- package/objects/README.md +24 -109
- package/package.json +8 -3
- package/paper/Makefile +17 -0
- package/paper/k-paper.pdf +0 -0
- package/paper/k-paper.tex +435 -0
- package/parser.jison +0 -10
- package/parser.mjs +32 -43
- package/repl.mjs +79 -101
- package/scripts/benchmark-idempotency.mjs +3 -3
- package/scripts/run-tests.mjs +1 -1
- package/scripts/test-package.mjs +64 -0
- package/tests/README.md +66 -46
- package/tests/code-derivation/core.mjs +61 -0
- package/tests/helpers/cli.mjs +45 -0
- package/tests/integration.sh +2 -3
- package/tests/test-arithmetics.mjs +47 -237
- package/tests/test-djson-methods.mjs +71 -275
- package/tests/test-hash-normalization.mjs +22 -1
- package/tests/test-karatsuba.mjs +18 -126
- package/tests/test-named-patterns.mjs +174 -606
- package/tests/test-paper-examples.mjs +90 -0
- package/tests/test-poly.mjs +2 -2
- package/tests/test-repl-html.mjs +2 -2
- package/tests/test-repl.mjs +89 -38
- package/tests/test.mjs +27 -68
- package/AGENTS.md +0 -35
- package/CLAUDE.md +0 -64
- package/DOCS/BIG_PICTURE.md +0 -439
- package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +0 -74
- package/DOCS/CANONICAL_PATTERNS.md +0 -349
- package/DOCS/CODEC_ARCHITECTURE.md +0 -257
- package/DOCS/CONVERGENCE.md +0 -123
- package/DOCS/DIVERGENCE_DETECTION.md +0 -23
- package/DOCS/JSON_APPROACHES_COMPARISON.md +0 -302
- package/DOCS/KIR_SPECIALIZATION_DESIGN.md +0 -472
- package/DOCS/Makefile +0 -99
- package/DOCS/NAMED_PATTERNS.md +0 -340
- package/DOCS/OBJECT_FILE_AND_PATTERN.md +0 -343
- package/DOCS/PATTERNS_AND_TYPED_VALUES.md +0 -665
- package/DOCS/WEB_REPL_REQUIREMENTS.md +0 -136
- package/DOCS/book/01-introduction.md +0 -48
- package/DOCS/book/02-syntax-and-values.md +0 -110
- package/DOCS/book/03-types-as-finite-automata.md +0 -155
- package/DOCS/book/04-partial-functions-and-composition.md +0 -145
- package/DOCS/book/05-typing-filters-and-normalization.md +0 -109
- package/DOCS/book/06-values-in-memory.md +0 -1085
- package/DOCS/book/07-the-partial-function-abi.md +0 -143
- package/DOCS/book/08-operational-semantics-and-execution.md +0 -196
- package/DOCS/book/09-compiler-architecture.md +0 -160
- package/DOCS/book/10-from-ast-to-intermediate-representation.md +0 -166
- package/DOCS/book/14-canonical-serialization.md +0 -176
- package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +0 -156
- package/DOCS/book/overview.txt +0 -68
- package/DOCS/book.md +0 -50
- package/Examples/bnat.k +0 -308
- package/Examples/buda.k +0 -66
- package/Examples/byte.k +0 -35
- package/Examples/code.k +0 -43
- package/Examples/vhdl_to_k.k +0 -67
- package/JOURNAL.md +0 -288
- package/TODO.md +0 -107
- package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +0 -233
- package/backends/wasm/TECHNICAL-DEBT.md +0 -58
- package/backends/wasm/demo_ieee.mjs +0 -318
- package/browser/vfs-data.mjs +0 -21
- package/codecs/BINARY_FORMAT.md +0 -53
- package/codecs/POLYMORPHIC_BINARY_FORMAT.md +0 -303
- package/codecs/pandoc.css +0 -71
- package/tests/code-derivation/blowup.mjs +0 -25
- package/tests/code-derivation/bnat.mjs +0 -11
- package/tests/code-derivation/dot.mjs +0 -37
- package/tests/code-derivation/index.mjs +0 -20
- package/tests/code-derivation/loop.mjs +0 -17
- package/tests/code-derivation/nat.mjs +0 -10
- package/tests/code-derivation/not-not.mjs +0 -14
- package/tests/code-derivation/product.mjs +0 -22
- package/tests/code-derivation/rlz.mjs +0 -13
- package/tests/test-fingerprint.mjs +0 -38
package/Examples/arithmetics.k
CHANGED
|
@@ -1,33 +1,16 @@
|
|
|
1
1
|
-- Examples for defining arithmetic functions natively
|
|
2
|
-
|
|
3
|
-
yes = {}|yes;
|
|
4
|
-
no
|
|
2
|
+
yes_no[] = <{} yes, {} no>;
|
|
3
|
+
yes = {}|yes ? yes_no[];
|
|
4
|
+
no = {}|no ? yes_no[];
|
|
5
|
+
swap_yes_no = ? yes_no[] < /yes no, yes >;
|
|
6
|
+
|
|
5
7
|
|
|
6
8
|
bits[] = < {} _, B 0, B 1 > = B;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
-
|
|
25
|
-
0b = {} | _;
|
|
26
|
-
-- |1 = | 1;
|
|
27
|
-
-- |0 = | 0;
|
|
28
|
-
|
|
29
|
-
0 = 0b |0;
|
|
30
|
-
succ = {inv x, 0b y} _inc;
|
|
9
|
+
|
|
10
|
+
_ = {}|_ ?bits[];
|
|
11
|
+
0 = _ |0 ?bits[];
|
|
12
|
+
|
|
13
|
+
succ = {inv x, _ y} _inc;
|
|
31
14
|
1 = 0 succ;
|
|
32
15
|
2 = 1 succ;
|
|
33
16
|
3 = 2 succ;
|
|
@@ -45,7 +28,7 @@ empty = < /_ yes, no >;
|
|
|
45
28
|
empty? = {empty /yes if, () then} .then;
|
|
46
29
|
|
|
47
30
|
concat = {.x inv x, .y y} concat_;
|
|
48
|
-
concat_ = ?
|
|
31
|
+
concat_ = ? { bits[] x, bits[] y }
|
|
49
32
|
<
|
|
50
33
|
{.x empty? if, .y then} .then,
|
|
51
34
|
<
|
|
@@ -53,8 +36,8 @@ concat_ = ? bits_pair[]
|
|
|
53
36
|
{.x /1 x, .y |1 y}
|
|
54
37
|
> concat_
|
|
55
38
|
>;
|
|
56
|
-
inv = {()x,
|
|
57
|
-
_inv = ?
|
|
39
|
+
inv = {()x, _ r} _inv;
|
|
40
|
+
_inv = ? { bits[] x, bits[] r }
|
|
58
41
|
<
|
|
59
42
|
{.x empty? if, .r then} .then,
|
|
60
43
|
<
|
|
@@ -64,7 +47,7 @@ _inv = ? inv_state[]
|
|
|
64
47
|
>;
|
|
65
48
|
|
|
66
49
|
inOrder = {.x inv x, .y inv y, yes inOrder} inOrder_;
|
|
67
|
-
inOrder_ = ?
|
|
50
|
+
inOrder_ = ?{ bits[] x, bits[] y, < {} yes, {} no > inOrder }
|
|
68
51
|
<
|
|
69
52
|
{.x </0,/1> if1, .y /_ if2, .inOrder swap_yes_no then} .then,
|
|
70
53
|
{.x /_ if, .inOrder then} .then,
|
|
@@ -85,9 +68,10 @@ int[] = < bits[] "+", bits[] "-" >;
|
|
|
85
68
|
int? = ? int[];
|
|
86
69
|
int_pair[] = { int[] x, int[] y };
|
|
87
70
|
|
|
88
|
-
remove_leading_zeros = <
|
|
89
|
-
/
|
|
90
|
-
|
|
71
|
+
remove_leading_zeros = ?bits[] <
|
|
72
|
+
/1 |1,
|
|
73
|
+
/_ |_,
|
|
74
|
+
/0 remove_leading_zeros
|
|
91
75
|
>;
|
|
92
76
|
nat = remove_leading_zeros <empty? 0, ()>;
|
|
93
77
|
int = nat | '+';
|
|
@@ -99,12 +83,12 @@ zero_int? = {/'+' /0 empty? if, () then} .then;
|
|
|
99
83
|
minus_one? = {/'-' /1 empty? if, () then} .then;
|
|
100
84
|
|
|
101
85
|
inc = <
|
|
102
|
-
/'+' {inv x,
|
|
86
|
+
/'+' {inv x, _ y} _inc nat |'+',
|
|
103
87
|
minus_one? 0 int,
|
|
104
|
-
/'-' {inv x,
|
|
88
|
+
/'-' {inv x, _ y} _dec nat |'-'
|
|
105
89
|
>;
|
|
106
90
|
|
|
107
|
-
_inc = ?
|
|
91
|
+
_inc = ? { bits[] x, bits[] y } <
|
|
108
92
|
{.x empty? if, .y |1 then} .then,
|
|
109
93
|
{.x /0 x, .y |1 y} concat_,
|
|
110
94
|
{.x /1 x, .y |0 y} _inc
|
|
@@ -112,15 +96,17 @@ _inc = ? bits_pair[] <
|
|
|
112
96
|
|
|
113
97
|
dec = <
|
|
114
98
|
zero_int? 1 |'-',
|
|
115
|
-
/'+' {inv x,
|
|
116
|
-
/'-' {inv x,
|
|
99
|
+
/'+' {inv x, _ y} _dec nat |'+',
|
|
100
|
+
/'-' {inv x, _ y} _inc nat |'-'
|
|
117
101
|
>;
|
|
118
102
|
|
|
119
|
-
_dec = ?
|
|
103
|
+
_dec = ? { bits[] x, bits[] y } <
|
|
120
104
|
{.x /1 x, .y |0 y} concat_,
|
|
121
105
|
{.x /0 x, .y |1 y} _dec
|
|
122
106
|
>;
|
|
123
107
|
|
|
108
|
+
|
|
109
|
+
carry[] = <{} 0,{} 1>;
|
|
124
110
|
c0 = {}|0;
|
|
125
111
|
c1 = {}|1;
|
|
126
112
|
|
|
@@ -143,11 +129,11 @@ swap_sign = <
|
|
|
143
129
|
minus =
|
|
144
130
|
{.x x, .y swap_sign y} plus;
|
|
145
131
|
|
|
146
|
-
_plus = {.x x, .y y, .c c,
|
|
132
|
+
_plus = {.x x, .y y, .c c, _ r} _plus_;
|
|
147
133
|
|
|
148
134
|
_plus_ =
|
|
149
135
|
-- least significant bit first addition with carry
|
|
150
|
-
?
|
|
136
|
+
? { bits[] x, bits[] y, carry[] c, bits[] r } = X
|
|
151
137
|
<
|
|
152
138
|
{.x empty? if, .y empty? and, .c/0 c, .r inv then} .then,
|
|
153
139
|
{.x empty? if, .y empty? and, {.r x, 1 r} _inv then} .then,
|
|
@@ -167,15 +153,15 @@ _plus_ =
|
|
|
167
153
|
|
|
168
154
|
{.x /1 x, .y /1 y, .c/0 c1 c, .r |0 r},
|
|
169
155
|
{.x /1 x, .y /1 y, .c/1 c1 c, .r |1 r}
|
|
170
|
-
> _plus_
|
|
156
|
+
> ?X _plus_
|
|
171
157
|
>
|
|
172
158
|
;
|
|
173
159
|
|
|
174
|
-
_minus = {.x x, .y y, .c c,
|
|
160
|
+
_minus = {.x x, .y y, .c c, _ r} _minus_;
|
|
175
161
|
|
|
176
162
|
_minus_ =
|
|
177
163
|
-- least significant bit first substraction with carry
|
|
178
|
-
?
|
|
164
|
+
? { bits[] x, bits[] y, carry[] c, bits[] r } = X
|
|
179
165
|
<
|
|
180
166
|
{.y empty? if, .c/0 and, {.r x, .x r} _inv then} .then,
|
|
181
167
|
<
|
|
@@ -190,7 +176,7 @@ _minus_ =
|
|
|
190
176
|
{.x/1 x, .y/0 y, .c/1 c0 c, .r |0 r},
|
|
191
177
|
{.x/1 x, .y/1 y, .c/0 c0 c, .r |0 r},
|
|
192
178
|
{.x/1 x, .y/1 y, .c/1 c1 c, .r |1 r}
|
|
193
|
-
> _minus_
|
|
179
|
+
> ?X _minus_
|
|
194
180
|
>
|
|
195
181
|
;
|
|
196
182
|
|
|
@@ -206,7 +192,7 @@ times =
|
|
|
206
192
|
sign_minus
|
|
207
193
|
> sign,
|
|
208
194
|
{
|
|
209
|
-
|
|
195
|
+
_ result,
|
|
210
196
|
.x abs inv _x,
|
|
211
197
|
.y abs y
|
|
212
198
|
} _times result
|
|
@@ -218,7 +204,7 @@ times =
|
|
|
218
204
|
;
|
|
219
205
|
|
|
220
206
|
_times =
|
|
221
|
-
?
|
|
207
|
+
? { bits[] result, bits[] _x, bits[] y } = X
|
|
222
208
|
<
|
|
223
209
|
{ .y empty? if, .result then } .then,
|
|
224
210
|
<
|
|
@@ -232,7 +218,7 @@ _times =
|
|
|
232
218
|
._x _x,
|
|
233
219
|
.y /1 y
|
|
234
220
|
}
|
|
235
|
-
> _times
|
|
221
|
+
> ?X _times
|
|
236
222
|
>
|
|
237
223
|
;
|
|
238
224
|
|
|
@@ -245,7 +231,7 @@ shift_in_1 = inv |1 inv nat;
|
|
|
245
231
|
sub_bits = {.x inv x, .y inv y, c0 c} _minus inv nat;
|
|
246
232
|
|
|
247
233
|
_div_restoring =
|
|
248
|
-
?
|
|
234
|
+
? { bits[] x, bits[] y, bits[] q, bits[] r }
|
|
249
235
|
<
|
|
250
236
|
{.x empty? if, {.q inv int div, .r rem} then} .then,
|
|
251
237
|
<
|
|
@@ -269,7 +255,7 @@ _div_restoring =
|
|
|
269
255
|
_div_restoring
|
|
270
256
|
>;
|
|
271
257
|
|
|
272
|
-
_pad_down = ?
|
|
258
|
+
_pad_down = ? { bits[] x, bits[] steps, bits[] _y }
|
|
273
259
|
<
|
|
274
260
|
{.x empty? if, {._y _y, .steps steps} then} .then,
|
|
275
261
|
{
|
|
@@ -279,7 +265,7 @@ _pad_down = ? pad_state[]
|
|
|
279
265
|
} _pad_down
|
|
280
266
|
>;
|
|
281
267
|
|
|
282
|
-
_align_down = ?
|
|
268
|
+
_align_down = ? { bits[] x, bits[] y, bits[] _y }
|
|
283
269
|
<
|
|
284
270
|
{
|
|
285
271
|
.x </0, /1> x,
|
|
@@ -289,17 +275,17 @@ _align_down = ? align_down_state[]
|
|
|
289
275
|
|
|
290
276
|
{
|
|
291
277
|
.y empty? if,
|
|
292
|
-
{.x x, ._y _y,
|
|
278
|
+
{.x x, ._y _y, _ steps} _pad_down then
|
|
293
279
|
} .then,
|
|
294
280
|
|
|
295
281
|
{
|
|
296
282
|
.x empty? if,
|
|
297
283
|
.y empty? and,
|
|
298
|
-
{._y _y,
|
|
284
|
+
{._y _y, _ steps} then
|
|
299
285
|
} .then
|
|
300
286
|
>;
|
|
301
287
|
|
|
302
|
-
_div_down = ?
|
|
288
|
+
_div_down = ? { bits[] x, bits[] _y, bits[] steps, bits[] q }
|
|
303
289
|
{
|
|
304
290
|
.x x,
|
|
305
291
|
._y _y,
|
|
@@ -343,7 +329,7 @@ _div_down = ? div_down_state[]
|
|
|
343
329
|
} _div_down
|
|
344
330
|
>;
|
|
345
331
|
|
|
346
|
-
_check_2y = ?
|
|
332
|
+
_check_2y = ? { bits[] x, bits[] y }
|
|
347
333
|
<
|
|
348
334
|
{.x empty? if, {} | down_shift then} .then,
|
|
349
335
|
{.y empty? if, {} | restore then} .then,
|
|
@@ -390,12 +376,12 @@ div =
|
|
|
390
376
|
.x x,
|
|
391
377
|
.aligned._y _y,
|
|
392
378
|
.aligned.steps steps,
|
|
393
|
-
|
|
379
|
+
_ q
|
|
394
380
|
} _div_down then
|
|
395
381
|
} .then,
|
|
396
382
|
{
|
|
397
383
|
.choice /restore if,
|
|
398
|
-
{.x x, .y y, 0 r,
|
|
384
|
+
{.x x, .y y, 0 r, _ q} _div_restoring then
|
|
399
385
|
} .then
|
|
400
386
|
> res
|
|
401
387
|
}
|
|
@@ -415,7 +401,7 @@ gcd =
|
|
|
415
401
|
> _gcd
|
|
416
402
|
;
|
|
417
403
|
|
|
418
|
-
_gcd = ?
|
|
404
|
+
_gcd = ? { bits[] x, bits[] y } <
|
|
419
405
|
{ .y remove_leading_zeros empty? if, .x nat then } .then,
|
|
420
406
|
{ .y x, mod y } _gcd
|
|
421
407
|
>;
|