@fraczak/k 6.0.4 → 6.1.1

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.
Files changed (59) hide show
  1. package/.idea/codeStyles/codeStyleConfig.xml +5 -0
  2. package/.idea/copilot.data.migration.agent.xml +6 -0
  3. package/.idea/copilot.data.migration.ask.xml +6 -0
  4. package/.idea/copilot.data.migration.ask2agent.xml +6 -0
  5. package/.idea/copilot.data.migration.edit.xml +6 -0
  6. package/.idea/k.iml +9 -0
  7. package/.idea/misc.xml +6 -0
  8. package/.idea/modules.xml +8 -0
  9. package/.idea/vcs.xml +6 -0
  10. package/AGENTS.md +2 -2
  11. package/CHANGELOG.md +22 -5
  12. package/CLAUDE.md +4 -4
  13. package/DOCS/BIG_PICTURE.md +5 -5
  14. package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +3 -3
  15. package/DOCS/OBJECT_FILE_AND_PATTERN.md +9 -9
  16. package/DOCS/PATTERNS.md +6 -5
  17. package/DOCS/REPL.md +2 -2
  18. package/DOCS/book/06-values-in-memory.md +1 -1
  19. package/DOCS/book.md +20 -24
  20. package/README.md +200 -274
  21. package/codecs/README.md +21 -2
  22. package/codecs/example-pipeline.mjs +14 -0
  23. package/codecs/ieee.mjs +13 -3
  24. package/codecs/int.mjs +13 -3
  25. package/codecs/json.mjs +13 -3
  26. package/codecs/k-parse.mjs +5 -0
  27. package/codecs/k-print.mjs +16 -1
  28. package/codecs/show.mjs +15 -1
  29. package/codecs/unit.mjs +13 -3
  30. package/codecs/utf16.mjs +13 -3
  31. package/codecs/utf8.mjs +13 -3
  32. package/k-vscode/README.md +3 -2
  33. package/k.mjs +9 -1
  34. package/k_compiler/runtime.c +205 -0
  35. package/k_compiler/runtime.h +34 -0
  36. package/objects/README.md +11 -8
  37. package/package.json +13 -7
  38. package/{repl2.mjs → repl.mjs} +47 -9
  39. package/{test-repl2.mjs → test-repl.mjs} +16 -5
  40. package/.codex +0 -0
  41. package/DOCS/a.html +0 -398
  42. package/NAT.k~ +0 -452
  43. package/UU/ta.k +0 -1
  44. package/UU/ta.k~ +0 -0
  45. package/UU/tb.k +0 -0
  46. package/UU/tc.k +0 -0
  47. package/codecs/BINARY_FORMAT.html +0 -471
  48. package/codecs/POLYMORPHIC_BINARY_FORMAT.html +0 -355
  49. package/core.json +0 -1
  50. package/core.klib +0 -1
  51. package/core.klib~ +0 -0
  52. package/ieee.klib +0 -0
  53. package/k-vscode/package-lock.json +0 -4176
  54. package/kkk.k +0 -197
  55. package/kkk.klib +0 -0
  56. package/kkk.klib~ +0 -0
  57. package/p.json +0 -5475
  58. package/type_registry/registry.mjs~ +0 -105
  59. package/type_registry/typeParser.jison~ +0 -132
package/NAT.k~ DELETED
@@ -1,452 +0,0 @@
1
- -- Examples for defining arithmetic functions natively
2
- $ yes_no = < {} yes, {} no >;
3
- swap_yes_no = < /yes |no, /no |yes > $yes_no;
4
- yes = {}|yes $yes_no;
5
- no = yes swap_yes_no;
6
-
7
- list? = ?< {} nil, {X car, Y cdr} cons > = Y;
8
- nil = {}|nil list?;
9
- singleton = {() car, nil cdr} |cons list?;
10
- cons = {.car car, .cdr cdr} |cons list?;
11
- nil? = list? / nil nil;
12
- car = list? /cons .car;
13
- cdr = list? /cons .cdr;
14
-
15
- $ bits = < {} _, bits 0, bits 1 >;
16
-
17
- 0b = {} | _;
18
- 1_ = | 1;
19
- 0_ = | 0;
20
-
21
- 0 = 0b 0_;
22
- succ = {inv x, 0b y} _inc;
23
- 1 = 0 succ;
24
- 2 = 1 succ;
25
- 3 = 2 succ;
26
- 4 = 3 succ;
27
- 5 = 4 succ;
28
- 6 = 5 succ;
29
- 7 = 6 succ;
30
- 8 = 7 succ;
31
- 9 = 8 succ;
32
- 10 = 9 succ;
33
- 11 = 10 succ;
34
- 12 = 11 succ;
35
-
36
- empty = $bits < /_ yes, no >;
37
- empty? = {empty /yes if, () then} .then;
38
-
39
- concat = ${bits x, bits y} {.x inv x, .y y} concat_;
40
- concat_ = ${bits x, bits y}
41
- <
42
- {.x /0 x, .y 0_ y} concat_,
43
- {.x /1 x, .y 1_ y} concat_,
44
- .y
45
- >;
46
- inv = {()x, 0b r} _inv;
47
- _inv = ${bits x, bits r} < {.x /0 x, .r 0_ r} _inv, {.x /1 x, .r 1_ r} _inv, .r >;
48
-
49
- inOrder = ${bits x, bits y} {.x inv x, .y inv y, yes inOrder} inOrder_;
50
- inOrder_ = ${bits x, bits y, yes_no inOrder}
51
- <
52
- {.x /0 x, .y /0 y, .inOrder inOrder} inOrder_,
53
- {.x /1 x, .y /1 y, .inOrder inOrder} inOrder_,
54
- {.x /0 x, .y /1 y, .inOrder inOrder} inOrder_,
55
- {.x /1 y, .y /0 x, .inOrder swap_yes_no inOrder} inOrder_,
56
- {.x </0,/1> if, .inOrder swap_yes_no then} .then,
57
- .inOrder
58
- >;
59
-
60
- GT = {inOrder /no if, () then} . then;
61
-
62
- EQ = GT {.x y, .y x} GT;
63
-
64
- toBitList = {inv x, nil r} _toBitList;
65
- _toBitList = <
66
- {.x empty? if, .r then }.then,
67
- {.x /0 x, {0 car, .r cdr} cons r} _toBitList,
68
- {.x /1 x, {1 car, .r cdr} cons r} _toBitList
69
- >;
70
- fromBitList =
71
- <
72
- cdr <
73
- {car /0 if, cdr fromBitList 0_ then} .then,
74
- {car /1 if, cdr fromBitList 1_ then} .then
75
- >,
76
- 0b
77
- > $bits;
78
-
79
- --------------------------------------
80
- $ int = < bits '+', bits '-' >;
81
-
82
- remove_leading_zeros = <
83
- /0 remove_leading_zeros,
84
- ()
85
- >;
86
- nat = remove_leading_zeros <empty? 0, ()>;
87
- int = nat | '+' $int;
88
- abs = $int </'+',/'-'>;
89
-
90
- pos? = $int {dec /'+' if, () then} .then;
91
- neg? = $int {/'-' if, () then} .then;
92
- zero_int? = $int {/'+' /0 empty? if, () then} .then;
93
- minus_one? = $int {/'-' /1 empty? if, () then} .then;
94
-
95
- inc = $int <
96
- /'+' {inv x, 0b y} _inc |'+',
97
- minus_one? 0 int,
98
- /'-' {inv x, 0b y} _dec |'-'
99
- >;
100
-
101
- _inc = ${bits x, bits y} <
102
- {.x /0 x, .y 1_ y} concat_,
103
- {.x /1 x, .y 0_ y} _inc,
104
- .y 1_
105
- >;
106
-
107
- dec = $int <
108
- zero_int? 1 |'-',
109
- /'+' {inv x, 0b y} _dec |'+',
110
- /'-' {inv x, 0b y} _inc |'-'
111
- > $int;
112
-
113
- _dec = ?{X x, X y, ...} <
114
- {.x /1 x, .y 0_ y} concat_,
115
- {.x /0 x, .y 1_ y} _dec
116
- > ?X;
117
-
118
- $carry = <{} 0, {} 1>;
119
- c0 = {}|0 $carry;
120
- c1 = {}|1 $carry;
121
-
122
- plus = ${int x, int y}
123
- <
124
- {.x/'+' inv x, .y/'+' inv y, c0 c } _plus inv nat | '+',
125
- {.x/'-' inv x, .y/'-' inv y, c0 c } _plus inv nat | '-',
126
- {.y/'-' x, .x/'+' y} GT {.x inv x, .y inv y, c0 c} _minus inv nat | '-',
127
- {.x/'+' inv x, .y/'-' inv y, c0 c} _minus inv nat |'+',
128
- {.x/'-' x, .y/'+' y} GT {.x inv x, .y inv y, c0 c} _minus inv nat | '-',
129
- {.y/'+' inv x, .x/'-' inv y, c0 c} _minus inv nat | '+'
130
- >
131
- $int;
132
-
133
- swap_sign = <
134
- zero_int?,
135
- /'+' | '-',
136
- /'-' | '+'
137
- >;
138
-
139
- minus = ${int x, int y}
140
- {.x x, .y swap_sign y} plus;
141
-
142
- _plus =
143
- -- least significant bit first addition with carry
144
- ${bits x, bits y, carry c}
145
- <
146
- {.x empty? if, .y empty? and, <.c/0 0, 1> then} .then,
147
- {.x empty? if, .c/0 and, .y then} .then,
148
-
149
- {.x y, .y empty? x, .c c} _plus,
150
- {<.c/0 0, 1> x,
151
- .y y, .x empty? c0 c} _plus,
152
-
153
- {.x /0 x, .y /0 y, .c/0 c0 c} _plus 0_,
154
- {.x /0 x, .y /0 y, .c/1 c0 c} _plus 1_,
155
-
156
- {.x /0 x, .y /1 y, .c/0 c0 c} _plus 1_,
157
- {.x /0 x, .y /1 y, .c/1 c1 c} _plus 0_,
158
- {.x /1 x, .y /0 y, .c/0 c0 c} _plus 1_,
159
- {.x /1 x, .y /0 y, .c/1 c1 c} _plus 0_,
160
-
161
- {.x /1 x, .y /1 y, .c/0 c1 c} _plus 0_,
162
- {.x /1 x, .y /1 y, .c/1 c1 c} _plus 1_
163
- >
164
- ;
165
-
166
- _minus =
167
- -- least significat bit first substraction with carry
168
- ${bits x, bits y, carry c}
169
- <
170
- {.x then, .y empty? if, .c/0 and} .then,
171
- {.x/1 then, .y empty? if, .c/1 and} .then 0_,
172
- {.x/0 x, .y empty? y, .c/1 c1 c} _minus 1_,
173
- {.x/0 x, .y/0 y, .c/0 c0 c} _minus 0_,
174
- {.x/0 x, .y/0 y, .c/1 c1 c} _minus 1_,
175
- {.x/0 x, .y/1 y, .c/0 c1 c} _minus 1_,
176
- {.x/0 x, .y/1 y, .c/1 c1 c} _minus 0_,
177
- {.x/1 x, .y/0 y, .c/0 c0 c} _minus 1_,
178
- {.x/1 x, .y/0 y, .c/1 c0 c} _minus 0_,
179
- {.x/1 x, .y/1 y, .c/0 c0 c} _minus 0_,
180
- {.x/1 x, .y/1 y, .c/1 c1 c} _minus 1_
181
- >
182
- ;
183
-
184
- $ sign = < {} '+' , {} '-' >;
185
- sign_plus = {}|'+' $sign;
186
- sign_minus = {}|'-' $sign;
187
- get_sign = $int </'+' sign_plus, /'-' sign_minus>;
188
-
189
- times = ${int x, int y}
190
- {
191
- <
192
- {.x/'+' if, .y /'+' and} sign_plus,
193
- {.x/'-' if, .y /'-' and} sign_plus,
194
- sign_minus
195
- > sign,
196
- {
197
- 0 result,
198
- .x abs inv _x,
199
- .y abs y
200
- } _times result
201
- }
202
- <
203
- {.sign/'+' if, .result inv int then} .then,
204
- {.sign/'-' if, .result inv int swap_sign then} .then
205
- >
206
- ;
207
-
208
- _times =
209
- ${bits result, bits _x, bits y}
210
- <
211
- { .y empty? if, .result then } .then,
212
- {
213
- .result 0_ result,
214
- ._x _x,
215
- .y /0 y
216
- } _times,
217
- {
218
- {._x x, .result 0_ y, c0 c } _plus result,
219
- ._x _x,
220
- .y /1 y
221
- } _times
222
- >
223
- ;
224
-
225
-
226
- mod =
227
- ${bits x, bits y}
228
- _log!
229
- <
230
- -- if y > x then (x mod y) = x
231
- {.y x, .x y} GT .y,
232
- -- else, i.e.
233
- -- if x >= y then (x mod y) = ((x-y mod 2y) mod y)
234
- {
235
- {
236
- {.x int x, .y int y} minus /"+" x,
237
- .y inv 0_ inv y
238
- } mod x,
239
- .y y
240
- } mod
241
- >
242
- ;
243
-
244
-
245
- // gratest common divisor
246
- gcd = ${bits x, bits y}
247
- <
248
- {.y x, .x y} GT,
249
- ()
250
- > _gcd
251
- ;
252
-
253
- _gcd = <
254
- { .y /0 if, .x then } .then,
255
- { .y x, mod y } _gcd
256
- >;
257
-
258
-
259
-
260
- // div
261
-
262
- log2_floor =
263
- ${bits x, bits step}
264
- -- finds the largest k, such that 'step * 2^k <= x',
265
- -- returns k1_, i.e., 0 if x < step, and k1_ otherwise.
266
- {.x x, .step step, 0 log} log2_floor_;
267
-
268
- log2_floor_ = <
269
- {{.step x,.x y} GT if, .log then} .then,
270
- {.x x, {.step x,0 y} GT {.x x,2 y} {.x int x, .y int y} times abs step, .log int inc abs log} log2_floor_
271
- >;
272
-
273
-
274
- x_times_2_to_k =
275
- ${bits x, bits k}
276
- {.x inv _x, .k int k} _x_times_2_to_the_power_of_k inv
277
- ;
278
-
279
- _x_times_2_to_the_power_of_k = <
280
- {.k zero_int? if, ._x then} .then,
281
- {._x 0_ _x, .k dec k} _x_times_2_to_the_power_of_k
282
- >
283
- ;
284
-
285
- div =
286
- ${int x, int y}
287
- // remember sign
288
- {
289
- <
290
- { .x/'+' if, .y get_sign then } .then,
291
- { .x/'-' if, .y swap_sign get_sign then } .then
292
- > sign,
293
- {.x abs x, .y abs step, 0 int res} _div res_wo_sign
294
- }
295
- <
296
- {.sign/'+' if, {.res_wo_sign.div div, .res_wo_sign.rem rem} then} .then,
297
- { .res_wo_sign.div swap_sign div, .res_wo_sign.rem rem }
298
- >
299
- ;
300
-
301
- _div =
302
- {
303
- .x x,
304
- .step step,
305
- .res res,
306
- {.x x, .step step} log2_floor log
307
- }
308
- <
309
- { .log int zero_int? if, {.res div, .x rem} then } .then,
310
- {
311
- {1 x, .log int dec abs k} x_times_2_to_k factor,
312
- () i
313
- }
314
- {
315
- {.i.x int x, {.i.step int x, .factor int y} times y} minus abs x,
316
- .i.step step,
317
- {.i.res x, .factor int y} plus res
318
- } _div
319
- >;
320
-
321
-
322
- lcm =
323
- ${bits x, bits y}
324
- {{.x int x, .y int y} times x, gcd int y} div .div abs
325
- ;
326
-
327
- factorial = <
328
- zero_int? 1 int,
329
- /"+" int {dec factorial x, () y} times
330
- >;
331
-
332
- fib_ =
333
- <
334
- { .n int dec /"+" n,
335
- ."fib(n),fib(n.1)" {plus x, .x y} "fib(n),fib(n.1)"
336
- } fib_,
337
- ."fib(n),fib(n.1)"
338
- >
339
- ;
340
-
341
- fib = {$bits n, {1 int x, 0 int y} "fib(n),fib(n.1)"} fib_ .x;
342
-
343
-
344
- --------------------
345
- $ rat = { int n, int d };
346
-
347
- rat = ${bits n, bits d} {.n int n, .d int d } normalize_rat;
348
-
349
- normalize_rat =
350
- $rat
351
- <
352
- { .d/"-" if,
353
- {
354
- .n swap_sign n,
355
- .d swap_sign d
356
- } then
357
- } .then,
358
- ()
359
- >
360
- {() x, {.n abs x,.d abs y} gcd int y}
361
- {
362
- {.x.n x,.y y} div .div n,
363
- {.x.d x,.y y} div .div d
364
- }
365
- $rat
366
- ;
367
-
368
-
369
- plus_rat =
370
- ${rat x, rat y}
371
- {
372
- .x x,
373
- .y y,
374
- {.x.d abs x,.y.d abs y} lcm int lcm
375
- }
376
- {
377
- {
378
- {.x.n x, {.lcm x, .x.d y} div .div y} times x,
379
- {.y.n x, {.lcm x, .y.d y} div .div y} times y
380
- } plus n,
381
- .lcm d
382
- }
383
- normalize_rat
384
- ;
385
-
386
-
387
- times_rat =
388
- ${rat x, rat y}
389
- {
390
- {.x.n x,.y.n y} times n,
391
- {.x.d x,.y.d y} times d
392
- }
393
- normalize_rat;
394
-
395
-
396
- minus_rat =
397
- ${rat x, rat y}
398
- {
399
- .x x,
400
- .y {.n swap_sign n, .d d} y
401
- }
402
- plus_rat;
403
-
404
- inverse_rat =
405
- $rat
406
- { .d n, .n d }
407
- normalize_rat
408
- ;
409
-
410
- div_rat =
411
- ${rat x, rat y}
412
- { .x x, .y inverse_rat y} times_rat
413
- ;
414
-
415
-
416
- --------------------
417
-
418
- int_rat =
419
- $rat
420
- { .n n, .d d, {.n x,.d y} div div }
421
- { .div.div int, {.div.rem int n, .d d} rat}
422
- ${int int, rat rat}
423
- ;
424
-
425
- /*
426
- ratToString =
427
- -- $rat
428
- [
429
- .n toJSON,
430
- "/",
431
- .d toJSON,
432
- " = ",
433
- normalize_rat
434
- <
435
- [.d,.n] GT [.1 toJSON ,"/", .0 toJSON] CONCAT,
436
- {.n 0,[.d,1] EQ 1} [.0 toJSON] CONCAT,
437
- int_rat [.int toJSON,"+",.rat.n toJSON,"/",.rat.d toJSON] CONCAT
438
- >
439
- ] CONCAT
440
- -- $@string
441
- ;
442
-
443
- _rat! = $rat {
444
- () carryOn,
445
- ratToString _log! drop
446
- } .carryOn;
447
-
448
-
449
-
450
- */
451
-
452
- times
package/UU/ta.k DELETED
@@ -1 +0,0 @@
1
- $bit
package/UU/ta.k~ DELETED
File without changes
package/UU/tb.k DELETED
File without changes
package/UU/tc.k DELETED
File without changes