@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.
Files changed (113) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/DOCS/ARCHITECTURE.md +83 -0
  3. package/DOCS/CODECS.md +93 -204
  4. package/DOCS/DICTIONARY.md +42 -1241
  5. package/DOCS/FILE_FORMATS.md +170 -625
  6. package/DOCS/GETTING_STARTED.md +164 -0
  7. package/DOCS/KIR_V1.md +22 -33
  8. package/DOCS/KVM_EXECUTION_MODEL.md +88 -481
  9. package/DOCS/PATTERNS.md +156 -123
  10. package/DOCS/POLYMORPHIC_IR.md +83 -101
  11. package/DOCS/PROFILING_AND_TRACING.md +52 -166
  12. package/DOCS/README.md +35 -0
  13. package/DOCS/REPL.md +100 -371
  14. package/DOCS/TEXTUAL_VALUES.md +53 -81
  15. package/DOCS/TYPE_DERIVATION.md +95 -234
  16. package/DOCS/book-combined.md +3786 -0
  17. package/Examples/arithmetics.k +45 -59
  18. package/Examples/djson.k +102 -112
  19. package/Examples/karatsuba-mult.k +2 -3
  20. package/Examples/list.k +6 -7
  21. package/Examples/nat.k +6 -6
  22. package/Examples/poly.k +45 -45
  23. package/LICENSE +15 -0
  24. package/README.md +72 -410
  25. package/backends/llvm/README.md +75 -97
  26. package/backends/llvm/src/cli.mjs +0 -31
  27. package/backends/wasm/README.md +88 -66
  28. package/backends/wasm/src/cli.mjs +0 -31
  29. package/canonical-pattern.mjs +69 -35
  30. package/codecs/README.md +23 -261
  31. package/codecs/WIRE_FORMAT.md +132 -0
  32. package/core.k +5 -15
  33. package/index.mjs +5 -1
  34. package/named-patterns.mjs +55 -7
  35. package/object.mjs +28 -6
  36. package/objects/README.md +24 -109
  37. package/package.json +8 -3
  38. package/paper/Makefile +17 -0
  39. package/paper/k-paper.pdf +0 -0
  40. package/paper/k-paper.tex +435 -0
  41. package/parser.jison +0 -10
  42. package/parser.mjs +32 -43
  43. package/repl.mjs +79 -101
  44. package/scripts/benchmark-idempotency.mjs +3 -3
  45. package/scripts/run-tests.mjs +1 -1
  46. package/scripts/test-package.mjs +64 -0
  47. package/tests/README.md +66 -46
  48. package/tests/code-derivation/core.mjs +61 -0
  49. package/tests/helpers/cli.mjs +45 -0
  50. package/tests/integration.sh +2 -3
  51. package/tests/test-arithmetics.mjs +47 -237
  52. package/tests/test-djson-methods.mjs +71 -275
  53. package/tests/test-hash-normalization.mjs +22 -1
  54. package/tests/test-karatsuba.mjs +18 -126
  55. package/tests/test-named-patterns.mjs +174 -606
  56. package/tests/test-paper-examples.mjs +90 -0
  57. package/tests/test-poly.mjs +2 -2
  58. package/tests/test-repl-html.mjs +2 -2
  59. package/tests/test-repl.mjs +89 -38
  60. package/tests/test.mjs +27 -68
  61. package/AGENTS.md +0 -35
  62. package/CLAUDE.md +0 -64
  63. package/DOCS/BIG_PICTURE.md +0 -439
  64. package/DOCS/BINARY_CODEC_STATUS_AND_TODO.md +0 -74
  65. package/DOCS/CANONICAL_PATTERNS.md +0 -349
  66. package/DOCS/CODEC_ARCHITECTURE.md +0 -257
  67. package/DOCS/CONVERGENCE.md +0 -123
  68. package/DOCS/DIVERGENCE_DETECTION.md +0 -23
  69. package/DOCS/JSON_APPROACHES_COMPARISON.md +0 -302
  70. package/DOCS/KIR_SPECIALIZATION_DESIGN.md +0 -472
  71. package/DOCS/Makefile +0 -99
  72. package/DOCS/NAMED_PATTERNS.md +0 -340
  73. package/DOCS/OBJECT_FILE_AND_PATTERN.md +0 -343
  74. package/DOCS/PATTERNS_AND_TYPED_VALUES.md +0 -665
  75. package/DOCS/WEB_REPL_REQUIREMENTS.md +0 -136
  76. package/DOCS/book/01-introduction.md +0 -48
  77. package/DOCS/book/02-syntax-and-values.md +0 -110
  78. package/DOCS/book/03-types-as-finite-automata.md +0 -155
  79. package/DOCS/book/04-partial-functions-and-composition.md +0 -145
  80. package/DOCS/book/05-typing-filters-and-normalization.md +0 -109
  81. package/DOCS/book/06-values-in-memory.md +0 -1085
  82. package/DOCS/book/07-the-partial-function-abi.md +0 -143
  83. package/DOCS/book/08-operational-semantics-and-execution.md +0 -196
  84. package/DOCS/book/09-compiler-architecture.md +0 -160
  85. package/DOCS/book/10-from-ast-to-intermediate-representation.md +0 -166
  86. package/DOCS/book/14-canonical-serialization.md +0 -176
  87. package/DOCS/book/appendix-b-incorporating-external-predefined-types-and-functions.md +0 -156
  88. package/DOCS/book/overview.txt +0 -68
  89. package/DOCS/book.md +0 -50
  90. package/Examples/bnat.k +0 -308
  91. package/Examples/buda.k +0 -66
  92. package/Examples/byte.k +0 -35
  93. package/Examples/code.k +0 -43
  94. package/Examples/vhdl_to_k.k +0 -67
  95. package/JOURNAL.md +0 -288
  96. package/TODO.md +0 -107
  97. package/backends/wasm/DOCS/USE-WEB-ASSEMBLY.md +0 -233
  98. package/backends/wasm/TECHNICAL-DEBT.md +0 -58
  99. package/backends/wasm/demo_ieee.mjs +0 -318
  100. package/browser/vfs-data.mjs +0 -21
  101. package/codecs/BINARY_FORMAT.md +0 -53
  102. package/codecs/POLYMORPHIC_BINARY_FORMAT.md +0 -303
  103. package/codecs/pandoc.css +0 -71
  104. package/tests/code-derivation/blowup.mjs +0 -25
  105. package/tests/code-derivation/bnat.mjs +0 -11
  106. package/tests/code-derivation/dot.mjs +0 -37
  107. package/tests/code-derivation/index.mjs +0 -20
  108. package/tests/code-derivation/loop.mjs +0 -17
  109. package/tests/code-derivation/nat.mjs +0 -10
  110. package/tests/code-derivation/not-not.mjs +0 -14
  111. package/tests/code-derivation/product.mjs +0 -22
  112. package/tests/code-derivation/rlz.mjs +0 -13
  113. package/tests/test-fingerprint.mjs +0 -38
@@ -1,33 +1,16 @@
1
1
  -- Examples for defining arithmetic functions natively
2
- swap_yes_no = < /yes |no, /no |yes >;
3
- yes = {}|yes;
4
- no = yes swap_yes_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
- bits? = ? bits[];
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
-
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_ = ? bits_pair[]
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, 0b r} _inv;
57
- _inv = ? inv_state[]
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_ = ? in_order_state[]
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
- /0 remove_leading_zeros,
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, 0b y} _inc nat |'+',
86
+ /'+' {inv x, _ y} _inc nat |'+',
103
87
  minus_one? 0 int,
104
- /'-' {inv x, 0b y} _dec nat |'-'
88
+ /'-' {inv x, _ y} _dec nat |'-'
105
89
  >;
106
90
 
107
- _inc = ? bits_pair[] <
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, 0b y} _dec nat |'+',
116
- /'-' {inv x, 0b y} _inc nat |'-'
99
+ /'+' {inv x, _ y} _dec nat |'+',
100
+ /'-' {inv x, _ y} _inc nat |'-'
117
101
  >;
118
102
 
119
- _dec = ? bits_pair[] <
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, 0b r} _plus_;
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
- ? add_state[]
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, 0b r} _minus_;
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
- ? add_state[]
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
- 0b result,
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
- ? times_state[]
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
- ? div_restoring_state[]
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 = ? pad_state[]
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 = ? align_down_state[]
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, 0b steps} _pad_down then
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, 0b steps} then
284
+ {._y _y, _ steps} then
299
285
  } .then
300
286
  >;
301
287
 
302
- _div_down = ? div_down_state[]
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 = ? bits_pair[]
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
- 0b q
379
+ _ q
394
380
  } _div_down then
395
381
  } .then,
396
382
  {
397
383
  .choice /restore if,
398
- {.x x, .y y, 0 r, 0b q} _div_restoring then
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 = ? bits_pair[] <
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
  >;