@fraczak/k 8.6.1 → 8.7.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 (75) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/DOCS/CODECS.md +20 -14
  3. package/DOCS/FILE_FORMATS.md +53 -12
  4. package/DOCS/GETTING_STARTED.md +15 -8
  5. package/DOCS/PATTERNS.md +10 -0
  6. package/DOCS/REPL.md +12 -7
  7. package/Examples/arithmetics.k +81 -468
  8. package/Examples/ieee-arithmetics.k +37 -33
  9. package/Examples/poly.k +107 -104
  10. package/README.md +19 -11
  11. package/backend-api.mjs +3 -1
  12. package/backends/llvm/README.md +1 -1
  13. package/backends/llvm/bin/k-llvm-build.mjs +5 -5
  14. package/backends/llvm/bin/k-llvm-compile.mjs +4 -4
  15. package/backends/llvm/bin/k-llvm.mjs +6 -6
  16. package/backends/llvm/src/cli.mjs +17 -15
  17. package/backends/llvm/tests/integration.sh +6 -1
  18. package/backends/wasm/README.md +3 -3
  19. package/backends/wasm/bin/k-wasm-compile.mjs +4 -4
  20. package/backends/wasm/bin/k-wasm.mjs +4 -4
  21. package/backends/wasm/src/cli.mjs +12 -10
  22. package/backends/wasm/src/wasm.mjs +4 -4
  23. package/backends/wasm/tests/integration.sh +24 -4
  24. package/browser/shims/fs.mjs +19 -3
  25. package/canonical-pattern.mjs +214 -57
  26. package/compiler.mjs +26 -0
  27. package/core.k +2 -1
  28. package/harness/suites/poly.mjs +14 -30
  29. package/index.mjs +67 -2
  30. package/k.mjs +14 -18
  31. package/kir.mjs +3 -1
  32. package/kvm.mjs +11 -11
  33. package/lib/arithmetics.k +286 -0
  34. package/lib/bit.k +68 -0
  35. package/lib/bits.k +227 -0
  36. package/lib/btrie.k +187 -0
  37. package/lib/djson.k +327 -0
  38. package/lib/ieee.k +709 -0
  39. package/lib/list.k +49 -0
  40. package/lib/string.k +588 -0
  41. package/named-patterns.mjs +2 -14
  42. package/object.mjs +32 -7
  43. package/objects/README.md +9 -3
  44. package/objects/compile.mjs +11 -14
  45. package/package.json +1 -1
  46. package/repl.mjs +24 -5
  47. package/scripts/benchmark-idempotency.mjs +22 -15
  48. package/scripts/benchmark.mjs +29 -10
  49. package/scripts/build-repl-html.mjs +41 -13
  50. package/scripts/perf-ieee.mjs +9 -1
  51. package/scripts/perf-int.mjs +10 -17
  52. package/scripts/perf-multiplication.mjs +9 -15
  53. package/scripts/perf-poly.mjs +16 -32
  54. package/scripts/run-tests.mjs +3 -2
  55. package/scripts/test-package.mjs +13 -2
  56. package/tests/helpers/cli.mjs +19 -3
  57. package/tests/integration.sh +25 -5
  58. package/tests/test-arithmetics.mjs +4 -2
  59. package/tests/test-djson-methods.mjs +21 -12
  60. package/tests/test-ieee-arithmetic.mjs +11 -3
  61. package/tests/test-ieee-text.mjs +19 -4
  62. package/tests/test-k-object.mjs +15 -4
  63. package/tests/test-karatsuba.mjs +1 -1
  64. package/tests/test-kvm-polymorphic.mjs +58 -34
  65. package/tests/test-lib.mjs +117 -0
  66. package/tests/test-library-isolation.mjs +381 -0
  67. package/tests/test-named-patterns.mjs +21 -3
  68. package/tests/test-parse-json.mjs +44 -14
  69. package/tests/test-poly.mjs +10 -4
  70. package/tests/test-repl-html.mjs +18 -7
  71. package/tests/test-repl.mjs +50 -35
  72. package/Examples/djson.k +0 -894
  73. package/Examples/ieee.k +0 -969
  74. package/Examples/karatsuba-mult.k +0 -227
  75. package/Examples/list.k +0 -10
@@ -1,495 +1,108 @@
1
- -- Examples for defining arithmetic functions natively
2
- yes_no[] = <{} yes, {} no>;
3
- yes = {}|yes ? yes_no[];
4
- no = {}|no ? yes_no[];
5
- swap_yes_no = ? yes_no[] < /yes no, yes >;
1
+ -- Examples/arithmetics.k — Higher-level arithmetic examples:
2
+ -- factorial, Fibonacci, and exact rational numbers (rat[])
6
3
 
4
+ ---------------------------------------------------------------
5
+ -- §1. Base Patterns, Primitives & Imported Relations
6
+ ---------------------------------------------------------------
7
7
 
8
8
  bits[] = < {} _, B 0, B 1 > = B;
9
-
10
- _ = {}|_ ?bits[];
11
- 0 = _ |0 ?bits[];
12
-
13
- succ = {inv x, _ y} _inc;
14
- 1 = 0 succ;
15
- 2 = 1 succ;
16
- 3 = 2 succ;
17
- 4 = 3 succ;
18
- 5 = 4 succ;
19
- 6 = 5 succ;
20
- 7 = 6 succ;
21
- 8 = 7 succ;
22
- 9 = 8 succ;
23
- 10 = 9 succ;
24
- 11 = 10 succ;
25
- 12 = 11 succ;
26
-
27
- empty = < /_ yes, no >;
28
- empty? = {empty /yes if, () then} .then;
29
-
30
- concat = {.x inv x, .y y} concat_;
31
- concat_ = ? { bits[] x, bits[] y }
32
- <
33
- {.x empty? if, .y then} .then,
34
- <
35
- {.x /0 x, .y |0 y},
36
- {.x /1 x, .y |1 y}
37
- > concat_
38
- >;
39
- inv = {()x, _ r} _inv;
40
- _inv = ? { bits[] x, bits[] r }
41
- <
42
- {.x empty? if, .r then} .then,
43
- <
44
- {.x /0 x, .r |0 r},
45
- {.x /1 x, .r |1 r}
46
- > _inv
47
- >;
48
-
49
- inOrder = {.x inv x, .y inv y, yes inOrder} inOrder_;
50
- inOrder_ = ?{ bits[] x, bits[] y, < {} yes, {} no > inOrder }
51
- <
52
- {.x </0,/1> if1, .y /_ if2, .inOrder swap_yes_no then} .then,
53
- {.x /_ if, .inOrder then} .then,
54
- <
55
- {.x /0 x, .y /0 y, .inOrder inOrder},
56
- {.x /1 x, .y /1 y, .inOrder inOrder},
57
- {.x /0 x, .y /1 y, .inOrder inOrder},
58
- {.x /1 y, .y /0 x, .inOrder swap_yes_no inOrder}
59
- > inOrder_
60
- >;
61
-
62
- GT = {inOrder /no if, () then} . then;
63
-
64
- EQ = {inOrder /yes if, {.x y, .y x} inOrder /yes and, () then} .then;
65
-
66
- --------------------------------------
67
- int[] = < bits[] "+", bits[] "-" >;
68
- int? = ? int[];
69
- int_pair[] = { int[] x, int[] y };
70
-
71
- remove_leading_zeros = ?bits[] <
72
- /1 |1,
73
- /_ |_,
74
- /0 remove_leading_zeros
75
- >;
76
- nat = remove_leading_zeros <empty? 0, ()>;
77
- int = nat | '+';
78
- abs = </'+',/'-'>;
79
-
80
- pos? = {/'+' remove_leading_zeros /1 if, () then} .then;
81
- neg? = {/'-' if, () then} .then;
82
- zero_int? = {/'+' /0 empty? if, () then} .then;
83
- minus_one? = {/'-' /1 empty? if, () then} .then;
84
-
85
- inc = <
86
- /'+' {inv x, _ y} _inc nat |'+',
87
- minus_one? 0 int,
88
- /'-' {inv x, _ y} _dec nat |'-'
9
+ int[] = < bits[] -, bits[] + >;
10
+
11
+ _ = {} |_ ?bits[];
12
+ 0b = |0 ?bits[];
13
+ 1b = |1 ?bits[];
14
+ 0 = _ 0b;
15
+ 1 = _ 1b;
16
+ pos_int = |+ ?int[];
17
+ neg_int = |- ?int[];
18
+ un_pos = ?int[] /+;
19
+ un_neg = ?int[] /-;
20
+
21
+ # --lib lib/arithmetics.k --use nat int abs pos? neg? zero_int? swap_sign inc dec plus minus times div mod gcd lcm
22
+ nat = @ktrZRefK3zvnUqYYisWBH3MgzcQJD9hs2P5LeSJ7tFrA;
23
+ int = @BgUayEar88hcmcAYM5giDsucyGbtumbaXpyussK8k7vm;
24
+ abs = @Ddc8MjqrgS5t9dPUFQKkBHTLmrcxazshhCwN9HHGtd35;
25
+ pos? = @DExHRdJ6UPsdQEWM6ZenLCUyXLwwZfTRq6CZHf5iahJp;
26
+ neg? = @Hwv3CjPRgtJDb7ZGn7cReF9iZ25LFpjv2MXRBJyUGBij;
27
+ zero_int? = @eYsSuRAeFTejC7W9mLLtxuSeebLuszMGxadnZsg4iuUf;
28
+ swap_sign = @PXVmSLQzpkiLL8qAWLp3MKRAxvwN72hNs64Q2k5hQQS7;
29
+ inc = @FpWRmLNa6guwACtpwYUAuX3usE27WqAKPzQxrVRtyKND;
30
+ dec = @3YRXdfNimGSWfSpCcTzBauTU7HRrjLUFMp4dbRZGhJXv;
31
+ plus = @mfq4QANXe43SuEGb5wM256eKrE7E28BteUgWUxNCLJa6;
32
+ minus = @RdyBXVdSsmFBa7gSBprpFEtktzRrkeFZMrQSk6L4JiBz;
33
+ times = @HCBYSVndz9bsMsBPtFA8auXiMLbJTfELsu9PsXKNSuSw;
34
+ div = @SMg7EvWCmmVaEuLKFzasvSqdyYthsTcGPueypecetysk;
35
+ mod = @g3EchGkuPu29bjTRKZMpxePRSzanyffFvCKgwEBz4SuX;
36
+ gcd = @F32iDHCtp5jvgx2TC6jPJPLSPYX2RyS7EjwxdTMXLCAf;
37
+ lcm = @C2GmSRuqEVawpDA6WBGanePw7Gq3rmzsRqXNb6yQHryL;
38
+
39
+ ---------------------------------------------------------------
40
+ -- §2. Factorial & Fibonacci
41
+ ---------------------------------------------------------------
42
+
43
+ factorial_ = <
44
+ { .n zero_int? if, .acc then } .then,
45
+ { .n pos? dec n, { .n x, .acc y } times acc } factorial_
89
46
  >;
47
+ factorial = { () n, 1 int acc } factorial_;
90
48
 
91
- _inc = ? { bits[] x, bits[] y } <
92
- {.x empty? if, .y |1 then} .then,
93
- {.x /0 x, .y |1 y} concat_,
94
- {.x /1 x, .y |0 y} _inc
95
- >;
96
-
97
- dec = <
98
- zero_int? 1 |'-',
99
- /'+' {inv x, _ y} _dec nat |'+',
100
- /'-' {inv x, _ y} _inc nat |'-'
101
- >;
102
-
103
- _dec = ? { bits[] x, bits[] y } <
104
- {.x /1 x, .y |0 y} concat_,
105
- {.x /0 x, .y |1 y} _dec
106
- >;
107
-
108
-
109
- carry[] = <{} 0,{} 1>;
110
- c0 = {}|0;
111
- c1 = {}|1;
112
-
113
- plus =
114
- <
115
- {.x/'+' inv x, .y/'+' inv y, c0 c } _plus inv nat | '+',
116
- {.x/'-' inv x, .y/'-' inv y, c0 c } _plus inv nat | '-',
117
- {.y/'-' x, .x/'+' y} GT {.x inv x, .y inv y, c0 c} _minus inv nat | '-',
118
- {.x/'+' inv x, .y/'-' inv y, c0 c} _minus inv nat |'+',
119
- {.x/'-' x, .y/'+' y} GT {.x inv x, .y inv y, c0 c} _minus inv nat | '-',
120
- {.y/'+' inv x, .x/'-' inv y, c0 c} _minus inv nat | '+'
121
- >;
122
-
123
- swap_sign = <
124
- zero_int?,
125
- /'+' | '-',
126
- /'-' | '+'
49
+ fib_ = <
50
+ { .n zero_int? if, .pair then } .then,
51
+ {
52
+ .n pos? dec n,
53
+ .pair { plus x, .x y } pair
54
+ } fib_
127
55
  >;
56
+ fib = { () n, { 1 int x, 0 int y } pair } fib_ .x;
128
57
 
129
- minus =
130
- {.x x, .y swap_sign y} plus;
131
-
132
- _plus = {.x x, .y y, .c c, _ r} _plus_;
133
-
134
- _plus_ =
135
- -- least significant bit first addition with carry
136
- ? { bits[] x, bits[] y, carry[] c, bits[] r } = X
137
- <
138
- {.x empty? if, .y empty? and, .c/0 c, .r inv then} .then,
139
- {.x empty? if, .y empty? and, {.r x, 1 r} _inv then} .then,
140
- {.x empty? if, .c/0 and, {.r x, .y r} _inv then} .then,
141
-
142
- <
143
- {.x y, .y empty? x, .c c, .r r},
144
- {<.c/0 0, 1> x, .y y, .x empty? c0 c, .r r},
145
-
146
- {.x /0 x, .y /0 y, .c/0 c0 c, .r |0 r},
147
- {.x /0 x, .y /0 y, .c/1 c0 c, .r |1 r},
58
+ ---------------------------------------------------------------
59
+ -- §3. Rational Numbers (rat[])
60
+ ---------------------------------------------------------------
148
61
 
149
- {.x /0 x, .y /1 y, .c/0 c0 c, .r |1 r},
150
- {.x /0 x, .y /1 y, .c/1 c1 c, .r |0 r},
151
- {.x /1 x, .y /0 y, .c/0 c0 c, .r |1 r},
152
- {.x /1 x, .y /0 y, .c/1 c1 c, .r |0 r},
153
-
154
- {.x /1 x, .y /1 y, .c/0 c1 c, .r |0 r},
155
- {.x /1 x, .y /1 y, .c/1 c1 c, .r |1 r}
156
- > ?X _plus_
157
- >
158
- ;
62
+ rat[] = { int[] n, int[] d };
159
63
 
160
- _minus = {.x x, .y y, .c c, _ r} _minus_;
161
-
162
- _minus_ =
163
- -- least significant bit first substraction with carry
164
- ? { bits[] x, bits[] y, carry[] c, bits[] r } = X
64
+ normalize_rat =
165
65
  <
166
- {.y empty? if, .c/0 and, {.r x, .x r} _inv then} .then,
167
- <
168
- {.x/0 x, .y empty? y, .c/1 c1 c, .r |1 r},
169
- {.x/1 x, .y empty? y, .c/1 c0 c, .r |0 r},
170
-
171
- {.x/0 x, .y/0 y, .c/0 c0 c, .r |0 r},
172
- {.x/0 x, .y/0 y, .c/1 c1 c, .r |1 r},
173
- {.x/0 x, .y/1 y, .c/0 c1 c, .r |1 r},
174
- {.x/0 x, .y/1 y, .c/1 c1 c, .r |0 r},
175
- {.x/1 x, .y/0 y, .c/0 c0 c, .r |1 r},
176
- {.x/1 x, .y/0 y, .c/1 c0 c, .r |0 r},
177
- {.x/1 x, .y/1 y, .c/0 c0 c, .r |0 r},
178
- {.x/1 x, .y/1 y, .c/1 c1 c, .r |1 r}
179
- > ?X _minus_
66
+ { .d neg? if, { .n swap_sign n, .d swap_sign d } then } .then,
67
+ { .d pos? if, { .n ?int[] n, .d d } then } .then
180
68
  >
181
- ;
182
-
183
- sign_plus = {}|'+';
184
- sign_minus = {}|'-';
185
- get_sign = </'+' sign_plus, /'-' sign_minus>;
186
-
187
- times =
69
+ { () x, { .n abs x, .d abs y } gcd int y }
188
70
  {
189
- <
190
- {.x/'+' if, .y /'+' and} sign_plus,
191
- {.x/'-' if, .y /'-' and} sign_plus,
192
- sign_minus
193
- > sign,
194
- {
195
- _ result,
196
- .x abs inv _x,
197
- .y abs y
198
- } _times result
199
- }
200
- <
201
- {.sign/'+' if, .result inv int then} .then,
202
- {.sign/'-' if, .result inv int swap_sign then} .then
203
- >
204
- ;
205
-
206
- _times =
207
- ? { bits[] result, bits[] _x, bits[] y } = X
208
- <
209
- { .y empty? if, .result then } .then,
210
- <
211
- {
212
- .result |0 result,
213
- ._x _x,
214
- .y /0 y
215
- },
216
- {
217
- {._x x, .result |0 y, c0 c } _plus result,
218
- ._x _x,
219
- .y /1 y
220
- }
221
- > ?X _times
222
- >
223
- ;
224
-
225
- // ----------------------------------------------------------------------
226
- // Integer Division: Adaptive Hybrid Algorithm
227
- // ----------------------------------------------------------------------
228
-
229
- shift_in_0 = inv |0 inv nat;
230
- shift_in_1 = inv |1 inv nat;
231
- sub_bits = {.x inv x, .y inv y, c0 c} _minus inv nat;
232
-
233
- _div_restoring =
234
- ? { bits[] x, bits[] y, bits[] q, bits[] r }
235
- <
236
- {.x empty? if, {.q inv int div, .r rem} then} .then,
237
- <
238
- {.x /0 x, .r shift_in_0 r, .y y, .q q},
239
- {.x /1 x, .r shift_in_1 r, .y y, .q q}
240
- >
241
- {
242
- .x x,
243
- .y y,
244
- .q q,
245
- .r r,
246
- {.y x, .r y} inOrder test
247
- }
248
- <
249
- {
250
- .test /yes if,
251
- {.x x, .y y, .q |1 q, {.r x, .y y} sub_bits r} then
252
- } .then,
253
- {.test /no if, {.x x, .y y, .q |0 q, .r r} then} .then
254
- >
255
- _div_restoring
256
- >;
257
-
258
- _pad_down = ? { bits[] x, bits[] steps, bits[] _y }
259
- <
260
- {.x empty? if, {._y _y, .steps steps} then} .then,
261
- {
262
- .x </0, /1> x,
263
- ._y |0 _y,
264
- .steps |0 steps
265
- } _pad_down
266
- >;
267
-
268
- _align_down = ? { bits[] x, bits[] y, bits[] _y }
269
- <
270
- {
271
- .x </0, /1> x,
272
- .y </0, /1> y,
273
- ._y _y
274
- } _align_down,
71
+ { .x.n x, .y y } div .div n,
72
+ { .x.d x, .y y } div .div d
73
+ };
275
74
 
276
- {
277
- .y empty? if,
278
- {.x x, ._y _y, _ steps} _pad_down then
279
- } .then,
75
+ rat = { .n int n, .d int d } normalize_rat;
280
76
 
281
- {
282
- .x empty? if,
283
- .y empty? and,
284
- {._y _y, _ steps} then
285
- } .then
286
- >;
287
-
288
- _div_down = ? { bits[] x, bits[] _y, bits[] steps, bits[] q }
289
- {
290
- .x x,
291
- ._y _y,
292
- .steps steps,
293
- .q q,
294
- ._y inv nat y_scaled
295
- }
77
+ plus_rat =
78
+ { .x x, .y y, { .x.d abs x, .y.d abs y } lcm int lcm }
296
79
  {
297
- .x x,
298
- ._y _y,
299
- .steps steps,
300
- .q q,
301
- .y_scaled y_scaled,
302
- {.y_scaled x, .x y} inOrder test
303
- }
304
- <
305
- {
306
- .test /yes if,
307
- {
308
- {.x int x, .y_scaled int y} minus /+ x,
309
- .q |1 q,
310
- ._y _y,
311
- .steps steps
312
- } then
313
- } .then,
314
- {
315
- .test /no if,
316
- {.x x, .q |0 q, ._y _y, .steps steps} then
317
- } .then
318
- >
319
- <
320
80
  {
321
- .steps empty? if,
322
- {.q inv int div, .x rem} then
323
- } .then,
324
- {
325
- .x x,
326
- ._y /0 _y,
327
- .steps /0 steps,
328
- .q q
329
- } _div_down
330
- >;
331
-
332
- _check_2y = ? { bits[] x, bits[] y }
333
- <
334
- {.x empty? if, {} | down_shift then} .then,
335
- {.y empty? if, {} | restore then} .then,
336
- {.x </0, /1> empty? if, {} | down_shift then} .then,
337
- {
338
- .x </0, /1> </0, /1> x,
339
- .y </0, /1> y
340
- } _check_2y
341
- >;
342
-
343
- div =
344
- {
345
- <
346
- { .x/'+' if, .y get_sign then } .then,
347
- { .x/'-' if, .y swap_sign get_sign then } .then
348
- > sign,
349
- .x abs remove_leading_zeros x_norm,
350
- .y abs remove_leading_zeros y_norm
81
+ { .x.n x, { .lcm x, .x.d y } div .div y } times x,
82
+ { .y.n x, { .lcm x, .y.d y } div .div y } times y
83
+ } plus n,
84
+ .lcm d
351
85
  }
352
- <
353
- {
354
- .x_norm empty? if,
355
- { 0 int div, 0 rem } then
356
- } .then,
357
- {
358
- {.y_norm x, .x_norm y} GT if,
359
- { 0 int div, .x_norm rem } then
360
- } .then,
361
- {
362
- .sign sign,
363
- {
364
- .x_norm x,
365
- .y_norm y,
366
- {.x_norm x, .y_norm y} _check_2y choice
367
- }
368
- <
369
- {
370
- .choice /down_shift if,
371
- {
372
- .x x,
373
- {.x x, .y y, .y inv _y} _align_down aligned
374
- }
375
- {
376
- .x x,
377
- .aligned._y _y,
378
- .aligned.steps steps,
379
- _ q
380
- } _div_down then
381
- } .then,
382
- {
383
- .choice /restore if,
384
- {.x x, .y y, 0 r, _ q} _div_restoring then
385
- } .then
386
- > res
387
- }
388
- <
389
- {.sign/'+' if, {.res.div div, .res.rem rem} then} .then,
390
- { .res.div swap_sign div, .res.rem rem }
391
- >
392
- >;
393
-
394
- mod =
395
- {{.x int x, .y int y} div .rem rem} .rem;
396
-
397
- gcd =
398
- <
399
- {.y x, .x y} GT,
400
- ()
401
- > _gcd
402
- ;
403
-
404
- _gcd = ? { bits[] x, bits[] y } <
405
- { .y remove_leading_zeros empty? if, .x nat then } .then,
406
- { .y x, mod y } _gcd
407
- >;
408
-
409
- lcm =
410
- {{.x int x, gcd int y} div .div x, .y int y} times abs
411
- ;
412
-
413
- factorial = {()n, 1 int acc} factorial_;
414
- factorial_ = <
415
- {.n zero_int? if, .acc then} .then,
416
- {.n pos? dec n, {.n x, .acc y} times acc} factorial_
417
- >;
418
-
419
- fib_ =
420
- <
421
- {.n int zero_int? if, ."fib(n),fib(n.1)" then} .then,
422
- { .n int dec /"+" n,
423
- ."fib(n),fib(n.1)" {plus x, .x y} "fib(n),fib(n.1)"
424
- } fib_
425
- >
426
- ;
427
-
428
- fib = {() n, {1 int x, 0 int y} "fib(n),fib(n.1)"} fib_ .x;
429
-
430
- --------------------
431
-
432
- rat = {.n int n, .d int d } normalize_rat;
86
+ normalize_rat;
433
87
 
434
- normalize_rat =
435
- <
436
- { .d/"-" if,
437
- {
438
- .n swap_sign n,
439
- .d swap_sign d
440
- } then
441
- } .then,
442
- ()
443
- >
444
- {() x, {.n abs x,.d abs y} gcd int y}
88
+ times_rat =
445
89
  {
446
- {.x.n x,.y y} div .div n,
447
- {.x.d x,.y y} div .div d
448
- }
449
- ;
450
-
451
- plus_rat =
452
- {
453
- .x x,
454
- .y y,
455
- {.x.d abs x,.y.d abs y} lcm int lcm
456
- }
457
- {
458
- {
459
- {.x.n x, {.lcm x, .x.d y} div .div y} times x,
460
- {.y.n x, {.lcm x, .y.d y} div .div y} times y
461
- } plus n,
462
- .lcm d
90
+ { .x.n x, .y.n y } times n,
91
+ { .x.d x, .y.d y } times d
463
92
  }
464
- normalize_rat
465
- ;
466
-
467
- times_rat =
468
- {
469
- {.x.n x,.y.n y} times n,
470
- {.x.d x,.y.d y} times d
471
- }
472
93
  normalize_rat;
473
94
 
474
- minus_rat =
475
- {
476
- .x x,
477
- .y {.n swap_sign n, .d d} y
478
- }
95
+ minus_rat =
96
+ { .x x, .y { .n swap_sign n, .d d } y }
479
97
  plus_rat;
480
98
 
481
- inverse_rat =
99
+ inverse_rat =
482
100
  { .d n, .n d }
483
- normalize_rat
484
- ;
485
-
486
- div_rat =
487
- { .x x, .y inverse_rat y} times_rat
488
- ;
101
+ normalize_rat;
489
102
 
490
- int_rat =
491
- { .n n, .d d, {.n x,.d y} div div }
492
- { .div.div int, {.div.rem int n, .d d} rat}
493
- ;
103
+ div_rat =
104
+ { .x x, .y inverse_rat y } times_rat;
494
105
 
495
- ()
106
+ int_rat =
107
+ { .n n, .d d, { .n x, .d y } div div }
108
+ { .div.div int, { .div.rem int n, .d d } normalize_rat rat };
@@ -1,36 +1,44 @@
1
1
  -- IEEE binary64 arithmetic.
2
2
 
3
- -- Defined in 'core.k':
4
- bit[] = @MjusyRWZnVj8TR4BhFCCcMRrQbdKp9gPtC29LT9nZvjg[];
3
+ bit[] = < {} 0, {} 1 >;
4
+ sign[] = < {} "+", {} "-" >;
5
5
 
6
- -- Defined in 'ieee.k':
7
- sign[] = @F7RNp9YbYnUXpfZBQViADi6LZyXYYuiJHc9KckfZtDvm[];
8
- exp11[] = @F2KGfE24DhFGChv6pVAb4pkC9YWvt5izpnnwFDTLfG2h[];
9
- frac52[] = @ziwtSeiWqhQZjbpnB2TpWhSJCDTWfX2QzGvPd3DdVJCZ[];
6
+ 0 = {} |0 ?bit[];
7
+ 1 = {} |1 ?bit[];
8
+ 0? = ?X /0 |0 ?X;
9
+ 1? = ?X /1 |1 ?X;
10
+
11
+ + = {} |"+" ?sign[];
12
+ - = {} |"-" ?sign[];
13
+ +? = ?sign[] /"+" +;
14
+ -? = ?sign[] /"-" -;
15
+
16
+ # --lib lib/bit.k --use not_bit and_bit or_bit xor_bit
17
+ not_bit = @WAu2W28Cay64FmGXCumzmrgbMiEnSjyNUETbSzXtCvEw;
18
+ and_bit = @4iKmE8nmJDxBMREydctSER6UnbA5ufURFJjr4mtKt9AZ;
19
+ or_bit = @FYe6xjtJKkYaiVT6UahWqGsfmihAyvwiqWE4zaEYekkz;
20
+ xor_bit = @QecMdpq96xrRQyYENRgJvqhbYnxyqb66Uxc4mrdWSFpu;
21
+
22
+ # --lib lib/ieee.k --use exp11[] frac52[] float64[]
23
+ exp11[] = @F2KGfE24DhFGChv6pVAb4pkC9YWvt5izpnnwFDTLfG2h[];
24
+ frac52[] = @ziwtSeiWqhQZjbpnB2TpWhSJCDTWfX2QzGvPd3DdVJCZ[];
10
25
  float64[] = @AR4sFGMwXgjj5p7dhi8sJKDWNbrXxLefQUCBRNm5fP6t[];
11
26
 
12
- + = @7rHQSPwedgnq3zPpQgdudpvwzxgbmDXhK9nVRCYciJpX;
13
- +? = @a8hxtzfPEwjBCV4gLQNTcKNLiFYYFdBy5b5pWNNF2qmd;
14
- - = @2Zn83TkHQiN5MgeFFLzYx2a9UYi22MsXzR8rD5VusHsh;
15
- -? = @s8tFQFzJS2jNPsH5unFXxVEKhMivTt9MuMbgWmN2epW6;
16
- 0 = @EHvJE7XLDHFp85gcBQGN5j3EDqUi3ChEdMtByVxF7mk9;
17
- 0? = @AKPd8uefduHxVJUyz3XH7XcaPjgAZDNtiNfiAQhyi3Se;
18
- 1 = @yhn2hGiP2VyS2BeYjrSrtrucugXJNDqWpNEYGMCjQQqV;
19
- 1? = @WwekAdU3rQX7n7ceNKETesMJCz7uZFtBWYYKGhkQ2KXV;
20
- all_ones_exponent? = @pbafpfeff6YNWzPHUgQGjqaCu9Jpgtw8B2iGpiwYY6ci;
21
- exp0 = @ZBjGG7ckLADNGrtPLuJxYVyAQtPYKakzV37GkPhcaRqd;
22
- finite_float64? = @eDmk2p9N36L83zS9SHZFgu3zxpSydaj8vi9GqykygVdJ;
23
- frac0 = @8QkHeZu2V5wT7sETQ5DQ4ERpYxYueZFpKGPRtCmVr2Yd;
24
- inf_float64? = @DsFUGCxGqsbd6YM5W7FrHMjEQW26pnHyg5ShcUngtudk;
25
- nan_float64? = @dWdP2hq5QyUeuH7weaz7xvuLGSp4Yza2i43MH7xUePjX;
26
- neg_inf = @BGVC7XsEfrMXyesxD6dH6nZQj4vDYyerVPG36VZArCw2;
27
- neg_zero = @qfTMB9KAaJPVQVdMjRgCzeRdQCD4Vw5P98ji7ibAVfzz;
28
- negate_float64 = @fg3Zb7t5Vfag3ph2WiiLUu6yq8vbuMrUHMegdgaFcj9b;
29
- pos_inf = @Bg9YTcFFRt3CgJqtye9qpm75Hgtc5AnSSMiSm6btNGyg;
30
- pos_zero = @T7v7wwPqsqif8XVzYcWb4GtDGcc8VDxKUGUPZspZRh4E;
31
- qnan = @H9LK3iPGE9EHQU53rGqu6WEJcsGRMurVpUfgNA79Q2bE;
32
- zero_exponent? = @QsNFmFdBwp8bGMp5fmyjS54hkDBnuvwrW58pXvq3THKs;
33
- zero_float64? = @Y6XSWx63XRjvethPAVcQ7f9dQyEcjRMTuuQbhhDapcpZ;
27
+ # --lib lib/ieee.k --use all_ones_exponent? exp0 finite_float64? frac0 inf_float64? nan_float64? neg_inf neg_zero negate_float64 pos_inf pos_zero qnan zero_exponent? zero_float64?
28
+ all_ones_exponent? = @G4gtCwkTyvUKAjhAKFFj5bfAMfW53t8A6CRxXpTyafcS;
29
+ exp0 = @5CevJXf7TxM9xpnE6d7wUPxCNUCb8hjcyDxK27Qdb99d;
30
+ finite_float64? = @rXgbm83aU9KXZKsnUTKaMa6xE4L4SYr53QKWj55HgTXX;
31
+ frac0 = @cEAdYip46Ce9nsZS8HvRg6iHqeLaAzWBcB5PvWyZHcPz;
32
+ inf_float64? = @EHkVn6SrkQf9xx5j7nxBpA4wrMbrmnkNZGAGVHZESTbE;
33
+ nan_float64? = @C4gMueJKY8pkL4sXhA9Mm4myUdMfMnKLDNsJijr4Gzmw;
34
+ neg_inf = @dDUVHjeQsLRfZ9P2faLv7uHd94w8XUxhug9xJr3CxhGA;
35
+ neg_zero = @KkPWjqrqkhgNXTTeG8J7ZZhBUjVmN9nRnp42j6nRtb8V;
36
+ negate_float64 = @3V9Ve52CnGsvJ9xxHgBwL47L4ac3Xh98bPuLJLHgeU78;
37
+ pos_inf = @8paZ6U6NYfRBsnQHEJQFxtSWsET5WyAZBDUWaZkcPtmz;
38
+ pos_zero = @QTRfm3S4BKsqqt7WDPAPhfNA2dajxcj9iyAtvnqpMcdf;
39
+ qnan = @tS5xBCNSeuX3ahkFgk8wpX8nB6XrH9teHvYhhKxTFu7h;
40
+ zero_exponent? = @NqwhAUhQMnLpZgCevmmMtDFcSLtfxLgkbq85ckwgrKHy;
41
+ zero_float64? = @4p9tjLASFGTZCbW7sha7JzZDBkCNuVwyLhLKiQfBJSNV;
34
42
 
35
43
  // Arithmetic-specific patterns and ripple-carry helpers.
36
44
 
@@ -113,11 +121,7 @@ underflow = {} |underflow;
113
121
  inexact = {} |inexact;
114
122
  div_by_zero = {} |div_by_zero;
115
123
 
116
- // Primitive boolean operators and a 3-input XOR helper.
117
- not_bit = </0 1, 0>;
118
- or_bit = <.x/1 1, .y/1 1, 0> ;
119
- and_bit = <.x/0 0, .y/0 0, 1> ;
120
- xor_bit = <{.x/0 if, .y then} .then, .y not_bit> ;
124
+ // 3-input XOR helper.
121
125
  xor_abc =
122
126
  {{.a x, .b y} xor_bit x, .c y} xor_bit;
123
127